check bpf_dummy_struct_ops program params for test runs
Subsystems (4)
Eduard Zingerman
maintainercover check bpf_dummy_struct_ops program params for test runs
When doing BPF_PROG_TEST_RUN for bpf_dummy_struct_ops programs, execution should be rejected when NULL is passed for non-nullable params, because for such params verifier assumes that such params are never NULL and thus might optimize out NULL checks.
This problem was reported by Jose E. Marchesi in off-list discussion. The code generated by GCC for dummy_st_ops_success/test_1() function differs from LLVM variant in a way that allows verifier to remove the NULL check. The test dummy_st_ops/dummy_init_ret_value actually sets the 'state' parameter to NULL, thus GCC-generated version of the test triggers NULL pointer dereference when BPF program is executed.
This patch-set addresses the issue in the following steps:
- patch #1 marks bpf_dummy_struct_ops.test_1 parameter as nullable, for verifier to have correct assumptions about test_1() programs;
- patch #2 modifies dummy_st_ops/dummy_init_ret_value to trigger NULL dereference with both GCC and LLVM (if patch #1 is not applied);
- patch #3 adjusts a few dummy_st_ops test cases to avoid passing NULL for 'state' parameter of test_2() and test_sleepable() functions, as parameters of these functions are not marked as nullable;
- patch #4 adjusts bpf_dummy_struct_ops to reject test execution of programs if NULL is passed for non-nullable parameter;
- patch #5 adds a test to verify logic from patch #4.
Eduard Zingerman (5):
bpf: mark bpf_dummy_struct_ops.test_1 parameter as nullable selftests/bpf: adjust dummy_st_ops_success to detect additional error selftests/bpf: do not pass NULL for non-nullable params in
dummy_st_ops
bpf: check bpf_dummy_struct_ops program params for test runs selftests/bpf: dummy_st_ops should reject 0 for non-nullable params
net/bpf/bpf_dummy_struct_ops.c | 55 ++++++++++++++++++- .../selftests/bpf/prog_tests/dummy_st_ops.c | 34 +++++++++++- .../bpf/progs/dummy_st_ops_success.c | 15 ++++- 3 files changed, 96 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerPATCH 1/5 bpf: mark bpf_dummy_struct_ops.test_1 parameter as nullable
Test case dummy_st_ops/dummy_init_ret_value passes NULL as the first parameter of the test_1() function. Mark this parameter as nullable to make verifier aware of such possibility.
Otherwise, NULL check in the test_1() code:
SEC("struct_ops/test_1")
int BPF_PROG(test_1, struct bpf_dummy_ops_state *state)
{
if (!state)
return ...;
... access state ...
}Might be removed by verifier, thus triggering NULL pointer dereference under certain conditions.
--- net/bpf/bpf_dummy_struct_ops.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/bpf/bpf_dummy_struct_ops.c +2 -2
@@ -232,7 +232,7 @@ static void bpf_dummy_unreg(void *kdata) { } -static int bpf_dummy_test_1 (struct bpf_dummy_ops_state *cb) +static int bpf_dummy_ops__test_1 (struct bpf_dummy_ops_state *cb__nullable) {return 0 ; }
@@ -249,7 +249,7 @@ static int bpf_dummy_test_sleepable(struct bpf_dummy_ops_state *cb) }static struct bpf_dummy_ops __bpf_bpf_dummy_ops = { - .test_1 = bpf_dummy_test_1, + .test_1 = bpf_dummy_ops__test_1, .test_2 = bpf_dummy_test_2, .test_sleepable = bpf_dummy_test_sleepable, };
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerPATCH 2/5 selftests/bpf: adjust dummy_st_ops_success to detect additional error
In reply to Eduard ZingermanAs reported by Jose E. Marchesi in off-list discussion, GCC and LLVM generate slightly different code for dummy_st_ops_success/test_1():
SEC("struct_ops/test_1")
int BPF_PROG(test_1, struct bpf_dummy_ops_state *state)
{
int ret; if (!state)
return 0xf2f3f4f5;
ret = state->val; state->val = 0x5a; return ret; }
GCC-generated LLVM-generated ---------------------------- --------------------------- 0: r1 = *(u64 *)(r1 + 0x0) 0: w0 = -0xd0c0b0b 1: if r1 == 0x0 goto 5f 1: r1 = *(u64 *)(r1 + 0x0) 2: r0 = *(s32 *)(r1 + 0x0) 2: if r1 == 0x0 goto 6f 3: *(u32 *)(r1 + 0x0) = 0x5a 3: r0 = *(u32 *)(r1 + 0x0) 4: exit 4: w2 = 0x5a 5: r0 = -0xd0c0b0b 5: *(u32 *)(r1 + 0x0) = r2 6: exit 6: exit
If the 'state' argument is not marked as nullable in net/bpf/bpf_dummy_struct_ops.c, the verifier would assume that 'r1 == 0x0' is never true:
- for the GCC version, this means that instructions #5-6 would be marked as dead and removed;
- for the LLVM version, all instructions would be marked as live.
The test dummy_st_ops/dummy_init_ret_value actually sets the 'state' parameter to NULL.
Therefore, when the 'state' argument is not marked as nullable, the GCC-generated version of the code would trigger a NULL pointer dereference at instruction #3.
This patch updates the test_1() test case to always follow a shape similar to the GCC-generated version above, in order to verify whether the 'state' nullability is marked correctly.
--- .../selftests/bpf/progs/dummy_st_ops_success.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/dummy_st_ops_success.c +11 -2
@@ -11,8 +11,17 @@ int BPF_PROG(test_1, struct bpf_dummy_ops_state *state) {int ret; -if (!state) -return 0xf2f3f4f5 ; +/* Check that 'state' nullable status is detected correctly. +* If 'state' argument would be assumed non-null by verifier +* the code below would be deleted as dead (which it shouldn't). +* Hide it from the compiler behind 'asm' block to avoid +* unnecessary optimizations. +*/ + asmvolatile ( +"if %[state] != 0 goto +2;" +"r0 = 0xf2f3f4f5;" +"exit;" + ::[state]"p" (state)); ret = state->val; state->val =0x5a ;
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerPATCH 3/5 selftests/bpf: do not pass NULL for non-nullable params in dummy_st_ops
In reply to Eduard Zingermandummy_st_ops.test_2 and dummy_st_ops.test_sleepable do not have their 'state' parameter marked as nullable. Update dummy_st_ops.c to avoid passing NULL for such parameters, as the next patch would allow kernel to enforce this restriction.
--- tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c | 7 +++++-- tools/testing/selftests/bpf/progs/dummy_st_ops_success.c | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c +5 -2
@@ -98,7 +98,8 @@ static void test_dummy_init_ptr_arg(void)static void test_dummy_multiple_args (void ) { -__u64 args[5 ] = {0 ,-100 ,0x8a5f ,'c' ,0x1234567887654321ULL }; +struct bpf_dummy_ops_state st = {7 }; +__u64 args[5 ] = {(__u64)&st,-100 ,0x8a5f ,'c' ,0x1234567887654321ULL }; LIBBPF_OPTS(bpf_test_run_opts, attr, .ctx_in = args, .ctx_size_in = sizeof(args),
@@ -115,6 +116,7 @@ static void test_dummy_multiple_args(void) fd =bpf_program__fd (skel->progs.test_2); err =bpf_prog_test_run_opts (fd, &attr);ASSERT_OK (err,"test_run" ); + args[0 ] =7 ;for (i =0 ; i <ARRAY_SIZE (args); i++) {snprintf (name,sizeof (name),"arg %zu" , i);ASSERT_EQ (skel->bss->test_2_args[i], args[i], name);
@@ -125,7 +127,8 @@ static void test_dummy_multiple_args(void)static void test_dummy_sleepable (void ) { -__u64 args[1 ] = {0 }; +struct bpf_dummy_ops_state st; +__u64 args[1 ] = {(__u64)&st}; LIBBPF_OPTS(bpf_test_run_opts, attr, .ctx_in = args, .ctx_size_in = sizeof(args),
tools/testing/selftests/bpf/progs/dummy_st_ops_success.c +1 -1
@@ -34,7 +34,7 @@ SEC("struct_ops/test_2")int BPF_PROG (test_2 ,struct bpf_dummy_ops_state *state,int a1,unsigned short a2,char a3,unsigned long a4) { - test_2_args[0 ] = (unsigned long )state; + test_2_args[0 ] = state->val; test_2_args[1 ] = a1; test_2_args[2 ] = a2; test_2_args[3 ] = a3;
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerPATCH 4/5 bpf: check bpf_dummy_struct_ops program params for test runs
In reply to Eduard ZingermanWhen doing BPF_PROG_TEST_RUN for bpf_dummy_struct_ops programs, reject execution when NULL is passed for non-nullable params. For programs with non-nullable params verifier assumes that such params are never NULL and thus might optimize out NULL checks.
--- net/bpf/bpf_dummy_struct_ops.c | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)
net/bpf/bpf_dummy_struct_ops.c +50 -1
@@ -79,6 +79,51 @@ static int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args) args->args[3], args->args[4]); } +static const struct bpf_ctx_arg_aux *find_ctx_arg_info (struct bpf_prog_aux *aux,int offset) +{ +int i; + +for (i =0 ; i < aux->ctx_arg_info_size; i++) +if (aux->ctx_arg_info[i].offset == offset) +return &aux->ctx_arg_info[i]; + +return NULL ; +} + +/* There is only one check at the moment: +* - zero should not be passed for pointer parameters not marked as nullable. +*/ +static int check_test_run_args (struct bpf_prog *prog,struct bpf_dummy_ops_test_args *args) +{ +const struct btf_type *func_proto = prog->aux->attach_func_proto; + +for (u32 arg_no =0 ; arg_no <btf_type_vlen (func_proto) ; ++arg_no) { +const struct btf_param *param = &btf_params (func_proto)[arg_no]; +const struct bpf_ctx_arg_aux *info; +const struct btf_type *t; +int offset; + +if (args->args[arg_no] !=0 ) +continue ; + +/* Program is validated already, so there is no need +* to check if t is NULL. +*/ + t =btf_type_skip_modifiers (bpf_dummy_ops_btf, param->type,NULL ); +if (!btf_type_is_ptr (t)) +continue ; + + offset =btf_ctx_arg_offset (bpf_dummy_ops_btf, func_proto, arg_no); + info =find_ctx_arg_info (prog->aux, offset); +if (info && (info->reg_type &PTR_MAYBE_NULL )) +continue ; + +return -EINVAL ; + } + +return 0 ; +} +extern const struct bpf_link_ops bpf_struct_ops_link_lops; int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
@@ -87,7 +132,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops;const struct btf_type *func_proto;struct bpf_dummy_ops_test_args *args; -struct bpf_tramp_links *tlinks; +struct bpf_tramp_links *tlinks =NULL ;struct bpf_tramp_link *link =NULL ;void *image =NULL ;unsigned int op_idx;
@@ -109,6 +154,10 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,if (IS_ERR (args))return PTR_ERR (args); + err =check_test_run_args (prog, args); +if (err) + goto out; + tlinks =kcalloc (BPF_TRAMP_MAX ,sizeof (*tlinks),GFP_KERNEL );if (!tlinks) { err = -ENOMEM ;
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerPATCH 5/5 selftests/bpf: dummy_st_ops should reject 0 for non-nullable params
In reply to Eduard ZingermanCheck if BPF_PROG_TEST_RUN for bpf_dummy_struct_ops programs rejects execution if NULL is passed for non-nullable parameter.
--- .../selftests/bpf/prog_tests/dummy_st_ops.c | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+)
tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c +27 -0
@@ -147,6 +147,31 @@ static void test_dummy_sleepable(void)dummy_st_ops_success__destroy (skel); } +/* dummy_st_ops.test_sleepable() parameter is not marked as nullable, +* thus bpf_prog_test_run_opts() below should be rejected as it tries +* to pass NULL for this parameter. +*/ +static void test_dummy_sleepable_reject_null (void ) +{ +__u64 args[1 ] = {0 }; +LIBBPF_OPTS (bpf_test_run_opts, attr, + .ctx_in = args, + .ctx_size_in = sizeof(args), + ); +struct dummy_st_ops_success *skel; +int fd, err; + + skel =dummy_st_ops_success__open_and_load (); +if (!ASSERT_OK_PTR (skel,"dummy_st_ops_load" )) +return ; + + fd =bpf_program__fd (skel->progs.test_sleepable); + err =bpf_prog_test_run_opts (fd, &attr); +ASSERT_EQ (err, -EINVAL ,"test_run" ); + +dummy_st_ops_success__destroy (skel); +} +void test_dummy_st_ops (void ) {if (test__start_subtest ("dummy_st_ops_attach" ))
@@ -159,6 +184,8 @@ void test_dummy_st_ops(void)test_dummy_multiple_args ();if (test__start_subtest ("dummy_sleepable" ))test_dummy_sleepable (); +if (test__start_subtest ("dummy_sleepable_reject_null" )) +test_dummy_sleepable_reject_null ();RUN_TESTS (dummy_st_ops_fail); }
Signature · 1 line
2.34.1
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 5 lines · On Tue, 23 Apr 2024 18:28:16 -0700 you wrote:
When doing
BPF_PROG_TEST_RUNforbpf_dummy_struct_opsprograms, execution should be rejected when NULL is passed for non-nullable params, because for such params verifier assumes that such params are never NULL and thus might optimize out NULL checks.
Quoted 8 lines
This problem was reported by Jose E. Marchesi in off-list discussion. The code generated by GCC for dummy_st_ops_success/test_1() function differs from LLVM variant in a way that allows verifier to remove the NULL check. The test dummy_st_ops/dummy_init_ret_value actually sets the 'state' parameter to NULL, thus GCC-generated version of the test triggers NULL pointer dereference when BPF program is executed.
[...]
Here is the summary with links:
- [bpf-next,1/5] bpf: mark bpf_dummy_struct_ops.test_1 parameter as nullable
https://git.kernel.org/bpf/bpf-next/c/1479eaff1f16 - [bpf-next,2/5] selftests/bpf: adjust dummy_st_ops_success to detect additional error https://git.kernel.org/bpf/bpf-next/c/3b3b84aacb44 - [bpf-next,3/5] selftests/bpf: do not pass NULL for non-nullable params in dummy_st_ops https://git.kernel.org/bpf/bpf-next/c/f612210d456a - [bpf-next,4/5] bpf: check bpf_dummy_struct_ops program params for test runs https://git.kernel.org/bpf/bpf-next/c/980ca8ceeae6 - [bpf-next,5/5] selftests/bpf: dummy_st_ops should reject 0 for non-nullable params https://git.kernel.org/bpf/bpf-next/c/6a2d30d3c5bf
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html