bpf: mark kfunc memory size argument precise when buffer is null
PATCH v2 1/1 bpf: mark kfunc memory size argument precise when buffer is null
check_kfunc_args() allows a null buffer register paired with KF_ARG_MEM_SIZE. When buff_reg is null, check_mem_size_reg() is bypassed via break, leaving the corresponding size register at argno un-marked for precision.
Consequently, checkpoints created along paths where a nullable buffer is null would prune against arbitrary scalar values in the size register.
Mark the paired size argument precise before breaking when buff_reg is null to ensure proper state pruning.
---
v1 -> v2: Fix email formatting and whitespace mangling in submission.
kernel/bpf/verifier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +5 -1
@@ -13191,8 +13191,12 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mereturn -EINVAL ; } -if (bpf_register_is_null (buff_reg)) +if (bpf_register_is_null (buff_reg)) { + ret =mark_arg_precision (env, argno); +if (ret) +return ret;break ; + } ret =check_mem_size_reg (env, buff_reg, size_reg, buff_argno, argno,BPF_READ |BPF_WRITE , true, meta, &failure);
Signature · 1 line
2.55.0.windows.3
[email protected]
AI reviewTL;DR: The extra mark_arg_precision() on the null-buffer kfunc size looks unnecessary: size is unused there and __szk is already marked precise. It may reduce pruning and cause complexity-limit…
Quoted 1 line
bpf: mark kfunc memory size argument precise when buffer is null
TL;DR: The extra mark_arg_precision() on the null-buffer kfunc size looks
unnecessary: size is unused there and __szk is already marked precise. It may reduce pruning and cause complexity-limit failures. Asks for a concrete wrongly-accepted program.
Quoted 8 lines
kernel/bpf/verifier.c +0
-0@@ -13219,8 +13219,12 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mereturn -EINVAL ; }
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 13230
Quoted 10 lines
kernel/bpf/verifier.c +5
-1-if (bpf_register_is_null (buff_reg)) +if (bpf_register_is_null (buff_reg)) { + ret =mark_arg_precision (env, argno); +if (ret) +return ret;break ; + } ret =check_mem_size_reg (env, buff_reg, size_reg, buff_argno, argno,BPF_READ |BPF_WRITE , true, meta, &failure);
The commit message says that checkpoints on the null-buffer path "would prune against arbitrary scalar values in the size register." Is that actually a soundness problem?
Precision is only needed when a scalar's value affects a verifier decision. That is why 506ada89629e ("bpf: mark a NULL kfunc argument precise") and 1a3a10b030c9 mark the NULL pointer precise: whether it equals 0 decides whether checks are skipped. On this path nothing reads the value of the size register:
if (reg->type != SCALAR_VALUE) { ... return -EINVAL; } /* type only */
if (bpf_register_is_null(buff_reg)) {
...
break; /* size value unused */
}check_mem_size_reg() is skipped, so there is no smin/umax/zero-size check and no meta->msize_max_value. That field is only used by do_refine_retval_range(), whose only caller is check_helper_call(), so kfuncs never use it.
regsafe() already requires the register types to match, so the SCALAR_VALUE check is safe to prune on. The buffer register, whose zero value does decide this branch, is already marked precise one iteration earlier:
if (bpf_register_is_null(reg) && type_may_be_null(kf_arg_type)) {
ret = mark_arg_precision(env, argno);Helpers are different, since check_helper_mem_access() lets a NULL mem through only when the size is 0, so the size value does matter there.
Looking at the two size cases separately:
For KF_ARG_CONST_MEM_SIZE (__szk), the only in-tree kfuncs with a __nullable buffer plus size are bpf_dynptr_slice() and bpf_dynptr_slice_rw(), each declared as 'void *buffer__nullable, u64 buffer__szk'. The value is used there for regs[R0].mem_size via meta->arg_constant.value, but process_const_arg() has already marked it precise just before the fallthrough:
case KF_ARG_CONST_MEM_SIZE:
ret = process_const_arg(env, reg, argno, meta); /* marks precise */
...
fallthrough;
case KF_ARG_MEM_SIZE:Commit f16e80c2c451 ("bpf: Fold __szk const size handling into the scalar arg path") closed exactly this gap, and the selftest verifier_mem_size_reg.c:dynptr_slice_null_buf_size_precise covers it. Isn't the new mark_arg_precision() call just re-marking the same register, at the cost of one more backtracking walk on every such call?
For KF_ARG_MEM_SIZE (__sz) with a nullable buffer, no in-tree kfunc has this combination. Any scalar size is accepted when the buffer is NULL, so pruning on an imprecise size cannot hide an unsafe state. Could marking it precise instead push precision back into parent states and their source registers (for example a loop counter)? That would reduce pruning and might stop open-coded iterator loops from converging, so programs that loaded before could hit the complexity limit with no soundness benefit.
Could the changelog name a concrete program that is wrongly accepted without this change? If there isn't one, the change and its rationale look unnecessary.
---
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/37642948251
Quoted 2 lines · Rahad Bhuiya
check_kfunc_args()allows a null buffer register paired withKF_ARG_MEM_SIZE. Whenbuff_regis null,check_mem_size_reg()is bypassed via break, leaving the corresponding size register at argno un-marked for precision.
This should already be fixed in f16e80c2c451 ("bpf: Fold __szk const size handling into the scalar arg path"), currently only in bpf-next.
Thanks for the pointer, Amery!
I verified commit f16e80c2c451 in bpf-next and indeed it addresses the precision gap nicely. Please disregard this patch.
Best regards,
Rahad
Quoted 7 lines · Amery Hung
Quoted 2 lines · Rahad Bhuiya
check_kfunc_args()allows a null buffer register paired withKF_ARG_MEM_SIZE. Whenbuff_regis null,check_mem_size_reg()is bypassed via break, leaving the corresponding size register at argno un-marked for precision.This should already be fixed in
f16e80c2c451("bpf: Fold__szkconst size handling into the scalar arg path"), currently only in bpf-next.