AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/09 22:49 flow patch-triage
3m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "mark_frame_scoped_arg",
    "release_frame_references",
    "acquire_frame_reference",
    "array_map_set_for_each_callback_args",
    "set_user_ringbuf_callback_state",
    "map_set_for_each_callback_args",
    "invalidate_reference"
  ],
  "KMSANReasoning": "The patch enhances the BPF verifier's lifetime tracking by introducing frame-scoped reference states (REF_TYPE_FRAME) to prevent BPF callback arguments (such as array map keys and dynptrs) from outliving the callee execution frame.\n\nFrom a memory safety and sanitizer perspective:\n1. Kernel-to-user info leaks: The patch touches only internal BPF verifier tracking state and diagnostics logging. No kernel structures or buffers copied to user space are introduced or modified.\n2. Uninitialized memory reads: All newly introduced or modified structures (such as `struct bpf_reference_state ref = { ... }` in the refactored `acquire_*` helpers) use C designated initializers which ensure zero-initialization of all fields and padding. Newly allocated callee frame states are zero-allocated using `kzalloc_obj()` and initialized via `init_func_state()`.\n3. Verifier logic risks: Any potential bugs introduced by these changes are limited to verifier state comparison/invalidation logic, reference counting, or boundary management, which would manifest as verifier rejections/acceptances or out-of-bounds/NULL dereferences fully covered by KASAN and standard kernel debugging facilities.\n\nBecause no uninitialized memory reads or leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch introduces frame-scoped reference tracking (REF_TYPE_FRAME) in the BPF verifier to invalidate values and derived objects (e.g., ringbuf dynptrs or array map keys passed into callbacks) upon exiting the callee frame. It adds new functions (mark_frame_scoped_arg, release_frame_references, acquire_frame_reference, array_map_set_for_each_callback_args) and new verifier invariants (verifier_bug checks on unreleased frame references and frame 0 scoping). These changes affect reachable core BPF verification logic and should be fuzzed via the bpf() syscall.",
  "WorthFuzzing": true
}

