Skip to content

Commit

Permalink
selftests/bpf: Accept mem from dynptr in helper funcs
Browse files Browse the repository at this point in the history
This ensures that buffers retrieved from dynptr_data are allowed to be
passed in to helpers that take mem, like bpf_strncmp

Signed-off-by: Daniel Rosenberg <drosen@google.com>
Link: https://lore.kernel.org/r/20230506013134.2492210-6-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 2012c86 commit 798e48f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/prog_tests/dynptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static struct {
{"test_dynptr_is_rdonly", SETUP_SKB_PROG},
{"test_dynptr_clone", SETUP_SKB_PROG},
{"test_dynptr_skb_no_buff", SETUP_SKB_PROG},
{"test_dynptr_skb_strcmp", SETUP_SKB_PROG},
};

static void verify_success(const char *prog_name, enum test_setup_type setup_type)
Expand Down
21 changes: 21 additions & 0 deletions tools/testing/selftests/bpf/progs/dynptr_success.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,24 @@ int test_dynptr_skb_no_buff(struct __sk_buff *skb)

return !!data;
}

SEC("?cgroup_skb/egress")
int test_dynptr_skb_strcmp(struct __sk_buff *skb)
{
struct bpf_dynptr ptr;
char *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, NULL, 10);
if (data) {
bpf_strncmp(data, 10, "foo");
return 1;
}

return 1;
}

0 comments on commit 798e48f

Please sign in to comment.