bpf: Validate stack arg reads against caller state
Subsystems (4)
PATCH v2 1/2 bpf: Validate stack arg reads against caller state
check_stack_arg_read() bounds a stack argument index with the callee's BTF-derived argument count, then uses the index in the caller's stack_arg_regs[] array. The array's actual logical length is recorded in caller->out_stack_arg_cnt, so the bound and indexed object can diverge.
A crafted BPF_PROG_LOAD can use an argument mismatch to mark a static subprogram's BTF information unreliable. Later calls return from btf_check_subprog_call() before check_outgoing_stack_args(), while check_func_call() continues the static call on -EINVAL. The callee can then read a slot that the caller never allocated or initialized. The earlier commit 4286f5deee14 ("bpf: Validate outgoing stack args when btf_prepare_func_args fails") closed this hole for the first call, but not for later calls after the subprogram has been marked unreliable.
Callbacks have a related problem. The verifier can make incoming stack arguments readable from outgoing slots at the helper call site. However, helpers and kfuncs invoke callbacks through an ABI that passes at most five arguments, so the runtime callback receives no stack arguments. Async callback verification can also reach check_stack_arg_read() without a caller frame.
On x86-64, KASAN reports the direct-call out-of-bounds copy as:
BUG: KASAN: slab-out-of-bounds in check_load_mem+0xb51/0xc60 Read of size 80 at addr ffff8881009af550 by task poc/131 Call Trace: check_stack_arg_read at kernel/bpf/verifier.c:4234 (inlined by) check_load_mem at kernel/bpf/verifier.c:6645 do_check_common at kernel/bpf/verifier.c:19568 bpf_check at kernel/bpf/verifier.c:21318 bpf_prog_load at kernel/bpf/syscall.c:3134
Reject callback subprograms with incoming stack arguments before their state is pushed. For direct calls, retrieve the slot with bpf_get_spilled_stack_arg(), which validates the index against the caller's out_stack_arg_cnt and rejects uninitialized slots before copying the register state.
---
Changes in v2:
- Reject callback subprograms that declare stack arguments.
- Drop the curframe-zero check because only async callbacks can reach that state, and callbacks with stack arguments are now rejected earlier.
- Explain that commit 4286f5deee14 closed the hole for the first call only.
kernel/bpf/verifier.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +10 -1
@@ -4228,7 +4228,12 @@ static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_st } caller = vstate->frame[vstate->curframe -1 ]; - arg = &caller->stack_arg_regs[spi]; + arg =bpf_get_spilled_stack_arg (spi, caller); +if (!arg) { +verbose (env,"invalid read from uninitialized stack arg off %d\n" , off); +return -EACCES ; + } + cur = vstate->frame[vstate->curframe];bpf_diag_mod_begin (env, &cur->regs[dst_regno], arg,BPF_DIAG_MOD_WRITE ); cur->regs[dst_regno] = *arg;
@@ -9839,6 +9844,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins err =btf_check_subprog_call (env, subprog, caller->regs);if (err == -EFAULT )return err; +if (bpf_in_stack_arg_cnt (&env->subprog_info[subprog])) { +verbose (env,"callback subprog cannot have stack args\n" ); +return -EINVAL ; + }/* set_callee_state is used for direct subprog calls, but we are * interested in validating only BPF helpers that can call subprogs as
Signature · 1 line
2.56.0
PATCH v2 2/2 selftests/bpf: Test missing and callback stack arguments
Exercise the path where a static seven-argument subprogram has unreliable BTF and is called through a bridge with only one initialized outgoing stack argument. The callee expects two stack arguments, but the unreliable-BTF path skips check_outgoing_stack_args(). Expect the verifier to reject the second r11 load at offset 16.
Also exercise synchronous and asynchronous callbacks whose BTF prototypes declare a sixth argument. Initialize the outgoing slot at each helper call site so the programs reach the callback-specific rejection. Keep a valid six-argument direct call in each program so bpf_fixup_call_args() records the stack-argument requirement and cannot become an unrelated rejection fallback.
The fix also rejects the existing uninitialized-stack-argument test before the later unreadable-register check. Update that test's expected diagnostic accordingly.
---
Changes in v2:
- Add synchronous bpf_loop callback coverage.
- Add asynchronous bpf_timer callback coverage.
- Add the six-argument callback BTF prototype and initialize the outgoing stack slot so both tests reach the callback-specific rejection.
.../bpf/progs/btf__verifier_stack_arg_order.c | 10 ++ .../bpf/progs/verifier_stack_arg_order.c | 121 +++++++++++++++++- 2 files changed, 130 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/btf__verifier_stack_arg_order.c +10 -0
@@ -25,6 +25,11 @@ void subprog_bad_ptr_7args(long *a, int b, int c, int d, int e, int f, int g) { } +int callback_6args (long a,long b,long c,long d,long e,long f) +{ +return a + b + c + d + e + f; +} +#else int subprog_bad_order_6args (void )
@@ -46,4 +51,9 @@ void subprog_bad_ptr_7args(void) { } +int callback_6args (void ) +{ +return 0 ; +} +#endif
tools/testing/selftests/bpf/progs/verifier_stack_arg_order.c +120 -1
145 lines not on this page · raw message
Signature · 1 line
2.56.0
Quoted 38 lines · Weiming Shi
check_stack_arg_read()bounds a stack argument index with the callee's BTF-derived argument count, then uses the index in the caller's stack_arg_regs[] array. The array's actual logical length is recorded incaller->out_stack_arg_cnt, so the bound and indexed object can diverge.A crafted
BPF_PROG_LOADcan use an argument mismatch to mark a static subprogram's BTF information unreliable. Later calls return frombtf_check_subprog_call()beforecheck_outgoing_stack_args(), whilecheck_func_call()continues the static call on-EINVAL. The callee can then read a slot that the caller never allocated or initialized. The earlier commit4286f5deee14("bpf: Validate outgoing stack args whenbtf_prepare_func_argsfails") closed this hole for the first call, but not for later calls after the subprogram has been marked unreliable.Callbacks have a related problem. The verifier can make incoming stack arguments readable from outgoing slots at the helper call site. However, helpers and kfuncs invoke callbacks through an ABI that passes at most five arguments, so the runtime callback receives no stack arguments. Async callback verification can also reach
check_stack_arg_read()without a caller frame.On x86-64, KASAN reports the direct-call out-of-bounds copy as:
BUG: KASAN: slab-out-of-bounds in check_load_mem+0xb51/0xc60 Read of size 80 at addr ffff8881009af550 by task poc/131 Call Trace: check_stack_arg_read at kernel/bpf/verifier.c:4234 (inlined by) check_load_mem at kernel/bpf/verifier.c:6645 do_check_common at kernel/bpf/verifier.c:19568 bpf_check at kernel/bpf/verifier.c:21318 bpf_prog_load at kernel/bpf/syscall.c:3134Reject callback subprograms with incoming stack arguments before their state is pushed. For direct calls, retrieve the slot with
bpf_get_spilled_stack_arg(), which validates the index against the caller'sout_stack_arg_cntand rejects uninitialized slots before copying the register state.
Quoted 5 lines
Fixes 0f6bd5e7a804 ("bpf: Support stack arguments for bpf functions")Reported-by <[email protected]>Suggested-by Alexei Starovoitov <[email protected]>Assisted-by LLMSigned-off-by Weiming Shi <[email protected]>
looks good, but doesn't apply.
please rebase on top of bpf tree.
pw-bot: cr