1/1 2026/10/09 22:49 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit bc6d2ef517514c1872eba74146a3baa86d9b986a\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Fri Oct 9 22:49:45 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 54144372281cb..e4498e6fa88a6 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -39,6 +39,7 @@\n #include \u003casm/rqspinlock.h\u003e\n \n struct bpf_verifier_env;\n+struct bpf_verifier_state;\n struct bpf_verifier_log;\n struct perf_event;\n struct bpf_prog;\n@@ -178,6 +179,7 @@ struct bpf_map_ops {\n \n \n \tint (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,\n+\t\t\t\t\t      struct bpf_verifier_state *state,\n \t\t\t\t\t      struct bpf_func_state *caller,\n \t\t\t\t\t      struct bpf_func_state *callee);\n \tlong (*map_for_each_callback)(struct bpf_map *map,\n@@ -3183,8 +3185,11 @@ int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,\n \t\t\t\tstruct bpf_link_info *info);\n \n int map_set_for_each_callback_args(struct bpf_verifier_env *env,\n+\t\t\t\t   struct bpf_verifier_state *state,\n \t\t\t\t   struct bpf_func_state *caller,\n \t\t\t\t   struct bpf_func_state *callee);\n+int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\n+\t\t\t  struct bpf_func_state *callee, u32 regno);\n \n int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 flags);\n int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags);\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex c51083c761cf2..54a4f440c33b3 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -272,15 +272,13 @@ struct bpf_stack_state {\n };\n \n struct bpf_reference_state {\n-\t/* Each reference object has a type. Ensure REF_TYPE_PTR is zero to\n-\t * default to pointer reference on zero initialization of a state.\n-\t */\n \tenum ref_state_type {\n \t\tREF_TYPE_PTR\t\t= (1 \u003c\u003c 1),\n \t\tREF_TYPE_IRQ\t\t= (1 \u003c\u003c 2),\n \t\tREF_TYPE_LOCK\t\t= (1 \u003c\u003c 3),\n \t\tREF_TYPE_RES_LOCK \t= (1 \u003c\u003c 4),\n \t\tREF_TYPE_RES_LOCK_IRQ\t= (1 \u003c\u003c 5),\n+\t\tREF_TYPE_FRAME\t\t= (1 \u003c\u003c 6),\n \t\tREF_TYPE_LOCK_MASK\t= REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,\n \t} type;\n \t/* Track each reference created with a unique id, even if the same\n@@ -298,6 +296,8 @@ struct bpf_reference_state {\n \t\t * it matches on unlock.\n \t\t */\n \t\tvoid *ptr;\n+\t\t/* For REF_TYPE_FRAME */\n+\t\tu32 frameno;\n \t};\n };\n \ndiff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c\nindex 0fe9afd4a591e..5e8669e311b6d 100644\n--- a/kernel/bpf/arraymap.c\n+++ b/kernel/bpf/arraymap.c\n@@ -855,6 +855,20 @@ static u64 array_map_mem_usage(const struct bpf_map *map)\n \treturn usage;\n }\n \n+static int array_map_set_for_each_callback_args(struct bpf_verifier_env *env,\n+\t\t\t\t\t\tstruct bpf_verifier_state *state,\n+\t\t\t\t\t\tstruct bpf_func_state *caller,\n+\t\t\t\t\t\tstruct bpf_func_state *callee)\n+{\n+\tint err;\n+\n+\terr = map_set_for_each_callback_args(env, state, caller, callee);\n+\tif (err)\n+\t\treturn err;\n+\n+\treturn mark_frame_scoped_arg(env, state, callee, BPF_REG_2);\n+}\n+\n BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)\n const struct bpf_map_ops array_map_ops = {\n \t.map_meta_equal = array_map_meta_equal,\n@@ -875,7 +889,7 @@ const struct bpf_map_ops array_map_ops = {\n \t.map_check_btf = array_map_check_btf,\n \t.map_lookup_batch = generic_map_lookup_batch,\n \t.map_update_batch = generic_map_update_batch,\n-\t.map_set_for_each_callback_args = map_set_for_each_callback_args,\n+\t.map_set_for_each_callback_args = array_map_set_for_each_callback_args,\n \t.map_for_each_callback = bpf_for_each_array_elem,\n \t.map_mem_usage = array_map_mem_usage,\n \t.map_btf_id = \u0026array_map_btf_ids[0],\n@@ -900,7 +914,7 @@ const struct bpf_map_ops percpu_array_map_ops = {\n \t.map_check_btf = array_map_check_btf,\n \t.map_lookup_batch = generic_map_lookup_batch,\n \t.map_update_batch = generic_map_update_batch,\n-\t.map_set_for_each_callback_args = map_set_for_each_callback_args,\n+\t.map_set_for_each_callback_args = array_map_set_for_each_callback_args,\n \t.map_for_each_callback = bpf_for_each_array_elem,\n \t.map_mem_usage = array_map_mem_usage,\n \t.map_btf_id = \u0026array_map_btf_ids[0],\ndiff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c\nindex 857b668212fb3..ba1a3f07d58e3 100644\n--- a/kernel/bpf/diagnostics.c\n+++ b/kernel/bpf/diagnostics.c\n@@ -2238,6 +2238,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_h\n \t\t\t\t\t\t\t\t   \"resource release invalidated \"\n \t\t\t\t\t\t\t\t   \"this value\";\n \t\tbreak;\n+\tcase BPF_DIAG_MOD_FRAME_RELEASE:\n+\t\treason = \"the callback that owned this value returned\";\n+\t\tbreak;\n \tcase BPF_DIAG_MOD_PKT_DATA_CHANGE:\n \t\treason = \"packet data may have moved\";\n \t\tbreak;\ndiff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h\nindex a4102fb049ece..b5cab4d79c1a2 100644\n--- a/kernel/bpf/diagnostics.h\n+++ b/kernel/bpf/diagnostics.h\n@@ -22,6 +22,7 @@ enum bpf_diag_mod_reason {\n \tBPF_DIAG_MOD_SPILL,\n \tBPF_DIAG_MOD_VAR_WRITE,\n \tBPF_DIAG_MOD_REF_RELEASE,\n+\tBPF_DIAG_MOD_FRAME_RELEASE,\n \tBPF_DIAG_MOD_PKT_DATA_CHANGE,\n \tBPF_DIAG_MOD_NON_OWN_REF,\n \tBPF_DIAG_MOD_CALLER_SAVED,\ndiff --git a/kernel/bpf/states.c b/kernel/bpf/states.c\nindex 18bf7b660c2f6..1291824a3a7dd 100644\n--- a/kernel/bpf/states.c\n+++ b/kernel/bpf/states.c\n@@ -899,6 +899,10 @@ static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c\n \t\t\tbreak;\n \t\tcase REF_TYPE_IRQ:\n \t\t\tbreak;\n+\t\tcase REF_TYPE_FRAME:\n+\t\t\tif (old-\u003erefs[i].frameno != cur-\u003erefs[i].frameno)\n+\t\t\t\treturn false;\n+\t\t\tbreak;\n \t\tcase REF_TYPE_LOCK:\n \t\tcase REF_TYPE_RES_LOCK:\n \t\tcase REF_TYPE_RES_LOCK_IRQ:\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex 353bde9ae2274..edc78e50dc22c 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -713,11 +713,12 @@ static void mark_dynptr_stack_regs(struct bpf_verifier_env *env,\n \t__mark_dynptr_reg(sreg2, type, false, id, parent_id);\n }\n \n-static void mark_dynptr_cb_reg(struct bpf_verifier_env *env,\n-\t\t\t       struct bpf_reg_state *reg,\n-\t\t\t       enum bpf_dynptr_type type)\n+static int mark_dynptr_cb_reg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\n+\t\t\t      struct bpf_func_state *callee, u32 regno,\n+\t\t\t      enum bpf_dynptr_type type)\n {\n-\t__mark_dynptr_reg(reg, type, true, ++env-\u003eid_gen, 0);\n+\t__mark_dynptr_reg(\u0026callee-\u003eregs[regno], type, true, 0, 0);\n+\treturn mark_frame_scoped_arg(env, state, callee, regno);\n }\n \n static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,\n@@ -1497,51 +1498,96 @@ static int grow_stack_arg_slots(struct bpf_verifier_env *env,\n \treturn 0;\n }\n \n-/* Acquire a pointer id from the env and update the state-\u003erefs to include\n- * this new pointer reference.\n- * On success, returns a valid pointer id to associate with the register\n- * On failure, returns a negative errno.\n+/*\n+ * Append @ref to @state-\u003erefs. Return its id, or a negative errno.\n  */\n-static struct bpf_reference_state *acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)\n+static int acquire_reference_state(struct bpf_verifier_state *state,\n+\t\t\t\t   const struct bpf_reference_state *ref)\n {\n-\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n \tint new_ofs = state-\u003eacquired_refs;\n \tint err;\n \n \terr = resize_reference_state(state, state-\u003eacquired_refs + 1);\n \tif (err)\n-\t\treturn NULL;\n-\tstate-\u003erefs[new_ofs].insn_idx = insn_idx;\n-\n-\treturn \u0026state-\u003erefs[new_ofs];\n+\t\treturn err;\n+\tstate-\u003erefs[new_ofs] = *ref;\n+\treturn ref-\u003eid;\n }\n \n static int acquire_reference(struct bpf_verifier_env *env, int insn_idx, int parent_id)\n {\n-\tstruct bpf_reference_state *s;\n+\tstruct bpf_reference_state ref = {\n+\t\t.type = REF_TYPE_PTR,\n+\t\t.id = ++env-\u003eid_gen,\n+\t\t.insn_idx = insn_idx,\n+\t\t.parent_id = parent_id,\n+\t};\n+\tint id;\n \n-\ts = acquire_reference_state(env, insn_idx);\n-\tif (!s)\n-\t\treturn -ENOMEM;\n-\ts-\u003etype = REF_TYPE_PTR;\n-\ts-\u003eid = ++env-\u003eid_gen;\n-\ts-\u003eparent_id = parent_id;\n-\tbpf_diag_record_ref_acquire(env, insn_idx, s-\u003eid);\n-\treturn s-\u003eid;\n+\tid = acquire_reference_state(env-\u003ecur_state, \u0026ref);\n+\tif (id \u003e= 0)\n+\t\tbpf_diag_record_ref_acquire(env, insn_idx, id);\n+\treturn id;\n+}\n+\n+/* Acquire a reference owned by frame @frameno of @state */\n+static int acquire_frame_reference(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\n+\t\t\t\t   int insn_idx, u32 frameno)\n+{\n+\tstruct bpf_reference_state ref = {\n+\t\t.type = REF_TYPE_FRAME,\n+\t\t.id = ++env-\u003eid_gen,\n+\t\t.insn_idx = insn_idx,\n+\t\t.frameno = frameno,\n+\t};\n+\n+\treturn acquire_reference_state(state, \u0026ref);\n+}\n+\n+/*\n+ * Anchor @regno in @callee to a reference owned by the callee frame, so the\n+ * value and everything derived from it is invalidated when the frame is\n+ * popped. @state owns @callee; for a callback it is not env-\u003ecur_state.\n+ */\n+int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\n+\t\t\t  struct bpf_func_state *callee, u32 regno)\n+{\n+\tint id;\n+\n+\tif (verifier_bug_if(!callee-\u003eframeno, env,\n+\t\t\t    \"cannot scope an argument to frame 0\"))\n+\t\treturn -EFAULT;\n+\n+\tid = acquire_frame_reference(env, state, callee-\u003ecallsite, callee-\u003eframeno);\n+\tif (id \u003c 0)\n+\t\treturn id;\n+\t/*\n+\t * The value is its own lifetime anchor: there is no associated\n+\t * object to borrow from, only the frame. parent_id = id here\n+\t * covers both possible derived references:\n+\t *  - through the id (e.g. dynptr slice)\n+\t *  - through parent_id (e.g. dynptr clone)\n+\t */\n+\tcallee-\u003eregs[regno].id = id;\n+\tcallee-\u003eregs[regno].parent_id = id;\n+\treturn 0;\n }\n \n static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum ref_state_type type,\n \t\t\t      int id, void *ptr)\n {\n \tstruct bpf_verifier_state *state = env-\u003ecur_state;\n-\tstruct bpf_reference_state *s;\n+\tstruct bpf_reference_state ref = {\n+\t\t.type = type,\n+\t\t.id = id,\n+\t\t.insn_idx = insn_idx,\n+\t\t.ptr = ptr,\n+\t};\n+\tint err;\n \n-\ts = acquire_reference_state(env, insn_idx);\n-\tif (!s)\n-\t\treturn -ENOMEM;\n-\ts-\u003etype = type;\n-\ts-\u003eid = id;\n-\ts-\u003eptr = ptr;\n+\terr = acquire_reference_state(state, \u0026ref);\n+\tif (err \u003c 0)\n+\t\treturn err;\n \n \tstate-\u003eactive_locks++;\n \tstate-\u003eactive_lock_id = id;\n@@ -1554,18 +1600,21 @@ static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum r\n static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx)\n {\n \tstruct bpf_verifier_state *state = env-\u003ecur_state;\n-\tstruct bpf_reference_state *s;\n+\tstruct bpf_reference_state ref = {\n+\t\t.type = REF_TYPE_IRQ,\n+\t\t.id = ++env-\u003eid_gen,\n+\t\t.insn_idx = insn_idx,\n+\t};\n+\tint id;\n \n-\ts = acquire_reference_state(env, insn_idx);\n-\tif (!s)\n-\t\treturn -ENOMEM;\n-\ts-\u003etype = REF_TYPE_IRQ;\n-\ts-\u003eid = ++env-\u003eid_gen;\n+\tid = acquire_reference_state(state, \u0026ref);\n+\tif (id \u003c 0)\n+\t\treturn id;\n \n-\tstate-\u003eactive_irq_id = s-\u003eid;\n+\tstate-\u003eactive_irq_id = id;\n \tbpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_IRQ, true,\n \t\t\t\tbpf_diag_irq_depth(state));\n-\treturn s-\u003eid;\n+\treturn id;\n }\n \n static void release_reference_state(struct bpf_verifier_state *state, int idx)\n@@ -10663,8 +10712,9 @@ static int idstack_pop(struct bpf_idmap *idmap)\n \treturn idmap-\u003emap[--idmap-\u003ecnt].old;\n }\n \n-/* Release id and objects derived from it iteratively in a DFS manner */\n-static int release_reference(struct bpf_verifier_env *env, int id)\n+/* Invalidate id and values derived from it iteratively in a DFS manner. */\n+static int invalidate_reference(struct bpf_verifier_env *env, int id,\n+\t\t\t\tenum bpf_diag_mod_reason reason)\n {\n \tu32 mask = (1 \u003c\u003c STACK_SPILL) | (1 \u003c\u003c STACK_DYNPTR);\n \tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n@@ -10679,11 +10729,6 @@ static int release_reference(struct bpf_verifier_env *env, int id)\n \tif (err)\n \t\treturn err;\n \n-\tif (find_reference_state(vstate, id)) {\n-\t\terr = release_reference_nomark(env, id);\n-\t\tWARN_ON_ONCE(err);\n-\t}\n-\n \twhile ((id = idstack_pop(idstack))) {\n \t\t/*\n \t\t * Child references are inaccessible after parent is released,\n@@ -10704,7 +10749,7 @@ static int release_reference(struct bpf_verifier_env *env, int id)\n \t\t\t\tcontinue;\n \n \t\t\t/* Free objects derived from the current object */\n-\t\t\tif (reg-\u003eparent_id == id) {\n+\t\t\tif (reg-\u003eparent_id == id \u0026\u0026 reg-\u003eid != id) {\n \t\t\t\terr = idstack_push(idstack, reg-\u003eid);\n \t\t\t\tif (err)\n \t\t\t\t\treturn err;\n@@ -10719,14 +10764,12 @@ static int release_reference(struct bpf_verifier_env *env, int id)\n \n \t\t\t\tif (reg-\u003edynptr.first_slot)\n \t\t\t\t\tdyn_stack--;\n-\t\t\t\tbpf_diag_record_scrub(env, \u0026dyn_stack[0].spilled_ptr,\n-\t\t\t\t\t\t      BPF_DIAG_MOD_REF_RELEASE);\n-\t\t\t\tbpf_diag_record_scrub(env, \u0026dyn_stack[1].spilled_ptr,\n-\t\t\t\t\t\t      BPF_DIAG_MOD_REF_RELEASE);\n+\t\t\t\tbpf_diag_record_scrub(env, \u0026dyn_stack[0].spilled_ptr, reason);\n+\t\t\t\tbpf_diag_record_scrub(env, \u0026dyn_stack[1].spilled_ptr, reason);\n \t\t\t\tinvalidate_dynptr(env, dyn_stack);\n \t\t\t\tcontinue;\n \t\t\t}\n-\t\t\tbpf_diag_record_scrub(env, reg, BPF_DIAG_MOD_REF_RELEASE);\n+\t\t\tbpf_diag_record_scrub(env, reg, reason);\n \t\t\tif (!stack || stack-\u003eslot_type[BPF_REG_SIZE - 1] == STACK_SPILL)\n \t\t\t\tmark_reg_invalid(env, reg);\n \t\t}));\n@@ -10735,6 +10778,38 @@ static int release_reference(struct bpf_verifier_env *env, int id)\n \treturn 0;\n }\n \n+static int release_reference(struct bpf_verifier_env *env, int id)\n+{\n+\tint err;\n+\n+\tif (find_reference_state(env-\u003ecur_state, id)) {\n+\t\terr = release_reference_nomark(env, id);\n+\t\tWARN_ON_ONCE(err);\n+\t}\n+\treturn invalidate_reference(env, id, BPF_DIAG_MOD_REF_RELEASE);\n+}\n+\n+static int release_frame_references(struct bpf_verifier_env *env, u32 frameno)\n+{\n+\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n+\tint i, id, err;\n+\n+\tfor (i = 0; i \u003c state-\u003eacquired_refs;) {\n+\t\tif (state-\u003erefs[i].type != REF_TYPE_FRAME ||\n+\t\t    state-\u003erefs[i].frameno != frameno) {\n+\t\t\ti++;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tid = state-\u003erefs[i].id;\n+\t\trelease_reference_state(state, i);\n+\t\terr = invalidate_reference(env, id, BPF_DIAG_MOD_FRAME_RELEASE);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\n+\treturn 0;\n+}\n+\n static void invalidate_non_owning_refs(struct bpf_verifier_env *env)\n {\n \tstruct bpf_func_state *unused;\n@@ -10817,36 +10892,30 @@ static void invalidate_outgoing_stack_args(struct bpf_verifier_env *env,\n }\n \n typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env,\n+\t\t\t\t   struct bpf_verifier_state *state,\n \t\t\t\t   struct bpf_func_state *caller,\n \t\t\t\t   struct bpf_func_state *callee,\n \t\t\t\t   int insn_idx);\n \n-static int set_callee_state(struct bpf_verifier_env *env,\n-\t\t\t    struct bpf_func_state *caller,\n-\t\t\t    struct bpf_func_state *callee, int insn_idx);\n-\n-static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int callsite,\n-\t\t\t    set_callee_state_fn set_callee_state_cb,\n-\t\t\t    struct bpf_verifier_state *state)\n+static struct bpf_func_state *setup_func_entry(struct bpf_verifier_env *env, int subprog,\n+\t\t\t\t\t       int callsite, struct bpf_verifier_state *state)\n {\n-\tstruct bpf_func_state *caller, *callee;\n-\tint err;\n+\tstruct bpf_func_state *callee;\n \n \tif (state-\u003ecurframe + 1 \u003e= MAX_CALL_FRAMES) {\n \t\tverbose(env, \"the call stack of %d frames is too deep\\n\",\n \t\t\tstate-\u003ecurframe + 2);\n-\t\treturn -E2BIG;\n+\t\treturn ERR_PTR(-E2BIG);\n \t}\n \n \tif (state-\u003eframe[state-\u003ecurframe + 1]) {\n \t\tverifier_bug(env, \"Frame %d already allocated\", state-\u003ecurframe + 1);\n-\t\treturn -EFAULT;\n+\t\treturn ERR_PTR(-EFAULT);\n \t}\n \n-\tcaller = state-\u003eframe[state-\u003ecurframe];\n \tcallee = kzalloc_obj(*callee, GFP_KERNEL_ACCOUNT);\n \tif (!callee)\n-\t\treturn -ENOMEM;\n+\t\treturn ERR_PTR(-ENOMEM);\n \tstate-\u003eframe[state-\u003ecurframe + 1] = callee;\n \n \t/* callee cannot access r0, r6 - r9 for reading and has to write\n@@ -10858,19 +10927,9 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int calls\n \t\t\tcallsite,\n \t\t\tstate-\u003ecurframe + 1 /* frameno within this callchain */,\n \t\t\tsubprog /* subprog number within this prog */);\n-\terr = set_callee_state_cb(env, caller, callee, callsite);\n-\tif (err)\n-\t\tgoto err_out;\n-\n-\t/* only increment it after check_reg_arg() finished */\n \tstate-\u003ecurframe++;\n \n-\treturn 0;\n-\n-err_out:\n-\tfree_func_state(callee);\n-\tstate-\u003eframe[state-\u003ecurframe + 1] = NULL;\n-\treturn err;\n+\treturn callee;\n }\n \n static void gen_subprog_arg_proto(const struct bpf_subprog_info *sub, const struct btf *btf,\n@@ -10990,10 +11049,6 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins\n \tif (err == -EFAULT)\n \t\treturn err;\n \n-\t/* set_callee_state is used for direct subprog calls, but we are\n-\t * interested in validating only BPF helpers that can call subprogs as\n-\t * callbacks\n-\t */\n \tenv-\u003esubprog_info[subprog].is_cb = true;\n \tif (bpf_pseudo_kfunc_call(insn) \u0026\u0026\n \t    !is_callback_calling_kfunc(insn-\u003eimm)) {\n@@ -11021,7 +11076,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins\n \t\tcallee-\u003easync_entry_cnt = state-\u003eframe[0]-\u003easync_entry_cnt + 1;\n \n \t\t/* Convert bpf_timer_set_callback() args into timer callback args */\n-\t\terr = set_callee_state_cb(env, caller, callee, insn_idx);\n+\t\terr = set_callee_state_cb(env, async_cb, caller, callee, insn_idx);\n \t\tif (err)\n \t\t\treturn err;\n \n@@ -11044,8 +11099,11 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins\n \tif (IS_ERR(callback_state))\n \t\treturn PTR_ERR(callback_state);\n \n-\terr = setup_func_entry(env, subprog, insn_idx, set_callee_state_cb,\n-\t\t\t       callback_state);\n+\tcallee = setup_func_entry(env, subprog, insn_idx, callback_state);\n+\tif (IS_ERR(callee))\n+\t\treturn PTR_ERR(callee);\n+\n+\terr = set_callee_state_cb(env, callback_state, caller, callee, insn_idx);\n \tif (err)\n \t\treturn err;\n \n@@ -11069,8 +11127,8 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,\n \tstruct bpf_verifier_state *state = env-\u003ecur_state;\n \tstruct bpf_subprog_info *caller_info;\n \tu16 callee_incoming, stack_arg_cnt;\n-\tstruct bpf_func_state *caller;\n-\tint err;\n+\tstruct bpf_func_state *caller, *callee;\n+\tint i;\n \n \tcaller = state-\u003eframe[state-\u003ecurframe];\n \n@@ -11088,9 +11146,12 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,\n \t * For regular function entry setup new frame and continue\n \t * from that frame.\n \t */\n-\terr = setup_func_entry(env, subprog, *insn_idx, set_callee_state, state);\n-\tif (err)\n-\t\treturn err;\n+\tcallee = setup_func_entry(env, subprog, *insn_idx, state);\n+\tif (IS_ERR(callee))\n+\t\treturn PTR_ERR(callee);\n+\n+\tfor (i = BPF_REG_1; i \u003c= BPF_REG_5; i++)\n+\t\tcallee-\u003eregs[i] = caller-\u003eregs[i];\n \n \tbpf_diag_record_scrub(env, \u0026caller-\u003eregs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED);\n \tclear_caller_saved_regs(env, caller-\u003eregs);\n@@ -11271,7 +11332,18 @@ static int check_func_callx(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \treturn check_static_func_call(env, subprog, insn_idx);\n }\n \n+static void mark_map_callback_reg(struct bpf_reg_state *reg,\n+\t\t\t\t  const struct bpf_reg_state *map_reg,\n+\t\t\t\t  enum bpf_reg_type type)\n+{\n+\treg-\u003etype = type;\n+\t__mark_reg_known_zero(reg);\n+\treg-\u003emap_ptr = map_reg-\u003emap_ptr;\n+\treg-\u003emap_uid = map_reg-\u003emap_uid;\n+}\n+\n int map_set_for_each_callback_args(struct bpf_verifier_env *env,\n+\t\t\t\t   struct bpf_verifier_state *state,\n \t\t\t\t   struct bpf_func_state *caller,\n \t\t\t\t   struct bpf_func_state *callee)\n {\n@@ -11282,40 +11354,17 @@ int map_set_for_each_callback_args(struct bpf_verifier_env *env,\n \t */\n \tcallee-\u003eregs[BPF_REG_1] = caller-\u003eregs[BPF_REG_1];\n \n-\tcallee-\u003eregs[BPF_REG_2].type = PTR_TO_MAP_KEY;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_2]);\n-\tcallee-\u003eregs[BPF_REG_2].map_ptr = caller-\u003eregs[BPF_REG_1].map_ptr;\n-\tcallee-\u003eregs[BPF_REG_2].map_uid = caller-\u003eregs[BPF_REG_1].map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_3].type = PTR_TO_MAP_VALUE;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_3]);\n-\tcallee-\u003eregs[BPF_REG_3].map_ptr = caller-\u003eregs[BPF_REG_1].map_ptr;\n-\tcallee-\u003eregs[BPF_REG_3].map_uid = caller-\u003eregs[BPF_REG_1].map_uid;\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_2], \u0026caller-\u003eregs[BPF_REG_1], PTR_TO_MAP_KEY);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_3], \u0026caller-\u003eregs[BPF_REG_1], PTR_TO_MAP_VALUE);\n \tcallee-\u003eregs[BPF_REG_3].id = ++env-\u003eid_gen;\n \n \t/* pointer to stack or null */\n \tcallee-\u003eregs[BPF_REG_4] = caller-\u003eregs[BPF_REG_3];\n-\n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n-\treturn 0;\n-}\n-\n-static int set_callee_state(struct bpf_verifier_env *env,\n-\t\t\t    struct bpf_func_state *caller,\n-\t\t\t    struct bpf_func_state *callee, int insn_idx)\n-{\n-\tint i;\n-\n-\t/* copy r1 - r5 args that callee can access.  The copy includes parent\n-\t * pointers, which connects us up to the liveness chain\n-\t */\n-\tfor (i = BPF_REG_1; i \u003c= BPF_REG_5; i++)\n-\t\tcallee-\u003eregs[i] = caller-\u003eregs[i];\n \treturn 0;\n }\n \n static int set_map_elem_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t       struct bpf_verifier_state *state,\n \t\t\t\t       struct bpf_func_state *caller,\n \t\t\t\t       struct bpf_func_state *callee,\n \t\t\t\t       int insn_idx)\n@@ -11332,7 +11381,7 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,\n \t\treturn -ENOTSUPP;\n \t}\n \n-\terr = map-\u003eops-\u003emap_set_for_each_callback_args(env, caller, callee);\n+\terr = map-\u003eops-\u003emap_set_for_each_callback_args(env, state, caller, callee);\n \tif (err)\n \t\treturn err;\n \n@@ -11342,6 +11391,7 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,\n }\n \n static int set_loop_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t   struct bpf_verifier_state *state,\n \t\t\t\t   struct bpf_func_state *caller,\n \t\t\t\t   struct bpf_func_state *callee,\n \t\t\t\t   int insn_idx)\n@@ -11353,52 +11403,31 @@ static int set_loop_callback_state(struct bpf_verifier_env *env,\n \tcallee-\u003eregs[BPF_REG_1].type = SCALAR_VALUE;\n \tcallee-\u003eregs[BPF_REG_2] = caller-\u003eregs[BPF_REG_3];\n \n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_3]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n-\n \tcallee-\u003ein_callback_fn = true;\n \tcallee-\u003ecallback_ret_range = retval_range(0, 1);\n \treturn 0;\n }\n \n static int set_timer_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t    struct bpf_verifier_state *state,\n \t\t\t\t    struct bpf_func_state *caller,\n \t\t\t\t    struct bpf_func_state *callee,\n \t\t\t\t    int insn_idx)\n {\n-\tstruct bpf_map *map_ptr = caller-\u003eregs[BPF_REG_1].map_ptr;\n-\tu32 map_uid = caller-\u003eregs[BPF_REG_1].map_uid;\n-\n \t/* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn);\n \t * callback_fn(struct bpf_map *map, void *key, void *value);\n \t */\n-\tcallee-\u003eregs[BPF_REG_1].type = CONST_PTR_TO_MAP;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_1]);\n-\tcallee-\u003eregs[BPF_REG_1].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_1].map_uid = map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_2].type = PTR_TO_MAP_KEY;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_2]);\n-\tcallee-\u003eregs[BPF_REG_2].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_2].map_uid = map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_3].type = PTR_TO_MAP_VALUE;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_3]);\n-\tcallee-\u003eregs[BPF_REG_3].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_3].map_uid = map_uid;\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_1], \u0026caller-\u003eregs[BPF_REG_1], CONST_PTR_TO_MAP);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_2], \u0026caller-\u003eregs[BPF_REG_1], PTR_TO_MAP_KEY);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_3], \u0026caller-\u003eregs[BPF_REG_1], PTR_TO_MAP_VALUE);\n \tcallee-\u003eregs[BPF_REG_3].id = ++env-\u003eid_gen;\n \n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n \tcallee-\u003ein_async_callback_fn = true;\n-\tcallee-\u003ecallback_ret_range = retval_range(0, 0);\n \treturn 0;\n }\n \n static int set_find_vma_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t       struct bpf_verifier_state *state,\n \t\t\t\t       struct bpf_func_state *caller,\n \t\t\t\t       struct bpf_func_state *callee,\n \t\t\t\t       int insn_idx)\n@@ -11417,39 +11446,35 @@ static int set_find_vma_callback_state(struct bpf_verifier_env *env,\n \n \t/* pointer to stack or null */\n \tcallee-\u003eregs[BPF_REG_3] = caller-\u003eregs[BPF_REG_4];\n-\n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n \tcallee-\u003ein_callback_fn = true;\n \tcallee-\u003ecallback_ret_range = retval_range(0, 1);\n \treturn 0;\n }\n \n static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t\t   struct bpf_verifier_state *state,\n \t\t\t\t\t   struct bpf_func_state *caller,\n \t\t\t\t\t   struct bpf_func_state *callee,\n \t\t\t\t\t   int insn_idx)\n {\n+\tint err;\n+\n \t/* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void\n \t *\t\t\t  callback_ctx, u64 flags);\n \t * callback_fn(const struct bpf_dynptr_t* dynptr, void *callback_ctx);\n \t */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_0]);\n-\tmark_dynptr_cb_reg(env, \u0026callee-\u003eregs[BPF_REG_1], BPF_DYNPTR_TYPE_LOCAL);\n+\terr = mark_dynptr_cb_reg(env, state, callee, BPF_REG_1, BPF_DYNPTR_TYPE_LOCAL);\n+\tif (err)\n+\t\treturn err;\n \tcallee-\u003eregs[BPF_REG_2] = caller-\u003eregs[BPF_REG_3];\n \n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_3]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n-\n \tcallee-\u003ein_callback_fn = true;\n \tcallee-\u003ecallback_ret_range = retval_range(0, 1);\n \treturn 0;\n }\n \n static int set_rbtree_add_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t\t struct bpf_verifier_state *state,\n \t\t\t\t\t struct bpf_func_state *caller,\n \t\t\t\t\t struct bpf_func_state *callee,\n \t\t\t\t\t int insn_idx)\n@@ -11474,78 +11499,43 @@ static int set_rbtree_add_callback_state(struct bpf_verifier_env *env,\n \tmark_reg_graph_node(callee-\u003eregs, BPF_REG_2, \u0026field-\u003egraph_root);\n \tref_set_non_owning(env, \u0026callee-\u003eregs[BPF_REG_2]);\n \n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_3]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n \tcallee-\u003ein_callback_fn = true;\n \tcallee-\u003ecallback_ret_range = retval_range(0, 1);\n \treturn 0;\n }\n \n static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t\t\t struct bpf_verifier_state *state,\n \t\t\t\t\t\t struct bpf_func_state *caller,\n \t\t\t\t\t\t struct bpf_func_state *callee,\n \t\t\t\t\t\t int insn_idx)\n {\n-\tstruct bpf_map *map_ptr = caller-\u003eregs[BPF_REG_3].map_ptr;\n-\tu32 map_uid = caller-\u003eregs[BPF_REG_3].map_uid;\n-\n \t/*\n \t * callback_fn(struct bpf_map *map, void *key, void *value);\n \t */\n-\tcallee-\u003eregs[BPF_REG_1].type = CONST_PTR_TO_MAP;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_1]);\n-\tcallee-\u003eregs[BPF_REG_1].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_1].map_uid = map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_2].type = PTR_TO_MAP_KEY;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_2]);\n-\tcallee-\u003eregs[BPF_REG_2].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_2].map_uid = map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_3].type = PTR_TO_MAP_VALUE;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_3]);\n-\tcallee-\u003eregs[BPF_REG_3].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_3].map_uid = map_uid;\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_1], \u0026caller-\u003eregs[BPF_REG_3], CONST_PTR_TO_MAP);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_2], \u0026caller-\u003eregs[BPF_REG_3], PTR_TO_MAP_KEY);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_3], \u0026caller-\u003eregs[BPF_REG_3], PTR_TO_MAP_VALUE);\n \tcallee-\u003eregs[BPF_REG_3].id = ++env-\u003eid_gen;\n \n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n \tcallee-\u003ein_async_callback_fn = true;\n \tcallee-\u003ecallback_ret_range = retval_range(S32_MIN, S32_MAX);\n \treturn 0;\n }\n \n static int set_rcu_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t  struct bpf_verifier_state *state,\n \t\t\t\t  struct bpf_func_state *caller,\n \t\t\t\t  struct bpf_func_state *callee,\n \t\t\t\t  int insn_idx)\n {\n-\tstruct bpf_map *map_ptr = caller-\u003eregs[BPF_REG_2].map_ptr;\n-\tu32 map_uid = caller-\u003eregs[BPF_REG_2].map_uid;\n-\n \t/*\n \t * callback_fn(struct bpf_map *map, void *key, void *value);\n \t */\n-\tcallee-\u003eregs[BPF_REG_1].type = CONST_PTR_TO_MAP;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_1]);\n-\tcallee-\u003eregs[BPF_REG_1].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_1].map_uid = map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_2].type = PTR_TO_MAP_KEY;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_2]);\n-\tcallee-\u003eregs[BPF_REG_2].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_2].map_uid = map_uid;\n-\n-\tcallee-\u003eregs[BPF_REG_3].type = PTR_TO_MAP_VALUE;\n-\t__mark_reg_known_zero(\u0026callee-\u003eregs[BPF_REG_3]);\n-\tcallee-\u003eregs[BPF_REG_3].map_ptr = map_ptr;\n-\tcallee-\u003eregs[BPF_REG_3].map_uid = map_uid;\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_1], \u0026caller-\u003eregs[BPF_REG_2], CONST_PTR_TO_MAP);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_2], \u0026caller-\u003eregs[BPF_REG_2], PTR_TO_MAP_KEY);\n+\tmark_map_callback_reg(\u0026callee-\u003eregs[BPF_REG_3], \u0026caller-\u003eregs[BPF_REG_2], PTR_TO_MAP_VALUE);\n \n-\t/* unused */\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n-\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n \tcallee-\u003ein_async_callback_fn = true;\n \tcallee-\u003ecallback_ret_range = retval_range(S32_MIN, S32_MAX);\n \treturn 0;\n@@ -11708,6 +11698,15 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)\n \t\tverbose(env, \"to caller at %d:\\n\", *insn_idx);\n \t\tprint_verifier_state(env, state, caller-\u003eframeno, true);\n \t}\n+\n+\t/*\n+\t * Values the caller only guaranteed for the duration of the call stop\n+\t * being valid here.\n+\t */\n+\terr = release_frame_references(env, callee-\u003eframeno);\n+\tif (err)\n+\t\treturn err;\n+\n \taccount_processed_insns(env, callee, caller);\n \t/* clear everything in the callee. In case of exceptional exits using\n \t * bpf_throw, this will be done by copy_verifier_state for extra frames. */\n@@ -11885,6 +11884,11 @@ static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exi\n \t\treturn 0;\n \n \tfor (i = 0; i \u003c state-\u003eacquired_refs; i++) {\n+\t\tif (!exception_exit \u0026\u0026 state-\u003erefs[i].type == REF_TYPE_FRAME) {\n+\t\t\tverifier_bug(env, \"frame %u reference id=%d alive at program exit\",\n+\t\t\t\t     state-\u003erefs[i].frameno, state-\u003erefs[i].id);\n+\t\t\treturn -EFAULT;\n+\t\t}\n \t\tif (state-\u003erefs[i].type != REF_TYPE_PTR)\n \t\t\tcontinue;\n \t\t/* Allow struct_ops programs to return a referenced kptr back to\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_fail.c b/tools/testing/selftests/bpf/progs/exceptions_fail.c\nindex 22503cf62e9f3..86a0667ba3484 100644\n--- a/tools/testing/selftests/bpf/progs/exceptions_fail.c\n+++ b/tools/testing/selftests/bpf/progs/exceptions_fail.c\n@@ -31,6 +31,11 @@ struct {\n \t__type(value, struct hmap_elem);\n } hmap SEC(\".maps\");\n \n+struct {\n+\t__uint(type, BPF_MAP_TYPE_USER_RINGBUF);\n+\t__uint(max_entries, 4096);\n+} user_ringbuf SEC(\".maps\");\n+\n private(A) struct bpf_spin_lock lock;\n private(A) struct bpf_rb_root rbtree __contains(foo, node);\n \n@@ -110,6 +115,21 @@ static int timer_cb(void *map, int *key, struct bpf_timer *timer)\n \treturn 0;\n }\n \n+static long drain_cb(struct bpf_dynptr *dynptr, void *context)\n+{\n+\tbpf_throw(0);\n+\treturn 0;\n+}\n+\n+SEC(\"?tc\")\n+__failure\n+__msg(\"bpf_throw kfunc (insn {{[0-9]+}}) cannot be called from callback subprog {{[0-9]+}}\")\n+int reject_user_ringbuf_callback_throw(struct __sk_buff *ctx)\n+{\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, drain_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n SEC(\"?tc\")\n __failure __msg(\"cannot be called from callback subprog\")\n int reject_async_callback_throw(struct __sk_buff *ctx)\ndiff --git a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c\nindex c0d0422b8030b..4e0f29c2d1f25 100644\n--- a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c\n+++ b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c\n@@ -1,9 +1,11 @@\n // SPDX-License-Identifier: GPL-2.0\n /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */\n \n+#include \u003cstdbool.h\u003e\n #include \u003clinux/bpf.h\u003e\n #include \u003cbpf/bpf_helpers.h\u003e\n #include \"bpf_misc.h\"\n+#include \"bpf_kfuncs.h\"\n \n char _license[] SEC(\"license\") = \"GPL\";\n \n@@ -243,3 +245,136 @@ int user_ringbuf_callback_const_ptr_to_dynptr_reg_off(void *ctx)\n \t\t\t       callback_adjust_bpf_dynptr_reg_off, NULL, 0);\n \treturn 0;\n }\n+\n+/* The sample goes back to the producer as soon as the callback returns. */\n+struct dynptr_ctx {\n+\tstruct bpf_dynptr *saved;\n+};\n+\n+static long callback_park_dynptr(struct bpf_dynptr *dynptr, void *context)\n+{\n+\tstruct dynptr_ctx *c = context;\n+\n+\tc-\u003esaved = dynptr;\n+\treturn 0;\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"the callback that owned this value returned\")\n+int user_ringbuf_callback_park_dynptr(void *ctx)\n+{\n+\tstruct dynptr_ctx c = {};\n+\tchar buf[8] = {};\n+\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_dynptr, \u0026c, 0);\n+\tif (c.saved)\n+\t\tbpf_dynptr_read(buf, sizeof(buf), c.saved, 0, 0);\n+\treturn buf[0];\n+}\n+\n+struct slice_ctx {\n+\tchar *p;\n+};\n+\n+static long callback_park_data_slice(struct bpf_dynptr *dynptr, void *context)\n+{\n+\tstruct slice_ctx *c = context;\n+\n+\tc-\u003ep = bpf_dynptr_data(dynptr, 0, 8);\n+\treturn 0;\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"the callback that owned this value returned\")\n+int user_ringbuf_callback_park_data_slice(void *ctx)\n+{\n+\tstruct slice_ctx c = {};\n+\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_data_slice, \u0026c, 0);\n+\tif (c.p)\n+\t\treturn c.p[0];\n+\treturn 0;\n+}\n+\n+static long callback_park_kfunc_slice(struct bpf_dynptr *dynptr, void *context)\n+{\n+\tstruct slice_ctx *c = context;\n+\n+\tc-\u003ep = bpf_dynptr_slice(dynptr, 0, NULL, 8);\n+\treturn 0;\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"the callback that owned this value returned\")\n+int user_ringbuf_callback_park_kfunc_slice(void *ctx)\n+{\n+\tstruct slice_ctx c = {};\n+\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_kfunc_slice, \u0026c, 0);\n+\tif (c.p)\n+\t\treturn c.p[0];\n+\treturn 0;\n+}\n+\n+struct clone_ctx {\n+\tstruct bpf_dynptr clone;\n+\t__u64 armed;\n+};\n+\n+static long callback_park_clone(struct bpf_dynptr *dynptr, void *context)\n+{\n+\tstruct clone_ctx *c = context;\n+\n+\tbpf_dynptr_clone(dynptr, \u0026c-\u003eclone);\n+\tc-\u003earmed = 1;\n+\treturn 0;\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"Expected an initialized dynptr as R3\")\n+int user_ringbuf_callback_park_clone(void *ctx)\n+{\n+\tstruct clone_ctx c = {};\n+\tchar buf[8] = {};\n+\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_clone, \u0026c, 0);\n+\tif (c.armed)\n+\t\tbpf_dynptr_read(buf, sizeof(buf), \u0026c.clone, 0, 0);\n+\treturn buf[0];\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"Expected an initialized dynptr as R1\")\n+int user_ringbuf_callback_park_clone_then_slice(void *ctx)\n+{\n+\tstruct clone_ctx c = {};\n+\tchar *p;\n+\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_clone, \u0026c, 0);\n+\tif (c.armed) {\n+\t\tp = bpf_dynptr_data(\u0026c.clone, 0, 8);\n+\t\tif (p)\n+\t\t\treturn p[0];\n+\t}\n+\treturn 0;\n+}\n+\n+/* An inner drain's dynptr must not escape into the outer callback either. */\n+static long callback_park_outer(struct bpf_dynptr *dynptr, void *context)\n+{\n+\tstruct dynptr_ctx inner = {};\n+\tchar buf[8] = {};\n+\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_dynptr, \u0026inner, 0);\n+\tif (inner.saved)\n+\t\tbpf_dynptr_read(buf, sizeof(buf), inner.saved, 0, 0);\n+\treturn buf[0] ? 1 : 0;\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"the callback that owned this value returned\")\n+int user_ringbuf_callback_nested_park_inner(void *ctx)\n+{\n+\tbpf_user_ringbuf_drain(\u0026user_ringbuf, callback_park_outer, NULL, 0);\n+\treturn 0;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c\nindex 1fbcc52283061..2e0c56888953b 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c\n@@ -9,6 +9,20 @@ struct {\n \t__type(value, __u64);\n } map SEC(\".maps\");\n \n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);\n+\t__uint(max_entries, 8);\n+\t__type(key, __u32);\n+\t__type(value, __u64);\n+} percpu_map SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_HASH);\n+\t__uint(max_entries, 8);\n+\t__type(key, __u32);\n+\t__type(value, __u64);\n+} hash_map SEC(\".maps\");\n+\n struct {\n \t__uint(type, BPF_MAP_TYPE_USER_RINGBUF);\n \t__uint(max_entries, 8);\n@@ -800,4 +814,81 @@ __naked void check_add_const_regsafe_off(void)\n \t: __clobber_common);\n }\n \n+struct key_ctx {\n+\t__u32 *key;\n+};\n+\n+static long park_key_cb(struct bpf_map *map, __u32 *key, __u64 *value,\n+\t\t\tvoid *context)\n+{\n+\tstruct key_ctx *c = context;\n+\n+\tc-\u003ekey = key;\n+\treturn 0;\n+}\n+\n+/* bpf_for_each_array_elem() passes a key from its own stack frame. */\n+SEC(\"?raw_tp\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int array_park_map_key(void *ctx)\n+{\n+\tstruct key_ctx c = {};\n+\n+\tbpf_for_each_map_elem(\u0026map, park_key_cb, \u0026c, 0);\n+\tif (c.key)\n+\t\treturn *c.key;\n+\treturn 0;\n+}\n+\n+SEC(\"?raw_tp\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int percpu_array_park_map_key(void *ctx)\n+{\n+\tstruct key_ctx c = {};\n+\n+\tbpf_for_each_map_elem(\u0026percpu_map, park_key_cb, \u0026c, 0);\n+\tif (c.key)\n+\t\treturn *c.key;\n+\treturn 0;\n+}\n+\n+/* A hash key points into the element, which outlives the callback. */\n+SEC(\"?raw_tp\")\n+__success\n+int hash_park_map_key(void *ctx)\n+{\n+\tstruct key_ctx c = {};\n+\n+\tbpf_for_each_map_elem(\u0026hash_map, park_key_cb, \u0026c, 0);\n+\tif (c.key)\n+\t\treturn *c.key;\n+\treturn 0;\n+}\n+\n+struct value_ctx {\n+\t__u64 *value;\n+};\n+\n+static long park_value_cb(struct bpf_map *map, __u32 *key, __u64 *value,\n+\t\t\t  void *context)\n+{\n+\tstruct value_ctx *c = context;\n+\n+\tc-\u003evalue = value;\n+\treturn 0;\n+}\n+\n+/* Only the key is frame-scoped; the element lives until map teardown. */\n+SEC(\"?raw_tp\")\n+__success\n+int array_park_map_value(void *ctx)\n+{\n+\tstruct value_ctx c = {};\n+\n+\tbpf_for_each_map_elem(\u0026map, park_value_cb, \u0026c, 0);\n+\tif (c.value)\n+\t\treturn *c.value;\n+\treturn 0;\n+}\n+\n char _license[] SEC(\"license\") = \"GPL\";\n"
}

