Skip to content

Commit

Permalink
style: Improve error handling in buffer_append_skb_bytes
Browse files Browse the repository at this point in the history
* Remove the unnecessary check (`offset >= skb->len`). Apparently,
  verifier doesn't expect it.
* Improve error messages.
  • Loading branch information
vadorovsky committed May 21, 2024
1 parent 7f7678c commit aa4da82
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions crates/bpf-builder/include/buffer.bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,31 @@ static __always_inline void buffer_append_skb_bytes(struct buffer *buffer,
__u32 offset) {
int pos = (index->start + index->len);
if (pos >= HALF_BUFFER_MASK) {
LOG_ERROR("trying to write over half: %d+%d", index->start, index->len);
return;
}
if (offset >= skb->len) {
LOG_ERROR("invalid offset: %zu, greater than packet length: %zu", offset, skb->len);
LOG_ERROR(
"Attempting to write beyond buffer capacity. Calculated position: %d (start: %d + length: %d). Maximum allowed: %d.",
pos, index->start, index->len, HALF_BUFFER_MASK
);
return;
}

s32 len = skb->len - offset;
if (len >= HALF_BUFFER_MASK)
if (len >= HALF_BUFFER_MASK) {
LOG_ERROR("Payload size (%d) exceeds buffer capacity (%d).",
len, HALF_BUFFER_MASK);
return;
if (len <= 0)
}

if (len <= 0) {
LOG_ERROR("Invalid offset (%zu) exceeding the packet length (%zu).",
offset, len, skb->len);
return;
}

int r = bpf_skb_load_bytes(skb, offset, &((char *)buffer->buffer)[pos],
len);

if (r < 0) {
LOG_ERROR("reading failure: %d", r);
LOG_ERROR("Could not read the network packet payload: %d", r);
return;
}

Expand Down

0 comments on commit aa4da82

Please sign in to comment.