selftests/bpf: update verifier test for default trusted pointer semantics
Subsystems (4)
PATCH 1/1 selftests/bpf: update verifier test for default trusted pointer semantics
Replace the verifier test for default trusted pointer semantics, which previously relied on BPF kfunc bpf_get_root_mem_cgroup(), with a new test utilizing dedicated BPF kfuncs defined within the bpf_testmod.
bpf_get_root_mem_cgroup() was modified such that it again relies on KF_ACQUIRE semantics, therefore no longer making it a suitable candidate to test BPF verifier default trusted pointer semantics against.
Signed-off-by Matt Bobrowski <[email protected]>
--- .../selftests/bpf/prog_tests/verifier.c | 4 +-- .../bpf/progs/verifier_default_trusted_ptr.c | 29 +++++++++++++++++ .../selftests/bpf/progs/verifier_memcontrol.c | 32 ------------------- .../selftests/bpf/test_kmods/bpf_testmod.c | 18 +++++++++++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 3 ++ 5 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/verifier_default_trusted_ptr.c delete mode 100644 tools/testing/selftests/bpf/progs/verifier_memcontrol.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -2
@@ -30,6 +30,7 @@#include "verifier_ctx.skel.h" #include "verifier_ctx_sk_msg.skel.h" #include "verifier_d_path.skel.h" +#include "verifier_default_trusted_ptr.skel.h" #include "verifier_direct_packet_access.skel.h" #include "verifier_direct_stack_access_wraparound.skel.h" #include "verifier_div0.skel.h"
@@ -61,7 +62,6 @@#include "verifier_masking.skel.h" #include "verifier_may_goto_1.skel.h" #include "verifier_may_goto_2.skel.h" -#include "verifier_memcontrol.skel.h" #include "verifier_meta_access.skel.h" #include "verifier_movsx.skel.h" #include "verifier_mtu.skel.h"
@@ -172,6 +172,7 @@ void test_verifier_const_or(void) { RUN(verifier_const_or); }void test_verifier_ctx (void ) {RUN (verifier_ctx); }void test_verifier_ctx_sk_msg (void ) {RUN (verifier_ctx_sk_msg); }void test_verifier_d_path (void ) {RUN (verifier_d_path); } +void test_verifier_default_trusted_ptr (void ) {RUN_TESTS (verifier_default_trusted_ptr); }void test_verifier_direct_packet_access (void ) {RUN (verifier_direct_packet_access); }void test_verifier_direct_stack_access_wraparound (void ) {RUN (verifier_direct_stack_access_wraparound); }void test_verifier_div0 (void ) {RUN (verifier_div0); }
@@ -203,7 +204,6 @@ void test_verifier_map_ret_val(void) { RUN(verifier_map_ret_val); }void test_verifier_masking (void ) {RUN (verifier_masking); }void test_verifier_may_goto_1 (void ) {RUN (verifier_may_goto_1); }void test_verifier_may_goto_2 (void ) {RUN (verifier_may_goto_2); } -void test_verifier_memcontrol (void ) {RUN (verifier_memcontrol); }void test_verifier_meta_access (void ) {RUN (verifier_meta_access); }void test_verifier_movsx (void ) {RUN (verifier_movsx); }void test_verifier_mul (void ) {RUN (verifier_mul); }
tools/testing/selftests/bpf/progs/verifier_default_trusted_ptr.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2026 Google LLC. +*/ + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +#include "bpf_misc.h" +#include "../test_kmods/bpf_testmod_kfunc.h" + +SEC("syscall") +__success __retval(0) +int test_default_trusted_ptr(void *ctx) +{ +struct prog_test_member *trusted_ptr; + + trusted_ptr =bpf_kfunc_get_default_trusted_ptr_test (); +/* +* Test BPF kfunc bpf_get_default_trusted_ptr_test() returns a +* PTR_TO_BTF_ID | PTR_TRUSTED, therefore it should be accepted when +* passed to a BPF kfunc only accepting KF_TRUSTED_ARGS. +*/ +bpf_kfunc_put_default_trusted_ptr_test (trusted_ptr); +return 0 ; +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_memcontrol.c deleted +0 -32
@@ -1,32 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* -* Copyright 2026 Google LLC. -*/ - -#include <vmlinux.h> -#include <bpf/bpf_helpers.h> -#include <bpf/bpf_tracing.h> -#include "bpf_misc.h" - -SEC("syscall") -__success __retval(0) -int root_mem_cgroup_default_trusted(void *ctx) -{ -unsigned long usage; -struct mem_cgroup *root_mem_cgroup; - - root_mem_cgroup =bpf_get_root_mem_cgroup (); -if (!root_mem_cgroup) -return 1 ; - -/* -* BPF kfunc bpf_get_root_mem_cgroup() returns a PTR_TO_BTF_ID | -* PTR_TRUSTED | PTR_MAYBE_NULL, therefore it should be accepted when -* passed to a BPF kfunc only accepting KF_TRUSTED_ARGS. -*/ - usage =bpf_mem_cgroup_usage (root_mem_cgroup); -__sink (usage); -return 0 ; -} - -char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +18 -0
@@ -254,6 +254,22 @@ __bpf_kfunc int *bpf_kfunc_ret_rcu_test_nostruct(int rdonly_buf_size)return NULL ; } +static struct prog_test_member trusted_ptr; + +__bpf_kfunc struct prog_test_member *bpf_kfunc_get_default_trusted_ptr_test(void) +{ +return &trusted_ptr; +} + +__bpf_kfunc void bpf_kfunc_put_default_trusted_ptr_test(struct prog_test_member *trusted_ptr) +{ +/* +* This BPF kfunc doesn't actually have any put/KF_ACQUIRE +* semantics. We're simply wanting to simulate a BPF kfunc that takes a +* struct prog_test_member pointer as an argument. +*/ +} + __bpf_kfunc struct bpf_testmod_ctx *bpf_testmod_ctx_create (int *err) {
@@ -709,6 +725,8 @@ BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1) BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_2) +BTF_ID_FLAGS (func, bpf_kfunc_get_default_trusted_ptr_test); +BTF_ID_FLAGS (func, bpf_kfunc_put_default_trusted_ptr_test);BTF_KFUNCS_END (bpf_testmod_common_kfunc_ids)BTF_ID_LIST (bpf_testmod_dtor_ids)
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +3 -0
@@ -164,4 +164,7 @@ int *bpf_kfunc_ret_rcu_test_nostruct(int rdonly_buf_size) __ksym; int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym; int bpf_kfunc_multi_st_ops_test_1_impl(struct st_ops_args *args, void *aux__prog) __ksym; +struct prog_test_member *bpf_kfunc_get_default_trusted_ptr_test (void ) __ksym; +void bpf_kfunc_put_default_trusted_ptr_test(struct prog_test_member *trusted_ptr) __ksym; +#endif /* _BPF_TESTMOD_KFUNC_H */
Signature · 1 line
2.52.0.457.g6b5491de43-goog