Skip to content

Commit 6db59c4

Browse files
juntongdengAlexei Starovoitov
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/AM6PR03MB5848CB6F0D4D9068669A905B99952@AM6PR03MB5848.eurprd03.prod.outlook.com Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f633919 commit 6db59c4

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ __bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr,
183183
{
184184
}
185185

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+
186200
__bpf_kfunc struct bpf_testmod_ctx *
187201
bpf_testmod_ctx_create(int *err)
188202
{
@@ -541,6 +555,9 @@ BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
541555
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value)
542556
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
543557
BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
558+
BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_nonzero_offset_test, KF_ACQUIRE)
559+
BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_zero_offset_test, KF_ACQUIRE)
560+
BTF_ID_FLAGS(func, bpf_kfunc_nested_release_test, KF_RELEASE)
544561
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
545562
BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
546563
BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,8 @@ void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, struct bpf_dynptr *ptr__nulla
144144
struct bpf_testmod_ctx *bpf_testmod_ctx_create(int *err) __ksym;
145145
void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx) __ksym;
146146

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+
147151
#endif /* _BPF_TESTMOD_KFUNC_H */

tools/testing/selftests/bpf/prog_tests/nested_trust.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
#include <test_progs.h>
55
#include "nested_trust_failure.skel.h"
66
#include "nested_trust_success.skel.h"
7+
#include "nested_acquire.skel.h"
78

89
void test_nested_trust(void)
910
{
1011
RUN_TESTS(nested_trust_success);
1112
RUN_TESTS(nested_trust_failure);
13+
14+
if (env.has_testmod)
15+
RUN_TESTS(nested_acquire);
1216
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)