bpf: Validate outgoing stack args when btf_prepare_func_args fails
Subsystems (4)
Yonghong Song
reviewerPATCH 1/2 bpf: Validate outgoing stack args when btf_prepare_func_args fails
btf_prepare_func_args() sets sub->arg_cnt before validating arg types. If validation fails (e.g. unsupported pointer type in a static subprog), check_outgoing_stack_args() is skipped because btf_check_func_arg_match() returns early. For static subprogs, check_func_call() ignores non-EFAULT errors and proceeds with the call.
This causes the callee to read stack arg slots that the caller never stored or not initialized, potentially dereferencing NULL caller->stack_arg_regs or getting no-initialized value.
To fix the issue, when btf_prepare_func_args() fails and the subprog expects stack args, call check_outgoing_stack_args() to verify the caller initialized the slots. Return -EFAULT on failure so the error is not ignored.
--- kernel/bpf/verifier.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +8 -2
@@ -9118,11 +9118,17 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,struct bpf_func_state *caller =cur_func (env);struct bpf_verifier_log *log = &env->log;u32 i; -int ret; +int ret, err; ret =btf_prepare_func_args (env, subprog); -if (ret) +if (ret) { +if (bpf_in_stack_arg_cnt (sub) >0 ) { + err =check_outgoing_stack_args (env, caller, sub->arg_cnt); +if (err) +return err; + }return ret; + } ret =check_outgoing_stack_args (env, caller, sub->arg_cnt);if (ret)
Signature · 1 line
2.53.0-Meta
Yonghong Song
reviewerPATCH 2/2 selftests/bpf: Add test for stack arg read without caller write
Add negative tests for the outgoing stack arg validation. A static subprog with a 'long *' arg causes btf_prepare_func_args() to fail after setting arg_cnt. The validation ensures check_outgoing_stack_args() still runs.
Also update two existing tests (release_ref, stale_pkt_ptr) whose expected error messages changed: invalidated stack arg slots are now caught by check_outgoing_stack_args() at the call site instead of at the callee's dereference.
--- .../bpf/progs/btf__verifier_stack_arg_order.c | 8 +++ .../selftests/bpf/progs/verifier_stack_arg.c | 4 +- .../bpf/progs/verifier_stack_arg_order.c | 58 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/btf__verifier_stack_arg_order.c +8 -0
@@ -21,6 +21,10 @@ int subprog_pruning_call_before_load_6args(int a, int b, int c, int d, int e, in return a + b + c + d + e + f; } +void subprog_bad_ptr_7args (long *a,int b,int c,int d,int e,int f,int g) +{ +} +#else int subprog_bad_order_6args (void )
@@ -38,4 +42,8 @@ int subprog_pruning_call_before_load_6args(void) return 0; } +void subprog_bad_ptr_7args (void ) +{ +} +#endif
tools/testing/selftests/bpf/progs/verifier_stack_arg.c +2 -2
@@ -152,7 +152,7 @@ __naked void stack_arg_pruning_type_mismatch(void) SEC("tc") __description("stack_arg: release_reference invalidates stack arg slot") __failure -__msg("R{{[0-9]}} !read_ok") +__msg("callee expects 6 args, stack arg1 is not initialized")__naked void stack_arg_release_ref (void ) { asm volatile (
@@ -201,7 +201,7 @@ __naked void stack_arg_release_ref(void) SEC("tc") __description("stack_arg: pkt pointer in stack arg slot invalidated after pull_data") __failure -__msg("R{{[0-9]}} !read_ok") +__msg("callee expects 6 args, stack arg1 is not initialized")__naked void stack_arg_stale_pkt_ptr (void ) { asm volatile (
tools/testing/selftests/bpf/progs/verifier_stack_arg_order.c +58 -0
64 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewThis looks like a bug fix for stack argument handling. Should this include:
Quoted 15 lines
commit
284cf62e75bd5632d61c528e48fe3f738870b269
Author: Yonghong Song <[email protected]>bpf: Validate outgoing stack args when
btf_prepare_func_argsfails
btf_prepare_func_args()setssub->arg_cntbefore validating arg types. If validation fails (e.g. unsupported pointer type in a static subprog),check_outgoing_stack_args()is skipped becausebtf_check_func_arg_match()returns early. For static subprogs,check_func_call()ignores non-EFAULT errors and proceeds with the call.This causes the callee to read stack arg slots that the caller never stored or not initialized, potentially dereferencing NULL
caller->stack_arg_regsor getting no-initialized value.
Quoted 6 lines
To fix the issue, when
btf_prepare_func_args()fails and the subprog expects stack args, callcheck_outgoing_stack_args()to verify the caller initialized the slots. Return-EFAULTon failure so the error is not ignored.Reported-by Sashiko <[email protected]>Signed-off-by Yonghong Song <[email protected]>
This looks like a bug fix for stack argument handling. Should this include:
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25879695095
Quoted 19 lines · bot+bpf-ci
Quoted 19 lines
commit
284cf62e75bd5632d61c528e48fe3f738870b269
Author: Yonghong Song <[email protected]>bpf: Validate outgoing stack args when
btf_prepare_func_argsfails
btf_prepare_func_args()setssub->arg_cntbefore validating arg types. If validation fails (e.g. unsupported pointer type in a static subprog),check_outgoing_stack_args()is skipped becausebtf_check_func_arg_match()returns early. For static subprogs,check_func_call()ignores non-EFAULT errors and proceeds with the call.This causes the callee to read stack arg slots that the caller never stored or not initialized, potentially dereferencing NULL
caller->stack_arg_regsor getting no-initialized value.To fix the issue, when
btf_prepare_func_args()fails and the subprog expects stack args, callcheck_outgoing_stack_args()to verify the caller initialized the slots. Return-EFAULTon failure so the error is not ignored.
Quoted 6 lines
Quoted 2 lines
Reported-by Sashiko <[email protected]>Signed-off-by Yonghong Song <[email protected]>This looks like a bug fix for stack argument handling. Should this include:
Fixes 3ab5bd317ee2 ("bpf: Set sub->arg_cnt earlier in btf_prepare_func_args()")
fixes tag is necessary indeed, but please drop 'Reported-by: Sashiko'. If we start doing such things every patch will have it.
Yonghong Song
reviewerQuoted 20 lines · Alexei Starovoitov
Quoted 19 lines · bot+bpf-ci
Quoted 19 lines
commit
284cf62e75bd5632d61c528e48fe3f738870b269
Author: Yonghong Song <[email protected]>bpf: Validate outgoing stack args when
btf_prepare_func_argsfails
btf_prepare_func_args()setssub->arg_cntbefore validating arg types. If validation fails (e.g. unsupported pointer type in a static subprog),check_outgoing_stack_args()is skipped becausebtf_check_func_arg_match()returns early. For static subprogs,check_func_call()ignores non-EFAULT errors and proceeds with the call.This causes the callee to read stack arg slots that the caller never stored or not initialized, potentially dereferencing NULL
caller->stack_arg_regsor getting no-initialized value.To fix the issue, when
btf_prepare_func_args()fails and the subprog expects stack args, callcheck_outgoing_stack_args()to verify the caller initialized the slots. Return-EFAULTon failure so the error is not ignored.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Reported-by Sashiko <[email protected]>Signed-off-by Yonghong Song <[email protected]>This looks like a bug fix for stack argument handling. Should this include:
Fixes 3ab5bd317ee2 ("bpf: Set sub->arg_cnt earlier in btf_prepare_func_args()")fixes tag is necessary indeed, but please drop 'Reported-by: Sashiko'. If we start doing such things every patch will have it.
Thanks. Will add fixes tag and drop 'Reported-by: Sashiko'.