Skip to contentSkip to conversation

Fix generic __uninit kfunc output buffers

PATCH Superseded Kumar Kartikeya Dwivedi 4 participants 12 messages Sep 16 – Sep 17

Subsystems (6)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

v3 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.

Keep the single-output fix and its immediate regression tests separate from the extension for multiple outputs. The first three patches provide the capability-test prerequisite, the backportable kernel fix, and its tests. The kernel fix itself has no dependency on the test fixture. The final two patches add per-argument output tracking and its focused tests. This extension is optional; there is no current production consumer for multiple outputs.

The opt-in __prepare_priv annotation uses libbpf's prepare/load boundary to resolve module BTF before dropping CAP_SYS_ADMIN and CAP_PERFMON. Program loading then runs with the capabilities selected by __caps_unpriv. Ordinary unprivileged tests keep their existing preparation path, and disabled or undetectable CPU mitigations still cause the relevant tests to be skipped.

Changelog:
----------
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)

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                         |  93 ++++++++++----
 .../selftests/bpf/prog_tests/verifier.c       |   4 +
 tools/testing/selftests/bpf/progs/bpf_misc.h  |   9 +-
 .../bpf/progs/verifier_kfunc_uninit.c         | 100 ++++++++++++++++
 .../bpf/progs/verifier_kfunc_uninit_multi.c   | 113 ++++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  44 +++++++
 .../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 +
 12 files changed, 418 insertions(+), 56 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: 5ef40d69b38a93bc9951dadb1a15c85c597e1a40

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v3 1/5 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
index eb88d9ce6c34..2ced1d751ace 100644
@@ -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
index 2c8c5edb8751..c8dd5d848584 100644
@@ -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
index a7ceb51577cd..c24d53e14f3a 100644
@@ -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
author maintainer
#2 lore raw

PATCH v3 2/5 bpf: Fix generic __uninit kfunc output buffers

In reply to Kumar Kartikeya Dwivedi

Stack liveness treats __uninit kfunc arguments as writes, but generic memory argument validation still checks them as both reads and writes. A checkpoint can consequently poison an output buffer before the call and cause its read check to reject the program. Allowing that read would not suffice: ordinary clobber checks leave poisoned bytes poisoned after the call.

Check generic __uninit memory arguments as write-only and reuse the helper output descriptor to record definite initialization of a single output. Apply the writes after validating all arguments so an output does not make an aliased, uninitialized input appear valid. Look up the output register state only when there are recorded bytes to initialize.

Preserve MEM_UNINIT when a scalar-only struct pointer is resolved to a fixed-size memory argument. Use a common access-mode helper for fixed-size and sized arguments. Disable raw mode for a variable-size argument only when it is the tracked output.

Reuse check_raw_mode_ok() after generating the kfunc prototype to reject multiple generic outputs before checking any call arguments. Include struct pointers that are resolved to generic memory later. Record the actual output slot during call checking, where the ABI slot and resolved memory type are known.

Document that generic output buffers must be fully initialized on every return path, including error returns and padding. Multiple-output tracking is left to a separate change.

---
 Documentation/bpf/kfuncs.rst | 27 ++++++++---
 include/linux/bpf_verifier.h |  7 +--
 kernel/bpf/verifier.c        | 88 +++++++++++++++++++++++++++---------
 3 files changed, 90 insertions(+), 32 deletions(-)
Documentation/bpf/kfuncs.rst +20 -7
index 6c2c048dccef..71fb0ca72d69 100644
@@ -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
index cf85141ea167..3c1b06a3daf3 100644
@@ -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 regno; /* Register number, or one-based kfunc argument slot. */
 	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 +66 -22

175 lines not on this page · raw message

Signature · 1 line
2.53.0
Reviewed-by on v3 2/5
#3 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Fix generic __uninit kfunc output buffers

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p · line 9195

Quoted 10 lines

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)

kernel/bpf/verifier.c +3 -0
@@ -9176,15 +9190,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
                enum bpf_access_type access_type;
                bool known_memory;
 
+               if (meta->btf && (arg_type & MEM_UNINIT))
+                       meta->arg_raw_mem.regno = slot + 1;
+

Nit: Ideally check_raw_mode_ok should take slot instead of or with regno and we don't need it here, but it is getting larger than what the fix needs to be. I can do it as part of other func check refactoring if you want to keep it as is.

#4 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Fix generic __uninit kfunc output buffers

Quoted 17 lines · Amery Hung

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p · line 9195

Quoted 11 lines

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)

kernel/bpf/verifier.c +3 -0
@@ -9176,15 +9190,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
                enum bpf_access_type access_type;
                bool known_memory;
 
