bpf: Reject passing scalar NULL to nonnull arg of a global subprog
Subsystems (4)
PATCH 1/2 bpf: Reject passing scalar NULL to nonnull arg of a global subprog
A global subprogram argument tagged __arg_nonnull is set up as a non-nullable PTR_TO_MEM. However the verifier does not check against a scalar NULL, leading to real NULL pointer dereference. Reject it as well.
--- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
@@ -9331,7 +9331,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,return ret;if (check_mem_reg (env, reg, argno, arg->mem_size,BPF_READ |BPF_WRITE ,NULL ))return -EINVAL ; -if (!(arg->arg_type &PTR_MAYBE_NULL ) && (reg->type &PTR_MAYBE_NULL )) { +if (!(arg->arg_type &PTR_MAYBE_NULL ) && + (type_may_be_null (reg->type) ||bpf_register_is_null (reg))) {bpf_log (log,"%s is expected to be non-NULL\n" ,reg_arg_name (env, argno));return -EINVAL ;
Signature · 1 line
2.52.0
PATCH 2/2 selftests/bpf: Test passing scalar NULL to nonnull global subprog
Make sure the verifier reject passing a hardcoded NULL to an __arg_nonnull argument.
--- .../selftests/bpf/progs/verifier_global_subprogs.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c +13 -0
@@ -185,6 +185,19 @@ int arg_tag_nonnull_ptr_good(void *ctx)return subprog_nonnull_ptr_good (&x, &y); } +/* a hardcoded NULL is a SCALAR_VALUE, not a PTR_MAYBE_NULL, so make sure it +* still gets rejected for an __arg_nonnull argument +*/ +SEC("?raw_tp") +__failure __log_level(2) +__msg("R1 is expected to be non-NULL") +int arg_tag_nonnull_ptr_null_bad (void *ctx) +{ +int y =74 ; + +return subprog_nonnull_ptr_good (NULL , &y); +} +/* this global subprog can be now called from many types of entry progs, each * with different context type */
Signature · 1 line
2.52.0
Quoted 8 lines · Amery Hung
A global subprogram argument tagged
__arg_nonnullis set up as a non-nullablePTR_TO_MEM. However the verifier does not check against a scalar NULL, leading to real NULL pointer dereference. Reject it as well.Fixes 94e1c70a3452 ("bpf: support 'arg:xxx' btf_decl_tag-based hints for global subprog args")Signed-off-by Amery Hung <[email protected]>---
Amery, git am complains about merge conflict when applying on top of the current bpf/master (patch can apply it with fuzz). Because of that CI did not trigger for this patch-set. Could you please rebase and resend?
Quoted 11 lines · Eduard Zingerman
Quoted 8 lines · Amery Hung
A global subprogram argument tagged
__arg_nonnullis set up as a non-nullablePTR_TO_MEM. However the verifier does not check against a scalar NULL, leading to real NULL pointer dereference. Reject it as well.Fixes 94e1c70a3452 ("bpf: support 'arg:xxx' btf_decl_tag-based hints for global subprog args")Signed-off-by Amery Hung <[email protected]>---
Quoted 6 lines
Acked-by Eduard Zingerman <[email protected]>Amery, git am complains about merge conflict when applying on top of the current bpf/master (
patchcan apply it with fuzz). Because of that CI did not trigger for this patch-set. Could you please rebase and resend?
Okay. I will send a v2 based on bpf/master. It was based on bpf-next/master, which probably explains the conflict.