| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/15 18:56 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL"
],
"FocusSymbols": [
"check_kfunc_call",
"check_helper_call",
"mark_raw_stack",
"check_stack_range_initialized",
"bpf_kfunc_stack_access_bytes"
],
"KMSANReasoning": "The patch series updates the BPF verifier's handling of kfunc arguments tagged with '__uninit' and raw memory arguments across helper and kfunc calls. Specifically, it expands 'struct arg_raw_mem_desc' in 'struct bpf_call_arg_meta' from tracking a single argument register and size to a bitmask and per-slot size array ('mask' and 'size[MAX_BPF_FUNC_ARGS]'), updates stack range checks and access types during verifier analysis, and adds 'mark_raw_stack()' to check and mark stack slots as written for all raw memory arguments. It also updates BPF selftests and documentation.\n\nKMSAN vs. KASAN applicability:\n- The changes are strictly within the BPF verifier's abstract analysis logic ('kernel/bpf/verifier.c', 'include/linux/bpf_verifier.h') and test code ('tools/testing/selftests/').\n- No kernel heap or stack buffers copied to userspace (via copy_to_user, put_user, netlink, ioctls, etc.) are introduced or altered.\n- All verifier metadata structures, including 'struct bpf_call_arg_meta', are fully zero-initialized (via memset) before use in 'check_helper_call()' and 'check_kfunc_call()'.\n- Local variables and loop counters introduced by the patch are explicitly initialized before evaluation.\n- Any potential bounds issues regarding argument slot array indexing ('size[MAX_BPF_FUNC_ARGS]') are bounds-checked in code or would represent out-of-bounds accesses detectable by KASAN, not uninitialized memory reads.\n- Furthermore, BPF execution runtime stack memory is either explicitly unpoisoned for KMSAN in the interpreter or run as JITted instructions without KMSAN compiler instrumentation.\n\nTherefore, this patch series does not introduce or expose uninitialized memory reads or kernel info-leaks, and running a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch modifies BPF verifier logic to support multiple uninitialized stack arguments for kfuncs and helpers (__uninit / raw memory). It introduces mark_raw_stack(), refactors arg_raw_mem tracking to support bitmasks and multiple argument slots, and updates check_stack_range_initialized(), check_func_arg(), check_kfunc_call(), and check_helper_call(). These changes affect core BPF verifier safety tracking and are fully reachable via the bpf(BPF_PROG_LOAD) system call.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/15 18:56 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 87c9e72c98fa84e06f40d66656613184dc97807c\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Tue Sep 15 18:56:56 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst\nindex 6c2c048dccef6..71fb0ca72d698 100644\n--- a/Documentation/bpf/kfuncs.rst\n+++ b/Documentation/bpf/kfuncs.rst\n@@ -164,19 +164,32 @@ suffix should be used.\n 2.3.3 __uninit Annotation\n -------------------------\n \n-This annotation is used to indicate that the argument will be treated as\n-uninitialized.\n+Use ``__uninit`` on a pointer parameter for an output that the kfunc\n+initializes without reading its incoming contents.\n \n-An example is given below::\n+For generic memory buffers, the kfunc must initialize every byte in the\n+declared range on every return path, including error returns and struct\n+padding. The range is determined by the pointed-to type or the associated\n+``__sz`` or ``__szk`` size argument.\n+\n+The annotation does not change the accepted pointer types. A stack-backed\n+struct passed as a generic memory buffer must still be scalar-only.\n+\n+A stack buffer with a verifier-known constant offset and size may be\n+uninitialized before the call and is considered initialized afterwards.\n+For variable offsets or sizes, callers with neither ``CAP_PERFMON`` nor\n+``CAP_SYS_ADMIN`` must initialize the potentially accessed stack range before\n+the call.\n+\n+For dynptr parameters, ``__uninit`` indicates that the kfunc constructs a\n+dynptr in the supplied storage. For example::\n \n- __bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr_kern *ptr__uninit)\n+ __bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr *ptr__uninit)\n {\n ...\n }\n \n-Here, the dynptr will be treated as an uninitialized dynptr. Without this\n-annotation, the verifier will reject the program if the dynptr passed in is\n-not initialized.\n+Without this annotation, a dynptr argument must already be initialized.\n \n 2.3.4 __nullable Annotation\n ---------------------------\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex cf85141ea1674..9ddbb20ec1e92 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -1547,12 +1547,13 @@ struct ref_obj_desc {\n };\n \n /*\n- * A memory argument a call fills in. The verifier allows the stack to be uninitialized if\n- * the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access().\n+ * Memory arguments a call fills in, indexed by argument slot. The verifier allows the\n+ * stack to be uninitialized if the range is a known constant. Stack slots are marked as\n+ * STACK_MISC by check_mem_access() after all arguments have been checked.\n */\n struct arg_raw_mem_desc {\n-\tu8 regno;\n-\tint size;\n+\tu16 mask;\n+\tint size[MAX_BPF_FUNC_ARGS];\n };\n \n /* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */\n@@ -1579,6 +1580,7 @@ struct bpf_call_arg_meta {\n \tstruct bpf_dynptr_desc dynptr;\n \tstruct ref_obj_desc ref_obj;\n \tstruct ret_mem_desc ret_mem;\n+\tstruct arg_raw_mem_desc arg_raw_mem;\n \n \t/* Only set by kfunc */\n \tbool r0_rdonly;\n@@ -1617,7 +1619,6 @@ struct bpf_call_arg_meta {\n \ts64 const_map_key;\n \tstruct btf *ret_btf;\n \tstruct btf_field *kptr_field;\n-\tstruct arg_raw_mem_desc arg_raw_mem;\n };\n \n int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id,\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex 6c6b8d8520cdf..070ec5daeec33 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -6981,7 +6981,9 @@ static int check_stack_range_initialized(\n \t */\n \tbool allow_poison = access_size \u003c 0 || clobber;\n \t/* The call will initialize the memory; uninitialized stack allowed */\n-\tbool raw_mode = meta \u0026\u0026 meta-\u003earg_raw_mem.regno == reg_from_argno(argno);\n+\tu32 arg_slot = abs(argno.argno) - 1;\n+\tbool raw_mode = meta \u0026\u0026 arg_slot \u003c MAX_BPF_FUNC_ARGS \u0026\u0026\n+\t\t (meta-\u003earg_raw_mem.mask \u0026 BIT(arg_slot));\n \n \taccess_size = abs(access_size);\n \n@@ -7023,7 +7025,7 @@ static int check_stack_range_initialized(\n \t}\n \n \tif (raw_mode) {\n-\t\tmeta-\u003earg_raw_mem.size = access_size;\n+\t\tmeta-\u003earg_raw_mem.size[arg_slot] = access_size;\n \t\treturn 0;\n \t}\n \n@@ -7216,7 +7218,7 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,\n \t * the memory that the helper could just partially fill up.\n \t */\n \tif (!tnum_is_const(size_reg-\u003evar_off))\n-\t\tmeta-\u003earg_raw_mem.regno = 0;\n+\t\tmeta-\u003earg_raw_mem.mask \u0026= ~BIT(abs(mem_argno.argno) - 1);\n \n \tif (reg_smin(size_reg) \u003c 0) {\n \t\tverbose(env, \"%s min value is negative, either use unsigned or 'var \u0026= const'\\n\",\n@@ -9024,7 +9026,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p\n \t\t */\n \t\tif (is_helper_call(meta, BPF_FUNC_map_peek_elem) \u0026\u0026\n \t\t meta-\u003emap.ptr-\u003emap_type == BPF_MAP_TYPE_BLOOM_FILTER)\n-\t\t\tmeta-\u003earg_raw_mem.regno = 0;\n+\t\t\tmeta-\u003earg_raw_mem.mask \u0026= ~BIT(slot);\n \n \t\terr = check_helper_mem_access(env, reg, argno, meta-\u003emap.ptr-\u003evalue_size,\n \t\t\t\t\t arg_type \u0026 MEM_WRITE ? BPF_WRITE : BPF_READ,\n@@ -9176,6 +9178,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p\n \t\tenum bpf_access_type access_type;\n \t\tbool known_memory;\n \n+\t\tif (meta-\u003ebtf \u0026\u0026 (arg_type \u0026 MEM_UNINIT))\n+\t\t\tmeta-\u003earg_raw_mem.mask |= BIT(slot);\n+\n \t\t/* The access to this pointer is only checked when we hit the\n \t\t * next is_mem_size argument below.\n \t\t */\n@@ -9184,7 +9189,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p\n \n \t\taccess_type = arg_type \u0026 MEM_WRITE ? BPF_WRITE : BPF_READ;\n \t\tif (meta-\u003ebtf)\n-\t\t\taccess_type = BPF_READ | BPF_WRITE;\n+\t\t\taccess_type = arg_type \u0026 MEM_UNINIT ? BPF_WRITE : BPF_READ | BPF_WRITE;\n \n \t\terr = check_mem_reg(env, reg, argno, arg_size, access_type, meta, \u0026known_memory);\n \t\tif (err \u003c 0) {\n@@ -9232,7 +9237,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p\n \n \t\taccess_type = fn-\u003earg_type[arg - 1] \u0026 MEM_WRITE ? BPF_WRITE : BPF_READ;\n \t\tif (meta-\u003ebtf)\n-\t\t\taccess_type = BPF_READ | BPF_WRITE;\n+\t\t\taccess_type = fn-\u003earg_type[arg - 1] \u0026 MEM_UNINIT ?\n+\t\t\t\t BPF_WRITE : BPF_READ | BPF_WRITE;\n \n \t\tzero_size_allowed = meta-\u003ebtf || base_type(arg_type) == ARG_MEM_SIZE_OR_ZERO;\n \n@@ -9550,6 +9556,33 @@ static int check_func_args(struct bpf_verifier_env *env, struct bpf_call_arg_met\n \treturn 0;\n }\n \n+static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n+\t\t\t int insn_idx)\n+{\n+\tstruct bpf_func_state *caller = cur_func(env);\n+\tu32 slot;\n+\tint i, err;\n+\n+\t/*\n+\t * Validate every argument before initializing outputs: an input argument\n+\t * may alias an output buffer. Use the normal stack-write checks to discard\n+\t * stale spills and preserve the rules for special stack objects.\n+\t */\n+\tfor (slot = 0; slot \u003c MAX_BPF_FUNC_ARGS; slot++) {\n+\t\tstruct bpf_reg_state *reg = get_func_arg_reg(caller, cur_regs(env), slot);\n+\t\targno_t argno = argno_from_arg(slot + 1);\n+\n+\t\tfor (i = 0; i \u003c meta-\u003earg_raw_mem.size[slot]; i++) {\n+\t\t\terr = check_mem_access(env, insn_idx, reg, argno, i, BPF_B,\n+\t\t\t\t\t BPF_WRITE, -1, false, false);\n+\t\t\tif (err)\n+\t\t\t\treturn err;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id)\n {\n \tenum bpf_attach_type eatype = env-\u003eprog-\u003eexpected_attach_type;\n@@ -9851,9 +9884,9 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a\n \t\t\tbreak;\n \t\tif (!arg_type_is_raw_mem(fn-\u003earg_type[i]))\n \t\t\tcontinue;\n-\t\tif (meta-\u003earg_raw_mem.regno)\n+\t\tif (meta-\u003earg_raw_mem.mask)\n \t\t\treturn false;\n-\t\tmeta-\u003earg_raw_mem.regno = i + 1;\n+\t\tmeta-\u003earg_raw_mem.mask = BIT(i);\n \t}\n \n \treturn true;\n@@ -11572,16 +11605,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn\n \n \tregs = cur_regs(env);\n \n-\t/* Mark slots with STACK_MISC in case of raw mode, stack offset\n-\t * is inferred from register state.\n-\t */\n-\tfor (i = 0; i \u003c meta.arg_raw_mem.size; i++) {\n-\t\terr = check_mem_access(env, insn_idx, regs + meta.arg_raw_mem.regno,\n-\t\t\t\t argno_from_reg(meta.arg_raw_mem.regno), i, BPF_B,\n-\t\t\t\t BPF_WRITE, -1, false, false);\n-\t\tif (err)\n-\t\t\treturn err;\n-\t}\n+\terr = mark_raw_stack(env, \u0026meta, insn_idx);\n+\tif (err)\n+\t\treturn err;\n \n \tif (meta.release_regno) {\n \t\tstruct bpf_reg_state *reg = \u0026regs[meta.release_regno];\n@@ -12437,7 +12463,7 @@ static int resolve_func_arg_type(struct bpf_verifier_env *env,\n \t\t\tPTR_ERR(resolve_ret));\n \t\treturn -EINVAL;\n \t}\n-\t*arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | (*arg_type \u0026 PTR_MAYBE_NULL);\n+\t*arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | (*arg_type \u0026 (PTR_MAYBE_NULL | MEM_UNINIT));\n \n \treturn 0;\n }\n@@ -13859,7 +13885,7 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *\n \t/* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */\n \tif (arg == 0 \u0026\u0026 meta.kfunc_flags \u0026 KF_ITER_NEW)\n \t\treturn -size;\n-\tif (is_kfunc_arg_uninit(btf, \u0026args[arg]))\n+\tif (is_kfunc_arg_uninit(btf, \u0026args[i]))\n \t\treturn -size;\n \treturn size;\n }\n@@ -14140,6 +14166,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tif (err \u003c 0)\n \t\treturn err;\n \n+\terr = mark_raw_stack(env, \u0026meta, insn_idx);\n+\tif (err)\n+\t\treturn err;\n+\n \tif ((is_bpf_obj_drop_kfunc(meta.func_id) ||\n \t is_bpf_percpu_obj_drop_kfunc(meta.func_id)) \u0026\u0026 (is_tracing_prog_type(prog_type) ||\n \t /* is_tracing_prog_type() for now doesn't cover non-iterator tracing progs. */\ndiff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c\nindex 7732df9bc8701..4a9affe81ee16 100644\n--- a/tools/testing/selftests/bpf/prog_tests/verifier.c\n+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c\n@@ -55,6 +55,7 @@\n #include \"verifier_iterating_callbacks.skel.h\"\n #include \"verifier_jeq_infer_not_null.skel.h\"\n #include \"verifier_jit_convergence.skel.h\"\n+#include \"verifier_kfunc_uninit.skel.h\"\n #include \"verifier_kfunc_packet_access.skel.h\"\n #include \"verifier_ld_ind.skel.h\"\n #include \"verifier_ldsx.skel.h\"\n@@ -221,6 +222,7 @@ void test_verifier_int_ptr(void) { RUN(verifier_int_ptr); }\n void test_verifier_iterating_callbacks(void) { RUN(verifier_iterating_callbacks); }\n void test_verifier_jeq_infer_not_null(void) { RUN(verifier_jeq_infer_not_null); }\n void test_verifier_jit_convergence(void) { RUN(verifier_jit_convergence); }\n+void test_verifier_kfunc_uninit(void) { RUN_TESTS(verifier_kfunc_uninit); }\n void test_verifier_kfunc_packet_access(void) { RUN_TESTS(verifier_kfunc_packet_access); }\n void test_verifier_load_acquire(void) { RUN(verifier_load_acquire); }\n void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }\ndiff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h\nindex eb88d9ce6c348..2ced1d751acea 100644\n--- a/tools/testing/selftests/bpf/progs/bpf_misc.h\n+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h\n@@ -9,7 +9,8 @@\n #define QUOTE(str) #str\n #define EXPAND_QUOTE(str) QUOTE(str)\n \n-/* This set of attributes controls behavior of the\n+/*\n+ * This set of attributes controls behavior of the\n * test_loader.c:test_loader__run_subtests().\n *\n * The test_loader sequentially loads each program in a skeleton.\n@@ -131,6 +132,11 @@\n * Several __arch_* annotations could be specified at once.\n * When test case is not run on current arch it is marked as skipped.\n * __caps_unpriv Specify the capabilities that should be set when running the test.\n+ * __prepare_priv In unprivileged mode, prepare the object with the fixture's\n+ * initial capabilities before dropping them for program loading.\n+ * Preparation includes map creation and BTF/kfunc resolution;\n+ * these operations are not tested at the reduced capabilities.\n+ * Program loading uses the normal __caps_unpriv selection.\n *\n * __linear_size Specify the size of the linear area of non-linear skbs, or\n * 0 for linear skbs.\n@@ -166,6 +172,7 @@\n #define __arch_s390x\t\t__arch(\"s390x\")\n #define __arch_loongarch\t__arch(\"LOONGARCH\")\n #define __caps_unpriv(caps)\t__test_tag(\"test_caps_unpriv=\" EXPAND_QUOTE(caps))\n+#define __prepare_priv\t\t__test_tag(\"test_prepare_priv\")\n #define __load_if_JITed()\t__test_tag(\"load_mode=jited\")\n #define __load_if_no_JITed()\t__test_tag(\"load_mode=no_jited\")\n #define __stderr(msg)\t\t__test_tag(\"test_expect_stderr=\" msg)\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c b/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c\nnew file mode 100644\nindex 0000000000000..99c0ee562ea03\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c\n@@ -0,0 +1,399 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"../test_kmods/bpf_testmod_kfunc.h\"\n+\n+/* Keep the kfunc BTF records used by the inline assembly. */\n+void __kfunc_btf_root(void)\n+{\n+\tasm volatile (\"\" :\n+\t\t: \"r\"(\u0026bpf_kfunc_test_uninit_scalar),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_struct),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_mem),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_mem_const),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_multi),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_alias),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_pair),\n+\t\t \"r\"(\u0026bpf_kfunc_test_uninit_stack));\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__flag(BPF_F_TEST_STATE_FREQ)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void scalar_poisoned_at_checkpoint(void)\n+{\n+\tasm volatile (\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"goto +0;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_scalar];\"\n+\t\t\"r0 = *(u32 *)(r10 - 8);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void scalar_uninitialized(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_scalar];\"\n+\t\t\"r0 = *(u32 *)(r10 - 8);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(10)\n+__flag(BPF_F_TEST_STATE_FREQ)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void struct_poisoned_at_checkpoint(void)\n+{\n+\tasm volatile (\n+\t\t\"*(u64 *)(r10 - 16) = 0;\"\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"goto +0;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -16;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_struct];\"\n+\t\t\"r0 = *(u32 *)(r10 - 16);\"\n+\t\t\"r1 = *(u32 *)(r10 - 12);\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r1 = *(u32 *)(r10 - 8);\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r1 = *(u32 *)(r10 - 4);\"\n+\t\t\"r0 += r1;\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_struct) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(10)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void struct_uninitialized(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -16;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_struct];\"\n+\t\t\"r0 = *(u32 *)(r10 - 16);\"\n+\t\t\"r1 = *(u32 *)(r10 - 12);\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r1 = *(u32 *)(r10 - 8);\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r1 = *(u32 *)(r10 - 4);\"\n+\t\t\"r0 += r1;\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_struct) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(0x2a2a2a2a)\n+__flag(BPF_F_TEST_STATE_FREQ)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void sized_buffer_poisoned_at_checkpoint(void)\n+{\n+\tasm volatile (\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"goto +0;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"r2 = 8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_mem];\"\n+\t\t\"r0 = *(u32 *)(r10 - 8);\"\n+\t\t\"r1 = *(u32 *)(r10 - 4);\"\n+\t\t\"if r0 == r1 goto +1;\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(0x2a2a2a2a)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void constant_size_buffer_uninitialized(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"r2 = 8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_mem_const];\"\n+\t\t\"r0 = *(u32 *)(r10 - 8);\"\n+\t\t\"r1 = *(u32 *)(r10 - 4);\"\n+\t\t\"if r0 == r1 goto +1;\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_mem_const) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__flag(BPF_F_TEST_STATE_FREQ)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void multiple_outputs(void)\n+{\n+\tasm volatile (\n+\t\t\"*(u64 *)(r10 - 16) = 0;\"\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"goto +0;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -16;\"\n+\t\t\"r2 = r10;\"\n+\t\t\"r2 += -8;\"\n+\t\t\"r3 = 8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_multi];\"\n+\t\t\"r0 = *(u32 *)(r10 - 16);\"\n+\t\t\"r1 = *(u32 *)(r10 - 8);\"\n+\t\t\"if r1 != 0x2a2a2a2a goto 1f;\"\n+\t\t\"r1 = *(u32 *)(r10 - 4);\"\n+\t\t\"if r1 == 0x2a2a2a2a goto 2f;\"\n+\t\"1:;\"\n+\t\t\"r0 = 0;\"\n+\t\"2:;\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_multi) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void variable_size_preserves_other_output(void)\n+{\n+\tasm volatile (\n+\t\t\"r3 = *(u32 *)(r1 + 0);\"\n+\t\t\"r3 \u0026= 7;\"\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -16;\"\n+\t\t\"r2 = r10;\"\n+\t\t\"r2 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_multi];\"\n+\t\t\"r0 = *(u32 *)(r10 - 16);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_multi) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void variable_size_initialized_buffer(void)\n+{\n+\tasm volatile (\n+\t\t\"r2 = *(u32 *)(r1 + 0);\"\n+\t\t\"r2 \u0026= 7;\"\n+\t\t\"r2 += 1;\"\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_mem];\"\n+\t\t\"r0 = *(u8 *)(r10 - 8);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__failure_unpriv __msg_unpriv(\"invalid read from stack\")\n+__naked void variable_size_uninitialized_buffer(void)\n+{\n+\tasm volatile (\n+\t\t\"r2 = *(u32 *)(r1 + 0);\"\n+\t\t\"r2 \u0026= 7;\"\n+\t\t\"r2 += 1;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_mem];\"\n+\t\t\"r0 = *(u8 *)(r10 - 8);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(42)\n+__naked void variable_offset_initialized_buffer(void)\n+{\n+\tasm volatile (\n+\t\t\"r2 = *(u32 *)(r1 + 0);\"\n+\t\t\"r2 \u0026= 4;\"\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"r6 = r10;\"\n+\t\t\"r6 += -8;\"\n+\t\t\"r6 += r2;\"\n+\t\t\"r1 = r6;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_scalar];\"\n+\t\t\"r0 = *(u32 *)(r6 + 0);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__failure_unpriv __msg_unpriv(\"invalid read from stack\")\n+__naked void output_does_not_initialize_adjacent_bytes(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_scalar];\"\n+\t\t\"r0 = *(u32 *)(r10 - 4);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(7)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void initialized_input_alias(void)\n+{\n+\tasm volatile (\n+\t\t\"*(u64 *)(r10 - 8) = 7;\"\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"r2 = r1;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_alias];\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_alias) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__success\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__failure_unpriv __msg_unpriv(\"invalid read from stack\")\n+__naked void uninitialized_input_alias(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -8;\"\n+\t\t\"r2 = r1;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_alias];\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_alias) : __clobber_all);\n+}\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(map_flags, BPF_F_WRONLY_PROG);\n+\t__type(key, __u32);\n+\t__type(value, int);\n+} write_only_map SEC(\".maps\");\n+\n+SEC(\"tc\")\n+__success __retval(0)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+int write_only_map_output(void *ctx)\n+{\n+\t__u32 key = 0;\n+\tint *out = bpf_map_lookup_elem(\u0026write_only_map, \u0026key);\n+\n+\tif (out)\n+\t\tbpf_kfunc_test_uninit_scalar(out);\n+\treturn 0;\n+}\n+\n+static const int read_only_output;\n+\n+SEC(\"tc\")\n+__failure __msg(\"write into map forbidden\")\n+int read_only_map_output(void *ctx)\n+{\n+\tbpf_kfunc_test_uninit_scalar((int *)\u0026read_only_output);\n+\treturn 0;\n+}\n+\n+SEC(\"tc\")\n+__failure __msg(\"stack R1 off=-2 size=4\")\n+__naked void output_out_of_bounds(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = r10;\"\n+\t\t\"r1 += -2;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_scalar];\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__arch_x86_64 __arch_arm64\n+__success __retval(42)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void output_after_by_value_argument(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = 20;\"\n+\t\t\"r2 = 22;\"\n+\t\t\"r3 = r10;\"\n+\t\t\"r3 += -8;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_pair];\"\n+\t\t\"r0 = *(u32 *)(r10 - 8);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_pair) : __clobber_all);\n+}\n+\n+#if defined(__BPF_FEATURE_STACK_ARGUMENT)\n+SEC(\"tc\")\n+__arch_x86_64 __arch_arm64 __arch_riscv64\n+__success __retval(15)\n+__flag(BPF_F_TEST_STATE_FREQ)\n+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)\n+__prepare_priv\n+__success_unpriv\n+__naked void output_passed_on_stack(void)\n+{\n+\tasm volatile (\n+\t\t\"*(u64 *)(r10 - 8) = 0;\"\n+\t\t\"goto +0;\"\n+\t\t\"r1 = 1;\"\n+\t\t\"r2 = 2;\"\n+\t\t\"r3 = 3;\"\n+\t\t\"r4 = 4;\"\n+\t\t\"r5 = 5;\"\n+\t\t\"r6 = r10;\"\n+\t\t\"r6 += -8;\"\n+\t\t\"*(u64 *)(r11 - 8) = r6;\"\n+\t\t\"call %[bpf_kfunc_test_uninit_stack];\"\n+\t\t\"r0 = *(u32 *)(r10 - 8);\"\n+\t\t\"exit;\"\n+\t\t: : __imm(bpf_kfunc_test_uninit_stack) : __clobber_all);\n+}\n+#endif\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\nindex fd2c0cdc91b18..4015cf4fc416c 100644\n--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\n+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\n@@ -1316,6 +1316,54 @@ __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)\n {\n }\n \n+__bpf_kfunc void bpf_kfunc_test_uninit_scalar(int *out__uninit)\n+{\n+\tput_unaligned(42, out__uninit);\n+}\n+\n+__bpf_kfunc void bpf_kfunc_test_uninit_struct(struct prog_test_pass1 *out__uninit)\n+{\n+\tout__uninit-\u003ex0 = 1;\n+\tout__uninit-\u003ex1 = 2;\n+\tout__uninit-\u003ex2 = 3;\n+\tout__uninit-\u003ex3 = 4;\n+}\n+\n+__bpf_kfunc void bpf_kfunc_test_uninit_mem(void *out__uninit, u32 out__sz)\n+{\n+\tmemset(out__uninit, 0x2a, out__sz);\n+}\n+\n+__bpf_kfunc void bpf_kfunc_test_uninit_mem_const(void *out__uninit, u32 out__szk)\n+{\n+\tmemset(out__uninit, 0x2a, out__szk);\n+}\n+\n+__bpf_kfunc void bpf_kfunc_test_uninit_multi(int *a__uninit, void *b__uninit, u32 b__sz)\n+{\n+\tput_unaligned(42, a__uninit);\n+\tmemset(b__uninit, 0x2a, b__sz);\n+}\n+\n+__bpf_kfunc int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in)\n+{\n+\tint value = get_unaligned(in);\n+\n+\tput_unaligned(42, out__uninit);\n+\treturn value;\n+}\n+\n+__bpf_kfunc void bpf_kfunc_test_uninit_pair(struct prog_test_pair_arg p, int *out__uninit)\n+{\n+\tput_unaligned((int)(p.lo + p.hi), out__uninit);\n+}\n+\n+__bpf_kfunc void bpf_kfunc_test_uninit_stack(u64 a, u64 b, u64 c, u64 d, u64 e,\n+\t\t\t\t\t int *out__uninit)\n+{\n+\tput_unaligned((int)(a + b + c + d + e), out__uninit);\n+}\n+\n __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)\n {\n }\n@@ -1747,6 +1795,14 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_scalar)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_struct)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_mem)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_mem_const)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_multi)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_alias)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_pair)\n+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_stack)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)\ndiff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h\nindex d3696d5254c9e..2ec8192a102fe 100644\n--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h\n+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h\n@@ -289,6 +289,15 @@ __u64 bpf_kfunc_call_stack_arg_big(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,\n void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;\n void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;\n void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;\n+void bpf_kfunc_test_uninit_scalar(int *out__uninit) __ksym;\n+void bpf_kfunc_test_uninit_struct(struct prog_test_pass1 *out__uninit) __ksym;\n+void bpf_kfunc_test_uninit_mem(void *out__uninit, __u32 out__sz) __ksym;\n+void bpf_kfunc_test_uninit_mem_const(void *out__uninit, __u32 out__szk) __ksym;\n+void bpf_kfunc_test_uninit_multi(int *a__uninit, void *b__uninit, __u32 b__sz) __ksym;\n+int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in) __ksym;\n+void bpf_kfunc_test_uninit_pair(struct prog_test_pair_arg p, int *out__uninit) __ksym;\n+void bpf_kfunc_test_uninit_stack(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,\n+\t\t\t\t int *out__uninit) __ksym;\n void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;\n \n void bpf_kfunc_call_test_destructive(void) __ksym;\ndiff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c\nindex 28724de063220..940b6d8e47ba4 100644\n--- a/tools/testing/selftests/bpf/test_loader.c\n+++ b/tools/testing/selftests/bpf/test_loader.c\n@@ -71,6 +71,7 @@ struct test_spec {\n \tint load_mask;\n \tint linear_sz;\n \tconst char *skip_reason;\n+\tbool prepare_priv;\n \tbool auxiliary;\n \tbool valid;\n };\n@@ -606,6 +607,8 @@ static int parse_test_spec(struct test_loader *tester,\n \t\t\tif (err)\n \t\t\t\tgoto cleanup;\n \t\t\tspec-\u003emode_mask |= UNPRIV;\n+\t\t} else if (strcmp(s, \"test_prepare_priv\") == 0) {\n+\t\t\tspec-\u003eprepare_priv = true;\n \t\t} else if ((val = str_has_pfx(s, \"load_mode=\"))) {\n \t\t\tif (strcmp(val, \"jited\") == 0) {\n \t\t\t\tload_mask = JITED;\n@@ -1015,10 +1018,10 @@ struct cap_state {\n \tbool initialized;\n };\n \n-static int drop_capabilities(struct cap_state *caps)\n+static int drop_capabilities(struct cap_state *caps, __u64 keep_caps)\n {\n \tconst __u64 caps_to_drop = (1ULL \u003c\u003c CAP_SYS_ADMIN | 1ULL \u003c\u003c CAP_NET_ADMIN |\n-\t\t\t\t 1ULL \u003c\u003c CAP_PERFMON | 1ULL \u003c\u003c CAP_BPF);\n+\t\t\t\t 1ULL \u003c\u003c CAP_PERFMON | 1ULL \u003c\u003c CAP_BPF) \u0026 ~keep_caps;\n \tint err;\n \n \terr = cap_disable_effective(caps_to_drop, \u0026caps-\u003eold_caps);\n@@ -1028,6 +1031,13 @@ static int drop_capabilities(struct cap_state *caps)\n \t}\n \n \tcaps-\u003einitialized = true;\n+\tif (keep_caps) {\n+\t\terr = cap_enable_effective(keep_caps, NULL);\n+\t\tif (err) {\n+\t\t\tPRINT_FAIL(\"failed to set capabilities: %i, %s\\n\", err, strerror(-err));\n+\t\t\treturn err;\n+\t\t}\n+\t}\n \treturn 0;\n }\n \n@@ -1049,7 +1059,7 @@ static bool can_execute_unpriv(struct test_loader *tester, struct test_spec *spe\n {\n \tif (sysctl_unpriv_disabled \u003c 0)\n \t\tsysctl_unpriv_disabled = get_unpriv_disabled() ? 1 : 0;\n-\tif (sysctl_unpriv_disabled)\n+\tif (sysctl_unpriv_disabled \u0026\u0026 !(spec-\u003eunpriv.caps \u0026 (1ULL \u003c\u003c CAP_BPF)))\n \t\treturn false;\n \tif ((spec-\u003eprog_flags \u0026 BPF_F_ANY_ALIGNMENT) \u0026\u0026 !EFFICIENT_UNALIGNED_ACCESS)\n \t\treturn false;\n@@ -1351,17 +1361,8 @@ void run_subtest(struct test_loader *tester,\n \t\t\ttest__end_subtest();\n \t\t\treturn;\n \t\t}\n-\t\tif (drop_capabilities(\u0026caps)) {\n-\t\t\ttest__end_subtest();\n-\t\t\treturn;\n-\t\t}\n-\t\tif (subspec-\u003ecaps) {\n-\t\t\terr = cap_enable_effective(subspec-\u003ecaps, NULL);\n-\t\t\tif (err) {\n-\t\t\t\tPRINT_FAIL(\"failed to set capabilities: %i, %s\\n\", err, strerror(-err));\n-\t\t\t\tgoto subtest_cleanup;\n-\t\t\t}\n-\t\t}\n+\t\tif (!spec-\u003eprepare_priv \u0026\u0026 drop_capabilities(\u0026caps, subspec-\u003ecaps))\n+\t\t\tgoto subtest_cleanup;\n \t}\n \n \t/* Implicitly reset to NULL if next test case doesn't specify.\n@@ -1414,6 +1415,18 @@ void run_subtest(struct test_loader *tester,\n \tbpf_object__for_each_map(map, tobj)\n \t\tbpf_map__set_autocreate(map, !unpriv || is_unpriv_capable_map(map));\n \n+\tif (unpriv \u0026\u0026 spec-\u003eprepare_priv) {\n+\t\t/*\n+\t\t * Module BTF lookup needs CAP_SYS_ADMIN. Allow tests to prepare\n+\t\t * their objects first, then verify programs with the requested caps.\n+\t\t */\n+\t\terr = bpf_object__prepare(tobj);\n+\t\tif (!ASSERT_OK(err, \"obj_prepare\"))\n+\t\t\tgoto tobj_cleanup;\n+\t\tif (drop_capabilities(\u0026caps, subspec-\u003ecaps))\n+\t\t\tgoto tobj_cleanup;\n+\t}\n+\n \terr = bpf_object__load(tobj);\n \tif (subspec-\u003eexpect_failure) {\n \t\tif (!ASSERT_ERR(err, \"unexpected_load_success\")) {\n"
} |
| 2/1 | 2026/09/15 18:56 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL"
],
"FocusSymbols": [
"check_kfunc_call",
"check_helper_call",
"mark_raw_stack",
"check_stack_range_initialized",
"bpf_kfunc_stack_access_bytes"
],
"Reasoning": "The patch modifies BPF verifier logic to support multiple uninitialized stack arguments for kfuncs and helpers (__uninit / raw memory). It introduces mark_raw_stack(), refactors arg_raw_mem tracking to support bitmasks and multiple argument slots, and updates check_stack_range_initialized(), check_func_arg(), check_kfunc_call(), and check_helper_call(). These changes affect core BPF verifier safety tracking and are fully reachable via the bpf(BPF_PROG_LOAD) system call.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 87c9e72c98fa84e06f40d66656613184dc97807c
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Sep 15 18:56:56 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index 6c2c048dccef6..71fb0ca72d698 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -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
---------------------------
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index cf85141ea1674..9ddbb20ec1e92 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1547,12 +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().
+ * 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;
- int size;
+ u16 mask;
+ int size[MAX_BPF_FUNC_ARGS];
};
/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
@@ -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,
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6c6b8d8520cdf..070ec5daeec33 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6981,7 +6981,9 @@ 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);
+ u32 arg_slot = abs(argno.argno) - 1;
+ bool raw_mode = meta && arg_slot < MAX_BPF_FUNC_ARGS &&
+ (meta->arg_raw_mem.mask & BIT(arg_slot));
access_size = abs(access_size);
@@ -7023,7 +7025,7 @@ static int check_stack_range_initialized(
}
if (raw_mode) {
- meta->arg_raw_mem.size = access_size;
+ meta->arg_raw_mem.size[arg_slot] = access_size;
return 0;
}
@@ -7216,7 +7218,7 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,
* the memory that the helper could just partially fill up.
*/
if (!tnum_is_const(size_reg->var_off))
- meta->arg_raw_mem.regno = 0;
+ meta->arg_raw_mem.mask &= ~BIT(abs(mem_argno.argno) - 1);
if (reg_smin(size_reg) < 0) {
verbose(env, "%s min value is negative, either use unsigned or 'var &= const'\n",
@@ -9024,7 +9026,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
*/
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;
+ meta->arg_raw_mem.mask &= ~BIT(slot);
err = check_helper_mem_access(env, reg, argno, meta->map.ptr->value_size,
arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ,
@@ -9176,6 +9178,9 @@ 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.mask |= BIT(slot);
+
/* The access to this pointer is only checked when we hit the
* next is_mem_size argument below.
*/
@@ -9184,7 +9189,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
access_type = arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ;
if (meta->btf)
- access_type = BPF_READ | BPF_WRITE;
+ access_type = arg_type & MEM_UNINIT ? BPF_WRITE : BPF_READ | BPF_WRITE;
err = check_mem_reg(env, reg, argno, arg_size, access_type, meta, &known_memory);
if (err < 0) {
@@ -9232,7 +9237,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
access_type = fn->arg_type[arg - 1] & MEM_WRITE ? BPF_WRITE : BPF_READ;
if (meta->btf)
- access_type = BPF_READ | BPF_WRITE;
+ access_type = fn->arg_type[arg - 1] & MEM_UNINIT ?
+ BPF_WRITE : BPF_READ | BPF_WRITE;
zero_size_allowed = meta->btf || base_type(arg_type) == ARG_MEM_SIZE_OR_ZERO;
@@ -9550,6 +9556,33 @@ static int check_func_args(struct bpf_verifier_env *env, struct bpf_call_arg_met
return 0;
}
+static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
+ int insn_idx)
+{
+ struct bpf_func_state *caller = cur_func(env);
+ u32 slot;
+ int i, err;
+
+ /*
+ * Validate every argument before initializing outputs: an input argument
+ * may alias an output buffer. Use the normal stack-write checks to discard
+ * stale spills and preserve the rules for special stack objects.
+ */
+ for (slot = 0; slot < MAX_BPF_FUNC_ARGS; slot++) {
+ struct bpf_reg_state *reg = get_func_arg_reg(caller, cur_regs(env), slot);
+ argno_t argno = argno_from_arg(slot + 1);
+
+ for (i = 0; i < meta->arg_raw_mem.size[slot]; i++) {
+ err = check_mem_access(env, insn_idx, reg, argno, i, BPF_B,
+ BPF_WRITE, -1, false, false);
+ if (err)
+ return err;
+ }
+ }
+
+ return 0;
+}
+
static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id)
{
enum bpf_attach_type eatype = env->prog->expected_attach_type;
@@ -9851,9 +9884,9 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a
break;
if (!arg_type_is_raw_mem(fn->arg_type[i]))
continue;
- if (meta->arg_raw_mem.regno)
+ if (meta->arg_raw_mem.mask)
return false;
- meta->arg_raw_mem.regno = i + 1;
+ meta->arg_raw_mem.mask = BIT(i);
}
return true;
@@ -11572,16 +11605,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
regs = cur_regs(env);
- /* Mark slots with STACK_MISC in case of raw mode, stack offset
- * is inferred from register state.
- */
- for (i = 0; i < meta.arg_raw_mem.size; i++) {
- err = check_mem_access(env, insn_idx, regs + meta.arg_raw_mem.regno,
- argno_from_reg(meta.arg_raw_mem.regno), i, BPF_B,
- BPF_WRITE, -1, false, false);
- if (err)
- return err;
- }
+ err = mark_raw_stack(env, &meta, insn_idx);
+ if (err)
+ return err;
if (meta.release_regno) {
struct bpf_reg_state *reg = ®s[meta.release_regno];
@@ -12437,7 +12463,7 @@ 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 | (*arg_type & (PTR_MAYBE_NULL | MEM_UNINIT));
return 0;
}
@@ -13859,7 +13885,7 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *
/* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */
if (arg == 0 && meta.kfunc_flags & KF_ITER_NEW)
return -size;
- if (is_kfunc_arg_uninit(btf, &args[arg]))
+ if (is_kfunc_arg_uninit(btf, &args[i]))
return -size;
return size;
}
@@ -14140,6 +14166,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (err < 0)
return err;
+ err = mark_raw_stack(env, &meta, insn_idx);
+ if (err)
+ return err;
+
if ((is_bpf_obj_drop_kfunc(meta.func_id) ||
is_bpf_percpu_obj_drop_kfunc(meta.func_id)) && (is_tracing_prog_type(prog_type) ||
/* is_tracing_prog_type() for now doesn't cover non-iterator tracing progs. */
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 7732df9bc8701..4a9affe81ee16 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -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); }
diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index eb88d9ce6c348..2ced1d751acea 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -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)
diff --git a/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c b/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c
new file mode 100644
index 0000000000000..99c0ee562ea03
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c
@@ -0,0 +1,399 @@
+// 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)
+{
+ asm volatile ("" :
+ : "r"(&bpf_kfunc_test_uninit_scalar),
+ "r"(&bpf_kfunc_test_uninit_struct),
+ "r"(&bpf_kfunc_test_uninit_mem),
+ "r"(&bpf_kfunc_test_uninit_mem_const),
+ "r"(&bpf_kfunc_test_uninit_multi),
+ "r"(&bpf_kfunc_test_uninit_alias),
+ "r"(&bpf_kfunc_test_uninit_pair),
+ "r"(&bpf_kfunc_test_uninit_stack));
+}
+
+SEC("tc")
+__success __retval(42)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void scalar_poisoned_at_checkpoint(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = 0;"
+ "goto +0;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void scalar_uninitialized(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(10)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void struct_poisoned_at_checkpoint(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 16) = 0;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "goto +0;"
+ "r1 = r10;"
+ "r1 += -16;"
+ "call %[bpf_kfunc_test_uninit_struct];"
+ "r0 = *(u32 *)(r10 - 16);"
+ "r1 = *(u32 *)(r10 - 12);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 8);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 4);"
+ "r0 += r1;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_struct) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(10)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void struct_uninitialized(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -16;"
+ "call %[bpf_kfunc_test_uninit_struct];"
+ "r0 = *(u32 *)(r10 - 16);"
+ "r1 = *(u32 *)(r10 - 12);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 8);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 4);"
+ "r0 += r1;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_struct) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(0x2a2a2a2a)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void sized_buffer_poisoned_at_checkpoint(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = 0;"
+ "goto +0;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = 8;"
+ "call %[bpf_kfunc_test_uninit_mem];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "r1 = *(u32 *)(r10 - 4);"
+ "if r0 == r1 goto +1;"
+ "r0 = 0;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(0x2a2a2a2a)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void constant_size_buffer_uninitialized(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = 8;"
+ "call %[bpf_kfunc_test_uninit_mem_const];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "r1 = *(u32 *)(r10 - 4);"
+ "if r0 == r1 goto +1;"
+ "r0 = 0;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem_const) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void multiple_outputs(void)
+{
+ asm volatile (
+ "*(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);"
+ "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);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void variable_size_preserves_other_output(void)
+{
+ asm volatile (
+ "r3 = *(u32 *)(r1 + 0);"
+ "r3 &= 7;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "r1 = r10;"
+ "r1 += -16;"
+ "r2 = r10;"
+ "r2 += -8;"
+ "call %[bpf_kfunc_test_uninit_multi];"
+ "r0 = *(u32 *)(r10 - 16);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_multi) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void variable_size_initialized_buffer(void)
+{
+ asm volatile (
+ "r2 = *(u32 *)(r1 + 0);"
+ "r2 &= 7;"
+ "r2 += 1;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_mem];"
+ "r0 = *(u8 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__failure_unpriv __msg_unpriv("invalid read from stack")
+__naked void variable_size_uninitialized_buffer(void)
+{
+ asm volatile (
+ "r2 = *(u32 *)(r1 + 0);"
+ "r2 &= 7;"
+ "r2 += 1;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_mem];"
+ "r0 = *(u8 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__naked void variable_offset_initialized_buffer(void)
+{
+ asm volatile (
+ "r2 = *(u32 *)(r1 + 0);"
+ "r2 &= 4;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "r6 = r10;"
+ "r6 += -8;"
+ "r6 += r2;"
+ "r1 = r6;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r6 + 0);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__failure_unpriv __msg_unpriv("invalid read from stack")
+__naked void output_does_not_initialize_adjacent_bytes(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r10 - 4);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(7)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void initialized_input_alias(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = 7;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = r1;"
+ "call %[bpf_kfunc_test_uninit_alias];"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_alias) : __clobber_all);
+}
+
+SEC("tc")
+__success
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__failure_unpriv __msg_unpriv("invalid read from stack")
+__naked void uninitialized_input_alias(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = r1;"
+ "call %[bpf_kfunc_test_uninit_alias];"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_alias) : __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, int);
+} write_only_map SEC(".maps");
+
+SEC("tc")
+__success __retval(0)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+int write_only_map_output(void *ctx)
+{
+ __u32 key = 0;
+ int *out = bpf_map_lookup_elem(&write_only_map, &key);
+
+ if (out)
+ bpf_kfunc_test_uninit_scalar(out);
+ return 0;
+}
+
+static const int read_only_output;
+
+SEC("tc")
+__failure __msg("write into map forbidden")
+int read_only_map_output(void *ctx)
+{
+ bpf_kfunc_test_uninit_scalar((int *)&read_only_output);
+ return 0;
+}
+
+SEC("tc")
+__failure __msg("stack R1 off=-2 size=4")
+__naked void output_out_of_bounds(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -2;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = 0;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__arch_x86_64 __arch_arm64
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void output_after_by_value_argument(void)
+{
+ asm volatile (
+ "r1 = 20;"
+ "r2 = 22;"
+ "r3 = r10;"
+ "r3 += -8;"
+ "call %[bpf_kfunc_test_uninit_pair];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_pair) : __clobber_all);
+}
+
+#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;"
+ "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
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index fd2c0cdc91b18..4015cf4fc416c 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -1316,6 +1316,54 @@ __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
{
}
+__bpf_kfunc void bpf_kfunc_test_uninit_scalar(int *out__uninit)
+{
+ put_unaligned(42, out__uninit);
+}
+
+__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 void bpf_kfunc_test_uninit_mem_const(void *out__uninit, u32 out__szk)
+{
+ memset(out__uninit, 0x2a, out__szk);
+}
+
+__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 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_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)
{
}
@@ -1747,6 +1795,14 @@ 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_scalar)
+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_mem_const)
+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_multi)
+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_alias)
+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)
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
index d3696d5254c9e..2ec8192a102fe 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
@@ -289,6 +289,15 @@ __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_scalar(int *out__uninit) __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;
+void bpf_kfunc_test_uninit_mem_const(void *out__uninit, __u32 out__szk) __ksym;
+void bpf_kfunc_test_uninit_multi(int *a__uninit, void *b__uninit, __u32 b__sz) __ksym;
+int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in) __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;
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 28724de063220..940b6d8e47ba4 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -71,6 +71,7 @@ struct test_spec {
int load_mask;
int linear_sz;
const char *skip_reason;
+ bool prepare_priv;
bool auxiliary;
bool valid;
};
@@ -606,6 +607,8 @@ static int parse_test_spec(struct test_loader *tester,
if (err)
goto cleanup;
spec->mode_mask |= UNPRIV;
+ } else if (strcmp(s, "test_prepare_priv") == 0) {
+ spec->prepare_priv = true;
} else if ((val = str_has_pfx(s, "load_mode="))) {
if (strcmp(val, "jited") == 0) {
load_mask = JITED;
@@ -1015,10 +1018,10 @@ struct cap_state {
bool initialized;
};
-static int drop_capabilities(struct cap_state *caps)
+static int drop_capabilities(struct cap_state *caps, __u64 keep_caps)
{
const __u64 caps_to_drop = (1ULL << CAP_SYS_ADMIN | 1ULL << CAP_NET_ADMIN |
- 1ULL << CAP_PERFMON | 1ULL << CAP_BPF);
+ 1ULL << CAP_PERFMON | 1ULL << CAP_BPF) & ~keep_caps;
int err;
err = cap_disable_effective(caps_to_drop, &caps->old_caps);
@@ -1028,6 +1031,13 @@ static int drop_capabilities(struct cap_state *caps)
}
caps->initialized = true;
+ if (keep_caps) {
+ err = cap_enable_effective(keep_caps, NULL);
+ if (err) {
+ PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err));
+ return err;
+ }
+ }
return 0;
}
@@ -1049,7 +1059,7 @@ static bool can_execute_unpriv(struct test_loader *tester, struct test_spec *spe
{
if (sysctl_unpriv_disabled < 0)
sysctl_unpriv_disabled = get_unpriv_disabled() ? 1 : 0;
- if (sysctl_unpriv_disabled)
+ if (sysctl_unpriv_disabled && !(spec->unpriv.caps & (1ULL << CAP_BPF)))
return false;
if ((spec->prog_flags & BPF_F_ANY_ALIGNMENT) && !EFFICIENT_UNALIGNED_ACCESS)
return false;
@@ -1351,17 +1361,8 @@ void run_subtest(struct test_loader *tester,
test__end_subtest();
return;
}
- if (drop_capabilities(&caps)) {
- test__end_subtest();
- return;
- }
- if (subspec->caps) {
- err = cap_enable_effective(subspec->caps, NULL);
- if (err) {
- PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err));
- goto subtest_cleanup;
- }
- }
+ if (!spec->prepare_priv && drop_capabilities(&caps, subspec->caps))
+ goto subtest_cleanup;
}
/* Implicitly reset to NULL if next test case doesn't specify.
@@ -1414,6 +1415,18 @@ void run_subtest(struct test_loader *tester,
bpf_object__for_each_map(map, tobj)
bpf_map__set_autocreate(map, !unpriv || is_unpriv_capable_map(map));
+ if (unpriv && spec->prepare_priv) {
+ /*
+ * Module BTF lookup needs CAP_SYS_ADMIN. Allow tests to prepare
+ * their objects first, then verify programs with the requested caps.
+ */
+ err = bpf_object__prepare(tobj);
+ if (!ASSERT_OK(err, "obj_prepare"))
+ goto tobj_cleanup;
+ if (drop_capabilities(&caps, subspec->caps))
+ goto tobj_cleanup;
+ }
+
err = bpf_object__load(tobj);
if (subspec->expect_failure) {
if (!ASSERT_ERR(err, "unexpected_load_success")) {
|
| 3/2 | 2026/09/15 18:56 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 15229 output: 33 thoughts: 1610 Thoughts: **Analysis of BPF Verifier Commit for Syzkaller Fuzzing Potential** Okay, so the task is to determine if this commit warrants fuzzing with syzkaller. First, I'll assess the modified files, starting with the diff. The changes primarily touch the BPF verifier logic. I see modifications to `Documentation/bpf/kfuncs.rst`, `include/linux/bpf_verifier.h`, and, significantly, `kernel/bpf/verifier.c`. Also, various `selftests` related to BPF are modified. Looking deeper at `kernel/bpf/verifier.c` and `include/linux/bpf_verifier.h`, there's a clear impact on core BPF verifier functionality. Specifically, changes in `struct arg_raw_mem_desc` and `struct bpf_call_arg_meta` are notable. The structure `arg_raw_mem_desc` changed, and the organization of `arg_raw_mem` within `bpf_call_arg_meta` has been altered. I'm focusing on `kernel/bpf/verifier.c` now. The `check_stack_range_initialized`, `check_mem_size_reg`, `check_func_arg`, `check_raw_mode_ok`, `resolve_func_arg_type`, `bpf_kfunc_stack_access_bytes`, and `check_kfunc_call` functions have been modified, and a new function `mark_raw_stack` is introduced. These are all critical verifier functions. Now, considering reachability, the BPF verifier is definitely reachable. Any BPF program loaded via `sys_bpf` goes through this code path, hitting functions like `check_helper_call`, `check_kfunc_call`, `mark_raw_stack`, etc. Syzkaller is known to heavily test the `bpf()` syscall. According to the rules, since this patch modifies BPF, which is a core subsystem, the "WorthFuzzing" criteria is met. I have to extract functions for "FocusSymbols". Given that `mark_raw_stack` is a new function, and others are modified, I should be looking at `check_stack_range_initialized`, `check_mem_size_reg`, `check_func_arg`, `mark_raw_stack`, `check_raw_mode_ok`, `check_helper_call`, `resolve_func_arg_type`, `bpf_kfunc_stack_access_bytes`, and `check_kfunc_call`. Now, I need to check whether `mark_raw_stack` or any of the callers are symbols or inlined. Even if `mark_raw_stack` is static in .c, it will exist in the kallsyms and vmlinux symbol table. These are specific functions that implement the logic being altered, or direct API entrypoints for the feature. I need to make sure I'm not listing ubiquitous lifecycle hot-paths. I'm going to carefully review the surrounding code in `kernel/bpf/verifier.c` to understand the function signatures and configurations better. |
| 4/2 | 2026/09/15 18:57 | tool | grepper |
0mArgs:{
"Expression": "mark_raw_stack",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=9499=static int check_func_args(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-9558-\nkernel/bpf/verifier.c:9559:static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\nkernel/bpf/verifier.c-9560-\t\t\t int insn_idx)\n--\nkernel/bpf/verifier.c=11508=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11607-\nkernel/bpf/verifier.c:11608:\terr = mark_raw_stack(env, \u0026meta, insn_idx);\nkernel/bpf/verifier.c-11609-\tif (err)\n--\nkernel/bpf/verifier.c=14069=static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14168-\nkernel/bpf/verifier.c:14169:\terr = mark_raw_stack(env, \u0026meta, insn_idx);\nkernel/bpf/verifier.c-14170-\tif (err)\n"
} |
| 5/2 | 2026/09/15 18:57 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 17935 output: 25 thoughts: 20 Thoughts: Here's my thought process, as if I were considering this myself: **Investigating `bpf_kfunc_stack_access_bytes`** Okay, so I need to dig into this `bpf_kfunc_stack_access_bytes` function. I should start by getting a handle on *what* it's doing, obviously. But beyond that, I need to understand its context. Where is it being used? Who calls it? What kind of data is it interacting with? The goal here isn't just to see the lines of code, but to build a mental map of this function's role within the larger BPF ecosystem. Time to check its callers and understand its behavior! |
| 6/2 | 2026/09/15 18:57 | tool | grepper |
0mArgs:{
"Expression": "bpf_kfunc_stack_access_bytes"
}Results: {
"Output": "include/linux/bpf_verifier.h=1648=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env,\n--\ninclude/linux/bpf_verifier.h-1650-\t\t\t\t int insn_idx);\ninclude/linux/bpf_verifier.h:1651:s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env,\ninclude/linux/bpf_verifier.h-1652-\t\t\t\t struct bpf_insn *insn, int arg,\n--\nkernel/bpf/liveness.c=1393=static int record_arg_access(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-1409-\t} else if (bpf_pseudo_kfunc_call(insn)) {\nkernel/bpf/liveness.c:1410:\t\tbytes = bpf_kfunc_stack_access_bytes(env, insn, arg_idx, insn_idx);\nkernel/bpf/liveness.c-1411-\t} else {\n--\nkernel/bpf/verifier.c=13705=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13822- */\nkernel/bpf/verifier.c:13823:s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\nkernel/bpf/verifier.c-13824-\t\t\t\t int arg, int insn_idx)\n"
} |
| 7/2 | 2026/09/15 18:57 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 18569 output: 28 thoughts: 39 Thoughts: **Analyzing `bpf_kfunc_stack_access_bytes` and Kernel Configuration** Okay, so `bpf_kfunc_stack_access_bytes` is declared as a non-static symbol. This means it's potentially exposed for use outside of its immediate compilation unit. My first instinct, given its name, is that it's related to BPF and, more specifically, the stack access behavior within the kernel. I need to understand its context. Let's dig into the Kconfig dependencies associated with `kernel/bpf/verifier.c`. This file is critical for verifying BPF programs, and any stack-related functions are likely intertwined with the verifier's operations. Understanding the Kconfig dependencies will illuminate when and under what kernel configurations this function is actually relevant. I want to see which features or options might enable or disable its inclusion in the kernel image. This should help me pinpoint its purpose and potential implications more precisely. |
| 8/2 | 2026/09/15 18:57 | tool | grepper |
0mArgs:{
"Expression": "verifier.o",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/Makefile=7=CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)\nkernel/bpf/Makefile-8-\nkernel/bpf/Makefile:9:obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o\nkernel/bpf/Makefile-10-obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o\n--\nkernel/bpf/bpf_lsm.c=413=const struct bpf_prog_ops lsm_prog_ops = {\n--\nkernel/bpf/bpf_lsm.c-415-\nkernel/bpf/bpf_lsm.c:416:const struct bpf_verifier_ops lsm_verifier_ops = {\nkernel/bpf/bpf_lsm.c-417-\t.get_func_proto = bpf_lsm_func_proto,\n--\nkernel/bpf/bpf_struct_ops.c=64=static DEFINE_MUTEX(update_mutex);\n--\nkernel/bpf/bpf_struct_ops.c-68-\nkernel/bpf/bpf_struct_ops.c:69:const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {\nkernel/bpf/bpf_struct_ops.c-70-};\n--\nkernel/bpf/btf.c=5980=static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/btf.c-5996-\nkernel/bpf/btf.c:5997:\t/* user could have requested verbose verifier output\nkernel/bpf/btf.c-5998-\t * and supplied buffer to store the verification trace\n--\nkernel/bpf/cgroup.c=1923=const struct bpf_prog_ops cg_dev_prog_ops = {\n--\nkernel/bpf/cgroup.c-1925-\nkernel/bpf/cgroup.c:1926:const struct bpf_verifier_ops cg_dev_verifier_ops = {\nkernel/bpf/cgroup.c-1927-\t.get_func_proto\t\t= cgroup_dev_func_proto,\n--\nkernel/bpf/cgroup.c=2478=static u32 sysctl_convert_ctx_access(enum bpf_access_type type,\n--\nkernel/bpf/cgroup.c-2541-\nkernel/bpf/cgroup.c:2542:const struct bpf_verifier_ops cg_sysctl_verifier_ops = {\nkernel/bpf/cgroup.c-2543-\t.get_func_proto\t\t= sysctl_func_proto,\n--\nkernel/bpf/cgroup.c=2756=static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,\n--\nkernel/bpf/cgroup.c-2764-\nkernel/bpf/cgroup.c:2765:const struct bpf_verifier_ops cg_sockopt_verifier_ops = {\nkernel/bpf/cgroup.c-2766-\t.get_func_proto\t\t= cg_sockopt_func_proto,\n--\nkernel/bpf/fixups.c=773=int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-775-\tstruct bpf_subprog_info *subprogs = env-\u003esubprog_info;\nkernel/bpf/fixups.c:776:\tconst struct bpf_verifier_ops *ops = env-\u003eops;\nkernel/bpf/fixups.c-777-\tint i, cnt, size, ctx_field_size, ret, delta = 0, epilogue_cnt = 0;\n--\nkernel/bpf/helpers.c-35-/* If kernel subsystem is allowing eBPF programs to call this function,\nkernel/bpf/helpers.c:36: * inside its own verifier_ops-\u003eget_func_proto() callback it should return\nkernel/bpf/helpers.c-37- * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments\n--\nkernel/bpf/syscall.c=6630=syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)\n--\nkernel/bpf/syscall.c-6646-\nkernel/bpf/syscall.c:6647:const struct bpf_verifier_ops bpf_syscall_verifier_ops = {\nkernel/bpf/syscall.c-6648-\t.get_func_proto = syscall_prog_func_proto,\n--\nkernel/bpf/task_iter.c=992=__bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)\n--\nkernel/bpf/task_iter.c-1010-\t/*\nkernel/bpf/task_iter.c:1011:\t * The verifier only trusts vm_mm and vm_file (see\nkernel/bpf/task_iter.c-1012-\t * BTF_TYPE_SAFE_TRUSTED_OR_NULL in verifier.c). Take a reference\n--\nkernel/bpf/trampoline.c-17-/* dummy _ops. The verifier will operate on target program's ops. */\nkernel/bpf/trampoline.c:18:const struct bpf_verifier_ops bpf_extension_verifier_ops = {\nkernel/bpf/trampoline.c-19-};\n--\nkernel/bpf/verifier.c-40-\nkernel/bpf/verifier.c:41:static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {\nkernel/bpf/verifier.c-42-#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \\\nkernel/bpf/verifier.c:43:\t[_id] = \u0026 _name ## _verifier_ops,\nkernel/bpf/verifier.c-44-#define BPF_MAP_TYPE(_id, _ops)\n--\nkernel/bpf/verifier.c=7360=enum {\n--\nkernel/bpf/verifier.c-7371- * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier\nkernel/bpf/verifier.c:7372: * clears reg-\u003eid after value_or_null-\u003evalue transition, since the verifier only\nkernel/bpf/verifier.c-7373- * cares about the range of access to valid map value pointer and doesn't care\n--\nkernel/bpf/verifier.c=14912=static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-15086-\t\t\t\tenv, \"This operation subtracts pointer register R%d from scalar register R%d. \"\nkernel/bpf/verifier.c:15087:\t\t\t\t\"The verifier only tracks pointer-minus-scalar arithmetic for allowed pointer types.\",\nkernel/bpf/verifier.c-15088-\t\t\t\tptr_regno, dst);\n--\nkernel/bpf/verifier.c=18051=bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,\n--\nkernel/bpf/verifier.c-18116- *\nkernel/bpf/verifier.c:18117: * - if so, and if verifier or current JIT inlines the call to the\nkernel/bpf/verifier.c-18118- * bpf_fastcall function (e.g. a helper call), kernel removes unnecessary\n--\nkernel/bpf/verifier.c=20101=static int check_struct_ops_btf_id(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-20218-\tprog-\u003eaux-\u003eattach_func_name = mname;\nkernel/bpf/verifier.c:20219:\tenv-\u003eops = st_ops-\u003everifier_ops;\nkernel/bpf/verifier.c-20220-\n--\nkernel/bpf/verifier.c=20852=static int check_attach_btf_id(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-20891-\t\t */\nkernel/bpf/verifier.c:20892:\t\tenv-\u003eops = bpf_verifier_ops[tgt_prog-\u003etype];\nkernel/bpf/verifier.c-20893-\t\tprog-\u003eexpected_attach_type = tgt_prog-\u003eexpected_attach_type;\n--\nkernel/bpf/verifier.c=21541=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-21551-\t/* no program is valid */\nkernel/bpf/verifier.c:21552:\tif (ARRAY_SIZE(bpf_verifier_ops) == 0)\nkernel/bpf/verifier.c-21553-\t\treturn -EINVAL;\n--\nkernel/bpf/verifier.c-21563-\tenv-\u003eprog = *prog;\nkernel/bpf/verifier.c:21564:\tenv-\u003eops = bpf_verifier_ops[env-\u003eprog-\u003etype];\nkernel/bpf/verifier.c-21565-\n--\nkernel/bpf/verifier.c-21572-\nkernel/bpf/verifier.c:21573:\t/* user could have requested verbose verifier output\nkernel/bpf/verifier.c-21574-\t * and supplied buffer to store the verification trace\n"
} |
| 9/2 | 2026/09/15 18:57 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21405 output: 31 thoughts: 28 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating the `BPF_SYSCALL` Configuration Dependency** Alright, so the clue is the `BPF_SYSCALL` config option. I need to figure out the environment in which it sits. My immediate thought is, let's dive into the Kconfig files. Time to check for any dependencies. I'm especially interested in knowing if there are any other configurations that have to be enabled or are exclusive to certain kernels. This is just a preliminary scan, but the Kconfig files will have everything I need to get this straight. |
| 10/2 | 2026/09/15 18:57 | tool | grepper |
0mError:no matches Args: {
"Expression": "config BPF_SYSCALL",
"PathPrefix": "init/Kconfig"
}Results: {
"error": "no matches"
} |
| 11/2 | 2026/09/15 18:57 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21612 output: 21 thoughts: 14 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **BPF_SYSCALL Location Quest** Okay, so I need to track down the definition of `BPF_SYSCALL`. My initial approach would be to dive into the kernel source, probably starting with the headers related to the BPF subsystem. I'd anticipate it being defined in one of the core BPF header files, something like `linux/bpf.h` or potentially a header related to system call wrappers if it's involved in syscall tracing or interception. Given the naming, it's highly likely to be an enumeration or macro. I'll need to check if there are platform-specific variations, so I should be prepared to handle `#ifdef` or similar conditional compilation directives within the header files. I should also consider how this relates to other related constants and structures, such as `bpf_prog_type` or any associated syscall numbers. The goal is to get the precise value and understand the context within the kernel's BPF implementation. |
| 12/2 | 2026/09/15 18:57 | tool | grepper |
0mArgs:{
"Expression": "config BPF_SYSCALL"
}Results: {
"Output": "kernel/bpf/Kconfig=29=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-30-\nkernel/bpf/Kconfig:31:config BPF_SYSCALL\nkernel/bpf/Kconfig-32-\tbool \"Enable bpf() system call\"\n"
} |
| 13/2 | 2026/09/15 18:57 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21951 output: 34 thoughts: 17 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Diving into the BPF Configuration** Okay, so I need to understand something about the BPF configuration in the kernel. A good starting point, as always, is the `Kconfig` file. I'll zero in on the relevant section – probably around line 31 in `kernel/bpf/Kconfig`. That's where I'd expect to find the core BPF options defined. I'm looking for the foundational choices: are we talking about enabling or disabling the overall BPF subsystem? Or perhaps specific BPF features? Perhaps options controlling JIT compilation, or tracing? I anticipate this section will outline the key knobs that control BPF's behaviour within this kernel build. Let's see what's actually in there! This `Kconfig` file should give me the ground truth of what's configurable from a build perspective, and, assuming it's well-structured, will also hint at dependencies and any cross-architecture considerations. This should be a quick way to baseline my understanding. |
| 14/2 | 2026/09/15 18:57 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 30,
"LineCount": 40
}Results: {
"Contents": " 30:\t\n 31:\tconfig BPF_SYSCALL\n 32:\t\tbool \"Enable bpf() system call\"\n 33:\t\tselect BPF\n 34:\t\tselect IRQ_WORK\n 35:\t\tselect NEED_TASKS_RCU\n 36:\t\tselect TASKS_TRACE_RCU\n 37:\t\tselect BINARY_PRINTF\n 38:\t\tselect NET_SOCK_MSG if NET\n 39:\t\tselect NET_XGRESS if NET\n 40:\t\tselect PAGE_POOL if NET\n 41:\t\tdefault n\n 42:\t\thelp\n 43:\t\t Enable the bpf() system call that allows to manipulate BPF programs\n 44:\t\t and maps via file descriptors.\n 45:\t\n 46:\tconfig BPF_JIT\n 47:\t\tbool \"Enable BPF Just In Time compiler\"\n 48:\t\tdepends on BPF\n 49:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n 50:\t\tselect EXECMEM\n 51:\t\thelp\n 52:\t\t BPF programs are normally handled by a BPF interpreter. This option\n 53:\t\t allows the kernel to generate native code when a program is loaded\n 54:\t\t into the kernel. This will significantly speed-up processing of BPF\n 55:\t\t programs.\n 56:\t\n 57:\t\t Note, an admin should enable this feature changing:\n 58:\t\t /proc/sys/net/core/bpf_jit_enable\n 59:\t\t /proc/sys/net/core/bpf_jit_harden (optional)\n 60:\t\t /proc/sys/net/core/bpf_jit_kallsyms (optional)\n 61:\t\n 62:\tconfig BPF_JIT_ALWAYS_ON\n 63:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n 64:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n 65:\t\thelp\n 66:\t\t Enables BPF JIT and removes BPF interpreter to avoid speculative\n 67:\t\t execution of BPF instructions by the interpreter.\n 68:\t\n 69:\t\t When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\n"
} |
| 15/2 | 2026/09/15 18:57 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22842 output: 199 thoughts: 853 Thoughts: **BPF Verification Logic for Raw Stack Arguments: A Deep Dive** Okay, let's break this down systematically. My initial assessment is that `BPF_SYSCALL` is a key configuration. It seems pretty straightforward, no prerequisites aside from its presence in the menu config. Now, let's get into the heart of the matter – the `FocusSymbols`. I need to pinpoint the functions that were modified, particularly within `kernel/bpf/verifier.c`. I've got a list here: 1. `mark_raw_stack`: Aha, a new function! Crucial. It's called from both `check_helper_call` and `check_kfunc_call`. It’s likely to be either inlined or not, dependent on compiler optimizations and the overall complexity. 2. `check_helper_call`: This one is critical. This checks BPF helper calls, *and* crucially, calls `mark_raw_stack`, indicating that raw mode stack initialization is handled here. 3. `check_kfunc_call`: Similar to above, this checks BPF kfunc calls, and also calls `mark_raw_stack` to handle raw memory stack init. 4. `check_stack_range_initialized`: Modified. This seems to be the place where multiple raw memory arguments and the related mask/size array logic comes into play. 5. `check_func_arg`: This validates arguments for helpers and kfunc calls, and importantly sets the raw memory mask for uninitialized memory (`MEM_UNINIT`). 6. `bpf_kfunc_stack_access_bytes`: A non-static function with a fixed index bug fix, relevant to uninit stack arguments. 7. `resolve_func_arg_type`: It's preserving `MEM_UNINIT` during type resolution, ensuring uninitialized state is properly carried through. `check_helper_call` and `check_kfunc_call` jump out as excellent FocusSymbols, since they directly execute the changed logic. Considering all of these, I think my final `FocusSymbols` list includes `mark_raw_stack`, `check_stack_range_initialized`, `bpf_kfunc_stack_access_bytes` as well. Now, a quick sanity check: Are any of these functions *ubiquitous*? Nope. They aren't general memory allocators or deallocators, or page fault handlers. They're specifically tied to BPF program verification, specifically when we're checking function calls (kfuncs and helpers) that involve memory arguments, and more specifically *raw* or *uninitialized* memory. This is targeted. Reachability: The verification logic executes during `BPF_PROG_LOAD`, which is reachable via `sys_bpf`. Any program leveraging helper/kfunc calls with uninit or raw stack arguments will trigger this verification. WorthFuzzing: Absolutely. The BPF verifier is a core subsystem with a complex web of safety rules. Fuzzing this area is critical. I'm especially concerned about potential vulnerabilities stemming from uninitialized stack slots, masks, sizes, and any potential for aliasing. This is a clear path to memory leaks or out-of-bounds accesses. EnableConfigs: `BPF_SYSCALL` is the obvious one. FocusSymbols: `check_kfunc_call`, `check_helper_call`, `mark_raw_stack`, `check_stack_range_initialized`, `bpf_kfunc_stack_access_bytes`. With that analysis complete, I'm ready to craft the `set-results` response. Everything looks solid. |
| 16/2 | 2026/09/15 18:57 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL"
],
"FocusSymbols": [
"check_kfunc_call",
"check_helper_call",
"mark_raw_stack",
"check_stack_range_initialized",
"bpf_kfunc_stack_access_bytes"
],
"Reasoning": "The patch modifies BPF verifier logic to support multiple uninitialized stack arguments for kfuncs and helpers (__uninit / raw memory). It introduces mark_raw_stack(), refactors arg_raw_mem tracking to support bitmasks and multiple argument slots, and updates check_stack_range_initialized(), check_func_arg(), check_kfunc_call(), and check_helper_call(). These changes affect core BPF verifier safety tracking and are fully reachable via the bpf(BPF_PROG_LOAD) system call.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL"
],
"FocusSymbols": [
"check_kfunc_call",
"check_helper_call",
"mark_raw_stack",
"check_stack_range_initialized",
"bpf_kfunc_stack_access_bytes"
],
"Reasoning": "The patch modifies BPF verifier logic to support multiple uninitialized stack arguments for kfuncs and helpers (__uninit / raw memory). It introduces mark_raw_stack(), refactors arg_raw_mem tracking to support bitmasks and multiple argument slots, and updates check_stack_range_initialized(), check_func_arg(), check_kfunc_call(), and check_helper_call(). These changes affect core BPF verifier safety tracking and are fully reachable via the bpf(BPF_PROG_LOAD) system call.",
"WorthFuzzing": true
} |
| 17/1 | 2026/09/15 18:57 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 18/2 | 2026/09/15 18:57 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series updates the BPF verifier's handling of kfunc arguments tagged with '__uninit' and raw memory arguments across helper and kfunc calls. Specifically, it expands 'struct arg_raw_mem_desc' in 'struct bpf_call_arg_meta' from tracking a single argument register and size to a bitmask and per-slot size array ('mask' and 'size[MAX_BPF_FUNC_ARGS]'), updates stack range checks and access types during verifier analysis, and adds 'mark_raw_stack()' to check and mark stack slots as written for all raw memory arguments. It also updates BPF selftests and documentation.\n\nKMSAN vs. KASAN applicability:\n- The changes are strictly within the BPF verifier's abstract analysis logic ('kernel/bpf/verifier.c', 'include/linux/bpf_verifier.h') and test code ('tools/testing/selftests/').\n- No kernel heap or stack buffers copied to userspace (via copy_to_user, put_user, netlink, ioctls, etc.) are introduced or altered.\n- All verifier metadata structures, including 'struct bpf_call_arg_meta', are fully zero-initialized (via memset) before use in 'check_helper_call()' and 'check_kfunc_call()'.\n- Local variables and loop counters introduced by the patch are explicitly initialized before evaluation.\n- Any potential bounds issues regarding argument slot array indexing ('size[MAX_BPF_FUNC_ARGS]') are bounds-checked in code or would represent out-of-bounds accesses detectable by KASAN, not uninitialized memory reads.\n- Furthermore, BPF execution runtime stack memory is either explicitly unpoisoned for KMSAN in the interpreter or run as JITted instructions without KMSAN compiler instrumentation.\n\nTherefore, this patch series does not introduce or expose uninitialized memory reads or kernel info-leaks, and running a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 87c9e72c98fa84e06f40d66656613184dc97807c
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Sep 15 18:56:56 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index 6c2c048dccef6..71fb0ca72d698 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -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
---------------------------
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index cf85141ea1674..9ddbb20ec1e92 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1547,12 +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().
+ * 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;
- int size;
+ u16 mask;
+ int size[MAX_BPF_FUNC_ARGS];
};
/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
@@ -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,
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6c6b8d8520cdf..070ec5daeec33 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6981,7 +6981,9 @@ 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);
+ u32 arg_slot = abs(argno.argno) - 1;
+ bool raw_mode = meta && arg_slot < MAX_BPF_FUNC_ARGS &&
+ (meta->arg_raw_mem.mask & BIT(arg_slot));
access_size = abs(access_size);
@@ -7023,7 +7025,7 @@ static int check_stack_range_initialized(
}
if (raw_mode) {
- meta->arg_raw_mem.size = access_size;
+ meta->arg_raw_mem.size[arg_slot] = access_size;
return 0;
}
@@ -7216,7 +7218,7 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,
* the memory that the helper could just partially fill up.
*/
if (!tnum_is_const(size_reg->var_off))
- meta->arg_raw_mem.regno = 0;
+ meta->arg_raw_mem.mask &= ~BIT(abs(mem_argno.argno) - 1);
if (reg_smin(size_reg) < 0) {
verbose(env, "%s min value is negative, either use unsigned or 'var &= const'\n",
@@ -9024,7 +9026,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
*/
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;
+ meta->arg_raw_mem.mask &= ~BIT(slot);
err = check_helper_mem_access(env, reg, argno, meta->map.ptr->value_size,
arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ,
@@ -9176,6 +9178,9 @@ 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.mask |= BIT(slot);
+
/* The access to this pointer is only checked when we hit the
* next is_mem_size argument below.
*/
@@ -9184,7 +9189,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
access_type = arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ;
if (meta->btf)
- access_type = BPF_READ | BPF_WRITE;
+ access_type = arg_type & MEM_UNINIT ? BPF_WRITE : BPF_READ | BPF_WRITE;
err = check_mem_reg(env, reg, argno, arg_size, access_type, meta, &known_memory);
if (err < 0) {
@@ -9232,7 +9237,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
access_type = fn->arg_type[arg - 1] & MEM_WRITE ? BPF_WRITE : BPF_READ;
if (meta->btf)
- access_type = BPF_READ | BPF_WRITE;
+ access_type = fn->arg_type[arg - 1] & MEM_UNINIT ?
+ BPF_WRITE : BPF_READ | BPF_WRITE;
zero_size_allowed = meta->btf || base_type(arg_type) == ARG_MEM_SIZE_OR_ZERO;
@@ -9550,6 +9556,33 @@ static int check_func_args(struct bpf_verifier_env *env, struct bpf_call_arg_met
return 0;
}
+static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
+ int insn_idx)
+{
+ struct bpf_func_state *caller = cur_func(env);
+ u32 slot;
+ int i, err;
+
+ /*
+ * Validate every argument before initializing outputs: an input argument
+ * may alias an output buffer. Use the normal stack-write checks to discard
+ * stale spills and preserve the rules for special stack objects.
+ */
+ for (slot = 0; slot < MAX_BPF_FUNC_ARGS; slot++) {
+ struct bpf_reg_state *reg = get_func_arg_reg(caller, cur_regs(env), slot);
+ argno_t argno = argno_from_arg(slot + 1);
+
+ for (i = 0; i < meta->arg_raw_mem.size[slot]; i++) {
+ err = check_mem_access(env, insn_idx, reg, argno, i, BPF_B,
+ BPF_WRITE, -1, false, false);
+ if (err)
+ return err;
+ }
+ }
+
+ return 0;
+}
+
static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id)
{
enum bpf_attach_type eatype = env->prog->expected_attach_type;
@@ -9851,9 +9884,9 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a
break;
if (!arg_type_is_raw_mem(fn->arg_type[i]))
continue;
- if (meta->arg_raw_mem.regno)
+ if (meta->arg_raw_mem.mask)
return false;
- meta->arg_raw_mem.regno = i + 1;
+ meta->arg_raw_mem.mask = BIT(i);
}
return true;
@@ -11572,16 +11605,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
regs = cur_regs(env);
- /* Mark slots with STACK_MISC in case of raw mode, stack offset
- * is inferred from register state.
- */
- for (i = 0; i < meta.arg_raw_mem.size; i++) {
- err = check_mem_access(env, insn_idx, regs + meta.arg_raw_mem.regno,
- argno_from_reg(meta.arg_raw_mem.regno), i, BPF_B,
- BPF_WRITE, -1, false, false);
- if (err)
- return err;
- }
+ err = mark_raw_stack(env, &meta, insn_idx);
+ if (err)
+ return err;
if (meta.release_regno) {
struct bpf_reg_state *reg = ®s[meta.release_regno];
@@ -12437,7 +12463,7 @@ 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 | (*arg_type & (PTR_MAYBE_NULL | MEM_UNINIT));
return 0;
}
@@ -13859,7 +13885,7 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *
/* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */
if (arg == 0 && meta.kfunc_flags & KF_ITER_NEW)
return -size;
- if (is_kfunc_arg_uninit(btf, &args[arg]))
+ if (is_kfunc_arg_uninit(btf, &args[i]))
return -size;
return size;
}
@@ -14140,6 +14166,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (err < 0)
return err;
+ err = mark_raw_stack(env, &meta, insn_idx);
+ if (err)
+ return err;
+
if ((is_bpf_obj_drop_kfunc(meta.func_id) ||
is_bpf_percpu_obj_drop_kfunc(meta.func_id)) && (is_tracing_prog_type(prog_type) ||
/* is_tracing_prog_type() for now doesn't cover non-iterator tracing progs. */
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 7732df9bc8701..4a9affe81ee16 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -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); }
diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index eb88d9ce6c348..2ced1d751acea 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -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)
diff --git a/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c b/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c
new file mode 100644
index 0000000000000..99c0ee562ea03
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_kfunc_uninit.c
@@ -0,0 +1,399 @@
+// 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)
+{
+ asm volatile ("" :
+ : "r"(&bpf_kfunc_test_uninit_scalar),
+ "r"(&bpf_kfunc_test_uninit_struct),
+ "r"(&bpf_kfunc_test_uninit_mem),
+ "r"(&bpf_kfunc_test_uninit_mem_const),
+ "r"(&bpf_kfunc_test_uninit_multi),
+ "r"(&bpf_kfunc_test_uninit_alias),
+ "r"(&bpf_kfunc_test_uninit_pair),
+ "r"(&bpf_kfunc_test_uninit_stack));
+}
+
+SEC("tc")
+__success __retval(42)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void scalar_poisoned_at_checkpoint(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = 0;"
+ "goto +0;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void scalar_uninitialized(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(10)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void struct_poisoned_at_checkpoint(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 16) = 0;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "goto +0;"
+ "r1 = r10;"
+ "r1 += -16;"
+ "call %[bpf_kfunc_test_uninit_struct];"
+ "r0 = *(u32 *)(r10 - 16);"
+ "r1 = *(u32 *)(r10 - 12);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 8);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 4);"
+ "r0 += r1;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_struct) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(10)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void struct_uninitialized(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -16;"
+ "call %[bpf_kfunc_test_uninit_struct];"
+ "r0 = *(u32 *)(r10 - 16);"
+ "r1 = *(u32 *)(r10 - 12);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 8);"
+ "r0 += r1;"
+ "r1 = *(u32 *)(r10 - 4);"
+ "r0 += r1;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_struct) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(0x2a2a2a2a)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void sized_buffer_poisoned_at_checkpoint(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = 0;"
+ "goto +0;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = 8;"
+ "call %[bpf_kfunc_test_uninit_mem];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "r1 = *(u32 *)(r10 - 4);"
+ "if r0 == r1 goto +1;"
+ "r0 = 0;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(0x2a2a2a2a)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void constant_size_buffer_uninitialized(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = 8;"
+ "call %[bpf_kfunc_test_uninit_mem_const];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "r1 = *(u32 *)(r10 - 4);"
+ "if r0 == r1 goto +1;"
+ "r0 = 0;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem_const) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__flag(BPF_F_TEST_STATE_FREQ)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void multiple_outputs(void)
+{
+ asm volatile (
+ "*(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);"
+ "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);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void variable_size_preserves_other_output(void)
+{
+ asm volatile (
+ "r3 = *(u32 *)(r1 + 0);"
+ "r3 &= 7;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "r1 = r10;"
+ "r1 += -16;"
+ "r2 = r10;"
+ "r2 += -8;"
+ "call %[bpf_kfunc_test_uninit_multi];"
+ "r0 = *(u32 *)(r10 - 16);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_multi) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void variable_size_initialized_buffer(void)
+{
+ asm volatile (
+ "r2 = *(u32 *)(r1 + 0);"
+ "r2 &= 7;"
+ "r2 += 1;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_mem];"
+ "r0 = *(u8 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__failure_unpriv __msg_unpriv("invalid read from stack")
+__naked void variable_size_uninitialized_buffer(void)
+{
+ asm volatile (
+ "r2 = *(u32 *)(r1 + 0);"
+ "r2 &= 7;"
+ "r2 += 1;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_mem];"
+ "r0 = *(u8 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_mem) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(42)
+__naked void variable_offset_initialized_buffer(void)
+{
+ asm volatile (
+ "r2 = *(u32 *)(r1 + 0);"
+ "r2 &= 4;"
+ "*(u64 *)(r10 - 8) = 0;"
+ "r6 = r10;"
+ "r6 += -8;"
+ "r6 += r2;"
+ "r1 = r6;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r6 + 0);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__failure_unpriv __msg_unpriv("invalid read from stack")
+__naked void output_does_not_initialize_adjacent_bytes(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = *(u32 *)(r10 - 4);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__success __retval(7)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void initialized_input_alias(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = 7;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = r1;"
+ "call %[bpf_kfunc_test_uninit_alias];"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_alias) : __clobber_all);
+}
+
+SEC("tc")
+__success
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__failure_unpriv __msg_unpriv("invalid read from stack")
+__naked void uninitialized_input_alias(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -8;"
+ "r2 = r1;"
+ "call %[bpf_kfunc_test_uninit_alias];"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_alias) : __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, int);
+} write_only_map SEC(".maps");
+
+SEC("tc")
+__success __retval(0)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+int write_only_map_output(void *ctx)
+{
+ __u32 key = 0;
+ int *out = bpf_map_lookup_elem(&write_only_map, &key);
+
+ if (out)
+ bpf_kfunc_test_uninit_scalar(out);
+ return 0;
+}
+
+static const int read_only_output;
+
+SEC("tc")
+__failure __msg("write into map forbidden")
+int read_only_map_output(void *ctx)
+{
+ bpf_kfunc_test_uninit_scalar((int *)&read_only_output);
+ return 0;
+}
+
+SEC("tc")
+__failure __msg("stack R1 off=-2 size=4")
+__naked void output_out_of_bounds(void)
+{
+ asm volatile (
+ "r1 = r10;"
+ "r1 += -2;"
+ "call %[bpf_kfunc_test_uninit_scalar];"
+ "r0 = 0;"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_scalar) : __clobber_all);
+}
+
+SEC("tc")
+__arch_x86_64 __arch_arm64
+__success __retval(42)
+__caps_unpriv(CAP_BPF | CAP_NET_ADMIN)
+__prepare_priv
+__success_unpriv
+__naked void output_after_by_value_argument(void)
+{
+ asm volatile (
+ "r1 = 20;"
+ "r2 = 22;"
+ "r3 = r10;"
+ "r3 += -8;"
+ "call %[bpf_kfunc_test_uninit_pair];"
+ "r0 = *(u32 *)(r10 - 8);"
+ "exit;"
+ : : __imm(bpf_kfunc_test_uninit_pair) : __clobber_all);
+}
+
+#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;"
+ "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
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index fd2c0cdc91b18..4015cf4fc416c 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -1316,6 +1316,54 @@ __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
{
}
+__bpf_kfunc void bpf_kfunc_test_uninit_scalar(int *out__uninit)
+{
+ put_unaligned(42, out__uninit);
+}
+
+__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 void bpf_kfunc_test_uninit_mem_const(void *out__uninit, u32 out__szk)
+{
+ memset(out__uninit, 0x2a, out__szk);
+}
+
+__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 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_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)
{
}
@@ -1747,6 +1795,14 @@ 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_scalar)
+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_mem_const)
+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_multi)
+BTF_ID_FLAGS(func, bpf_kfunc_test_uninit_alias)
+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)
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
index d3696d5254c9e..2ec8192a102fe 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
@@ -289,6 +289,15 @@ __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_scalar(int *out__uninit) __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;
+void bpf_kfunc_test_uninit_mem_const(void *out__uninit, __u32 out__szk) __ksym;
+void bpf_kfunc_test_uninit_multi(int *a__uninit, void *b__uninit, __u32 b__sz) __ksym;
+int bpf_kfunc_test_uninit_alias(int *out__uninit, const int *in) __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;
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 28724de063220..940b6d8e47ba4 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -71,6 +71,7 @@ struct test_spec {
int load_mask;
int linear_sz;
const char *skip_reason;
+ bool prepare_priv;
bool auxiliary;
bool valid;
};
@@ -606,6 +607,8 @@ static int parse_test_spec(struct test_loader *tester,
if (err)
goto cleanup;
spec->mode_mask |= UNPRIV;
+ } else if (strcmp(s, "test_prepare_priv") == 0) {
+ spec->prepare_priv = true;
} else if ((val = str_has_pfx(s, "load_mode="))) {
if (strcmp(val, "jited") == 0) {
load_mask = JITED;
@@ -1015,10 +1018,10 @@ struct cap_state {
bool initialized;
};
-static int drop_capabilities(struct cap_state *caps)
+static int drop_capabilities(struct cap_state *caps, __u64 keep_caps)
{
const __u64 caps_to_drop = (1ULL << CAP_SYS_ADMIN | 1ULL << CAP_NET_ADMIN |
- 1ULL << CAP_PERFMON | 1ULL << CAP_BPF);
+ 1ULL << CAP_PERFMON | 1ULL << CAP_BPF) & ~keep_caps;
int err;
err = cap_disable_effective(caps_to_drop, &caps->old_caps);
@@ -1028,6 +1031,13 @@ static int drop_capabilities(struct cap_state *caps)
}
caps->initialized = true;
+ if (keep_caps) {
+ err = cap_enable_effective(keep_caps, NULL);
+ if (err) {
+ PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err));
+ return err;
+ }
+ }
return 0;
}
@@ -1049,7 +1059,7 @@ static bool can_execute_unpriv(struct test_loader *tester, struct test_spec *spe
{
if (sysctl_unpriv_disabled < 0)
sysctl_unpriv_disabled = get_unpriv_disabled() ? 1 : 0;
- if (sysctl_unpriv_disabled)
+ if (sysctl_unpriv_disabled && !(spec->unpriv.caps & (1ULL << CAP_BPF)))
return false;
if ((spec->prog_flags & BPF_F_ANY_ALIGNMENT) && !EFFICIENT_UNALIGNED_ACCESS)
return false;
@@ -1351,17 +1361,8 @@ void run_subtest(struct test_loader *tester,
test__end_subtest();
return;
}
- if (drop_capabilities(&caps)) {
- test__end_subtest();
- return;
- }
- if (subspec->caps) {
- err = cap_enable_effective(subspec->caps, NULL);
- if (err) {
- PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err));
- goto subtest_cleanup;
- }
- }
+ if (!spec->prepare_priv && drop_capabilities(&caps, subspec->caps))
+ goto subtest_cleanup;
}
/* Implicitly reset to NULL if next test case doesn't specify.
@@ -1414,6 +1415,18 @@ void run_subtest(struct test_loader *tester,
bpf_object__for_each_map(map, tobj)
bpf_map__set_autocreate(map, !unpriv || is_unpriv_capable_map(map));
+ if (unpriv && spec->prepare_priv) {
+ /*
+ * Module BTF lookup needs CAP_SYS_ADMIN. Allow tests to prepare
+ * their objects first, then verify programs with the requested caps.
+ */
+ err = bpf_object__prepare(tobj);
+ if (!ASSERT_OK(err, "obj_prepare"))
+ goto tobj_cleanup;
+ if (drop_capabilities(&caps, subspec->caps))
+ goto tobj_cleanup;
+ }
+
err = bpf_object__load(tobj);
if (subspec->expect_failure) {
if (!ASSERT_ERR(err, "unexpected_load_success")) {
|
| 19/3 | 2026/09/15 18:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 14441 output: 25 thoughts: 1484 Thoughts: **BPF Verifier and Uninitialized Memory Handling** Okay, so this patch touches the BPF verifier and selftests, specifically dealing with kernel functions (`kfuncs`). The files it's modifying point towards changes in how the verifier handles uninitialized memory within these kfuncs and potentially raw memory handling. My initial thought is to dive into the core logic. First, I need to understand the changes to the data structures. The `arg_raw_mem_desc` struct is updated to use a bitmask (`u16 mask`) and an array (`int size[MAX_BPF_FUNC_ARGS]`). It seems like the single `regno` field has been replaced. Also, the `struct bpf_call_arg_meta` has updated content. This hints at handling multiple raw memory arguments or arguments within a call. Looking deeper into the changes in `kernel/bpf/verifier.c`, the `check_stack_range_initialized` function now uses the bitmask to determine if an argument is in raw mode. The `check_mem_size_reg` function also updates the bitmask accordingly. In `check_func_arg`, the update to the access type is interesting, particularly the interplay between `MEM_UNINIT`, the `BTF` information, and read/write access. The logic appears to be refining how uninitialized memory is treated. Also the new function `mark_raw_stack` looks like the core of the new initialization check, iterating through arguments and validating each byte. This is added to `check_helper_call` and `check_kfunc_call`. The fix is for the loop index and is fixing a bug in `bpf_kfunc_stack_access_bytes`. Okay, so I think I can summarize the gist of the changes. The patch seems to be refining how uninitialized stack data is tracked within the BPF verifier, especially for kfunc arguments and raw memory scenarios. The bitmask is used for finer-grained control over which arguments are considered "raw" or uninitialized, potentially allowing multiple arguments to be handled separately. The `mark_raw_stack` function is likely the central part of the changes, validating the memory access for uninitialized arguments before output. To understand this better, I need to analyze how the verifier handles uninitialized stack memory in general, and I'll need to study these modified functions along with related functions such as `check_helper_call`, `check_kfunc_call`, `bpf_kfunc_stack_access_bytes` and others using file tools or grepping to understand the full context. |
| 20/3 | 2026/09/15 18:57 | tool | grepper |
0mArgs:{
"Expression": "bpf_kfunc_stack_access_bytes"
}Results: {
"Output": "include/linux/bpf_verifier.h=1648=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env,\n--\ninclude/linux/bpf_verifier.h-1650-\t\t\t\t int insn_idx);\ninclude/linux/bpf_verifier.h:1651:s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env,\ninclude/linux/bpf_verifier.h-1652-\t\t\t\t struct bpf_insn *insn, int arg,\n--\nkernel/bpf/liveness.c=1393=static int record_arg_access(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-1409-\t} else if (bpf_pseudo_kfunc_call(insn)) {\nkernel/bpf/liveness.c:1410:\t\tbytes = bpf_kfunc_stack_access_bytes(env, insn, arg_idx, insn_idx);\nkernel/bpf/liveness.c-1411-\t} else {\n--\nkernel/bpf/verifier.c=13705=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13822- */\nkernel/bpf/verifier.c:13823:s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\nkernel/bpf/verifier.c-13824-\t\t\t\t int arg, int insn_idx)\n"
} |
| 21/3 | 2026/09/15 18:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 16943 output: 38 thoughts: 28 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into `bpf_kfunc_stack_access_bytes`** Okay, time to dig into the `bpf_kfunc_stack_access_bytes` function. I'm heading over to `kernel/bpf/verifier.c` right now. I know this is a critical area for ensuring the safety and correctness of BPF programs. I need to understand exactly how the verifier handles stack access within kernel functions. Specifically, I'm interested in how it checks byte-level access, which is crucial for preventing out-of-bounds reads and writes, and maintaining memory integrity. I'm expecting to see checks for offset calculations, size validation, and potentially, some interaction with the BPF type information. I'll need to pay close attention to the data structures involved, the logic of the access checks, and any potential optimizations that might be in place. This is where the rubber meets the road in terms of BPF security, so I need to make sure I understand it thoroughly. This will give me a good understanding of stack access in the verifier. |
| 22/3 | 2026/09/15 18:57 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 13823,
"LineCount": 70
}Results: {
"Contents": "13823:\ts64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n13824:\t\t\t\t\t int arg, int insn_idx)\n13825:\t{\n13826:\t\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\n13827:\t\tstruct bpf_call_arg_meta meta;\n13828:\t\tconst struct btf_param *args;\n13829:\t\tconst struct btf_type *t, *ref_t;\n13830:\t\tconst struct btf *btf;\n13831:\t\tu32 i, slot, nargs, type_size;\n13832:\t\ts64 size;\n13833:\t\n13834:\t\tif (bpf_fetch_kfunc_arg_meta(env, insn-\u003eimm, insn-\u003eoff, \u0026meta) \u003c 0)\n13835:\t\t\treturn S64_MIN;\n13836:\t\n13837:\t\tbtf = meta.btf;\n13838:\t\targs = btf_params(meta.func_proto);\n13839:\t\tnargs = btf_type_vlen(meta.func_proto);\n13840:\t\n13841:\t\t/*\n13842:\t\t * @arg is an argument slot and a 16-byte parameter takes two of them,\n13843:\t\t * so walk the parameters to find the one that starts at this slot. A\n13844:\t\t * slot holding the upper eightbyte of such a parameter belongs to no\n13845:\t\t * pointer, and neither does a slot past the last parameter.\n13846:\t\t */\n13847:\t\tfor (i = 0, slot = 0; i \u003c nargs \u0026\u0026 slot \u003c arg; i++)\n13848:\t\t\tslot += kfunc_arg_slots(btf_type_skip_modifiers(btf, args[i].type, NULL));\n13849:\t\tif (i \u003e= nargs || slot != arg)\n13850:\t\t\treturn 0;\n13851:\t\n13852:\t\tt = btf_type_skip_modifiers(btf, args[i].type, NULL);\n13853:\t\tif (!btf_type_is_ptr(t))\n13854:\t\t\treturn 0;\n13855:\t\n13856:\t\t/* dynptr: fixed 16-byte on-stack representation */\n13857:\t\tif (is_kfunc_arg_dynptr(btf, \u0026args[i])) {\n13858:\t\t\tsize = BPF_DYNPTR_SIZE;\n13859:\t\t\tgoto out;\n13860:\t\t}\n13861:\t\n13862:\t\t/* ptr + __sz/__szk pair: the size follows the pointer */\n13863:\t\tif (i + 1 \u003c nargs \u0026\u0026\n13864:\t\t (btf_param_match_suffix(btf, \u0026args[i + 1], \"__sz\") ||\n13865:\t\t btf_param_match_suffix(btf, \u0026args[i + 1], \"__szk\"))) {\n13866:\t\t\tint size_reg = BPF_REG_1 + arg + 1;\n13867:\t\n13868:\t\t\tif (size_reg \u003c= MAX_BPF_FUNC_REG_ARGS \u0026\u0026\n13869:\t\t\t (aux-\u003econst_reg_mask \u0026 BIT(size_reg))) {\n13870:\t\t\t\tsize = (s64)aux-\u003econst_reg_vals[size_reg];\n13871:\t\t\t\tgoto out;\n13872:\t\t\t}\n13873:\t\t\treturn MAX_BPF_STACK;\n13874:\t\t}\n13875:\t\n13876:\t\t/* fixed-size pointed-to type: resolve via BTF */\n13877:\t\tref_t = btf_type_skip_modifiers(btf, t-\u003etype, NULL);\n13878:\t\tif (!IS_ERR(btf_resolve_size(btf, ref_t, \u0026type_size))) {\n13879:\t\t\tsize = type_size;\n13880:\t\t\tgoto out;\n13881:\t\t}\n13882:\t\n13883:\t\treturn S64_MIN;\n13884:\tout:\n13885:\t\t/* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */\n13886:\t\tif (arg == 0 \u0026\u0026 meta.kfunc_flags \u0026 KF_ITER_NEW)\n13887:\t\t\treturn -size;\n13888:\t\tif (is_kfunc_arg_uninit(btf, \u0026args[i]))\n13889:\t\t\treturn -size;\n13890:\t\treturn size;\n13891:\t}\n13892:\t\n"
} |
| 23/3 | 2026/09/15 18:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 18567 output: 37 thoughts: 263 Thoughts: **My Analysis of a BPF Kernel Function Bug Fix** Okay, let's break this down. Line 13888-13889... ah, yes, I see the critical change: `if (is_kfunc_arg_uninit(btf, &args[i])) return -size;` compared to the original code `if (is_kfunc_arg_uninit(btf, &args[arg]))`. The subtle difference in indexing is glaring now. They were using `arg` (the *slot* index) instead of `i` (the *parameter* index) to access the argument, `args`. That's a classic off-by-one error waiting to happen, especially when dealing with parameter structures that aren't uniformly sized or aligned. Different slot sizes and offsets would completely break this. This would be a nasty bug in a BPF context, potentially leading to memory corruption or incorrect state management during kfunc calls. Right, so that's the fix. Now, let's see how this ties into the larger picture. I want to understand *why* this was a problem and what it impacts. `bpf_kfunc_stack_access_bytes` is the key function to inspect. Looking at the code, it's used in `liveness.c` within `record_arg_access`. Ah, right, liveness tracking! That would be for pruning, checkpointing, and `mark_reg_read`. So this relates to the verifier's optimization and safety checks for register and stack usage. I remember, a negative `bytes` value there means a write or clobber - essentially the callee is initializing the memory. Positive value signifies a read. Makes sense. Now let's delve a bit further and look at the specifics, particularly around `mark_raw_stack` and `arg_raw_mem` within the verifier. It would be helpful to check `check_stack_range_initialized` and examine the surrounding code, around lines 6980-7050, in `kernel/bpf/verifier.c`. This will give me a better understanding of how the uninitialized argument check integrates with the broader verifier checks. Specifically, I'm interested in how they are making sure a BPF program doesn't read garbage off the stack. |
| 24/3 | 2026/09/15 18:57 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 6975,
"LineCount": 80
}Results: {
"Contents": "6975:\t\tbool clobber = type == BPF_WRITE;\n6976:\t\t/*\n6977:\t\t * Negative access_size signals global subprog arg check where\n6978:\t\t * STACK_POISON slots are acceptable. static stack liveness\n6979:\t\t * might have determined that subprog doesn't read them,\n6980:\t\t * but BTF based global subprog validation isn't accurate enough.\n6981:\t\t */\n6982:\t\tbool allow_poison = access_size \u003c 0 || clobber;\n6983:\t\t/* The call will initialize the memory; uninitialized stack allowed */\n6984:\t\tu32 arg_slot = abs(argno.argno) - 1;\n6985:\t\tbool raw_mode = meta \u0026\u0026 arg_slot \u003c MAX_BPF_FUNC_ARGS \u0026\u0026\n6986:\t\t\t (meta-\u003earg_raw_mem.mask \u0026 BIT(arg_slot));\n6987:\t\n6988:\t\taccess_size = abs(access_size);\n6989:\t\n6990:\t\tif (access_size == 0 \u0026\u0026 !zero_size_allowed) {\n6991:\t\t\tverbose(env, \"invalid zero-sized read\\n\");\n6992:\t\t\treturn -EACCES;\n6993:\t\t}\n6994:\t\n6995:\t\terr = check_stack_access_within_bounds(env, reg, argno, off, access_size, type);\n6996:\t\tif (err)\n6997:\t\t\treturn err;\n6998:\t\n6999:\t\tif (tnum_is_const(reg-\u003evar_off)) {\n7000:\t\t\tmin_off = max_off = reg-\u003evar_off.value + off;\n7001:\t\t} else {\n7002:\t\t\t/* Variable offset is prohibited for unprivileged mode for\n7003:\t\t\t * simplicity since it requires corresponding support in\n7004:\t\t\t * Spectre masking for stack ALU.\n7005:\t\t\t * See also retrieve_ptr_limit().\n7006:\t\t\t */\n7007:\t\t\tif (!env-\u003ebypass_spec_v1) {\n7008:\t\t\t\tchar tn_buf[48];\n7009:\t\n7010:\t\t\t\ttnum_strn(tn_buf, sizeof(tn_buf), reg-\u003evar_off);\n7011:\t\t\t\tverbose(env, \"%s variable offset stack access prohibited for !root, var_off=%s\\n\",\n7012:\t\t\t\t\treg_arg_name(env, argno), tn_buf);\n7013:\t\t\t\treturn -EACCES;\n7014:\t\t\t}\n7015:\t\t\t/* Only initialized buffer on stack is allowed to be accessed\n7016:\t\t\t * with variable offset. With uninitialized buffer it's hard to\n7017:\t\t\t * guarantee that whole memory is marked as initialized on\n7018:\t\t\t * helper return since specific bounds are unknown what may\n7019:\t\t\t * cause uninitialized stack leaking.\n7020:\t\t\t */\n7021:\t\t\traw_mode = false;\n7022:\t\n7023:\t\t\tmin_off = reg_smin(reg) + off;\n7024:\t\t\tmax_off = reg_smax(reg) + off;\n7025:\t\t}\n7026:\t\n7027:\t\tif (raw_mode) {\n7028:\t\t\tmeta-\u003earg_raw_mem.size[arg_slot] = access_size;\n7029:\t\t\treturn 0;\n7030:\t\t}\n7031:\t\n7032:\t\tfor (i = min_off; i \u003c max_off + access_size; i++) {\n7033:\t\t\tu8 *stype;\n7034:\t\n7035:\t\t\tslot = -i - 1;\n7036:\t\t\tspi = slot / BPF_REG_SIZE;\n7037:\t\t\tif (state-\u003eallocated_stack \u003c= slot) {\n7038:\t\t\t\tverbose(env, \"allocated_stack too small\\n\");\n7039:\t\t\t\treturn -EFAULT;\n7040:\t\t\t}\n7041:\t\n7042:\t\t\tstype = \u0026state-\u003estack[spi].slot_type[slot % BPF_REG_SIZE];\n7043:\t\t\tif (*stype == STACK_MISC)\n7044:\t\t\t\tgoto mark;\n7045:\t\t\tif ((*stype == STACK_ZERO) ||\n7046:\t\t\t (*stype == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)) {\n7047:\t\t\t\tif (clobber) {\n7048:\t\t\t\t\t/* helper can write anything into the stack */\n7049:\t\t\t\t\t*stype = STACK_MISC;\n7050:\t\t\t\t}\n7051:\t\t\t\tgoto mark;\n7052:\t\t\t}\n7053:\t\n7054:\t\t\tif (bpf_is_spilled_reg(\u0026state-\u003estack[spi]) \u0026\u0026\n"
} |
| 25/3 | 2026/09/15 18:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 20649 output: 38 thoughts: 429 Thoughts: **Uninitialized Stack in Raw Mode: A Potential Vulnerability** Okay, let's break this down. In the `check_stack_range_initialized` function, specifically when `raw_mode` is enabled for arguments passed to helpers or kfuncs, I've spotted something interesting – or rather, a potential oversight. The code snippet `meta->arg_raw_mem.size[arg_slot] = access_size; return 0;` immediately sets the size and returns without verifying if the stack range has actually been initialized first! That's...unsettling. We're effectively allowing uninitialized stack data to be accessed under specific conditions. This seems like a potential security risk that needs a closer look. My next thought then is, *where* does the stack typically get marked as initialized? I'm trying to trace the lifecycle here. Let's see... looking at the old code, specifically within `check_helper_call`, I see a loop that *should* handle marking the memory with `STACK_MISC` when `raw_mode` is active. It iterates through the `meta.arg_raw_mem.size` (which seems to be the size of the memory argument, not the number of arguments) and then calls `check_mem_access` for each byte, I think, and, then, it calls `check_mem_access`. But wait. This old code snippet raises other questions. The old implementation's `arg_raw_mem` seems to be limited to a single argument, using just `regno` and `size`. What happens if a helper or kfunc accepts multiple raw memory arguments? More importantly, did `kfuncs` ever call `check_mem_access`? Were they even handled correctly? I need to verify that this was even implemented correctly. My concern is that, in the original code, this critical initialization check might have been completely bypassed for kfuncs if it wasn't implemented there. I'm going to dig into the old version's `check_kfunc_call` in `kernel/bpf/verifier.c` to be certain. I'll focus on lines 14130-14175 to see how this function functioned *before* the patch. This is going to be key to understanding the full implications of this code change and confirming the presence of an actual vulnerability. |
| 26/3 | 2026/09/15 18:57 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 14135,
"LineCount": 45
}Results: {
"Contents": "14135:\t\t\t\treturn err;\n14136:\t\t\t}\n14137:\t\t} else if (!insn-\u003eoff \u0026\u0026 insn-\u003eimm == special_kfunc_list[KF___bpf_trap]) {\n14138:\t\t\tverbose(env, \"unexpected __bpf_trap() due to uninitialized variable?\\n\");\n14139:\t\t\treturn -EFAULT;\n14140:\t\t}\n14141:\t\n14142:\t\tif (is_kfunc_destructive(\u0026meta) \u0026\u0026 !capable(CAP_SYS_BOOT)) {\n14143:\t\t\tverbose(env, \"destructive kfunc calls require CAP_SYS_BOOT capability\\n\");\n14144:\t\t\toperation = bpf_diag_fmt(env, \"destructive kfunc %s\", meta.func_name);\n14145:\t\t\tbpf_diag_policy(\n14146:\t\t\t\tenv, insn_idx, operation, \"destructive kfuncs require CAP_SYS_BOOT\",\n14147:\t\t\t\t\"Load the program with CAP_SYS_BOOT, or avoid destructive kfuncs.\");\n14148:\t\t\treturn -EACCES;\n14149:\t\t}\n14150:\t\n14151:\t\tsleepable = bpf_is_kfunc_sleepable(\u0026meta);\n14152:\t\tif (sleepable \u0026\u0026 !in_sleepable(env)) {\n14153:\t\t\tverbose(env, \"program must be sleepable to call sleepable kfunc %s\\n\", func_name);\n14154:\t\t\toperation = bpf_diag_fmt(env, \"sleepable kfunc %s\", func_name);\n14155:\t\t\tbpf_diag_ctx_forbidden(env, insn_idx, operation,\n14156:\t\t\t\t\"Mark the program sleepable if the program type allows it, or use a non-sleepable kfunc.\");\n14157:\t\t\treturn -EACCES;\n14158:\t\t}\n14159:\t\n14160:\t\t/* Track non-sleepable context for kfuncs, same as for helpers. */\n14161:\t\tif (!in_sleepable_context(env))\n14162:\t\t\tinsn_aux-\u003enon_sleepable = true;\n14163:\t\n14164:\t\t/* Check the arguments */\n14165:\t\terr = check_func_args(env, \u0026meta, insn_idx);\n14166:\t\tif (err \u003c 0)\n14167:\t\t\treturn err;\n14168:\t\n14169:\t\terr = mark_raw_stack(env, \u0026meta, insn_idx);\n14170:\t\tif (err)\n14171:\t\t\treturn err;\n14172:\t\n14173:\t\tif ((is_bpf_obj_drop_kfunc(meta.func_id) ||\n14174:\t\t is_bpf_percpu_obj_drop_kfunc(meta.func_id)) \u0026\u0026 (is_tracing_prog_type(prog_type) ||\n14175:\t\t /* is_tracing_prog_type() for now doesn't cover non-iterator tracing progs. */\n14176:\t\t (prog_type == BPF_PROG_TYPE_TRACING \u0026\u0026 env-\u003eprog-\u003eexpected_attach_type != BPF_TRACE_ITER\n14177:\t\t \u0026\u0026 !env-\u003eprog-\u003esleepable))) {\n14178:\t\t\tstruct btf_struct_meta *struct_meta;\n14179:\t\n"
} |
| 27/3 | 2026/09/15 18:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22529 output: 29 thoughts: 95 Thoughts: **Analyzing the Change in `check_kfunc_call`** Okay, let's dive in. The commit I'm looking at, specifically the `check_kfunc_call` function, is the key here. Before this patch, the code completely *omitted* a crucial call to `mark_raw_stack` within this function! That's a significant oversight, and it really warrants a closer examination. I need to understand how uninitialized kfunc arguments were handled *before* this change. To do that, I'm going to search for `MEM_UNINIT` and the function `is_kfunc_arg_uninit` within the `kernel/bpf/verifier.c` file. This should give me the historical context, and the rationale behind the original approach. I suspect there might be a subtle bug that this is addressing, and I want to grasp it fully. This initial scan will guide my next steps to fully unpack this change. |
| 28/3 | 2026/09/15 18:57 | tool | grepper |
0mArgs:{
"Expression": "MEM_UNINIT",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/helpers.c=112=const struct bpf_func_proto bpf_map_pop_elem_proto = {\n--\nkernel/bpf/helpers.c-116-\t.arg1_type\t= ARG_CONST_MAP_PTR,\nkernel/bpf/helpers.c:117:\t.arg2_type\t= ARG_PTR_TO_MAP_VALUE | MEM_UNINIT | MEM_WRITE,\nkernel/bpf/helpers.c-118-};\n--\nkernel/bpf/helpers.c=125=const struct bpf_func_proto bpf_map_peek_elem_proto = {\n--\nkernel/bpf/helpers.c-129-\t.arg1_type\t= ARG_CONST_MAP_PTR,\nkernel/bpf/helpers.c:130:\t.arg2_type\t= ARG_PTR_TO_MAP_VALUE | MEM_UNINIT | MEM_WRITE,\nkernel/bpf/helpers.c-131-};\n--\nkernel/bpf/helpers.c=537=const struct bpf_func_proto bpf_strtol_proto = {\n--\nkernel/bpf/helpers.c-543-\t.arg3_type\t= ARG_ANYTHING,\nkernel/bpf/helpers.c:544:\t.arg4_type\t= ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,\nkernel/bpf/helpers.c-545-\t.arg4_size\t= sizeof(s64),\n--\nkernel/bpf/helpers.c=565=const struct bpf_func_proto bpf_strtoul_proto = {\n--\nkernel/bpf/helpers.c-571-\t.arg3_type\t= ARG_ANYTHING,\nkernel/bpf/helpers.c:572:\t.arg4_type\t= ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,\nkernel/bpf/helpers.c-573-\t.arg4_size\t= sizeof(u64),\n--\nkernel/bpf/helpers.c=1887=static const struct bpf_func_proto bpf_dynptr_from_mem_proto = {\n--\nkernel/bpf/helpers.c-1893-\t.arg3_type\t= ARG_ANYTHING,\nkernel/bpf/helpers.c:1894:\t.arg4_type\t= ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT | MEM_WRITE,\nkernel/bpf/helpers.c-1895-};\n--\nkernel/bpf/ringbuf.c=701=const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto = {\n--\nkernel/bpf/ringbuf.c-706-\t.arg3_type\t= ARG_ANYTHING,\nkernel/bpf/ringbuf.c:707:\t.arg4_type\t= ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | MEM_UNINIT | MEM_WRITE,\nkernel/bpf/ringbuf.c-708-};\n--\nkernel/bpf/syscall.c=6618=static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {\n--\nkernel/bpf/syscall.c-6624-\t.arg3_type\t= ARG_ANYTHING,\nkernel/bpf/syscall.c:6625:\t.arg4_type\t= ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,\nkernel/bpf/syscall.c-6626-\t.arg4_size\t= sizeof(u64),\n--\nkernel/bpf/verifier.c=7658=__printf(6, 7) static void bpf_diag_call_arg_fmt(struct bpf_verifier_env *env, u32 insn_idx,\n--\nkernel/bpf/verifier.c-7665- * @meta carries the dynptr and referenced-object state. The dynptr is populated\nkernel/bpf/verifier.c:7666: * when the argument is tagged with MEM_UNINIT (i.e., the dynptr argument that\nkernel/bpf/verifier.c-7667- * will be constructed) and consumed when the argument is expected to be an\n--\nkernel/bpf/verifier.c=7685=static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n--\nkernel/bpf/verifier.c-7702-\nkernel/bpf/verifier.c:7703:\t/* MEM_UNINIT - Points to memory that is an appropriate candidate for\nkernel/bpf/verifier.c-7704-\t *\t\t constructing a mutable bpf_dynptr object.\n--\nkernel/bpf/verifier.c-7716-\t */\nkernel/bpf/verifier.c:7717:\tif (arg_type \u0026 MEM_UNINIT) {\nkernel/bpf/verifier.c-7718-\t\tint i;\n--\nkernel/bpf/verifier.c=8156=static bool arg_type_is_raw_mem(enum bpf_arg_type type)\n--\nkernel/bpf/verifier.c-8164-\t\tbase_type(type) == ARG_PTR_TO_MAP_VALUE) \u0026\u0026\nkernel/bpf/verifier.c:8165:\t type \u0026 MEM_UNINIT;\nkernel/bpf/verifier.c-8166-}\n--\nkernel/bpf/verifier.c=8890=static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 prev_slot,\n--\nkernel/bpf/verifier.c-9180-\nkernel/bpf/verifier.c:9181:\t\tif (meta-\u003ebtf \u0026\u0026 (arg_type \u0026 MEM_UNINIT))\nkernel/bpf/verifier.c-9182-\t\t\tmeta-\u003earg_raw_mem.mask |= BIT(slot);\n--\nkernel/bpf/verifier.c-9191-\t\tif (meta-\u003ebtf)\nkernel/bpf/verifier.c:9192:\t\t\taccess_type = arg_type \u0026 MEM_UNINIT ? BPF_WRITE : BPF_READ | BPF_WRITE;\nkernel/bpf/verifier.c-9193-\n--\nkernel/bpf/verifier.c-9239-\t\tif (meta-\u003ebtf)\nkernel/bpf/verifier.c:9240:\t\t\taccess_type = fn-\u003earg_type[arg - 1] \u0026 MEM_UNINIT ?\nkernel/bpf/verifier.c-9241-\t\t\t\t BPF_WRITE : BPF_READ | BPF_WRITE;\n--\nkernel/bpf/verifier.c-9280-\t\tif (is_bpf_dynptr_clone_kfunc(meta) \u0026\u0026\nkernel/bpf/verifier.c:9281:\t\t (arg_type \u0026 MEM_UNINIT)) {\nkernel/bpf/verifier.c-9282-\t\t\tenum bpf_dynptr_type parent_type = meta-\u003edynptr.type;\n--\nkernel/bpf/verifier.c=12428=static int resolve_func_arg_type(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-12465-\t}\nkernel/bpf/verifier.c:12466:\t*arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | (*arg_type \u0026 (PTR_MAYBE_NULL | MEM_UNINIT));\nkernel/bpf/verifier.c-12467-\n--\nkernel/bpf/verifier.c=12811=get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-12991-\tif (is_kfunc_arg_uninit(meta-\u003ebtf, \u0026args[arg]))\nkernel/bpf/verifier.c:12992:\t\targ_type |= MEM_UNINIT;\nkernel/bpf/verifier.c-12993-\n--\nkernel/bpf/verifier.c=13705=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13784-\t\t\t */\nkernel/bpf/verifier.c:13785:\t\t\tif (at \u0026 MEM_UNINIT)\nkernel/bpf/verifier.c-13786-\t\t\t\treturn 0;\n--\nkernel/bpf/verifier.c-13804-\t/*\nkernel/bpf/verifier.c:13805:\t * MEM_UNINIT args are write-only: the helper initializes the\nkernel/bpf/verifier.c-13806-\t * buffer without reading it.\nkernel/bpf/verifier.c-13807-\t */\nkernel/bpf/verifier.c:13808:\tif (at \u0026 MEM_UNINIT)\nkernel/bpf/verifier.c-13809-\t\treturn -size;\n"
} |
| 29/3 | 2026/09/15 18:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 25204 output: 38 thoughts: 65 Thoughts: **Analyzing the Impact of this Patch** Okay, so I need to understand the implications of this patch. My immediate thought is to trace the usage of `is_kfunc_arg_uninit` *before* this change. I'm going to head straight to `kernel/bpf/verifier.c` and focus on the vicinity of lines 12980-13010. This area should give me a good baseline for comparison and help me grasp what's been altered and why. This will allow me to assess if this change has introduced any unexpected behaviors or potential regressions. Let's dig in. |
| 30/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 12980,
"LineCount": 40
}Results: {
"Contents": "12980:\t\t\tif (IS_ERR(resolve_ret)) {\n12981:\t\t\t\tverbose(env,\n12982:\t\t\t\t\t\"%s reference type('%s %s') size cannot be determined: %ld\\n\",\n12983:\t\t\t\t\treg_arg_name(env, argno), btf_type_str(ref_t),\n12984:\t\t\t\t\tref_tname, PTR_ERR(resolve_ret));\n12985:\t\t\t\treturn -EINVAL;\n12986:\t\t\t}\n12987:\t\t\tproto-\u003earg_size[arg] = type_size;\n12988:\t\t\targ_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE;\n12989:\t\t}\n12990:\t\n12991:\t\tif (is_kfunc_arg_uninit(meta-\u003ebtf, \u0026args[arg]))\n12992:\t\t\targ_type |= MEM_UNINIT;\n12993:\t\n12994:\t\tif (is_kfunc_arg_nullable(meta-\u003ebtf, \u0026args[arg]))\n12995:\t\t\targ_type |= PTR_MAYBE_NULL;\n12996:\t\n12997:\t\t/*\n12998:\t\t * Only the first argument of a KF_RELEASE kfunc releases anything, and\n12999:\t\t * bpf_fetch_kfunc_arg_meta() only ever records BPF_REG_1 for it.\n13000:\t\t */\n13001:\t\tif (is_kfunc_release(meta) \u0026\u0026 arg == 0)\n13002:\t\t\targ_type |= OBJ_RELEASE;\n13003:\t\n13004:\t\tif (base_type(arg_type) == ARG_PTR_TO_BTF_ID) {\n13005:\t\t\tif (is_kfunc_arg_map(meta-\u003ebtf, \u0026args[arg]))\n13006:\t\t\t\tproto-\u003earg_btf_id[arg] = reg2btf_ids[CONST_PTR_TO_MAP];\n13007:\t\t\telse\n13008:\t\t\t\tproto-\u003earg_btf_id[arg] = ref_id_ptr;\n13009:\t\n13010:\t\t\t/*\n13011:\t\t\t * A KF_RCU kfunc accepts an RCU-protected pointer where it would\n13012:\t\t\t * otherwise demand a referenced or trusted one. Other argument kinds\n13013:\t\t\t * have their own provenance requirements and must not inherit MEM_RCU.\n13014:\t\t\t */\n13015:\t\t\tif (is_kfunc_rcu(meta))\n13016:\t\t\t\targ_type |= MEM_RCU;\n13017:\t\t}\n13018:\t\n13019:\t\treturn arg_type;\n"
} |
| 31/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26176 output: 38 thoughts: 137 Thoughts: **Analyzing Function Argument Type Resolution** Okay, let's dive into this `resolve_func_arg_type` function. I'm focusing on this snippet here: ```c static int resolve_func_arg_type(struct bpf_verifier_env *env, ... *arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | (*arg_type & PTR_MAYBE_NULL); ``` Wait a second...I'm getting a flash of something I've seen before. Didn't we specifically *strip* `MEM_UNINIT` from `*arg_type` earlier in `resolve_func_arg_type`? I need to refresh my memory on what this function is actually doing. To get the full picture, I'll need to go back and examine the context. I'm going to jump to lines 12425 through 12470 to refresh my understanding of the flow within `resolve_func_arg_type`. I need to ensure this change doesn't introduce any unforeseen issues or dependencies on uninitialized memory, given the earlier manipulation. |
| 32/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 12425,
"LineCount": 50
}Results: {
"Contents": "12425:\t\treturn btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR);\n12426:\t}\n12427:\t\n12428:\tstatic int resolve_func_arg_type(struct bpf_verifier_env *env,\n12429:\t\t\t\t\t struct bpf_reg_state *reg, u32 arg, argno_t argno,\n12430:\t\t\t\t\t struct bpf_call_arg_meta *meta,\n12431:\t\t\t\t\t enum bpf_arg_type *arg_type, u32 *arg_size)\n12432:\t{\n12433:\t\tconst struct btf_param *args;\n12434:\t\tconst struct btf_type *ref_t, *resolve_ret;\n12435:\t\tconst struct btf *btf;\n12436:\t\tconst char *ref_tname;\n12437:\t\tu32 ref_id;\n12438:\t\n12439:\t\tif (base_type(*arg_type) == ARG_PTR_TO_MAP_VALUE)\n12440:\t\t\treturn resolve_map_arg_type(env, meta, arg_type);\n12441:\t\n12442:\t\tif (base_type(*arg_type) != ARG_PTR_TO_BTF_ID)\n12443:\t\t\treturn 0;\n12444:\t\n12445:\t\tif (!meta-\u003ebtf || arg_type_is_release(*arg_type) ||\n12446:\t\t base_type(reg-\u003etype) == PTR_TO_BTF_ID ||\n12447:\t\t reg2btf_ids[base_type(reg-\u003etype)])\n12448:\t\t\treturn 0;\n12449:\t\n12450:\t\targs = btf_params(meta-\u003efunc_proto);\n12451:\t\tref_id = *meta-\u003efn-\u003earg_btf_id[arg];\n12452:\t\tbtf = is_kfunc_arg_map(meta-\u003ebtf, \u0026args[arg]) ? btf_vmlinux : meta-\u003ebtf;\n12453:\t\tref_t = btf_type_skip_modifiers(btf, ref_id, \u0026ref_id);\n12454:\t\tref_tname = btf_name_by_offset(btf, ref_t-\u003ename_off);\n12455:\t\n12456:\t\tif (!btf_type_is_scalar_struct(env, btf, ref_t))\n12457:\t\t\treturn 0;\n12458:\t\n12459:\t\tresolve_ret = btf_resolve_size(btf, ref_t, arg_size);\n12460:\t\tif (IS_ERR(resolve_ret)) {\n12461:\t\t\tverbose(env, \"%s reference type('%s %s') size cannot be determined: %ld\\n\",\n12462:\t\t\t\treg_arg_name(env, argno), btf_type_str(ref_t), ref_tname,\n12463:\t\t\t\tPTR_ERR(resolve_ret));\n12464:\t\t\treturn -EINVAL;\n12465:\t\t}\n12466:\t\t*arg_type = ARG_PTR_TO_MEM | MEM_FIXED_SIZE | (*arg_type \u0026 (PTR_MAYBE_NULL | MEM_UNINIT));\n12467:\t\n12468:\t\treturn 0;\n12469:\t}\n12470:\t\n12471:\tstatic void btf_member_path_str(const struct btf *btf, const struct btf_member_path *path,\n12472:\t\t\t\t\tchar *buf, size_t buf_sz)\n12473:\t{\n12474:\t\tsize_t len = 0;\n"
} |
| 33/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27617 output: 37 thoughts: 40 Thoughts: Here's my take on those lines of code... **Digging into `check_func_arg` in the BPF Verifier** Alright, let's dive into this `check_func_arg` function. I need to understand how arguments are validated during BPF program verification. I know that issues here can lead to serious vulnerabilities, so I need to be thorough. I'm focusing on lines 9165 through 9250 within `kernel/bpf/verifier.c`. My expert understanding tells me this area probably deals with the core logic for ensuring arguments passed to BPF functions are sound – correct types, permitted ranges, avoiding out-of-bounds access, you know the drill. I'll need to look for things like: * **Type checking:** How does it determine if the argument types match the expected function signature? Is it doing static analysis? Dynamic checks? Both? * **Safety checks:** What guardrails are in place to prevent memory corruption or other exploits from crafted arguments? Are there bounds checks, alignment requirements, or other security mitigations? * **State management:** How does it handle the program's state when validating arguments? Does it track the symbolic values of arguments, or does it do a simpler check based on known types? * **Error reporting:** What does the error handling look like if it detects an invalid argument? Are error messages informative or vague? This is crucial for debugging bad programs. * **Relationships between arguments** Does the function evaluate the relationships between arguments? This could mean checking that a pointer is within the bounds of a buffer, or that an index is within range. I'll carefully analyze the control flow and data dependencies. I'm looking for potential weaknesses or edge cases that might be exploited. I have an intuitive grasp of the BPF verifier already. This will likely provide a valuable insight into the design decisions that were made. |
| 34/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 9170,
"LineCount": 75
}Results: {
"Contents": "9170:\t\t\t\tmeta-\u003earg_btf_id = reg-\u003ebtf_id;\n9171:\t\t\t}\n9172:\t\t\tbreak;\n9173:\t\tcase ARG_PTR_TO_FUNC:\n9174:\t\t\tmeta-\u003esubprogno = reg-\u003esubprogno;\n9175:\t\t\tbreak;\n9176:\t\tcase ARG_PTR_TO_MEM:\n9177:\t\t{\n9178:\t\t\tenum bpf_access_type access_type;\n9179:\t\t\tbool known_memory;\n9180:\t\n9181:\t\t\tif (meta-\u003ebtf \u0026\u0026 (arg_type \u0026 MEM_UNINIT))\n9182:\t\t\t\tmeta-\u003earg_raw_mem.mask |= BIT(slot);\n9183:\t\n9184:\t\t\t/* The access to this pointer is only checked when we hit the\n9185:\t\t\t * next is_mem_size argument below.\n9186:\t\t\t */\n9187:\t\t\tif (!(arg_type \u0026 MEM_FIXED_SIZE))\n9188:\t\t\t\tbreak;\n9189:\t\n9190:\t\t\taccess_type = arg_type \u0026 MEM_WRITE ? BPF_WRITE : BPF_READ;\n9191:\t\t\tif (meta-\u003ebtf)\n9192:\t\t\t\taccess_type = arg_type \u0026 MEM_UNINIT ? BPF_WRITE : BPF_READ | BPF_WRITE;\n9193:\t\n9194:\t\t\terr = check_mem_reg(env, reg, argno, arg_size, access_type, meta, \u0026known_memory);\n9195:\t\t\tif (err \u003c 0) {\n9196:\t\t\t\tif (known_memory)\n9197:\t\t\t\t\tbpf_diag_call_arg_fmt(env, insn_idx, argno, meta-\u003efunc_name,\n9198:\t\t\t\t\t\t\"Pass memory with at least the required number of accessible bytes and suitable read or write access.\",\n9199:\t\t\t\t\t\t\"the function expects %u bytes of memory, but the verifier cannot prove that %s provides a range of that size with the required read or write access\",\n9200:\t\t\t\t\t\targ_size,\n9201:\t\t\t\t\t\tbpf_diag_reg_type_plain(env, reg-\u003etype));\n9202:\t\t\t\telse\n9203:\t\t\t\t\tbpf_diag_call_arg_fmt(env, insn_idx, argno, meta-\u003efunc_name,\n9204:\t\t\t\t\t\t\"Pass stack, map, context, or other verifier-known memory of the expected type and size, not an integer cast to a pointer.\",\n9205:\t\t\t\t\t\t\"the function expects %u bytes of memory, but it is %s and not verifier-known memory\",\n9206:\t\t\t\t\t\targ_size,\n9207:\t\t\t\t\t\tbpf_diag_reg_type_plain(env, reg-\u003etype));\n9208:\t\t\t\treturn err;\n9209:\t\t\t}\n9210:\t\t\tif (arg_type \u0026 MEM_ALIGNED)\n9211:\t\t\t\terr = check_ptr_alignment(env, reg, 0, arg_size, true);\n9212:\t\t\tbreak;\n9213:\t\t}\n9214:\t\tcase ARG_CONST_MEM_SIZE:\n9215:\t\t\terr = process_const_arg(env, reg, argno, meta);\n9216:\t\t\tif (err \u003c 0) {\n9217:\t\t\t\tif (err == -EINVAL)\n9218:\t\t\t\t\tbpf_diag_call_arg_fmt(env, insn_idx, argno, meta-\u003efunc_name,\n9219:\t\t\t\t\t\t\t \"Pass a compile-time constant or a value the verifier can prove is constant at this call.\",\n9220:\t\t\t\t\t\t\t \"the function requires this memory size to be a verifier-known constant, but %s is variable on this path\",\n9221:\t\t\t\t\t\t\t reg_arg_name(env, argno));\n9222:\t\t\t\treturn err;\n9223:\t\t\t}\n9224:\t\t\tfallthrough;\n9225:\t\tcase ARG_MEM_SIZE:\n9226:\t\tcase ARG_MEM_SIZE_OR_ZERO:\n9227:\t\t{\n9228:\t\t\tstruct bpf_reg_state *buff_reg = get_func_arg_reg(caller, regs, prev_slot);\n9229:\t\t\targno_t buff_argno = argno_from_arg(prev_slot + 1);\n9230:\t\t\tenum bpf_mem_size_failure failure;\n9231:\t\t\tconst char *buff_arg, *size_arg;\n9232:\t\t\tbool zero_size_allowed;\n9233:\t\t\tu32 access_type;\n9234:\t\n9235:\t\t\tif (meta-\u003ebtf \u0026\u0026 bpf_register_is_null(buff_reg))\n9236:\t\t\t\tbreak;\n9237:\t\n9238:\t\t\taccess_type = fn-\u003earg_type[arg - 1] \u0026 MEM_WRITE ? BPF_WRITE : BPF_READ;\n9239:\t\t\tif (meta-\u003ebtf)\n9240:\t\t\t\taccess_type = fn-\u003earg_type[arg - 1] \u0026 MEM_UNINIT ?\n9241:\t\t\t\t\t BPF_WRITE : BPF_READ | BPF_WRITE;\n9242:\t\n9243:\t\t\tzero_size_allowed = meta-\u003ebtf || base_type(arg_type) == ARG_MEM_SIZE_OR_ZERO;\n9244:\t\n"
} |
| 35/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29486 output: 35 thoughts: 2404 Thoughts: **Analyzing the KFunc Uninit Patch: A Verifier and Memory Safety Deep Dive** Okay, so I'm digging into this patch. The gist is about how we handle `__uninit` arguments in kfuncs. The documentation is clear: `__uninit` marks an output pointer that a kfunc initializes without reading the input. It's about memory buffers, handling error returns, struct padding, and the stack. But the verifier's previous behavior is the key. Before this patch, the verifier used the `arg_raw_mem_desc` struct. It was only tracking a single register and size for raw memory arguments. The `check_raw_mode_ok` function seemed to only be for helpers, not kfuncs, meaning the verifier wasn't fully checking uninitialized stack memory with kfuncs. With kfuncs and uninitialized arguments, it was a mess. The verifier either failed because it required a read before a write, even for `MEM_UNINIT` or it marked a buffer as initialized when it wasn't! And even the raw memory tracking was limited to one argument! But now, the verifier marks stack slots as initialized in `check_kfunc_call`. This is problematic. The verifier checks that reads come after writes. If the verifier incorrectly marks a stack slot as initialized, that's a potential vulnerability. It means the BPF program *could* read uninitialized stack memory, leading to information leaks or other issues. And this `arg_raw_mem` struct has been extended from tracking a single argument slot with `u8 regno` to now tracking a bitmask (`u16 mask`) and a size array (`int size[MAX_BPF_FUNC_ARGS]`). This allows support for multiple raw memory arguments in both helpers and kfuncs. So, let's look at kernel memory safety. Does this patch open any avenues for copying kernel data to userspace? This is where I have to be careful. Now, wait... What's being instrumented by KMSAN? What's the context for this patch? I'm thinking about the KMSAN criteria: kernel structures to userspace, conditional logic on uninitialized variables, allocation of complex data structures with uninitialized fields, and bounds checks that allow access to uninitialized bytes. Crucially, is this happening from BPF code? Okay, let's step back and think about the BPF stack. BPF programs *run* in the kernel, and have their own stack. The verifier keeps track of stack slots using `STACK_INVALID`, `STACK_MISC`, and `STACK_ZERO`. Normally, the verifier enforces that reads happen *after* writes. We use BPF stacks for BPF code. BPF code can get the ability to read from the stack if the stack has been written to. Then this data is available. Wait, does KMSAN instrument BPF? No! It instruments kernel C code. KMSAN *does not* instrument BPF bytecode or JIT code! Is it picking up kfunc calls? What about if a kfunc is buggy? I need to check *where* the code changes are happening. I see a bunch of new `__bpf_kfunc` functions in `tools/testing/selftests/bpf/test_kmods/bpf_testmod.c`, but that's for testing, not kernel code, and it also changed documentation. I need to look carefully at the actual code changed in `include/linux/bpf_verifier.h` and `kernel/bpf/verifier.c`. I see `struct arg_raw_mem_desc` changed to track a bitmask and array of sizes and added to `bpf_call_arg_meta`. But is `bpf_call_arg_meta` zero-initialized? Where is this struct being initialized? And is this tracking of the mask and size correct? Is the logic correct in `check_kfunc_call`? |
| 36/3 | 2026/09/15 18:58 | tool | grepper |
0mArgs:{
"Expression": "struct bpf_call_arg_meta meta",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/cfg.c=453=static int visit_insn(int t, struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-520-\t\t} else if (insn-\u003esrc_reg == BPF_PSEUDO_KFUNC_CALL) {\nkernel/bpf/cfg.c:521:\t\t\tstruct bpf_call_arg_meta meta;\nkernel/bpf/cfg.c-522-\n--\nkernel/bpf/verifier.c=2849=int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)\nkernel/bpf/verifier.c-2850-{\nkernel/bpf/verifier.c:2851:\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c-2852-\tstruct bpf_kfunc_btf_tab *btf_tab;\n--\nkernel/bpf/verifier.c=10334=static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,\n--\nkernel/bpf/verifier.c-10342-\tconst struct btf_type *func, *func_proto;\nkernel/bpf/verifier.c:10343:\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c-10344-\tu32 i;\n--\nkernel/bpf/verifier.c=11508=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11516-\tstruct bpf_reg_state *regs;\nkernel/bpf/verifier.c:11517:\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c-11518-\tconst char *operation;\n--\nkernel/bpf/verifier.c=13823=s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13826-\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c:13827:\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c-13828-\tconst struct btf_param *args;\n--\nkernel/bpf/verifier.c=14069=static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14076-\tconst struct btf_type *t, *ptr_type;\nkernel/bpf/verifier.c:14077:\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c-14078-\tstruct bpf_insn_aux_data *insn_aux;\n--\nkernel/bpf/verifier.c=18051=bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,\n--\nkernel/bpf/verifier.c-18053-{\nkernel/bpf/verifier.c:18054:\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c-18055-\tconst struct bpf_func_proto *fn;\n"
} |
| 37/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33695 output: 38 thoughts: 27 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Investigating `meta` Initialization in Call Checks** Right, let's dive into the initialization of `meta` within `check_helper_call` and `check_kfunc_call`. My primary concern here is understanding how this `meta` structure is populated, since it's critical to proper call validation and potentially influences the analysis performed. I need to pinpoint the exact locations where `meta` receives its initial values, specifically looking for any potential discrepancies or areas that might lead to unexpected behavior during runtime. Any differences in how it's handled between helper calls and kfunc calls are particularly interesting. I'll need to trace the data flow to ensure there are no subtle initialization errors or race conditions that could compromise security or performance. So, let me quickly go through the relevant code paths to verify this, focusing on any preconditions or assumptions made about the state of `meta` at those points. |
| 38/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 11515,
"LineCount": 25
}Results: {
"Contents": "11515:\t\tenum bpf_type_flag ret_flag;\n11516:\t\tstruct bpf_reg_state *regs;\n11517:\t\tstruct bpf_call_arg_meta meta;\n11518:\t\tconst char *operation;\n11519:\t\tint insn_idx = *insn_idx_p;\n11520:\t\tbool changes_data;\n11521:\t\tint i, err, func_id;\n11522:\t\n11523:\t\t/* find function prototype */\n11524:\t\tfunc_id = insn-\u003eimm;\n11525:\t\terr = bpf_get_helper_proto(env, insn-\u003eimm, \u0026fn);\n11526:\t\tif (err == -ERANGE) {\n11527:\t\t\tverbose(env, \"invalid func %s#%d\\n\", func_id_name(func_id), func_id);\n11528:\t\t\treturn -EINVAL;\n11529:\t\t}\n11530:\t\n11531:\t\tif (err) {\n11532:\t\t\tverbose(env, \"program of this type cannot use helper %s#%d\\n\",\n11533:\t\t\t\tfunc_id_name(func_id), func_id);\n11534:\t\t\toperation = bpf_diag_fmt(env, \"helper %s#%d\", func_id_name(func_id), func_id);\n11535:\t\t\tbpf_diag_policy(\n11536:\t\t\t\tenv, insn_idx, operation, \"this program type does not allow the helper\",\n11537:\t\t\t\t\"Use a helper allowed for this program type, or move the logic to a compatible program type.\");\n11538:\t\t\treturn err;\n11539:\t\t}\n"
} |
| 39/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34449 output: 38 thoughts: 17 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Investigating a `memset` Call** Okay, so I need to track down this `memset` call. The context seems crucial, but without more, I'm going to start with the basics. Let's dig into where `memset(&meta, 0, ...)` is actually called. I'll need to know the scope of the `meta` variable and what's being initialized. Knowing the size being passed to `memset` is also critical. Is it a fixed size or dynamically calculated? That could tell me if there's a potential for buffer overflows or incorrect initialization. My immediate focus is going to be on understanding the surrounding code to determine why this initialization is necessary and how it relates to the overall data structure and application logic. From there I can determine if this call might be a potential point for performance or correctness issues. |
| 40/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 11540,
"LineCount": 25
}Results: {
"Contents": "11540:\t\n11541:\t\t/* eBPF programs must be GPL compatible to use GPL-ed functions */\n11542:\t\tif (!env-\u003eprog-\u003egpl_compatible \u0026\u0026 fn-\u003egpl_only) {\n11543:\t\t\tverbose(env, \"cannot call GPL-restricted function from non-GPL compatible program\\n\");\n11544:\t\t\toperation = bpf_diag_fmt(env, \"helper %s#%d\", func_id_name(func_id), func_id);\n11545:\t\t\tbpf_diag_policy(\n11546:\t\t\t\tenv, insn_idx, operation,\n11547:\t\t\t\t\"this helper is restricted to GPL-compatible programs\",\n11548:\t\t\t\t\"Use a GPL-compatible license, or replace the helper with one that is available to non-GPL programs.\");\n11549:\t\t\treturn -EINVAL;\n11550:\t\t}\n11551:\t\n11552:\t\tif (fn-\u003eallowed \u0026\u0026 !fn-\u003eallowed(env-\u003eprog)) {\n11553:\t\t\tverbose(env, \"helper call is not allowed in probe\\n\");\n11554:\t\t\toperation = bpf_diag_fmt(env, \"helper %s#%d\", func_id_name(func_id), func_id);\n11555:\t\t\tbpf_diag_policy(\n11556:\t\t\t\tenv, insn_idx, operation,\n11557:\t\t\t\t\"the helper-specific policy callback rejected this program\",\n11558:\t\t\t\t\"Use the helper only from an allowed attach point or program configuration.\");\n11559:\t\t\treturn -EINVAL;\n11560:\t\t}\n11561:\t\n11562:\t\t/* With LD_ABS/IND some JITs save/restore skb from r1. */\n11563:\t\tchanges_data = bpf_helper_changes_pkt_data(func_id);\n11564:\t\tif (changes_data \u0026\u0026 fn-\u003earg1_type != ARG_PTR_TO_CTX) {\n"
} |
| 41/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35229 output: 38 thoughts: 14 Thoughts: Okay, here's my attempt at summarizing the thought process, as requested, geared toward an expert audience, and written in the first person: **Navigating the Code - Onward to the Initialization of `meta`** Alright, I'm tracking the flow... I'm reading through this code, probably aiming to understand a specific function or module. My focus is laser-targeted on the section where `meta` gets its initial value. It's a key variable, and understanding its starting state is crucial for interpreting how the program works, especially how it operates on data or sets up the execution context. I'm mentally parsing the surrounding lines, looking for any immediate assignments, function calls that might be returning a value to it, or conditional logic that determines its initial setup. I'm already anticipating the implications of what I'll see when `meta` is finally declared! |
| 42/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 11565,
"LineCount": 35
}Results: {
"Contents": "11565:\t\t\tverifier_bug(env, \"func %s#%d: r1 != ctx\", func_id_name(func_id), func_id);\n11566:\t\t\treturn -EFAULT;\n11567:\t\t}\n11568:\t\n11569:\t\tmemset(\u0026meta, 0, sizeof(meta));\n11570:\t\n11571:\t\terr = check_func_proto(env, fn, \u0026meta);\n11572:\t\tif (err) {\n11573:\t\t\tverifier_bug(env, \"incorrect func proto %s#%d\", func_id_name(func_id), func_id);\n11574:\t\t\treturn err;\n11575:\t\t}\n11576:\t\n11577:\t\tif (fn-\u003emight_sleep \u0026\u0026 !in_sleepable_context(env)) {\n11578:\t\t\tverbose(env, \"sleepable helper %s#%d in %s\\n\", func_id_name(func_id), func_id,\n11579:\t\t\t\tnon_sleepable_context_description(env));\n11580:\t\t\toperation = bpf_diag_fmt(env, \"sleepable helper %s#%d\",\n11581:\t\t\t\t\t\t func_id_name(func_id), func_id);\n11582:\t\t\tbpf_diag_ctx_forbidden(env, insn_idx, operation,\n11583:\t\t\t\t\"Move the helper call outside the critical section, or use a non-sleepable helper.\");\n11584:\t\t\treturn -EINVAL;\n11585:\t\t}\n11586:\t\n11587:\t\t/* Track non-sleepable context for helpers. */\n11588:\t\tif (!in_sleepable_context(env))\n11589:\t\t\tenv-\u003einsn_aux_data[insn_idx].non_sleepable = true;\n11590:\t\n11591:\t\tmeta.func_id = func_id;\n11592:\t\tmeta.func_name = func_id_name(func_id);\n11593:\t\tmeta.fn = fn;\n11594:\t\terr = check_func_args(env, \u0026meta, insn_idx);\n11595:\t\tif (err)\n11596:\t\t\treturn err;\n11597:\t\n11598:\t\terr = record_func_map(env, \u0026meta, func_id, insn_idx);\n11599:\t\tif (err)\n"
} |
| 43/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36128 output: 38 thoughts: 40 Thoughts: Here's my summary, as if I were thinking these thoughts: **Analyzing Memory Initialization and Function Calls** Okay, here we go. Line 11569. Ah, `memset(&meta, 0, sizeof(meta));` - that's a classic. They're explicitly zeroing out the `meta` structure. Makes perfect sense for initialization, especially given the context of the larger piece of code I'm probably looking at. Keeps things predictable. Now, before I move on, let me quickly glance at `check_kfunc_call`. Gotta make sure the kernel function call verification is behaving as expected, making sure nothing untoward is happening with the function call. Important to double check all areas that might lead to an access violation. This looks like a potentially important check to make sure the program is behaving correctly! |
| 44/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 14075,
"LineCount": 40
}Results: {
"Contents": "14075:\t\tconst char *func_name, *ptr_type_name;\n14076:\t\tconst struct btf_type *t, *ptr_type;\n14077:\t\tstruct bpf_call_arg_meta meta;\n14078:\t\tstruct bpf_insn_aux_data *insn_aux;\n14079:\t\tconst char *operation;\n14080:\t\tint err, insn_idx = *insn_idx_p;\n14081:\t\tu32 i, proto_slots, ptr_type_id, ret_nregs = 1;\n14082:\t\tstruct bpf_kfunc_desc *desc;\n14083:\t\tstruct btf *desc_btf;\n14084:\t\tint id;\n14085:\t\n14086:\t\t/* skip for now, but return error when we find this in fixup_kfunc_call */\n14087:\t\tif (!insn-\u003eimm)\n14088:\t\t\treturn 0;\n14089:\t\n14090:\t\terr = bpf_fetch_kfunc_arg_meta(env, insn-\u003eimm, insn-\u003eoff, \u0026meta);\n14091:\t\tif (err == -EACCES \u0026\u0026 meta.func_name) {\n14092:\t\t\tverbose(env, \"calling kernel function %s is not allowed\\n\", meta.func_name);\n14093:\t\t\toperation = bpf_diag_fmt(env, \"kfunc %s\", meta.func_name);\n14094:\t\t\tbpf_diag_policy(\n14095:\t\t\t\tenv, insn_idx, operation, \"this program cannot call the kfunc\",\n14096:\t\t\t\t\"Use a kfunc allowed for this program type and attach point, or change the program context.\");\n14097:\t\t}\n14098:\t\tif (err)\n14099:\t\t\treturn err;\n14100:\t\tdesc_btf = meta.btf;\n14101:\t\tfunc_name = meta.func_name;\n14102:\t\tinsn_aux = \u0026env-\u003einsn_aux_data[insn_idx];\n14103:\t\n14104:\t\tdesc = find_kfunc_desc(env-\u003eprog, insn-\u003eimm, insn-\u003eoff);\n14105:\t\tif (!desc) {\n14106:\t\t\tverifier_bug(env, \"kfunc descriptor not found for func_id %u\", insn-\u003eimm);\n14107:\t\t\treturn -EFAULT;\n14108:\t\t}\n14109:\t\tmeta.fn = \u0026desc-\u003eproto;\n14110:\t\n14111:\t\tinsn_aux-\u003eis_iter_next = bpf_is_iter_next_kfunc(\u0026meta);\n14112:\t\n14113:\t\tif (!insn-\u003eoff \u0026\u0026\n14114:\t\t (insn-\u003eimm == special_kfunc_list[KF_bpf_res_spin_lock] ||\n"
} |
| 45/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37177 output: 37 thoughts: 22 Thoughts: Here's a first-person summary, tailored for an expert audience, based on your prompt: **Diving into `bpf_fetch_kfunc_arg_meta` Initialization** Okay, so I need to understand how the `meta` variable is getting set up within `bpf_fetch_kfunc_arg_meta`. It's crucial for understanding the overall execution context, especially when dealing with kfuncs and their arguments within the BPF framework. I need to trace this initialization to be absolutely sure about how the program is getting information about a certain kernel function argument. I need to be sure that it is properly set up from the call to the execution. That's the core question here. Where does `meta` *actually* get its initial state? I want to make sure the arguments for a BPF program are not only valid but are also ready to be used. Let's see... |
| 46/3 | 2026/09/15 18:58 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 2800,
"LineCount": 40
}Results: {
"Contents": "2800:\t\t\tverbose(env, \"failed to find BTF for kernel function\\n\");\n2801:\t\t\treturn PTR_ERR(btf);\n2802:\t\t}\n2803:\t\n2804:\t\t/*\n2805:\t\t * Note that kfunc_flags may be NULL at this point, which\n2806:\t\t * means that we couldn't find func_id in any relevant\n2807:\t\t * kfunc_id_set. This most likely indicates an invalid kfunc\n2808:\t\t * call. However we don't fail with an error here,\n2809:\t\t * and let the caller decide what to do with NULL kfunc-\u003eflags.\n2810:\t\t */\n2811:\t\tkfunc_flags = btf_kfunc_flags(btf, func_id, env-\u003eprog);\n2812:\t\n2813:\t\tfunc = btf_type_by_id(btf, func_id);\n2814:\t\tif (!func || !btf_type_is_func(func)) {\n2815:\t\t\tverbose(env, \"kernel btf_id %d is not a function\\n\", func_id);\n2816:\t\t\treturn -EINVAL;\n2817:\t\t}\n2818:\t\n2819:\t\tfunc_name = btf_name_by_offset(btf, func-\u003ename_off);\n2820:\t\n2821:\t\t/*\n2822:\t\t * An actual prototype of a kfunc with KF_IMPLICIT_ARGS flag\n2823:\t\t * can be found through the counterpart _impl kfunc.\n2824:\t\t */\n2825:\t\tif (kfunc_flags \u0026\u0026 (*kfunc_flags \u0026 KF_IMPLICIT_ARGS))\n2826:\t\t\tfunc_proto = find_kfunc_impl_proto(\u0026env-\u003elog, btf, func_name);\n2827:\t\telse\n2828:\t\t\tfunc_proto = btf_type_by_id(btf, func-\u003etype);\n2829:\t\n2830:\t\tif (!func_proto || !btf_type_is_func_proto(func_proto)) {\n2831:\t\t\tverbose(env, \"kernel function btf_id %d does not have a valid func_proto\\n\",\n2832:\t\t\t\tfunc_id);\n2833:\t\t\treturn -EINVAL;\n2834:\t\t}\n2835:\t\n2836:\t\tmemset(kfunc, 0, sizeof(*kfunc));\n2837:\t\tkfunc-\u003ebtf = btf;\n2838:\t\tkfunc-\u003eid = func_id;\n2839:\t\tkfunc-\u003ename = func_name;\n"
} |
| 47/3 | 2026/09/15 18:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38127 output: 32 thoughts: 380 Thoughts: **My Immediate Thoughts on the BPF Code** Okay, so I'm diving into this BPF code and my initial thought is: why the `memset` of `kfunc`, which is actually `meta`, to zero? I need to understand that better. Something feels off, but let's press on. Next, I see the `mark_raw_stack` function. I recognize this as part of the verifier's argument validation process. It looks like it's iterating through arguments to ensure they're valid before initializing any outputs. That makes sense, handling potential aliasing between input and output buffers is critical here, especially in this context. And the iteration through `slot`, checking memory access with `check_mem_access`, looks right. It iterates through the size specified by `meta->arg_raw_mem.size[slot]` which indicates that the size is stored per argument and looks correct. However, I'm stuck on how `get_func_arg_reg` works. I really need to understand how it's retrieving the register state for each function argument. It's crucial for the memory access checks. I need to know how it's identifying the correct registers. This is fundamental for verifying the arguments and prevents any sneaky behavior. So, let's look at that function! |
| 48/3 | 2026/09/15 18:59 | tool | grepper |
0mArgs:{
"Expression": "get_func_arg_reg",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/verifier.c=4322=static int check_outgoing_stack_args(struct bpf_verifier_env *env, struct bpf_func_state *caller,\n--\nkernel/bpf/verifier.c-4346-\nkernel/bpf/verifier.c:4347:static struct bpf_reg_state *get_func_arg_reg(struct bpf_func_state *caller,\nkernel/bpf/verifier.c-4348-\t\t\t\t\t struct bpf_reg_state *regs, int arg)\n--\nkernel/bpf/verifier.c=8890=static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 prev_slot,\n--\nkernel/bpf/verifier.c-8898-\targno_t argno = argno_from_arg(slot + 1);\nkernel/bpf/verifier.c:8899:\tstruct bpf_reg_state *reg = get_func_arg_reg(caller, regs, slot);\nkernel/bpf/verifier.c-8900-\tenum bpf_arg_type arg_type = fn-\u003earg_type[arg];\n--\nkernel/bpf/verifier.c-9227-\t{\nkernel/bpf/verifier.c:9228:\t\tstruct bpf_reg_state *buff_reg = get_func_arg_reg(caller, regs, prev_slot);\nkernel/bpf/verifier.c-9229-\t\targno_t buff_argno = argno_from_arg(prev_slot + 1);\n--\nkernel/bpf/verifier.c=9482=static int check_arg_extra_slot(struct bpf_verifier_env *env, struct bpf_func_state *caller,\n--\nkernel/bpf/verifier.c-9484-{\nkernel/bpf/verifier.c:9485:\tstruct bpf_reg_state *reg = get_func_arg_reg(caller, cur_regs(env), slot);\nkernel/bpf/verifier.c-9486-\targno_t argno = argno_from_arg(slot + 1);\n--\nkernel/bpf/verifier.c=9559=static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-9571-\tfor (slot = 0; slot \u003c MAX_BPF_FUNC_ARGS; slot++) {\nkernel/bpf/verifier.c:9572:\t\tstruct bpf_reg_state *reg = get_func_arg_reg(caller, cur_regs(env), slot);\nkernel/bpf/verifier.c-9573-\t\targno_t argno = argno_from_arg(slot + 1);\n--\nkernel/bpf/verifier.c=10334=static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,\n--\nkernel/bpf/verifier.c-10377-\t\targno_t argno = argno_from_arg(i + 1);\nkernel/bpf/verifier.c:10378:\t\tstruct bpf_reg_state *reg = get_func_arg_reg(caller, regs, i);\nkernel/bpf/verifier.c-10379-\t\tstruct bpf_subprog_arg_info *arg = \u0026sub-\u003eargs[i];\n"
} |
| 49/3 | 2026/09/15 18:59 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39697 output: 34 thoughts: 199 Thoughts: **Analyzing BPF Argument Registers and Memory Access** Okay, let's break this down. I'm looking at how arguments are handled, specifically the register allocation. `get_func_arg_reg(caller, cur_regs(env), slot)` is responsible for retrieving the register associated with a specific argument slot, `slot`. That seems straightforward enough. Now, I'm diving into `mark_raw_stack`, which is where things get interesting. I see a loop from slot 0 up to `MAX_BPF_FUNC_ARGS - 1`. If `meta->arg_raw_mem.size[slot]` is zero, the inner loop is skipped for that slot. That makes sense; no size, no memory to check. However, if the size is greater than zero, then we run a loop and, for each byte `i`, it calls `check_mem_access(env, insn_idx, reg, argno, i, BPF_B, BPF_WRITE, -1, false, false)`. The crucial question is, how does `check_mem_access` behave when the register, `reg`, points to the stack? That's the core of what I need to understand. I need to make sure the memory access checks are being performed correctly on stack variables. Time to go right to the source. I'm opening up `kernel/bpf/verifier.c` now to examine the implementation of `check_mem_access` and confirm my assumptions about how it handles stack pointers. |
| 50/3 | 2026/09/15 18:59 | tool | grepper |
0mArgs:{
"Expression": "check_mem_access\\(",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=59=static bool bpf_global_percpu_ma_set;\n--\nkernel/bpf/verifier.c-106- * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are\nkernel/bpf/verifier.c:107: * four pointer types recognized by check_mem_access() function.\nkernel/bpf/verifier.c-108- *\n--\nkernel/bpf/verifier.c=3565=static void scrub_special_slot(struct bpf_func_state *state, int spi)\n--\nkernel/bpf/verifier.c-3577-/* check_stack_{read,write}_fixed_off functions track spill/fill of registers,\nkernel/bpf/verifier.c:3578: * stack boundary and alignment are checked in check_mem_access()\nkernel/bpf/verifier.c-3579- */\n--\nkernel/bpf/verifier.c=4356=static int check_map_access_type(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n--\nkernel/bpf/verifier.c-4377-/* check read/write into memory region (e.g., map value, ringbuf sample, etc) */\nkernel/bpf/verifier.c:4378:static int __check_mem_access(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno,\nkernel/bpf/verifier.c-4379-\t\t\t int off, int size, u32 mem_size,\n--\nkernel/bpf/verifier.c=4416=static int check_mem_region_access(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno,\n--\nkernel/bpf/verifier.c-4450-\nkernel/bpf/verifier.c:4451:\terr = __check_mem_access(env, reg, argno, reg_smin(reg) + off, size,\nkernel/bpf/verifier.c-4452-\t\t\t\t mem_size, zero_size_allowed);\n--\nkernel/bpf/verifier.c-4479-\nkernel/bpf/verifier.c:4480:\terr = __check_mem_access(env, reg, argno, reg_umax(reg) + off, size,\nkernel/bpf/verifier.c-4481-\t\t\t\t mem_size, zero_size_allowed);\n--\nkernel/bpf/verifier.c=6409=static int check_map_mem_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int off,\n--\nkernel/bpf/verifier.c-6459- */\nkernel/bpf/verifier.c:6460:static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct bpf_reg_state *reg, argno_t argno,\nkernel/bpf/verifier.c-6461-\t\t\t int off, int bpf_size, enum bpf_access_type t,\n--\nkernel/bpf/verifier.c=6714=static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-6748-\tbpf_diag_mod_begin(env, \u0026regs[insn-\u003edst_reg], NULL, BPF_DIAG_MOD_WRITE);\nkernel/bpf/verifier.c:6749:\terr = check_mem_access(env, env-\u003einsn_idx, regs + insn-\u003esrc_reg, argno_from_reg(insn-\u003esrc_reg), insn-\u003eoff,\nkernel/bpf/verifier.c-6750-\t\t\t BPF_SIZE(insn-\u003ecode), BPF_READ, insn-\u003edst_reg,\n--\nkernel/bpf/verifier.c=6761=static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-6790-\t/* Check if (dst_reg + off) is writeable. */\nkernel/bpf/verifier.c:6791:\terr = check_mem_access(env, env-\u003einsn_idx, regs + insn-\u003edst_reg, argno_from_reg(insn-\u003edst_reg), insn-\u003eoff,\nkernel/bpf/verifier.c-6792-\t\t\t BPF_SIZE(insn-\u003ecode), BPF_WRITE, insn-\u003esrc_reg,\n--\nkernel/bpf/verifier.c=6799=static int check_atomic_rmw(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-6859-\t */\nkernel/bpf/verifier.c:6860:\terr = check_mem_access(env, env-\u003einsn_idx, dst_reg, argno_from_reg(insn-\u003edst_reg), insn-\u003eoff,\nkernel/bpf/verifier.c-6861-\t\t\t BPF_SIZE(insn-\u003ecode), BPF_READ, -1, true, false);\n--\nkernel/bpf/verifier.c-6863-\t\tbpf_diag_mod_begin(env, cur_regs(env) + load_reg, NULL, BPF_DIAG_MOD_WRITE);\nkernel/bpf/verifier.c:6864:\t\terr = check_mem_access(env, env-\u003einsn_idx, dst_reg, argno_from_reg(insn-\u003edst_reg),\nkernel/bpf/verifier.c-6865-\t\t\t\t insn-\u003eoff, BPF_SIZE(insn-\u003ecode),\n--\nkernel/bpf/verifier.c-6876-\t/* Check whether we can write into the same memory. */\nkernel/bpf/verifier.c:6877:\terr = check_mem_access(env, env-\u003einsn_idx, dst_reg, argno_from_reg(insn-\u003edst_reg), insn-\u003eoff,\nkernel/bpf/verifier.c-6878-\t\t\t BPF_SIZE(insn-\u003ecode), BPF_WRITE, -1, true, false);\n--\nkernel/bpf/verifier.c=7685=static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n--\nkernel/bpf/verifier.c-7730-\t\tfor (i = 0; i \u003c BPF_DYNPTR_SIZE; i += 8) {\nkernel/bpf/verifier.c:7731:\t\t\terr = check_mem_access(env, insn_idx, reg, argno,\nkernel/bpf/verifier.c-7732-\t\t\t\t\t i, BPF_DW, BPF_WRITE, -1, false, false);\n--\nkernel/bpf/verifier.c=7822=static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno, int insn_idx,\n--\nkernel/bpf/verifier.c-7872-\t\tfor (i = 0; i \u003c nr_slots * 8; i += BPF_REG_SIZE) {\nkernel/bpf/verifier.c:7873:\t\t\terr = check_mem_access(env, insn_idx, reg, argno,\nkernel/bpf/verifier.c-7874-\t\t\t\t\t i, BPF_DW, BPF_WRITE, -1, false, false);\n--\nkernel/bpf/verifier.c=9559=static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-9575-\t\tfor (i = 0; i \u003c meta-\u003earg_raw_mem.size[slot]; i++) {\nkernel/bpf/verifier.c:9576:\t\t\terr = check_mem_access(env, insn_idx, reg, argno, i, BPF_B,\nkernel/bpf/verifier.c-9577-\t\t\t\t\t BPF_WRITE, -1, false, false);\n--\nkernel/bpf/verifier.c=11508=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11859-\t\t\t\t * it will confuse the check of PTR_TO_BTF_ID in\nkernel/bpf/verifier.c:11860:\t\t\t\t * check_mem_access().\nkernel/bpf/verifier.c-11861-\t\t\t\t */\n--\nkernel/bpf/verifier.c=13167=static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno,\n--\nkernel/bpf/verifier.c-13200-\nkernel/bpf/verifier.c:13201:\t\terr = check_mem_access(env, env-\u003einsn_idx, reg, argno, 0, BPF_DW,\nkernel/bpf/verifier.c-13202-\t\t\t\t BPF_WRITE, -1, false, false);\n--\nkernel/bpf/verifier.c=18616=static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)\n--\nkernel/bpf/verifier.c-18653-\nkernel/bpf/verifier.c:18654:\t\terr = check_mem_access(env, env-\u003einsn_idx, cur_regs(env) + insn-\u003edst_reg, argno_from_reg(insn-\u003edst_reg),\nkernel/bpf/verifier.c-18655-\t\t\t\t insn-\u003eoff, BPF_SIZE(insn-\u003ecode),\n"
} |
| 51/3 | 2026/09/15 18:59 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42771 output: 27 thoughts: 1295 Thoughts: **Analyzing the Intersection of BPF, the Verifier, and KMSAN** Okay, let's break this down. My initial focus is on `check_mem_access` – it handles BPF stack simulation during verification, which is crucial. It marks stack slots written during a simulated `BPF_WRITE` as initialized (`STACK_MISC`), a key part of the abstract interpretation process. Now, let's step back... what about KMSAN? It's all about catching reads of uninitialized kernel memory: stack, heap, pages. It *specifically* checks for leaks from the kernel to user space. Does KMSAN even look at the BPF program's abstract state? No, it's concerned with the C code running in the kernel itself and will never check the abstract state of a BPF program. So, this patch, where is it modifying? It's in `bpf/verifier.c`, dealing with how the verifier handles `__uninit`-tagged kfunc arguments. Are there kernel structures being sent to user space where initialization might be an issue? No, this patch isn't about that. And the kfuncs themselves, are they causing any issues? I checked those test kfuncs, and they're *fully* initializing their output pointers! The functions are found in a `selftest` directory, so they are not part of the production code. Alright, here's where the real question lies: Could KMSAN catch a BPF program reading uninitialized kernel memory? What if the verifier has a bug and *thinks* the BPF stack is initialized when it isn't? Then the BPF program reads from the uninitialized stack, and perhaps writes it to a map or sends it somewhere. But let's think about the BPF stack in the JIT. On x86_64 or with the BPF interpreter, the BPF stack *is* on the kernel stack of the executing thread. Is that kernel stack initialized *before* the BPF program runs? Nope! The kernel stack has whatever was left there by the previous stack frame. Could a verifier bug that lets BPF read from this uninitialized area be detectable by KMSAN? Hold on... does KMSAN instrument BPF JIT code? No, KMSAN's instrumentation is through a compiler plugin for C code, compiled with Clang/GCC! The JIT generates native machine code at runtime – KMSAN has *zero* instrumentation for JITted instructions. What about if the BPF interpreter is used? Does syzkaller run KMSAN on BPF? I need to check how KMSAN interacts with BPF specifically. Let's start by searching the kernel sources for KMSAN and BPF. |
| 52/3 | 2026/09/15 18:59 | tool | grepper |
0mArgs:{
"Expression": "kmsan",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/core.c=2409=static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \\\n--\nkernel/bpf/core.c-2413-\\\nkernel/bpf/core.c:2414:\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\nkernel/bpf/core.c-2415-\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n--\nkernel/bpf/core.c=2422=static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \\\n--\nkernel/bpf/core.c-2427-\\\nkernel/bpf/core.c:2428:\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\nkernel/bpf/core.c-2429-\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n"
} |
| 53/3 | 2026/09/15 18:59 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44956 output: 37 thoughts: 56 Thoughts: Here's my take: **A Kernel BPF Intrigue** Okay, hold on a second. Did I just see what I *think* I saw? `kmsan_unpoison_memory(stack, sizeof(stack));` called from within `kernel/bpf/core.c`? That's... interesting. Let's dig into this. I'm going to jump straight over to that file, likely around line 2409, to get the full context of what's happening. I need to understand what's being unpoisoned and, more importantly, *why*. This could be a critical piece in understanding the memory management, and potential vulnerabilities, related to the BPF subsystem. I have a feeling this is going to be a good one. |
| 54/3 | 2026/09/15 18:59 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/core.c",
"FirstLine": 2405,
"LineCount": 35
}Results: {
"Contents": "2405:\t}\n2406:\t\n2407:\t#define PROG_NAME(stack_size) __bpf_prog_run##stack_size\n2408:\t#define DEFINE_BPF_PROG_RUN(stack_size) \\\n2409:\tstatic unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \\\n2410:\t{ \\\n2411:\t\tu64 stack[stack_size / sizeof(u64)]; \\\n2412:\t\tu64 regs[MAX_BPF_EXT_REG] = {}; \\\n2413:\t\\\n2414:\t\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\n2415:\t\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n2416:\t\tARG1 = (u64) (unsigned long) ctx; \\\n2417:\t\treturn ___bpf_prog_run(regs, insn); \\\n2418:\t}\n2419:\t\n2420:\t#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size\n2421:\t#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \\\n2422:\tstatic u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \\\n2423:\t\t\t\t\t const struct bpf_insn *insn) \\\n2424:\t{ \\\n2425:\t\tu64 stack[stack_size / sizeof(u64)]; \\\n2426:\t\tu64 regs[MAX_BPF_EXT_REG]; \\\n2427:\t\\\n2428:\t\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\n2429:\t\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n2430:\t\tBPF_R1 = r1; \\\n2431:\t\tBPF_R2 = r2; \\\n2432:\t\tBPF_R3 = r3; \\\n2433:\t\tBPF_R4 = r4; \\\n2434:\t\tBPF_R5 = r5; \\\n2435:\t\treturn ___bpf_prog_run(regs, insn); \\\n2436:\t}\n2437:\t\n2438:\t#define EVAL1(FN, X) FN(X)\n2439:\t#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)\n"
} |
| 55/3 | 2026/09/15 18:59 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 45922 output: 30 thoughts: 1383 Thoughts: **Analyzing BPF Verifier for Uninitialized Memory**
Okay, so I'm digging into how the BPF verifier handles uninitialized memory, specifically around stack operations. The initial observation is that `kmsan_unpoison_memory(stack, sizeof(stack))` explicitly unpoisons the interpreter stack, which is good, but the JITted code skips KMSAN instrumentation altogether, which means the verifier really is our last line of defense. Now, let's see if the verifier itself can be tricked.
My focus is the verifier code in `kernel/bpf/verifier.c`. I'm looking for any potential avenues where uninitialized kernel memory might be used or introduced. The recent changes are interesting.
* `check_stack_range_initialized` is updated, the change seems to fix the issue where `arg_raw_mem.size` was not being indexed correctly using `arg_slot`.
* I noticed `meta->arg_raw_mem` is zero-initialized using `memset` at the beginning of `check_helper_call` and `check_kfunc_call`. That's important. This means that, at the start of a function call, all the tracking information is clean.
* In `check_mem_size_reg`, there's a modification to use a bitmask (`mask`) instead of just a register number (`regno`) to track raw memory. This is probably a good thing. The same goes for `check_func_arg`.
* The conditional checks using `MEM_UNINIT` look relevant. If `meta->btf && (arg_type & MEM_UNINIT)`, a bit is set in the `mask`. This is tracking uninitialized memory. Also, the access type is modified in two places, based on whether `arg_type` has the `MEM_UNINIT` flag set.
* Now, `mark_raw_stack` is where things get interesting. I see a loop from `slot = 0` to `MAX_BPF_FUNC_ARGS - 1`, and it's using this `slot` to iterate and check each argument for potential stack access via `check_mem_access`.
* Okay, wait a second. Could `slot` cause an out-of-bounds access? It's looping from 0 to `MAX_BPF_FUNC_ARGS - 1`. That seems like a reasonable range, but I need to make sure I know what `MAX_BPF_FUNC_ARGS` is. I'd better double check that definition.
* In `check_raw_mode_ok` the logic has changed to use the `mask` for conditional checks. Instead of checking if `regno` is set, now a bit is being used from the `mask`.
* `mark_raw_stack` is called inside `check_helper_call` and `check_kfunc_call`. This confirms that we are indeed going to validate the argument for uninitialized mem.
* The `resolve_func_arg_type` function seems to deal with arguments that are memory pointers, and flags those with the MEM_UNINIT flag.
* `bpf_kfunc_stack_access_bytes` calls `is_kfunc_arg_uninit` which appears related to whether an argument is uninitialized.
Overall, it seems the code is tracking uninitialized memory usage within arguments to BPF helper calls more carefully now, and using the `arg_raw_mem.mask` as a bitfield is a good approach for that. The key seems to be the `mark_raw_stack` function. I will check the MAX_BPF_FUNC_ARGS to ensure there aren't any out of bound access.
|
| 56/3 | 2026/09/15 18:59 | tool | grepper |
0mArgs:{
"Expression": "MAX_BPF_FUNC_ARGS",
"PathPrefix": "include/"
}Results: {
"Output": "include/linux/bpf.h=979=static_assert(__BPF_RET_TYPE_MAX \u003c= BPF_BASE_TYPE_LIMIT);\n--\ninclude/linux/bpf.h-988- */\ninclude/linux/bpf.h:989:#define MAX_BPF_FUNC_ARGS 12\ninclude/linux/bpf.h-990-\n--\ninclude/linux/bpf.h-996-/* A by-value argument takes two eightbytes at most, so the maximum number of\ninclude/linux/bpf.h:997: * argument slots of any function is 2 * MAX_BPF_FUNC_ARGS. A local array may\ninclude/linux/bpf.h-998- * need that size for processing, although eventually the maximum slots will\ninclude/linux/bpf.h:999: * be capped at MAX_BPF_FUNC_ARGS.\ninclude/linux/bpf.h-1000- */\ninclude/linux/bpf.h:1001:#define MAX_BPF_FUNC_ARG_SLOTS (2 * MAX_BPF_FUNC_ARGS)\ninclude/linux/bpf.h-1002-\n--\ninclude/linux/bpf.h=1007=struct bpf_func_proto {\n--\ninclude/linux/bpf.h-1026-\t\t};\ninclude/linux/bpf.h:1027:\t\tenum bpf_arg_type arg_type[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf.h-1028-\t};\n--\ninclude/linux/bpf.h-1036-\t\t};\ninclude/linux/bpf.h:1037:\t\tconst u32 *arg_btf_id[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf.h-1038-\t\tstruct {\n--\ninclude/linux/bpf.h-1044-\t\t};\ninclude/linux/bpf.h:1045:\t\tsize_t arg_size[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf.h-1046-\t};\n--\ninclude/linux/bpf.h=1232=struct btf_func_model {\n--\ninclude/linux/bpf.h-1235-\tu8 nr_args;\ninclude/linux/bpf.h:1236:\tu8 arg_size[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf.h:1237:\tu8 arg_flags[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf.h-1238-};\n--\ninclude/linux/bpf.h=3158=static inline bool bpf_tracing_ctx_access(int off, int size,\n--\ninclude/linux/bpf.h-3160-{\ninclude/linux/bpf.h:3161:\tif (off \u003c 0 || off \u003e= sizeof(__u64) * MAX_BPF_FUNC_ARGS)\ninclude/linux/bpf.h-3162-\t\treturn false;\n--\ninclude/linux/bpf_verifier.h=441=static_assert(MAX_BPF_STACK / 8 \u003c= (1 \u003c\u003c 6));\n--\ninclude/linux/bpf_verifier.h-443-/* Maximum number of bpf_reg_state objects that can exist at once */\ninclude/linux/bpf_verifier.h:444:#define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS)\ninclude/linux/bpf_verifier.h-445-#define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE + \\\n--\ninclude/linux/bpf_verifier.h=803=struct bpf_subprog_info {\n--\ninclude/linux/bpf_verifier.h-833-\tenum priv_stack_mode priv_stack_mode;\ninclude/linux/bpf_verifier.h:834:\tstruct bpf_subprog_arg_info args[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf_verifier.h-835-\tu16 stack_arg_cnt; /* incoming + max outgoing */\n--\ninclude/linux/bpf_verifier.h=1554=struct arg_raw_mem_desc {\ninclude/linux/bpf_verifier.h-1555-\tu16 mask;\ninclude/linux/bpf_verifier.h:1556:\tint size[MAX_BPF_FUNC_ARGS];\ninclude/linux/bpf_verifier.h-1557-};\n"
} |
| 57/3 | 2026/09/15 18:59 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49316 output: 453 thoughts: 1286 Thoughts: **KMSAN Assessment: Analyzing the BPF Verifier Patch** Okay, let's break this down. My initial assessment is that this patch, while touching a few areas, seems relatively contained. The core of it seems to be in the BPF verifier, specifically related to tracking uninitialized memory access within BPF programs. First, I see the change from `u8 regno; int size;` to `u16 mask; int size[MAX_BPF_FUNC_ARGS];` in `struct arg_raw_mem_desc`. The `u16 mask` allows me to track whether each argument slot is potentially uninitialized. Given that `mask` is a `u16` and the maximum number of BPF function arguments, `MAX_BPF_FUNC_ARGS`, is 12, the `BIT(slot)` approach for bitmasking seems safe. Now, looking at the code, it's about tracking memory access within BPF programs. Specifically, it seems designed to prevent potentially unsafe memory access within BPF programs by making sure that all memory is initialized before usage. The changes in `verifier.c` seem to be the heart of it. The bitmask is used to mark uninitialized memory regions. The verifier then explicitly checks and sets/clears bits within this mask, ensuring that access to potentially uninitialized memory within BPF programs is correctly flagged. Importantly, these changes seem to be abstract state tracking, and don't introduce direct kernel memory allocations or manipulation that might be vulnerable to uninitialized memory reads. I've carefully considered what this patch touches: documentation, the header file defining the structures, and the verifier code itself. I'm focusing on whether any of this introduces risks related to uninitialized memory in the *kernel*, specifically: 1. **Kernel memory exposure:** No, there's no evidence of new kernel structures being sent to userspace with potentially uninitialized fields, or that uninitialized kernel memory is being exposed to userspace. 2. **Conditional logic based on uninitialized data:** No. All variables involved in the logic, like `meta`, `slot`, `i`, and `err`, are explicitly initialized before use. 3. **Complex data structures with uninitialized fields:** No. The `bpf_call_arg_meta` structure is explicitly zero-initialized using `memset` in `check_helper_call` and `bpf_fetch_kfunc_arg_meta` to handle `check_kfunc_call`, eliminating that concern. 4. **Bounds check vulnerabilities:** This patch doesn't appear to be modifying bounds checks in a way that would allow the access of uninitialized memory in C kernel buffers, it’s all in the BPF verifier's abstract state. Now, let's consider potential bug scenarios: * **Out-of-bounds array access (e.g., `meta->arg_raw_mem.size[arg_slot]`)**: The code explicitly checks to ensure `arg_slot` is within bounds before using it in the `mask` or `size` array: `arg_slot < MAX_BPF_FUNC_ARGS`. KASAN should catch any out-of-bounds reads. * **Logic bugs in the verifier:** Standard verifier tests and fuzzing methods should detect those errors, not KMSAN. My conclusion is that KMSAN is not applicable here because there are no *kernel* code areas being modified that are susceptible to uninitialized memory reads. The BPF verifier abstract state is tracked and any access to that tracked state is safe. This is a crucial distinction. All changes are contained within the BPF verifier and do not expose kernel memory vulnerabilities or leak information. This is all verifier abstract state, and the code explicitly addresses those issues with proper array bounds checks. I've re-reviewed the code specifically for: * `copy_to_user`/`put_user` calls: None are touched. * Network buffer/netlink/ioctl etc. interactions: No relevant changes. * New stack/heap allocations with uninitialized reads: No, the `meta` struct initialization covers that. * Reading uninitialized fields in new logic: All local variables are initialized. * Kfuncs exposing kernel memory: The test kfuncs use constants or calculations, not raw kernel memory. * Memory error risks in verifier logic: KASAN will catch array out-of-bounds accesses. Everything is consistent. We're good to go. |
| 58/3 | 2026/09/15 18:59 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series updates the BPF verifier's handling of kfunc arguments tagged with '__uninit' and raw memory arguments across helper and kfunc calls. Specifically, it expands 'struct arg_raw_mem_desc' in 'struct bpf_call_arg_meta' from tracking a single argument register and size to a bitmask and per-slot size array ('mask' and 'size[MAX_BPF_FUNC_ARGS]'), updates stack range checks and access types during verifier analysis, and adds 'mark_raw_stack()' to check and mark stack slots as written for all raw memory arguments. It also updates BPF selftests and documentation.\n\nKMSAN vs. KASAN applicability:\n- The changes are strictly within the BPF verifier's abstract analysis logic ('kernel/bpf/verifier.c', 'include/linux/bpf_verifier.h') and test code ('tools/testing/selftests/').\n- No kernel heap or stack buffers copied to userspace (via copy_to_user, put_user, netlink, ioctls, etc.) are introduced or altered.\n- All verifier metadata structures, including 'struct bpf_call_arg_meta', are fully zero-initialized (via memset) before use in 'check_helper_call()' and 'check_kfunc_call()'.\n- Local variables and loop counters introduced by the patch are explicitly initialized before evaluation.\n- Any potential bounds issues regarding argument slot array indexing ('size[MAX_BPF_FUNC_ARGS]') are bounds-checked in code or would represent out-of-bounds accesses detectable by KASAN, not uninitialized memory reads.\n- Furthermore, BPF execution runtime stack memory is either explicitly unpoisoned for KMSAN in the interpreter or run as JITted instructions without KMSAN compiler instrumentation.\n\nTherefore, this patch series does not introduce or expose uninitialized memory reads or kernel info-leaks, and running a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series updates the BPF verifier's handling of kfunc arguments tagged with '__uninit' and raw memory arguments across helper and kfunc calls. Specifically, it expands 'struct arg_raw_mem_desc' in 'struct bpf_call_arg_meta' from tracking a single argument register and size to a bitmask and per-slot size array ('mask' and 'size[MAX_BPF_FUNC_ARGS]'), updates stack range checks and access types during verifier analysis, and adds 'mark_raw_stack()' to check and mark stack slots as written for all raw memory arguments. It also updates BPF selftests and documentation.\n\nKMSAN vs. KASAN applicability:\n- The changes are strictly within the BPF verifier's abstract analysis logic ('kernel/bpf/verifier.c', 'include/linux/bpf_verifier.h') and test code ('tools/testing/selftests/').\n- No kernel heap or stack buffers copied to userspace (via copy_to_user, put_user, netlink, ioctls, etc.) are introduced or altered.\n- All verifier metadata structures, including 'struct bpf_call_arg_meta', are fully zero-initialized (via memset) before use in 'check_helper_call()' and 'check_kfunc_call()'.\n- Local variables and loop counters introduced by the patch are explicitly initialized before evaluation.\n- Any potential bounds issues regarding argument slot array indexing ('size[MAX_BPF_FUNC_ARGS]') are bounds-checked in code or would represent out-of-bounds accesses detectable by KASAN, not uninitialized memory reads.\n- Furthermore, BPF execution runtime stack memory is either explicitly unpoisoned for KMSAN in the interpreter or run as JITted instructions without KMSAN compiler instrumentation.\n\nTherefore, this patch series does not introduce or expose uninitialized memory reads or kernel info-leaks, and running a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|