Fix generic __uninit kfunc output buffers
Subsystems (6)
Kumar Kartikeya Dwivedi
maintainerv4 cover Fix generic __uninit kfunc output buffers
Generic __uninit kfunc arguments are output buffers. Stack liveness treats them as writes, but argument checking still requires readable contents and does not record definite initialization after the call. Check these arguments as write-only and record their initialization after validating all inputs, including inputs that alias an output.
Following Eduard's rework, helpers and kfuncs now record generic outputs in the same argument-checking path after type resolution. Their memory access checks also use the same argument flags. This includes a separate helper
fix: MEM_WRITE without MEM_UNINIT requires read as well as write permission. The helper fix and its permission tests remain separate from the original kfunc fix and its immediate tests.
Changelog:
----------
v3 -> v4
v3: https://lore.kernel.org/bpf/[email protected]
* Record helper and kfunc outputs after type resolution. (Eduard, Amery)
* Derive generic memory access from flags for helpers and kfuncs. (Eduard)
* Fix MEM_WRITE read checks separately and add permission tests. (Eduard)
* Remove the output-count validator in the multiple-output extension. (Amery)
* Clarify argument-slot naming and move its accessor into the fix. (BPF CI)
* Add the unaligned header and order the new test registrations. (BPF CI)
* Shorten the kfunc fix description while retaining its rationale. (BPF CI)
v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]
* Reuse check_raw_mode_ok() after kfunc prototype generation, including struct outputs resolved to generic memory later. (Amery)
* Remove the now-redundant kfunc output-count check in the multiple-output extension and simplify the helper validator.
* Leave the stack-passed output uninitialized so the reduced-capability test detects missing __uninit handling. (Sashiko)
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Separate the single-output fix and tests from multiple-output support and its tests; reduce coverage to focused cases. (Eduard)
* Skip inactive output slots before looking up argument register state. (Sashiko, Amery)
* Separate sysctl restrictions from mitigation-related test skips. (BPF CI)
* Use an int-width initialization store in the alias test for big-endian targets. (BPF CI)
* Centralize conversion from argument numbers to slots. (Eduard)
* Share clear access-mode selection between fixed-size and sized arguments. (Amery)
* Clarify the opt-in prepare/load capability boundary and retain the reduced-capability alias rejection test. (Eduard)
Eduard Zingerman (3):
bpf: Record raw memory arguments during argument checking
bpf: Check read access for initialized writable memory arguments selftests/bpf: Cover helper memory access permissions
Kumar Kartikeya Dwivedi (5):
selftests/bpf: Allow privileged preparation for capability tests
bpf: Fix generic __uninit kfunc output buffers selftests/bpf: Cover generic __uninit output initialization
bpf: Support multiple __uninit kfunc output arguments selftests/bpf: Cover __uninit kfunc output argument slots
Documentation/bpf/kfuncs.rst | 27 +++- include/linux/bpf_verifier.h | 11 +- kernel/bpf/verifier.c | 131 ++++++++++-------- .../selftests/bpf/prog_tests/verifier.c | 4 + tools/testing/selftests/bpf/progs/bpf_misc.h | 9 +- .../progs/verifier_helper_access_var_len.c | 41 ++++++ .../bpf/progs/verifier_kfunc_uninit.c | 100 +++++++++++++ .../bpf/progs/verifier_kfunc_uninit_multi.c | 113 +++++++++++++++ .../selftests/bpf/progs/verifier_mtu.c | 75 ++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 45 ++++++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 7 + tools/testing/selftests/bpf/test_loader.c | 48 +++++-- tools/testing/selftests/bpf/unpriv_helpers.c | 16 ++- tools/testing/selftests/bpf/unpriv_helpers.h | 2 + 14 files changed, 536 insertions(+), 93 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c
base-commit: 961b8946acb482b6d7a39c266d623e5f9c4e873f
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 1/8 selftests/bpf: Allow privileged preparation for capability tests
The annotation-driven loader drops capabilities before libbpf prepares an object. Resolving bpf_testmod kfuncs requires CAP_SYS_ADMIN to enumerate and open module BTF, so tests without that capability fail before reaching the verifier.
Add an opt-in __prepare_priv annotation. Call bpf_object__prepare() with the fixture's initial capabilities, then apply __caps_unpriv before loading the programs. This uses libbpf's explicit prepare/load boundary. In particular, CAP_SYS_ADMIN must be dropped along with CAP_PERFMON to test uninitialized stack checks, since CAP_SYS_ADMIN satisfies the verifier's CAP_PERFMON check.
Preparation also creates maps and loads BTF. Keep it opt-in so existing tests continue checking those operations with reduced capabilities. The existing pre-execution callback runs after program loading and is too late for this.
Allow tests retaining CAP_BPF to run when the unprivileged-BPF sysctl is set. Check CPU mitigations separately: disabled or undetectable mitigations must still skip these tests, because CAP_BPF does not restore speculative execution checks.
--- tools/testing/selftests/bpf/progs/bpf_misc.h | 9 +++- tools/testing/selftests/bpf/test_loader.c | 48 ++++++++++++++------ tools/testing/selftests/bpf/unpriv_helpers.c | 16 +++++-- tools/testing/selftests/bpf/unpriv_helpers.h | 2 + 4 files changed, 55 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/progs/bpf_misc.h +8 -1
@@ -9,7 +9,8 @@#define QUOTE (str) #str#define EXPAND_QUOTE (str) QUOTE(str) -/* This set of attributes controls behavior of the +/* +* This set of attributes controls behavior of the * test_loader.c:test_loader__run_subtests(). * * The test_loader sequentially loads each program in a skeleton.
@@ -131,6 +132,11 @@* Several __arch_* annotations could be specified at once. * When test case is not run on current arch it is marked as skipped. * __caps_unpriv Specify the capabilities that should be set when running the test. +* __prepare_priv In unprivileged mode, prepare the object with the fixture's +* initial capabilities before dropping them for program loading. +* Preparation includes map creation and BTF/kfunc resolution; +* these operations are not tested at the reduced capabilities. +* Program loading uses the normal __caps_unpriv selection. * * __linear_size Specify the size of the linear area of non-linear skbs, or * 0 for linear skbs.
@@ -166,6 +172,7 @@#define __arch_s390x __arch("s390x")#define __arch_loongarch __arch("LOONGARCH")#define __caps_unpriv (caps) __test_tag("test_caps_unpriv=" EXPAND_QUOTE(caps)) +#define __prepare_priv __test_tag("test_prepare_priv")#define __load_if_JITed () __test_tag("load_mode=jited")#define __load_if_no_JITed () __test_tag("load_mode=no_jited")#define __stderr (msg) __test_tag("test_expect_stderr=" msg)
tools/testing/selftests/bpf/test_loader.c +33 -15
98 lines not on this page · raw message
tools/testing/selftests/bpf/unpriv_helpers.c +12 -4
@@ -111,9 +111,8 @@ static int get_mitigations_off(void)return !enabled_in_config; } -bool get_unpriv_disabled (void ) +bool get_unpriv_sysctl_disabled (void ) { -int mitigations_off;bool disabled;char buf[2 ];FILE *fd;
@@ -127,8 +126,12 @@ bool get_unpriv_disabled(void) disabled = true; } -if (disabled) -return true; +return disabled; +} + +bool get_unpriv_mitigations_disabled (void ) +{ +int mitigations_off;/* * Some unpriv tests rely on spectre mitigations being on.
@@ -144,6 +147,11 @@ bool get_unpriv_disabled(void)return mitigations_off; } +bool get_unpriv_disabled (void ) +{ +return get_unpriv_sysctl_disabled () ||get_unpriv_mitigations_disabled (); +} +bool get_kasan_jit_enabled (void ) {return config_contains ("CONFIG_BPF_JIT_KASAN=y" ) ==1 ;
tools/testing/selftests/bpf/unpriv_helpers.h +2 -0
@@ -5,5 +5,7 @@#define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"bool get_unpriv_disabled (void ); +bool get_unpriv_sysctl_disabled (void ); +bool get_unpriv_mitigations_disabled (void );bool get_kasan_jit_enabled (void );bool get_kasan_multi_shot_enabled (void );
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 2/8 bpf: Record raw memory arguments during argument checking
In reply to Kumar Kartikeya DwivediFrom: Eduard Zingerman <[email protected]>
Record meta->arg_raw_mem.regno in check_func_arg() instead of check_raw_mode_ok(). This information would be used by the patch fixing MEM_UNINIT handling for kfuncs further in the series.
For bpf_map_peek_elem() on a bloom filter, resolve_map_arg_type() does:
case BPF_MAP_TYPE_BLOOM_FILTER:
if (is_helper_call(meta, BPF_FUNC_map_peek_elem))
*arg_type = ARG_PTR_TO_MAP_VALUE;
That replaces the entire type, removing both MEM_UNINIT and MEM_WRITE. Recording the resolved type makes the later raw-mode clearing unnecessary.
In the check_mem_size_reg() only clear raw mode for a variable size when it belongs to the recorded output. An unrelated memory argument must not discard the output's identity after its initialization size has been recorded.
--- kernel/bpf/verifier.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
kernel/bpf/verifier.c +12 -13
@@ -7215,7 +7215,8 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,* raw mode so that the program is required to initialize all * the memory that the helper could just partially fill up. */ -if (!tnum_is_const (size_reg->var_off)) +if (!tnum_is_const (size_reg->var_off) && + meta->arg_raw_mem.regno ==reg_from_argno (mem_argno)) meta->arg_raw_mem.regno =0 ;if (reg_smin (size_reg) <0 ) {
@@ -8929,6 +8930,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 pif (err)return err; +if (!meta->btf && (arg_type &MEM_UNINIT ) && + (base_type (arg_type) ==ARG_PTR_TO_MEM || +base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE )) + meta->arg_raw_mem.regno = slot +1 ; +if (bpf_register_is_null (reg) &&type_may_be_null (arg_type)) { err =mark_arg_precision (env, argno);if (err)
@@ -9018,14 +9024,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 preturn -EFAULT ; } -/* -* Disable raw mode for bpf_map_peek_elem() on a bloom filter. The helper reads -* the value buffer as an input rather than filling it. -*/ -if (is_helper_call (meta, BPF_FUNC_map_peek_elem) && - meta->map.ptr->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) - meta->arg_raw_mem.regno =0 ; - err =check_helper_mem_access (env, reg, argno, meta->map.ptr->value_size, arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , false, meta,NULL );
@@ -9842,8 +9840,9 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,return -EINVAL ; } -static bool check_raw_mode_ok (const struct bpf_func_proto *fn,struct bpf_call_arg_meta *meta) +static bool check_raw_mode_ok (const struct bpf_func_proto *fn) { +bool seen = false;int i;for (i =0 ; i <ARRAY_SIZE (fn->arg_type); i++) {
@@ -9851,9 +9850,9 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_abreak ;if (!arg_type_is_raw_mem (fn->arg_type[i]))continue ; -if (meta->arg_raw_mem.regno) +if (seen)return false; - meta->arg_raw_mem.regno = i +1 ; + seen = true; }return true;
@@ -9985,7 +9984,7 @@ static int check_func_proto(struct bpf_verifier_env *env, const struct bpf_func_ struct bpf_call_arg_meta *meta) {return check_arg_prog_aux (env, fn) && -check_raw_mode_ok (fn, meta) && +check_raw_mode_ok (fn) &&check_arg_pair_ok (fn) &&check_mem_arg_rw_flag_ok (fn) && check_proto_release_reg(fn, meta) &&
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 2/8] bpf: Record raw memory arguments during argument checking
This isn't a bug, but would it help to say which helpers this actually changes (bpf_strtol/bpf_strtoul/bpf_kallsyms_lookup_name, where the unrelated variable-length input is checked before the…
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env, · line 7220
Quoted 8 lines
kernel/bpf/verifier.c +2
-1@@ -7215,7 +7215,8 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,* raw mode so that the program is required to initialize all * the memory that the helper could just partially fill up. */ -if (!tnum_is_const (size_reg->var_off)) +if (!tnum_is_const (size_reg->var_off) && + meta->arg_raw_mem.regno ==reg_from_argno (mem_argno)) meta->arg_raw_mem.regno =0 ;
This isn't a bug, but would it help to say which helpers this actually changes (bpf_strtol/bpf_strtoul/bpf_kallsyms_lookup_name, where the unrelated variable-length input is checked before the output is recorded) and that the effect is to stop withdrawing raw mode from their output buffer?
The commit message states that an unrelated memory argument must not discard the output's identity after its initialization size has been recorded. Scanning the existing helper protos, the three that have both an unrelated variable-size pair and a MEM_UNINIT output all place the variable-size input before the output:
bpf_strtol_proto / bpf_strtoul_proto (kernel/bpf/helpers.c) bpf_kallsyms_lookup_name_proto (kernel/bpf/syscall.c)
.arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
.arg2_type = ARG_MEM_SIZE[_OR_ZERO],
.arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | ...So the observable effect today is the opposite ordering: a variable buf_len / name_sz no longer withdraws raw mode from the arg4 output buffer.
[ ... ]
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p · line 8936
Quoted 8 lines
kernel/bpf/verifier.c +4
-0@@ -8929,6 +8930,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 pif (err)return err; +if (!meta->btf && (arg_type &MEM_UNINIT ) && + (base_type (arg_type) ==ARG_PTR_TO_MEM || +base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE )) + meta->arg_raw_mem.regno = slot +1 ;
This isn't a bug, but could this reuse arg_type_is_raw_mem() (defined at verifier.c:8155) so the recording site and check_raw_mode_ok() share one definition of a raw memory argument, or are the two predicates meant to diverge later in the series?
The new predicate is term-for-term the body of arg_type_is_raw_mem():
static bool arg_type_is_raw_mem(enum bpf_arg_type type)
{
return (base_type(type) == ARG_PTR_TO_MEM ||
base_type(type) == ARG_PTR_TO_MAP_VALUE) &&
type & MEM_UNINIT;
}which is still used by check_raw_mode_ok().
[ ... ]
---
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/35311871659
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 2/8] bpf: Record raw memory arguments during argument checking
Quoted 34 lines · On Fri, 2026-09-18 at 06:35 +0000, [email protected] wrote:
Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env, · line 7220
Quoted 8 lines
kernel/bpf/verifier.c +2
-1@@ -7215,7 +7215,8 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,* raw mode so that the program is required to initialize all * the memory that the helper could just partially fill up. */ -if (!tnum_is_const (size_reg->var_off)) +if (!tnum_is_const (size_reg->var_off) && + meta->arg_raw_mem.regno ==reg_from_argno (mem_argno)) meta->arg_raw_mem.regno = 0;This isn't a bug, but would it help to say which helpers this actually changes (bpf_strtol/bpf_strtoul/bpf_kallsyms_lookup_name, where the unrelated variable-length input is checked before the output is recorded) and that the effect is to stop withdrawing raw mode from their output buffer?
The commit message states that an unrelated memory argument must not discard the output's identity after its initialization size has been recorded. Scanning the existing helper protos, the three that have both an unrelated variable-size pair and a
MEM_UNINIToutput all place the variable-size input before the output:bpf_strtol_proto /
bpf_strtoul_proto(kernel/bpf/helpers.c)bpf_kallsyms_lookup_name_proto(kernel/bpf/syscall.c) .arg1_type =ARG_PTR_TO_MEM|MEM_RDONLY, .arg2_type = ARG_MEM_SIZE[_OR_ZERO],
.arg4_type =ARG_PTR_TO_FIXED_SIZE_MEM|MEM_UNINIT| ...
Quoted 3 lines
So the observable effect today is the opposite ordering: a variable
buf_len/name_szno longer withdraws raw mode from the arg4 output buffer.
Extending the commit message might make sense.
Quoted 18 lines
[ ... ]
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p · line 8936
Quoted 8 lines
kernel/bpf/verifier.c +4
-0@@ -8929,6 +8930,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p if (err) return err; +if (!meta->btf && (arg_type &MEM_UNINIT ) && + (base_type (arg_type) ==ARG_PTR_TO_MEM || +base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE )) + meta->arg_raw_mem.regno = slot +1 ;This isn't a bug, but could this reuse
arg_type_is_raw_mem()(defined at verifier.c:8155) so the recording site andcheck_raw_mode_ok()share one definition of a raw memory argument, or are the two predicates meant to diverge later in the series?The new predicate is term-for-term the body of
arg_type_is_raw_mem():
Quoted 8 lines
static bool
arg_type_is_raw_mem(enum bpf_arg_type type){
return (base_type(type)==ARG_PTR_TO_MEM||base_type(type)==ARG_PTR_TO_MAP_VALUE) && type &MEM_UNINIT;
}which is still used by
check_raw_mode_ok().
Let's indeed reuse the helper.
...
Kumar Kartikeya Dwivedi
maintainerPATCH v4 3/8 bpf: Check read access for initialized writable memory arguments
In reply to Kumar Kartikeya DwivediFrom: Eduard Zingerman <[email protected]>
MEM_WRITE without MEM_UNINIT denotes memory that may be read as well as written. Helper argument checking currently requests only BPF_WRITE, which checks stack initialization but omits read permission checks on map values. For example, bpf_check_mtu() reads its mtu_len argument before overwriting it, but the verifier permits that argument to point into a BPF_F_WRONLY_PROG map.
Derive generic memory access from argument flags: read-only for inputs, write-only for MEM_WRITE | MEM_UNINIT, and read/write for MEM_WRITE alone. Encode MEM_WRITE when classifying kfunc memory arguments and resolving scalar-only struct pointers, replacing the kfunc-specific access override.
This rejects write-only map values passed to helper arguments that require initialized writable memory. Generic kfunc __uninit output bookkeeping is completed in a following patch.
--- kernel/bpf/verifier.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +15 -9
@@ -8886,6 +8886,15 @@ static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_statreturn 0 ; } +static enum bpf_access_type func_arg_access_type (enum bpf_arg_type arg_type) +{ +if (!(arg_type &MEM_WRITE )) +return BPF_READ ; +if (arg_type &MEM_UNINIT ) +return BPF_WRITE ; +return BPF_READ |BPF_WRITE ; +} +static int check_func_arg (struct bpf_verifier_env *env,u32 arg,u32 slot,u32 prev_slot,struct bpf_call_arg_meta *meta,int insn_idx)
@@ -9180,9 +9189,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 pif (!(arg_type &MEM_FIXED_SIZE ))break ; - access_type = arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ ; -if (meta->btf) - access_type =BPF_READ |BPF_WRITE ; + access_type =func_arg_access_type (arg_type); err =check_mem_reg (env, reg, argno, arg_size, access_type, meta, &known_memory);if (err <0 ) {
@@ -9228,9 +9235,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 pif (meta->btf &&bpf_register_is_null (buff_reg))break ; - access_type = fn->arg_type[arg -1 ] &MEM_WRITE ?BPF_WRITE :BPF_READ ; -if (meta->btf) - access_type =BPF_READ |BPF_WRITE ; + access_type =func_arg_access_type (fn->arg_type[arg -1 ]); zero_size_allowed = meta->btf ||base_type (arg_type) ==ARG_MEM_SIZE_OR_ZERO ;
@@ -12436,7 +12441,8 @@ static int resolve_func_arg_type(struct bpf_verifier_env *env, PTR_ERR(resolve_ret));return -EINVAL ; } - *arg_type =ARG_PTR_TO_MEM |MEM_FIXED_SIZE | (*arg_type &PTR_MAYBE_NULL ); + *arg_type =ARG_PTR_TO_MEM |MEM_FIXED_SIZE |MEM_WRITE | + (*arg_type &PTR_MAYBE_NULL );return 0 ; }
@@ -12933,7 +12939,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, reg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);return -EINVAL ; } - arg_type =ARG_PTR_TO_MEM ; + arg_type =ARG_PTR_TO_MEM |MEM_WRITE ; }else if (btf_type_is_struct (ref_t))/* A pointer to a struct without a size argument is classified as ARG_PTR_TO_BTF_ID */ arg_type =ARG_PTR_TO_BTF_ID ;
@@ -12958,7 +12964,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,return -EINVAL ; } proto->arg_size[arg] = type_size; - arg_type =ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; + arg_type =ARG_PTR_TO_MEM |MEM_FIXED_SIZE |MEM_WRITE ; }if (is_kfunc_arg_uninit (meta->btf, &args[arg]))
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 3/8] bpf: Check read access for initialized writable memory arguments
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] bpf: MEM_WRITE without MEM_UNINIT enforces BPF_READ breaking output helpers --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] bpf: MEM_WRITE without MEM_UNINIT enforces BPF_READ breaking output helpers --
commit aebadfb01a3b32540321fe2b60bcb6fb1c8673be
Author: Eduard Zingerman <[email protected]>
bpf: Check read access for initialized writable memory arguments
This commit updates the BPF verifier to derive memory access types based on argument flags, correctly enforcing read access for writable memory arguments that are already initialized. The change addresses an issue where helper argument checking previously omitted read permission checks on map values. It adds a generic func_arg_access_type() helper to replace the kfunc-specific access overrides.
Quoted 6 lines
kernel/bpf/verifier.c +0
-0@@ -8886,6 +8886,15 @@ static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_statreturn 0 ;
kernel/bpf/verifier.c static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_stat · line 8896
Quoted 10 lines
kernel/bpf/verifier.c +8
-0} +static enum bpf_access_type func_arg_access_type (enum bpf_arg_type arg_type) +{ +if (!(arg_type &MEM_WRITE )) +return BPF_READ ; +if (arg_type &MEM_UNINIT ) +return BPF_WRITE ; +return BPF_READ |BPF_WRITE ; +}
[Severity: Critical]
Does this regression break pure output helpers that lack the MEM_UNINIT flag?
For example, bpf_snprintf() is defined as an output helper:
kernel/bpf/helpers.c:
const struct bpf_func_proto bpf_snprintf_proto = {
...
.arg1_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE,
...
};Because bpf_snprintf() does not guarantee full buffer initialization, it cannot be safely marked with MEM_UNINIT. However, with this change, the lack of MEM_UNINIT means func_arg_access_type() will return BPF_READ | BPF_WRITE instead of just BPF_WRITE.
Will this inadvertently force check_helper_mem_access() to reject valid write-only maps (BPF_F_WRONLY_PROG) when passed to such output helpers, causing valid BPF programs to fail verification?
Additionally, for unprivileged programs lacking CAP_PERFMON, won't this erroneously enforce stack buffer pre-initialization by enforcing a read check requiring stack initialization in check_stack_range_initialized()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Eduard Zingerman
maintainerRe: [PATCH bpf-next v4 3/8] bpf: Check read access for initialized writable memory arguments
Quoted 45 lines · On Fri, 2026-09-18 at 05:50 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] bpf:MEM_WRITEwithoutMEM_UNINITenforcesBPF_READbreaking output helpers --commit
aebadfb01a3b32540321fe2b60bcb6fb1c8673be
Author: Eduard Zingerman <[email protected]>bpf: Check read access for initialized writable memory arguments
This commit updates the BPF verifier to derive memory access types based on argument flags, correctly enforcing read access for writable memory arguments that are already initialized. The change addresses an issue where helper argument checking previously omitted read permission checks on map values. It adds a generic
func_arg_access_type()helper to replace the kfunc-specific access overrides.Quoted 6 lines
kernel/bpf/verifier.c +0
-0@@ -8886,6 +8886,15 @@ static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_statreturn 0 ;kernel/bpf/verifier.c static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_stat · line 8896
Quoted 10 lines
kernel/bpf/verifier.c +8
-0} +static enum bpf_access_type func_arg_access_type (enum bpf_arg_type arg_type) +{ +if (!(arg_type &MEM_WRITE )) +return BPF_READ ; +if (arg_type &MEM_UNINIT ) +return BPF_WRITE ; +return BPF_READ |BPF_WRITE ; +}[Severity: Critical]
Does this regression break pure output helpers that lack theMEM_UNINITflag?For example,
bpf_snprintf()is defined as an output helper:
kernel/bpf/helpers.c:const struct bpf_func_proto bpf_snprintf_proto = { ... .arg1_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE, ... };
Quoted 8 lines
Because
bpf_snprintf()does not guarantee full buffer initialization, it cannot be safely marked withMEM_UNINIT. However, with this change, the lack ofMEM_UNINITmeansfunc_arg_access_type()will returnBPF_READ|BPF_WRITEinstead of justBPF_WRITE.Will this inadvertently force
check_helper_mem_access()to reject valid write-only maps (BPF_F_WRONLY_PROG) when passed to such output helpers, causing valid BPF programs to fail verification?
I think it should be safe to relax MEM_UNINIT interpretation for priv and not to require full buffer initialization. That would be only a documentation change, as far as I understand. Then it would be fine to put MEM_UNINIT for snprintf. For unpriv and MEM_UNINIT buffer we can scratch the buffer as a combination of BPF_MISC (for previously filled parts) / BPF_INVALID (for previously invalid parts) instead of requiring full initialization. Kartikeya, could you please take a look if this requires any code changes?
Quoted 3 lines
Additionally, for unprivileged programs lacking
CAP_PERFMON, won't this erroneously enforce stack buffer pre-initialization by enforcing a read check requiring stack initialization incheck_stack_range_initialized()?
But that's not an error. If the helper reads the memory, there is no MEM_UNINIT annotation and we pass a pointer to uninitiated memory buffer to the program, then such a program should be rejected.
Re: [PATCH bpf-next v4 3/8] bpf: Check read access for initialized writable memory arguments
In reply to [email protected]Quoted 25 lines · On Thu, Sep 17, 2026 at 10:50 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] bpf:MEM_WRITEwithoutMEM_UNINITenforcesBPF_READbreaking output helpers --commit
aebadfb01a3b32540321fe2b60bcb6fb1c8673be
Author: Eduard Zingerman <[email protected]>bpf: Check read access for initialized writable memory arguments
This commit updates the BPF verifier to derive memory access types based on argument flags, correctly enforcing read access for writable memory arguments that are already initialized. The change addresses an issue where helper argument checking previously omitted read permission checks on map values. It adds a generic
func_arg_access_type()helper to replace the kfunc-specific access overrides.Quoted 8 lines
kernel/bpf/verifier.c +0
-0@@ -8886,6 +8886,15 @@ static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_statreturn 0 ; }
Quoted 11 lines
kernel/bpf/verifier.c static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_stat · line 8896
Quoted 8 lines
kernel/bpf/verifier.c +8
-0+static enum bpf_access_type func_arg_access_type (enum bpf_arg_type arg_type) +{ +if (!(arg_type &MEM_WRITE )) +return BPF_READ ; +if (arg_type &MEM_UNINIT ) +return BPF_WRITE ; +return BPF_READ |BPF_WRITE ; +}[Severity: Critical]
Does this regression break pure output helpers that lack theMEM_UNINITflag?
This is a valid concern.
I still think we should assign BPF_READ | BPF_WRITE to kfunc arguments when generating the prototype, and only BPF_WRITE for __uninit arguments. This would also avoid the need for func_arg_access_type().
Quoted 26 lines
For example,
bpf_snprintf()is defined as an output helper:
kernel/bpf/helpers.c:const struct bpf_func_proto bpf_snprintf_proto = { ... .arg1_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE, ... };Because
bpf_snprintf()does not guarantee full buffer initialization, it cannot be safely marked withMEM_UNINIT. However, with this change, the lack ofMEM_UNINITmeansfunc_arg_access_type()will returnBPF_READ|BPF_WRITEinstead of justBPF_WRITE.Will this inadvertently force
check_helper_mem_access()to reject valid write-only maps (BPF_F_WRONLY_PROG) when passed to such output helpers, causing valid BPF programs to fail verification?Additionally, for unprivileged programs lacking
CAP_PERFMON, won't this erroneously enforce stack buffer pre-initialization by enforcing a read check requiring stack initialization incheck_stack_range_initialized()?--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Kumar Kartikeya Dwivedi
maintainerPATCH v4 4/8 selftests/bpf: Cover helper memory access permissions
In reply to Kumar Kartikeya DwivediFrom: Eduard Zingerman <[email protected]>
Check that fixed-size and sized helper in/out buffers require both read and write permission. Cover bpf_check_mtu() and bpf_fib_lookup(), including read/write buffers as positive controls.
Keep rejecting read-only buffers for bpf_check_mtu(), and verify that a write-only map remains a valid destination for bpf_get_current_comm(), whose output is annotated MEM_UNINIT.
--- .../progs/verifier_helper_access_var_len.c | 41 ++++++++++ .../selftests/bpf/progs/verifier_mtu.c | 75 +++++++++++++++++++ 2 files changed, 116 insertions(+)
tools/testing/selftests/bpf/progs/verifier_helper_access_var_len.c +41 -0
@@ -822,4 +822,45 @@ __naked void bytes_no_leak_init_memory(void) : __clobber_all); } +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __uint(map_flags, BPF_F_WRONLY_PROG); + __type(key, __u32); + __type(value, struct bpf_fib_lookup); +} map_fib_wo SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, struct bpf_fib_lookup); +} map_fib_rw SEC(".maps"); + +SEC("tc") +__failure __msg("read from map forbidden") +int writeonly_sized_input(struct __sk_buff *ctx) +{ +struct bpf_fib_lookup *params; +__u32 key =0 ; + + params =bpf_map_lookup_elem (&map_fib_wo, &key); +if (params) +bpf_fib_lookup (ctx, params,sizeof (*params),0 ); +return 0 ; +} + +SEC("tc") +__success +int readwrite_sized_input (struct __sk_buff *ctx) +{ +struct bpf_fib_lookup *params; +__u32 key =0 ; + + params =bpf_map_lookup_elem (&map_fib_rw, &key); +if (params) +bpf_fib_lookup (ctx, params,sizeof (*params),0 ); +return 0 ; +} + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_mtu.c +75 -0
79 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 4/8] selftests/bpf: Cover helper memory access permissions
Does the first sentence accurately describe the test coverage added?
Quoted 7 lines
Check that fixed-size and sized helper in/out buffers require both read and write permission. Cover
bpf_check_mtu()andbpf_fib_lookup(), including read/write buffers as positive controls.Keep rejecting read-only buffers for
bpf_check_mtu(), and verify that a write-only map remains a valid destination forbpf_get_current_comm(), whose output is annotatedMEM_UNINIT.
Does the first sentence accurately describe the test coverage added?
For the fixed-size buffer case using bpf_check_mtu() (arg3 = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED), verifier_mtu.c adds all three test variants:
- map_mtu_wo (write-only map) -> expects "read from map forbidden"
- map_mtu_ro (read-only map) -> expects "write into map forbidden"
- map_mtu_rw (read-write map) -> __success
This covers both the read permission requirement and the write permission requirement for fixed-size buffers.
For the sized buffer case using bpf_fib_lookup() (arg2 = ARG_PTR_TO_MEM | MEM_WRITE with arg3 = ARG_MEM_SIZE), verifier_helper_access_var_len.c only adds:
- map_fib_wo (write-only map with BPF_F_WRONLY_PROG) -> expects "read from map forbidden"
- map_fib_rw (plain array) -> __success
There is no map_fib_ro test with a read-only map (BPF_F_RDONLY_PROG) expecting "write into map forbidden" to mirror tc_readonly_mtu, so the write-permission half of the claim is not exercised on the sized path even though check_mem_size_reg() runs the same BPF_READ-then-BPF_WRITE pair as check_mem_reg():
if (access_type & BPF_READ)
err = check_helper_mem_access(env, mem_reg, mem_argno, reg_umax(size_reg),
BPF_READ, zero_size_allowed, meta, NULL);
if (!err && access_type & BPF_WRITE)
err = check_helper_mem_access(env, mem_reg, mem_argno, reg_umax(size_reg),
BPF_WRITE, zero_size_allowed, meta, NULL);
Could you either add a BPF_F_RDONLY_PROG fib map test expecting "write into map forbidden" (mirroring tc_readonly_mtu), or narrow the first sentence to say the read-permission requirement is what is newly covered for the sized case (the later paragraph already scopes read-only rejection to bpf_check_mtu() only)?
---
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/35311871659
Kumar Kartikeya Dwivedi
maintainerPATCH v4 5/8 bpf: Fix generic __uninit kfunc output buffers
In reply to Kumar Kartikeya DwivediStack liveness treats __uninit kfunc arguments as writes. Even with write-only access checks, ordinary clobber handling leaves poisoned stack bytes poisoned after the call, so the output cannot be read.
Reuse the helper output descriptor for generic kfunc buffers. Record the output after resolving its memory type, and initialize its stack bytes only after all arguments have been checked. This prevents an output from making an aliased, uninitialized input appear valid.
Track the output by its ABI argument slot, since a kfunc pointer may be passed on the stack or follow a multi-slot by-value argument. Keep the single-output restriction in prototype validation; multiple-output tracking is a separate extension.
Document that output buffers must be fully initialized on every return path, including error returns and padding.
--- Documentation/bpf/kfuncs.rst | 27 ++++++++++---- include/linux/bpf_verifier.h | 7 ++-- kernel/bpf/verifier.c | 71 +++++++++++++++++++++++++++--------- 3 files changed, 78 insertions(+), 27 deletions(-)
Documentation/bpf/kfuncs.rst +20 -7
@@ -164,19 +164,32 @@ suffix should be used. 2.3.3 __uninit Annotation ------------------------- -This annotation is used to indicate that the argument will be treated as -uninitialized. +Use ``__uninit`` on a pointer parameter for an output that the kfunc +initializes without reading its incoming contents. -An example is given below:: +For generic memory buffers, the kfunc must initialize every byte in the +declared range on every return path, including error returns and struct +padding. The range is determined by the pointed-to type or the associated +``__sz`` or ``__szk`` size argument. + +The annotation does not change the accepted pointer types. A stack-backed +struct passed as a generic memory buffer must still be scalar-only. + +A stack buffer with a verifier-known constant offset and size may be +uninitialized before the call and is considered initialized afterwards. +For variable offsets or sizes, callers with neither ``CAP_PERFMON`` nor +``CAP_SYS_ADMIN`` must initialize the potentially accessed stack range before +the call. + +For dynptr parameters, ``__uninit`` indicates that the kfunc constructs a +dynptr in the supplied storage. For example:: - __bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr_kern *ptr__uninit) + __bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr *ptr__uninit) { ... } -Here, the dynptr will be treated as an uninitialized dynptr. Without this -annotation, the verifier will reject the program if the dynptr passed in is -not initialized. +Without this annotation, a dynptr argument must already be initialized. 2.3.4 __nullable Annotation ---------------------------
include/linux/bpf_verifier.h +4 -3
@@ -1548,10 +1548,11 @@ struct ref_obj_desc {/* * A memory argument a call fills in. The verifier allows the stack to be uninitialized if -* the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access(). +* the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access() +* after all arguments have been checked. */ struct arg_raw_mem_desc { -u8 regno; +u8 argno;/* One-based ABI argument slot; zero means no output. */ int size; };
@@ -1579,6 +1580,7 @@ struct bpf_call_arg_meta {struct bpf_dynptr_desc dynptr;struct ref_obj_desc ref_obj;struct ret_mem_desc ret_mem; +struct arg_raw_mem_desc arg_raw_mem;/* Only set by kfunc */ bool r0_rdonly;
@@ -1617,7 +1619,6 @@ struct bpf_call_arg_meta {s64 const_map_key;struct btf *ret_btf;struct btf_field *kptr_field; -struct arg_raw_mem_desc arg_raw_mem; }; int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id,
kernel/bpf/verifier.c +54 -17
135 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 5/8] bpf: Fix generic __uninit kfunc output buffers
Does this capability-conditioned requirement match what the verifier actually enforces?
Quoted 17 lines
Documentation/bpf/kfuncs.rst +7
-2@@ -164,19 +164,32 @@ suffix should be used. 2.3.3 __uninit Annotation ------------------------- -This annotation is used to indicate that the argument will be treated as -uninitialized. +Use ``__uninit`` on a pointer parameter for an output that the kfunc +initializes without reading its incoming contents. + +For generic memory buffers, the kfunc must initialize every byte in the +declared range on every return path, including error returns and struct +padding. The range is determined by the pointed-to type or the associated +``__sz`` or ``__szk`` size argument.
Documentation/bpf/kfuncs.rst suffix should be used. · line 182
Quoted 10 lines
Documentation/bpf/kfuncs.rst +8
-1+ +The annotation does not change the accepted pointer types. A stack-backed +struct passed as a generic memory buffer must still be scalar-only. -An example is given below:: +A stack buffer with a verifier-known constant offset and size may be +uninitialized before the call and is considered initialized afterwards. +For variable offsets or sizes, callers with neither ``CAP_PERFMON`` nor +``CAP_SYS_ADMIN`` must initialize the potentially accessed stack range before +the call.
Does this capability-conditioned requirement match what the verifier actually enforces?
Looking at check_stack_range_initialized() for the variable offset case, unprivileged callers are rejected outright:
kernel/bpf/verifier.c:check_stack_range_initialized() {
if (!env->bypass_spec_v1) {
...
verbose(env, "%s variable offset stack access prohibited for !root, var_off=%s\n",
reg_arg_name(env, argno), tn_buf);
return -EACCES;
}so pre-initializing the range does not make an unprivileged call verify. Privileged callers that pass bypass_spec_v1 then hit:
kernel/bpf/verifier.c:check_stack_range_initialized() {
...
raw_mode = false;which means exactly the callers the documentation exempts are the ones required to have the range initialized. The requirement appears inverted for variable offsets.
For the variable size case, check_mem_size_reg() clears the output descriptor unconditionally with no capability test:
kernel/bpf/verifier.c:check_mem_size_reg() {
if (!tnum_is_const(size_reg->var_off) &&
meta->arg_raw_mem.argno == arg_slot_from_argno(mem_argno) + 1)
meta->arg_raw_mem.argno = 0;With argno cleared, raw_mode becomes false for every caller, so every caller must have the whole reg_umax(size) range initialized beforehand, not just unprivileged callers.
The gate is also env->bypass_spec_v1 rather than a pure capability check. bpf_bypass_spec_v1() returns true for bpf_jit_bypass_spec_v1(), cpu_mitigations_off(), or bpf_token_capable(token, CAP_PERFMON), so the behavior also flips with mitigations=off or a JIT that bypasses spec-v1.
The commit message only mentions documenting that outputs must be fully initialized on return paths and does not claim anything about capabilities, so the capability sentence appears to be an unbacked addition. The pre-existing comment above check_mem_size_reg()'s clearing (line 7218-7222) has similar inaccuracy and may be the source.
Could the documentation drop the capability qualifier? A variable size always requires initialization, and a variable pointer offset additionally requires CAP_PERFMON/CAP_SYS_ADMIN merely to be accepted at all.
---
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/35311871659
Re: [PATCH bpf-next v4 5/8] bpf: Fix generic __uninit kfunc output buffers
In reply to Kumar Kartikeya DwivediQuoted 17 lines · Kumar Kartikeya Dwivedi
Stack liveness treats
__uninitkfunc arguments as writes. Even with write-only access checks, ordinary clobber handling leaves poisoned stack bytes poisoned after the call, so the output cannot be read.Reuse the helper output descriptor for generic kfunc buffers. Record the output after resolving its memory type, and initialize its stack bytes only after all arguments have been checked. This prevents an output from making an aliased, uninitialized input appear valid.
Track the output by its ABI argument slot, since a kfunc pointer may be passed on the stack or follow a multi-slot by-value argument. Keep the single-output restriction in prototype validation; multiple-output tracking is a separate extension.
Document that output buffers must be fully initialized on every return path, including error returns and padding.
Quoted 6 lines
Fixes 2cb27158adb3 ("bpf: poison dead stack slots")Reported-by Tejun Heo <[email protected]>Suggested-by Eduard Zingerman <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
Kumar Kartikeya Dwivedi
maintainerPATCH v4 6/8 selftests/bpf: Cover generic __uninit output initialization
In reply to Kumar Kartikeya DwivediExercise the struct and sized-buffer cases where stack liveness poisons an output before a kfunc call. Check that the verifier accepts these outputs and that the kfunc initializes the memory read after the call.
Verify that an uninitialized input aliasing an output is still rejected without CAP_PERFMON or CAP_SYS_ADMIN. Include an initialized alias as a positive control, using an int-width store so its value is independent of endianness.
Use __prepare_priv to resolve the test module's BTF before dropping to CAP_BPF and CAP_NET_ADMIN for program loading. Keep multiple-output and argument-slot coverage separate from these immediate regression tests.
--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_kfunc_uninit.c | 100 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 25 +++++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 3 + 4 files changed, 130 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -56,6 +56,7 @@#include "verifier_jeq_infer_not_null.skel.h" #include "verifier_jit_convergence.skel.h" #include "verifier_kfunc_packet_access.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"
@@ -222,6 +223,7 @@ void test_verifier_iterating_callbacks(void) { RUN(verifier_iterating_callbacksvoid 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_packet_access (void ) {RUN_TESTS (verifier_kfunc_packet_access); } +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 +100 -0
100 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +25 -0
@@ -17,6 +17,7 @@#include <linux/in.h> #include <linux/in6.h> #include <linux/un.h> +#include <linux/unaligned.h> #include <linux/filter.h> #include <linux/rcupdate_trace.h> #include <net/sock.h>
@@ -1316,6 +1317,27 @@ __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) { } +__bpf_kfunc void bpf_kfunc_test_uninit_struct(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_test_uninit_mem(void *out__uninit, u32 out__sz) +{ +memset (out__uninit,0x2a , out__sz); +} + +__bpf_kfunc int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in) +{ +int value =get_unaligned (in); + +put_unaligned (42 , out__uninit); +return value; +} + __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) { }
@@ -1747,6 +1769,9 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE) 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_test_uninit_struct) +BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_mem) +BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_alias) 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 +3 -0
@@ -289,6 +289,9 @@ __u64 bpf_kfunc_call_stack_arg_big(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e, 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_test_uninit_struct(struct prog_test_pass1 *out__uninit) __ksym; +void bpf_kfunc_test_uninit_mem(void *out__uninit, __u32 out__sz) __ksym; +int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in) __ksym; void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; void bpf_kfunc_call_test_destructive(void) __ksym;
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 7/8 bpf: Support multiple __uninit kfunc output arguments
In reply to Kumar Kartikeya DwivediA single output descriptor cannot retain the initialization ranges of two __uninit memory arguments. Track raw-mode eligibility and the definite output size separately for each ABI slot, so a variable-size output leaves independent constant-size outputs intact.
Use the shared argument-checking path to record outputs for both helpers and kfuncs. With per-slot tracking, neither needs the single-output prototype restriction. Initialize every recorded output after checking all arguments, skipping empty slots before looking up their register state.
Use the resolved BTF parameter when looking up __uninit for stack liveness. A preceding by-value parameter can consume multiple ABI slots, so its slot number cannot index the BTF parameter array.
--- include/linux/bpf_verifier.h | 10 ++--- kernel/bpf/verifier.c | 79 +++++++++++------------------------- 2 files changed, 28 insertions(+), 61 deletions(-)
include/linux/bpf_verifier.h +5 -5
@@ -1547,13 +1547,13 @@ struct ref_obj_desc { };/* -* A memory argument a call fills in. The verifier allows the stack to be uninitialized if -* the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access() -* after all arguments have been checked. +* Memory arguments a call fills in, indexed by argument slot. The verifier allows the +* stack to be uninitialized if the range is a known constant. Stack slots are marked as +* STACK_MISC by check_mem_access() after all arguments have been checked. */ struct arg_raw_mem_desc { -u8 argno;/* One-based ABI argument slot; zero means no output. */ -int size; +u16 mask; +int size[MAX_BPF_FUNC_ARGS ]; };/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
kernel/bpf/verifier.c +23 -56
146 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 8/8 selftests/bpf: Cover __uninit kfunc output argument slots
In reply to Kumar Kartikeya DwivediKeep coverage for per-slot output tracking separate from the immediate single-output regression tests. Check that both constant-size outputs are initialized, and that a variable-size output does not disable initialization of an independent constant-size output.
Also exercise an output following a by-value parameter that occupies two argument slots, and an output pointer passed on the stack. Run each case with normal capabilities and with CAP_BPF and CAP_NET_ADMIN only. Leave the stack-passed output uninitialized so its reduced-capability case fails if the verifier treats it as an ordinary input buffer.
--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_kfunc_uninit_multi.c | 113 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 20 ++++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 4 + 4 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -57,6 +57,7 @@#include "verifier_jit_convergence.skel.h" #include "verifier_kfunc_packet_access.skel.h" #include "verifier_kfunc_uninit.skel.h" +#include "verifier_kfunc_uninit_multi.skel.h" #include "verifier_ld_ind.skel.h" #include "verifier_ldsx.skel.h" #include "verifier_leak_ptr.skel.h"
@@ -224,6 +225,7 @@ void 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_packet_access (void ) {RUN_TESTS (verifier_kfunc_packet_access); }void test_verifier_kfunc_uninit (void ) {RUN_TESTS (verifier_kfunc_uninit); } +void test_verifier_kfunc_uninit_multi (void ) {RUN_TESTS (verifier_kfunc_uninit_multi); }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_multi.c new file +113 -0
113 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +20 -0
@@ -1338,6 +1338,23 @@ __bpf_kfunc int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in)return value; } +__bpf_kfunc void bpf_kfunc_test_uninit_multi(int *a__uninit, void *b__uninit, u32 b__sz) +{ +put_unaligned (42 , a__uninit); +memset (b__uninit,0x2a , b__sz); +} + +__bpf_kfunc void bpf_kfunc_test_uninit_pair(struct prog_test_pair_arg p, int *out__uninit) +{ +put_unaligned ((int )(p.lo + p.hi), out__uninit); +} + +__bpf_kfunc void bpf_kfunc_test_uninit_stack(u64 a, u64 b, u64 c, u64 d, u64 e, +int *out__uninit) +{ +put_unaligned ((int )(a + b + c + d + e), out__uninit); +} + __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) { }
@@ -1772,6 +1789,9 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2) BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_struct) BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_mem) BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_alias) +BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_multi) +BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_pair) +BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_stack) 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 +4 -0
@@ -292,6 +292,10 @@ void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; void bpf_kfunc_test_uninit_struct(struct prog_test_pass1 *out__uninit) __ksym; void bpf_kfunc_test_uninit_mem(void *out__uninit, __u32 out__sz) __ksym; int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in) __ksym; +void bpf_kfunc_test_uninit_multi(int *a__uninit, void *b__uninit, __u32 b__sz) __ksym; +void bpf_kfunc_test_uninit_pair(struct prog_test_pair_arg p, int *out__uninit) __ksym; +void bpf_kfunc_test_uninit_stack(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e, + int *out__uninit) __ksym; void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; void bpf_kfunc_call_test_destructive(void) __ksym;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 8/8] selftests/bpf: Cover __uninit kfunc output argument slots
A subsystem pattern flags this as potentially concerning: the new prog file progs/verifier_kfunc_uninit_multi.c (plus a new #include and a new test_verifier_kfunc_uninit_multi() entry in…
Quoted 41 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +2
-0@@ -57,6 +57,7 @@#include "verifier_jit_convergence.skel.h" #include "verifier_kfunc_packet_access.skel.h" #include "verifier_kfunc_uninit.skel.h" +#include "verifier_kfunc_uninit_multi.skel.h" #include "verifier_ld_ind.skel.h" #include "verifier_ldsx.skel.h" #include "verifier_leak_ptr.skel.h" @@ -224,6 +225,7 @@ void 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_packet_access (void ) {RUN_TESTS (verifier_kfunc_packet_access); }void test_verifier_kfunc_uninit (void ) {RUN_TESTS (verifier_kfunc_uninit); } +void test_verifier_kfunc_uninit_multi (void ) {RUN_TESTS (verifier_kfunc_uninit_multi); }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_multi.c new file +15
-0@@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" +#include "../test_kmods/bpf_testmod_kfunc.h" + +/* Keep the kfunc BTF records used by the inline assembly. */ +void __kfunc_btf_root (void ) +{ + asmvolatile ("" : + :"r" (&bpf_kfunc_test_uninit_multi), +"r" (&bpf_kfunc_test_uninit_pair), +"r" (&bpf_kfunc_test_uninit_stack));
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c line 25
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c new file +10
-0+} + +SEC("tc") +__success __retval(42) +__flag (BPF_F_TEST_STATE_FREQ) +__caps_unpriv(CAP_BPF | CAP_NET_ADMIN) +__prepare_priv +__success_unpriv +__naked voidmultiple_outputs (void) +{
A subsystem pattern flags this as potentially concerning: the new prog file progs/verifier_kfunc_uninit_multi.c (plus a new #include and a new test_verifier_kfunc_uninit_multi() entry in prog_tests/verifier.c) is added for the per-argument-slot variant of __uninit output tracking.
Looking at progs/verifier_kfunc_uninit.c in the same directory - added two commits earlier in the same series (40e810febe863) - it already covers __uninit kfunc output initialization and uses exactly the same
scaffolding: identical SPDX/copyright header, identical include set, an identically shaped __kfunc_btf_root() stub, and the same __flag(BPF_F_TEST_STATE_FREQ) + 'goto +0' poisoning idiom with the same __caps_unpriv(CAP_BPF | CAP_NET_ADMIN) __prepare_priv __success_unpriv tag block.
The four new cases (multiple_outputs, variable_size_preserves_other_output, output_after_by_value_argument, output_passed_on_stack) could be appended to verifier_kfunc_uninit.c as additional cases, avoiding the duplicated boilerplate and the extra skeleton/registration.
The commit message states an explicit rationale: "Keep coverage for per-slot output tracking separate from the immediate single-output regression tests." Splitting also keeps the arch-gated cases (__arch_x86_64/__arch_arm64 and the __BPF_FEATURE_STACK_ARGUMENT case) out of the arch-neutral file, and the BPF selftest Makefile globs progs/*.c so no build plumbing is needed.
Worth asking: is the split into a second file wanted, or should these be additional cases in verifier_kfunc_uninit.c?
Quoted 11 lines
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c new file +11
-0+ asmvolatile ( +"*(u64 *)(r10 - 16) = 0;" +"*(u64 *)(r10 - 8) = 0;" +"goto +0;" +"r1 = r10;" +"r1 += -16;" +"r2 = r10;" +"r2 += -8;" +"r3 = 8;" +"call %[bpf_kfunc_test_uninit_multi];" + "r0 = *(u32 *)(r10 - 16);"
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c line 46
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c new file +10
-0+"r1 = *(u32 *)(r10 - 8);" +"if r1 != 0x2a2a2a2a goto 1f;" +"r1 = *(u32 *)(r10 - 4);" +"if r1 == 0x2a2a2a2a goto 2f;" +"1:;" +"r0 = 0;" +"2:;" +"exit;" + : : __imm(bpf_kfunc_test_uninit_multi) : __clobber_all); +}
[ ... ]
---
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/35311871659