2/1 2026/10/09 22:49 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "mark_frame_scoped_arg",
    "release_frame_references",
    "acquire_frame_reference",
    "array_map_set_for_each_callback_args",
    "set_user_ringbuf_callback_state",
    "map_set_for_each_callback_args",
    "invalidate_reference"
  ],
  "Reasoning": "The patch introduces frame-scoped reference tracking (REF_TYPE_FRAME) in the BPF verifier to invalidate values and derived objects (e.g., ringbuf dynptrs or array map keys passed into callbacks) upon exiting the callee frame. It adds new functions (mark_frame_scoped_arg, release_frame_references, acquire_frame_reference, array_map_set_for_each_callback_args) and new verifier invariants (verifier_bug checks on unreleased frame references and frame 0 scoping). These changes affect reachable core BPF verification logic and should be fuzzed via the bpf() syscall.",
  "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 bc6d2ef517514c1872eba74146a3baa86d9b986a
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Oct 9 22:49:45 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 54144372281cb..e4498e6fa88a6 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -39,6 +39,7 @@
 #include <asm/rqspinlock.h>
 
 struct bpf_verifier_env;
+struct bpf_verifier_state;
 struct bpf_verifier_log;
 struct perf_event;
 struct bpf_prog;
@@ -178,6 +179,7 @@ struct bpf_map_ops {
 
 
 	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
+					      struct bpf_verifier_state *state,
 					      struct bpf_func_state *caller,
 					      struct bpf_func_state *callee);
 	long (*map_for_each_callback)(struct bpf_map *map,
@@ -3183,8 +3185,11 @@ int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
 				struct bpf_link_info *info);
 
 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee);
+int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+			  struct bpf_func_state *callee, u32 regno);
 
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 flags);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags);
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index c51083c761cf2..54a4f440c33b3 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -272,15 +272,13 @@ struct bpf_stack_state {
 };
 
 struct bpf_reference_state {
-	/* Each reference object has a type. Ensure REF_TYPE_PTR is zero to
-	 * default to pointer reference on zero initialization of a state.
-	 */
 	enum ref_state_type {
 		REF_TYPE_PTR		= (1 << 1),
 		REF_TYPE_IRQ		= (1 << 2),
 		REF_TYPE_LOCK		= (1 << 3),
 		REF_TYPE_RES_LOCK 	= (1 << 4),
 		REF_TYPE_RES_LOCK_IRQ	= (1 << 5),
+		REF_TYPE_FRAME		= (1 << 6),
 		REF_TYPE_LOCK_MASK	= REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,
 	} type;
 	/* Track each reference created with a unique id, even if the same
@@ -298,6 +296,8 @@ struct bpf_reference_state {
 		 * it matches on unlock.
 		 */
 		void *ptr;
+		/* For REF_TYPE_FRAME */
+		u32 frameno;
 	};
 };
 
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 0fe9afd4a591e..5e8669e311b6d 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -855,6 +855,20 @@ static u64 array_map_mem_usage(const struct bpf_map *map)
 	return usage;
 }
 
+static int array_map_set_for_each_callback_args(struct bpf_verifier_env *env,
+						struct bpf_verifier_state *state,
+						struct bpf_func_state *caller,
+						struct bpf_func_state *callee)
+{
+	int err;
+
+	err = map_set_for_each_callback_args(env, state, caller, callee);
+	if (err)
+		return err;
+
+	return mark_frame_scoped_arg(env, state, callee, BPF_REG_2);
+}
+
 BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
 const struct bpf_map_ops array_map_ops = {
 	.map_meta_equal = array_map_meta_equal,
@@ -875,7 +889,7 @@ const struct bpf_map_ops array_map_ops = {
 	.map_check_btf = array_map_check_btf,
 	.map_lookup_batch = generic_map_lookup_batch,
 	.map_update_batch = generic_map_update_batch,
-	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_set_for_each_callback_args = array_map_set_for_each_callback_args,
 	.map_for_each_callback = bpf_for_each_array_elem,
 	.map_mem_usage = array_map_mem_usage,
 	.map_btf_id = &array_map_btf_ids[0],
@@ -900,7 +914,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
 	.map_check_btf = array_map_check_btf,
 	.map_lookup_batch = generic_map_lookup_batch,
 	.map_update_batch = generic_map_update_batch,
-	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_set_for_each_callback_args = array_map_set_for_each_callback_args,
 	.map_for_each_callback = bpf_for_each_array_elem,
 	.map_mem_usage = array_map_mem_usage,
 	.map_btf_id = &array_map_btf_ids[0],
diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c
index 857b668212fb3..ba1a3f07d58e3 100644
--- a/kernel/bpf/diagnostics.c
+++ b/kernel/bpf/diagnostics.c
@@ -2238,6 +2238,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_h
 								   "resource release invalidated "
 								   "this value";
 		break;
+	case BPF_DIAG_MOD_FRAME_RELEASE:
+		reason = "the callback that owned this value returned";
+		break;
 	case BPF_DIAG_MOD_PKT_DATA_CHANGE:
 		reason = "packet data may have moved";
 		break;
diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h
index a4102fb049ece..b5cab4d79c1a2 100644
--- a/kernel/bpf/diagnostics.h
+++ b/kernel/bpf/diagnostics.h
@@ -22,6 +22,7 @@ enum bpf_diag_mod_reason {
 	BPF_DIAG_MOD_SPILL,
 	BPF_DIAG_MOD_VAR_WRITE,
 	BPF_DIAG_MOD_REF_RELEASE,
+	BPF_DIAG_MOD_FRAME_RELEASE,
 	BPF_DIAG_MOD_PKT_DATA_CHANGE,
 	BPF_DIAG_MOD_NON_OWN_REF,
 	BPF_DIAG_MOD_CALLER_SAVED,
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 18bf7b660c2f6..1291824a3a7dd 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -899,6 +899,10 @@ static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c
 			break;
 		case REF_TYPE_IRQ:
 			break;
+		case REF_TYPE_FRAME:
+			if (old->refs[i].frameno != cur->refs[i].frameno)
+				return false;
+			break;
 		case REF_TYPE_LOCK:
 		case REF_TYPE_RES_LOCK:
 		case REF_TYPE_RES_LOCK_IRQ:
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 353bde9ae2274..edc78e50dc22c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -713,11 +713,12 @@ static void mark_dynptr_stack_regs(struct bpf_verifier_env *env,
 	__mark_dynptr_reg(sreg2, type, false, id, parent_id);
 }
 
-static void mark_dynptr_cb_reg(struct bpf_verifier_env *env,
-			       struct bpf_reg_state *reg,
-			       enum bpf_dynptr_type type)
+static int mark_dynptr_cb_reg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+			      struct bpf_func_state *callee, u32 regno,
+			      enum bpf_dynptr_type type)
 {
-	__mark_dynptr_reg(reg, type, true, ++env->id_gen, 0);
+	__mark_dynptr_reg(&callee->regs[regno], type, true, 0, 0);
+	return mark_frame_scoped_arg(env, state, callee, regno);
 }
 
 static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
@@ -1497,51 +1498,96 @@ static int grow_stack_arg_slots(struct bpf_verifier_env *env,
 	return 0;
 }
 
-/* Acquire a pointer id from the env and update the state->refs to include
- * this new pointer reference.
- * On success, returns a valid pointer id to associate with the register
- * On failure, returns a negative errno.
+/*
+ * Append @ref to @state->refs. Return its id, or a negative errno.
  */
-static struct bpf_reference_state *acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
+static int acquire_reference_state(struct bpf_verifier_state *state,
+				   const struct bpf_reference_state *ref)
 {
-	struct bpf_verifier_state *state = env->cur_state;
 	int new_ofs = state->acquired_refs;
 	int err;
 
 	err = resize_reference_state(state, state->acquired_refs + 1);
 	if (err)
-		return NULL;
-	state->refs[new_ofs].insn_idx = insn_idx;
-
-	return &state->refs[new_ofs];
+		return err;
+	state->refs[new_ofs] = *ref;
+	return ref->id;
 }
 
 static int acquire_reference(struct bpf_verifier_env *env, int insn_idx, int parent_id)
 {
-	struct bpf_reference_state *s;
+	struct bpf_reference_state ref = {
+		.type = REF_TYPE_PTR,
+		.id = ++env->id_gen,
+		.insn_idx = insn_idx,
+		.parent_id = parent_id,
+	};
+	int id;
 
-	s = acquire_reference_state(env, insn_idx);
-	if (!s)
-		return -ENOMEM;
-	s->type = REF_TYPE_PTR;
-	s->id = ++env->id_gen;
-	s->parent_id = parent_id;
-	bpf_diag_record_ref_acquire(env, insn_idx, s->id);
-	return s->id;
+	id = acquire_reference_state(env->cur_state, &ref);
+	if (id >= 0)
+		bpf_diag_record_ref_acquire(env, insn_idx, id);
+	return id;
+}
+
+/* Acquire a reference owned by frame @frameno of @state */
+static int acquire_frame_reference(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+				   int insn_idx, u32 frameno)
+{
+	struct bpf_reference_state ref = {
+		.type = REF_TYPE_FRAME,
+		.id = ++env->id_gen,
+		.insn_idx = insn_idx,
+		.frameno = frameno,
+	};
+
+	return acquire_reference_state(state, &ref);
+}
+
+/*
+ * Anchor @regno in @callee to a reference owned by the callee frame, so the
+ * value and everything derived from it is invalidated when the frame is
+ * popped. @state owns @callee; for a callback it is not env->cur_state.
+ */
+int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+			  struct bpf_func_state *callee, u32 regno)
+{
+	int id;
+
+	if (verifier_bug_if(!callee->frameno, env,
+			    "cannot scope an argument to frame 0"))
+		return -EFAULT;
+
+	id = acquire_frame_reference(env, state, callee->callsite, callee->frameno);
+	if (id < 0)
+		return id;
+	/*
+	 * The value is its own lifetime anchor: there is no associated
+	 * object to borrow from, only the frame. parent_id = id here
+	 * covers both possible derived references:
+	 *  - through the id (e.g. dynptr slice)
+	 *  - through parent_id (e.g. dynptr clone)
+	 */
+	callee->regs[regno].id = id;
+	callee->regs[regno].parent_id = id;
+	return 0;
 }
 
 static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum ref_state_type type,
 			      int id, void *ptr)
 {
 	struct bpf_verifier_state *state = env->cur_state;
-	struct bpf_reference_state *s;
+	struct bpf_reference_state ref = {
+		.type = type,
+		.id = id,
+		.insn_idx = insn_idx,
+		.ptr = ptr,
+	};
+	int err;
 
-	s = acquire_reference_state(env, insn_idx);
-	if (!s)
-		return -ENOMEM;
-	s->type = type;
-	s->id = id;
-	s->ptr = ptr;
+	err = acquire_reference_state(state, &ref);
+	if (err < 0)
+		return err;
 
 	state->active_locks++;
 	state->active_lock_id = id;
@@ -1554,18 +1600,21 @@ static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum r
 static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx)
 {
 	struct bpf_verifier_state *state = env->cur_state;
-	struct bpf_reference_state *s;
+	struct bpf_reference_state ref = {
+		.type = REF_TYPE_IRQ,
+		.id = ++env->id_gen,
+		.insn_idx = insn_idx,
+	};
+	int id;
 
-	s = acquire_reference_state(env, insn_idx);
-	if (!s)
-		return -ENOMEM;
-	s->type = REF_TYPE_IRQ;
-	s->id = ++env->id_gen;
+	id = acquire_reference_state(state, &ref);
+	if (id < 0)
+		return id;
 
-	state->active_irq_id = s->id;
+	state->active_irq_id = id;
 	bpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_IRQ, true,
 				bpf_diag_irq_depth(state));
-	return s->id;
+	return id;
 }
 
 static void release_reference_state(struct bpf_verifier_state *state, int idx)
@@ -10663,8 +10712,9 @@ static int idstack_pop(struct bpf_idmap *idmap)
 	return idmap->map[--idmap->cnt].old;
 }
 
-/* Release id and objects derived from it iteratively in a DFS manner */
-static int release_reference(struct bpf_verifier_env *env, int id)
+/* Invalidate id and values derived from it iteratively in a DFS manner. */
+static int invalidate_reference(struct bpf_verifier_env *env, int id,
+				enum bpf_diag_mod_reason reason)
 {
 	u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
 	struct bpf_verifier_state *vstate = env->cur_state;
@@ -10679,11 +10729,6 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 	if (err)
 		return err;
 
-	if (find_reference_state(vstate, id)) {
-		err = release_reference_nomark(env, id);
-		WARN_ON_ONCE(err);
-	}
-
 	while ((id = idstack_pop(idstack))) {
 		/*
 		 * Child references are inaccessible after parent is released,
@@ -10704,7 +10749,7 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 				continue;
 
 			/* Free objects derived from the current object */
-			if (reg->parent_id == id) {
+			if (reg->parent_id == id && reg->id != id) {
 				err = idstack_push(idstack, reg->id);
 				if (err)
 					return err;
@@ -10719,14 +10764,12 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 
 				if (reg->dynptr.first_slot)
 					dyn_stack--;
-				bpf_diag_record_scrub(env, &dyn_stack[0].spilled_ptr,
-						      BPF_DIAG_MOD_REF_RELEASE);
-				bpf_diag_record_scrub(env, &dyn_stack[1].spilled_ptr,
-						      BPF_DIAG_MOD_REF_RELEASE);
+				bpf_diag_record_scrub(env, &dyn_stack[0].spilled_ptr, reason);
+				bpf_diag_record_scrub(env, &dyn_stack[1].spilled_ptr, reason);
 				invalidate_dynptr(env, dyn_stack);
 				continue;
 			}
-			bpf_diag_record_scrub(env, reg, BPF_DIAG_MOD_REF_RELEASE);
+			bpf_diag_record_scrub(env, reg, reason);
 			if (!stack || stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL)
 				mark_reg_invalid(env, reg);
 		}));
@@ -10735,6 +10778,38 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 	return 0;
 }
 
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+	int err;
+
+	if (find_reference_state(env->cur_state, id)) {
+		err = release_reference_nomark(env, id);
+		WARN_ON_ONCE(err);
+	}
+	return invalidate_reference(env, id, BPF_DIAG_MOD_REF_RELEASE);
+}
+
+static int release_frame_references(struct bpf_verifier_env *env, u32 frameno)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	int i, id, err;
+
+	for (i = 0; i < state->acquired_refs;) {
+		if (state->refs[i].type != REF_TYPE_FRAME ||
+		    state->refs[i].frameno != frameno) {
+			i++;
+			continue;
+		}
+		id = state->refs[i].id;
+		release_reference_state(state, i);
+		err = invalidate_reference(env, id, BPF_DIAG_MOD_FRAME_RELEASE);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static void invalidate_non_owning_refs(struct bpf_verifier_env *env)
 {
 	struct bpf_func_state *unused;
@@ -10817,36 +10892,30 @@ static void invalidate_outgoing_stack_args(struct bpf_verifier_env *env,
 }
 
 typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee,
 				   int insn_idx);
 
-static int set_callee_state(struct bpf_verifier_env *env,
-			    struct bpf_func_state *caller,
-			    struct bpf_func_state *callee, int insn_idx);
-
-static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int callsite,
-			    set_callee_state_fn set_callee_state_cb,
-			    struct bpf_verifier_state *state)
+static struct bpf_func_state *setup_func_entry(struct bpf_verifier_env *env, int subprog,
+					       int callsite, struct bpf_verifier_state *state)
 {
-	struct bpf_func_state *caller, *callee;
-	int err;
+	struct bpf_func_state *callee;
 
 	if (state->curframe + 1 >= MAX_CALL_FRAMES) {
 		verbose(env, "the call stack of %d frames is too deep\n",
 			state->curframe + 2);
-		return -E2BIG;
+		return ERR_PTR(-E2BIG);
 	}
 
 	if (state->frame[state->curframe + 1]) {
 		verifier_bug(env, "Frame %d already allocated", state->curframe + 1);
-		return -EFAULT;
+		return ERR_PTR(-EFAULT);
 	}
 
-	caller = state->frame[state->curframe];
 	callee = kzalloc_obj(*callee, GFP_KERNEL_ACCOUNT);
 	if (!callee)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 	state->frame[state->curframe + 1] = callee;
 
 	/* callee cannot access r0, r6 - r9 for reading and has to write
@@ -10858,19 +10927,9 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int calls
 			callsite,
 			state->curframe + 1 /* frameno within this callchain */,
 			subprog /* subprog number within this prog */);
-	err = set_callee_state_cb(env, caller, callee, callsite);
-	if (err)
-		goto err_out;
-
-	/* only increment it after check_reg_arg() finished */
 	state->curframe++;
 