+               if (meta->btf && (arg_type & MEM_UNINIT))
+                       meta->arg_raw_mem.regno = slot + 1;
+

Nit: Ideally check_raw_mode_ok should take slot instead of or with regno and we don't need it here, but it is getting larger than what the fix needs to be. I can do it as part of other func check refactoring if you want to keep it as is.

Quoted 1 line

Actually after reading patch 4, I think we should either just drop check_raw_mode_ok() all together or keep it and fully populate mask there. This reduces the divergence in helper/kfunc handling.

#5 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Fix generic __uninit kfunc output buffers

In reply to Kumar Kartikeya Dwivedi
This isn't a bug, but could the middle paragraphs be trimmed to the decisions that are not obvious from the diff (for example why raw mode must only be dropped for the tracked output), rather than…
Quoted 30 lines
Documentation/bpf/kfuncs.rst +19 -2
index 6c2c048dccef6..71fb0ca72d698 100644
@@ -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.
+
+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::
 

Documentation/bpf/kfuncs.rst suffix should be used. · line 192

Quoted 10 lines
Documentation/bpf/kfuncs.rst +2 -4
-An example is given below::
+        __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.

[ ... ]

This isn't a bug, but could the middle paragraphs be trimmed to the decisions that are not obvious from the diff (for example why raw mode must only be dropped for the tracked output), rather than listing each hunk?

The first two paragraphs state the problem and the fix rationale well. The middle paragraphs then walk the diff hunk by hunk, with several sentences that restate a single line of code rather than explain a
decision: 'Use a common access-mode helper for fixed-size and sized arguments' (the new func_arg_access_type()), 'Look up the output register state only when there are recorded bytes to initialize' (the 'if (!meta->arg_raw_mem.size) return 0;' early exit), and 'Include struct pointers that are resolved to generic memory later' (the ARG_PTR_TO_BTF_ID term added to arg_type_is_raw_mem()).

A reader who has the diff learns little from those lines.

Quoted 7 lines
include/linux/bpf_verifier.h +0 -0
index cf85141ea1674..3c1b06a3daf30 100644
@@ -1548,10 +1548,11 @@ struct ref_obj_desc {
 
 /*

include/linux/bpf_verifier.h struct ref_obj_desc { · line 1557

Quoted 10 lines
include/linux/bpf_verifier.h +3 -2
  * 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 regno; /* Register number, or one-based kfunc argument slot. */
 	int size;
 };

This isn't a bug, but now that this holds a one-based argument slot for kfuncs, would naming it after the slot (e.g. arg_slot) read better next to argno_from_arg()/release_regno, so the comment does not have to carry two meanings?

The field keeps the name regno but, for kfuncs, now holds a one-based argument slot rather than a register number, and the comment is what carries that dual meaning. Every consumer converts it back:
mark_raw_stack() does 'u32 slot = meta->arg_raw_mem.regno - 1;' before get_func_arg_reg(), and check_stack_range_initialized()/ check_mem_size_reg() compare it against abs(argno.argno) rather than against a register number. The same file/function already uses distinct slot terminology (argno_t, argno_from_arg(), argno_from_reg(), reg_from_argno(), check_func_arg(env, arg, slot, prev_slot, ...)), and release_regno next to it really is a register number, so a field named regno holding a slot reads against the surrounding convention.

[ ... ]

Quoted 10 lines
kernel/bpf/verifier.c +1 -1
index 6c6b8d8520cdf..d7a40dc159aea 100644
@@ -6981,7 +6981,7 @@ static int check_stack_range_initialized(
 	 */
 	bool allow_poison = access_size < 0 || clobber;
 	/* The call will initialize the memory; uninitialized stack allowed */
-	bool raw_mode = meta && meta->arg_raw_mem.regno == reg_from_argno(argno);
+	bool raw_mode = meta && meta->arg_raw_mem.regno == abs(argno.argno);

[ ... ]

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 == abs(mem_argno.argno))
 		meta->arg_raw_mem.regno = 0;

This isn't a bug, but would a small arg_slot_from_argno() helper next to the other argno_t accessors read better than open-coding abs(argno.argno) in both check_stack_range_initialized() and check_mem_size_reg()?

