bpf: Keep generic __uninit kfunc arguments live
Subsystems (6)
Tejun Heo
reviewerPATCH v2 1/1 bpf: Keep generic __uninit kfunc arguments live
Stack liveness models a kfunc's __uninit pointer argument as a pure write:
whatever the buffer holds before the call is dead, and 2cb27158adb3 ("bpf:
poison dead stack slots") poisons those slots at every checkpoint the path visits before the call. Argument validation disagrees. Only dynptr arguments honor __uninit. A generic fixed-size buffer goes through check_mem_reg(), whose read pass requires every byte readable. The two rules meet at the call and the poisoned buffer is rejected with "slot poisoned by dead code elimination", even when the program initialized it.
Every kfunc with a generic __uninit output argument is affected, bpf_ksock_create() through err__uninit and sched_ext's scx_bpf_cid_topo() through its output struct. The rejection needs the buffer's slots to be allocated and dead at a checkpoint, for example on the second iteration of a loop around the call. Before e566701b9b0c ("bpf: Check fixed-size mem args of helpers and kfuncs the same way") kfunc arguments could read poisoned slots, so the call passed, but the slots stayed poisoned and the program's next read of the buffer was rejected instead.
Restrict the liveness write-only exception to dynptr arguments, where validation accepts uninitialized output. A generic __uninit buffer stays live and is never poisoned, and validation keeps requiring it to be initialized. Add a selftest with a scalar output buffer and a checkpoint between its initialization and the call.
v2: Rebased on bpf/master.
--- kernel/bpf/verifier.c | 3 tools/testing/selftests/bpf/prog_tests/verifier.c | 2 tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c | 45 +++++++++++++ tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 9 ++ tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h | 1 5 files changed, 59 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
@@ -13588,7 +13588,8 @@ out:/* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */ if (arg ==0 && meta.kfunc_flags &KF_ITER_NEW )return -size; -if (is_kfunc_arg_uninit (btf, &args[arg])) +/* only dynptr validation accepts an uninitialized __uninit argument */ +if (is_kfunc_arg_dynptr (btf, &args[arg]) &&is_kfunc_arg_uninit (btf, &args[arg]))return -size;return size; }
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -55,6 +55,7 @@#include "verifier_jeq_infer_not_null.skel.h" #include "verifier_jit_convergence.skel.h" #include "verifier_kfunc_perfmon.skel.h" +#include "verifier_kfunc_uninit.skel.h" #include "verifier_ld_ind.skel.h" #include "verifier_ldsx.skel.h" #include "verifier_leak_ptr.skel.h"
@@ -220,6 +221,7 @@ void test_verifier_iterating_callbacks(vvoid test_verifier_jeq_infer_not_null (void ) {RUN (verifier_jeq_infer_not_null); }void test_verifier_jit_convergence (void ) {RUN (verifier_jit_convergence); }void test_verifier_kfunc_perfmon (void ) {RUN (verifier_kfunc_perfmon); } +void test_verifier_kfunc_uninit (void ) {RUN_TESTS (verifier_kfunc_uninit); }void test_verifier_load_acquire (void ) {RUN (verifier_load_acquire); }void test_verifier_ld_ind (void ) {RUN (verifier_ld_ind); }void test_verifier_ldsx (void ) {RUN (verifier_ldsx); }
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c new file +45 -0
45 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +9 -0
@@ -1102,6 +1102,14 @@ __bpf_kfunc void bpf_kfunc_call_test_pas { } +__bpf_kfunc void bpf_kfunc_call_test_uninit(struct prog_test_pass1 *out__uninit) +{ + out__uninit->x0 =1 ; + out__uninit->x1 =2 ; + out__uninit->x2 =3 ; + out__uninit->x3 =4 ; +} + __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) { }
@@ -1508,6 +1516,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_int_me BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx) BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2) +BTF_ID_FLAGS(func, bpf_kfunc_call_test_uninit) BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)BTF_ID_FLAGS (func, bpf_kfunc_call_test_fail3)
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +1 -0
@@ -161,6 +161,7 @@ __u64 bpf_kfunc_call_stack_arg_big(__u64 void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym; void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; +void bpf_kfunc_call_test_uninit(struct prog_test_pass1 *out__uninit) __ksym; void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; void bpf_kfunc_call_test_destructive(void) __ksym;
[email protected]
AI reviewThis isn't a bug, but could the 'v2: Rebased on bpf/master.' note move below the --- line so it stays out of the git log?
Quoted 20 lines
bpf: Keep generic
__uninitkfunc arguments liveStack liveness models a kfunc's
__uninitpointer argument as a pure write:
whatever the buffer holds before the call is dead, and2cb27158adb3("bpf:
poison dead stack slots") poisons those slots at every checkpoint the path visits before the call. Argument validation disagrees. Only dynptr arguments honor__uninit. A generic fixed-size buffer goes throughcheck_mem_reg(), whose read pass requires every byte readable. The two rules meet at the call and the poisoned buffer is rejected with "slot poisoned by dead code elimination", even when the program initialized it.Every kfunc with a generic
__uninitoutput argument is affected,bpf_ksock_create()througherr__uninitand sched_ext'sscx_bpf_cid_topo()through its output struct. The rejection needs the buffer's slots to be allocated and dead at a checkpoint, for example on the second iteration of a loop around the call. Beforee566701b9b0c("bpf: Check fixed-size mem args of helpers and kfuncs the same way") kfunc arguments could read poisoned slots, so the call passed, but the slots stayed poisoned and the program's next read of the buffer was rejected instead.
Quoted 7 lines
Restrict the liveness write-only exception to dynptr arguments, where validation accepts uninitialized output. A generic
__uninitbuffer stays live and is never poisoned, and validation keeps requiring it to be initialized. Add a selftest with a scalar output buffer and a checkpoint between its initialization and the call.v2: Rebased on bpf/master.
This isn't a bug, but could the 'v2: Rebased on bpf/master.' note move below the --- line so it stays out of the git log?
Quoted 2 lines
Fixes 2cb27158adb3 ("bpf: poison dead stack slots")Cc [email protected] # v7.1+
This isn't a bug, but since this targets bpf, would it be worth dropping the Cc: stable trailer and instead noting the v7.1+ backport request under the --- line, as bpf_devel_QA.rst asks?
Quoted 7 lines
Signed-off-by Tejun Heo <[email protected]>kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn * · line 13595
Quoted 9 lines
kernel/bpf/verifier.c +2
-1@@ -13588,7 +13588,8 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn */* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */ if (arg ==0 && meta.kfunc_flags &KF_ITER_NEW )return -size; -if (is_kfunc_arg_uninit (btf, &args[arg])) +/* only dynptr validation accepts an uninitialized __uninit argument */ +if (is_kfunc_arg_dynptr (btf, &args[arg]) &&is_kfunc_arg_uninit (btf, &args[arg]))return -size;return size; }
Amery Hung raised a design concern on v1 about whether restricting the liveness exception to dynptrs is the right fix.
The reviewer suggests that the underlying issue is that generic argument validation doesn't honor __uninit, and that generic __uninit arguments should behave like helper output buffers: generate MEM_UNINIT | MEM_WRITE, validate as write-only, and mark the written range initialized after the kfunc call. With that approach, bpf_kfunc_stack_access_bytes() could continue returning -size for generic __uninit arguments.
The current change instead makes their previous contents live and requires callers to initialize an argument explicitly declared uninitialized.
Was this comment considered for v2?
---
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/34913753963