-	return 0;
-
-err_out:
-	free_func_state(callee);
-	state->frame[state->curframe + 1] = NULL;
-	return err;
+	return callee;
 }
 
 static void gen_subprog_arg_proto(const struct bpf_subprog_info *sub, const struct btf *btf,
@@ -10990,10 +11049,6 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
 	if (err == -EFAULT)
 		return err;
 
-	/* set_callee_state is used for direct subprog calls, but we are
-	 * interested in validating only BPF helpers that can call subprogs as
-	 * callbacks
-	 */
 	env->subprog_info[subprog].is_cb = true;
 	if (bpf_pseudo_kfunc_call(insn) &&
 	    !is_callback_calling_kfunc(insn->imm)) {
@@ -11021,7 +11076,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
 		callee->async_entry_cnt = state->frame[0]->async_entry_cnt + 1;
 
 		/* Convert bpf_timer_set_callback() args into timer callback args */
-		err = set_callee_state_cb(env, caller, callee, insn_idx);
+		err = set_callee_state_cb(env, async_cb, caller, callee, insn_idx);
 		if (err)
 			return err;
 
@@ -11044,8 +11099,11 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
 	if (IS_ERR(callback_state))
 		return PTR_ERR(callback_state);
 
-	err = setup_func_entry(env, subprog, insn_idx, set_callee_state_cb,
-			       callback_state);
+	callee = setup_func_entry(env, subprog, insn_idx, callback_state);
+	if (IS_ERR(callee))
+		return PTR_ERR(callee);
+
+	err = set_callee_state_cb(env, callback_state, caller, callee, insn_idx);
 	if (err)
 		return err;
 
@@ -11069,8 +11127,8 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
 	struct bpf_verifier_state *state = env->cur_state;
 	struct bpf_subprog_info *caller_info;
 	u16 callee_incoming, stack_arg_cnt;
-	struct bpf_func_state *caller;
-	int err;
+	struct bpf_func_state *caller, *callee;
+	int i;
 
 	caller = state->frame[state->curframe];
 
@@ -11088,9 +11146,12 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
 	 * For regular function entry setup new frame and continue
 	 * from that frame.
 	 */
-	err = setup_func_entry(env, subprog, *insn_idx, set_callee_state, state);
-	if (err)
-		return err;
+	callee = setup_func_entry(env, subprog, *insn_idx, state);
+	if (IS_ERR(callee))
+		return PTR_ERR(callee);
+
+	for (i = BPF_REG_1; i <= BPF_REG_5; i++)
+		callee->regs[i] = caller->regs[i];
 
 	bpf_diag_record_scrub(env, &caller->regs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED);
 	clear_caller_saved_regs(env, caller->regs);
@@ -11271,7 +11332,18 @@ static int check_func_callx(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	return check_static_func_call(env, subprog, insn_idx);
 }
 
+static void mark_map_callback_reg(struct bpf_reg_state *reg,
+				  const struct bpf_reg_state *map_reg,
+				  enum bpf_reg_type type)
+{
+	reg->type = type;
+	__mark_reg_known_zero(reg);
+	reg->map_ptr = map_reg->map_ptr;
+	reg->map_uid = map_reg->map_uid;
+}
+
 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee)
 {
@@ -11282,40 +11354,17 @@ int map_set_for_each_callback_args(struct bpf_verifier_env *env,
 	 */
 	callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1];
 
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr;
-	callee->regs[BPF_REG_2].map_uid = caller->regs[BPF_REG_1].map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr;
-	callee->regs[BPF_REG_3].map_uid = caller->regs[BPF_REG_1].map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_1], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_1], PTR_TO_MAP_VALUE);
 	callee->regs[BPF_REG_3].id = ++env->id_gen;
 
 	/* pointer to stack or null */
 	callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3];
-
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
-	return 0;
-}
-
-static int set_callee_state(struct bpf_verifier_env *env,
-			    struct bpf_func_state *caller,
-			    struct bpf_func_state *callee, int insn_idx)
-{
-	int i;
-
-	/* copy r1 - r5 args that callee can access.  The copy includes parent
-	 * pointers, which connects us up to the liveness chain
-	 */
-	for (i = BPF_REG_1; i <= BPF_REG_5; i++)
-		callee->regs[i] = caller->regs[i];
 	return 0;
 }
 
 static int set_map_elem_callback_state(struct bpf_verifier_env *env,
+				       struct bpf_verifier_state *state,
 				       struct bpf_func_state *caller,
 				       struct bpf_func_state *callee,
 				       int insn_idx)
@@ -11332,7 +11381,7 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,
 		return -ENOTSUPP;
 	}
 
-	err = map->ops->map_set_for_each_callback_args(env, caller, callee);
+	err = map->ops->map_set_for_each_callback_args(env, state, caller, callee);
 	if (err)
 		return err;
 
@@ -11342,6 +11391,7 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,
 }
 
 static int set_loop_callback_state(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee,
 				   int insn_idx)
@@ -11353,52 +11403,31 @@ static int set_loop_callback_state(struct bpf_verifier_env *env,
 	callee->regs[BPF_REG_1].type = SCALAR_VALUE;
 	callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3];
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
-
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_timer_callback_state(struct bpf_verifier_env *env,
+				    struct bpf_verifier_state *state,
 				    struct bpf_func_state *caller,
 				    struct bpf_func_state *callee,
 				    int insn_idx)
 {
-	struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr;
-	u32 map_uid = caller->regs[BPF_REG_1].map_uid;
-
 	/* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn);
 	 * callback_fn(struct bpf_map *map, void *key, void *value);
 	 */
-	callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_1]);
-	callee->regs[BPF_REG_1].map_ptr = map_ptr;
-	callee->regs[BPF_REG_1].map_uid = map_uid;
-
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = map_ptr;
-	callee->regs[BPF_REG_2].map_uid = map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = map_ptr;
-	callee->regs[BPF_REG_3].map_uid = map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_1], &caller->regs[BPF_REG_1], CONST_PTR_TO_MAP);
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_1], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_1], PTR_TO_MAP_VALUE);
 	callee->regs[BPF_REG_3].id = ++env->id_gen;
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
-	callee->callback_ret_range = retval_range(0, 0);
 	return 0;
 }
 
 static int set_find_vma_callback_state(struct bpf_verifier_env *env,
+				       struct bpf_verifier_state *state,
 				       struct bpf_func_state *caller,
 				       struct bpf_func_state *callee,
 				       int insn_idx)
@@ -11417,39 +11446,35 @@ static int set_find_vma_callback_state(struct bpf_verifier_env *env,
 
 	/* pointer to stack or null */
 	callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4];
-
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env,
+					   struct bpf_verifier_state *state,
 					   struct bpf_func_state *caller,
 					   struct bpf_func_state *callee,
 					   int insn_idx)
 {
+	int err;
+
 	/* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void
 	 *			  callback_ctx, u64 flags);
 	 * callback_fn(const struct bpf_dynptr_t* dynptr, void *callback_ctx);
 	 */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_0]);
-	mark_dynptr_cb_reg(env, &callee->regs[BPF_REG_1], BPF_DYNPTR_TYPE_LOCAL);
+	err = mark_dynptr_cb_reg(env, state, callee, BPF_REG_1, BPF_DYNPTR_TYPE_LOCAL);
+	if (err)
+		return err;
 	callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3];
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
-
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_rbtree_add_callback_state(struct bpf_verifier_env *env,
+					 struct bpf_verifier_state *state,
 					 struct bpf_func_state *caller,
 					 struct bpf_func_state *callee,
 					 int insn_idx)
@@ -11474,78 +11499,43 @@ static int set_rbtree_add_callback_state(struct bpf_verifier_env *env,
 	mark_reg_graph_node(callee->regs, BPF_REG_2, &field->graph_root);
 	ref_set_non_owning(env, &callee->regs[BPF_REG_2]);
 
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,
+						 struct bpf_verifier_state *state,
 						 struct bpf_func_state *caller,
 						 struct bpf_func_state *callee,
 						 int insn_idx)
 {
-	struct bpf_map *map_ptr = caller->regs[BPF_REG_3].map_ptr;
-	u32 map_uid = caller->regs[BPF_REG_3].map_uid;
-
 	/*
 	 * callback_fn(struct bpf_map *map, void *key, void *value);
 	 */
-	callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_1]);
-	callee->regs[BPF_REG_1].map_ptr = map_ptr;
-	callee->regs[BPF_REG_1].map_uid = map_uid;
-
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = map_ptr;
-	callee->regs[BPF_REG_2].map_uid = map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = map_ptr;
-	callee->regs[BPF_REG_3].map_uid = map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_1], &caller->regs[BPF_REG_3], CONST_PTR_TO_MAP);
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_3], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_3], PTR_TO_MAP_VALUE);
 	callee->regs[BPF_REG_3].id = ++env->id_gen;
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
 	callee->callback_ret_range = retval_range(S32_MIN, S32_MAX);
 	return 0;
 }
 
 static int set_rcu_callback_state(struct bpf_verifier_env *env,
+				  struct bpf_verifier_state *state,
 				  struct bpf_func_state *caller,
 				  struct bpf_func_state *callee,
 				  int insn_idx)
 {
-	struct bpf_map *map_ptr = caller->regs[BPF_REG_2].map_ptr;
-	u32 map_uid = caller->regs[BPF_REG_2].map_uid;
-
 	/*
 	 * callback_fn(struct bpf_map *map, void *key, void *value);
 	 */
-	callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_1]);
-	callee->regs[BPF_REG_1].map_ptr = map_ptr;
-	callee->regs[BPF_REG_1].map_uid = map_uid;
-
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = map_ptr;
-	callee->regs[BPF_REG_2].map_uid = map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = map_ptr;
-	callee->regs[BPF_REG_3].map_uid = map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_1], &caller->regs[BPF_REG_2], CONST_PTR_TO_MAP);
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_2], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_2], PTR_TO_MAP_VALUE);
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
 	callee->callback_ret_range = retval_range(S32_MIN, S32_MAX);
 	return 0;
@@ -11708,6 +11698,15 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 		verbose(env, "to caller at %d:\n", *insn_idx);
 		print_verifier_state(env, state, caller->frameno, true);
 	}
+
+	/*
+	 * Values the caller only guaranteed for the duration of the call stop
+	 * being valid here.
+	 */
+	err = release_frame_references(env, callee->frameno);
+	if (err)
+		return err;
+
 	account_processed_insns(env, callee, caller);
 	/* clear everything in the callee. In case of exceptional exits using
 	 * bpf_throw, this will be done by copy_verifier_state for extra frames. */
@@ -11885,6 +11884,11 @@ static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exi
 		return 0;
 
 	for (i = 0; i < state->acquired_refs; i++) {
+		if (!exception_exit && state->refs[i].type == REF_TYPE_FRAME) {
+			verifier_bug(env, "frame %u reference id=%d alive at program exit",
+				     state->refs[i].frameno, state->refs[i].id);
+			return -EFAULT;
+		}
 		if (state->refs[i].type != REF_TYPE_PTR)
 			continue;
 		/* Allow struct_ops programs to return a referenced kptr back to
diff --git a/tools/testing/selftests/bpf/progs/exceptions_fail.c b/tools/testing/selftests/bpf/progs/exceptions_fail.c
index 22503cf62e9f3..86a0667ba3484 100644
--- a/tools/testing/selftests/bpf/progs/exceptions_fail.c
+++ b/tools/testing/selftests/bpf/progs/exceptions_fail.c
@@ -31,6 +31,11 @@ struct {
 	__type(value, struct hmap_elem);
 } hmap SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
+	__uint(max_entries, 4096);
+} user_ringbuf SEC(".maps");
+
 private(A) struct bpf_spin_lock lock;
 private(A) struct bpf_rb_root rbtree __contains(foo, node);
 
@@ -110,6 +115,21 @@ static int timer_cb(void *map, int *key, struct bpf_timer *timer)
 	return 0;
 }
 
+static long drain_cb(struct bpf_dynptr *dynptr, void *context)
+{
+	bpf_throw(0);
+	return 0;
+}
+
+SEC("?tc")
+__failure
+__msg("bpf_throw kfunc (insn {{[0-9]+}}) cannot be called from callback subprog {{[0-9]+}}")
+int reject_user_ringbuf_callback_throw(struct __sk_buff *ctx)
+{
+	bpf_user_ringbuf_drain(&user_ringbuf, drain_cb, NULL, 0);
+	return 0;
+}
+
 SEC("?tc")
 __failure __msg("cannot be called from callback subprog")
 int reject_async_callback_throw(struct __sk_buff *ctx)
diff --git a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
index c0d0422b8030b..4e0f29c2d1f25 100644
--- a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
+++ b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
@@ -1,9 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
 
+#include <stdbool.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
+#include "bpf_kfuncs.h"
 
 char _license[] SEC("license") = "GPL";
 
@@ -243,3 +245,136 @@ int user_ringbuf_callback_const_ptr_to_dynptr_reg_off(void *ctx)
 			       callback_adjust_bpf_dynptr_reg_off, NULL, 0);
 	return 0;
 }
+
+/* The sample goes back to the producer as soon as the callback returns. */
+struct dynptr_ctx {
+	struct bpf_dynptr *saved;
+};
+
+static long callback_park_dynptr(struct bpf_dynptr *dynptr, void *context)
+{
+	struct dynptr_ctx *c = context;
+
+	c->saved = dynptr;
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_park_dynptr(void *ctx)
+{
+	struct dynptr_ctx c = {};
+	char buf[8] = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_dynptr, &c, 0);
+	if (c.saved)
+		bpf_dynptr_read(buf, sizeof(buf), c.saved, 0, 0);
+	return buf[0];
+}
+
+struct slice_ctx {
+	char *p;
+};
+
+static long callback_park_data_slice(struct bpf_dynptr *dynptr, void *context)
+{
+	struct slice_ctx *c = context;
+
+	c->p = bpf_dynptr_data(dynptr, 0, 8);
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_park_data_slice(void *ctx)
+{
+	struct slice_ctx c = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_data_slice, &c, 0);
+	if (c.p)
+		return c.p[0];
+	return 0;
+}
+
+static long callback_park_kfunc_slice(struct bpf_dynptr *dynptr, void *context)
+{
+	struct slice_ctx *c = context;
+
+	c->p = bpf_dynptr_slice(dynptr, 0, NULL, 8);
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_park_kfunc_slice(void *ctx)
+{
+	struct slice_ctx c = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_kfunc_slice, &c, 0);
+	if (c.p)
+		return c.p[0];
+	return 0;
+}
+
+struct clone_ctx {
+	struct bpf_dynptr clone;
+	__u64 armed;
+};
+
+static long callback_park_clone(struct bpf_dynptr *dynptr, void *context)
+{
+	struct clone_ctx *c = context;
+
+	bpf_dynptr_clone(dynptr, &c->clone);
+	c->armed = 1;
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("Expected an initialized dynptr as R3")
+int user_ringbuf_callback_park_clone(void *ctx)
+{
+	struct clone_ctx c = {};
+	char buf[8] = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_clone, &c, 0);
+	if (c.armed)
+		bpf_dynptr_read(buf, sizeof(buf), &c.clone, 0, 0);
+	return buf[0];
+}
+
+SEC("?raw_tp")
+__failure __msg("Expected an initialized dynptr as R1")
+int user_ringbuf_callback_park_clone_then_slice(void *ctx)
+{
+	struct clone_ctx c = {};
+	char *p;
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_clone, &c, 0);
+	if (c.armed) {
+		p = bpf_dynptr_data(&c.clone, 0, 8);
+		if (p)
+			return p[0];
+	}
+	return 0;
+}
+
+/* An inner drain's dynptr must not escape into the outer callback either. */
+static long callback_park_outer(struct bpf_dynptr *dynptr, void *context)
+{
+	struct dynptr_ctx inner = {};
+	char buf[8] = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_dynptr, &inner, 0);
+	if (inner.saved)
+		bpf_dynptr_read(buf, sizeof(buf), inner.saved, 0, 0);
+	return buf[0] ? 1 : 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_nested_park_inner(void *ctx)
+{
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_outer, NULL, 0);
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
index 1fbcc52283061..2e0c56888953b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
@@ -9,6 +9,20 @@ struct {
 	__type(value, __u64);
 } map SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(max_entries, 8);
+	__type(key, __u32);
+	__type(value, __u64);
+} percpu_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 8);
+	__type(key, __u32);
+	__type(value, __u64);
+} hash_map SEC(".maps");
+
 struct {
 	__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
 	__uint(max_entries, 8);
@@ -800,4 +814,81 @@ __naked void check_add_const_regsafe_off(void)
 	: __clobber_common);
 }
 
+struct key_ctx {
+	__u32 *key;
+};
+
+static long park_key_cb(struct bpf_map *map, __u32 *key, __u64 *value,
+			void *context)
+{
+	struct key_ctx *c = context;
+
+	c->key = key;
+	return 0;
+}
+
+/* bpf_for_each_array_elem() passes a key from its own stack frame. */
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int array_park_map_key(void *ctx)
+{
+	struct key_ctx c = {};
+
+	bpf_for_each_map_elem(&map, park_key_cb, &c, 0);
+	if (c.key)
+		return *c.key;
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int percpu_array_park_map_key(void *ctx)
+{
+	struct key_ctx c = {};
+
+	bpf_for_each_map_elem(&percpu_map, park_key_cb, &c, 0);
+	if (c.key)
+		return *c.key;
+	return 0;
+}
+
+/* A hash key points into the element, which outlives the callback. */
+SEC("?raw_tp")
+__success
+int hash_park_map_key(void *ctx)
+{
+	struct key_ctx c = {};
+
+	bpf_for_each_map_elem(&hash_map, park_key_cb, &c, 0);
+	if (c.key)
+		return *c.key;
+	return 0;
+}
+
+struct value_ctx {
+	__u64 *value;
+};
+
+static long park_value_cb(struct bpf_map *map, __u32 *key, __u64 *value,
+			  void *context)
+{
+	struct value_ctx *c = context;
+
+	c->value = value;
+	return 0;
+}
+
+/* Only the key is frame-scoped; the element lives until map teardown. */
+SEC("?raw_tp")
+__success
+int array_park_map_value(void *ctx)
+{
+	struct value_ctx c = {};
+
+	bpf_for_each_map_elem(&map, park_value_cb, &c, 0);
+	if (c.value)
+		return *c.value;
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";

3/2 2026/10/09 22:49 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 18628
							output: 42
							thoughts: 620
						

Thoughts:
**My Assessment of the Patch's Fuzzing Potential**

Okay, so this patch dives into the BPF verifier, specifically messing with its logic within `kernel/bpf/verifier.c`, `kernel/bpf/arraymap.c`, `kernel/bpf/diagnostics.c`, and associated headers, along with test additions in the selftests directory. This immediately screams "WorthFuzzing = true". The core triage philosophy's guidelines perfectly align – we're looking at modifications to BPF, which falls squarely within the "Core Subsystems" category, *and* we see refactoring related to data structure restructuring by the introduction of `REF_TYPE_FRAME` and related functions. This introduces frame scoped arguments to prevent leaking values from callbacks to outer contexts; this is something that is going to need to be rigorously examined.

The verifier is definitely reachable – virtualized environments are key here, through standard QEMU/GCE setups, the `bpf()` syscall, program loading, and map creation. This patch is touching core verifier lifetime tracking, which I know is a common source of subtle bugs. The introduction of frame-scoped arguments, such as `REF_TYPE_FRAME`, `mark_frame_scoped_arg`, and associated functions, to prevent leaking references from callbacks (consider `bpf_user_ringbuf_drain` or `bpf_for_each_map_elem` calls) feels like a very targeted and potentially delicate change. We're also seeing modifications in the function calls `prepare_func_exit`, `push_callback_call`, `check_static_func_call`, `invalidate_reference`, and `refsafe`, all of which will have to be examined.

Given this, I need to be precise on what symbols to guide the fuzzer. Avoiding ubiquitous hot-paths is key. I'll need to focus on the newly introduced functions and the specific points where the reference management is being altered, which feels like a good approach. Now, let's carefully check the necessary Kconfig options by going through the code. I'll need to figure out the minimal set of configurations to reproduce the features covered in this patch. Grepper or read-file should do the trick.



4/2 2026/10/09 22:49 tool grepper
0m Args:
{
  "Expression": "config BPF_SYSCALL",
  "PathPrefixes": [
    "kernel/bpf/Kconfig",
    "init/Kconfig"
  ]
}

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"
}

5/2 2026/10/09 22:49 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 19845
							output: 33
							thoughts: 34
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach understanding the build process, assuming I'm already pretty familiar with the kernel and BPF:

**Inspecting the BPF Build Process**

Alright, my first instinct is to zero in on `kernel/bpf/Makefile`. That's where I expect to find the recipes for compiling key BPF components like `verifier.o`, `arraymap.o`, and `states.o`. I need to figure out how these object files are constructed because they are fundamental to BPF operation. I'm looking for the build rules, the compiler flags used, and any dependencies these modules might have. This will give me a good handle on how the kernel BPF infrastructure is put together, from the low-level object files up. Understanding the Makefile structure will help me quickly identify build options, debugging steps, or any customizations applied.



6/2 2026/10/09 22:50 tool grepper
0m Args:
{
  "Expression": "verifier|arraymap|states",
  "PathPrefixes": [
    "kernel/bpf/Makefile"
  ]
}

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\nkernel/bpf/Makefile:11:obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o\nkernel/bpf/Makefile-12-obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o\nkernel/bpf/Makefile=13=obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\nkernel/bpf/Makefile:14:obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\nkernel/bpf/Makefile-15-obj-${CONFIG_BPF_LSM}\t  += bpf_inode_storage.o\n"
}

7/2 2026/10/09 22:50 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20498
							output: 34
							thoughts: 72
						

Thoughts:
Here's my summary, as I see it:

**BPF Configuration Deep Dive**

Okay, so `CONFIG_BPF_SYSCALL` is the cornerstone, the main config setting for the BPF subsystem, that's clear. But I need to be thorough. I can't just assume that's *all* I need. What about more specialized BPF features like `bpf_user_ringbuf` – are there any additional config options required to enable those functionalities? Or perhaps array maps, or other features I might be relying on in my current development? I should meticulously review the dependencies of `BPF_SYSCALL` itself. A deep dive into `kernel/bpf/Kconfig` will reveal all the interconnected settings and ensure I've covered every configuration nuance. I need to be absolutely certain I have a fully enabled and optimized BPF environment.



8/2 2026/10/09 22:50 tool read-file
0m Args:
{
  "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"
}

9/2 2026/10/09 22:50 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 21399
							output: 50
							thoughts: 1346
						

Thoughts:
**Analyzing BPF Symbol Changes**

Okay, so I'm looking at this patch and the first thing that jumps out is the `BPF_SYSCALL` config flag. That's our starting point. Now, let's dissect the symbols that are being added, modified, or affected. I need to get a clear picture of the scope of these changes.

First, I've got `array_map_set_for_each_callback_args` in `arraymap.c`. Hmm, it's static in that file, but it's used in `bpf_map_ops array_map_ops`, so it's probably visible in `vmlinux`. Yep, static functions in .c files are normal ELF symbols in `vmlinux`, so it's a local symbol.

Now, let's look at the other functions. In `kernel/bpf/verifier.c`, I see a bunch: `mark_frame_scoped_arg` (global), and then a slew of static functions: `release_frame_references`, `acquire_frame_reference`, `invalidate_reference`, `mark_dynptr_cb_reg`, `set_user_ringbuf_callback_state`, `push_callback_call`, `check_reference_leak`, `prepare_func_exit`, and `setup_func_entry`. There's also `map_set_for_each_callback_args` which is a global symbol.

Also, `refsafe` in `kernel/bpf/states.c`, which is also static.

Okay, I need to keep the goal in mind; these could be related to `bpf_user_ringbuf_drain` callback or `for_each_map_elem` callbacks.

But, wait... this reminds me of a general guideline: don't list ubiquitous functions, only list functions related to the added logic. This is for the fuzzer. So, `prepare_func_exit` and `setup_func_entry` are out. They're called by every subprog.

