selftests/bpf: Clean up user_ringbuf, cgrp_kfunc, kfunc_dynptr_param tests
Subsystems (3)
PATCH v2 1/2 selftests/bpf: Clean up user_ringbuf, cgrp_kfunc, kfunc_dynptr_param tests
Clean up user_ringbuf, cgrp_kfunc, and kfunc_dynptr_param tests to use the generic verification tester for checking verifier rejections. The generic verification tester uses btf_decl_tag-based annotations for verifying that the tests fail with the expected log messages.
--- .../selftests/bpf/prog_tests/cgrp_kfunc.c | 69 +----------------- .../bpf/prog_tests/kfunc_dynptr_param.c | 72 ++++--------------- .../selftests/bpf/prog_tests/user_ringbuf.c | 62 +--------------- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 17 ++++- .../bpf/progs/test_kfunc_dynptr_param.c | 4 ++ .../selftests/bpf/progs/user_ringbuf_fail.c | 31 +++++--- 6 files changed, 58 insertions(+), 197 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c +1 -68
@@ -8,9 +8,6 @@#include "cgrp_kfunc_failure.skel.h" #include "cgrp_kfunc_success.skel.h" -static size_t log_buf_sz =1 <<20 ;/* 1 MB */ -static char obj_log_buf[1048576 ]; -static struct cgrp_kfunc_success *open_load_cgrp_kfunc_skel (void ) {struct cgrp_kfunc_success *skel;
@@ -89,65 +86,6 @@ static const char * const success_tests[] = { "test_cgrp_get_ancestors", }; -static struct { -const char *prog_name; -const char *expected_err_msg; -} failure_tests[] = { - {"cgrp_kfunc_acquire_untrusted" ,"Possibly NULL pointer passed to trusted arg0" }, - {"cgrp_kfunc_acquire_fp" ,"arg#0 pointer type STRUCT cgroup must point" }, - {"cgrp_kfunc_acquire_unsafe_kretprobe" ,"reg type unsupported for arg#0 function" }, - {"cgrp_kfunc_acquire_trusted_walked" ,"R1 must be referenced or trusted" }, - {"cgrp_kfunc_acquire_null" ,"Possibly NULL pointer passed to trusted arg0" }, - {"cgrp_kfunc_acquire_unreleased" ,"Unreleased reference" }, - {"cgrp_kfunc_get_non_kptr_param" ,"arg#0 expected pointer to map value" }, - {"cgrp_kfunc_get_non_kptr_acquired" ,"arg#0 expected pointer to map value" }, - {"cgrp_kfunc_get_null" ,"arg#0 expected pointer to map value" }, - {"cgrp_kfunc_xchg_unreleased" ,"Unreleased reference" }, - {"cgrp_kfunc_get_unreleased" ,"Unreleased reference" }, - {"cgrp_kfunc_release_untrusted" ,"arg#0 is untrusted_ptr_or_null_ expected ptr_ or socket" }, - {"cgrp_kfunc_release_fp" ,"arg#0 pointer type STRUCT cgroup must point" }, - {"cgrp_kfunc_release_null" ,"arg#0 is ptr_or_null_ expected ptr_ or socket" }, - {"cgrp_kfunc_release_unacquired" ,"release kernel function bpf_cgroup_release expects" }, -}; - -static void verify_fail (const char *prog_name,const char *expected_err_msg) -{ -LIBBPF_OPTS (bpf_object_open_opts, opts); -struct cgrp_kfunc_failure *skel; -int err, i; - - opts.kernel_log_buf = obj_log_buf; - opts.kernel_log_size = log_buf_sz; - opts.kernel_log_level =1 ; - - skel =cgrp_kfunc_failure__open_opts (&opts); -if (!ASSERT_OK_PTR (skel,"cgrp_kfunc_failure__open_opts" )) - goto cleanup; - -for (i =0 ; i <ARRAY_SIZE (failure_tests); i++) { -struct bpf_program *prog; -const char *curr_name = failure_tests[i].prog_name; - - prog =bpf_object__find_program_by_name (skel->obj, curr_name); -if (!ASSERT_OK_PTR (prog,"bpf_object__find_program_by_name" )) - goto cleanup; - -bpf_program__set_autoload (prog, !strcmp (curr_name, prog_name)); - } - - err =cgrp_kfunc_failure__load (skel); -if (!ASSERT_ERR (err,"unexpected load success" )) - goto cleanup; - -if (!ASSERT_OK_PTR (strstr (obj_log_buf, expected_err_msg),"expected_err_msg" )) { -fprintf (stderr,"Expected err_msg: %s\n" , expected_err_msg); -fprintf (stderr,"Verifier output: %s\n" , obj_log_buf); - } - -cleanup: -cgrp_kfunc_failure__destroy (skel); -} -void test_cgrp_kfunc (void ) {int i, err;
@@ -163,12 +101,7 @@ void test_cgrp_kfunc(void)run_success_test (success_tests[i]); } -for (i =0 ; i <ARRAY_SIZE (failure_tests); i++) { -if (!test__start_subtest (failure_tests[i].prog_name)) -continue ; - -verify_fail (failure_tests[i].prog_name, failure_tests[i].expected_err_msg); - } +RUN_TESTS (cgrp_kfunc_failure); cleanup:cleanup_cgroup_environment ();
tools/testing/selftests/bpf/prog_tests/kfunc_dynptr_param.c +15 -57
123 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/user_ringbuf.c +1 -61
84 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgrp_kfunc_failure.c +16 -1
@@ -5,6 +5,7 @@#include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_misc.h" #include "cgrp_kfunc_common.h" char _license[] SEC("license") = "GPL";
@@ -28,6 +29,7 @@ static struct __cgrps_kfunc_map_value *insert_lookup_cgrp(struct cgroup *cgrp) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("Possibly NULL pointer passed to trusted arg0") int BPF_PROG(cgrp_kfunc_acquire_untrusted, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -45,6 +47,7 @@ int BPF_PROG(cgrp_kfunc_acquire_untrusted, struct cgroup *cgrp, const char *path } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 pointer type STRUCT cgroup must point") int BPF_PROG(cgrp_kfunc_acquire_fp, struct cgroup *cgrp, const char *path) {struct cgroup *acquired, *stack_cgrp = (struct cgroup *)&path;
@@ -57,6 +60,7 @@ int BPF_PROG(cgrp_kfunc_acquire_fp, struct cgroup *cgrp, const char *path) } SEC("kretprobe/cgroup_destroy_locked") +__failure __msg("reg type unsupported for arg#0 function") int BPF_PROG(cgrp_kfunc_acquire_unsafe_kretprobe, struct cgroup *cgrp) {struct cgroup *acquired;
@@ -69,6 +73,7 @@ int BPF_PROG(cgrp_kfunc_acquire_unsafe_kretprobe, struct cgroup *cgrp) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("cgrp_kfunc_acquire_trusted_walked") int BPF_PROG(cgrp_kfunc_acquire_trusted_walked, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -80,8 +85,8 @@ int BPF_PROG(cgrp_kfunc_acquire_trusted_walked, struct cgroup *cgrp, const charreturn 0 ; } - SEC("tp_btf/cgroup_mkdir") +__failure __msg("Possibly NULL pointer passed to trusted arg0") int BPF_PROG(cgrp_kfunc_acquire_null, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -96,6 +101,7 @@ int BPF_PROG(cgrp_kfunc_acquire_null, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("Unreleased reference") int BPF_PROG(cgrp_kfunc_acquire_unreleased, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -108,6 +114,7 @@ int BPF_PROG(cgrp_kfunc_acquire_unreleased, struct cgroup *cgrp, const char *pat } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 expected pointer to map value") int BPF_PROG(cgrp_kfunc_get_non_kptr_param, struct cgroup *cgrp, const char *path) {struct cgroup *kptr;
@@ -123,6 +130,7 @@ int BPF_PROG(cgrp_kfunc_get_non_kptr_param, struct cgroup *cgrp, const char *pat } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 expected pointer to map value") int BPF_PROG(cgrp_kfunc_get_non_kptr_acquired, struct cgroup *cgrp, const char *path) {struct cgroup *kptr, *acquired;
@@ -141,6 +149,7 @@ int BPF_PROG(cgrp_kfunc_get_non_kptr_acquired, struct cgroup *cgrp, const char * } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 expected pointer to map value") int BPF_PROG(cgrp_kfunc_get_null, struct cgroup *cgrp, const char *path) {struct cgroup *kptr;
@@ -156,6 +165,7 @@ int BPF_PROG(cgrp_kfunc_get_null, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("Unreleased reference") int BPF_PROG(cgrp_kfunc_xchg_unreleased, struct cgroup *cgrp, const char *path) {struct cgroup *kptr;
@@ -175,6 +185,7 @@ int BPF_PROG(cgrp_kfunc_xchg_unreleased, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("Unreleased reference") int BPF_PROG(cgrp_kfunc_get_unreleased, struct cgroup *cgrp, const char *path) {struct cgroup *kptr;
@@ -194,6 +205,7 @@ int BPF_PROG(cgrp_kfunc_get_unreleased, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 is untrusted_ptr_or_null_ expected ptr_ or socket") int BPF_PROG(cgrp_kfunc_release_untrusted, struct cgroup *cgrp, const char *path) {struct __cgrps_kfunc_map_value *v;
@@ -209,6 +221,7 @@ int BPF_PROG(cgrp_kfunc_release_untrusted, struct cgroup *cgrp, const char *path } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 pointer type STRUCT cgroup must point") int BPF_PROG(cgrp_kfunc_release_fp, struct cgroup *cgrp, const char *path) {struct cgroup *acquired = (struct cgroup *)&path;
@@ -220,6 +233,7 @@ int BPF_PROG(cgrp_kfunc_release_fp, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("arg#0 is ptr_or_null_ expected ptr_ or socket") int BPF_PROG(cgrp_kfunc_release_null, struct cgroup *cgrp, const char *path) {struct __cgrps_kfunc_map_value local, *v;
@@ -251,6 +265,7 @@ int BPF_PROG(cgrp_kfunc_release_null, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") +__failure __msg("release kernel function bpf_cgroup_release expects") int BPF_PROG(cgrp_kfunc_release_unacquired, struct cgroup *cgrp, const char *path) {/* Cannot release trusted cgroup pointer which was not acquired. */
tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c +4 -0
@@ -10,6 +10,7 @@#include <errno.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> +#include "bpf_misc.h" extern struct bpf_key *bpf_lookup_system_key (__u64 id) __ksym; extern void bpf_key_put(struct bpf_key *key) __ksym;
@@ -19,6 +20,7 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,struct { __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 4096); } ringbuf SEC(".maps");struct {
@@ -33,6 +35,7 @@ int err, pid; char _license[] SEC("license") = "GPL"; SEC("?lsm.s/bpf") +__failure __msg("cannot pass in dynptr at an offset=-8") int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size) {unsigned long val;
@@ -42,6 +45,7 @@ int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size) } SEC("?lsm.s/bpf") +__failure __msg("arg#0 expected pointer to stack or dynptr_ptr") int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size) {unsigned long val;
tools/testing/selftests/bpf/progs/user_ringbuf_fail.c +21 -10
@@ -16,6 +16,7 @@ struct sample {struct { __uint(type, BPF_MAP_TYPE_USER_RINGBUF); + __uint(max_entries, 4096); } user_ringbuf SEC(".maps");struct {
@@ -39,7 +40,8 @@ bad_access1(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to read before the pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("negative offset dynptr_ptr ptr")int user_ringbuf_callback_bad_access1 (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, bad_access1,NULL ,0 );
@@ -61,7 +63,8 @@ bad_access2(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to read past the end of the pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("dereference of modified dynptr_ptr ptr")int user_ringbuf_callback_bad_access2 (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, bad_access2,NULL ,0 );
@@ -80,7 +83,8 @@ write_forbidden(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to write to that pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("invalid mem access 'dynptr_ptr'")int user_ringbuf_callback_write_forbidden (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, write_forbidden,NULL ,0 );
@@ -99,7 +103,8 @@ null_context_write(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to write to that pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("invalid mem access 'scalar'")int user_ringbuf_callback_null_context_write (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, null_context_write,NULL ,0 );
@@ -120,7 +125,8 @@ null_context_read(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to write to that pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("invalid mem access 'scalar'")int user_ringbuf_callback_null_context_read (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, null_context_read,NULL ,0 );
@@ -139,7 +145,8 @@ try_discard_dynptr(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to read past the end of the pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("cannot release unowned const bpf_dynptr")int user_ringbuf_callback_discard_dynptr (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, try_discard_dynptr,NULL ,0 );
@@ -158,7 +165,8 @@ try_submit_dynptr(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to read past the end of the pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("cannot release unowned const bpf_dynptr")int user_ringbuf_callback_submit_dynptr (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, try_submit_dynptr,NULL ,0 );
@@ -175,7 +183,8 @@ invalid_drain_callback_return(struct bpf_dynptr *dynptr, void *context)/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should * not be able to write to that pointer. */ -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("At callback return the register R0 has value")int user_ringbuf_callback_invalid_return (void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, invalid_drain_callback_return,NULL ,0 );
@@ -197,14 +206,16 @@ try_reinit_dynptr_ringbuf(struct bpf_dynptr *dynptr, void *context)return 0 ; } -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("Dynptr has to be an uninitialized dynptr") int user_ringbuf_callback_reinit_dynptr_mem(void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, try_reinit_dynptr_mem,NULL ,0 );return 0 ; } -SEC("?raw_tp/") +SEC("?raw_tp") +__failure __msg("Dynptr has to be an uninitialized dynptr") int user_ringbuf_callback_reinit_dynptr_ringbuf(void *ctx) {bpf_user_ringbuf_drain (&user_ringbuf, try_reinit_dynptr_ringbuf,NULL ,0 );
Signature · 1 line
2.30.2
PATCH v2 2/2 selftests/bpf: Clean up dynptr prog_tests
Clean up prog_tests/dynptr.c by removing the unneeded "expected_err_msg" in the dynptr_tests struct, which is a remnant from converting the fail tests cases to use the generic verification tester.
--- .../testing/selftests/bpf/prog_tests/dynptr.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +7 -11
@@ -5,14 +5,10 @@#include "dynptr_fail.skel.h" #include "dynptr_success.skel.h" -static struct { -const char *prog_name; -const char *expected_err_msg; -} dynptr_tests[] = { -/* success cases */ - {"test_read_write" ,NULL }, - {"test_data_slice" ,NULL }, - {"test_ringbuf" ,NULL }, +static const char *const success_tests[] = { +"test_read_write" , +"test_data_slice" , +"test_ringbuf" , };static void verify_success (const char *prog_name)
@@ -53,11 +49,11 @@ void test_dynptr(void) {int i; -for (i =0 ; i <ARRAY_SIZE (dynptr_tests); i++) { -if (!test__start_subtest (dynptr_tests[i].prog_name)) +for (i =0 ; i <ARRAY_SIZE (success_tests); i++) { +if (!test__start_subtest (success_tests[i]))continue ; -verify_success (dynptr_tests[i].prog_name); +verify_success (success_tests[i]); }RUN_TESTS (dynptr_fail);
Signature · 1 line
2.30.2
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Mon, 13 Feb 2023 21:13:31 -0800 you wrote:
Clean up
user_ringbuf,cgrp_kfunc, andkfunc_dynptr_paramtests to use the generic verification tester for checking verifier rejections. The generic verification tester uses btf_decl_tag-based annotations for verifying that the tests fail with the expected log messages.Signed-off-by Joanne Koong <[email protected]>Acked-by David Vernet <[email protected]>Reviewed-by Roberto Sassu <[email protected]>[...]
Here is the summary with links:
- [v2,bpf-next,1/2] selftests/bpf: Clean up user_ringbuf, cgrp_kfunc, kfunc_dynptr_param tests
https://git.kernel.org/bpf/bpf-next/c/8032cad10302 - [v2,bpf-next,2/2] selftests/bpf: Clean up dynptr prog_tests https://git.kernel.org/bpf/bpf-next/c/50a7cedb150a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html