The same expression abs(<argno>.argno) is open-coded in two places (check_stack_range_initialized() at line 6984 and check_mem_size_reg() at line 7219). argno_t is a wrapper struct (typedef at verifier.c:270-
272) with three dedicated accessors right next to it - reg_from_argno() (line 284), arg_from_argno() (line 293) and arg_idx_from_argno() (line 300). Grepping the file shows these two new lines are the only reads of the raw .argno member anywhere in verifier.c outside those four accessor functions; every other site goes through an accessor. Neither existing accessor has the needed semantics (reg_from_argno() returns -1 for argument slots beyond MAX_BPF_FUNC_REG_ARGS, arg_from_argno() returns -1 for register-encoded values), which is exactly why the raw member is being read here.

[ ... ]

---
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/35141528669

maintainer
#6 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Fix generic __uninit kfunc output buffers

In reply to Kumar Kartikeya Dwivedi

On Wed, 2026-09-16 at 21:27 +0200, Kumar Kartikeya Dwivedi wrote:

...

Quoted 21 lines
(quoted diff) +0 -0
@@ -8885,6 +8889,16 @@ static int process_map_ptr_arg(struct bpf_verifier_env *env, struct bpf_reg_stat
  	return 0;
  }

kernel/bpf/verifier.c +10 -0
+static enum bpf_access_type func_arg_access_type(enum bpf_arg_type arg_type,
+						 const struct bpf_call_arg_meta *meta)
+{
+	if (arg_type & MEM_UNINIT)
+		return BPF_WRITE;
+	if (meta->btf)
+		return BPF_READ | BPF_WRITE;
+	return arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ;
+}
+

 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)

kernel/bpf/verifier.c +0 -0
@@ -9176,15 +9190,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
  		enum bpf_access_type access_type;
  		bool known_memory;

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p · line 9195

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+		if (meta->btf && (arg_type & MEM_UNINIT))
+			meta->arg_raw_mem.regno = slot + 1;
+

Let's avoid adding special cases for kfuncs as in two hunks above. Please consider a rework as in [1], which makes processing of the arg_raw_mem.regno and MEM_WRITE bit identical for kfuncs and helpers. The details are in commit messages (partial slop).
Not identified incorrect handling of MEM_WRITE for helpers (MEM_WRITE should imply MEM_READ).

[1] https://github.com/eddyz87/bpf/tree/kfunc-uninit-tweaks

...

author maintainer
#7 lore raw

PATCH v3 3/5 selftests/bpf: Cover generic __uninit output initialization

In reply to Kumar Kartikeya Dwivedi

Exercise 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    |  24 +++++
 .../bpf/test_kmods/bpf_testmod_kfunc.h        |   3 +
 4 files changed, 129 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 7732df9bc870..4a9affe81ee1 100644
@@ -55,6 +55,7 @@
 #include "verifier_iterating_callbacks.skel.h"
 #include "verifier_jeq_infer_not_null.skel.h"
 #include "verifier_jit_convergence.skel.h"
+#include "verifier_kfunc_uninit.skel.h"
 #include "verifier_kfunc_packet_access.skel.h"
 #include "verifier_ld_ind.skel.h"
 #include "verifier_ldsx.skel.h"
@@ -221,6 +222,7 @@ void test_verifier_int_ptr(void)              { RUN(verifier_int_ptr); }
 void test_verifier_iterating_callbacks(void)  { RUN(verifier_iterating_callbacks); }
 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_uninit(void)         { RUN_TESTS(verifier_kfunc_uninit); }
 void test_verifier_kfunc_packet_access(void)  { RUN_TESTS(verifier_kfunc_packet_access); }
 void test_verifier_load_acquire(void)         { RUN(verifier_load_acquire); }
 void test_verifier_ld_ind(void)               { RUN(verifier_ld_ind); }
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 +24 -0
index fd2c0cdc91b1..dfffbdff06fa 100644
@@ -1316,6 +1316,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 +1768,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
index d3696d5254c9..91b64e783123 100644
@@ -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
Reviewed-by on v3 3/5
#8 lore raw

Re: [PATCH bpf-next v3 3/5] selftests/bpf: Cover generic __uninit output initialization

Quoted 10 lines · Kumar Kartikeya Dwivedi

Exercise 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.

Quoted 5 lines

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.

#9 lore raw

Re: [PATCH bpf-next v3 3/5] selftests/bpf: Cover generic __uninit output initialization

In reply to Kumar Kartikeya Dwivedi
This isn't a bug, but the include placement breaks the alphabetical ordering that the rest of this list follows - "kfunc_uninit" sorts after "kfunc_packet_access", so should this move one line down?

tools/testing/selftests/bpf/prog_tests/verifier.c line 61

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +1 -0
index 7732df9bc8701..4a9affe81ee16 100644
@@ -55,6 +55,7 @@
 #include "verifier_iterating_callbacks.skel.h"
 #include "verifier_jeq_infer_not_null.skel.h"
 #include "verifier_jit_convergence.skel.h"
