From: Hou Tao <houtao1@xxxxxxxxxx> Now bpf_dynptr is also not usable for map key, so add two test cases to ensure that: one to directly use bpf_dynptr use as map key and another to use a struct with embedded bpf_dynptr as map key. Signed-off-by: Hou Tao <houtao1@xxxxxxxxxx> --- .../testing/selftests/bpf/prog_tests/dynptr.c | 2 + .../testing/selftests/bpf/progs/dynptr_fail.c | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c index 8fc4e6c02bfd..7844f76c4ee6 100644 --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c @@ -20,6 +20,8 @@ static struct { {"ringbuf_invalid_api", "type=mem expected=alloc_mem"}, {"add_dynptr_to_map1", "invalid indirect read from stack"}, {"add_dynptr_to_map2", "invalid indirect read from stack"}, + {"add_dynptr_to_key1", "invalid indirect read from stack"}, + {"add_dynptr_to_key2", "invalid indirect read from stack"}, {"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"}, {"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"}, {"data_slice_use_after_release1", "invalid mem access 'scalar'"}, diff --git a/tools/testing/selftests/bpf/progs/dynptr_fail.c b/tools/testing/selftests/bpf/progs/dynptr_fail.c index b0f08ff024fb..85cc32999e8e 100644 --- a/tools/testing/selftests/bpf/progs/dynptr_fail.c +++ b/tools/testing/selftests/bpf/progs/dynptr_fail.c @@ -35,6 +35,20 @@ struct { __type(value, __u32); } array_map3 SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __type(key, struct bpf_dynptr); + __type(value, __u32); +} hash_map1 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __type(key, struct test_info); + __type(value, __u32); +} hash_map2 SEC(".maps"); + struct sample { int pid; long value; @@ -206,6 +220,35 @@ int add_dynptr_to_map2(void *ctx) return 0; } +/* Can't use a dynptr as key of normal map */ +SEC("?raw_tp") +int add_dynptr_to_key1(void *ctx) +{ + struct bpf_dynptr ptr; + + get_map_val_dynptr(&ptr); + bpf_map_lookup_elem(&hash_map1, &ptr); + + return 0; +} + +/* Can't use a struct with an embedded dynptr as key of normal map */ +SEC("?raw_tp") +int add_dynptr_to_key2(void *ctx) +{ + struct test_info x; + + x.x = 0; + bpf_ringbuf_reserve_dynptr(&ringbuf, val, 0, &x.ptr); + + /* this should fail */ + bpf_map_lookup_elem(&hash_map2, &x); + + bpf_ringbuf_submit_dynptr(&x.ptr, 0); + + return 0; +} + /* A data slice can't be accessed out of bounds */ SEC("?raw_tp") int data_slice_out_of_bounds_ringbuf(void *ctx) -- 2.29.2