Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add test for zero offset or non-zero offset pointers as KF_ACQUIRE kfuncs argument

This patch adds test cases for zero offset (implicit cast) or non-zero
offset pointer as KF_ACQUIRE kfuncs argument. Currently KF_ACQUIRE
kfuncs should support passing in pointers like &sk->sk_write_queue
(non-zero offset) or &sk->__sk_common (zero offset) and not be rejected
by the verifier.

Signed-off-by: Juntong Deng <juntong.deng@outlook.com>
Link: https://lore.kernel.org/r/AM6PR03MB5848CB6F0D4D9068669A905B99952@AM6PR03MB5848.eurprd03.prod.outlook.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Juntong Deng and committed by
Alexei Starovoitov
6db59c49 f633919d

+58
+17
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
··· 183 183 { 184 184 } 185 185 186 + __bpf_kfunc struct sk_buff *bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head *ptr) 187 + { 188 + return NULL; 189 + } 190 + 191 + __bpf_kfunc struct sk_buff *bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common *ptr) 192 + { 193 + return NULL; 194 + } 195 + 196 + __bpf_kfunc void bpf_kfunc_nested_release_test(struct sk_buff *ptr) 197 + { 198 + } 199 + 186 200 __bpf_kfunc struct bpf_testmod_ctx * 187 201 bpf_testmod_ctx_create(int *err) 188 202 { ··· 555 541 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value) 556 542 BTF_ID_FLAGS(func, bpf_kfunc_common_test) 557 543 BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test) 544 + BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_nonzero_offset_test, KF_ACQUIRE) 545 + BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_zero_offset_test, KF_ACQUIRE) 546 + BTF_ID_FLAGS(func, bpf_kfunc_nested_release_test, KF_RELEASE) 558 547 BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL) 559 548 BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE) 560 549 BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
+4
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h
··· 144 144 struct bpf_testmod_ctx *bpf_testmod_ctx_create(int *err) __ksym; 145 145 void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx) __ksym; 146 146 147 + struct sk_buff *bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head *ptr) __ksym; 148 + struct sk_buff *bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common *ptr) __ksym; 149 + void bpf_kfunc_nested_release_test(struct sk_buff *ptr) __ksym; 150 + 147 151 #endif /* _BPF_TESTMOD_KFUNC_H */
+4
tools/testing/selftests/bpf/prog_tests/nested_trust.c
··· 4 4 #include <test_progs.h> 5 5 #include "nested_trust_failure.skel.h" 6 6 #include "nested_trust_success.skel.h" 7 + #include "nested_acquire.skel.h" 7 8 8 9 void test_nested_trust(void) 9 10 { 10 11 RUN_TESTS(nested_trust_success); 11 12 RUN_TESTS(nested_trust_failure); 13 + 14 + if (env.has_testmod) 15 + RUN_TESTS(nested_acquire); 12 16 }
+33
tools/testing/selftests/bpf/progs/nested_acquire.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include <vmlinux.h> 4 + #include <bpf/bpf_tracing.h> 5 + #include <bpf/bpf_helpers.h> 6 + #include "bpf_misc.h" 7 + #include "../bpf_testmod/bpf_testmod_kfunc.h" 8 + 9 + char _license[] SEC("license") = "GPL"; 10 + 11 + SEC("tp_btf/tcp_probe") 12 + __success 13 + int BPF_PROG(test_nested_acquire_nonzero, struct sock *sk, struct sk_buff *skb) 14 + { 15 + struct sk_buff *ptr; 16 + 17 + ptr = bpf_kfunc_nested_acquire_nonzero_offset_test(&sk->sk_write_queue); 18 + 19 + bpf_kfunc_nested_release_test(ptr); 20 + return 0; 21 + } 22 + 23 + SEC("tp_btf/tcp_probe") 24 + __success 25 + int BPF_PROG(test_nested_acquire_zero, struct sock *sk, struct sk_buff *skb) 26 + { 27 + struct sk_buff *ptr; 28 + 29 + ptr = bpf_kfunc_nested_acquire_zero_offset_test(&sk->__sk_common); 30 + 31 + bpf_kfunc_nested_release_test(ptr); 32 + return 0; 33 + }