+#include "verifier_kfunc_uninit.skel.h"
 #include "verifier_kfunc_packet_access.skel.h"
 #include "verifier_ld_ind.skel.h"
 #include "verifier_ldsx.skel.h"

This isn't a bug, but the include placement breaks the alphabetical ordering that the rest of this list follows - "kfunc_uninit" sorts after "kfunc_packet_access", so should this move one line down?

tools/testing/selftests/bpf/prog_tests/verifier.c void test_verifier_int_ptr(void) { RUN(verifier_int_ptr); } · line 228

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +1 -0
@@ -221,6 +222,7 @@ void test_verifier_int_ptr(void)              { RUN(verifier_int_ptr); }
 void test_verifier_iterating_callbacks(void)  { RUN(verifier_iterating_callbacks); }
 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_uninit(void)         { RUN_TESTS(verifier_kfunc_uninit); }
 void test_verifier_kfunc_packet_access(void)  { RUN_TESTS(verifier_kfunc_packet_access); }
 void test_verifier_load_acquire(void)         { RUN(verifier_load_acquire); }
 void test_verifier_ld_ind(void)               { RUN(verifier_ld_ind); }

Same ordering question here - should this also move down one line to maintain alphabetical order?

[ ... ]

Quoted 18 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +10 -0
index fd2c0cdc91b18..dfffbdff06fa9 100644
@@ -1316,6 +1316,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)
+{

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) · line 1338

Quoted 10 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +10 -0
+	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;
+}

This isn't a bug, but bpf_kfunc_test_uninit_alias() uses get_unaligned()/put_unaligned() which are new to this file. They currently resolve via a transitive include chain (linux/filter.h -> if_vlan.h -> etherdevice.h -> unaligned.h). Should <linux/unaligned.h> be added to the include block so the file doesn't depend on that chain?

---
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/35141528669

author maintainer
#10 lore raw

PATCH v3 4/5 bpf: Support multiple __uninit kfunc output arguments

In reply to Kumar Kartikeya Dwivedi

A 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 argument slot, so a variable-size output also leaves independent constant-size outputs intact.

Normalize helper register numbers and kfunc argument numbers through one slot conversion helper. After checking every argument, initialize each recorded output. Skip empty entries before looking up register state: an unused slot need not have an allocated stack-argument record. Helpers keep their existing single-output restriction in check_raw_mode_ok(). Remove the kfunc output-count check, which no longer constrains the prototype, and simplify the validator back to its helper-only role.

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. Keep this argument-slot handling with the extension rather than the minimal single-output regression fix.

---
 include/linux/bpf_verifier.h | 10 +++---
 kernel/bpf/verifier.c        | 65 ++++++++++++++++++------------------
 2 files changed, 38 insertions(+), 37 deletions(-)
include/linux/bpf_verifier.h +5 -5
index 3c1b06a3daf3..9ddbb20ec1e9 100644
@@ -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 regno; /* Register number, or one-based kfunc argument slot. */
-	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 +33 -32

147 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#11 lore raw

PATCH v3 5/5 selftests/bpf: Cover __uninit kfunc output argument slots

In reply to Kumar Kartikeya Dwivedi

Keep 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
index 4a9affe81ee1..f3c1428ac155 100644
@@ -56,6 +56,7 @@
 #include "verifier_jeq_infer_not_null.skel.h"
 #include "verifier_jit_convergence.skel.h"
 #include "verifier_kfunc_uninit.skel.h"
+#include "verifier_kfunc_uninit_multi.skel.h"
 #include "verifier_kfunc_packet_access.skel.h"
 #include "verifier_ld_ind.skel.h"
 #include "verifier_ldsx.skel.h"
@@ -223,6 +224,7 @@ void test_verifier_iterating_callbacks(void)  { RUN(verifier_iterating_callbacks
 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_uninit(void)         { RUN_TESTS(verifier_kfunc_uninit); }
+void test_verifier_kfunc_uninit_multi(void)   { RUN_TESTS(verifier_kfunc_uninit_multi); }
 void test_verifier_kfunc_packet_access(void)  { RUN_TESTS(verifier_kfunc_packet_access); }
 void test_verifier_load_acquire(void)         { RUN(verifier_load_acquire); }
 void test_verifier_ld_ind(void)               { RUN(verifier_ld_ind); }
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
index dfffbdff06fa..2fd5b6719308 100644
@@ -1337,6 +1337,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)
 {
 }
@@ -1771,6 +1788,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
index 91b64e783123..524f2cb9bdf4 100644
@@ -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