Skip to content

Commit

Permalink
selftests/bpf: Check overflow in optional buffer
Browse files Browse the repository at this point in the history
This ensures we still reject invalid memory accesses in buffers that are
marked optional.

Signed-off-by: Daniel Rosenberg <drosen@google.com>
Link: https://lore.kernel.org/r/20230506013134.2492210-4-drosen@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Daniel Rosenberg authored and Alexei Starovoitov committed May 6, 2023
1 parent 1ce33b6 commit 3881fdf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tools/testing/selftests/bpf/progs/dynptr_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -1665,3 +1665,23 @@ int clone_xdp_packet_data(struct xdp_md *xdp)

return 0;
}

/* Buffers that are provided must be sufficiently long */
SEC("?cgroup_skb/egress")
__failure __msg("memory, len pair leads to invalid memory access")
int test_dynptr_skb_small_buff(struct __sk_buff *skb)
{
struct bpf_dynptr ptr;
char buffer[8] = {};
__u64 *data;

if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
err = 1;
return 1;
}

/* This may return NULL. SKB may require a buffer */
data = bpf_dynptr_slice(&ptr, 0, buffer, 9);

return !!data;
}

0 comments on commit 3881fdf

Please sign in to comment.