So, let's identify what actually implements the *new* stuff. We've got:

*   `mark_frame_scoped_arg` - This is a *new* function. Looks important.
*   `release_frame_references` - Another *new* one.
*   `acquire_frame_reference` - And another one.
*   `invalidate_reference` - This is renamed/adapted!
*   `array_map_set_for_each_callback_args` - *New* function in `arraymap.c`!
*   `set_user_ringbuf_callback_state` - Modified to mark dynptr cb reg.
*   `mark_dynptr_cb_reg` - Modified to call `mark_frame_scoped_arg`!
*   `map_set_for_each_callback_args` - Modified.

Okay, need to figure out how callbacks in BPF verifier work. I should check how `set_user_ringbuf_callback_state` is used.



10/2 2026/10/09 22:50 tool grepper
0m Args:
{
  "Expression": "set_user_ringbuf_callback_state|mark_frame_scoped_arg|release_frame_references",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/arraymap.c=858=static int array_map_set_for_each_callback_args(struct bpf_verifier_env *env,\n--\nkernel/bpf/arraymap.c-868-\nkernel/bpf/arraymap.c:869:\treturn mark_frame_scoped_arg(env, state, callee, BPF_REG_2);\nkernel/bpf/arraymap.c-870-}\n--\nkernel/bpf/verifier.c=716=static int mark_dynptr_cb_reg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\n--\nkernel/bpf/verifier.c-720-\t__mark_dynptr_reg(\u0026callee-\u003eregs[regno], type, true, 0, 0);\nkernel/bpf/verifier.c:721:\treturn mark_frame_scoped_arg(env, state, callee, regno);\nkernel/bpf/verifier.c-722-}\n--\nkernel/bpf/verifier.c=1534=static int acquire_frame_reference(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\n--\nkernel/bpf/verifier.c-1551- */\nkernel/bpf/verifier.c:1552:int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,\nkernel/bpf/verifier.c-1553-\t\t\t  struct bpf_func_state *callee, u32 regno)\n--\nkernel/bpf/verifier.c=10781=static int release_reference(struct bpf_verifier_env *env, int id)\n--\nkernel/bpf/verifier.c-10791-\nkernel/bpf/verifier.c:10792:static int release_frame_references(struct bpf_verifier_env *env, u32 frameno)\nkernel/bpf/verifier.c-10793-{\n--\nkernel/bpf/verifier.c=11429=static int set_find_vma_callback_state(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-11453-\nkernel/bpf/verifier.c:11454:static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-11455-\t\t\t\t\t   struct bpf_verifier_state *state,\n--\nkernel/bpf/verifier.c=11616=static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)\n--\nkernel/bpf/verifier.c-11705-\t */\nkernel/bpf/verifier.c:11706:\terr = release_frame_references(env, callee-\u003eframeno);\nkernel/bpf/verifier.c-11707-\tif (err)\n--\nkernel/bpf/verifier.c=12125=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-12380-\t\terr = push_callback_call(env, insn, insn_idx, meta.subprogno,\nkernel/bpf/verifier.c:12381:\t\t\t\t\t set_user_ringbuf_callback_state);\nkernel/bpf/verifier.c-12382-\t\tbreak;\n"
}

11/2 2026/10/09 22:50 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24359
							output: 35
							thoughts: 18
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person:

**Analyzing `set_map_elem_callback_state`**

Alright, let's dive in and dissect what's going on with `set_map_elem_callback_state`. I need to understand its specific role and how it interacts with the broader system. I'm especially keen to see what makes this function unique, and how it differs from other, perhaps more general, methods we might have. What's the context? What problem does it solve directly, and what's the intended interaction? Does it handle any locking, synchronization or any unique resource management? I'll probably be able to infer some of the internal workings, if I carefully consider the broader architecture, but seeing the implementation will be revealing. Now, let's see what the code has to say.



