bpf: use type_may_be_null() helper for nullable-param check
Subsystems (4)
PATCH 1/1 bpf: use type_may_be_null() helper for nullable-param check
Commit 980ca8ceeae6 ("bpf: check bpf_dummy_struct_ops program params for test runs") does bitwise AND between reg_type and PTR_MAYBE_NULL, which is correct, but due to type difference the compiler complains:
net/bpf/bpf_dummy_struct_ops.c:118:31: warning: bitwise operation between different enumeration types ('const enum bpf_reg_type' and 'enum bpf_type_flag') [-Wenum-enum-conversion]
118 | if (info && (info->reg_type & PTR_MAYBE_NULL))
| ~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~Workaround the warning by moving the type_may_be_null() helper from verifier.c into bpf_verifier.h, and reuse it here to check whether param is nullable.
---
Due to kernel test bot not setting the correct email header (reported[1]) Eduard probably never saw the report about the warning (nor did it show up on Patchwork).
1: https://github.com/intel/lkp-tests/issues/383
--- include/linux/bpf_verifier.h | 5 +++++ kernel/bpf/verifier.c | 5 ----- net/bpf/bpf_dummy_struct_ops.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
include/linux/bpf_verifier.h +5 -0
@@ -927,6 +927,11 @@ static inline bool type_is_sk_pointer(enum bpf_reg_type type) type ==PTR_TO_XDP_SOCK ; } +static inline bool type_may_be_null (u32 type) +{ +return type &PTR_MAYBE_NULL ; +} +static inline void mark_reg_scratched (struct bpf_verifier_env *env,u32 regno) { env->scratched_regs |=1U << regno;
kernel/bpf/verifier.c +0 -5
@@ -383,11 +383,6 @@ static void verbose_invalid_scalar(struct bpf_verifier_env *env,verbose (env," should have been in [%d, %d]\n" , range.minval, range.maxval); } -static bool type_may_be_null (u32 type) -{ -return type &PTR_MAYBE_NULL ; -} -static bool reg_not_null (const struct bpf_reg_state *reg) {enum bpf_reg_type type;
net/bpf/bpf_dummy_struct_ops.c +1 -1
@@ -115,7 +115,7 @@ static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_ 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 )) +if (info &&type_may_be_null (info->reg_type))continue ;return -EINVAL ;
Signature · 1 line
2.46.0
Quoted 66 lines · Shung-Hsi Yu
Commit
980ca8ceeae6("bpf: checkbpf_dummy_struct_opsprogram params for test runs") does bitwise AND betweenreg_typeandPTR_MAYBE_NULL, which is correct, but due to type difference the compiler complains:
net/bpf/bpf_dummy_struct_ops.c:118:31: warning: bitwise operation between different enumeration types ('const enumbpf_reg_type' and 'enumbpf_type_flag') [-Wenum-enum-conversion]118 | if (info && (info->reg_type & PTR_MAYBE_NULL)) | ~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~Workaround the warning by moving the
type_may_be_null()helper fromverifier.cintobpf_verifier.h, and reuse it here to check whether param is nullable.Fixes 980ca8ceeae6 ("bpf: check bpf_dummy_struct_ops program params for test runs")Reported-by kernel test robot <[email protected]>Signed-off-by Shung-Hsi Yu <[email protected]>---
Due to kernel test bot not setting the correct email header (reported[1]) Eduard probably never saw the report about the warning (nor did it show up on Patchwork).1: https://github.com/intel/lkp-tests/issues/383
--- include/linux/bpf_verifier.h | 5 +++++ kernel/bpf/verifier.c | 5 ----- net/bpf/bpf_dummy_struct_ops.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)include/linux/bpf_verifier.h +5
-0@@ -927,6 +927,11 @@ static inline bool type_is_sk_pointer(enum bpf_reg_type type) type ==PTR_TO_XDP_SOCK ; } +static inline bool type_may_be_null (u32 type) +{ +return type &PTR_MAYBE_NULL ; +} +static inline void mark_reg_scratched (struct bpf_verifier_env *env,u32 regno) {include/linux/bpf_verifier.h +0
-0env->scratched_regs |=1U << regno;kernel/bpf/verifier.c +0
-5@@ -383,11 +383,6 @@ static void verbose_invalid_scalar(struct bpf_verifier_env *env,verbose (env," should have been in [%d, %d]\n" , range.minval, range.maxval); } -static bool type_may_be_null (u32 type) -{ -return type &PTR_MAYBE_NULL ; -} -static bool reg_not_null (const struct bpf_reg_state *reg) {enum bpf_reg_type type;net/bpf/bpf_dummy_struct_ops.c +0
-0
net/bpf/bpf_dummy_struct_ops.c static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_ · line 118
Quoted 5 lines
net/bpf/bpf_dummy_struct_ops.c +1
-1@@ -115,7 +115,7 @@ static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_ 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 )) +if (info &&type_may_be_null (info->reg_type))
Maybe as part of this clean up, we should also consider replacing all the open-coded & PTR_MAYBE_NULL checks with type_may_be_null() which we have sprinkled throughout kernel/bpf/verifier.c?
/M
Quoted 1 line · Matt Bobrowski
On Thu, Sep 05, 2024 at 01:52:32PM +0800, Shung-Hsi Yu wrote:
[...]
Quoted 4 lines
Quoted 4 lines
net/bpf/bpf_dummy_struct_ops.c +0
-0@@ -115,7 +115,7 @@ static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_
Quoted 8 lines
net/bpf/bpf_dummy_struct_ops.c static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_ · line 118
Quoted 4 lines
net/bpf/bpf_dummy_struct_ops.c +1
-1offset =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 )) +if (info &&type_may_be_null (info->reg_type))Maybe as part of this clean up, we should also consider replacing all the open-coded &
PTR_MAYBE_NULLchecks withtype_may_be_null()which we have sprinkled throughoutkernel/bpf/verifier.c?
Agree we should. Usage like this could be replaced
if (ptr_reg->type & PTR_MAYBE_NULL) {
verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
dst, reg_type_str(env, ptr_reg->type));
return -EACCES;
}OTOH replacing & PTR_MAYBE_NULL here probably won't help improve clarity.
if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
reg->type = PTR_TO_BTF_ID;
if (arg->arg_type & PTR_MAYBE_NULL)
reg->type |= PTR_MAYBE_NULL;
if (arg->arg_type & PTR_UNTRUSTED)
reg->type |= PTR_UNTRUSTED;
if (arg->arg_type & PTR_TRUSTED)
reg->type |= PTR_TRUSTED;
...For such case we might need to introduce another helper (bitwise-OR between enum bpf_type_flag should be free of compiler warning).
reg->type = type_flag_apply(PTR_TO_BTF_ID, arg->arg_type, PTR_MAYBE_NULL | PTR_UNTRUSTED | PTR_TRUSTED);
WDYT?
Quoted 12 lines · Shung-Hsi Yu
Commit
980ca8ceeae6("bpf: checkbpf_dummy_struct_opsprogram params for test runs") does bitwise AND betweenreg_typeandPTR_MAYBE_NULL, which is correct, but due to type difference the compiler complains:
net/bpf/bpf_dummy_struct_ops.c:118:31: warning: bitwise operation between different enumeration types ('const enumbpf_reg_type' and 'enumbpf_type_flag') [-Wenum-enum-conversion]118 | if (info && (info->reg_type & PTR_MAYBE_NULL)) | ~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~Workaround the warning by moving the
type_may_be_null()helper fromverifier.cintobpf_verifier.h, and reuse it here to check whether param is nullable.
Quoted 5 lines
Fixes 980ca8ceeae6 ("bpf: check bpf_dummy_struct_ops program params for test runs")Reported-by kernel test robot <[email protected]>Signed-off-by Shung-Hsi Yu <[email protected]>---
Thank you for this fix.
Replacing other uses of PTR_MAYBE_NULL suggested by Matt seems like a good idea, but it does not preclude merge for this patch.
[...]
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Thu, 5 Sep 2024 13:52:32 +0800 you wrote:
Commit
980ca8ceeae6("bpf: checkbpf_dummy_struct_opsprogram params for test runs") does bitwise AND betweenreg_typeandPTR_MAYBE_NULL, which is correct, but due to type difference the compiler complains:
net/bpf/bpf_dummy_struct_ops.c:118:31: warning: bitwise operation between different enumeration types ('const enumbpf_reg_type' and 'enumbpf_type_flag') [-Wenum-enum-conversion]118 | if (info && (info->reg_type & PTR_MAYBE_NULL)) | ~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~[...]
Here is the summary with links:
- [bpf-next] bpf: use type_may_be_null() helper for nullable-param check
https://git.kernel.org/bpf/bpf-next/c/1ae497c78f01
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html