Skip to contentSkip to conversation

Fix generic __uninit kfunc output buffers

PATCH Superseded Kumar Kartikeya Dwivedi 4 participants 9 messages Sep 16

Subsystems (6)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

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

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:
----------
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   | 116 ++++++++++++++++++
 .../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, 421 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 v2 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 v2 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.

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        | 77 +++++++++++++++++++++++++++---------
 3 files changed, 82 insertions(+), 29 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 +58 -19

137 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

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

Quoted 5 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 +0 -0
@@ -9176,15 +9187,21 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
                enum bpf_access_type access_type;

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

Quoted 10 lines
kernel/bpf/verifier.c +8 -0
                bool known_memory;
 
+               if (meta->btf && (arg_type & MEM_UNINIT)) {
+                       if (meta->arg_raw_mem.regno) {
+                               verbose(env, "multiple __uninit buffers are not supported\n");
+                               return -EINVAL;
+                       }
+                       meta->arg_raw_mem.regno = slot + 1;
+               }
+

Lets resue helper proto validation for arg_raw_mem, check_raw_mode_ok(). For kfunc, the validation should happen after we finish generating the proto, i.e., at the end of gen_kfunc_arg_proto(). There is already one example:
check_arg_prog_aux().

Quoted 5 lines
kernel/bpf/verifier.c +0 -0
                /* The access to this pointer is only checked when we hit the
                 * next is_mem_size argument below.
                 */
                if (!(arg_type & MEM_FIXED_SIZE))
                        break;
author maintainer
#4 lore raw

PATCH v2 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
author maintainer
#5 lore raw

PATCH v2 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.

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        | 60 ++++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 32 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 -27

122 lines not on this page · raw message

Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH bpf-next v2 4/5] bpf: Support multiple __uninit kfunc output arguments

This looks like a bug fix for multiple __uninit kfunc output arguments. Should this include:
Quoted 11 lines

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.

Quoted 6 lines

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.

This looks like a bug fix for multiple __uninit kfunc output arguments. Should this include:

Fixes ae6abae582b7 ("bpf: Recognize by-value struct and __int128 kfunc arguments")

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

author maintainer
#7 lore raw

PATCH v2 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.

---
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../bpf/progs/verifier_kfunc_uninit_multi.c   | 116 ++++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  20 +++
 .../bpf/test_kmods/bpf_testmod_kfunc.h        |   4 +
 4 files changed, 142 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 +116 -0

116 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
#8 lore raw

Re: [PATCH bpf-next v2 5/5] selftests/bpf: Cover __uninit kfunc output argument slots

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] bpf: ineffective test output_passed_on_stack for __uninit tracking --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: ineffective test output_passed_on_stack for __uninit tracking --

commit 70a455597d07423eca32cc332d11faa394e65772
Author: Kumar Kartikeya Dwivedi <[email protected]>

selftests/bpf: Cover __uninit kfunc output argument slots

This commit adds regression tests for the verifier's tracking of __uninit kfunc output argument slots. It covers cases involving multiple outputs, outputs following by-value parameters, and output pointers passed on the stack.

Quoted 5 lines
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c new file +0 -0
new file mode 100644
index 0000000000000..b944346b35e34

[ ... ]

tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c line 100

Quoted 13 lines
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c new file +13 -0
+#if defined(__BPF_FEATURE_STACK_ARGUMENT)
+SEC("tc")
+__arch_x86_64 __arch_arm64 __arch_riscv64
+__success __retval(15)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void output_passed_on_stack(void)
+{
+	asm volatile (
+		"*(u64 *)(r10 - 8) = 0;"
+		"goto +0;"

[Severity: Medium]
Does pre-initializing the output stack slot here prevent the test from actually catching __uninit tracking regressions?

If the verifier fails to recognize the __uninit annotation, it treats the argument as a normal pointer. Because the slot is initialized and kept alive across the goto +0 checkpoint, the memory is tracked as STACK_ZERO. This satisfies the BPF_READ requirement in unprivileged mode, allowing the BPF_WRITE check to clobber the slot to STACK_MISC and verify successfully.

Should this test omit the initialization (similar to how it is done in output_after_by_value_argument) so that a broken verifier evaluates a BPF_READ on STACK_INVALID, forcing an expected verification failure in unprivileged mode?

Quoted 14 lines
tools/testing/selftests/bpf/progs/verifier_kfunc_uninit_multi.c new file +14 -0
+		"r1 = 1;"
+		"r2 = 2;"
+		"r3 = 3;"
+		"r4 = 4;"
+		"r5 = 5;"
+		"r6 = r10;"
+		"r6 += -8;"
+		"*(u64 *)(r11 - 8) = r6;"
+		"call %[bpf_kfunc_test_uninit_stack];"
+		"r0 = *(u32 *)(r10 - 8);"
+		"exit;"
+		: : __imm(bpf_kfunc_test_uninit_stack) : __clobber_all);
+}
+#endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5