12/2 2026/10/09 22:50 tool grepper
0m Args:
{
  "Expression": "set_map_elem_callback_state",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/verifier.c=11345=int map_set_for_each_callback_args(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-11365-\nkernel/bpf/verifier.c:11366:static int set_map_elem_callback_state(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-11367-\t\t\t\t       struct bpf_verifier_state *state,\n--\nkernel/bpf/verifier.c=12125=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-12263-\t\terr = push_callback_call(env, insn, insn_idx, meta.subprogno,\nkernel/bpf/verifier.c:12264:\t\t\t\t\t set_map_elem_callback_state);\nkernel/bpf/verifier.c-12265-\t\tbreak;\n"
}

13/2 2026/10/09 22:50 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24870
							output: 37
							thoughts: 2397
						

Thoughts:
**Focusing on Targeted BPF Verifier Logic for Effective Fuzzing**

Alright, let's break this down. My primary focus here is to identify key functions within the BPF verifier that are *specific* to the changes introduced, and not ubiquitous functions that would dilute fuzzing efforts.  I'm aiming to create a focused list for syzkaller.

First, I examined `kernel/bpf/verifier.c` and identified `mark_frame_scoped_arg` and `release_frame_references` as relevant starting points. I see that `mark_frame_scoped_arg` is called from `array_map_set_for_each_callback_args` (in `kernel/bpf/arraymap.c`) and `mark_dynptr_cb_reg` (in `kernel/bpf/verifier.c`). `release_frame_references` is called by `prepare_func_exit`. `acquire_frame_reference` is called by `mark_frame_scoped_arg`, and `invalidate_reference` is called from `release_frame_references`.

However, `release_frame_references` is called on *every* function exit within the BPF verifier, which is somewhat broad. While all BPF programs call it, this isn't a *specific* entrypoint, so it's less ideal.  `mark_frame_scoped_arg`, `acquire_frame_reference`, `array_map_set_for_each_callback_args`, and `set_user_ringbuf_callback_state` are much more targeted, dealing specifically with callback argument scoping (user ringbuf and array map callbacks). `map_set_for_each_callback_args` deals with map iterators.  These are my primary focus.

The guiding principle is to avoid listing functions used by almost every program.  `release_frame_references` might be too general, but `mark_frame_scoped_arg` is *exactly* the kind of function I'm looking for - it's directly tied to the new callback argument scoping logic.

Looking at the code, I can see that `mark_frame_scoped_arg` is non-static, `release_frame_references` is static, `acquire_frame_reference` is static, `array_map_set_for_each_callback_args` is static, and `set_user_ringbuf_callback_state` is static. This helps identify the scope of these functions.

I confirmed that `prepare_func_exit` calls `release_frame_references` for every BPF function exit, but this isn't a problem, as BPF programs *are* the focus.

Diving deeper, I also looked at `kernel/bpf/states.c` for `refsafe`, and `kernel/bpf/diagnostics.c` for `diag_print_mod`.

A very thorough review of the code diff indicates that the changes are:
1.  Adding a `struct bpf_verifier_state;` and modifying `map_set_for_each_callback_args` signature in `include/linux/bpf.h`, including declaring `mark_frame_scoped_arg`.
2.  Adding `REF_TYPE_FRAME = (1 << 6)` and `u32 frameno` to `bpf_reference_state` in `include/linux/bpf_verifier.h`.
3.  Adding `array_map_set_for_each_callback_args` to `kernel/bpf/arraymap.c` and assigning it to `array_map_ops` and `percpu_array_map_ops`.
4.  Adding `BPF_DIAG_MOD_FRAME_RELEASE` in `kernel/bpf/diagnostics.c` and `diagnostics.h`.
5.  In `kernel/bpf/states.c`, handling `REF_TYPE_FRAME` in `refsafe`.
6.  A number of code changes in `kernel/bpf/verifier.c`, including:

    *   `mark_dynptr_cb_reg` calls `mark_frame_scoped_arg`.
    *   Refactoring `acquire_reference_state`.
    *   `acquire_frame_reference` creates `REF_TYPE_FRAME`.
    *   `mark_frame_scoped_arg` scopes argument to callee frame.
    *   Refactoring `invalidate_reference` from `release_reference`.
    *   `release_frame_references` iterates and invalidates frame refs.
    *   Updates to `setup_func_entry` and `check_static_func_call`.
    *   Updating `push_callback_call


14/2 2026/10/09 22:50 tool grepper
0m Args:
{
  "Expression": "BPF_MAP_TYPE_USER_RINGBUF",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/syscall.c=1401=static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_verifier_log *log,\n--\nkernel/bpf/syscall.c-1515-\tcase BPF_MAP_TYPE_RINGBUF:\nkernel/bpf/syscall.c:1516:\tcase BPF_MAP_TYPE_USER_RINGBUF:\nkernel/bpf/syscall.c-1517-\tcase BPF_MAP_TYPE_CGROUP_STORAGE:\n--\nkernel/bpf/verifier.c=10234=static int check_map_func_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-10262-\t\tbreak;\nkernel/bpf/verifier.c:10263:\tcase BPF_MAP_TYPE_USER_RINGBUF:\nkernel/bpf/verifier.c-10264-\t\tif (func_id != BPF_FUNC_user_ringbuf_drain)\n--\nkernel/bpf/verifier.c-10395-\tcase BPF_FUNC_user_ringbuf_drain:\nkernel/bpf/verifier.c:10396:\t\tif (map-\u003emap_type != BPF_MAP_TYPE_USER_RINGBUF)\nkernel/bpf/verifier.c-10397-\t\t\tgoto error;\n--\nkernel/bpf/verifier.c=19935=static int check_map_prog_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-19993-\t\tcase BPF_MAP_TYPE_RINGBUF:\nkernel/bpf/verifier.c:19994:\t\tcase BPF_MAP_TYPE_USER_RINGBUF:\nkernel/bpf/verifier.c-19995-\t\tcase BPF_MAP_TYPE_INODE_STORAGE:\n"
}

15/2 2026/10/09 22:50 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28819
							output: 247
							thoughts: 543
						

Thoughts:
**Analysis of BPF Ring Buffer Configuration and Symbol Verification**

Okay, so I'm looking at `ringbuf.o` and I see that its build depends on `CONFIG_BPF_SYSCALL`, which is exactly what I expected based on the `Makefile` entry. That makes sense, only `BPF_SYSCALL` is needed here.

Now, let's go over the key symbols involved in this functionality, just to make sure we have a clear picture. The functions I'm focusing on are: `mark_frame_scoped_arg`, `release_frame_references`, `acquire_frame_reference`, `array_map_set_for_each_callback_args`, `set_user_ringbuf_callback_state`, `map_set_for_each_callback_args`, and `invalidate_reference`.

I've just verified that all of these functions exist in the kernel and are spelled correctly, cross-referencing them against the relevant source files – `kernel/bpf/verifier.c` and `kernel/bpf/arraymap.c` primarily. Yep, they are all present and directly related to the new feature being considered, as expected.

I want to be thorough, so I'll quickly check if any other configs are required. Since `BPF_SYSCALL` selects `BPF`, it seems the dependencies are handled correctly in `Kconfig`. And, yes, `config BPF_SYSCALL` (without `CONFIG_` prefix is `BPF_SYSCALL`) makes it all clear.

Everything looks good. No red flags. The setup is clean.

We are ready to move forward. `WorthFuzzing` is set to true. The `EnableConfigs` list contains just ["BPF_SYSCALL"], and the `FocusSymbols` are the ones I just checked.

Okay, I'm confident in the analysis. Let's call `set-results`.



16/2 2026/10/09 22:50 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "mark_frame_scoped_arg",
    "release_frame_references",
    "acquire_frame_reference",
    "array_map_set_for_each_callback_args",
    "set_user_ringbuf_callback_state",
    "map_set_for_each_callback_args",
    "invalidate_reference"
  ],
  "Reasoning": "The patch introduces frame-scoped reference tracking (REF_TYPE_FRAME) in the BPF verifier to invalidate values and derived objects (e.g., ringbuf dynptrs or array map keys passed into callbacks) upon exiting the callee frame. It adds new functions (mark_frame_scoped_arg, release_frame_references, acquire_frame_reference, array_map_set_for_each_callback_args) and new verifier invariants (verifier_bug checks on unreleased frame references and frame 0 scoping). These changes affect reachable core BPF verification logic and should be fuzzed via the bpf() syscall.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "mark_frame_scoped_arg",
    "release_frame_references",
    "acquire_frame_reference",
    "array_map_set_for_each_callback_args",
    "set_user_ringbuf_callback_state",
    "map_set_for_each_callback_args",
    "invalidate_reference"
  ],
  "Reasoning": "The patch introduces frame-scoped reference tracking (REF_TYPE_FRAME) in the BPF verifier to invalidate values and derived objects (e.g., ringbuf dynptrs or array map keys passed into callbacks) upon exiting the callee frame. It adds new functions (mark_frame_scoped_arg, release_frame_references, acquire_frame_reference, array_map_set_for_each_callback_args) and new verifier invariants (verifier_bug checks on unreleased frame references and frame 0 scoping). These changes affect reachable core BPF verification logic and should be fuzzed via the bpf() syscall.",
  "WorthFuzzing": true
}

17/1 2026/10/09 22:50 action If
1m Args:
{
  "WorthFuzzing": true
}

Results:
null

18/2 2026/10/09 22:50 agent kmsan-evaluator
1m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch enhances the BPF verifier's lifetime tracking by introducing frame-scoped reference states (REF_TYPE_FRAME) to prevent BPF callback arguments (such as array map keys and dynptrs) from outliving the callee execution frame.\n\nFrom a memory safety and sanitizer perspective:\n1. Kernel-to-user info leaks: The patch touches only internal BPF verifier tracking state and diagnostics logging. No kernel structures or buffers copied to user space are introduced or modified.\n2. Uninitialized memory reads: All newly introduced or modified structures (such as `struct bpf_reference_state ref = { ... }` in the refactored `acquire_*` helpers) use C designated initializers which ensure zero-initialization of all fields and padding. Newly allocated callee frame states are zero-allocated using `kzalloc_obj()` and initialized via `init_func_state()`.\n3. Verifier logic risks: Any potential bugs introduced by these changes are limited to verifier state comparison/invalidation logic, reference counting, or boundary management, which would manifest as verifier rejections/acceptances or out-of-bounds/NULL dereferences fully covered by KASAN and standard kernel debugging facilities.\n\nBecause no uninitialized memory reads or leaks are introduced or exposed, 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 bc6d2ef517514c1872eba74146a3baa86d9b986a
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Oct 9 22:49:45 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 54144372281cb..e4498e6fa88a6 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -39,6 +39,7 @@
 #include <asm/rqspinlock.h>
 
 struct bpf_verifier_env;
+struct bpf_verifier_state;
 struct bpf_verifier_log;
 struct perf_event;
 struct bpf_prog;
@@ -178,6 +179,7 @@ struct bpf_map_ops {
 
 
 	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
+					      struct bpf_verifier_state *state,
 					      struct bpf_func_state *caller,
 					      struct bpf_func_state *callee);
 	long (*map_for_each_callback)(struct bpf_map *map,
@@ -3183,8 +3185,11 @@ int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
 				struct bpf_link_info *info);
 
 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee);
+int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+			  struct bpf_func_state *callee, u32 regno);
 
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 flags);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags);
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index c51083c761cf2..54a4f440c33b3 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -272,15 +272,13 @@ struct bpf_stack_state {
 };
 
 struct bpf_reference_state {
-	/* Each reference object has a type. Ensure REF_TYPE_PTR is zero to
-	 * default to pointer reference on zero initialization of a state.
-	 */
 	enum ref_state_type {
 		REF_TYPE_PTR		= (1 << 1),
 		REF_TYPE_IRQ		= (1 << 2),
 		REF_TYPE_LOCK		= (1 << 3),
 		REF_TYPE_RES_LOCK 	= (1 << 4),
 		REF_TYPE_RES_LOCK_IRQ	= (1 << 5),
+		REF_TYPE_FRAME		= (1 << 6),
 		REF_TYPE_LOCK_MASK	= REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,
 	} type;
 	/* Track each reference created with a unique id, even if the same
@@ -298,6 +296,8 @@ struct bpf_reference_state {
 		 * it matches on unlock.
 		 */
 		void *ptr;
+		/* For REF_TYPE_FRAME */
+		u32 frameno;
 	};
 };
 
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 0fe9afd4a591e..5e8669e311b6d 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -855,6 +855,20 @@ static u64 array_map_mem_usage(const struct bpf_map *map)
 	return usage;
 }
 
+static int array_map_set_for_each_callback_args(struct bpf_verifier_env *env,
+						struct bpf_verifier_state *state,
+						struct bpf_func_state *caller,
+						struct bpf_func_state *callee)
+{
+	int err;
+
+	err = map_set_for_each_callback_args(env, state, caller, callee);
+	if (err)
+		return err;
+
+	return mark_frame_scoped_arg(env, state, callee, BPF_REG_2);
+}
+
 BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
 const struct bpf_map_ops array_map_ops = {
 	.map_meta_equal = array_map_meta_equal,
@@ -875,7 +889,7 @@ const struct bpf_map_ops array_map_ops = {
 	.map_check_btf = array_map_check_btf,
 	.map_lookup_batch = generic_map_lookup_batch,
 	.map_update_batch = generic_map_update_batch,
-	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_set_for_each_callback_args = array_map_set_for_each_callback_args,
 	.map_for_each_callback = bpf_for_each_array_elem,
 	.map_mem_usage = array_map_mem_usage,
 	.map_btf_id = &array_map_btf_ids[0],
@@ -900,7 +914,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
 	.map_check_btf = array_map_check_btf,
 	.map_lookup_batch = generic_map_lookup_batch,
 	.map_update_batch = generic_map_update_batch,
-	.map_set_for_each_callback_args = map_set_for_each_callback_args,
+	.map_set_for_each_callback_args = array_map_set_for_each_callback_args,
 	.map_for_each_callback = bpf_for_each_array_elem,
 	.map_mem_usage = array_map_mem_usage,
 	.map_btf_id = &array_map_btf_ids[0],
diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c
index 857b668212fb3..ba1a3f07d58e3 100644
--- a/kernel/bpf/diagnostics.c
+++ b/kernel/bpf/diagnostics.c
@@ -2238,6 +2238,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_h
 								   "resource release invalidated "
 								   "this value";
 		break;
+	case BPF_DIAG_MOD_FRAME_RELEASE:
+		reason = "the callback that owned this value returned";
+		break;
 	case BPF_DIAG_MOD_PKT_DATA_CHANGE:
 		reason = "packet data may have moved";
 		break;
diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h
index a4102fb049ece..b5cab4d79c1a2 100644
--- a/kernel/bpf/diagnostics.h
+++ b/kernel/bpf/diagnostics.h
@@ -22,6 +22,7 @@ enum bpf_diag_mod_reason {
 	BPF_DIAG_MOD_SPILL,
 	BPF_DIAG_MOD_VAR_WRITE,
 	BPF_DIAG_MOD_REF_RELEASE,
+	BPF_DIAG_MOD_FRAME_RELEASE,
 	BPF_DIAG_MOD_PKT_DATA_CHANGE,
 	BPF_DIAG_MOD_NON_OWN_REF,
 	BPF_DIAG_MOD_CALLER_SAVED,
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 18bf7b660c2f6..1291824a3a7dd 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -899,6 +899,10 @@ static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c
 			break;
 		case REF_TYPE_IRQ:
 			break;
+		case REF_TYPE_FRAME:
+			if (old->refs[i].frameno != cur->refs[i].frameno)
+				return false;
+			break;
 		case REF_TYPE_LOCK:
 		case REF_TYPE_RES_LOCK:
 		case REF_TYPE_RES_LOCK_IRQ:
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 353bde9ae2274..edc78e50dc22c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -713,11 +713,12 @@ static void mark_dynptr_stack_regs(struct bpf_verifier_env *env,
 	__mark_dynptr_reg(sreg2, type, false, id, parent_id);
 }
 
-static void mark_dynptr_cb_reg(struct bpf_verifier_env *env,
-			       struct bpf_reg_state *reg,
-			       enum bpf_dynptr_type type)
+static int mark_dynptr_cb_reg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+			      struct bpf_func_state *callee, u32 regno,
+			      enum bpf_dynptr_type type)
 {
-	__mark_dynptr_reg(reg, type, true, ++env->id_gen, 0);
+	__mark_dynptr_reg(&callee->regs[regno], type, true, 0, 0);
+	return mark_frame_scoped_arg(env, state, callee, regno);
 }
 
 static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
@@ -1497,51 +1498,96 @@ static int grow_stack_arg_slots(struct bpf_verifier_env *env,
 	return 0;
 }
 
-/* Acquire a pointer id from the env and update the state->refs to include
- * this new pointer reference.
- * On success, returns a valid pointer id to associate with the register
- * On failure, returns a negative errno.
+/*
+ * Append @ref to @state->refs. Return its id, or a negative errno.
  */
-static struct bpf_reference_state *acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
+static int acquire_reference_state(struct bpf_verifier_state *state,
+				   const struct bpf_reference_state *ref)
 {
-	struct bpf_verifier_state *state = env->cur_state;
 	int new_ofs = state->acquired_refs;
 	int err;
 
 	err = resize_reference_state(state, state->acquired_refs + 1);
 	if (err)
-		return NULL;
-	state->refs[new_ofs].insn_idx = insn_idx;
-
-	return &state->refs[new_ofs];
+		return err;
+	state->refs[new_ofs] = *ref;
+	return ref->id;
 }
 
 static int acquire_reference(struct bpf_verifier_env *env, int insn_idx, int parent_id)
 {
-	struct bpf_reference_state *s;
+	struct bpf_reference_state ref = {
+		.type = REF_TYPE_PTR,
+		.id = ++env->id_gen,
+		.insn_idx = insn_idx,
+		.parent_id = parent_id,
+	};
+	int id;
 
-	s = acquire_reference_state(env, insn_idx);
-	if (!s)
-		return -ENOMEM;
-	s->type = REF_TYPE_PTR;
-	s->id = ++env->id_gen;
-	s->parent_id = parent_id;
-	bpf_diag_record_ref_acquire(env, insn_idx, s->id);
-	return s->id;
+	id = acquire_reference_state(env->cur_state, &ref);
+	if (id >= 0)
+		bpf_diag_record_ref_acquire(env, insn_idx, id);
+	return id;
+}
+
+/* Acquire a reference owned by frame @frameno of @state */
+static int acquire_frame_reference(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+				   int insn_idx, u32 frameno)
+{
+	struct bpf_reference_state ref = {
+		.type = REF_TYPE_FRAME,
+		.id = ++env->id_gen,
+		.insn_idx = insn_idx,
+		.frameno = frameno,
+	};
+
+	return acquire_reference_state(state, &ref);
+}
+
+/*
+ * Anchor @regno in @callee to a reference owned by the callee frame, so the
+ * value and everything derived from it is invalidated when the frame is
+ * popped. @state owns @callee; for a callback it is not env->cur_state.
+ */
+int mark_frame_scoped_arg(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
+			  struct bpf_func_state *callee, u32 regno)
+{
+	int id;
+
+	if (verifier_bug_if(!callee->frameno, env,
+			    "cannot scope an argument to frame 0"))
+		return -EFAULT;
+
+	id = acquire_frame_reference(env, state, callee->callsite, callee->frameno);
+	if (id < 0)
+		return id;
+	/*
+	 * The value is its own lifetime anchor: there is no associated
+	 * object to borrow from, only the frame. parent_id = id here
+	 * covers both possible derived references:
+	 *  - through the id (e.g. dynptr slice)
+	 *  - through parent_id (e.g. dynptr clone)
+	 */
+	callee->regs[regno].id = id;
+	callee->regs[regno].parent_id = id;
+	return 0;
 }
 
 static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum ref_state_type type,
 			      int id, void *ptr)
 {
 	struct bpf_verifier_state *state = env->cur_state;
-	struct bpf_reference_state *s;
+	struct bpf_reference_state ref = {
+		.type = type,
+		.id = id,
+		.insn_idx = insn_idx,
+		.ptr = ptr,
+	};
+	int err;
 
-	s = acquire_reference_state(env, insn_idx);
-	if (!s)
-		return -ENOMEM;
-	s->type = type;
-	s->id = id;
-	s->ptr = ptr;
+	err = acquire_reference_state(state, &ref);
+	if (err < 0)
+		return err;
 
 	state->active_locks++;
 	state->active_lock_id = id;
@@ -1554,18 +1600,21 @@ static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum r
 static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx)
 {
 	struct bpf_verifier_state *state = env->cur_state;
-	struct bpf_reference_state *s;
+	struct bpf_reference_state ref = {
+		.type = REF_TYPE_IRQ,
+		.id = ++env->id_gen,
+		.insn_idx = insn_idx,
+	};
+	int id;
 
-	s = acquire_reference_state(env, insn_idx);
-	if (!s)
-		return -ENOMEM;
-	s->type = REF_TYPE_IRQ;
-	s->id = ++env->id_gen;
+	id = acquire_reference_state(state, &ref);
+	if (id < 0)
+		return id;
 
-	state->active_irq_id = s->id;
+	state->active_irq_id = id;
 	bpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_IRQ, true,
 				bpf_diag_irq_depth(state));
-	return s->id;
+	return id;
 }
 
 static void release_reference_state(struct bpf_verifier_state *state, int idx)
@@ -10663,8 +10712,9 @@ static int idstack_pop(struct bpf_idmap *idmap)
 	return idmap->map[--idmap->cnt].old;
 }
 
-/* Release id and objects derived from it iteratively in a DFS manner */
-static int release_reference(struct bpf_verifier_env *env, int id)
+/* Invalidate id and values derived from it iteratively in a DFS manner. */
+static int invalidate_reference(struct bpf_verifier_env *env, int id,
+				enum bpf_diag_mod_reason reason)
 {
 	u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
 	struct bpf_verifier_state *vstate = env->cur_state;
@@ -10679,11 +10729,6 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 	if (err)
 		return err;
 
-	if (find_reference_state(vstate, id)) {
-		err = release_reference_nomark(env, id);
-		WARN_ON_ONCE(err);
-	}
-
 	while ((id = idstack_pop(idstack))) {
 		/*
 		 * Child references are inaccessible after parent is released,
@@ -10704,7 +10749,7 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 				continue;
 
 			/* Free objects derived from the current object */
-			if (reg->parent_id == id) {
+			if (reg->parent_id == id && reg->id != id) {
 				err = idstack_push(idstack, reg->id);
 				if (err)
 					return err;
@@ -10719,14 +10764,12 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 
 				if (reg->dynptr.first_slot)
 					dyn_stack--;
-				bpf_diag_record_scrub(env, &dyn_stack[0].spilled_ptr,
-						      BPF_DIAG_MOD_REF_RELEASE);
-				bpf_diag_record_scrub(env, &dyn_stack[1].spilled_ptr,
-						      BPF_DIAG_MOD_REF_RELEASE);
+				bpf_diag_record_scrub(env, &dyn_stack[0].spilled_ptr, reason);
+				bpf_diag_record_scrub(env, &dyn_stack[1].spilled_ptr, reason);
 				invalidate_dynptr(env, dyn_stack);
 				continue;
 			}
-			bpf_diag_record_scrub(env, reg, BPF_DIAG_MOD_REF_RELEASE);
+			bpf_diag_record_scrub(env, reg, reason);
 			if (!stack || stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL)
 				mark_reg_invalid(env, reg);
 		}));
@@ -10735,6 +10778,38 @@ static int release_reference(struct bpf_verifier_env *env, int id)
 	return 0;
 }
 
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+	int err;
+
+	if (find_reference_state(env->cur_state, id)) {
+		err = release_reference_nomark(env, id);
+		WARN_ON_ONCE(err);
+	}
+	return invalidate_reference(env, id, BPF_DIAG_MOD_REF_RELEASE);
+}
+
+static int release_frame_references(struct bpf_verifier_env *env, u32 frameno)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	int i, id, err;
+
+	for (i = 0; i < state->acquired_refs;) {
+		if (state->refs[i].type != REF_TYPE_FRAME ||
+		    state->refs[i].frameno != frameno) {
+			i++;
+			continue;
+		}
+		id = state->refs[i].id;
+		release_reference_state(state, i);
+		err = invalidate_reference(env, id, BPF_DIAG_MOD_FRAME_RELEASE);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static void invalidate_non_owning_refs(struct bpf_verifier_env *env)
 {
 	struct bpf_func_state *unused;
@@ -10817,36 +10892,30 @@ static void invalidate_outgoing_stack_args(struct bpf_verifier_env *env,
 }
 
 typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee,
 				   int insn_idx);
 
-static int set_callee_state(struct bpf_verifier_env *env,
-			    struct bpf_func_state *caller,
-			    struct bpf_func_state *callee, int insn_idx);
-
-static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int callsite,
-			    set_callee_state_fn set_callee_state_cb,
-			    struct bpf_verifier_state *state)
+static struct bpf_func_state *setup_func_entry(struct bpf_verifier_env *env, int subprog,
+					       int callsite, struct bpf_verifier_state *state)
 {
-	struct bpf_func_state *caller, *callee;
-	int err;
+	struct bpf_func_state *callee;
 
 	if (state->curframe + 1 >= MAX_CALL_FRAMES) {
 		verbose(env, "the call stack of %d frames is too deep\n",
 			state->curframe + 2);
-		return -E2BIG;
+		return ERR_PTR(-E2BIG);
 	}
 
 	if (state->frame[state->curframe + 1]) {
 		verifier_bug(env, "Frame %d already allocated", state->curframe + 1);
-		return -EFAULT;
+		return ERR_PTR(-EFAULT);
 	}
 
-	caller = state->frame[state->curframe];
 	callee = kzalloc_obj(*callee, GFP_KERNEL_ACCOUNT);
 	if (!callee)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 	state->frame[state->curframe + 1] = callee;
 
 	/* callee cannot access r0, r6 - r9 for reading and has to write
@@ -10858,19 +10927,9 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int calls
 			callsite,
 			state->curframe + 1 /* frameno within this callchain */,
 			subprog /* subprog number within this prog */);
-	err = set_callee_state_cb(env, caller, callee, callsite);
-	if (err)
-		goto err_out;
-
-	/* only increment it after check_reg_arg() finished */
 	state->curframe++;
 
-	return 0;
-
-err_out:
-	free_func_state(callee);
-	state->frame[state->curframe + 1] = NULL;
-	return err;
+	return callee;
 }
 
 static void gen_subprog_arg_proto(const struct bpf_subprog_info *sub, const struct btf *btf,
@@ -10990,10 +11049,6 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
 	if (err == -EFAULT)
 		return err;
 
-	/* set_callee_state is used for direct subprog calls, but we are
-	 * interested in validating only BPF helpers that can call subprogs as
-	 * callbacks
-	 */
 	env->subprog_info[subprog].is_cb = true;
 	if (bpf_pseudo_kfunc_call(insn) &&
 	    !is_callback_calling_kfunc(insn->imm)) {
@@ -11021,7 +11076,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
 		callee->async_entry_cnt = state->frame[0]->async_entry_cnt + 1;
 
 		/* Convert bpf_timer_set_callback() args into timer callback args */
-		err = set_callee_state_cb(env, caller, callee, insn_idx);
+		err = set_callee_state_cb(env, async_cb, caller, callee, insn_idx);
 		if (err)
 			return err;
 
@@ -11044,8 +11099,11 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
 	if (IS_ERR(callback_state))
 		return PTR_ERR(callback_state);
 
-	err = setup_func_entry(env, subprog, insn_idx, set_callee_state_cb,
-			       callback_state);
+	callee = setup_func_entry(env, subprog, insn_idx, callback_state);
+	if (IS_ERR(callee))
+		return PTR_ERR(callee);
+
+	err = set_callee_state_cb(env, callback_state, caller, callee, insn_idx);
 	if (err)
 		return err;
 
@@ -11069,8 +11127,8 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
 	struct bpf_verifier_state *state = env->cur_state;
 	struct bpf_subprog_info *caller_info;
 	u16 callee_incoming, stack_arg_cnt;
-	struct bpf_func_state *caller;
-	int err;
+	struct bpf_func_state *caller, *callee;
+	int i;
 
 	caller = state->frame[state->curframe];
 
@@ -11088,9 +11146,12 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
 	 * For regular function entry setup new frame and continue
 	 * from that frame.
 	 */
-	err = setup_func_entry(env, subprog, *insn_idx, set_callee_state, state);
-	if (err)
-		return err;
+	callee = setup_func_entry(env, subprog, *insn_idx, state);
+	if (IS_ERR(callee))
+		return PTR_ERR(callee);
+
+	for (i = BPF_REG_1; i <= BPF_REG_5; i++)
+		callee->regs[i] = caller->regs[i];
 
 	bpf_diag_record_scrub(env, &caller->regs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED);
 	clear_caller_saved_regs(env, caller->regs);
@@ -11271,7 +11332,18 @@ static int check_func_callx(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	return check_static_func_call(env, subprog, insn_idx);
 }
 
+static void mark_map_callback_reg(struct bpf_reg_state *reg,
+				  const struct bpf_reg_state *map_reg,
+				  enum bpf_reg_type type)
+{
+	reg->type = type;
+	__mark_reg_known_zero(reg);
+	reg->map_ptr = map_reg->map_ptr;
+	reg->map_uid = map_reg->map_uid;
+}
+
 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee)
 {
@@ -11282,40 +11354,17 @@ int map_set_for_each_callback_args(struct bpf_verifier_env *env,
 	 */
 	callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1];
 
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr;
-	callee->regs[BPF_REG_2].map_uid = caller->regs[BPF_REG_1].map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr;
-	callee->regs[BPF_REG_3].map_uid = caller->regs[BPF_REG_1].map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_1], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_1], PTR_TO_MAP_VALUE);
 	callee->regs[BPF_REG_3].id = ++env->id_gen;
 
 	/* pointer to stack or null */
 	callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3];
-
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
-	return 0;
-}
-
-static int set_callee_state(struct bpf_verifier_env *env,
-			    struct bpf_func_state *caller,
-			    struct bpf_func_state *callee, int insn_idx)
-{
-	int i;
-
-	/* copy r1 - r5 args that callee can access.  The copy includes parent
-	 * pointers, which connects us up to the liveness chain
-	 */
-	for (i = BPF_REG_1; i <= BPF_REG_5; i++)
-		callee->regs[i] = caller->regs[i];
 	return 0;
 }
 
 static int set_map_elem_callback_state(struct bpf_verifier_env *env,
+				       struct bpf_verifier_state *state,
 				       struct bpf_func_state *caller,
 				       struct bpf_func_state *callee,
 				       int insn_idx)
@@ -11332,7 +11381,7 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,
 		return -ENOTSUPP;
 	}
 
-	err = map->ops->map_set_for_each_callback_args(env, caller, callee);
+	err = map->ops->map_set_for_each_callback_args(env, state, caller, callee);
 	if (err)
 		return err;
 
@@ -11342,6 +11391,7 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,
 }
 
 static int set_loop_callback_state(struct bpf_verifier_env *env,
+				   struct bpf_verifier_state *state,
 				   struct bpf_func_state *caller,
 				   struct bpf_func_state *callee,
 				   int insn_idx)
@@ -11353,52 +11403,31 @@ static int set_loop_callback_state(struct bpf_verifier_env *env,
 	callee->regs[BPF_REG_1].type = SCALAR_VALUE;
 	callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3];
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
-
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_timer_callback_state(struct bpf_verifier_env *env,
+				    struct bpf_verifier_state *state,
 				    struct bpf_func_state *caller,
 				    struct bpf_func_state *callee,
 				    int insn_idx)
 {
-	struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr;
-	u32 map_uid = caller->regs[BPF_REG_1].map_uid;
-
 	/* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn);
 	 * callback_fn(struct bpf_map *map, void *key, void *value);
 	 */
-	callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_1]);
-	callee->regs[BPF_REG_1].map_ptr = map_ptr;
-	callee->regs[BPF_REG_1].map_uid = map_uid;
-
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = map_ptr;
-	callee->regs[BPF_REG_2].map_uid = map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = map_ptr;
-	callee->regs[BPF_REG_3].map_uid = map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_1], &caller->regs[BPF_REG_1], CONST_PTR_TO_MAP);
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_1], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_1], PTR_TO_MAP_VALUE);
 	callee->regs[BPF_REG_3].id = ++env->id_gen;
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
-	callee->callback_ret_range = retval_range(0, 0);
 	return 0;
 }
 
 static int set_find_vma_callback_state(struct bpf_verifier_env *env,
+				       struct bpf_verifier_state *state,
 				       struct bpf_func_state *caller,
 				       struct bpf_func_state *callee,
 				       int insn_idx)
@@ -11417,39 +11446,35 @@ static int set_find_vma_callback_state(struct bpf_verifier_env *env,
 
 	/* pointer to stack or null */
 	callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4];
-
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_user_ringbuf_callback_state(struct bpf_verifier_env *env,
+					   struct bpf_verifier_state *state,
 					   struct bpf_func_state *caller,
 					   struct bpf_func_state *callee,
 					   int insn_idx)
 {
+	int err;
+
 	/* bpf_user_ringbuf_drain(struct bpf_map *map, void *callback_fn, void
 	 *			  callback_ctx, u64 flags);
 	 * callback_fn(const struct bpf_dynptr_t* dynptr, void *callback_ctx);
 	 */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_0]);
-	mark_dynptr_cb_reg(env, &callee->regs[BPF_REG_1], BPF_DYNPTR_TYPE_LOCAL);
+	err = mark_dynptr_cb_reg(env, state, callee, BPF_REG_1, BPF_DYNPTR_TYPE_LOCAL);
+	if (err)
+		return err;
 	callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3];
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
-
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_rbtree_add_callback_state(struct bpf_verifier_env *env,
+					 struct bpf_verifier_state *state,
 					 struct bpf_func_state *caller,
 					 struct bpf_func_state *callee,
 					 int insn_idx)
@@ -11474,78 +11499,43 @@ static int set_rbtree_add_callback_state(struct bpf_verifier_env *env,
 	mark_reg_graph_node(callee->regs, BPF_REG_2, &field->graph_root);
 	ref_set_non_owning(env, &callee->regs[BPF_REG_2]);
 
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
 }
 
 static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,
+						 struct bpf_verifier_state *state,
 						 struct bpf_func_state *caller,
 						 struct bpf_func_state *callee,
 						 int insn_idx)
 {
-	struct bpf_map *map_ptr = caller->regs[BPF_REG_3].map_ptr;
-	u32 map_uid = caller->regs[BPF_REG_3].map_uid;
-
 	/*
 	 * callback_fn(struct bpf_map *map, void *key, void *value);
 	 */
-	callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_1]);
-	callee->regs[BPF_REG_1].map_ptr = map_ptr;
-	callee->regs[BPF_REG_1].map_uid = map_uid;
-
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = map_ptr;
-	callee->regs[BPF_REG_2].map_uid = map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = map_ptr;
-	callee->regs[BPF_REG_3].map_uid = map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_1], &caller->regs[BPF_REG_3], CONST_PTR_TO_MAP);
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_3], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_3], PTR_TO_MAP_VALUE);
 	callee->regs[BPF_REG_3].id = ++env->id_gen;
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
 	callee->callback_ret_range = retval_range(S32_MIN, S32_MAX);
 	return 0;
 }
 
 static int set_rcu_callback_state(struct bpf_verifier_env *env,
+				  struct bpf_verifier_state *state,
 				  struct bpf_func_state *caller,
 				  struct bpf_func_state *callee,
 				  int insn_idx)
 {
-	struct bpf_map *map_ptr = caller->regs[BPF_REG_2].map_ptr;
-	u32 map_uid = caller->regs[BPF_REG_2].map_uid;
-
 	/*
 	 * callback_fn(struct bpf_map *map, void *key, void *value);
 	 */
-	callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_1]);
-	callee->regs[BPF_REG_1].map_ptr = map_ptr;
-	callee->regs[BPF_REG_1].map_uid = map_uid;
-
-	callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_2]);
-	callee->regs[BPF_REG_2].map_ptr = map_ptr;
-	callee->regs[BPF_REG_2].map_uid = map_uid;
-
-	callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE;
-	__mark_reg_known_zero(&callee->regs[BPF_REG_3]);
-	callee->regs[BPF_REG_3].map_ptr = map_ptr;
-	callee->regs[BPF_REG_3].map_uid = map_uid;
+	mark_map_callback_reg(&callee->regs[BPF_REG_1], &caller->regs[BPF_REG_2], CONST_PTR_TO_MAP);
+	mark_map_callback_reg(&callee->regs[BPF_REG_2], &caller->regs[BPF_REG_2], PTR_TO_MAP_KEY);
+	mark_map_callback_reg(&callee->regs[BPF_REG_3], &caller->regs[BPF_REG_2], PTR_TO_MAP_VALUE);
 
-	/* unused */
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
-	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
 	callee->callback_ret_range = retval_range(S32_MIN, S32_MAX);
 	return 0;
@@ -11708,6 +11698,15 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 		verbose(env, "to caller at %d:\n", *insn_idx);
 		print_verifier_state(env, state, caller->frameno, true);
 	}
+
+	/*
+	 * Values the caller only guaranteed for the duration of the call stop
+	 * being valid here.
+	 */
+	err = release_frame_references(env, callee->frameno);
+	if (err)
+		return err;
+
 	account_processed_insns(env, callee, caller);
 	/* clear everything in the callee. In case of exceptional exits using
 	 * bpf_throw, this will be done by copy_verifier_state for extra frames. */
@@ -11885,6 +11884,11 @@ static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exi
 		return 0;
 
 	for (i = 0; i < state->acquired_refs; i++) {
+		if (!exception_exit && state->refs[i].type == REF_TYPE_FRAME) {
+			verifier_bug(env, "frame %u reference id=%d alive at program exit",
+				     state->refs[i].frameno, state->refs[i].id);
+			return -EFAULT;
+		}
 		if (state->refs[i].type != REF_TYPE_PTR)
 			continue;
 		/* Allow struct_ops programs to return a referenced kptr back to
diff --git a/tools/testing/selftests/bpf/progs/exceptions_fail.c b/tools/testing/selftests/bpf/progs/exceptions_fail.c
index 22503cf62e9f3..86a0667ba3484 100644
--- a/tools/testing/selftests/bpf/progs/exceptions_fail.c
+++ b/tools/testing/selftests/bpf/progs/exceptions_fail.c
@@ -31,6 +31,11 @@ struct {
 	__type(value, struct hmap_elem);
 } hmap SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
+	__uint(max_entries, 4096);
+} user_ringbuf SEC(".maps");
+
 private(A) struct bpf_spin_lock lock;
 private(A) struct bpf_rb_root rbtree __contains(foo, node);
 
@@ -110,6 +115,21 @@ static int timer_cb(void *map, int *key, struct bpf_timer *timer)
 	return 0;
 }
 
+static long drain_cb(struct bpf_dynptr *dynptr, void *context)
+{
+	bpf_throw(0);
+	return 0;
+}
+
+SEC("?tc")
+__failure
+__msg("bpf_throw kfunc (insn {{[0-9]+}}) cannot be called from callback subprog {{[0-9]+}}")
+int reject_user_ringbuf_callback_throw(struct __sk_buff *ctx)
+{
+	bpf_user_ringbuf_drain(&user_ringbuf, drain_cb, NULL, 0);
+	return 0;
+}
+
 SEC("?tc")
 __failure __msg("cannot be called from callback subprog")
 int reject_async_callback_throw(struct __sk_buff *ctx)
diff --git a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
index c0d0422b8030b..4e0f29c2d1f25 100644
--- a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
+++ b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
@@ -1,9 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
 
+#include <stdbool.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
+#include "bpf_kfuncs.h"
 
 char _license[] SEC("license") = "GPL";
 
@@ -243,3 +245,136 @@ int user_ringbuf_callback_const_ptr_to_dynptr_reg_off(void *ctx)
 			       callback_adjust_bpf_dynptr_reg_off, NULL, 0);
 	return 0;
 }
+
+/* The sample goes back to the producer as soon as the callback returns. */
+struct dynptr_ctx {
+	struct bpf_dynptr *saved;
+};
+
+static long callback_park_dynptr(struct bpf_dynptr *dynptr, void *context)
+{
+	struct dynptr_ctx *c = context;
+
+	c->saved = dynptr;
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_park_dynptr(void *ctx)
+{
+	struct dynptr_ctx c = {};
+	char buf[8] = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_dynptr, &c, 0);
+	if (c.saved)
+		bpf_dynptr_read(buf, sizeof(buf), c.saved, 0, 0);
+	return buf[0];
+}
+
+struct slice_ctx {
+	char *p;
+};
+
+static long callback_park_data_slice(struct bpf_dynptr *dynptr, void *context)
+{
+	struct slice_ctx *c = context;
+
+	c->p = bpf_dynptr_data(dynptr, 0, 8);
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_park_data_slice(void *ctx)
+{
+	struct slice_ctx c = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_data_slice, &c, 0);
+	if (c.p)
+		return c.p[0];
+	return 0;
+}
+
+static long callback_park_kfunc_slice(struct bpf_dynptr *dynptr, void *context)
+{
+	struct slice_ctx *c = context;
+
+	c->p = bpf_dynptr_slice(dynptr, 0, NULL, 8);
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_park_kfunc_slice(void *ctx)
+{
+	struct slice_ctx c = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_kfunc_slice, &c, 0);
+	if (c.p)
+		return c.p[0];
+	return 0;
+}
+
+struct clone_ctx {
+	struct bpf_dynptr clone;
+	__u64 armed;
+};
+
+static long callback_park_clone(struct bpf_dynptr *dynptr, void *context)
+{
+	struct clone_ctx *c = context;
+
+	bpf_dynptr_clone(dynptr, &c->clone);
+	c->armed = 1;
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("Expected an initialized dynptr as R3")
+int user_ringbuf_callback_park_clone(void *ctx)
+{
+	struct clone_ctx c = {};
+	char buf[8] = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_clone, &c, 0);
+	if (c.armed)
+		bpf_dynptr_read(buf, sizeof(buf), &c.clone, 0, 0);
+	return buf[0];
+}
+
+SEC("?raw_tp")
+__failure __msg("Expected an initialized dynptr as R1")
+int user_ringbuf_callback_park_clone_then_slice(void *ctx)
+{
+	struct clone_ctx c = {};
+	char *p;
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_clone, &c, 0);
+	if (c.armed) {
+		p = bpf_dynptr_data(&c.clone, 0, 8);
+		if (p)
+			return p[0];
+	}
+	return 0;
+}
+
+/* An inner drain's dynptr must not escape into the outer callback either. */
+static long callback_park_outer(struct bpf_dynptr *dynptr, void *context)
+{
+	struct dynptr_ctx inner = {};
+	char buf[8] = {};
+
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_dynptr, &inner, 0);
+	if (inner.saved)
+		bpf_dynptr_read(buf, sizeof(buf), inner.saved, 0, 0);
+	return buf[0] ? 1 : 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("the callback that owned this value returned")
+int user_ringbuf_callback_nested_park_inner(void *ctx)
+{
+	bpf_user_ringbuf_drain(&user_ringbuf, callback_park_outer, NULL, 0);
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
index 1fbcc52283061..2e0c56888953b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
@@ -9,6 +9,20 @@ struct {
 	__type(value, __u64);
 } map SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(max_entries, 8);
+	__type(key, __u32);
+	__type(value, __u64);
+} percpu_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 8);
+	__type(key, __u32);
+	__type(value, __u64);
+} hash_map SEC(".maps");
+
 struct {
 	__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
 	__uint(max_entries, 8);
@@ -800,4 +814,81 @@ __naked void check_add_const_regsafe_off(void)
 	: __clobber_common);
 }
 
+struct key_ctx {
+	__u32 *key;
+};
+
+static long park_key_cb(struct bpf_map *map, __u32 *key, __u64 *value,
+			void *context)
+{
+	struct key_ctx *c = context;
+
+	c->key = key;
+	return 0;
+}
+
+/* bpf_for_each_array_elem() passes a key from its own stack frame. */
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int array_park_map_key(void *ctx)
+{
+	struct key_ctx c = {};
+
+	bpf_for_each_map_elem(&map, park_key_cb, &c, 0);
+	if (c.key)
+		return *c.key;
+	return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int percpu_array_park_map_key(void *ctx)
+{
+	struct key_ctx c = {};
+
+	bpf_for_each_map_elem(&percpu_map, park_key_cb, &c, 0);
+	if (c.key)
+		return *c.key;
+	return 0;
+}
+
+/* A hash key points into the element, which outlives the callback. */
+SEC("?raw_tp")
+__success
+int hash_park_map_key(void *ctx)
+{
+	struct key_ctx c = {};
+
+	bpf_for_each_map_elem(&hash_map, park_key_cb, &c, 0);
+	if (c.key)
+		return *c.key;
+	return 0;
+}
+
+struct value_ctx {
+	__u64 *value;
+};
+
+static long park_value_cb(struct bpf_map *map, __u32 *key, __u64 *value,
+			  void *context)
+{
+	struct value_ctx *c = context;
+
+	c->value = value;
+	return 0;
+}
+
+/* Only the key is frame-scoped; the element lives until map teardown. */
+SEC("?raw_tp")
+__success
+int array_park_map_value(void *ctx)
+{
+	struct value_ctx c = {};
+
+	bpf_for_each_map_elem(&map, park_value_cb, &c, 0);
+	if (c.value)
+		return *c.value;
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";

19/3 2026/10/09 22:50 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 17840
							output: 38
							thoughts: 1247
						

Thoughts:
**Analyzing a BPF Verifier Patch for KMSAN Fuzzing Needs**

Okay, let's dive into this BPF patch and figure out if it warrants a dedicated KMSAN fuzzing session. My initial thought is to understand the changes first, and then zero in on potential uninitialized memory risks.

Alright, the patch modifies the BPF verifier, specifically touching `verifier.c`, `arraymap.c`, and a few header files. It looks like `map_set_for_each_callback_args` has gained a `struct bpf_verifier_state *state` parameter. Hmm, it seems like `REF_TYPE_FRAME` has been added. And `arraymap.c` is calling this new function `mark_frame_scoped_arg`. Interesting... this relates to callbacks. So, `diagnostics.c` and `.h` are modified with a `BPF_DIAG_MOD_FRAME_RELEASE`. And there is a function to release frame references, and check for leaks.

Let's zoom out and grasp the bigger picture. This seems to be about addressing a use-after-free or out-of-bounds access issue within BPF programs. Essentially, the patch is guarding against a BPF program storing a pointer to a kernel stack variable or a map and then accessing it after the callback's scope. So, if a BPF program callback is passed a stack-allocated variable from the caller (e.g., `bpf_for_each_array_elem`), and the callback saves a pointer to this stack variable, there is a risk of accessing freed or overwritten stack memory. The verifier aims to prevent this.

So, this patch appears to be implementing a safeguard within the BPF verifier to prevent a BPF program from retaining pointers to short-lived kernel stack variables. Since this is an existing problem that has been fixed, I don't think it *introduces* a new uninitialized memory risk in the kernel. But it does handle uninitialized memory reads (perhaps a double free, or a use-after-free).

Let's investigate the changes to the verifier code itself, to make sure.

Okay, let's start with `verifier.c`. The `acquire_reference_state` function... I'm looking at it... It creates a `struct bpf_reference_state` ref. Okay, is `ref` fully initialized? What are the fields? I need to look at `struct bpf_reference_state` to make sure it is zeroed, and nothing is left uninitialized.



20/3 2026/10/09 22:51 tool read-file
0m Args:
{
  "File": "include/linux/bpf_verifier.h",
  "FirstLine": 270,
  "LineCount": 40
}

Results:
{
  "Contents": " 270:\t\tstruct bpf_reg_state spilled_ptr;\n 271:\t\tu8 slot_type[BPF_REG_SIZE];\n 272:\t};\n 273:\t\n 274:\tstruct bpf_reference_state {\n 275:\t\tenum ref_state_type {\n 276:\t\t\tREF_TYPE_PTR\t\t= (1 \u003c\u003c 1),\n 277:\t\t\tREF_TYPE_IRQ\t\t= (1 \u003c\u003c 2),\n 278:\t\t\tREF_TYPE_LOCK\t\t= (1 \u003c\u003c 3),\n 279:\t\t\tREF_TYPE_RES_LOCK \t= (1 \u003c\u003c 4),\n 280:\t\t\tREF_TYPE_RES_LOCK_IRQ\t= (1 \u003c\u003c 5),\n 281:\t\t\tREF_TYPE_FRAME\t\t= (1 \u003c\u003c 6),\n 282:\t\t\tREF_TYPE_LOCK_MASK\t= REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,\n 283:\t\t} type;\n 284:\t\t/* Track each reference created with a unique id, even if the same\n 285:\t\t * instruction creates the reference multiple times (eg, via CALL).\n 286:\t\t */\n 287:\t\tint id;\n 288:\t\t/* Instruction where the allocation of this reference occurred. This\n 289:\t\t * is used purely to inform the user of a reference leak.\n 290:\t\t */\n 291:\t\tint insn_idx;\n 292:\t\tunion {\n 293:\t\t\t/* For REF_TYPE_PTR */\n 294:\t\t\tint parent_id;\n 295:\t\t\t/* Use to keep track of the source object of a lock, to ensure\n 296:\t\t\t * it matches on unlock.\n 297:\t\t\t */\n 298:\t\t\tvoid *ptr;\n 299:\t\t\t/* For REF_TYPE_FRAME */\n 300:\t\t\tu32 frameno;\n 301:\t\t};\n 302:\t};\n 303:\t\n 304:\tstruct bpf_retval_range {\n 305:\t\ts32 minval;\n 306:\t\ts32 maxval;\n 307:\t\tbool return_32bit;\n 308:\t};\n 309:\t\n"
}

21/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20269
							output: 38
							thoughts: 270
						

Thoughts:
**My Initial Thoughts on the BPF Reference State**

Okay, so I'm looking at this `struct bpf_reference_state`. It seems straightforward: we have a `type`, an `id`, an instruction index `insn_idx`, and a union for `parent_id`, `ptr`, and `frameno`. The initialization uses a struct initializer like `struct bpf_reference_state ref = { ... }`.  The important part is that this kind of designated initializer zeroes all the unspecified fields and padding which is standard C. Then we copy it using `state->refs[new_ofs] = *ref;`. Nothing too surprising there.

But wait a minute... something about this is nagging at me.  I remember seeing `bpf_mark_reg_not_init` being removed somewhere. Where was that... oh, that's right, it was in those `set_*_callback_state` functions!  Why was that removed? That feels significant, I need to understand that.

Let's dig in a bit. What's happening in `setup_func_entry`? Okay, I see:

```c
	callee = kzalloc_obj(*callee, GFP_KERNEL_ACCOUNT);
```

Aha! `kzalloc_obj`! That's the key.  `kzalloc_obj` is zero-allocating `callee`. This means the state is already initialized to zero. This likely replaces any need for a dedicated `bpf_mark_reg_not_init` function, right? This is starting to make sense now.

To make sure I'm following this correctly, let's compare the code around `setup_func_entry` before and after the patch. I need to look at lines 10840-10900 of `kernel/bpf/verifier.c` to see exactly what changed and what the implications are. This should confirm my understanding.



22/3 2026/10/09 22:51 tool read-file
0m Args:
{
  "File": "kernel/bpf/verifier.c",
  "FirstLine": 10885,
  "LineCount": 60
}

Results:
{
  "Contents": "10885:\t{\n10886:\t\tint i, nslots = state-\u003eout_stack_arg_cnt;\n10887:\t\n10888:\t\tfor (i = 0; i \u003c nslots; i++) {\n10889:\t\t\tbpf_diag_record_scrub(env, \u0026state-\u003estack_arg_regs[i], BPF_DIAG_MOD_CALLER_SAVED);\n10890:\t\t\tbpf_mark_reg_not_init(env, \u0026state-\u003estack_arg_regs[i]);\n10891:\t\t}\n10892:\t}\n10893:\t\n10894:\ttypedef int (*set_callee_state_fn)(struct bpf_verifier_env *env,\n10895:\t\t\t\t\t   struct bpf_verifier_state *state,\n10896:\t\t\t\t\t   struct bpf_func_state *caller,\n10897:\t\t\t\t\t   struct bpf_func_state *callee,\n10898:\t\t\t\t\t   int insn_idx);\n10899:\t\n10900:\tstatic struct bpf_func_state *setup_func_entry(struct bpf_verifier_env *env, int subprog,\n10901:\t\t\t\t\t\t       int callsite, struct bpf_verifier_state *state)\n10902:\t{\n10903:\t\tstruct bpf_func_state *callee;\n10904:\t\n10905:\t\tif (state-\u003ecurframe + 1 \u003e= MAX_CALL_FRAMES) {\n10906:\t\t\tverbose(env, \"the call stack of %d frames is too deep\\n\",\n10907:\t\t\t\tstate-\u003ecurframe + 2);\n10908:\t\t\treturn ERR_PTR(-E2BIG);\n10909:\t\t}\n10910:\t\n10911:\t\tif (state-\u003eframe[state-\u003ecurframe + 1]) {\n10912:\t\t\tverifier_bug(env, \"Frame %d already allocated\", state-\u003ecurframe + 1);\n10913:\t\t\treturn ERR_PTR(-EFAULT);\n10914:\t\t}\n10915:\t\n10916:\t\tcallee = kzalloc_obj(*callee, GFP_KERNEL_ACCOUNT);\n10917:\t\tif (!callee)\n10918:\t\t\treturn ERR_PTR(-ENOMEM);\n10919:\t\tstate-\u003eframe[state-\u003ecurframe + 1] = callee;\n10920:\t\n10921:\t\t/* callee cannot access r0, r6 - r9 for reading and has to write\n10922:\t\t * into its own stack before reading from it.\n10923:\t\t * callee can read/write into caller's stack\n10924:\t\t */\n10925:\t\tinit_func_state(env, callee,\n10926:\t\t\t\t/* remember the callsite, it will be used by bpf_exit */\n10927:\t\t\t\tcallsite,\n10928:\t\t\t\tstate-\u003ecurframe + 1 /* frameno within this callchain */,\n10929:\t\t\t\tsubprog /* subprog number within this prog */);\n10930:\t\tstate-\u003ecurframe++;\n10931:\t\n10932:\t\treturn callee;\n10933:\t}\n10934:\t\n10935:\tstatic void gen_subprog_arg_proto(const struct bpf_subprog_info *sub, const struct btf *btf,\n10936:\t\t\t\t\t  const struct btf_type *func_proto, struct bpf_func_proto *proto)\n10937:\t{\n10938:\t\tconst struct btf_param *args = btf_params(func_proto);\n10939:\t\tu32 arg, slot = 0;\n10940:\t\n10941:\t\tmemset(proto, 0, sizeof(*proto));\n10942:\t\tfor (arg = 0; arg \u003c btf_type_vlen(func_proto); arg++) {\n10943:\t\t\tenum bpf_arg_type arg_type = sub-\u003eargs[slot].arg_type;\n10944:\t\t\tconst struct btf_type *t;\n"
}

23/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22103
							output: 35
							thoughts: 14
						

Thoughts:
Okay, let's dive into this code.

**Diving into `init_func_state`**

Alright, I'm going to take a look at this `init_func_state` function. My expectation is that, given its name, it’s responsible for initializing some form of internal state associated with a function. Depending on the context, this could be anything from a simple counter or flag to a complex data structure that holds intermediate results or persistent information specific to a given function's execution. Knowing the specific data structures involved would make this clearer, of course. I'll need to scrutinize how it's being used to understand its full impact on the overall architecture. I anticipate it likely interacts with the function's scope, potentially managing variables within the environment, which is not something I'd necessarily do if this was a purely functional system. Depending on the language, this is likely a wrapper of sorts.



24/3 2026/10/09 22:51 tool grepper
0m Args:
{
  "Expression": "void init_func_state",
  "PathPrefixes": [
    "kernel/bpf/verifier.c"
  ]
}

Results:
{
  "Output": "kernel/bpf/verifier.c=2414=static struct bpf_retval_range retval_range(s32 minval, s32 maxval)\n--\nkernel/bpf/verifier.c-2422-\nkernel/bpf/verifier.c:2423:static void init_func_state(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-2424-\t\t\t    struct bpf_func_state *state,\n"
}

25/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22473
							output: 37
							thoughts: 22
						

Thoughts:
Here's a first-person summary, tailored for an expert audience, of the provided text, focusing on the thought process:

**Diving into init\_func\_state in kernel/bpf/verifier.c**

Okay, let's get down to brass tacks. I need to understand how the BPF verifier handles the initial state of a function. The text points me directly to `init_func_state` in `kernel/bpf/verifier.c`. My thinking is, this is where it all begins. It’s the foundational point where the verifier sets up its understanding of the code.

I expect this function is responsible for analyzing the input arguments, probably checking their types, and setting up the initial registers and stack frame. It might be looking at what's passed in from user space and defining how the execution will "start". It’s likely going to build the initial *abstract* machine state. Crucially, I imagine it also sets some basic rules and bounds. Any program needs a safe starting point. This is probably where that happens. I anticipate that it uses the information in the BPF program itself, alongside any provided input arguments, to define this state. The way that state is *initialized* will provide a core framework for checking it later on.



26/3 2026/10/09 22:51 tool read-file
0m Args:
{
  "File": "kernel/bpf/verifier.c",
  "FirstLine": 2422,
  "LineCount": 25
}

Results:
{
  "Contents": "2422:\t\n2423:\tstatic void init_func_state(struct bpf_verifier_env *env,\n2424:\t\t\t\t    struct bpf_func_state *state,\n2425:\t\t\t\t    int callsite, int frameno, int subprogno)\n2426:\t{\n2427:\t\tstate-\u003ecallsite = callsite;\n2428:\t\tstate-\u003eframeno = frameno;\n2429:\t\tbpf_diag_init_frame(env, state);\n2430:\t\tstate-\u003esubprogno = subprogno;\n2431:\t\tstate-\u003ecallback_ret_range = retval_range(0, 0);\n2432:\t\tinit_reg_state(env, state);\n2433:\t\tmark_verifier_state_scratched(env);\n2434:\t}\n2435:\t\n2436:\t/* Similar to push_stack(), but for async callbacks */\n2437:\tstatic struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env,\n2438:\t\t\t\t\t\t\tint insn_idx, int prev_insn_idx,\n2439:\t\t\t\t\t\t\tint subprog, bool is_sleepable)\n2440:\t{\n2441:\t\tstruct bpf_verifier_stack_elem *elem;\n2442:\t\tstruct bpf_func_state *frame;\n2443:\t\n2444:\t\telem = kzalloc_obj(struct bpf_verifier_stack_elem, GFP_KERNEL_ACCOUNT);\n2445:\t\tif (!elem)\n2446:\t\t\treturn ERR_PTR(-ENOMEM);\n"
}

27/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23232
							output: 35
							thoughts: 14
						

Thoughts:
Okay, let's dive into this. Here's my take on understanding this code:

**Analyzing the Initialization of the Register State**

Alright, let's get down to brass tacks. My initial thought is, "Let's crack open this `init_reg_state` function and see what's actually happening under the hood." I need to understand exactly how the register state, presumably the core of the computation or control flow, is being initialized. What values are being set, what data structures are being populated? Are we talking zeroing out registers, setting up initial parameters, or something more complex? I need to build a mental model of this critical first step. This is where the whole process *begins*, and I need to understand it fully to ensure correct behavior down the line. It's the foundation – a bad foundation leads to a collapse, and I need a strong base.



28/3 2026/10/09 22:51 tool grepper
0m Args:
{
  "Expression": "void init_reg_state",
  "PathPrefixes": [
    "kernel/bpf/verifier.c"
  ]
}

Results:
{
  "Output": "kernel/bpf/verifier.c=2369=static int mark_btf_ld_reg(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-2397-\nkernel/bpf/verifier.c:2398:static void init_reg_state(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-2399-\t\t\t   struct bpf_func_state *state)\n"
}

29/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23594
							output: 37
							thoughts: 11
						

Thoughts:
Okay, here's my interpretation of "Let's read `init_reg_state`", expressed as a first-person thought process, keeping in mind I'm dealing with someone who's likely very familiar with the technical context:

**Analyzing `init_reg_state` - A Deep Dive**

Alright, the objective is to understand the operation of the function `init_reg_state`. I can only assume that this function initializes the register state, but of course that is just a basic assumption.

I need to quickly grasp its core function. I'm going to focus on its key operations and how it interacts with the underlying hardware or the broader system architecture. Is it zeroing registers, setting specific control bits, loading initial values from memory, or a combination of these? The details are crucial. Are there any potential optimizations in its implementation? Are any registers being initialised with values taken from external inputs?

I'll be looking for any hints about the register set being manipulated. Is this a general-purpose register initialization, or is it specific to a particular subsystem or hardware component? Understanding the context is paramount for effective debugging and optimization later. I need to note any dependencies this initialization has on other parts of the system and make sure there are no concurrency issues.

My immediate focus will be on identifying the purpose of the register initialisation and assessing the function's impact on overall system behavior. I hope it is all well documented.



30/3 2026/10/09 22:51 tool read-file
0m Args:
{
  "File": "kernel/bpf/verifier.c",
  "FirstLine": 2397,
  "LineCount": 25
}

Results:
{
  "Contents": "2397:\t\n2398:\tstatic void init_reg_state(struct bpf_verifier_env *env,\n2399:\t\t\t\t   struct bpf_func_state *state)\n2400:\t{\n2401:\t\tstruct bpf_reg_state *regs = state-\u003eregs;\n2402:\t\tint i;\n2403:\t\n2404:\t\tfor (i = 0; i \u003c MAX_BPF_REG; i++) {\n2405:\t\t\tbpf_mark_reg_not_init(env, \u0026regs[i]);\n2406:\t\t}\n2407:\t\n2408:\t\t/* frame pointer */\n2409:\t\tregs[BPF_REG_FP].type = PTR_TO_STACK;\n2410:\t\tmark_reg_known_zero(env, regs, BPF_REG_FP);\n2411:\t\tregs[BPF_REG_FP].frameno = state-\u003eframeno;\n2412:\t}\n2413:\t\n2414:\tstatic struct bpf_retval_range retval_range(s32 minval, s32 maxval)\n2415:\t{\n2416:\t\t/*\n2417:\t\t * return_32bit is set to false by default and set explicitly\n2418:\t\t * by the caller when necessary.\n2419:\t\t */\n2420:\t\treturn (struct bpf_retval_range){ minval, maxval, false };\n2421:\t}\n"
}

31/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24341
							output: 36
							thoughts: 639
						

Thoughts:
**Analyzing the Removal of Redundant BPF Initialization Calls**

Okay, so I'm looking at this patch and my initial thought is, "Ah, finally cleaning up some redundant code in the BPF state management." It seems that `init_reg_state` is *already* calling `bpf_mark_reg_not_init` for all registers from 0 to `MAX_BPF_REG` (except the frame pointer), and `state->callback_ret_range = retval_range(0, 0);` sets the default return range. This means the subsequent calls to `bpf_mark_reg_not_init` in those `set_*_callback_state` functions, specifically for registers like BPF_REG_4 and BPF_REG_5, were just unnecessary repetition. The same logic applied to the return value, the calls were redundant because of the default initialization. So, the patch removes those redundant calls, which makes sense from a code clarity perspective.

Now, as an expert in this, I need to make sure this change doesn't introduce any new security risks, specifically uninitialized memory leaks. That's my immediate concern. I need to run through the four KMSAN criteria:

1.  **Kernel structures to userspace:** I don't see any obvious changes that would directly send uninitialized kernel structures to userspace via the methods listed (copy\_to\_user, etc.). Still need to keep an eye on this.
2.  **Conditional logic and uninitialized variables:** The code removal itself seems straightforward, but I have to carefully examine the surrounding logic to ensure that any conditional branching isn't relying on uninitialized values because of this change.
3.  **Complex data structure initialization:** The patch touches `struct bpf_reference_state`. I need to verify that uninitialized fields in that structure aren't being read later. I'll need to study `refsafe` to confirm no new vulnerabilities exist.
4.  **Bounds checks and uninitialized bytes:** I don't see anything obvious in terms of bounds checks or logic that would allow access to uninitialized bytes of existing buffers as a direct result of this.

Specifically, I need to focus on that `struct bpf_reference_state` and its `union` to see if there is a risk of something being used before it is initialised. I should inspect `refsafe` in `kernel/bpf/states.c` to be sure. I'm going to carefully read the surrounding code, especially anything that uses `bpf_reference_state`, to ensure there are no uninitialized reads now that these calls are gone.



32/3 2026/10/09 22:51 tool read-file
0m Args:
{
  "File": "kernel/bpf/states.c",
  "FirstLine": 890,
  "LineCount": 40
}

Results:
{
  "Contents": " 890:\t\n 891:\t\tfor (i = 0; i \u003c old-\u003eacquired_refs; i++) {\n 892:\t\t\tif (!check_ids(old-\u003erefs[i].id, cur-\u003erefs[i].id, idmap) ||\n 893:\t\t\t    old-\u003erefs[i].type != cur-\u003erefs[i].type)\n 894:\t\t\t\treturn false;\n 895:\t\t\tswitch (old-\u003erefs[i].type) {\n 896:\t\t\tcase REF_TYPE_PTR:\n 897:\t\t\t\tif (!check_ids(old-\u003erefs[i].parent_id, cur-\u003erefs[i].parent_id, idmap))\n 898:\t\t\t\t\treturn false;\n 899:\t\t\t\tbreak;\n 900:\t\t\tcase REF_TYPE_IRQ:\n 901:\t\t\t\tbreak;\n 902:\t\t\tcase REF_TYPE_FRAME:\n 903:\t\t\t\tif (old-\u003erefs[i].frameno != cur-\u003erefs[i].frameno)\n 904:\t\t\t\t\treturn false;\n 905:\t\t\t\tbreak;\n 906:\t\t\tcase REF_TYPE_LOCK:\n 907:\t\t\tcase REF_TYPE_RES_LOCK:\n 908:\t\t\tcase REF_TYPE_RES_LOCK_IRQ:\n 909:\t\t\t\tif (old-\u003erefs[i].ptr != cur-\u003erefs[i].ptr)\n 910:\t\t\t\t\treturn false;\n 911:\t\t\t\tbreak;\n 912:\t\t\tdefault:\n 913:\t\t\t\tWARN_ONCE(1, \"Unhandled enum type for reference state: %d\\n\", old-\u003erefs[i].type);\n 914:\t\t\t\treturn false;\n 915:\t\t\t}\n 916:\t\t}\n 917:\t\n 918:\t\treturn true;\n 919:\t}\n 920:\t\n 921:\t/* compare two verifier states\n 922:\t *\n 923:\t * all states stored in state_list are known to be valid, since\n 924:\t * verifier reached 'bpf_exit' instruction through them\n 925:\t *\n 926:\t * this function is called when verifier exploring different branches of\n 927:\t * execution popped from the state stack. If it sees an old state that has\n 928:\t * more strict register state and more strict stack state then this execution\n 929:\t * branch doesn't need to be explored further, since verifier already\n"
}

33/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26206
							output: 37
							thoughts: 222
						

Thoughts:
**Analyzing Reference State in `refsafe`**

Okay, so I'm digging into this `refsafe` code, specifically focusing on how it handles frame references. My initial observation is that if a reference type is `REF_TYPE_FRAME`, the code checks if `old->refs[i].frameno != cur->refs[i].frameno`. The key here is to understand how `frameno` is set, and where. 

Now, looking at `acquire_frame_reference`, it seems pretty straightforward: `frameno` is explicitly initialized and passed as an argument to the state. This function is the single place which initializes the `frameno` field to its value for a reference.

This has me wondering: what happens when we copy or clone these states? Does that copying process, either through state pruning or state cloning, properly duplicate the `refs` array, including the crucial `frameno` value? That is what I need to check. To answer this, I'll need to scrutinize the `copy_verifier_state` and `resize_reference_state` functions to see if they handle this part correctly. It's a critical detail for maintaining the integrity of these frame references, and I want to make sure the state is properly maintained.



34/3 2026/10/09 22:51 tool grepper
0m Args:
{
  "Expression": "resize_reference_state|copy_reference_state",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/verifier.c=1381=static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)\n--\nkernel/bpf/verifier.c-1403-\nkernel/bpf/verifier.c:1404:static int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf_verifier_state *src)\nkernel/bpf/verifier.c-1405-{\n--\nkernel/bpf/verifier.c=1421=static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src)\n--\nkernel/bpf/verifier.c-1445-\nkernel/bpf/verifier.c:1446:static int resize_reference_state(struct bpf_verifier_state *state, size_t n)\nkernel/bpf/verifier.c-1447-{\n--\nkernel/bpf/verifier.c=1504=static int acquire_reference_state(struct bpf_verifier_state *state,\n--\nkernel/bpf/verifier.c-1509-\nkernel/bpf/verifier.c:1510:\terr = resize_reference_state(state, state-\u003eacquired_refs + 1);\nkernel/bpf/verifier.c-1511-\tif (err)\n--\nkernel/bpf/verifier.c=1770=int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n--\nkernel/bpf/verifier.c-1789-\t}\nkernel/bpf/verifier.c:1790:\terr = copy_reference_state(dst_state, src);\nkernel/bpf/verifier.c-1791-\tif (err)\n"
}

35/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27196
							output: 37
							thoughts: 19
						

Thoughts:
Here's my take on those thoughts:

**Checking Key Operations**

Okay, so I'm thinking we need to quickly verify a couple of critical operations within this state management process. The first one is `copy_reference_state` – I want to make absolutely certain that the data duplication is functioning as expected. Are we creating a true, independent copy, or are we inadvertently introducing shared references that could lead to unexpected side effects down the line? It's crucial this stage is rock solid.

Then, I'm turning my attention to `resize_reference_state`. This is another point where we could have some subtle but devastating issues if we're not careful. I need to confirm the resizing logic works flawlessly, and we aren’t triggering any memory corruption or data loss in the process. Are we correctly handling boundary conditions? Are we appropriately updating internal pointers and structures? A thorough validation of both of these procedures is an essential next step.



36/3 2026/10/09 22:51 tool read-file
0m Args:
{
  "File": "kernel/bpf/verifier.c",
  "FirstLine": 1403,
  "LineCount": 45
}

Results:
{
  "Contents": "1403:\t\n1404:\tstatic int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf_verifier_state *src)\n1405:\t{\n1406:\t\tdst-\u003erefs = copy_array(dst-\u003erefs, src-\u003erefs, src-\u003eacquired_refs,\n1407:\t\t\t\t       sizeof(struct bpf_reference_state), GFP_KERNEL_ACCOUNT);\n1408:\t\tif (!dst-\u003erefs)\n1409:\t\t\treturn -ENOMEM;\n1410:\t\n1411:\t\tdst-\u003eacquired_refs = src-\u003eacquired_refs;\n1412:\t\tdst-\u003eactive_locks = src-\u003eactive_locks;\n1413:\t\tdst-\u003eactive_preempt_locks = src-\u003eactive_preempt_locks;\n1414:\t\tdst-\u003eactive_rcu_locks = src-\u003eactive_rcu_locks;\n1415:\t\tdst-\u003eactive_irq_id = src-\u003eactive_irq_id;\n1416:\t\tdst-\u003eactive_lock_id = src-\u003eactive_lock_id;\n1417:\t\tdst-\u003eactive_lock_ptr = src-\u003eactive_lock_ptr;\n1418:\t\treturn 0;\n1419:\t}\n1420:\t\n1421:\tstatic int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src)\n1422:\t{\n1423:\t\tsize_t n = bpf_stack_nr_slots(src);\n1424:\t\n1425:\t\tdst-\u003estack = copy_array(dst-\u003estack, src-\u003estack, n, sizeof(struct bpf_stack_state),\n1426:\t\t\t\t\tGFP_KERNEL_ACCOUNT);\n1427:\t\tif (!dst-\u003estack)\n1428:\t\t\treturn -ENOMEM;\n1429:\t\n1430:\t\tdst-\u003eallocated_stack = src-\u003eallocated_stack;\n1431:\t\n1432:\t\t/* copy stack args state */\n1433:\t\tn = src-\u003eout_stack_arg_cnt;\n1434:\t\tif (n) {\n1435:\t\t\tdst-\u003estack_arg_regs = copy_array(dst-\u003estack_arg_regs, src-\u003estack_arg_regs, n,\n1436:\t\t\t\t\t\t\t sizeof(struct bpf_reg_state),\n1437:\t\t\t\t\t\t\t GFP_KERNEL_ACCOUNT);\n1438:\t\t\tif (!dst-\u003estack_arg_regs)\n1439:\t\t\t\treturn -ENOMEM;\n1440:\t\t}\n1441:\t\n1442:\t\tdst-\u003eout_stack_arg_cnt = src-\u003eout_stack_arg_cnt;\n1443:\t\treturn 0;\n1444:\t}\n1445:\t\n1446:\tstatic int resize_reference_state(struct bpf_verifier_state *state, size_t n)\n1447:\t{\n"
}

37/3 2026/10/09 22:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28212
							output: 37
							thoughts: 2923
						

Thoughts:
**BPF Verifier Enhancement: Analysis for KMSAN Necessity**

Okay, so I've been dissecting this patch and its implications. My primary concern, as always, is whether it introduces any risk of uninitialized memory usage or information leaks that would warrant a dedicated KMSAN fuzzing session. I'm operating under the assumption that standard KASAN and other kernel debuggers are already in place, so I'm focusing specifically on KMSAN's niche: detecting reads of uninitialized memory.

The core of this patch seems to be about tightening the BPF verifier's checks around the lifetimes of data, particularly when dealing with callbacks and stack frames. It appears we're preventing BPF programs from accessing memory that's gone out of scope – a crucial security measure. Specifically, the patch addresses scenarios where a BPF program might try to read a "parked" key or dynptr *after* the callback function that "parked" it has returned and its associated stack frame has been released. This is exactly the kind of situation that could lead to kernel stack use-after-scope/use-after-free vulnerabilities. The verifier now rejects programs that could cause such behavior.

My initial examination focused on the introduced code itself. I'm running through the newly added functions, variable declarations, and modifications meticulously. Specifically, `mark_map_callback_reg`, `invalidate_reference`, `release_frame_references`, `mark_frame_scoped_arg`, and the inlined `setup_func_entry` logic. I've confirmed that all local variables, data structures are either initialized directly or allocated and initialized via `kzalloc_obj` and `init_func_state`. There's no obvious introduction of uninitialized memory in the straightforward sense. The code is carefully constructed to ensure that registers and associated metadata are correctly initialized and tracked during verifier execution.

I've also zeroed in on the diagnostic aspects, specifically `bpf_diag_record_scrub` and `bpf_diag_record_ref_acquire`, to ensure the verifier's reporting doesn't leak uninitialized data. However, there are no references or any potential issues here.

Looking at the patch holistically, I don't see any changes that introduce uninitialized memory risks or modify existing data structures in a way that could lead to information leaks. The change is focused on *preventing* such problems by strengthening the verifier. It doesn't introduce any new kernel structures sent to user space, it is not predicated on uninitialized variables, it doesn't modify memory bounds to introduce vulnerabilities, and the data structures are initialized. It is a bug fix in the verifier, so it does not expose or modify existing code in an unsafe way, but rather in a safer way. My assessment is that this patch primarily mitigates potential vulnerabilities detectable by standard KASAN/UBSAN/etc., and does not expose new risks for KMSAN to detect. Therefore, a dedicated KMSAN fuzzing session is not justified.



38/3 2026/10/09 22:52 tool grepper
0m Args:
{
  "Expression": "BPF_DIAG_MOD_FRAME_RELEASE",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/diagnostics.c=2216=static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_history_event *event)\n--\nkernel/bpf/diagnostics.c-2240-\t\tbreak;\nkernel/bpf/diagnostics.c:2241:\tcase BPF_DIAG_MOD_FRAME_RELEASE:\nkernel/bpf/diagnostics.c-2242-\t\treason = \"the callback that owned this value returned\";\n--\nkernel/bpf/diagnostics.h=20=enum bpf_diag_mod_reason {\n--\nkernel/bpf/diagnostics.h-24-\tBPF_DIAG_MOD_REF_RELEASE,\nkernel/bpf/diagnostics.h:25:\tBPF_DIAG_MOD_FRAME_RELEASE,\nkernel/bpf/diagnostics.h-26-\tBPF_DIAG_MOD_PKT_DATA_CHANGE,\n--\nkernel/bpf/verifier.c=10792=static int release_frame_references(struct bpf_verifier_env *env, u32 frameno)\n--\nkernel/bpf/verifier.c-10804-\t\trelease_reference_state(state, i);\nkernel/bpf/verifier.c:10805:\t\terr = invalidate_reference(env, id, BPF_DIAG_MOD_FRAME_RELEASE);\nkernel/bpf/verifier.c-10806-\t\tif (err)\n"
}

39/3 2026/10/09 22:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 32184
							output: 35
							thoughts: 214
						

Thoughts:
**Analyzing Register Initialization in BPF Callback Registration**

Okay, let's dive into this `mark_map_callback_reg` function. My primary concern here is ensuring complete initialization. I need to make sure we're not inadvertently leaving any register fields untouched, leading to potential vulnerabilities or unexpected behavior down the line.

The code snippet shows a clear process: First, the type of the register (`reg`) is set. Then, the call to `__mark_reg_known_zero(reg)` catches my attention. I need to fully understand its function. I know it resets bounds and value tracking fields to known zeros. Then we have the lines that set the `map_ptr` and `map_uid` fields to those of a `map_reg`.

Going back a step, in `init_reg_state`, I'm reminded that the `callee` registers were already marked as not initialized using `bpf_mark_reg_not_init`. So, the context is that these are being re-initialized and populated. I need to keep this context in mind as I verify `__mark_reg_known_zero`.



40/3 2026/10/09 22:52 tool grepper
0m Args:
{
  "Expression": "void __mark_reg_known_zero",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/verifier.c=990=static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n--\nkernel/bpf/verifier.c-1003-\nkernel/bpf/verifier.c:1004:static void __mark_reg_known_zero(struct bpf_reg_state *reg);\nkernel/bpf/verifier.c-1005-\n--\nkernel/bpf/verifier.c=1996=static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm)\n--\nkernel/bpf/verifier.c-2004- */\nkernel/bpf/verifier.c:2005:static void __mark_reg_known_zero(struct bpf_reg_state *reg)\nkernel/bpf/verifier.c-2006-{\n"
}

41/3 2026/10/09 22:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 32910
							output: 37
							thoughts: 14
						

Thoughts:
Okay, here's my interpretation of that.

**Analyzing the Function `__mark_reg_known_zero`**

Alright, let's dive into this `__mark_reg_known_zero` function. I see it's likely part of a larger system, probably a compiler or an optimizer, dealing with register allocation or value propagation. The name itself is pretty telling; it suggests we're attempting to *mark* a register as having a *known* value of *zero*. That's a classic optimization technique. If we can prove a register is zero, we might be able to eliminate certain instructions, simplify conditional branches, or facilitate other transformations later on. I'm immediately thinking about how this ties into data-flow analysis. Is this function setting a flag? Modifying some internal data structure that tracks register state? Or perhaps it's interacting with a constraint solver of some kind? I need to look at the context to see how it's being used to get a better sense of its purpose. The "known" part is interesting, too. It means the system is (hopefully) doing some rigorous analysis to *prove* the value is zero. It can't just be an assumption; that would lead to incorrect results. It's probably considering a variety of factors: initialization code, arithmetic operations, conditional branches where the register is assigned zero under certain conditions, and maybe even external function calls that could zero out registers. This is where the magic (or the bugs!) typically live. I need to figure out what those considerations are, to understand its correctness and limitations, specifically, where it makes the crucial determination that a register can be confidently marked as zero. I'm really curious to know what the implications are in practice. Does it have any implications on the performance, or the size of the generated code? Let's get to the code, and see.



42/3 2026/10/09 22:52 tool read-file
0m Args:
{
  "File": "kernel/bpf/verifier.c",
  "FirstLine": 2004,
  "LineCount": 15
}

Results:
{
  "Contents": "2004:\t */\n2005:\tstatic void __mark_reg_known_zero(struct bpf_reg_state *reg)\n2006:\t{\n2007:\t\t__mark_reg_known(reg, 0);\n2008:\t}\n2009:\t\n2010:\tstatic void __mark_reg_const_zero(const struct bpf_verifier_env *env, struct bpf_reg_state *reg)\n2011:\t{\n2012:\t\t__mark_reg_known(reg, 0);\n2013:\t\treg-\u003etype = SCALAR_VALUE;\n2014:\t\t/* all scalars are assumed imprecise initially (unless unprivileged,\n2015:\t\t * in which case everything is forced to be precise)\n2016:\t\t */\n2017:\t\treg-\u003eprecise = !env-\u003ebpf_capable;\n2018:\t}\n"
}

43/3 2026/10/09 22:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 33596
							output: 306
							thoughts: 1285
						

Thoughts:
**KMSAN Assessment: NeedsKMSAN = false**

Okay, let's break this down. My initial assessment is that `NeedsKMSAN` should be `false`, but let's be thorough.

First, `mark_map_callback_reg` is just a straightforward refactoring. It consolidates identical code blocks that were previously duplicated across several callback functions related to map management and state setting. It's essentially about code reuse and doesn't introduce any new memory-related risks that I can see. The refactoring involves setting types, markers, and pointers, but all the initializations are explicit and well-defined.

Next, in `check_static_func_call`, the direct assignment of register values from the caller to the callee, instead of going via a callback, is functionally equivalent. No uninitialized memory issues here. The modification eliminates a layer of indirection, which is good for clarity.

Similarly, in `push_callback_call`, the code now first calls `setup_func_entry` to create the callee and then calls `set_callee_state_cb` to set the necessary callback state. No uninitialized memory issues either. It's a reordering of operations to avoid the double call.

Memory allocation is handled using `kzalloc_obj`, which zero-initializes the allocated memory. That's a good practice, and it means that any newly allocated memory is safe from containing uninitialized data.

Then we see the changes to `struct bpf_reference_state`. The addition of `REF_TYPE_FRAME` and the `frameno` field are within a union. The struct is always initialized using designated initializers, be it `acquire_frame_reference`, `acquire_reference`, `acquire_lock_state`, or `acquire_irq_state`. This means that all members, including those not explicitly set, are implicitly zero-initialized by the C designated initializer. Therefore, there's no way uninitialized memory can leak.

Given all this, any potential bugs would relate to verifier logic, array indexing, or reference leak detection (and this is verifiable with existing checks). These are all bugs that are detected by existing techniques (KASAN, etc.) and not KMSAN-specific.

Now, let's confirm the reasoning based on the established rules:

*   The changes don't primarily risk out-of-bounds access, array overflows, NULL pointer dereferences, or use-after-free bugs (KASAN handles those).
*   All touched structures are zeroed or initialized (using `={0}`, `memset`, `kzalloc`), so no uninitialized memory risks exist.
*   The changes don't introduce uninitialized memory usage or info-leaks.

Therefore, the guidelines clearly indicate that `NeedsKMSAN = false`. It's a no-brainer. Now, let me construct the appropriate tool call to `set-results`.



44/3 2026/10/09 22:52 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch enhances the BPF verifier's lifetime tracking by introducing frame-scoped reference states (REF_TYPE_FRAME) to prevent BPF callback arguments (such as array map keys and dynptrs) from outliving the callee execution frame.\n\nFrom a memory safety and sanitizer perspective:\n1. Kernel-to-user info leaks: The patch touches only internal BPF verifier tracking state and diagnostics logging. No kernel structures or buffers copied to user space are introduced or modified.\n2. Uninitialized memory reads: All newly introduced or modified structures (such as `struct bpf_reference_state ref = { ... }` in the refactored `acquire_*` helpers) use C designated initializers which ensure zero-initialization of all fields and padding. Newly allocated callee frame states are zero-allocated using `kzalloc_obj()` and initialized via `init_func_state()`.\n3. Verifier logic risks: Any potential bugs introduced by these changes are limited to verifier state comparison/invalidation logic, reference counting, or boundary management, which would manifest as verifier rejections/acceptances or out-of-bounds/NULL dereferences fully covered by KASAN and standard kernel debugging facilities.\n\nBecause no uninitialized memory reads or leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch enhances the BPF verifier's lifetime tracking by introducing frame-scoped reference states (REF_TYPE_FRAME) to prevent BPF callback arguments (such as array map keys and dynptrs) from outliving the callee execution frame.\n\nFrom a memory safety and sanitizer perspective:\n1. Kernel-to-user info leaks: The patch touches only internal BPF verifier tracking state and diagnostics logging. No kernel structures or buffers copied to user space are introduced or modified.\n2. Uninitialized memory reads: All newly introduced or modified structures (such as `struct bpf_reference_state ref = { ... }` in the refactored `acquire_*` helpers) use C designated initializers which ensure zero-initialization of all fields and padding. Newly allocated callee frame states are zero-allocated using `kzalloc_obj()` and initialized via `init_func_state()`.\n3. Verifier logic risks: Any potential bugs introduced by these changes are limited to verifier state comparison/invalidation logic, reference counting, or boundary management, which would manifest as verifier rejections/acceptances or out-of-bounds/NULL dereferences fully covered by KASAN and standard kernel debugging facilities.\n\nBecause no uninitialized memory reads or leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)