| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/27 14:17 | flow | patch-triage |
6mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_cfg",
"bpf_compute_scc",
"bpf_insn_array_init",
"bpf_insn_array_adjust",
"bpf_free_subprog_jts",
"bpf_patch_insn_data",
"bpf_opt_remove_nops",
"bpf_convert_ctx_accesses",
"bpf_do_misc_fixups",
"bpf_insn_successors"
],
"KMSANReasoning": "The patch series refactors BPF verifier indirect jump (gotox) table handling, CFG edge bounds checking, SCC iterative DFS traversal, and instruction patching logic.\n\n1. Memory Initialization:\n- All new fields in `struct bpf_verifier_env` (`cfg.gotox_edges`, `cfg.subprog_jts_ready`) and `subprog_info` (`jt`) are zero-initialized at allocation time via `kvzalloc_obj()`.\n- `exit_idx` is explicitly initialized to `U32_MAX` in `add_subprog()` and updated in `check_subprogs()` / `add_hidden_subprog()`.\n- The new `dfs_pos` array in `bpf_compute_scc()` is allocated with `kvcalloc()`, ensuring zero-initialization.\n- In `bpf_insn_array_init()`, stale map entries from previous verification runs are explicitly reset (`jitted_off = 0` and `ips[i] = 0`).\n- Vacated slots in `subprog_info` during subprogram removal are explicitly zeroed with `memset()`.\n\n2. User-space Exposure and Info-leaks:\n- The changes operate purely on internal BPF verifier structures and CFG traversal bookkeeping. No uninitialized data or kernel structure padding is copied to user space.\n\n3. KASAN vs KMSAN Applicability:\n- Any risks introduced by these changes pertain to jump table lifetime management (use-after-free, double free in `bpf_free_subprog_jts()` or `adjust_subprog_starts_after_remove()`), out-of-bounds array accesses in CFG/SCC graph traversal, or memory leaks. These are memory safety bugs detected by KASAN, KMEMLEAK, and standard bug detectors.\n- There are no risks of reading uninitialized stack, heap, or page allocations that would warrant a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false,
"Reasoning": "The patch modifies core BPF verifier logic and data structures reachable via the bpf() system call. Key modifications include restructuring CFG indirect jump (gotox) table tracking so tables are shared per-subprogram, enforcing edge limits, adjusting DFS traversal in strongly connected component computation (bpf_compute_scc), adding instruction patching modes to preserve or shift jump targets during fixup passes, and resetting BPF instruction array map state. Fuzzing is warranted to uncover regressions or invariant violations in verification and instruction patching.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/27 14:17 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 59d38d719edf105f6b1efc007088e85fcfe8fb0a\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Sep 27 14:17:06 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 8594c8aff745d..2cf04bba16bb7 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -4289,7 +4289,7 @@ struct bpf_prog *bpf_prog_find_from_stack(void);\n int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog);\n int bpf_insn_array_ready(struct bpf_map *map);\n void bpf_insn_array_release(struct bpf_map *map);\n-void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len);\n+void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len);\n void bpf_insn_array_adjust_after_remove(struct bpf_map *map, u32 off, u32 len);\n \n #ifdef CONFIG_BPF_SYSCALL\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 646e447cebd84..c775bd7577063 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -660,7 +660,6 @@ struct bpf_insn_aux_data {\n \t\t/* remember the offset of node field within type to rewrite */\n \t\tu64 insert_off;\n \t};\n-\tstruct bpf_iarray *jt;\t/* jump table for gotox or bpf_tailcall call instruction */\n \tstruct btf_struct_meta *kptr_struct_meta;\n \tu64 map_key_state; /* constant (32 bit) key tracking for maps */\n \tint ctx_field_size; /* the ctx field size for load insn, maybe 0 */\n@@ -815,7 +814,12 @@ struct bpf_subprog_info {\n \tu32 start; /* insn idx of function entry point */\n \tu32 linfo_idx; /* The idx to the main_prog-\u003eaux-\u003elinfo */\n \tu32 postorder_start; /* The idx to the env-\u003ecfg.insn_postorder */\n-\tu32 exit_idx; /* Index of one of the BPF_EXIT instructions in this subprogram */\n+\t/*\n+\t * Index of one of the BPF_EXIT instructions in this subprogram, or\n+\t * U32_MAX when it has none.\n+\t */\n+\tu32 exit_idx;\n+\tstruct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */\n \tu16 stack_depth; /* max. stack depth used by this function */\n \tu16 stack_extra;\n \tu32 insns_total;\n@@ -1004,6 +1008,8 @@ struct bpf_verifier_env {\n \t\tint cur_stack;\n \t\t/* current position in the insn_postorder vector */\n \t\tint cur_postorder;\n+\t\tu32 gotox_edges;\n+\t\tbool subprog_jts_ready;\n \t} cfg;\n \tstruct backtrack_state bt;\n \tstruct bpf_jmp_history_entry *cur_hist_ent;\n@@ -1582,6 +1588,7 @@ bool bpf_is_throw_kfunc(struct bpf_insn *insn);\n int bpf_compute_const_regs(struct bpf_verifier_env *env);\n int bpf_prune_dead_branches(struct bpf_verifier_env *env);\n int bpf_check_cfg(struct bpf_verifier_env *env);\n+void bpf_free_subprog_jts(struct bpf_verifier_env *env);\n int bpf_compute_postorder(struct bpf_verifier_env *env);\n int bpf_compute_scc(struct bpf_verifier_env *env);\n \n@@ -1800,7 +1807,6 @@ struct bpf_kfunc_desc_tab {\n };\n \n /* Functions exported from verifier.c, used by fixups.c */\n-void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len);\n void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog);\n bool bpf_allow_tail_call_in_subprogs(struct bpf_verifier_env *env);\n bool bpf_verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm);\ndiff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c\nindex a2f84afe6f7c6..92bbb71576ae3 100644\n--- a/kernel/bpf/bpf_insn_array.c\n+++ b/kernel/bpf/bpf_insn_array.c\n@@ -199,12 +199,16 @@ int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog)\n \t\treturn -EBUSY;\n \n \t/*\n-\t * Reset all the map indexes to the original values. This is needed,\n-\t * e.g., when a replay of verification with different log level should\n-\t * be performed.\n+\t * Reset the map to its pre-verification state. The xlated and jitted\n+\t * offsets and the jitted target pointers are recomputed by the verifier\n+\t * and the JIT for this program, so any values left by a previous owner\n+\t * must be cleared here.\n \t */\n-\tfor (i = 0; i \u003c map-\u003emax_entries; i++)\n+\tfor (i = 0; i \u003c map-\u003emax_entries; i++) {\n \t\tvalues[i].xlated_off = values[i].orig_off;\n+\t\tvalues[i].jitted_off = 0;\n+\t\tinsn_array-\u003eips[i] = 0;\n+\t}\n \n \treturn 0;\n }\n@@ -228,10 +232,11 @@ void bpf_insn_array_release(struct bpf_map *map)\n {\n \tstruct bpf_insn_array *insn_array = cast_insn_array(map);\n \n-\tatomic_set(\u0026insn_array-\u003eused, 0);\n+\t/* Paired with atomic_xchg() in bpf_insn_array_init(). */\n+\tatomic_set_release(\u0026insn_array-\u003eused, 0);\n }\n \n-void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len)\n+void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len)\n {\n \tstruct bpf_insn_array *insn_array = cast_insn_array(map);\n \tint i;\n@@ -240,7 +245,7 @@ void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len)\n \t\treturn;\n \n \tfor (i = 0; i \u003c map-\u003emax_entries; i++) {\n-\t\tif (insn_array-\u003evalues[i].xlated_off \u003c= off)\n+\t\tif (insn_array-\u003evalues[i].xlated_off \u003c first)\n \t\t\tcontinue;\n \t\tif (insn_array-\u003evalues[i].xlated_off == INSN_DELETED)\n \t\t\tcontinue;\ndiff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c\nindex 0de2f634ef679..b0bd9ba951dfb 100644\n--- a/kernel/bpf/cfg.c\n+++ b/kernel/bpf/cfg.c\n@@ -9,6 +9,8 @@\n \n #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n \n+#define BPF_MAX_GOTOX_EDGES\tBPF_COMPLEXITY_LIMIT_INSNS\n+\n /* non-recursive DFS pseudo code\n * 1 procedure DFS-iterative(G,v):\n * 2 label v as discovered\n@@ -284,15 +286,17 @@ static struct bpf_iarray *jt_from_map(struct bpf_map *map)\n }\n \n /*\n- * Find and collect all maps which fit in the subprog. Return the result as one\n- * combined jump table in jt-\u003eitems (allocated with kvcalloc)\n+ * Collect the jump table of every subprogram that has one, as the combined\n+ * table of all maps whose targets land inside that subprogram. All gotox\n+ * instructions of a subprogram share the same table, so this is done in a\n+ * single pass over the maps rather than once per gotox.\n */\n-static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,\n-\t\t\t\t\t int subprog_start, int subprog_end)\n+static int __compute_subprog_jts(struct bpf_verifier_env *env)\n {\n-\tstruct bpf_iarray *jt = NULL;\n+\tstruct bpf_subprog_info *subprog;\n+\tstruct bpf_iarray *jt, *jt_cur;\n \tstruct bpf_map *map;\n-\tstruct bpf_iarray *jt_cur;\n+\tu32 old_cnt;\n \tint i;\n \n \tfor (i = 0; i \u003c env-\u003einsn_array_map_cnt; i++) {\n@@ -303,73 +307,100 @@ static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,\n \t\tmap = env-\u003einsn_array_maps[i];\n \n \t\tjt_cur = jt_from_map(map);\n-\t\tif (IS_ERR(jt_cur)) {\n-\t\t\tkvfree(jt);\n-\t\t\treturn jt_cur;\n+\t\tif (IS_ERR(jt_cur))\n+\t\t\treturn PTR_ERR(jt_cur);\n+\n+\t\tsubprog = bpf_find_containing_subprog(env, jt_cur-\u003eitems[0]);\n+\t\tif (verifier_bug_if(!subprog, env, \"no subprog contains insn %u of an insn array map\",\n+\t\t\t\t jt_cur-\u003eitems[0])) {\n+\t\t\tkvfree(jt_cur);\n+\t\t\treturn -EFAULT;\n+\t\t}\n+\t\tif (jt_cur-\u003eitems[jt_cur-\u003ecnt - 1] \u003e= (subprog + 1)-\u003estart) {\n+\t\t\tverbose(env, \"jump table of subprog starting at %u spans multiple subprogs, map id=%d\\n\",\n+\t\t\t\tsubprog-\u003estart, map-\u003eid);\n+\t\t\tbpf_diag_program_structure(\n+\t\t\t\tenv, subprog-\u003estart, \"jump table spans subprograms\",\n+\t\t\t\t\"Keep every entry of a jump table inside one subprogram.\",\n+\t\t\t\t\"The jump table in map id %d for the subprogram that starts at instruction %u reaches past its end at instruction %u.\",\n+\t\t\t\tmap-\u003eid, subprog-\u003estart, (subprog + 1)-\u003estart);\n+\t\t\tkvfree(jt_cur);\n+\t\t\treturn -EINVAL;\n \t\t}\n \n-\t\t/*\n-\t\t * This is enough to check one element. The full table is\n-\t\t * checked to fit inside the subprog later in create_jt()\n-\t\t */\n-\t\tif (jt_cur-\u003eitems[0] \u003e= subprog_start \u0026\u0026 jt_cur-\u003eitems[0] \u003c subprog_end) {\n-\t\t\tu32 old_cnt = jt ? jt-\u003ecnt : 0;\n-\t\t\tjt = bpf_iarray_realloc(jt, old_cnt + jt_cur-\u003ecnt);\n-\t\t\tif (!jt) {\n-\t\t\t\tkvfree(jt_cur);\n-\t\t\t\treturn ERR_PTR(-ENOMEM);\n-\t\t\t}\n-\t\t\tmemcpy(jt-\u003eitems + old_cnt, jt_cur-\u003eitems, jt_cur-\u003ecnt \u003c\u003c 2);\n+\t\told_cnt = subprog-\u003ejt ? subprog-\u003ejt-\u003ecnt : 0;\n+\t\tjt = bpf_iarray_realloc(subprog-\u003ejt, old_cnt + jt_cur-\u003ecnt);\n+\t\tif (!jt) {\n+\t\t\tsubprog-\u003ejt = NULL;\n+\t\t\tkvfree(jt_cur);\n+\t\t\treturn -ENOMEM;\n \t\t}\n+\t\tmemcpy(jt-\u003eitems + old_cnt, jt_cur-\u003eitems, jt_cur-\u003ecnt \u003c\u003c 2);\n+\t\tsubprog-\u003ejt = jt;\n \n \t\tkvfree(jt_cur);\n \t}\n \n-\tif (!jt) {\n-\t\tverbose(env, \"no jump tables found for subprog starting at %u\\n\", subprog_start);\n-\t\tbpf_diag_program_structure(\n-\t\t\tenv, subprog_start, \"missing jump table\",\n-\t\t\t\"Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms.\",\n-\t\t\t\"No jump table was found for the subprogram that starts at instruction %u.\",\n-\t\t\tsubprog_start);\n-\t\treturn ERR_PTR(-EINVAL);\n+\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\n+\t\tjt = env-\u003esubprog_info[i].jt;\n+\t\tif (jt)\n+\t\t\tjt-\u003ecnt = sort_insn_array_uniq(jt-\u003eitems, jt-\u003ecnt);\n \t}\n \n-\tjt-\u003ecnt = sort_insn_array_uniq(jt-\u003eitems, jt-\u003ecnt);\n-\treturn jt;\n+\tenv-\u003ecfg.subprog_jts_ready = true;\n+\treturn 0;\n+}\n+\n+/* Leave nothing behind on failure. */\n+static int compute_subprog_jts(struct bpf_verifier_env *env)\n+{\n+\tint err;\n+\n+\terr = __compute_subprog_jts(env);\n+\tif (err)\n+\t\tbpf_free_subprog_jts(env);\n+\treturn err;\n+}\n+\n+void bpf_free_subprog_jts(struct bpf_verifier_env *env)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(env-\u003esubprog_info); i++) {\n+\t\tkvfree(env-\u003esubprog_info[i].jt);\n+\t\tenv-\u003esubprog_info[i].jt = NULL;\n+\t}\n+\tenv-\u003ecfg.subprog_jts_ready = false;\n }\n \n static struct bpf_iarray *\n-create_jt(int t, struct bpf_verifier_env *env)\n+subprog_jt(int t, struct bpf_verifier_env *env)\n {\n \tstruct bpf_subprog_info *subprog;\n-\tint subprog_start, subprog_end;\n-\tstruct bpf_iarray *jt;\n-\tint i;\n+\tint subprog_start, err;\n+\n+\tif (!env-\u003ecfg.subprog_jts_ready) {\n+\t\terr = compute_subprog_jts(env);\n+\t\tif (err)\n+\t\t\treturn ERR_PTR(err);\n+\t}\n \n \tsubprog = bpf_find_containing_subprog(env, t);\n+\tif (verifier_bug_if(!subprog, env, \"no subprog contains insn %d\", t))\n+\t\treturn ERR_PTR(-EFAULT);\n \tsubprog_start = subprog-\u003estart;\n-\tsubprog_end = (subprog + 1)-\u003estart;\n-\tjt = jt_from_subprog(env, subprog_start, subprog_end);\n-\tif (IS_ERR(jt))\n-\t\treturn jt;\n \n-\t/* Check that the every element of the jump table fits within the given subprogram */\n-\tfor (i = 0; i \u003c jt-\u003ecnt; i++) {\n-\t\tif (jt-\u003eitems[i] \u003c subprog_start || jt-\u003eitems[i] \u003e= subprog_end) {\n-\t\t\tverbose(env, \"jump table for insn %d points outside of the subprog [%u,%u]\\n\",\n-\t\t\t\t\tt, subprog_start, subprog_end);\n-\t\t\tbpf_diag_program_structure(\n-\t\t\t\tenv, t, \"jump table target out of range\",\n-\t\t\t\t\"Keep every jump-table target inside the same subprogram.\",\n-\t\t\t\t\"The jump table for instruction %d points outside subprogram range [%u,%u).\",\n-\t\t\t\tt, subprog_start, subprog_end);\n-\t\t\tkvfree(jt);\n-\t\t\treturn ERR_PTR(-EINVAL);\n-\t\t}\n+\tif (!subprog-\u003ejt) {\n+\t\tverbose(env, \"no jump tables found for subprog starting at %u\\n\", subprog_start);\n+\t\tbpf_diag_program_structure(\n+\t\t\tenv, subprog_start, \"missing jump table\",\n+\t\t\t\"Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms.\",\n+\t\t\t\"No jump table was found for the subprogram that starts at instruction %u.\",\n+\t\t\tsubprog_start);\n+\t\treturn ERR_PTR(-EINVAL);\n \t}\n \n-\treturn jt;\n+\treturn subprog-\u003ejt;\n }\n \n /* \"conditional jump with N edges\" */\n@@ -381,27 +412,39 @@ static int visit_gotox_insn(int t, struct bpf_verifier_env *env)\n \tstruct bpf_iarray *jt;\n \tint i, w;\n \n-\tjt = env-\u003einsn_aux_data[t].jt;\n-\tif (!jt) {\n-\t\tjt = create_jt(t, env);\n-\t\tif (IS_ERR(jt))\n-\t\t\treturn PTR_ERR(jt);\n+\t/*\n+\t * The first visit pushes every target that still needs exploring, and\n+\t * those are all done by the time this insn is revisited, so there is\n+\t * nothing left to walk the jump table for.\n+\t */\n+\tif (insn_state[t] \u0026 BRANCH)\n+\t\treturn DONE_EXPLORING;\n \n-\t\tenv-\u003einsn_aux_data[t].jt = jt;\n+\tjt = subprog_jt(t, env);\n+\tif (IS_ERR(jt))\n+\t\treturn PTR_ERR(jt);\n+\n+\tinsn_state[t] |= BRANCH;\n+\n+\tif (check_add_overflow(env-\u003ecfg.gotox_edges, jt-\u003ecnt,\n+\t\t\t \u0026env-\u003ecfg.gotox_edges) ||\n+\t env-\u003ecfg.gotox_edges \u003e BPF_MAX_GOTOX_EDGES) {\n+\t\tverbose(env, \"number of indirect jump edges in the program exceeds %u\\n\",\n+\t\t\tBPF_MAX_GOTOX_EDGES);\n+\t\tbpf_diag_program_structure(\n+\t\t\tenv, t, \"too many indirect jump edges\",\n+\t\t\t\"Reduce the number of indirect jumps, or the number of distinct targets they can reach.\",\n+\t\t\t\"The program has more than %u indirect jump edges in total, counted over every gotox instruction.\",\n+\t\t\tBPF_MAX_GOTOX_EDGES);\n+\t\treturn -E2BIG;\n \t}\n \n \tmark_prune_point(env, t);\n \tfor (i = 0; i \u003c jt-\u003ecnt; i++) {\n \t\tw = jt-\u003eitems[i];\n-\t\tif (w \u003c 0 || w \u003e= env-\u003eprog-\u003elen) {\n-\t\t\tverbose(env, \"indirect jump out of range from insn %d to %d\\n\", t, w);\n-\t\t\tbpf_diag_program_structure(\n-\t\t\t\tenv, t, \"indirect jump out of range\",\n-\t\t\t\t\"Keep indirect jump targets inside the program.\",\n-\t\t\t\t\"Instruction %d can jump indirectly to instruction %d, but the program only contains instructions 0 through %d.\",\n-\t\t\t\tt, w, env-\u003eprog-\u003elen - 1);\n-\t\t\treturn -EINVAL;\n-\t\t}\n+\t\tif (verifier_bug_if(w \u003c 0 || w \u003e= env-\u003eprog-\u003elen, env,\n+\t\t\t\t \"indirect jump from insn %d to %d out of range\", t, w))\n+\t\t\treturn -EFAULT;\n \n \t\tmark_jmp_point(env, w);\n \t\tmark_jump_target(env, w);\n@@ -491,30 +534,6 @@ static int visit_func_ptrs_insn(int t, struct bpf_verifier_env *env,\n \treturn DONE_EXPLORING;\n }\n \n-/*\n- * Instructions that can abnormally return from a subprog (tail_call\n- * upon success, ld_{abs,ind} upon load failure) have a hidden exit\n- * that the verifier must account for.\n- */\n-static int visit_abnormal_return_insn(struct bpf_verifier_env *env, int t)\n-{\n-\tstruct bpf_subprog_info *subprog;\n-\tstruct bpf_iarray *jt;\n-\n-\tif (env-\u003einsn_aux_data[t].jt)\n-\t\treturn 0;\n-\n-\tjt = bpf_iarray_realloc(NULL, 2);\n-\tif (!jt)\n-\t\treturn -ENOMEM;\n-\n-\tsubprog = bpf_find_containing_subprog(env, t);\n-\tjt-\u003eitems[0] = t + 1;\n-\tjt-\u003eitems[1] = subprog-\u003eexit_idx;\n-\tenv-\u003einsn_aux_data[t].jt = jt;\n-\treturn 0;\n-}\n-\n /* Visits the instruction at index t and returns one of the following:\n * \u003c 0 - an error occurred\n * DONE_EXPLORING - the instruction was fully explored\n@@ -537,13 +556,6 @@ static int visit_insn(int t, struct bpf_verifier_env *env)\n \t/* All non-branch instructions have a single fall-through edge. */\n \tif (BPF_CLASS(insn-\u003ecode) != BPF_JMP \u0026\u0026\n \t BPF_CLASS(insn-\u003ecode) != BPF_JMP32) {\n-\t\tif (BPF_CLASS(insn-\u003ecode) == BPF_LD \u0026\u0026\n-\t\t (BPF_MODE(insn-\u003ecode) == BPF_ABS ||\n-\t\t BPF_MODE(insn-\u003ecode) == BPF_IND)) {\n-\t\t\tret = visit_abnormal_return_insn(env, t);\n-\t\t\tif (ret)\n-\t\t\t\treturn ret;\n-\t\t}\n \t\tinsn_sz = bpf_is_ldimm64(insn) ? 2 : 1;\n \t\treturn push_insn(t, t + insn_sz, FALLTHROUGH, env);\n \t}\n@@ -588,11 +600,6 @@ static int visit_insn(int t, struct bpf_verifier_env *env)\n \t\t\t\tmark_subprog_might_sleep(env, t);\n \t\t\tif (bpf_helper_changes_pkt_data(insn-\u003eimm))\n \t\t\t\tmark_subprog_changes_pkt_data(env, t);\n-\t\t\tif (insn-\u003eimm == BPF_FUNC_tail_call) {\n-\t\t\t\tret = visit_abnormal_return_insn(env, t);\n-\t\t\t\tif (ret)\n-\t\t\t\t\treturn ret;\n-\t\t\t}\n \t\t} else if (insn-\u003esrc_reg == BPF_PSEUDO_KFUNC_CALL) {\n \t\t\tstruct bpf_call_arg_meta meta;\n \n@@ -825,7 +832,7 @@ int bpf_compute_scc(struct bpf_verifier_env *env)\n \tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\n \tconst u32 insn_cnt = env-\u003eprog-\u003elen;\n \tint stack_sz, dfs_sz, err = 0;\n-\tu32 *stack, *pre, *low, *dfs;\n+\tu32 *stack, *pre, *low, *dfs, *dfs_pos;\n \tu32 i, j, t, w;\n \tu32 next_preorder_num;\n \tu32 next_scc_id;\n@@ -838,13 +845,16 @@ int bpf_compute_scc(struct bpf_verifier_env *env)\n \t * - 'stack' accumulates vertices in DFS order, see invariant comment below;\n \t * - 'pre[t] == p' =\u003e preorder number of vertex 't' is 'p';\n \t * - 'low[t] == n' =\u003e smallest preorder number of the vertex reachable from 't' is 'n';\n-\t * - 'dfs' DFS traversal stack, used to emulate explicit recursion.\n+\t * - 'dfs' DFS traversal stack, used to emulate explicit recursion;\n+\t * - 'dfs_pos[k] == j' =\u003e the frame 'dfs[k]' resumes visiting its\n+\t * successors at index 'j'.\n \t */\n \tstack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);\n \tpre = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);\n \tlow = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);\n \tdfs = kvcalloc(insn_cnt, sizeof(*dfs), GFP_KERNEL_ACCOUNT);\n-\tif (!stack || !pre || !low || !dfs) {\n+\tdfs_pos = kvcalloc(insn_cnt, sizeof(*dfs_pos), GFP_KERNEL_ACCOUNT);\n+\tif (!stack || !pre || !low || !dfs || !dfs_pos) {\n \t\terr = -ENOMEM;\n \t\tgoto exit;\n \t}\n@@ -927,6 +937,7 @@ int bpf_compute_scc(struct bpf_verifier_env *env)\n \t\tstack_sz = 0;\n \t\tdfs_sz = 1;\n \t\tdfs[0] = i;\n+\t\tdfs_pos[0] = 0;\n dfs_continue:\n \t\twhile (dfs_sz) {\n \t\t\tw = dfs[dfs_sz - 1];\n@@ -936,13 +947,22 @@ int bpf_compute_scc(struct bpf_verifier_env *env)\n \t\t\t\tnext_preorder_num++;\n \t\t\t\tstack[stack_sz++] = w;\n \t\t\t}\n-\t\t\t/* Visit 'w' successors */\n+\t\t\t/* Visit remaining 'w' successors */\n \t\t\tsucc = bpf_insn_successors(env, w);\n-\t\t\tfor (j = 0; j \u003c succ-\u003ecnt; ++j) {\n+\t\t\tfor (j = dfs_pos[dfs_sz - 1]; j \u003c succ-\u003ecnt; ++j) {\n \t\t\t\tif (pre[succ-\u003eitems[j]]) {\n \t\t\t\t\tlow[w] = min(low[w], low[succ-\u003eitems[j]]);\n \t\t\t\t} else {\n-\t\t\t\t\tdfs[dfs_sz++] = succ-\u003eitems[j];\n+\t\t\t\t\t/*\n+\t\t\t\t\t * Once DFS for succ-\u003eitems[j] is complete,\n+\t\t\t\t\t * pre[succ-\u003eitems[j]] is non-zero, hence\n+\t\t\t\t\t * resuming at 'j' allows to follow the\n+\t\t\t\t\t * low[w] = min(...) update branch above.\n+\t\t\t\t\t */\n+\t\t\t\t\tdfs_pos[dfs_sz - 1] = j;\n+\t\t\t\t\tdfs_pos[dfs_sz] = 0;\n+\t\t\t\t\tdfs[dfs_sz] = succ-\u003eitems[j];\n+\t\t\t\t\tdfs_sz++;\n \t\t\t\t\tgoto dfs_continue;\n \t\t\t\t}\n \t\t\t}\n@@ -992,5 +1012,6 @@ int bpf_compute_scc(struct bpf_verifier_env *env)\n \tkvfree(pre);\n \tkvfree(low);\n \tkvfree(dfs);\n+\tkvfree(dfs_pos);\n \treturn err;\n }\ndiff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c\nindex e9c2d6c06218f..37cf130ebb57b 100644\n--- a/kernel/bpf/fixups.c\n+++ b/kernel/bpf/fixups.c\n@@ -214,13 +214,19 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,\n }\n #endif\n \n+enum bpf_patch_mode {\n+\tBPF_PATCH_KEEP_TARGET,\t/* targets of 'off' keep naming 'off', the first new insn */\n+\tBPF_PATCH_MOVE_TARGET,\t/* targets of 'off' follow the original insn to 'off + len - 1' */\n+};\n+\n /* single env-\u003eprog-\u003einsni[off] instruction was replaced with the range\n * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying\n * [0, off) and [off, end) to new locations, so the patched range stays zero\n */\n static void adjust_insn_aux_data(struct bpf_verifier_env *env,\n \t\t\t\t struct bpf_prog *new_prog, u32 off, u32 cnt,\n-\t\t\t\t struct bpf_insn *original_insn)\n+\t\t\t\t struct bpf_insn *original_insn,\n+\t\t\t\t enum bpf_patch_mode mode)\n {\n \tstruct bpf_insn_aux_data *data = env-\u003einsn_aux_data;\n \tstruct bpf_insn *insn = new_prog-\u003einsnsi;\n@@ -253,7 +259,12 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,\n \t\t/* Expand insni[off]'s seen count to the patched range. */\n \t\tdata[i].seen = old_seen;\n \t\tdata[i].zext_dst = bpf_insn_def32(new_prog, insn + i) \u003e= 0;\n-\t\tif (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {\n+\t\t/*\n+\t\t * In BPF_PATCH_MOVE_TARGET mode the original insn is the last\n+\t\t * slot by construction.\n+\t\t */\n+\t\tif (mode == BPF_PATCH_KEEP_TARGET \u0026\u0026\n+\t\t !memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {\n \t\t\tdata[i].non_stack_access =\n \t\t\t\tdata[off + cnt - 1].non_stack_access;\n \t\t\tdata[off + cnt - 1].non_stack_access = false;\n@@ -277,9 +288,10 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,\n \t * new instructions by the above memmove and memset, but the indirect jump target is\n \t * actually the first instruction, so move it back. This also matches with the behavior\n \t * of bpf_insn_array_adjust(), which preserves xlated_off to point to the first new\n-\t * instruction.\n+\t * instruction. For BPF_PATCH_MOVE_TARGET the original instruction is the last one,\n+\t * so the flag already sits where needed.\n \t */\n-\tif (data[off + cnt - 1].indirect_target) {\n+\tif (mode == BPF_PATCH_KEEP_TARGET \u0026\u0026 data[off + cnt - 1].indirect_target) {\n \t\tdata[off].indirect_target = 1;\n \t\tdata[off + cnt - 1].indirect_target = 0;\n \t}\n@@ -299,7 +311,7 @@ static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len\n \t}\n }\n \n-static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 off, u32 len)\n+static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 first, u32 len)\n {\n \tint i;\n \n@@ -307,7 +319,7 @@ static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 off, u32 len)\n \t\treturn;\n \n \tfor (i = 0; i \u003c env-\u003einsn_array_map_cnt; i++)\n-\t\tbpf_insn_array_adjust(env-\u003einsn_array_maps[i], off, len);\n+\t\tbpf_insn_array_adjust(env-\u003einsn_array_maps[i], first, len);\n }\n \n static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 off, u32 len)\n@@ -318,7 +330,7 @@ static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 of\n \t\tbpf_insn_array_adjust_after_remove(env-\u003einsn_array_maps[i], off, len);\n }\n \n-static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len)\n+static void adjust_poke_descs(struct bpf_prog *prog, u32 first, u32 len)\n {\n \tstruct bpf_jit_poke_descriptor *tab = prog-\u003eaux-\u003epoke_tab;\n \tint i, sz = prog-\u003eaux-\u003esize_poke_tab;\n@@ -326,7 +338,7 @@ static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len)\n \n \tfor (i = 0; i \u003c sz; i++) {\n \t\tdesc = \u0026tab[i];\n-\t\tif (desc-\u003einsn_idx \u003c= off)\n+\t\tif (desc-\u003einsn_idx \u003c first)\n \t\t\tcontinue;\n \t\tdesc-\u003einsn_idx += len - 1;\n \t}\n@@ -345,11 +357,14 @@ static bool bpf_rewrite_must_abort(void)\n \treturn false;\n }\n \n-struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n-\t\t\t\t const struct bpf_insn *patch, u32 len)\n+static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n+\t\t\t\t\t const struct bpf_insn *patch, u32 len,\n+\t\t\t\t\t enum bpf_patch_mode mode)\n {\n \tstruct bpf_prog *new_prog;\n \tstruct bpf_insn_aux_data *new_data = NULL;\n+\t/* The first slot whose targets move. */\n+\tu32 first = mode == BPF_PATCH_MOVE_TARGET ? off : off + 1;\n \tstruct bpf_insn original_insn;\n \n \tif (bpf_rewrite_must_abort())\n@@ -375,14 +390,20 @@ struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n \t\t\t\tenv-\u003einsn_aux_data[off].orig_idx);\n \t\treturn NULL;\n \t}\n-\tadjust_insn_aux_data(env, new_prog, off, len, \u0026original_insn);\n+\tadjust_insn_aux_data(env, new_prog, off, len, \u0026original_insn, mode);\n \tadjust_subprog_starts(env, off, len);\n-\tadjust_insn_arrays(env, off, len);\n+\tadjust_insn_arrays(env, first, len);\n \tbpf_adjust_func_ptrs(env, off, len);\n-\tadjust_poke_descs(new_prog, off, len);\n+\tadjust_poke_descs(new_prog, first, len);\n \treturn new_prog;\n }\n \n+struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n+\t\t\t\t const struct bpf_insn *patch, u32 len)\n+{\n+\treturn __bpf_patch_insn_data(env, off, patch, len, BPF_PATCH_KEEP_TARGET);\n+}\n+\n /*\n * insn was moved down by delta insns inside its own patch. Operands relative\n * to the pc that point in front of the old position did not move with it.\n@@ -480,7 +501,11 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,\n \n \tif (j \u003e i) {\n \t\tstruct bpf_prog_aux *aux = env-\u003eprog-\u003eaux;\n-\t\tint move;\n+\t\tint move, k;\n+\n+\t\t/* The removed subprogs own their jump tables. */\n+\t\tfor (k = i; k \u003c j; k++)\n+\t\t\tkvfree(env-\u003esubprog_info[k].jt);\n \n \t\t/* move fake 'exit' subprog as well */\n \t\tmove = env-\u003esubprog_cnt + 1 - j;\n@@ -488,6 +513,9 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,\n \t\tmemmove(env-\u003esubprog_info + i,\n \t\t\tenv-\u003esubprog_info + j,\n \t\t\tsizeof(*env-\u003esubprog_info) * move);\n+\t\t/* The vacated tail still holds copies of the moved entries. */\n+\t\tmemset(env-\u003esubprog_info + i + move, 0,\n+\t\t sizeof(*env-\u003esubprog_info) * (j - i));\n \t\tenv-\u003esubprog_cnt -= j - i;\n \n \t\t/* remove func_info and its aux */\n@@ -506,6 +534,9 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,\n \t\t\t * in adjust_btf_func() - no need to adjust\n \t\t\t */\n \t\t}\n+\t\t/* The exception callback is live, so it cannot sit inside the removed range. */\n+\t\tif (env-\u003eexception_callback_subprog \u003e= j)\n+\t\t\tenv-\u003eexception_callback_subprog -= j - i;\n \t} else {\n \t\t/* convert i from \"first prog to remove\" to \"first to adjust\" */\n \t\tif (env-\u003esubprog_info[i].start == off)\n@@ -583,23 +614,6 @@ static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,\n \treturn 0;\n }\n \n-/*\n- * Clean up dynamically allocated fields of aux data for instructions [start, ...]\n- */\n-void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len)\n-{\n-\tstruct bpf_insn_aux_data *aux_data = env-\u003einsn_aux_data;\n-\tint end = start + len;\n-\tint i;\n-\n-\tfor (i = start; i \u003c end; i++) {\n-\t\tif (aux_data[i].jt) {\n-\t\t\tkvfree(aux_data[i].jt);\n-\t\t\taux_data[i].jt = NULL;\n-\t\t}\n-\t}\n-}\n-\n static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)\n {\n \tstruct bpf_insn_aux_data *aux_data = env-\u003einsn_aux_data;\n@@ -612,8 +626,6 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)\n \tif (bpf_prog_is_offloaded(env-\u003eprog-\u003eaux))\n \t\tbpf_prog_offload_remove_insns(env, off, cnt);\n \n-\tbpf_clear_insn_aux_data(env, off, cnt);\n-\n \terr = bpf_remove_insns(env-\u003eprog, off, cnt);\n \tif (err)\n \t\treturn err;\n@@ -736,6 +748,7 @@ int bpf_opt_remove_dead_code(struct bpf_verifier_env *env)\n \n int bpf_opt_remove_nops(struct bpf_verifier_env *env)\n {\n+\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\n \tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n \tint insn_cnt = env-\u003eprog-\u003elen;\n \tbool is_may_goto_0, is_ja;\n@@ -747,6 +760,8 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)\n \n \t\tif (!is_may_goto_0 \u0026\u0026 !is_ja)\n \t\t\tcontinue;\n+\t\tif (aux[i].indirect_target)\n+\t\t\tcontinue;\n \n \t\terr = verifier_remove_insns(env, i, 1);\n \t\tif (err)\n@@ -889,7 +904,8 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n \t\t\tinsn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_1,\n \t\t\t\t\t\t -subprogs[0].stack_depth);\n \t\t\tinsn_buf[cnt++] = env-\u003eprog-\u003einsnsi[0];\n-\t\t\tnew_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);\n+\t\t\tnew_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,\n+\t\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n \t\t\tif (!new_prog)\n \t\t\t\treturn -ENOMEM;\n \t\t\tenv-\u003eprog = new_prog;\n@@ -912,7 +928,8 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n \t\t\tverifier_bug(env, \"prologue is too long\");\n \t\t\treturn -EFAULT;\n \t\t} else if (cnt) {\n-\t\t\tnew_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);\n+\t\t\tnew_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,\n+\t\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n \t\t\tif (!new_prog)\n \t\t\t\treturn -ENOMEM;\n \n@@ -1685,6 +1702,8 @@ static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *pat\n \tenv-\u003eprog = prog;\n \tinfo[cnt + 1].start = info[cnt].start;\n \tinfo[cnt].start = prog-\u003elen - len + 1;\n+\t/* The patch ends with the exit. */\n+\tinfo[cnt].exit_idx = prog-\u003elen - 1;\n \tenv-\u003esubprog_cnt++;\n \tenv-\u003ehidden_subprog_cnt++;\n \treturn 0;\n@@ -2670,7 +2689,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \t\t}\n \t\tcnt++;\n \n-\t\tnew_prog = bpf_patch_insn_data(env, subprog_start, insn_buf, cnt);\n+\t\tnew_prog = __bpf_patch_insn_data(env, subprog_start, insn_buf, cnt,\n+\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n \t\tif (!new_prog)\n \t\t\treturn -ENOMEM;\n \t\tenv-\u003eprog = prog = new_prog;\ndiff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c\nindex 16fb7dc43726f..cd9523f69298e 100644\n--- a/kernel/bpf/liveness.c\n+++ b/kernel/bpf/liveness.c\n@@ -350,14 +350,18 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n \tstruct bpf_iarray *succ, *jt;\n \tint insn_sz;\n \n-\tjt = env-\u003einsn_aux_data[idx].jt;\n-\tif (unlikely(jt))\n-\t\treturn jt;\n-\n \t/* pre-allocated array of size up to 2; reset cnt, as it may have been used already */\n \tsucc = env-\u003esucc;\n \tsucc-\u003ecnt = 0;\n \n+\t/* All gotox of a subprogram share its jump table, see compute_subprog_jts() */\n+\tif (unlikely(insn_is_gotox(insn))) {\n+\t\tjt = bpf_find_containing_subprog(env, idx)-\u003ejt;\n+\t\tif (verifier_bug_if(!jt, env, \"no jump table for gotox insn %u\", idx))\n+\t\t\treturn succ;\n+\t\treturn jt;\n+\t}\n+\n \topcode_info = \u0026opcode_info_tbl[BPF_CLASS(insn-\u003ecode) | BPF_OP(insn-\u003ecode)];\n \tinsn_sz = bpf_is_ldimm64(insn) ? 2 : 1;\n \tif (opcode_info-\u003ecan_fallthrough)\n@@ -366,6 +370,20 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n \tif (opcode_info-\u003ecan_jump)\n \t\tsucc-\u003eitems[succ-\u003ecnt++] = idx + bpf_jmp_offset(insn) + 1;\n \n+\t/*\n+\t * tail_call upon success and ld_{abs,ind} upon load failure return\n+\t * from the subprogram, which is a hidden edge to its exit. A subprogram\n+\t * without an exit insn has no exit to model.\n+\t */\n+\tif (unlikely((bpf_helper_call(insn) \u0026\u0026 insn-\u003eimm == BPF_FUNC_tail_call) ||\n+\t\t (BPF_CLASS(insn-\u003ecode) == BPF_LD \u0026\u0026\n+\t\t (BPF_MODE(insn-\u003ecode) == BPF_ABS || BPF_MODE(insn-\u003ecode) == BPF_IND)))) {\n+\t\tu32 exit_idx = bpf_find_containing_subprog(env, idx)-\u003eexit_idx;\n+\n+\t\tif (exit_idx != U32_MAX)\n+\t\t\tsucc-\u003eitems[succ-\u003ecnt++] = exit_idx;\n+\t}\n+\n \treturn succ;\n }\n \ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex c495dc9070dea..c1d6d217e0e86 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -2470,6 +2470,7 @@ static int add_subprog(struct bpf_verifier_env *env, int off)\n \t\treturn -E2BIG;\n \t}\n \t/* determine subprog starts. The end is one before the next starts */\n+\tenv-\u003esubprog_info[env-\u003esubprog_cnt].exit_idx = U32_MAX;\n \tenv-\u003esubprog_info[env-\u003esubprog_cnt++].start = off;\n \tsort(env-\u003esubprog_info, env-\u003esubprog_cnt,\n \t sizeof(env-\u003esubprog_info[0]), cmp_subprogs, NULL);\n@@ -19252,10 +19253,25 @@ static int indirect_jump_min_max_index(struct bpf_verifier_env *env,\n \treturn 0;\n }\n \n+static int reject_gotox_out_of_subprog(struct bpf_verifier_env *env, u32 target,\n+\t\t\t\t u32 subprog_start, u32 subprog_end)\n+{\n+\tverbose(env, \"indirect jump from insn %d to %u leaves the subprog [%u,%u)\\n\",\n+\t\tenv-\u003einsn_idx, target, subprog_start, subprog_end);\n+\tbpf_diag_program_structure(\n+\t\tenv, env-\u003einsn_idx, \"indirect jump leaves subprogram\",\n+\t\t\"Keep every reachable jump-table target inside the subprogram of the indirect jump.\",\n+\t\t\"Instruction %d can jump indirectly to instruction %u, which is outside its own subprogram [%u,%u).\",\n+\t\tenv-\u003einsn_idx, target, subprog_start, subprog_end);\n+\treturn -EINVAL;\n+}\n+\n /* gotox *dst_reg */\n static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)\n {\n \tstruct bpf_verifier_state *other_branch;\n+\tstruct bpf_subprog_info *subprog;\n+\tu32 subprog_start, subprog_end;\n \tstruct bpf_reg_state *dst_reg;\n \tstruct bpf_map *map;\n \tu32 min_index, max_index;\n@@ -19299,6 +19315,24 @@ static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *in\n \t\treturn -EINVAL;\n \t}\n \n+\tsubprog = bpf_find_containing_subprog(env, env-\u003einsn_idx);\n+\tif (verifier_bug_if(!subprog, env, \"no subprog contains insn %d\", env-\u003einsn_idx))\n+\t\treturn -EFAULT;\n+\tsubprog_start = subprog-\u003estart;\n+\tsubprog_end = (subprog + 1)-\u003estart;\n+\n+\t/*\n+\t * Maps are confined to a subprog, see compute_subprog_jts(), and the\n+\t * targets are sorted and free of duplicates, see sort_insn_array_uniq(),\n+\t * so either all of them belong to this subprog or none of them does.\n+\t */\n+\tif (env-\u003egotox_tmp_buf-\u003eitems[0] \u003c subprog_start)\n+\t\treturn reject_gotox_out_of_subprog(env, env-\u003egotox_tmp_buf-\u003eitems[0],\n+\t\t\t\t\t\t subprog_start, subprog_end);\n+\tif (env-\u003egotox_tmp_buf-\u003eitems[n - 1] \u003e= subprog_end)\n+\t\treturn reject_gotox_out_of_subprog(env, env-\u003egotox_tmp_buf-\u003eitems[n - 1],\n+\t\t\t\t\t\t subprog_start, subprog_end);\n+\n \tfor (i = 0; i \u003c n - 1; i++) {\n \t\tmark_indirect_target(env, env-\u003egotox_tmp_buf-\u003eitems[i]);\n \t\tother_branch = push_stack(env, env-\u003egotox_tmp_buf-\u003eitems[i],\n@@ -22750,8 +22784,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \trelease_maps(env);\n \trelease_btfs(env);\n err_free_env:\n-\tif (env-\u003einsn_aux_data)\n-\t\tbpf_clear_insn_aux_data(env, 0, env-\u003einsn_aux_data_len);\n+\tbpf_free_subprog_jts(env);\n \tvfree(env-\u003einsn_aux_data);\n \tkvfree(env-\u003efd_array);\n \tbpf_stack_liveness_free(env);\ndiff --git a/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c b/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c\nindex 0222a9a5d0761..bfedeed24ac54 100644\n--- a/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c\n+++ b/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c\n@@ -1,6 +1,7 @@\n // SPDX-License-Identifier: GPL-2.0\n \n #include \u003cbpf/bpf.h\u003e\n+#include \u003cbpf/btf.h\u003e\n #include \u003ctest_progs.h\u003e\n \n #if defined(__x86_64__) || defined(__powerpc__) || defined(__aarch64__)\n@@ -453,43 +454,672 @@ static void check_bpf_no_lookup(void)\n \tclose(map_fd);\n }\n \n+#define GOTOX_LOG_SZ\t\t(256 * 1024)\n+\n+/* Load and return the fd, which the caller owns. */\n+static int gotox_prog_load_funcs_fd(struct bpf_insn *insns, __u32 insn_cnt,\n+\t\t\t\t int *fd_array, __u32 fd_array_cnt, char *log,\n+\t\t\t\t int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt)\n+{\n+\tLIBBPF_OPTS(bpf_prog_load_opts, opts);\n+\n+\tlog[0] = 0;\n+\topts.fd_array = fd_array;\n+\topts.fd_array_cnt = fd_array_cnt;\n+\topts.log_buf = log;\n+\topts.log_size = GOTOX_LOG_SZ;\n+\topts.log_level = 1;\n+\tif (fi_cnt) {\n+\t\topts.prog_btf_fd = btf_fd;\n+\t\topts.func_info = fi;\n+\t\topts.func_info_cnt = fi_cnt;\n+\t\topts.func_info_rec_size = sizeof(*fi);\n+\t}\n+\n+\treturn bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, \"GPL\", insns, insn_cnt, \u0026opts);\n+}\n+\n+/* Load for the outcome only: 0 or the negative errno. */\n+static int gotox_prog_load_funcs(struct bpf_insn *insns, __u32 insn_cnt,\n+\t\t\t\t int *fd_array, __u32 fd_array_cnt, char *log,\n+\t\t\t\t int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt)\n+{\n+\tint prog_fd;\n+\n+\tprog_fd = gotox_prog_load_funcs_fd(insns, insn_cnt, fd_array, fd_array_cnt, log,\n+\t\t\t\t\t btf_fd, fi, fi_cnt);\n+\tif (prog_fd \u003c 0)\n+\t\treturn prog_fd;\n+\tclose(prog_fd);\n+\treturn 0;\n+}\n+\n+static int gotox_prog_load(struct bpf_insn *insns, __u32 insn_cnt,\n+\t\t\t int *fd_array, __u32 fd_array_cnt, char *log)\n+{\n+\treturn gotox_prog_load_funcs(insns, insn_cnt, fd_array, fd_array_cnt, log,\n+\t\t\t\t -1, NULL, 0);\n+}\n+\n+static int gotox_jt_create_offs(const __u32 *offs, __u32 cnt)\n+{\n+\tstruct bpf_insn_array_value val = {};\n+\tint map_fd;\n+\t__u32 i;\n+\n+\tmap_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, cnt);\n+\tif (!ASSERT_GE(map_fd, 0, \"map_create\"))\n+\t\treturn map_fd;\n+\n+\tfor (i = 0; i \u003c cnt; i++) {\n+\t\tval.orig_off = offs[i];\n+\t\tif (!ASSERT_EQ(bpf_map_update_elem(map_fd, \u0026i, \u0026val, 0), 0,\n+\t\t\t \"bpf_map_update_elem\"))\n+\t\t\tgoto err;\n+\t}\n+\n+\tif (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, \"bpf_map_freeze\"))\n+\t\tgoto err;\n+\n+\treturn map_fd;\n+err:\n+\tclose(map_fd);\n+\treturn -1;\n+}\n+\n+#define GOTOX_SUB_START\t\t4\n+#define GOTOX_MAIN_TGT\t\t2\n+#define GOTOX_SUB_TGT\t\t8\n+#define GOTOX_TWO_INSN_CNT\t10\n+\n+static void gotox_two_subprogs_fill(struct bpf_insn *insns, __u32 jt_idx, __u32 jt_off)\n+{\n+\tinsns[0] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,\n+\t\t\t\tGOTOX_SUB_START - 1 - 1);\n+\tinsns[GOTOX_MAIN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[3] = BPF_EXIT_INSN();\n+\n+\t/* r1 = \u0026jt[0], by index 'jt_idx' into fd_array */\n+\tinsns[GOTOX_SUB_START] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_1,\n+\t\t.src_reg = BPF_PSEUDO_MAP_IDX_VALUE,\n+\t\t.imm = jt_idx,\n+\t};\n+\tinsns[GOTOX_SUB_START + 1] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[6] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, jt_off * 8);\n+\tinsns[7] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);\n+\tinsns[GOTOX_SUB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 1);\n+\tinsns[9] = BPF_EXIT_INSN();\n+}\n+\n+/*\n+ * Load 'insns' with the two jump tables 'jt_a' and 'jt_b', in this order in\n+ * fd_array, and check the outcome. 'fi' describes the subprograms when given.\n+ */\n+static void check_gotox_two_maps(struct bpf_insn *insns, __u32 insn_cnt,\n+\t\t\t\t const __u32 *jt_a, __u32 a_cnt,\n+\t\t\t\t const __u32 *jt_b, __u32 b_cnt,\n+\t\t\t\t int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt,\n+\t\t\t\t int expect_err, const char *expect_msg)\n+{\n+\tint map_fd[2] = { -1, -1 };\n+\tchar *log;\n+\tint err;\n+\n+\tlog = calloc(1, GOTOX_LOG_SZ);\n+\tif (!ASSERT_OK_PTR(log, \"calloc log\"))\n+\t\treturn;\n+\n+\tmap_fd[0] = gotox_jt_create_offs(jt_a, a_cnt);\n+\tif (map_fd[0] \u003c 0)\n+\t\tgoto free_log;\n+\tmap_fd[1] = gotox_jt_create_offs(jt_b, b_cnt);\n+\tif (map_fd[1] \u003c 0)\n+\t\tgoto close_maps;\n+\n+\tif (fi)\n+\t\terr = gotox_prog_load_funcs(insns, insn_cnt, map_fd, 2, log,\n+\t\t\t\t\t btf_fd, fi, fi_cnt);\n+\telse\n+\t\terr = gotox_prog_load(insns, insn_cnt, map_fd, 2, log);\n+\tif (expect_err)\n+\t\tASSERT_EQ(err, expect_err, \"program should have been rejected\");\n+\telse\n+\t\tASSERT_EQ(err, 0, \"bpf(BPF_PROG_LOAD)\");\n+\tif (expect_msg)\n+\t\tASSERT_HAS_SUBSTR(log, expect_msg, \"verifier log\");\n+\n+close_maps:\n+\tclose(map_fd[0]);\n+\tif (map_fd[1] \u003e= 0)\n+\t\tclose(map_fd[1]);\n+free_log:\n+\tfree(log);\n+}\n+\n+static void check_gotox_target_other_subprog(void)\n+{\n+\tconst __u32 jt_main[] = { GOTOX_MAIN_TGT };\n+\tconst __u32 jt_sub[] = { GOTOX_SUB_TGT };\n+\tstruct bpf_insn insns[GOTOX_TWO_INSN_CNT];\n+\n+\tgotox_two_subprogs_fill(insns, 0, 0);\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),\n+\t\t\t jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,\n+\t\t\t -EINVAL, \"indirect jump from insn 7 to 2 leaves the subprog [4,10)\");\n+}\n+\n+static void check_gotox_jt_per_subprog(void)\n+{\n+\tconst __u32 jt_main[] = { GOTOX_MAIN_TGT };\n+\tconst __u32 jt_sub[] = { GOTOX_SUB_TGT };\n+\tstruct bpf_insn insns[GOTOX_TWO_INSN_CNT];\n+\n+\tgotox_two_subprogs_fill(insns, 1, 0);\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),\n+\t\t\t jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,\n+\t\t\t 0, NULL);\n+}\n+\n+#define GOTOX_DEAD_MAIN_LEN\t7\n+#define GOTOX_DEAD_SUB_LEN\t6\n+#define GOTOX_DEAD_INSN_CNT\t(GOTOX_DEAD_MAIN_LEN + 3 * GOTOX_DEAD_SUB_LEN)\n+#define GOTOX_DEAD_LIVE_CNT\t10\n+\n+/* r1 = \u0026jt[0], by index 'jt_idx' into fd_array; r1 = *r1; gotox r1; r0 = 0; exit */\n+static void gotox_sub_fill(struct bpf_insn *insns, __u32 start, __u32 jt_idx)\n+{\n+\tinsns[start] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_1,\n+\t\t.src_reg = BPF_PSEUDO_MAP_IDX_VALUE,\n+\t\t.imm = jt_idx,\n+\t};\n+\tinsns[start + 1] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[start + 2] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);\n+\tinsns[start + 3] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);\n+\tinsns[start + 4] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[start + 5] = BPF_EXIT_INSN();\n+}\n+\n+/*\n+ * Two subprograms which are only called from dead code sit in front of the\n+ * live one, and all three own a jump table. Dead code removal drops both in\n+ * one go, which compacts subprog_info[] along with the tables.\n+ */\n+static void check_gotox_jt_dead_subprogs(void)\n+{\n+\tconst __u32 sub_start[3] = { GOTOX_DEAD_MAIN_LEN,\n+\t\t\t\t GOTOX_DEAD_MAIN_LEN + GOTOX_DEAD_SUB_LEN,\n+\t\t\t\t GOTOX_DEAD_MAIN_LEN + 2 * GOTOX_DEAD_SUB_LEN };\n+\tstruct bpf_insn insns[GOTOX_DEAD_INSN_CNT];\n+\tint map_fd[3] = { -1, -1, -1 };\n+\tstruct bpf_prog_info info = {};\n+\t__u32 len = sizeof(info), tgt, i;\n+\tint prog_fd;\n+\tchar *log;\n+\n+\tlog = calloc(1, GOTOX_LOG_SZ);\n+\tif (!ASSERT_OK_PTR(log, \"calloc log\"))\n+\t\treturn;\n+\n+\tinsns[0] = BPF_MOV64_IMM(BPF_REG_1, 0);\n+\tinsns[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2);\n+\tinsns[2] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,\n+\t\t\t\tsub_start[0] - 2 - 1);\n+\tinsns[3] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,\n+\t\t\t\tsub_start[1] - 3 - 1);\n+\tinsns[4] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,\n+\t\t\t\tsub_start[2] - 4 - 1);\n+\tinsns[5] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[6] = BPF_EXIT_INSN();\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(sub_start); i++) {\n+\t\tgotox_sub_fill(insns, sub_start[i], i);\n+\t\ttgt = sub_start[i] + 4;\n+\t\tmap_fd[i] = gotox_jt_create_offs(\u0026tgt, 1);\n+\t\tif (map_fd[i] \u003c 0)\n+\t\t\tgoto close_maps;\n+\t}\n+\n+\tprog_fd = gotox_prog_load_funcs_fd(insns, ARRAY_SIZE(insns), map_fd,\n+\t\t\t\t\t ARRAY_SIZE(map_fd), log, -1, NULL, 0);\n+\tif (!ASSERT_GE(prog_fd, 0, \"bpf(BPF_PROG_LOAD)\"))\n+\t\tgoto close_maps;\n+\n+\t/* Both dead subprograms and the calls to them must be gone. */\n+\tif (ASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026len),\n+\t\t \"bpf_prog_get_info_by_fd\"))\n+\t\tASSERT_EQ(info.xlated_prog_len / sizeof(struct bpf_insn),\n+\t\t\t GOTOX_DEAD_LIVE_CNT, \"xlated insn count\");\n+\tclose(prog_fd);\n+close_maps:\n+\tfor (i = 0; i \u003c ARRAY_SIZE(map_fd); i++)\n+\t\tif (map_fd[i] \u003e= 0)\n+\t\t\tclose(map_fd[i]);\n+\tfree(log);\n+}\n+\n+#define GOTOX_FWD_GOTOX\t\t11\n+#define GOTOX_FWD_OWN_TGT\t12\n+#define GOTOX_FWD_SUB_START\t14\n+#define GOTOX_FWD_INSN_CNT\t16\n+\n+static void gotox_from_main_fill(struct bpf_insn *insns)\n+{\n+\tinsns[0] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);\n+\tinsns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,\n+\t\t\t\tGOTOX_FWD_SUB_START - 1 - 1);\n+\tinsns[2] = BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6,\n+\t\t\t offsetof(struct xdp_md, ingress_ifindex));\n+\tinsns[3] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_2, 0, 4);\n+\n+\t/* r1 = \u0026jt_leaves[0], by index 1 into fd_array */\n+\tinsns[4] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_1,\n+\t\t.src_reg = BPF_PSEUDO_MAP_IDX_VALUE,\n+\t\t.imm = 1,\n+\t};\n+\tinsns[5] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[6] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);\n+\tinsns[7] = BPF_JMP_A(3);\n+\n+\t/* r1 = \u0026jt_own[0], by index 0 into fd_array */\n+\tinsns[8] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_1,\n+\t\t.src_reg = BPF_PSEUDO_MAP_IDX_VALUE,\n+\t\t.imm = 0,\n+\t};\n+\tinsns[9] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[10] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);\n+\n+\tinsns[GOTOX_FWD_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);\n+\tinsns[GOTOX_FWD_OWN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[13] = BPF_EXIT_INSN();\n+\tinsns[GOTOX_FWD_SUB_START] = BPF_MOV64_IMM(BPF_REG_0, 1);\n+\tinsns[15] = BPF_EXIT_INSN();\n+}\n+\n+static void check_gotox_target_subprog_from_main(void)\n+{\n+\tconst __u32 jt_own[] = { GOTOX_FWD_OWN_TGT };\n+\tconst __u32 jt_leaves[] = { GOTOX_FWD_SUB_START };\n+\tstruct bpf_insn insns[GOTOX_FWD_INSN_CNT];\n+\n+\tgotox_from_main_fill(insns);\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_own, ARRAY_SIZE(jt_own),\n+\t\t\t jt_leaves, ARRAY_SIZE(jt_leaves), -1, NULL, 0,\n+\t\t\t -EINVAL, \"indirect jump from insn 11 to 14 leaves the subprog [0,14)\");\n+}\n+\n+/*\n+ * The only map of the subprog holding the gotox reaches past that subprog, so\n+ * the subprog is left without a jump table at all.\n+ */\n+static void check_gotox_jt_spans_subprogs(void)\n+{\n+\tconst __u32 jt_span[] = { GOTOX_FWD_OWN_TGT, GOTOX_FWD_SUB_START };\n+\tconst __u32 jt_leaves[] = { GOTOX_FWD_SUB_START };\n+\tstruct bpf_insn insns[GOTOX_FWD_INSN_CNT];\n+\n+\tgotox_from_main_fill(insns);\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_span, ARRAY_SIZE(jt_span),\n+\t\t\t jt_leaves, ARRAY_SIZE(jt_leaves), -1, NULL, 0,\n+\t\t\t -EINVAL, \"jump table of subprog starting at 0 spans multiple subprogs\");\n+}\n+\n+/*\n+ * The subprog holding the gotox has a well formed jump table of its own and\n+ * also collects a map that reaches past its end. The spanning map is still\n+ * rejected, even though the subprog is not left without a table.\n+ */\n+static void check_gotox_jt_spans_with_own_table(void)\n+{\n+\tconst __u32 jt_own[] = { GOTOX_FWD_OWN_TGT };\n+\tconst __u32 jt_span[] = { GOTOX_FWD_OWN_TGT, GOTOX_FWD_SUB_START };\n+\tstruct bpf_insn insns[GOTOX_FWD_INSN_CNT];\n+\n+\tgotox_from_main_fill(insns);\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_own, ARRAY_SIZE(jt_own),\n+\t\t\t jt_span, ARRAY_SIZE(jt_span), -1, NULL, 0,\n+\t\t\t -EINVAL, \"jump table of subprog starting at 0 spans multiple subprogs\");\n+}\n+\n+#define GOTOX_SLICE_SUB_START\t6\n+#define GOTOX_SLICE_GOTOX\t14\n+#define GOTOX_SLICE_SUB_TGT\t15\n+#define GOTOX_SLICE_INSN_CNT\t17\n+\n+static void gotox_slice_fill(struct bpf_insn *insns)\n+{\n+\tinsns[0] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,\n+\t\t\t\tGOTOX_SLICE_SUB_START - 1 - 1);\n+\tinsns[2] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[3] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[4] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[5] = BPF_EXIT_INSN();\n+\n+\tinsns[GOTOX_SLICE_SUB_START] =\n+\t\tBPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,\n+\t\t\t offsetof(struct xdp_md, ingress_ifindex));\n+\tinsns[7] = BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 1);\n+\tinsns[8] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1);\n+\tinsns[9] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);\n+\n+\t/* r1 = \u0026jt_main[0], by index 0 into fd_array */\n+\tinsns[10] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_1,\n+\t\t.src_reg = BPF_PSEUDO_MAP_IDX_VALUE,\n+\t\t.imm = 0,\n+\t};\n+\tinsns[11] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[12] = BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2);\n+\tinsns[13] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);\n+\n+\tinsns[GOTOX_SLICE_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);\n+\tinsns[GOTOX_SLICE_SUB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 1);\n+\tinsns[16] = BPF_EXIT_INSN();\n+}\n+\n+static void check_gotox_index_slice_other_subprog(void)\n+{\n+\tconst __u32 jt_main[] = { 2, 3, 4 };\n+\tconst __u32 jt_sub[] = { GOTOX_SLICE_SUB_TGT };\n+\tstruct bpf_insn insns[GOTOX_SLICE_INSN_CNT];\n+\n+\tgotox_slice_fill(insns);\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),\n+\t\t\t jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,\n+\t\t\t -EINVAL, \"indirect jump from insn 14 to 3 leaves the subprog [6,17)\");\n+}\n+\n+static int gotox_btf_create(const __u32 *starts, const __u8 *linkage, __u32 cnt,\n+\t\t\t struct bpf_func_info *fi, struct btf **pbtf)\n+{\n+\tint int_id, proto_id, id;\n+\tstruct btf *btf;\n+\tchar name[24];\n+\t__u32 i;\n+\n+\tbtf = btf__new_empty();\n+\tif (!ASSERT_OK_PTR(btf, \"btf__new_empty\"))\n+\t\treturn -1;\n+\n+\tint_id = btf__add_int(btf, \"int\", 4, BTF_INT_SIGNED);\n+\tif (!ASSERT_GT(int_id, 0, \"btf__add_int\"))\n+\t\tgoto err;\n+\n+\tproto_id = btf__add_func_proto(btf, int_id);\n+\tif (!ASSERT_GT(proto_id, 0, \"btf__add_func_proto\"))\n+\t\tgoto err;\n+\n+\tfor (i = 0; i \u003c cnt; i++) {\n+\t\tsnprintf(name, sizeof(name), \"gotox_f%u\", i);\n+\t\tid = btf__add_func(btf, name, linkage[i], proto_id);\n+\t\tif (!ASSERT_GT(id, 0, \"btf__add_func\"))\n+\t\t\tgoto err;\n+\t\tfi[i].insn_off = starts[i];\n+\t\tfi[i].type_id = id;\n+\t}\n+\n+\tif (!ASSERT_OK(btf__load_into_kernel(btf), \"btf__load_into_kernel\"))\n+\t\tgoto err;\n+\n+\t*pbtf = btf;\n+\treturn btf__fd(btf);\n+err:\n+\tbtf__free(btf);\n+\treturn -1;\n+}\n+\n+static void check_gotox_target_other_global_subprog(void)\n+{\n+\tconst __u32 starts[] = { 0, GOTOX_SUB_START };\n+\tconst __u8 linkage[] = { BTF_FUNC_GLOBAL, BTF_FUNC_GLOBAL };\n+\tconst __u32 jt_main[] = { GOTOX_MAIN_TGT };\n+\tconst __u32 jt_sub[] = { GOTOX_SUB_TGT };\n+\tstruct bpf_insn insns[GOTOX_TWO_INSN_CNT];\n+\tstruct bpf_func_info fi[2];\n+\tstruct btf *btf = NULL;\n+\tint btf_fd;\n+\n+\tgotox_two_subprogs_fill(insns, 0, 0);\n+\n+\tbtf_fd = gotox_btf_create(starts, linkage, ARRAY_SIZE(starts), fi, \u0026btf);\n+\tif (btf_fd \u003c 0)\n+\t\treturn;\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),\n+\t\t\t jt_sub, ARRAY_SIZE(jt_sub), btf_fd, fi, ARRAY_SIZE(fi),\n+\t\t\t -EINVAL, \"indirect jump from insn 7 to 2 leaves the subprog [4,10)\");\n+\tbtf__free(btf);\n+}\n+\n+#define GOTOX_CB_MAIN_TGT\t6\n+#define GOTOX_CB_START\t\t8\n+#define GOTOX_CB_GOTOX\t\t11\n+#define GOTOX_CB_TGT\t\t12\n+#define GOTOX_CB_INSN_CNT\t14\n+\n+static void gotox_callback_fill(struct bpf_insn *insns)\n+{\n+\tinsns[0] = BPF_MOV64_IMM(BPF_REG_1, 1);\n+\t/* r2 = \u0026callback */\n+\tinsns[1] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_2,\n+\t\t.src_reg = BPF_PSEUDO_FUNC,\n+\t\t.imm = GOTOX_CB_START - 1 - 1,\n+\t};\n+\tinsns[2] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[3] = BPF_MOV64_IMM(BPF_REG_3, 0);\n+\tinsns[4] = BPF_MOV64_IMM(BPF_REG_4, 0);\n+\tinsns[5] = BPF_EMIT_CALL(BPF_FUNC_loop);\n+\tinsns[GOTOX_CB_MAIN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[7] = BPF_EXIT_INSN();\n+\n+\t/* r1 = \u0026jt_main[0], by index 0 into fd_array */\n+\tinsns[GOTOX_CB_START] = (struct bpf_insn) {\n+\t\t.code = BPF_LD | BPF_DW | BPF_IMM,\n+\t\t.dst_reg = BPF_REG_1,\n+\t\t.src_reg = BPF_PSEUDO_MAP_IDX_VALUE,\n+\t\t.imm = 0,\n+\t};\n+\tinsns[9] = (struct bpf_insn) { .imm = 0 };\n+\tinsns[10] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);\n+\tinsns[GOTOX_CB_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);\n+\tinsns[GOTOX_CB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsns[13] = BPF_EXIT_INSN();\n+}\n+\n+static void check_gotox_callback_leaves_subprog(void)\n+{\n+\tconst __u32 starts[] = { 0, GOTOX_CB_START };\n+\tconst __u8 linkage[] = { BTF_FUNC_GLOBAL, BTF_FUNC_STATIC };\n+\tconst __u32 jt_main[] = { GOTOX_CB_MAIN_TGT };\n+\tconst __u32 jt_cb[] = { GOTOX_CB_TGT };\n+\tstruct bpf_insn insns[GOTOX_CB_INSN_CNT];\n+\tstruct bpf_func_info fi[2];\n+\tstruct btf *btf = NULL;\n+\tint btf_fd;\n+\n+\tgotox_callback_fill(insns);\n+\n+\tbtf_fd = gotox_btf_create(starts, linkage, ARRAY_SIZE(starts), fi, \u0026btf);\n+\tif (btf_fd \u003c 0)\n+\t\treturn;\n+\n+\tcheck_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),\n+\t\t\t jt_cb, ARRAY_SIZE(jt_cb), btf_fd, fi, ARRAY_SIZE(fi),\n+\t\t\t -EINVAL, \"indirect jump from insn 11 to 6 leaves the subprog [8,14)\");\n+\tbtf__free(btf);\n+}\n+\n static void check_bpf_side(void)\n {\n \tcheck_bpf_no_lookup();\n }\n \n+/* Return the xlated index of the first insn equal to 'insn', or -1. */\n+static int xlated_insn_index(int prog_fd, const struct bpf_insn *insn)\n+{\n+\tstruct bpf_insn *xlated;\n+\tint idx = -1;\n+\t__u32 cnt, i;\n+\n+\tif (!ASSERT_OK(get_xlated_program(prog_fd, \u0026xlated, \u0026cnt), \"get_xlated_program\"))\n+\t\treturn -1;\n+\tfor (i = 0; i \u003c cnt; i++)\n+\t\tif (!memcmp(\u0026xlated[i], insn, sizeof(*insn))) {\n+\t\t\tidx = i;\n+\t\t\tbreak;\n+\t\t}\n+\tfree(xlated);\n+\treturn idx;\n+}\n+\n+/*\n+ * Load 'insns' with a jump table naming insn 0, which a prologue prepend\n+ * pushes down, and check that the slot followed it.\n+ */\n+static void check_gotox_target_prepend(enum bpf_prog_type prog_type,\n+\t\t\t\t struct bpf_insn *insns, __u32 insn_cnt)\n+{\n+\tLIBBPF_OPTS(bpf_prog_load_opts, opts);\n+\tstruct bpf_insn_array_value val = {};\n+\tint prog_fd = -1, map_fd, idx;\n+\t__u32 key = 0;\n+\n+\tmap_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, 1);\n+\tif (!ASSERT_GE(map_fd, 0, \"map_create\"))\n+\t\treturn;\n+\n+\tval.orig_off = 0;\n+\tif (!ASSERT_EQ(bpf_map_update_elem(map_fd, \u0026key, \u0026val, 0), 0, \"bpf_map_update_elem\"))\n+\t\tgoto cleanup;\n+\tif (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, \"bpf_map_freeze\"))\n+\t\tgoto cleanup;\n+\n+\topts.fd_array = \u0026map_fd;\n+\topts.fd_array_cnt = 1;\n+\tprog_fd = bpf_prog_load(prog_type, NULL, \"GPL\", insns, insn_cnt, \u0026opts);\n+\tif (!ASSERT_GE(prog_fd, 0, \"bpf(BPF_PROG_LOAD)\"))\n+\t\tgoto cleanup;\n+\n+\tidx = xlated_insn_index(prog_fd, \u0026insns[0]);\n+\tif (!ASSERT_GT(idx, 0, \"insn 0 was prepended to\"))\n+\t\tgoto cleanup;\n+\tif (!ASSERT_EQ(bpf_map_lookup_elem(map_fd, \u0026key, \u0026val), 0, \"bpf_map_lookup_elem\"))\n+\t\tgoto cleanup;\n+\tASSERT_EQ(val.xlated_off, (__u32)idx, \"gotox target followed insn 0 past the prepend\");\n+cleanup:\n+\tif (prog_fd \u003e= 0)\n+\t\tclose(prog_fd);\n+\tclose(map_fd);\n+}\n+\n+static void check_gotox_target_prologue_shift(void)\n+{\n+\tstruct bpf_insn insns[] = {\n+\t\t/* insn 0: gotox target and subprog start */\n+\t\tBPF_MOV64_IMM(BPF_REG_0, 0),\n+\t\t/* may_goto +4 -\u003e exit block, bounds the loop */\n+\t\tBPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 4, 0),\n+\t\t/* r1 = \u0026jt[0] (insns 2 and 3) */\n+\t\tBPF_LD_IMM64_RAW(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, 0),\n+\t\tBPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0), /* r1 = ips[0] */\n+\t\t/* insn 5: gotox r1 -\u003e insn 0 */\n+\t\tBPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0),\n+\t\tBPF_MOV64_IMM(BPF_REG_0, XDP_PASS), /* insn 6: exit block */\n+\t\tBPF_EXIT_INSN(),\n+\t};\n+\n+\tcheck_gotox_target_prepend(BPF_PROG_TYPE_XDP, insns, ARRAY_SIZE(insns));\n+}\n+\n+static void check_gotox_target_ctx_prologue_shift(void)\n+{\n+\tstruct bpf_insn insns[] = {\n+\t\t/* insn 0: gotox target and subprog start */\n+\t\tBPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, -8),\n+\t\tBPF_JMP_IMM(BPF_JEQ, BPF_REG_7, 0x5a5a, 12), /* second visit exits */\n+\t\tBPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x5a5a),\n+\t\tBPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* r6 = ctx */\n+\t\tBPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6, offsetof(struct __sk_buff, data)),\n+\t\tBPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_6, offsetof(struct __sk_buff, data_end)),\n+\t\tBPF_MOV64_REG(BPF_REG_4, BPF_REG_2),\n+\t\tBPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 1),\n+\t\tBPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 5),\n+\t\t/* insn 9: direct packet write -\u003e tc unclone prologue prepended at insn 0 */\n+\t\tBPF_ST_MEM(BPF_B, BPF_REG_2, 0, 0),\n+\t\t/* r5 = \u0026jt[0] (insns 10 and 11) */\n+\t\tBPF_LD_IMM64_RAW(BPF_REG_5, BPF_PSEUDO_MAP_IDX_VALUE, 0),\n+\t\tBPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_5, 0), /* r5 = ips[0] */\n+\t\t/* insn 13: gotox r5 -\u003e insn 0 */\n+\t\tBPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_5, 0, 0, 0),\n+\t\tBPF_MOV64_IMM(BPF_REG_0, 0), /* insn 14: exit block */\n+\t\tBPF_EXIT_INSN(),\n+\t};\n+\n+\tcheck_gotox_target_prepend(BPF_PROG_TYPE_SCHED_CLS, insns, ARRAY_SIZE(insns));\n+}\n+\n static void __test_bpf_insn_array(void)\n {\n \t/* Test if offsets are adjusted properly */\n-\n \tif (test__start_subtest(\"one2one\"))\n \t\tcheck_one_to_one_mapping();\n-\n \tif (test__start_subtest(\"simple\"))\n \t\tcheck_simple();\n-\n \tif (test__start_subtest(\"deletions\"))\n \t\tcheck_deletions();\n-\n \tif (test__start_subtest(\"deletions-with-functions\"))\n \t\tcheck_deletions_with_functions();\n-\n \tif (test__start_subtest(\"blindness\"))\n \t\tcheck_blindness();\n-\n \t/* Check all kinds of operations and related restrictions */\n-\n \tif (test__start_subtest(\"incorrect-index\"))\n \t\tcheck_incorrect_index();\n-\n \tif (test__start_subtest(\"load-unfrozen-map\"))\n \t\tcheck_load_unfrozen_map();\n-\n \tif (test__start_subtest(\"no-map-reuse\"))\n \t\tcheck_no_map_reuse();\n-\n \tif (test__start_subtest(\"bpf-side-ops\"))\n \t\tcheck_bpf_side();\n+\tif (test__start_subtest(\"gotox-jt-spans-subprogs\"))\n+\t\tcheck_gotox_jt_spans_subprogs();\n+\tif (test__start_subtest(\"gotox-jt-spans-with-own-table\"))\n+\t\tcheck_gotox_jt_spans_with_own_table();\n+\tif (test__start_subtest(\"gotox-target-other-subprog\"))\n+\t\tcheck_gotox_target_other_subprog();\n+\tif (test__start_subtest(\"gotox-jt-per-subprog\"))\n+\t\tcheck_gotox_jt_per_subprog();\n+\tif (test__start_subtest(\"gotox-jt-dead-subprogs\"))\n+\t\tcheck_gotox_jt_dead_subprogs();\n+\tif (test__start_subtest(\"gotox-target-subprog-from-main\"))\n+\t\tcheck_gotox_target_subprog_from_main();\n+\tif (test__start_subtest(\"gotox-index-slice-other-subprog\"))\n+\t\tcheck_gotox_index_slice_other_subprog();\n+\tif (test__start_subtest(\"gotox-target-other-global-subprog\"))\n+\t\tcheck_gotox_target_other_global_subprog();\n+\tif (test__start_subtest(\"gotox-callback-leaves-subprog\"))\n+\t\tcheck_gotox_callback_leaves_subprog();\n+\tif (test__start_subtest(\"gotox-target-prologue-shift\"))\n+\t\tcheck_gotox_target_prologue_shift();\n+\tif (test__start_subtest(\"gotox-target-ctx-prologue-shift\"))\n+\t\tcheck_gotox_target_ctx_prologue_shift();\n }\n #else\n static void __test_bpf_insn_array(void)\ndiff --git a/tools/testing/selftests/bpf/prog_tests/exceptions.c b/tools/testing/selftests/bpf/prog_tests/exceptions.c\nindex 639866ce09a9f..3705db18c1bbc 100644\n--- a/tools/testing/selftests/bpf/prog_tests/exceptions.c\n+++ b/tools/testing/selftests/bpf/prog_tests/exceptions.c\n@@ -6,6 +6,7 @@\n #include \"exceptions_ext.skel.h\"\n #include \"exceptions_fail.skel.h\"\n #include \"exceptions_assert.skel.h\"\n+#include \"exceptions_dead_subprog.skel.h\"\n \n static char log_buf[1024 * 1024];\n \n@@ -410,9 +411,15 @@ static void test_exceptions_assertions(void)\n \tRUN_TESTS(exceptions_assert);\n }\n \n+static void test_exceptions_dead_subprog(void)\n+{\n+\tRUN_TESTS(exceptions_dead_subprog);\n+}\n+\n void test_exceptions(void)\n {\n \ttest_exceptions_success();\n \ttest_exceptions_failure();\n \ttest_exceptions_assertions();\n+\ttest_exceptions_dead_subprog();\n }\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c b/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c\nnew file mode 100644\nindex 0000000000000..4f8b82a85234f\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c\n@@ -0,0 +1,40 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"bpf_experimental.h\"\n+\n+/*\n+ * Read-only and frozen, so the verifier sees a constant zero, while clang\n+ * cannot fold the volatile read and keeps the call below.\n+ */\n+const volatile int never;\n+\n+/* Only called from dead code, hence removed as a whole before the JIT runs. */\n+static __noinline int dead_subprog(struct __sk_buff *ctx)\n+{\n+\treturn ctx-\u003elen;\n+}\n+\n+__noinline int exception_cb_behind_dead_subprog_cb(u64 cookie)\n+{\n+\treturn 42;\n+}\n+\n+/*\n+ * libbpf appends the exception callback behind the subprograms reached by\n+ * calls, so removing the dead one compacts subprog_info[] underneath the\n+ * callback's index.\n+ */\n+SEC(\"?tc\")\n+__exception_cb(exception_cb_behind_dead_subprog_cb)\n+__success __retval(42)\n+int exception_cb_behind_dead_subprog(struct __sk_buff *ctx)\n+{\n+\tif (never)\n+\t\treturn dead_subprog(ctx);\n+\tbpf_throw(0);\n+\treturn 0;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_cfg.c b/tools/testing/selftests/bpf/progs/verifier_cfg.c\nindex 3c3bb03e8217a..07388a922e5b5 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_cfg.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_cfg.c\n@@ -173,4 +173,42 @@ l1_%=:\texit;\t\t\t\t\t\t\\\n \"\t::: __clobber_all);\n }\n \n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, __u32);\n+} map_array SEC(\".maps\");\n+\n+extern void bpf_throw(__u64 cookie) __ksym;\n+\n+/*\n+ * The subprogram leaves through the tail call or the exception and never\n+ * reaches an exit insn, so the hidden edge of the tail call has no exit to\n+ * point at. The stack access makes the liveness pass walk the subprogram,\n+ * and the loop compiles to a single jump back onto itself.\n+ */\n+__noinline __used\n+static int tail_call_or_throw_subprog(void *ctx)\n+{\n+\tvolatile __u32 idx = 0;\n+\n+\tbpf_tail_call(ctx, \u0026map_array, idx);\n+\tbpf_throw(0);\n+\tfor (;;) {}\n+}\n+\n+SEC(\"tc\")\n+__description(\"tail call in a subprogram without an exit\")\n+__arch_x86_64\n+__arch_arm64\n+__success\n+__naked void tail_call_in_subprog_without_exit(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tcall tail_call_or_throw_subprog;\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_gotox.c b/tools/testing/selftests/bpf/progs/verifier_gotox.c\nindex 0e27c2c79c57e..f5a9878c7b8d4 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_gotox.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_gotox.c\n@@ -366,7 +366,7 @@ __used static int test_subprog(void)\n }\n \n SEC(\"socket\")\n-__failure __msg(\"jump table for insn 4 points outside of the subprog [0,10]\")\n+__failure __msg(\"jump table of subprog starting at 0 spans multiple subprogs\")\n __naked void jump_table_outside_subprog(void)\n {\n \tasm volatile (\"\t\t\t\t\t\t\\\n@@ -457,6 +457,129 @@ __naked void spill_fill_ptr_to_insn(void)\n \t: __clobber_all);\n }\n \n+/*\n+ * Emit each table entry next to the instruction it targets. Numeric labels\n+ * can be reused by .rept, while %= keeps table symbols unique per asm block.\n+ * BASE is the code section: libbpf relocates section-relative byte offsets\n+ * to instruction offsets in the loaded program.\n+ */\n+#define GOTOX_TABLE_BEGIN(COUNT) \\\n+\t\".pushsection .jumptables,\\\"\\\",@progbits;\" \\\n+\t\"jt_%=:;\" \\\n+\t\".global jt_%=;\" \\\n+\t\".size jt_%=, 8 * (\" #COUNT \");\" \\\n+\t\".popsection;\" \\\n+\t\"r1 = jt_%= ll;\"\n+\n+#define GOTOX_TABLE_ENTRY(LABEL, BASE) \\\n+\t\".pushsection .jumptables,\\\"\\\",@progbits;\" \\\n+\t\".quad \" LABEL \" - \" BASE \";\" \\\n+\t\".popsection;\"\n+\n+/* N gotox instructions, each targeting all N gotox plus the final block. */\n+#define GOTOX_SELF_TARGETS(N, BASE) \\\n+\tGOTOX_TABLE_BEGIN(N + 1) \\\n+\t\"r0 = 0;\" \\\n+\t\".rept \" #N \";\" \\\n+\t\tGOTOX_TABLE_ENTRY(\"1f\", BASE) \\\n+\t\t\"1:; .8byte %[gotox];\" \\\n+\t\".endr;\" \\\n+\tGOTOX_TABLE_ENTRY(\"2f\", BASE) \\\n+\t\"2:;\"\n+\n+#define GOTOX_OPERAND \\\n+\t__imm_insn(gotox, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0, 0))\n+\n+/*\n+ * Count edges even when their targets are already discovered. These\n+ * programs deliberately use a scalar: the CFG edge limit must reject them\n+ * before symbolic verification reaches the invalid gotox operand.\n+ */\n+SEC(\"socket\")\n+__description(\"too-many-gotox-edges\")\n+__failure __msg(\"number of indirect jump edges in the program exceeds\")\n+__naked void too_many_gotox_edges(void)\n+{\n+\tasm volatile (\n+\t\t/* 1000 * 1001 = 1,001,000 edges. */\n+\t\tGOTOX_SELF_TARGETS(1000, \"socket\")\n+\t\t\"r0 = 0; exit;\"\n+\t\t: : GOTOX_OPERAND : __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"gotox-edges-at-limit\")\n+__success __retval(0)\n+__naked void gotox_edges_at_limit(void)\n+{\n+\tasm volatile (\n+\t\t/*\n+\t\t * 1000 gotox * 1000 targets = 1,000,000 CFG edges. At run\n+\t\t * time, each block loads the next table entry and jumps to\n+\t\t * the following block, so the program terminates.\n+\t\t */\n+\t\tGOTOX_TABLE_BEGIN(1000)\n+\t\t\".rept 1000;\"\n+\t\t\tGOTOX_TABLE_ENTRY(\"1f\", \"socket\")\n+\t\t\t\"r0 = *(u64 *)(r1 + 0);\"\n+\t\t\t\"r1 += 8;\"\n+\t\t\t\".8byte %[gotox];\"\n+\t\t\t\"1:;\"\n+\t\t\".endr;\"\n+\t\t\"r0 = 0; exit;\"\n+\t\t: : GOTOX_OPERAND : __clobber_all);\n+}\n+\n+static __noinline __used __naked void gotox_edges_subprog(void)\n+{\n+\tasm volatile (\n+\t\tGOTOX_SELF_TARGETS(750, \".text\")\n+\t\t\"r0 = 0; exit;\"\n+\t\t: : GOTOX_OPERAND : __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"gotox-edges-across-subprogs\")\n+__failure __msg(\"number of indirect jump edges in the program exceeds\")\n+__naked void gotox_edges_across_subprogs(void)\n+{\n+\tasm volatile (\n+\t\t/* Each subprog has 563,250 edges; together, 1,126,500. */\n+\t\tGOTOX_SELF_TARGETS(750, \"socket\")\n+\t\t\"call gotox_edges_subprog;\"\n+\t\t\"exit;\"\n+\t\t: : GOTOX_OPERAND : __clobber_all);\n+}\n+\n+/*\n+ * The gotox target is a ja +0, which the nop removal pass must keep as the\n+ * jump table still names it.\n+ */\n+SEC(\"socket\")\n+__success __retval(0)\n+__xlated(\"goto pc+0\")\n+__naked void jump_table_target_nop(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\t\\\n+\t.pushsection .jumptables,\\\"\\\",@progbits;\t\t\\\n+jt0_%=:\t\t\t\t\t\t\t\t\\\n+\t.quad nop_%= - socket;\t\t\t\t\t\\\n+\t.size jt0_%=, 8;\t\t\t\t\t\\\n+\t.global jt0_%=;\t\t\t\t\t\t\\\n+\t.popsection;\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\\\n+\tr0 = jt0_%= ll;\t\t\t\t\t\t\\\n+\tr0 = *(u64 *)(r0 + 0);\t\t\t\t\t\\\n+\t.8byte %[gotox_r0];\t\t\t\t\t\\\n+nop_%=:\t\t\t\t\t\t\t\t\\\n+\tgoto +0;\t\t\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_insn(gotox_r0, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0, 0))\n+\t: __clobber_all);\n+}\n+\n #endif /* __TARGET_ARCH_x86 || __TARGET_ARCH_arm64 || __TARGET_ARCH_powerpc*/\n \n char _license[] SEC(\"license\") = \"GPL\";\n"
} |
| 2/1 | 2026/09/27 14:17 | agent | patch-evaluator |
3mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_cfg",
"bpf_compute_scc",
"bpf_insn_array_init",
"bpf_insn_array_adjust",
"bpf_free_subprog_jts",
"bpf_patch_insn_data",
"bpf_opt_remove_nops",
"bpf_convert_ctx_accesses",
"bpf_do_misc_fixups",
"bpf_insn_successors"
],
"Reasoning": "The patch modifies core BPF verifier logic and data structures reachable via the bpf() system call. Key modifications include restructuring CFG indirect jump (gotox) table tracking so tables are shared per-subprogram, enforcing edge limits, adjusting DFS traversal in strongly connected component computation (bpf_compute_scc), adding instruction patching modes to preserve or shift jump targets during fixup passes, and resetting BPF instruction array map state. Fuzzing is warranted to uncover regressions or invariant violations in verification and instruction patching.",
"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 59d38d719edf105f6b1efc007088e85fcfe8fb0a
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Sep 27 14:17:06 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 8594c8aff745d..2cf04bba16bb7 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -4289,7 +4289,7 @@ struct bpf_prog *bpf_prog_find_from_stack(void);
int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog);
int bpf_insn_array_ready(struct bpf_map *map);
void bpf_insn_array_release(struct bpf_map *map);
-void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len);
+void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len);
void bpf_insn_array_adjust_after_remove(struct bpf_map *map, u32 off, u32 len);
#ifdef CONFIG_BPF_SYSCALL
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 646e447cebd84..c775bd7577063 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -660,7 +660,6 @@ struct bpf_insn_aux_data {
/* remember the offset of node field within type to rewrite */
u64 insert_off;
};
- struct bpf_iarray *jt; /* jump table for gotox or bpf_tailcall call instruction */
struct btf_struct_meta *kptr_struct_meta;
u64 map_key_state; /* constant (32 bit) key tracking for maps */
int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
@@ -815,7 +814,12 @@ struct bpf_subprog_info {
u32 start; /* insn idx of function entry point */
u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
u32 postorder_start; /* The idx to the env->cfg.insn_postorder */
- u32 exit_idx; /* Index of one of the BPF_EXIT instructions in this subprogram */
+ /*
+ * Index of one of the BPF_EXIT instructions in this subprogram, or
+ * U32_MAX when it has none.
+ */
+ u32 exit_idx;
+ struct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */
u16 stack_depth; /* max. stack depth used by this function */
u16 stack_extra;
u32 insns_total;
@@ -1004,6 +1008,8 @@ struct bpf_verifier_env {
int cur_stack;
/* current position in the insn_postorder vector */
int cur_postorder;
+ u32 gotox_edges;
+ bool subprog_jts_ready;
} cfg;
struct backtrack_state bt;
struct bpf_jmp_history_entry *cur_hist_ent;
@@ -1582,6 +1588,7 @@ bool bpf_is_throw_kfunc(struct bpf_insn *insn);
int bpf_compute_const_regs(struct bpf_verifier_env *env);
int bpf_prune_dead_branches(struct bpf_verifier_env *env);
int bpf_check_cfg(struct bpf_verifier_env *env);
+void bpf_free_subprog_jts(struct bpf_verifier_env *env);
int bpf_compute_postorder(struct bpf_verifier_env *env);
int bpf_compute_scc(struct bpf_verifier_env *env);
@@ -1800,7 +1807,6 @@ struct bpf_kfunc_desc_tab {
};
/* Functions exported from verifier.c, used by fixups.c */
-void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len);
void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog);
bool bpf_allow_tail_call_in_subprogs(struct bpf_verifier_env *env);
bool bpf_verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm);
diff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c
index a2f84afe6f7c6..92bbb71576ae3 100644
--- a/kernel/bpf/bpf_insn_array.c
+++ b/kernel/bpf/bpf_insn_array.c
@@ -199,12 +199,16 @@ int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog)
return -EBUSY;
/*
- * Reset all the map indexes to the original values. This is needed,
- * e.g., when a replay of verification with different log level should
- * be performed.
+ * Reset the map to its pre-verification state. The xlated and jitted
+ * offsets and the jitted target pointers are recomputed by the verifier
+ * and the JIT for this program, so any values left by a previous owner
+ * must be cleared here.
*/
- for (i = 0; i < map->max_entries; i++)
+ for (i = 0; i < map->max_entries; i++) {
values[i].xlated_off = values[i].orig_off;
+ values[i].jitted_off = 0;
+ insn_array->ips[i] = 0;
+ }
return 0;
}
@@ -228,10 +232,11 @@ void bpf_insn_array_release(struct bpf_map *map)
{
struct bpf_insn_array *insn_array = cast_insn_array(map);
- atomic_set(&insn_array->used, 0);
+ /* Paired with atomic_xchg() in bpf_insn_array_init(). */
+ atomic_set_release(&insn_array->used, 0);
}
-void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len)
+void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len)
{
struct bpf_insn_array *insn_array = cast_insn_array(map);
int i;
@@ -240,7 +245,7 @@ void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len)
return;
for (i = 0; i < map->max_entries; i++) {
- if (insn_array->values[i].xlated_off <= off)
+ if (insn_array->values[i].xlated_off < first)
continue;
if (insn_array->values[i].xlated_off == INSN_DELETED)
continue;
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 0de2f634ef679..b0bd9ba951dfb 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -9,6 +9,8 @@
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
+#define BPF_MAX_GOTOX_EDGES BPF_COMPLEXITY_LIMIT_INSNS
+
/* non-recursive DFS pseudo code
* 1 procedure DFS-iterative(G,v):
* 2 label v as discovered
@@ -284,15 +286,17 @@ static struct bpf_iarray *jt_from_map(struct bpf_map *map)
}
/*
- * Find and collect all maps which fit in the subprog. Return the result as one
- * combined jump table in jt->items (allocated with kvcalloc)
+ * Collect the jump table of every subprogram that has one, as the combined
+ * table of all maps whose targets land inside that subprogram. All gotox
+ * instructions of a subprogram share the same table, so this is done in a
+ * single pass over the maps rather than once per gotox.
*/
-static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,
- int subprog_start, int subprog_end)
+static int __compute_subprog_jts(struct bpf_verifier_env *env)
{
- struct bpf_iarray *jt = NULL;
+ struct bpf_subprog_info *subprog;
+ struct bpf_iarray *jt, *jt_cur;
struct bpf_map *map;
- struct bpf_iarray *jt_cur;
+ u32 old_cnt;
int i;
for (i = 0; i < env->insn_array_map_cnt; i++) {
@@ -303,73 +307,100 @@ static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,
map = env->insn_array_maps[i];
jt_cur = jt_from_map(map);
- if (IS_ERR(jt_cur)) {
- kvfree(jt);
- return jt_cur;
+ if (IS_ERR(jt_cur))
+ return PTR_ERR(jt_cur);
+
+ subprog = bpf_find_containing_subprog(env, jt_cur->items[0]);
+ if (verifier_bug_if(!subprog, env, "no subprog contains insn %u of an insn array map",
+ jt_cur->items[0])) {
+ kvfree(jt_cur);
+ return -EFAULT;
+ }
+ if (jt_cur->items[jt_cur->cnt - 1] >= (subprog + 1)->start) {
+ verbose(env, "jump table of subprog starting at %u spans multiple subprogs, map id=%d\n",
+ subprog->start, map->id);
+ bpf_diag_program_structure(
+ env, subprog->start, "jump table spans subprograms",
+ "Keep every entry of a jump table inside one subprogram.",
+ "The jump table in map id %d for the subprogram that starts at instruction %u reaches past its end at instruction %u.",
+ map->id, subprog->start, (subprog + 1)->start);
+ kvfree(jt_cur);
+ return -EINVAL;
}
- /*
- * This is enough to check one element. The full table is
- * checked to fit inside the subprog later in create_jt()
- */
- if (jt_cur->items[0] >= subprog_start && jt_cur->items[0] < subprog_end) {
- u32 old_cnt = jt ? jt->cnt : 0;
- jt = bpf_iarray_realloc(jt, old_cnt + jt_cur->cnt);
- if (!jt) {
- kvfree(jt_cur);
- return ERR_PTR(-ENOMEM);
- }
- memcpy(jt->items + old_cnt, jt_cur->items, jt_cur->cnt << 2);
+ old_cnt = subprog->jt ? subprog->jt->cnt : 0;
+ jt = bpf_iarray_realloc(subprog->jt, old_cnt + jt_cur->cnt);
+ if (!jt) {
+ subprog->jt = NULL;
+ kvfree(jt_cur);
+ return -ENOMEM;
}
+ memcpy(jt->items + old_cnt, jt_cur->items, jt_cur->cnt << 2);
+ subprog->jt = jt;
kvfree(jt_cur);
}
- if (!jt) {
- verbose(env, "no jump tables found for subprog starting at %u\n", subprog_start);
- bpf_diag_program_structure(
- env, subprog_start, "missing jump table",
- "Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms.",
- "No jump table was found for the subprogram that starts at instruction %u.",
- subprog_start);
- return ERR_PTR(-EINVAL);
+ for (i = 0; i < env->subprog_cnt; i++) {
+ jt = env->subprog_info[i].jt;
+ if (jt)
+ jt->cnt = sort_insn_array_uniq(jt->items, jt->cnt);
}
- jt->cnt = sort_insn_array_uniq(jt->items, jt->cnt);
- return jt;
+ env->cfg.subprog_jts_ready = true;
+ return 0;
+}
+
+/* Leave nothing behind on failure. */
+static int compute_subprog_jts(struct bpf_verifier_env *env)
+{
+ int err;
+
+ err = __compute_subprog_jts(env);
+ if (err)
+ bpf_free_subprog_jts(env);
+ return err;
+}
+
+void bpf_free_subprog_jts(struct bpf_verifier_env *env)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(env->subprog_info); i++) {
+ kvfree(env->subprog_info[i].jt);
+ env->subprog_info[i].jt = NULL;
+ }
+ env->cfg.subprog_jts_ready = false;
}
static struct bpf_iarray *
-create_jt(int t, struct bpf_verifier_env *env)
+subprog_jt(int t, struct bpf_verifier_env *env)
{
struct bpf_subprog_info *subprog;
- int subprog_start, subprog_end;
- struct bpf_iarray *jt;
- int i;
+ int subprog_start, err;
+
+ if (!env->cfg.subprog_jts_ready) {
+ err = compute_subprog_jts(env);
+ if (err)
+ return ERR_PTR(err);
+ }
subprog = bpf_find_containing_subprog(env, t);
+ if (verifier_bug_if(!subprog, env, "no subprog contains insn %d", t))
+ return ERR_PTR(-EFAULT);
subprog_start = subprog->start;
- subprog_end = (subprog + 1)->start;
- jt = jt_from_subprog(env, subprog_start, subprog_end);
- if (IS_ERR(jt))
- return jt;
- /* Check that the every element of the jump table fits within the given subprogram */
- for (i = 0; i < jt->cnt; i++) {
- if (jt->items[i] < subprog_start || jt->items[i] >= subprog_end) {
- verbose(env, "jump table for insn %d points outside of the subprog [%u,%u]\n",
- t, subprog_start, subprog_end);
- bpf_diag_program_structure(
- env, t, "jump table target out of range",
- "Keep every jump-table target inside the same subprogram.",
- "The jump table for instruction %d points outside subprogram range [%u,%u).",
- t, subprog_start, subprog_end);
- kvfree(jt);
- return ERR_PTR(-EINVAL);
- }
+ if (!subprog->jt) {
+ verbose(env, "no jump tables found for subprog starting at %u\n", subprog_start);
+ bpf_diag_program_structure(
+ env, subprog_start, "missing jump table",
+ "Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms.",
+ "No jump table was found for the subprogram that starts at instruction %u.",
+ subprog_start);
+ return ERR_PTR(-EINVAL);
}
- return jt;
+ return subprog->jt;
}
/* "conditional jump with N edges" */
@@ -381,27 +412,39 @@ static int visit_gotox_insn(int t, struct bpf_verifier_env *env)
struct bpf_iarray *jt;
int i, w;
- jt = env->insn_aux_data[t].jt;
- if (!jt) {
- jt = create_jt(t, env);
- if (IS_ERR(jt))
- return PTR_ERR(jt);
+ /*
+ * The first visit pushes every target that still needs exploring, and
+ * those are all done by the time this insn is revisited, so there is
+ * nothing left to walk the jump table for.
+ */
+ if (insn_state[t] & BRANCH)
+ return DONE_EXPLORING;
- env->insn_aux_data[t].jt = jt;
+ jt = subprog_jt(t, env);
+ if (IS_ERR(jt))
+ return PTR_ERR(jt);
+
+ insn_state[t] |= BRANCH;
+
+ if (check_add_overflow(env->cfg.gotox_edges, jt->cnt,
+ &env->cfg.gotox_edges) ||
+ env->cfg.gotox_edges > BPF_MAX_GOTOX_EDGES) {
+ verbose(env, "number of indirect jump edges in the program exceeds %u\n",
+ BPF_MAX_GOTOX_EDGES);
+ bpf_diag_program_structure(
+ env, t, "too many indirect jump edges",
+ "Reduce the number of indirect jumps, or the number of distinct targets they can reach.",
+ "The program has more than %u indirect jump edges in total, counted over every gotox instruction.",
+ BPF_MAX_GOTOX_EDGES);
+ return -E2BIG;
}
mark_prune_point(env, t);
for (i = 0; i < jt->cnt; i++) {
w = jt->items[i];
- if (w < 0 || w >= env->prog->len) {
- verbose(env, "indirect jump out of range from insn %d to %d\n", t, w);
- bpf_diag_program_structure(
- env, t, "indirect jump out of range",
- "Keep indirect jump targets inside the program.",
- "Instruction %d can jump indirectly to instruction %d, but the program only contains instructions 0 through %d.",
- t, w, env->prog->len - 1);
- return -EINVAL;
- }
+ if (verifier_bug_if(w < 0 || w >= env->prog->len, env,
+ "indirect jump from insn %d to %d out of range", t, w))
+ return -EFAULT;
mark_jmp_point(env, w);
mark_jump_target(env, w);
@@ -491,30 +534,6 @@ static int visit_func_ptrs_insn(int t, struct bpf_verifier_env *env,
return DONE_EXPLORING;
}
-/*
- * Instructions that can abnormally return from a subprog (tail_call
- * upon success, ld_{abs,ind} upon load failure) have a hidden exit
- * that the verifier must account for.
- */
-static int visit_abnormal_return_insn(struct bpf_verifier_env *env, int t)
-{
- struct bpf_subprog_info *subprog;
- struct bpf_iarray *jt;
-
- if (env->insn_aux_data[t].jt)
- return 0;
-
- jt = bpf_iarray_realloc(NULL, 2);
- if (!jt)
- return -ENOMEM;
-
- subprog = bpf_find_containing_subprog(env, t);
- jt->items[0] = t + 1;
- jt->items[1] = subprog->exit_idx;
- env->insn_aux_data[t].jt = jt;
- return 0;
-}
-
/* Visits the instruction at index t and returns one of the following:
* < 0 - an error occurred
* DONE_EXPLORING - the instruction was fully explored
@@ -537,13 +556,6 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
/* All non-branch instructions have a single fall-through edge. */
if (BPF_CLASS(insn->code) != BPF_JMP &&
BPF_CLASS(insn->code) != BPF_JMP32) {
- if (BPF_CLASS(insn->code) == BPF_LD &&
- (BPF_MODE(insn->code) == BPF_ABS ||
- BPF_MODE(insn->code) == BPF_IND)) {
- ret = visit_abnormal_return_insn(env, t);
- if (ret)
- return ret;
- }
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
return push_insn(t, t + insn_sz, FALLTHROUGH, env);
}
@@ -588,11 +600,6 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
mark_subprog_might_sleep(env, t);
if (bpf_helper_changes_pkt_data(insn->imm))
mark_subprog_changes_pkt_data(env, t);
- if (insn->imm == BPF_FUNC_tail_call) {
- ret = visit_abnormal_return_insn(env, t);
- if (ret)
- return ret;
- }
} else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
struct bpf_call_arg_meta meta;
@@ -825,7 +832,7 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
struct bpf_insn_aux_data *aux = env->insn_aux_data;
const u32 insn_cnt = env->prog->len;
int stack_sz, dfs_sz, err = 0;
- u32 *stack, *pre, *low, *dfs;
+ u32 *stack, *pre, *low, *dfs, *dfs_pos;
u32 i, j, t, w;
u32 next_preorder_num;
u32 next_scc_id;
@@ -838,13 +845,16 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
* - 'stack' accumulates vertices in DFS order, see invariant comment below;
* - 'pre[t] == p' => preorder number of vertex 't' is 'p';
* - 'low[t] == n' => smallest preorder number of the vertex reachable from 't' is 'n';
- * - 'dfs' DFS traversal stack, used to emulate explicit recursion.
+ * - 'dfs' DFS traversal stack, used to emulate explicit recursion;
+ * - 'dfs_pos[k] == j' => the frame 'dfs[k]' resumes visiting its
+ * successors at index 'j'.
*/
stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
pre = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
low = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
dfs = kvcalloc(insn_cnt, sizeof(*dfs), GFP_KERNEL_ACCOUNT);
- if (!stack || !pre || !low || !dfs) {
+ dfs_pos = kvcalloc(insn_cnt, sizeof(*dfs_pos), GFP_KERNEL_ACCOUNT);
+ if (!stack || !pre || !low || !dfs || !dfs_pos) {
err = -ENOMEM;
goto exit;
}
@@ -927,6 +937,7 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
stack_sz = 0;
dfs_sz = 1;
dfs[0] = i;
+ dfs_pos[0] = 0;
dfs_continue:
while (dfs_sz) {
w = dfs[dfs_sz - 1];
@@ -936,13 +947,22 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
next_preorder_num++;
stack[stack_sz++] = w;
}
- /* Visit 'w' successors */
+ /* Visit remaining 'w' successors */
succ = bpf_insn_successors(env, w);
- for (j = 0; j < succ->cnt; ++j) {
+ for (j = dfs_pos[dfs_sz - 1]; j < succ->cnt; ++j) {
if (pre[succ->items[j]]) {
low[w] = min(low[w], low[succ->items[j]]);
} else {
- dfs[dfs_sz++] = succ->items[j];
+ /*
+ * Once DFS for succ->items[j] is complete,
+ * pre[succ->items[j]] is non-zero, hence
+ * resuming at 'j' allows to follow the
+ * low[w] = min(...) update branch above.
+ */
+ dfs_pos[dfs_sz - 1] = j;
+ dfs_pos[dfs_sz] = 0;
+ dfs[dfs_sz] = succ->items[j];
+ dfs_sz++;
goto dfs_continue;
}
}
@@ -992,5 +1012,6 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
kvfree(pre);
kvfree(low);
kvfree(dfs);
+ kvfree(dfs_pos);
return err;
}
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index e9c2d6c06218f..37cf130ebb57b 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -214,13 +214,19 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
}
#endif
+enum bpf_patch_mode {
+ BPF_PATCH_KEEP_TARGET, /* targets of 'off' keep naming 'off', the first new insn */
+ BPF_PATCH_MOVE_TARGET, /* targets of 'off' follow the original insn to 'off + len - 1' */
+};
+
/* single env->prog->insni[off] instruction was replaced with the range
* insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
* [0, off) and [off, end) to new locations, so the patched range stays zero
*/
static void adjust_insn_aux_data(struct bpf_verifier_env *env,
struct bpf_prog *new_prog, u32 off, u32 cnt,
- struct bpf_insn *original_insn)
+ struct bpf_insn *original_insn,
+ enum bpf_patch_mode mode)
{
struct bpf_insn_aux_data *data = env->insn_aux_data;
struct bpf_insn *insn = new_prog->insnsi;
@@ -253,7 +259,12 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
/* Expand insni[off]'s seen count to the patched range. */
data[i].seen = old_seen;
data[i].zext_dst = bpf_insn_def32(new_prog, insn + i) >= 0;
- if (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
+ /*
+ * In BPF_PATCH_MOVE_TARGET mode the original insn is the last
+ * slot by construction.
+ */
+ if (mode == BPF_PATCH_KEEP_TARGET &&
+ !memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
data[off + cnt - 1].non_stack_access = false;
@@ -277,9 +288,10 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
* new instructions by the above memmove and memset, but the indirect jump target is
* actually the first instruction, so move it back. This also matches with the behavior
* of bpf_insn_array_adjust(), which preserves xlated_off to point to the first new
- * instruction.
+ * instruction. For BPF_PATCH_MOVE_TARGET the original instruction is the last one,
+ * so the flag already sits where needed.
*/
- if (data[off + cnt - 1].indirect_target) {
+ if (mode == BPF_PATCH_KEEP_TARGET && data[off + cnt - 1].indirect_target) {
data[off].indirect_target = 1;
data[off + cnt - 1].indirect_target = 0;
}
@@ -299,7 +311,7 @@ static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len
}
}
-static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 off, u32 len)
+static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 first, u32 len)
{
int i;
@@ -307,7 +319,7 @@ static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 off, u32 len)
return;
for (i = 0; i < env->insn_array_map_cnt; i++)
- bpf_insn_array_adjust(env->insn_array_maps[i], off, len);
+ bpf_insn_array_adjust(env->insn_array_maps[i], first, len);
}
static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 off, u32 len)
@@ -318,7 +330,7 @@ static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 of
bpf_insn_array_adjust_after_remove(env->insn_array_maps[i], off, len);
}
-static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len)
+static void adjust_poke_descs(struct bpf_prog *prog, u32 first, u32 len)
{
struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab;
int i, sz = prog->aux->size_poke_tab;
@@ -326,7 +338,7 @@ static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len)
for (i = 0; i < sz; i++) {
desc = &tab[i];
- if (desc->insn_idx <= off)
+ if (desc->insn_idx < first)
continue;
desc->insn_idx += len - 1;
}
@@ -345,11 +357,14 @@ static bool bpf_rewrite_must_abort(void)
return false;
}
-struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
- const struct bpf_insn *patch, u32 len)
+static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
+ const struct bpf_insn *patch, u32 len,
+ enum bpf_patch_mode mode)
{
struct bpf_prog *new_prog;
struct bpf_insn_aux_data *new_data = NULL;
+ /* The first slot whose targets move. */
+ u32 first = mode == BPF_PATCH_MOVE_TARGET ? off : off + 1;
struct bpf_insn original_insn;
if (bpf_rewrite_must_abort())
@@ -375,14 +390,20 @@ struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
env->insn_aux_data[off].orig_idx);
return NULL;
}
- adjust_insn_aux_data(env, new_prog, off, len, &original_insn);
+ adjust_insn_aux_data(env, new_prog, off, len, &original_insn, mode);
adjust_subprog_starts(env, off, len);
- adjust_insn_arrays(env, off, len);
+ adjust_insn_arrays(env, first, len);
bpf_adjust_func_ptrs(env, off, len);
- adjust_poke_descs(new_prog, off, len);
+ adjust_poke_descs(new_prog, first, len);
return new_prog;
}
+struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
+ const struct bpf_insn *patch, u32 len)
+{
+ return __bpf_patch_insn_data(env, off, patch, len, BPF_PATCH_KEEP_TARGET);
+}
+
/*
* insn was moved down by delta insns inside its own patch. Operands relative
* to the pc that point in front of the old position did not move with it.
@@ -480,7 +501,11 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
if (j > i) {
struct bpf_prog_aux *aux = env->prog->aux;
- int move;
+ int move, k;
+
+ /* The removed subprogs own their jump tables. */
+ for (k = i; k < j; k++)
+ kvfree(env->subprog_info[k].jt);
/* move fake 'exit' subprog as well */
move = env->subprog_cnt + 1 - j;
@@ -488,6 +513,9 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
memmove(env->subprog_info + i,
env->subprog_info + j,
sizeof(*env->subprog_info) * move);
+ /* The vacated tail still holds copies of the moved entries. */
+ memset(env->subprog_info + i + move, 0,
+ sizeof(*env->subprog_info) * (j - i));
env->subprog_cnt -= j - i;
/* remove func_info and its aux */
@@ -506,6 +534,9 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
* in adjust_btf_func() - no need to adjust
*/
}
+ /* The exception callback is live, so it cannot sit inside the removed range. */
+ if (env->exception_callback_subprog >= j)
+ env->exception_callback_subprog -= j - i;
} else {
/* convert i from "first prog to remove" to "first to adjust" */
if (env->subprog_info[i].start == off)
@@ -583,23 +614,6 @@ static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
return 0;
}
-/*
- * Clean up dynamically allocated fields of aux data for instructions [start, ...]
- */
-void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len)
-{
- struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
- int end = start + len;
- int i;
-
- for (i = start; i < end; i++) {
- if (aux_data[i].jt) {
- kvfree(aux_data[i].jt);
- aux_data[i].jt = NULL;
- }
- }
-}
-
static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
{
struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
@@ -612,8 +626,6 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
if (bpf_prog_is_offloaded(env->prog->aux))
bpf_prog_offload_remove_insns(env, off, cnt);
- bpf_clear_insn_aux_data(env, off, cnt);
-
err = bpf_remove_insns(env->prog, off, cnt);
if (err)
return err;
@@ -736,6 +748,7 @@ int bpf_opt_remove_dead_code(struct bpf_verifier_env *env)
int bpf_opt_remove_nops(struct bpf_verifier_env *env)
{
+ struct bpf_insn_aux_data *aux = env->insn_aux_data;
struct bpf_insn *insn = env->prog->insnsi;
int insn_cnt = env->prog->len;
bool is_may_goto_0, is_ja;
@@ -747,6 +760,8 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
if (!is_may_goto_0 && !is_ja)
continue;
+ if (aux[i].indirect_target)
+ continue;
err = verifier_remove_insns(env, i, 1);
if (err)
@@ -889,7 +904,8 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_1,
-subprogs[0].stack_depth);
insn_buf[cnt++] = env->prog->insnsi[0];
- new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
+ new_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,
+ BPF_PATCH_MOVE_TARGET);
if (!new_prog)
return -ENOMEM;
env->prog = new_prog;
@@ -912,7 +928,8 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
verifier_bug(env, "prologue is too long");
return -EFAULT;
} else if (cnt) {
- new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
+ new_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,
+ BPF_PATCH_MOVE_TARGET);
if (!new_prog)
return -ENOMEM;
@@ -1685,6 +1702,8 @@ static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *pat
env->prog = prog;
info[cnt + 1].start = info[cnt].start;
info[cnt].start = prog->len - len + 1;
+ /* The patch ends with the exit. */
+ info[cnt].exit_idx = prog->len - 1;
env->subprog_cnt++;
env->hidden_subprog_cnt++;
return 0;
@@ -2670,7 +2689,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
}
cnt++;
- new_prog = bpf_patch_insn_data(env, subprog_start, insn_buf, cnt);
+ new_prog = __bpf_patch_insn_data(env, subprog_start, insn_buf, cnt,
+ BPF_PATCH_MOVE_TARGET);
if (!new_prog)
return -ENOMEM;
env->prog = prog = new_prog;
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 16fb7dc43726f..cd9523f69298e 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -350,14 +350,18 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
struct bpf_iarray *succ, *jt;
int insn_sz;
- jt = env->insn_aux_data[idx].jt;
- if (unlikely(jt))
- return jt;
-
/* pre-allocated array of size up to 2; reset cnt, as it may have been used already */
succ = env->succ;
succ->cnt = 0;
+ /* All gotox of a subprogram share its jump table, see compute_subprog_jts() */
+ if (unlikely(insn_is_gotox(insn))) {
+ jt = bpf_find_containing_subprog(env, idx)->jt;
+ if (verifier_bug_if(!jt, env, "no jump table for gotox insn %u", idx))
+ return succ;
+ return jt;
+ }
+
opcode_info = &opcode_info_tbl[BPF_CLASS(insn->code) | BPF_OP(insn->code)];
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
if (opcode_info->can_fallthrough)
@@ -366,6 +370,20 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
if (opcode_info->can_jump)
succ->items[succ->cnt++] = idx + bpf_jmp_offset(insn) + 1;
+ /*
+ * tail_call upon success and ld_{abs,ind} upon load failure return
+ * from the subprogram, which is a hidden edge to its exit. A subprogram
+ * without an exit insn has no exit to model.
+ */
+ if (unlikely((bpf_helper_call(insn) && insn->imm == BPF_FUNC_tail_call) ||
+ (BPF_CLASS(insn->code) == BPF_LD &&
+ (BPF_MODE(insn->code) == BPF_ABS || BPF_MODE(insn->code) == BPF_IND)))) {
+ u32 exit_idx = bpf_find_containing_subprog(env, idx)->exit_idx;
+
+ if (exit_idx != U32_MAX)
+ succ->items[succ->cnt++] = exit_idx;
+ }
+
return succ;
}
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index c495dc9070dea..c1d6d217e0e86 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -2470,6 +2470,7 @@ static int add_subprog(struct bpf_verifier_env *env, int off)
return -E2BIG;
}
/* determine subprog starts. The end is one before the next starts */
+ env->subprog_info[env->subprog_cnt].exit_idx = U32_MAX;
env->subprog_info[env->subprog_cnt++].start = off;
sort(env->subprog_info, env->subprog_cnt,
sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
@@ -19252,10 +19253,25 @@ static int indirect_jump_min_max_index(struct bpf_verifier_env *env,
return 0;
}
+static int reject_gotox_out_of_subprog(struct bpf_verifier_env *env, u32 target,
+ u32 subprog_start, u32 subprog_end)
+{
+ verbose(env, "indirect jump from insn %d to %u leaves the subprog [%u,%u)\n",
+ env->insn_idx, target, subprog_start, subprog_end);
+ bpf_diag_program_structure(
+ env, env->insn_idx, "indirect jump leaves subprogram",
+ "Keep every reachable jump-table target inside the subprogram of the indirect jump.",
+ "Instruction %d can jump indirectly to instruction %u, which is outside its own subprogram [%u,%u).",
+ env->insn_idx, target, subprog_start, subprog_end);
+ return -EINVAL;
+}
+
/* gotox *dst_reg */
static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)
{
struct bpf_verifier_state *other_branch;
+ struct bpf_subprog_info *subprog;
+ u32 subprog_start, subprog_end;
struct bpf_reg_state *dst_reg;
struct bpf_map *map;
u32 min_index, max_index;
@@ -19299,6 +19315,24 @@ static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *in
return -EINVAL;
}
+ subprog = bpf_find_containing_subprog(env, env->insn_idx);
+ if (verifier_bug_if(!subprog, env, "no subprog contains insn %d", env->insn_idx))
+ return -EFAULT;
+ subprog_start = subprog->start;
+ subprog_end = (subprog + 1)->start;
+
+ /*
+ * Maps are confined to a subprog, see compute_subprog_jts(), and the
+ * targets are sorted and free of duplicates, see sort_insn_array_uniq(),
+ * so either all of them belong to this subprog or none of them does.
+ */
+ if (env->gotox_tmp_buf->items[0] < subprog_start)
+ return reject_gotox_out_of_subprog(env, env->gotox_tmp_buf->items[0],
+ subprog_start, subprog_end);
+ if (env->gotox_tmp_buf->items[n - 1] >= subprog_end)
+ return reject_gotox_out_of_subprog(env, env->gotox_tmp_buf->items[n - 1],
+ subprog_start, subprog_end);
+
for (i = 0; i < n - 1; i++) {
mark_indirect_target(env, env->gotox_tmp_buf->items[i]);
other_branch = push_stack(env, env->gotox_tmp_buf->items[i],
@@ -22750,8 +22784,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
release_maps(env);
release_btfs(env);
err_free_env:
- if (env->insn_aux_data)
- bpf_clear_insn_aux_data(env, 0, env->insn_aux_data_len);
+ bpf_free_subprog_jts(env);
vfree(env->insn_aux_data);
kvfree(env->fd_array);
bpf_stack_liveness_free(env);
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c b/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c
index 0222a9a5d0761..bfedeed24ac54 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <bpf/bpf.h>
+#include <bpf/btf.h>
#include <test_progs.h>
#if defined(__x86_64__) || defined(__powerpc__) || defined(__aarch64__)
@@ -453,43 +454,672 @@ static void check_bpf_no_lookup(void)
close(map_fd);
}
+#define GOTOX_LOG_SZ (256 * 1024)
+
+/* Load and return the fd, which the caller owns. */
+static int gotox_prog_load_funcs_fd(struct bpf_insn *insns, __u32 insn_cnt,
+ int *fd_array, __u32 fd_array_cnt, char *log,
+ int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt)
+{
+ LIBBPF_OPTS(bpf_prog_load_opts, opts);
+
+ log[0] = 0;
+ opts.fd_array = fd_array;
+ opts.fd_array_cnt = fd_array_cnt;
+ opts.log_buf = log;
+ opts.log_size = GOTOX_LOG_SZ;
+ opts.log_level = 1;
+ if (fi_cnt) {
+ opts.prog_btf_fd = btf_fd;
+ opts.func_info = fi;
+ opts.func_info_cnt = fi_cnt;
+ opts.func_info_rec_size = sizeof(*fi);
+ }
+
+ return bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, &opts);
+}
+
+/* Load for the outcome only: 0 or the negative errno. */
+static int gotox_prog_load_funcs(struct bpf_insn *insns, __u32 insn_cnt,
+ int *fd_array, __u32 fd_array_cnt, char *log,
+ int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt)
+{
+ int prog_fd;
+
+ prog_fd = gotox_prog_load_funcs_fd(insns, insn_cnt, fd_array, fd_array_cnt, log,
+ btf_fd, fi, fi_cnt);
+ if (prog_fd < 0)
+ return prog_fd;
+ close(prog_fd);
+ return 0;
+}
+
+static int gotox_prog_load(struct bpf_insn *insns, __u32 insn_cnt,
+ int *fd_array, __u32 fd_array_cnt, char *log)
+{
+ return gotox_prog_load_funcs(insns, insn_cnt, fd_array, fd_array_cnt, log,
+ -1, NULL, 0);
+}
+
+static int gotox_jt_create_offs(const __u32 *offs, __u32 cnt)
+{
+ struct bpf_insn_array_value val = {};
+ int map_fd;
+ __u32 i;
+
+ map_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, cnt);
+ if (!ASSERT_GE(map_fd, 0, "map_create"))
+ return map_fd;
+
+ for (i = 0; i < cnt; i++) {
+ val.orig_off = offs[i];
+ if (!ASSERT_EQ(bpf_map_update_elem(map_fd, &i, &val, 0), 0,
+ "bpf_map_update_elem"))
+ goto err;
+ }
+
+ if (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, "bpf_map_freeze"))
+ goto err;
+
+ return map_fd;
+err:
+ close(map_fd);
+ return -1;
+}
+
+#define GOTOX_SUB_START 4
+#define GOTOX_MAIN_TGT 2
+#define GOTOX_SUB_TGT 8
+#define GOTOX_TWO_INSN_CNT 10
+
+static void gotox_two_subprogs_fill(struct bpf_insn *insns, __u32 jt_idx, __u32 jt_off)
+{
+ insns[0] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ GOTOX_SUB_START - 1 - 1);
+ insns[GOTOX_MAIN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[3] = BPF_EXIT_INSN();
+
+ /* r1 = &jt[0], by index 'jt_idx' into fd_array */
+ insns[GOTOX_SUB_START] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = jt_idx,
+ };
+ insns[GOTOX_SUB_START + 1] = (struct bpf_insn) { .imm = 0 };
+ insns[6] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, jt_off * 8);
+ insns[7] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_SUB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insns[9] = BPF_EXIT_INSN();
+}
+
+/*
+ * Load 'insns' with the two jump tables 'jt_a' and 'jt_b', in this order in
+ * fd_array, and check the outcome. 'fi' describes the subprograms when given.
+ */
+static void check_gotox_two_maps(struct bpf_insn *insns, __u32 insn_cnt,
+ const __u32 *jt_a, __u32 a_cnt,
+ const __u32 *jt_b, __u32 b_cnt,
+ int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt,
+ int expect_err, const char *expect_msg)
+{
+ int map_fd[2] = { -1, -1 };
+ char *log;
+ int err;
+
+ log = calloc(1, GOTOX_LOG_SZ);
+ if (!ASSERT_OK_PTR(log, "calloc log"))
+ return;
+
+ map_fd[0] = gotox_jt_create_offs(jt_a, a_cnt);
+ if (map_fd[0] < 0)
+ goto free_log;
+ map_fd[1] = gotox_jt_create_offs(jt_b, b_cnt);
+ if (map_fd[1] < 0)
+ goto close_maps;
+
+ if (fi)
+ err = gotox_prog_load_funcs(insns, insn_cnt, map_fd, 2, log,
+ btf_fd, fi, fi_cnt);
+ else
+ err = gotox_prog_load(insns, insn_cnt, map_fd, 2, log);
+ if (expect_err)
+ ASSERT_EQ(err, expect_err, "program should have been rejected");
+ else
+ ASSERT_EQ(err, 0, "bpf(BPF_PROG_LOAD)");
+ if (expect_msg)
+ ASSERT_HAS_SUBSTR(log, expect_msg, "verifier log");
+
+close_maps:
+ close(map_fd[0]);
+ if (map_fd[1] >= 0)
+ close(map_fd[1]);
+free_log:
+ free(log);
+}
+
+static void check_gotox_target_other_subprog(void)
+{
+ const __u32 jt_main[] = { GOTOX_MAIN_TGT };
+ const __u32 jt_sub[] = { GOTOX_SUB_TGT };
+ struct bpf_insn insns[GOTOX_TWO_INSN_CNT];
+
+ gotox_two_subprogs_fill(insns, 0, 0);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,
+ -EINVAL, "indirect jump from insn 7 to 2 leaves the subprog [4,10)");
+}
+
+static void check_gotox_jt_per_subprog(void)
+{
+ const __u32 jt_main[] = { GOTOX_MAIN_TGT };
+ const __u32 jt_sub[] = { GOTOX_SUB_TGT };
+ struct bpf_insn insns[GOTOX_TWO_INSN_CNT];
+
+ gotox_two_subprogs_fill(insns, 1, 0);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,
+ 0, NULL);
+}
+
+#define GOTOX_DEAD_MAIN_LEN 7
+#define GOTOX_DEAD_SUB_LEN 6
+#define GOTOX_DEAD_INSN_CNT (GOTOX_DEAD_MAIN_LEN + 3 * GOTOX_DEAD_SUB_LEN)
+#define GOTOX_DEAD_LIVE_CNT 10
+
+/* r1 = &jt[0], by index 'jt_idx' into fd_array; r1 = *r1; gotox r1; r0 = 0; exit */
+static void gotox_sub_fill(struct bpf_insn *insns, __u32 start, __u32 jt_idx)
+{
+ insns[start] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = jt_idx,
+ };
+ insns[start + 1] = (struct bpf_insn) { .imm = 0 };
+ insns[start + 2] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+ insns[start + 3] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[start + 4] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[start + 5] = BPF_EXIT_INSN();
+}
+
+/*
+ * Two subprograms which are only called from dead code sit in front of the
+ * live one, and all three own a jump table. Dead code removal drops both in
+ * one go, which compacts subprog_info[] along with the tables.
+ */
+static void check_gotox_jt_dead_subprogs(void)
+{
+ const __u32 sub_start[3] = { GOTOX_DEAD_MAIN_LEN,
+ GOTOX_DEAD_MAIN_LEN + GOTOX_DEAD_SUB_LEN,
+ GOTOX_DEAD_MAIN_LEN + 2 * GOTOX_DEAD_SUB_LEN };
+ struct bpf_insn insns[GOTOX_DEAD_INSN_CNT];
+ int map_fd[3] = { -1, -1, -1 };
+ struct bpf_prog_info info = {};
+ __u32 len = sizeof(info), tgt, i;
+ int prog_fd;
+ char *log;
+
+ log = calloc(1, GOTOX_LOG_SZ);
+ if (!ASSERT_OK_PTR(log, "calloc log"))
+ return;
+
+ insns[0] = BPF_MOV64_IMM(BPF_REG_1, 0);
+ insns[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2);
+ insns[2] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ sub_start[0] - 2 - 1);
+ insns[3] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ sub_start[1] - 3 - 1);
+ insns[4] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ sub_start[2] - 4 - 1);
+ insns[5] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[6] = BPF_EXIT_INSN();
+
+ for (i = 0; i < ARRAY_SIZE(sub_start); i++) {
+ gotox_sub_fill(insns, sub_start[i], i);
+ tgt = sub_start[i] + 4;
+ map_fd[i] = gotox_jt_create_offs(&tgt, 1);
+ if (map_fd[i] < 0)
+ goto close_maps;
+ }
+
+ prog_fd = gotox_prog_load_funcs_fd(insns, ARRAY_SIZE(insns), map_fd,
+ ARRAY_SIZE(map_fd), log, -1, NULL, 0);
+ if (!ASSERT_GE(prog_fd, 0, "bpf(BPF_PROG_LOAD)"))
+ goto close_maps;
+
+ /* Both dead subprograms and the calls to them must be gone. */
+ if (ASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, &info, &len),
+ "bpf_prog_get_info_by_fd"))
+ ASSERT_EQ(info.xlated_prog_len / sizeof(struct bpf_insn),
+ GOTOX_DEAD_LIVE_CNT, "xlated insn count");
+ close(prog_fd);
+close_maps:
+ for (i = 0; i < ARRAY_SIZE(map_fd); i++)
+ if (map_fd[i] >= 0)
+ close(map_fd[i]);
+ free(log);
+}
+
+#define GOTOX_FWD_GOTOX 11
+#define GOTOX_FWD_OWN_TGT 12
+#define GOTOX_FWD_SUB_START 14
+#define GOTOX_FWD_INSN_CNT 16
+
+static void gotox_from_main_fill(struct bpf_insn *insns)
+{
+ insns[0] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
+ insns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ GOTOX_FWD_SUB_START - 1 - 1);
+ insns[2] = BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6,
+ offsetof(struct xdp_md, ingress_ifindex));
+ insns[3] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_2, 0, 4);
+
+ /* r1 = &jt_leaves[0], by index 1 into fd_array */
+ insns[4] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 1,
+ };
+ insns[5] = (struct bpf_insn) { .imm = 0 };
+ insns[6] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+ insns[7] = BPF_JMP_A(3);
+
+ /* r1 = &jt_own[0], by index 0 into fd_array */
+ insns[8] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 0,
+ };
+ insns[9] = (struct bpf_insn) { .imm = 0 };
+ insns[10] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+
+ insns[GOTOX_FWD_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_FWD_OWN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[13] = BPF_EXIT_INSN();
+ insns[GOTOX_FWD_SUB_START] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insns[15] = BPF_EXIT_INSN();
+}
+
+static void check_gotox_target_subprog_from_main(void)
+{
+ const __u32 jt_own[] = { GOTOX_FWD_OWN_TGT };
+ const __u32 jt_leaves[] = { GOTOX_FWD_SUB_START };
+ struct bpf_insn insns[GOTOX_FWD_INSN_CNT];
+
+ gotox_from_main_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_own, ARRAY_SIZE(jt_own),
+ jt_leaves, ARRAY_SIZE(jt_leaves), -1, NULL, 0,
+ -EINVAL, "indirect jump from insn 11 to 14 leaves the subprog [0,14)");
+}
+
+/*
+ * The only map of the subprog holding the gotox reaches past that subprog, so
+ * the subprog is left without a jump table at all.
+ */
+static void check_gotox_jt_spans_subprogs(void)
+{
+ const __u32 jt_span[] = { GOTOX_FWD_OWN_TGT, GOTOX_FWD_SUB_START };
+ const __u32 jt_leaves[] = { GOTOX_FWD_SUB_START };
+ struct bpf_insn insns[GOTOX_FWD_INSN_CNT];
+
+ gotox_from_main_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_span, ARRAY_SIZE(jt_span),
+ jt_leaves, ARRAY_SIZE(jt_leaves), -1, NULL, 0,
+ -EINVAL, "jump table of subprog starting at 0 spans multiple subprogs");
+}
+
+/*
+ * The subprog holding the gotox has a well formed jump table of its own and
+ * also collects a map that reaches past its end. The spanning map is still
+ * rejected, even though the subprog is not left without a table.
+ */
+static void check_gotox_jt_spans_with_own_table(void)
+{
+ const __u32 jt_own[] = { GOTOX_FWD_OWN_TGT };
+ const __u32 jt_span[] = { GOTOX_FWD_OWN_TGT, GOTOX_FWD_SUB_START };
+ struct bpf_insn insns[GOTOX_FWD_INSN_CNT];
+
+ gotox_from_main_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_own, ARRAY_SIZE(jt_own),
+ jt_span, ARRAY_SIZE(jt_span), -1, NULL, 0,
+ -EINVAL, "jump table of subprog starting at 0 spans multiple subprogs");
+}
+
+#define GOTOX_SLICE_SUB_START 6
+#define GOTOX_SLICE_GOTOX 14
+#define GOTOX_SLICE_SUB_TGT 15
+#define GOTOX_SLICE_INSN_CNT 17
+
+static void gotox_slice_fill(struct bpf_insn *insns)
+{
+ insns[0] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ GOTOX_SLICE_SUB_START - 1 - 1);
+ insns[2] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[3] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[4] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[5] = BPF_EXIT_INSN();
+
+ insns[GOTOX_SLICE_SUB_START] =
+ BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+ offsetof(struct xdp_md, ingress_ifindex));
+ insns[7] = BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 1);
+ insns[8] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1);
+ insns[9] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);
+
+ /* r1 = &jt_main[0], by index 0 into fd_array */
+ insns[10] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 0,
+ };
+ insns[11] = (struct bpf_insn) { .imm = 0 };
+ insns[12] = BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2);
+ insns[13] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+
+ insns[GOTOX_SLICE_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_SLICE_SUB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insns[16] = BPF_EXIT_INSN();
+}
+
+static void check_gotox_index_slice_other_subprog(void)
+{
+ const __u32 jt_main[] = { 2, 3, 4 };
+ const __u32 jt_sub[] = { GOTOX_SLICE_SUB_TGT };
+ struct bpf_insn insns[GOTOX_SLICE_INSN_CNT];
+
+ gotox_slice_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,
+ -EINVAL, "indirect jump from insn 14 to 3 leaves the subprog [6,17)");
+}
+
+static int gotox_btf_create(const __u32 *starts, const __u8 *linkage, __u32 cnt,
+ struct bpf_func_info *fi, struct btf **pbtf)
+{
+ int int_id, proto_id, id;
+ struct btf *btf;
+ char name[24];
+ __u32 i;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "btf__new_empty"))
+ return -1;
+
+ int_id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
+ if (!ASSERT_GT(int_id, 0, "btf__add_int"))
+ goto err;
+
+ proto_id = btf__add_func_proto(btf, int_id);
+ if (!ASSERT_GT(proto_id, 0, "btf__add_func_proto"))
+ goto err;
+
+ for (i = 0; i < cnt; i++) {
+ snprintf(name, sizeof(name), "gotox_f%u", i);
+ id = btf__add_func(btf, name, linkage[i], proto_id);
+ if (!ASSERT_GT(id, 0, "btf__add_func"))
+ goto err;
+ fi[i].insn_off = starts[i];
+ fi[i].type_id = id;
+ }
+
+ if (!ASSERT_OK(btf__load_into_kernel(btf), "btf__load_into_kernel"))
+ goto err;
+
+ *pbtf = btf;
+ return btf__fd(btf);
+err:
+ btf__free(btf);
+ return -1;
+}
+
+static void check_gotox_target_other_global_subprog(void)
+{
+ const __u32 starts[] = { 0, GOTOX_SUB_START };
+ const __u8 linkage[] = { BTF_FUNC_GLOBAL, BTF_FUNC_GLOBAL };
+ const __u32 jt_main[] = { GOTOX_MAIN_TGT };
+ const __u32 jt_sub[] = { GOTOX_SUB_TGT };
+ struct bpf_insn insns[GOTOX_TWO_INSN_CNT];
+ struct bpf_func_info fi[2];
+ struct btf *btf = NULL;
+ int btf_fd;
+
+ gotox_two_subprogs_fill(insns, 0, 0);
+
+ btf_fd = gotox_btf_create(starts, linkage, ARRAY_SIZE(starts), fi, &btf);
+ if (btf_fd < 0)
+ return;
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), btf_fd, fi, ARRAY_SIZE(fi),
+ -EINVAL, "indirect jump from insn 7 to 2 leaves the subprog [4,10)");
+ btf__free(btf);
+}
+
+#define GOTOX_CB_MAIN_TGT 6
+#define GOTOX_CB_START 8
+#define GOTOX_CB_GOTOX 11
+#define GOTOX_CB_TGT 12
+#define GOTOX_CB_INSN_CNT 14
+
+static void gotox_callback_fill(struct bpf_insn *insns)
+{
+ insns[0] = BPF_MOV64_IMM(BPF_REG_1, 1);
+ /* r2 = &callback */
+ insns[1] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_2,
+ .src_reg = BPF_PSEUDO_FUNC,
+ .imm = GOTOX_CB_START - 1 - 1,
+ };
+ insns[2] = (struct bpf_insn) { .imm = 0 };
+ insns[3] = BPF_MOV64_IMM(BPF_REG_3, 0);
+ insns[4] = BPF_MOV64_IMM(BPF_REG_4, 0);
+ insns[5] = BPF_EMIT_CALL(BPF_FUNC_loop);
+ insns[GOTOX_CB_MAIN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[7] = BPF_EXIT_INSN();
+
+ /* r1 = &jt_main[0], by index 0 into fd_array */
+ insns[GOTOX_CB_START] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 0,
+ };
+ insns[9] = (struct bpf_insn) { .imm = 0 };
+ insns[10] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+ insns[GOTOX_CB_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_CB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[13] = BPF_EXIT_INSN();
+}
+
+static void check_gotox_callback_leaves_subprog(void)
+{
+ const __u32 starts[] = { 0, GOTOX_CB_START };
+ const __u8 linkage[] = { BTF_FUNC_GLOBAL, BTF_FUNC_STATIC };
+ const __u32 jt_main[] = { GOTOX_CB_MAIN_TGT };
+ const __u32 jt_cb[] = { GOTOX_CB_TGT };
+ struct bpf_insn insns[GOTOX_CB_INSN_CNT];
+ struct bpf_func_info fi[2];
+ struct btf *btf = NULL;
+ int btf_fd;
+
+ gotox_callback_fill(insns);
+
+ btf_fd = gotox_btf_create(starts, linkage, ARRAY_SIZE(starts), fi, &btf);
+ if (btf_fd < 0)
+ return;
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_cb, ARRAY_SIZE(jt_cb), btf_fd, fi, ARRAY_SIZE(fi),
+ -EINVAL, "indirect jump from insn 11 to 6 leaves the subprog [8,14)");
+ btf__free(btf);
+}
+
static void check_bpf_side(void)
{
check_bpf_no_lookup();
}
+/* Return the xlated index of the first insn equal to 'insn', or -1. */
+static int xlated_insn_index(int prog_fd, const struct bpf_insn *insn)
+{
+ struct bpf_insn *xlated;
+ int idx = -1;
+ __u32 cnt, i;
+
+ if (!ASSERT_OK(get_xlated_program(prog_fd, &xlated, &cnt), "get_xlated_program"))
+ return -1;
+ for (i = 0; i < cnt; i++)
+ if (!memcmp(&xlated[i], insn, sizeof(*insn))) {
+ idx = i;
+ break;
+ }
+ free(xlated);
+ return idx;
+}
+
+/*
+ * Load 'insns' with a jump table naming insn 0, which a prologue prepend
+ * pushes down, and check that the slot followed it.
+ */
+static void check_gotox_target_prepend(enum bpf_prog_type prog_type,
+ struct bpf_insn *insns, __u32 insn_cnt)
+{
+ LIBBPF_OPTS(bpf_prog_load_opts, opts);
+ struct bpf_insn_array_value val = {};
+ int prog_fd = -1, map_fd, idx;
+ __u32 key = 0;
+
+ map_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, 1);
+ if (!ASSERT_GE(map_fd, 0, "map_create"))
+ return;
+
+ val.orig_off = 0;
+ if (!ASSERT_EQ(bpf_map_update_elem(map_fd, &key, &val, 0), 0, "bpf_map_update_elem"))
+ goto cleanup;
+ if (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, "bpf_map_freeze"))
+ goto cleanup;
+
+ opts.fd_array = &map_fd;
+ opts.fd_array_cnt = 1;
+ prog_fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insn_cnt, &opts);
+ if (!ASSERT_GE(prog_fd, 0, "bpf(BPF_PROG_LOAD)"))
+ goto cleanup;
+
+ idx = xlated_insn_index(prog_fd, &insns[0]);
+ if (!ASSERT_GT(idx, 0, "insn 0 was prepended to"))
+ goto cleanup;
+ if (!ASSERT_EQ(bpf_map_lookup_elem(map_fd, &key, &val), 0, "bpf_map_lookup_elem"))
+ goto cleanup;
+ ASSERT_EQ(val.xlated_off, (__u32)idx, "gotox target followed insn 0 past the prepend");
+cleanup:
+ if (prog_fd >= 0)
+ close(prog_fd);
+ close(map_fd);
+}
+
+static void check_gotox_target_prologue_shift(void)
+{
+ struct bpf_insn insns[] = {
+ /* insn 0: gotox target and subprog start */
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ /* may_goto +4 -> exit block, bounds the loop */
+ BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 4, 0),
+ /* r1 = &jt[0] (insns 2 and 3) */
+ BPF_LD_IMM64_RAW(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, 0),
+ BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0), /* r1 = ips[0] */
+ /* insn 5: gotox r1 -> insn 0 */
+ BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0),
+ BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), /* insn 6: exit block */
+ BPF_EXIT_INSN(),
+ };
+
+ check_gotox_target_prepend(BPF_PROG_TYPE_XDP, insns, ARRAY_SIZE(insns));
+}
+
+static void check_gotox_target_ctx_prologue_shift(void)
+{
+ struct bpf_insn insns[] = {
+ /* insn 0: gotox target and subprog start */
+ BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, -8),
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, 0x5a5a, 12), /* second visit exits */
+ BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x5a5a),
+ BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* r6 = ctx */
+ BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6, offsetof(struct __sk_buff, data)),
+ BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_6, offsetof(struct __sk_buff, data_end)),
+ BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 1),
+ BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 5),
+ /* insn 9: direct packet write -> tc unclone prologue prepended at insn 0 */
+ BPF_ST_MEM(BPF_B, BPF_REG_2, 0, 0),
+ /* r5 = &jt[0] (insns 10 and 11) */
+ BPF_LD_IMM64_RAW(BPF_REG_5, BPF_PSEUDO_MAP_IDX_VALUE, 0),
+ BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_5, 0), /* r5 = ips[0] */
+ /* insn 13: gotox r5 -> insn 0 */
+ BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_5, 0, 0, 0),
+ BPF_MOV64_IMM(BPF_REG_0, 0), /* insn 14: exit block */
+ BPF_EXIT_INSN(),
+ };
+
+ check_gotox_target_prepend(BPF_PROG_TYPE_SCHED_CLS, insns, ARRAY_SIZE(insns));
+}
+
static void __test_bpf_insn_array(void)
{
/* Test if offsets are adjusted properly */
-
if (test__start_subtest("one2one"))
check_one_to_one_mapping();
-
if (test__start_subtest("simple"))
check_simple();
-
if (test__start_subtest("deletions"))
check_deletions();
-
if (test__start_subtest("deletions-with-functions"))
check_deletions_with_functions();
-
if (test__start_subtest("blindness"))
check_blindness();
-
/* Check all kinds of operations and related restrictions */
-
if (test__start_subtest("incorrect-index"))
check_incorrect_index();
-
if (test__start_subtest("load-unfrozen-map"))
check_load_unfrozen_map();
-
if (test__start_subtest("no-map-reuse"))
check_no_map_reuse();
-
if (test__start_subtest("bpf-side-ops"))
check_bpf_side();
+ if (test__start_subtest("gotox-jt-spans-subprogs"))
+ check_gotox_jt_spans_subprogs();
+ if (test__start_subtest("gotox-jt-spans-with-own-table"))
+ check_gotox_jt_spans_with_own_table();
+ if (test__start_subtest("gotox-target-other-subprog"))
+ check_gotox_target_other_subprog();
+ if (test__start_subtest("gotox-jt-per-subprog"))
+ check_gotox_jt_per_subprog();
+ if (test__start_subtest("gotox-jt-dead-subprogs"))
+ check_gotox_jt_dead_subprogs();
+ if (test__start_subtest("gotox-target-subprog-from-main"))
+ check_gotox_target_subprog_from_main();
+ if (test__start_subtest("gotox-index-slice-other-subprog"))
+ check_gotox_index_slice_other_subprog();
+ if (test__start_subtest("gotox-target-other-global-subprog"))
+ check_gotox_target_other_global_subprog();
+ if (test__start_subtest("gotox-callback-leaves-subprog"))
+ check_gotox_callback_leaves_subprog();
+ if (test__start_subtest("gotox-target-prologue-shift"))
+ check_gotox_target_prologue_shift();
+ if (test__start_subtest("gotox-target-ctx-prologue-shift"))
+ check_gotox_target_ctx_prologue_shift();
}
#else
static void __test_bpf_insn_array(void)
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions.c b/tools/testing/selftests/bpf/prog_tests/exceptions.c
index 639866ce09a9f..3705db18c1bbc 100644
--- a/tools/testing/selftests/bpf/prog_tests/exceptions.c
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions.c
@@ -6,6 +6,7 @@
#include "exceptions_ext.skel.h"
#include "exceptions_fail.skel.h"
#include "exceptions_assert.skel.h"
+#include "exceptions_dead_subprog.skel.h"
static char log_buf[1024 * 1024];
@@ -410,9 +411,15 @@ static void test_exceptions_assertions(void)
RUN_TESTS(exceptions_assert);
}
+static void test_exceptions_dead_subprog(void)
+{
+ RUN_TESTS(exceptions_dead_subprog);
+}
+
void test_exceptions(void)
{
test_exceptions_success();
test_exceptions_failure();
test_exceptions_assertions();
+ test_exceptions_dead_subprog();
}
diff --git a/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c b/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c
new file mode 100644
index 0000000000000..4f8b82a85234f
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+/*
+ * Read-only and frozen, so the verifier sees a constant zero, while clang
+ * cannot fold the volatile read and keeps the call below.
+ */
+const volatile int never;
+
+/* Only called from dead code, hence removed as a whole before the JIT runs. */
+static __noinline int dead_subprog(struct __sk_buff *ctx)
+{
+ return ctx->len;
+}
+
+__noinline int exception_cb_behind_dead_subprog_cb(u64 cookie)
+{
+ return 42;
+}
+
+/*
+ * libbpf appends the exception callback behind the subprograms reached by
+ * calls, so removing the dead one compacts subprog_info[] underneath the
+ * callback's index.
+ */
+SEC("?tc")
+__exception_cb(exception_cb_behind_dead_subprog_cb)
+__success __retval(42)
+int exception_cb_behind_dead_subprog(struct __sk_buff *ctx)
+{
+ if (never)
+ return dead_subprog(ctx);
+ bpf_throw(0);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_cfg.c b/tools/testing/selftests/bpf/progs/verifier_cfg.c
index 3c3bb03e8217a..07388a922e5b5 100644
--- a/tools/testing/selftests/bpf/progs/verifier_cfg.c
+++ b/tools/testing/selftests/bpf/progs/verifier_cfg.c
@@ -173,4 +173,42 @@ l1_%=: exit; \
" ::: __clobber_all);
}
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u32);
+} map_array SEC(".maps");
+
+extern void bpf_throw(__u64 cookie) __ksym;
+
+/*
+ * The subprogram leaves through the tail call or the exception and never
+ * reaches an exit insn, so the hidden edge of the tail call has no exit to
+ * point at. The stack access makes the liveness pass walk the subprogram,
+ * and the loop compiles to a single jump back onto itself.
+ */
+__noinline __used
+static int tail_call_or_throw_subprog(void *ctx)
+{
+ volatile __u32 idx = 0;
+
+ bpf_tail_call(ctx, &map_array, idx);
+ bpf_throw(0);
+ for (;;) {}
+}
+
+SEC("tc")
+__description("tail call in a subprogram without an exit")
+__arch_x86_64
+__arch_arm64
+__success
+__naked void tail_call_in_subprog_without_exit(void)
+{
+ asm volatile (" \
+ call tail_call_or_throw_subprog; \
+ exit; \
+" ::: __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_gotox.c b/tools/testing/selftests/bpf/progs/verifier_gotox.c
index 0e27c2c79c57e..f5a9878c7b8d4 100644
--- a/tools/testing/selftests/bpf/progs/verifier_gotox.c
+++ b/tools/testing/selftests/bpf/progs/verifier_gotox.c
@@ -366,7 +366,7 @@ __used static int test_subprog(void)
}
SEC("socket")
-__failure __msg("jump table for insn 4 points outside of the subprog [0,10]")
+__failure __msg("jump table of subprog starting at 0 spans multiple subprogs")
__naked void jump_table_outside_subprog(void)
{
asm volatile (" \
@@ -457,6 +457,129 @@ __naked void spill_fill_ptr_to_insn(void)
: __clobber_all);
}
+/*
+ * Emit each table entry next to the instruction it targets. Numeric labels
+ * can be reused by .rept, while %= keeps table symbols unique per asm block.
+ * BASE is the code section: libbpf relocates section-relative byte offsets
+ * to instruction offsets in the loaded program.
+ */
+#define GOTOX_TABLE_BEGIN(COUNT) \
+ ".pushsection .jumptables,\"\",@progbits;" \
+ "jt_%=:;" \
+ ".global jt_%=;" \
+ ".size jt_%=, 8 * (" #COUNT ");" \
+ ".popsection;" \
+ "r1 = jt_%= ll;"
+
+#define GOTOX_TABLE_ENTRY(LABEL, BASE) \
+ ".pushsection .jumptables,\"\",@progbits;" \
+ ".quad " LABEL " - " BASE ";" \
+ ".popsection;"
+
+/* N gotox instructions, each targeting all N gotox plus the final block. */
+#define GOTOX_SELF_TARGETS(N, BASE) \
+ GOTOX_TABLE_BEGIN(N + 1) \
+ "r0 = 0;" \
+ ".rept " #N ";" \
+ GOTOX_TABLE_ENTRY("1f", BASE) \
+ "1:; .8byte %[gotox];" \
+ ".endr;" \
+ GOTOX_TABLE_ENTRY("2f", BASE) \
+ "2:;"
+
+#define GOTOX_OPERAND \
+ __imm_insn(gotox, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0, 0))
+
+/*
+ * Count edges even when their targets are already discovered. These
+ * programs deliberately use a scalar: the CFG edge limit must reject them
+ * before symbolic verification reaches the invalid gotox operand.
+ */
+SEC("socket")
+__description("too-many-gotox-edges")
+__failure __msg("number of indirect jump edges in the program exceeds")
+__naked void too_many_gotox_edges(void)
+{
+ asm volatile (
+ /* 1000 * 1001 = 1,001,000 edges. */
+ GOTOX_SELF_TARGETS(1000, "socket")
+ "r0 = 0; exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+SEC("socket")
+__description("gotox-edges-at-limit")
+__success __retval(0)
+__naked void gotox_edges_at_limit(void)
+{
+ asm volatile (
+ /*
+ * 1000 gotox * 1000 targets = 1,000,000 CFG edges. At run
+ * time, each block loads the next table entry and jumps to
+ * the following block, so the program terminates.
+ */
+ GOTOX_TABLE_BEGIN(1000)
+ ".rept 1000;"
+ GOTOX_TABLE_ENTRY("1f", "socket")
+ "r0 = *(u64 *)(r1 + 0);"
+ "r1 += 8;"
+ ".8byte %[gotox];"
+ "1:;"
+ ".endr;"
+ "r0 = 0; exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+static __noinline __used __naked void gotox_edges_subprog(void)
+{
+ asm volatile (
+ GOTOX_SELF_TARGETS(750, ".text")
+ "r0 = 0; exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+SEC("socket")
+__description("gotox-edges-across-subprogs")
+__failure __msg("number of indirect jump edges in the program exceeds")
+__naked void gotox_edges_across_subprogs(void)
+{
+ asm volatile (
+ /* Each subprog has 563,250 edges; together, 1,126,500. */
+ GOTOX_SELF_TARGETS(750, "socket")
+ "call gotox_edges_subprog;"
+ "exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+/*
+ * The gotox target is a ja +0, which the nop removal pass must keep as the
+ * jump table still names it.
+ */
+SEC("socket")
+__success __retval(0)
+__xlated("goto pc+0")
+__naked void jump_table_target_nop(void)
+{
+ asm volatile (" \
+ .pushsection .jumptables,\"\",@progbits; \
+jt0_%=: \
+ .quad nop_%= - socket; \
+ .size jt0_%=, 8; \
+ .global jt0_%=; \
+ .popsection; \
+ \
+ r0 = jt0_%= ll; \
+ r0 = *(u64 *)(r0 + 0); \
+ .8byte %[gotox_r0]; \
+nop_%=: \
+ goto +0; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_insn(gotox_r0, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0, 0))
+ : __clobber_all);
+}
+
#endif /* __TARGET_ARCH_x86 || __TARGET_ARCH_arm64 || __TARGET_ARCH_powerpc*/
char _license[] SEC("license") = "GPL";
|
| 3/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30041 output: 34 thoughts: 867 Thoughts: **BPF Patch Series Evaluation for Syzkaller Fuzzing** Okay, let's break this down. The objective is to determine if this Linux kernel patch series merits fuzzing with syzkaller. The instructions lay out a clear framework. Reachability is the *key* factor. I'm not going to blindly trust claims of "no functional change" or refactoring. Any new or modified assertions in reachable code are definitely worth fuzzing. My initial scan tells me this series touches the BPF subsystem, specifically the verifier, control-flow graph analysis, fixups, liveness analysis, and instruction array map within `kernel/bpf/`. BPF is a *core* subsystem, and critically, it's reachable from userspace via the `bpf()` syscall. No question about it, this screams `WorthFuzzing = true`. Now, I need to get more granular. I need to zero in on specific functions and entry points to guide the fuzzing effort effectively. It's time to check what symbols are exposed in kallsyms or if they're non-static. Let's look at the touched files: * `kernel/bpf/bpf_insn_array.c`: I see `bpf_insn_array_init`, `bpf_insn_array_release`, and `bpf_insn_array_adjust`. I need to see how they're used. * `kernel/bpf/cfg.c`: I see `bpf_free_subprog_jts`, `bpf_compute_scc`, and a handful of `bpf_check_cfg` and `visit_gotox_insn` symbols that warrant close examination. * `kernel/bpf/fixups.c`: `bpf_patch_insn_data`, `__bpf_patch_insn_data`, `adjust_insn_aux_data`, and a slew of other symbols with "fixup" in their name. These likely have crucial impact on program correctness. * `kernel/bpf/liveness.c`: The function `bpf_insn_successors` catches my attention. * `kernel/bpf/verifier.c`: `check_indirect_jump` and `bpf_check` are critical to verify. My focus should be on *specific* feature logic and entrypoints, rather than ubiquitous lifecycle paths. I will need to use `grepper` or `read-file` to check kernel/bpf/cfg.c, kernel/bpf/fixups.c, kernel/bpf/verifier.c, kernel/bpf/liveness.c, kernel/bpf/bpf_insn_array.c. This will let me determine if symbols are visible in kallsyms or if they are non-static. Given the nature of the BPF subsystem and the touched functions, this is an excellent candidate for fuzzing with syzkaller. |
| 4/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "bpf_free_subprog_jts",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/cfg.c=355=static int compute_subprog_jts(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-360-\tif (err)\nkernel/bpf/cfg.c:361:\t\tbpf_free_subprog_jts(env);\nkernel/bpf/cfg.c-362-\treturn err;\n--\nkernel/bpf/cfg.c-364-\nkernel/bpf/cfg.c:365:void bpf_free_subprog_jts(struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-366-{\n--\nkernel/bpf/verifier.c=22438=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22786-err_free_env:\nkernel/bpf/verifier.c:22787:\tbpf_free_subprog_jts(env);\nkernel/bpf/verifier.c-22788-\tvfree(env-\u003einsn_aux_data);\n"
} |
| 5/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31901 output: 31 thoughts: 180 Thoughts: **Analyzing BPF Function Changes** Alright, let's dive into these BPF code modifications. My initial focus is always on the non-static functions, as those are the ones that potentially expose new functionality or alter the external API. Okay, so I see these new functions in `kernel/bpf/bpf_insn_array.c`: `bpf_insn_array_init`, `bpf_insn_array_release`, and `bpf_insn_array_adjust`. I need to understand what those are doing and if they have any implications on existing programs or the BPF verifier. Then, moving on to `kernel/bpf/cfg.c`, I spot `bpf_free_subprog_jts`, and `bpf_compute_scc`. Those are worth a quick look, though I'm immediately drawn to those two functions, `visit_gotox_insn` and `visit_insn`, in the same file. I remember those from the CFG analysis. Let me check who calls them. Ah, yes, `bpf_check_cfg`. So they are part of the CFG checker. Now I'm curious what's changed there; a closer look at the call sites will be important to understand the overall impact on the system. |
| 6/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "visit_gotox_insn",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/cfg.c=377=subprog_jt(int t, struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-406-/* \"conditional jump with N edges\" */\nkernel/bpf/cfg.c:407:static int visit_gotox_insn(int t, struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-408-{\n--\nkernel/bpf/cfg.c=542=static int visit_insn(int t, struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-637-\t\tif (BPF_SRC(insn-\u003ecode) == BPF_X)\nkernel/bpf/cfg.c:638:\t\t\treturn visit_gotox_insn(t, env);\nkernel/bpf/cfg.c-639-\n"
} |
| 7/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32625 output: 31 thoughts: 55 Thoughts: Here's my analysis of the provided information, as if these are my own thoughts: **Dissecting the BPF Control Flow Graph (CFG) Analysis** Okay, so I see that `visit_insn` is somehow connected to the `bpf_check_cfg` function. That's key. `bpf_check_cfg` isn't static, meaning it's accessible from outside of its source file, `cfg.c`. Right, so my immediate thought is to go directly to `kernel/bpf/cfg.c` and take a look at `bpf_check_cfg`. I need to understand how `visit_insn` is being used within the CFG verification process. Let's see what we've got in there. |
| 8/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "int bpf_check_cfg",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/cfg.c=542=static int visit_insn(int t, struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-672- */\nkernel/bpf/cfg.c:673:int bpf_check_cfg(struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-674-{\n"
} |
| 9/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32986 output: 32 thoughts: 38 Thoughts: Okay, let's dive into this. I need to get a handle on the code flow, specifically regarding BPF liveness analysis. So, my immediate focus is on `kernel/bpf/liveness.c`. This is where the magic should be happening. **Initial Code Investigation: BPF Liveness and `bpf_insn_successors`** My thought process is: *What's the first step in understanding the analysis?* I need to see how the code traverses the BPF instructions. And that means understanding how the successor instructions for each instruction are determined. The key to that, I suspect, lies in something called `bpf_insn_successors`. Is it static, is it global? Does it rely on state? That's what I have to figure out immediately. I'll need to know the scope and accessibility of this function to understand how the control flow graph is constructed. The properties of this function will inform all that follows. Let's see. |
| 10/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "bpf_insn_successors",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/cfg.c=775=int bpf_compute_postorder(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-802-\t\t\t}\nkernel/bpf/cfg.c:803:\t\t\tsucc = bpf_insn_successors(env, top);\nkernel/bpf/cfg.c-804-\t\t\tfor (s = 0; s \u003c succ-\u003ecnt; ++s) {\n--\nkernel/bpf/cfg.c=828=int bpf_compute_scc(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-950-\t\t\t/* Visit remaining 'w' successors */\nkernel/bpf/cfg.c:951:\t\t\tsucc = bpf_insn_successors(env, w);\nkernel/bpf/cfg.c-952-\t\t\tfor (j = dfs_pos[dfs_sz - 1]; j \u003c succ-\u003ecnt; ++j) {\n--\nkernel/bpf/const_fold.c=236=int bpf_compute_const_regs(struct bpf_verifier_env *env)\n--\nkernel/bpf/const_fold.c-271-\nkernel/bpf/const_fold.c:272:\t\tsucc = bpf_insn_successors(env, idx);\nkernel/bpf/const_fold.c-273-\t\tfor (int s = 0; s \u003c succ-\u003ecnt; s++)\n--\nkernel/bpf/liveness.c=319=inline struct bpf_iarray *\nkernel/bpf/liveness.c:320:bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\nkernel/bpf/liveness.c-321-{\n--\nkernel/bpf/liveness.c=392=static inline bool update_insn(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-402-\nkernel/bpf/liveness.c:403:\tsucc = bpf_insn_successors(env, insn_idx);\nkernel/bpf/liveness.c-404-\tif (succ-\u003ecnt == 0)\n--\nkernel/bpf/liveness.c=1820=static int compute_subprog_args(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-1907-\t\t/* Propagate to successors within this subprogram */\nkernel/bpf/liveness.c:1908:\t\tsucc = bpf_insn_successors(env, idx);\nkernel/bpf/liveness.c-1909-\t\tfor (int s = 0; s \u003c succ-\u003ecnt; s++) {\n--\nkernel/bpf/liveness.c=2469=int bpf_compute_live_registers(struct bpf_verifier_env *env)\n--\nkernel/bpf/liveness.c-2527-\nkernel/bpf/liveness.c:2528:\t\t\tsucc = bpf_insn_successors(env, insn_idx);\nkernel/bpf/liveness.c-2529-\t\t\tfor (int s = 0; s \u003c succ-\u003ecnt; ++s)\n--\nkernel/bpf/verifier.c=19462=static int do_check(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-19638-\t\t\t * calls. These must never skip the following insn\nkernel/bpf/verifier.c:19639:\t\t\t * (i.e., bpf_insn_successors()'s opcode_info.can_jump\nkernel/bpf/verifier.c-19640-\t\t\t * is false). Still, add a warning to document this in\n"
} |
| 11/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34223 output: 30 thoughts: 56 Thoughts: Here's my take on those thoughts: **Analyzing the BPF Instruction Successor Function** Okay, so `bpf_insn_successors`. I need to figure out how this function is defined and used. I see that it's in `kernel/bpf/liveness.c`, and it's declared inline, returning a `struct bpf_iarray*`. That's a good starting point. Now, to truly understand its role and potential impact, I need to dig into the headers. Knowing where it's declared in the headers is crucial because that's where the function's interface is exposed and how different parts of the kernel interact with it. I'll need to check the relevant header files for its declaration, see what parameters it takes, what it's expected to return, and potentially how other kernel components might call it. This will help me build a complete picture of its behavior within the BPF ecosystem. Time to start the header hunt! |
| 12/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "bpf_insn_successors",
"PathPrefix": "include/"
}Results: {
"Output": "include/linux/bpf_verifier.h=608=struct bpf_map_ptr_state {\n--\ninclude/linux/bpf_verifier.h-624- * An array of BPF instructions.\ninclude/linux/bpf_verifier.h:625: * Primary usage: return value of bpf_insn_successors.\ninclude/linux/bpf_verifier.h-626- */\n--\ninclude/linux/bpf_verifier.h=1571=int bpf_jmp_offset(struct bpf_insn *insn);\ninclude/linux/bpf_verifier.h:1572:struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);\ninclude/linux/bpf_verifier.h-1573-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask);\n"
} |
| 13/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34760 output: 31 thoughts: 445 Thoughts: **My Initial Assessment of `bpf_insn_successors` and Related Code** Okay, so `bpf_insn_successors` has external linkage, which immediately flags a potential area of interest for broader implications. I need to understand its connections. My first instinct is to dive into `kernel/bpf/fixups.c`. Let's see what functions are modified or directly involved: I see `bpf_patch_insn_data` which is *non-static*, so it's a key candidate. Then there's `adjust_insn_aux_data`, a *static* function, but it's called by `__bpf_patch_insn_data` which is called by `bpf_patch_insn_data`, `bpf_convert_ctx_accesses`, and `bpf_do_misc_fixups`, so it's critical. `adjust_subprog_starts_after_remove` is also *static*, and it is called by `adjust_subprog_starts_after_remove` -> `verifier_remove_insns` / `bpf_opt_remove_dead_code`. Then there are the *non-static* functions `bpf_opt_remove_nops`, `bpf_convert_ctx_accesses`, and `bpf_do_misc_fixups`. The *static* function `add_hidden_subprog` is called inside `bpf_convert_ctx_accesses`. So, these all seem like the immediate dependencies. Next, I'll shift gears and check `kernel/bpf/verifier.c`. Functions in the verifier are always important in BPF. I see these functions: I see `check_indirect_jump` which is *static*, and is called in `do_check_common` / `do_check`. I also see `add_subprog` which is *static*, called in `bpf_check_cfg` or `check_subprogs`. Finally there's `bpf_check`, the main entrypoint to the verifier. Then, I'll move on to `kernel/bpf/bpf_insn_array.c`. I see: `bpf_insn_array_init`, `bpf_insn_array_release`, and `bpf_insn_array_adjust`. These array management functions are likely relevant. Lastly, I need to check the Kconfig options, and where `bpf_insn_array.c` is compiled. I'll look at the `kernel/bpf/Makefile` to understand the build context. This will show me the relevant compile flags and ensure I understand any configuration dependencies. |
| 14/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "bpf_insn_array",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/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\n--\nkernel/bpf/bpf_insn_array.c-5-\nkernel/bpf/bpf_insn_array.c:6:struct bpf_insn_array {\nkernel/bpf/bpf_insn_array.c-7-\tstruct bpf_map map;\n--\nkernel/bpf/bpf_insn_array.c-9-\tlong *ips;\nkernel/bpf/bpf_insn_array.c:10:\tDECLARE_FLEX_ARRAY(struct bpf_insn_array_value, values);\nkernel/bpf/bpf_insn_array.c-11-};\n--\nkernel/bpf/bpf_insn_array.c-13-#define cast_insn_array(MAP_PTR) \\\nkernel/bpf/bpf_insn_array.c:14:\tcontainer_of((MAP_PTR), struct bpf_insn_array, map)\nkernel/bpf/bpf_insn_array.c-15-\n--\nkernel/bpf/bpf_insn_array.c=18=static inline u64 insn_array_alloc_size(u32 max_entries)\nkernel/bpf/bpf_insn_array.c-19-{\nkernel/bpf/bpf_insn_array.c:20:\tconst u64 base_size = sizeof(struct bpf_insn_array);\nkernel/bpf/bpf_insn_array.c:21:\tconst u64 entry_size = sizeof(struct bpf_insn_array_value);\nkernel/bpf/bpf_insn_array.c-22-\n--\nkernel/bpf/bpf_insn_array.c=26=static int insn_array_alloc_check(union bpf_attr *attr)\nkernel/bpf/bpf_insn_array.c-27-{\nkernel/bpf/bpf_insn_array.c:28:\tu32 value_size = sizeof(struct bpf_insn_array_value);\nkernel/bpf/bpf_insn_array.c-29-\n--\nkernel/bpf/bpf_insn_array.c=37=static void insn_array_free(struct bpf_map *map)\nkernel/bpf/bpf_insn_array.c-38-{\nkernel/bpf/bpf_insn_array.c:39:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-40-\n--\nkernel/bpf/bpf_insn_array.c=44=static struct bpf_map *insn_array_alloc(union bpf_attr *attr)\n--\nkernel/bpf/bpf_insn_array.c-46-\tu64 size = insn_array_alloc_size(attr-\u003emax_entries);\nkernel/bpf/bpf_insn_array.c:47:\tstruct bpf_insn_array *insn_array;\nkernel/bpf/bpf_insn_array.c-48-\n--\nkernel/bpf/bpf_insn_array.c=64=static void *insn_array_lookup_elem(struct bpf_map *map, void *key)\nkernel/bpf/bpf_insn_array.c-65-{\nkernel/bpf/bpf_insn_array.c:66:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-67-\tu32 index = *(u32 *)key;\n--\nkernel/bpf/bpf_insn_array.c=75=static long insn_array_update_elem(struct bpf_map *map, void *key, void *value, u64 map_flags)\nkernel/bpf/bpf_insn_array.c-76-{\nkernel/bpf/bpf_insn_array.c:77:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-78-\tu32 index = *(u32 *)key;\nkernel/bpf/bpf_insn_array.c:79:\tstruct bpf_insn_array_value val = {};\nkernel/bpf/bpf_insn_array.c-80-\n--\nkernel/bpf/bpf_insn_array.c=120=static int insn_array_map_direct_value_addr(const struct bpf_map *map, u64 *imm, u32 off)\nkernel/bpf/bpf_insn_array.c-121-{\nkernel/bpf/bpf_insn_array.c:122:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-123-\n--\nkernel/bpf/bpf_insn_array.c-133-\nkernel/bpf/bpf_insn_array.c:134:BTF_ID_LIST_SINGLE(insn_array_btf_ids, struct, bpf_insn_array)\nkernel/bpf/bpf_insn_array.c-135-\n--\nkernel/bpf/bpf_insn_array.c=157=static bool is_insn_array(const struct bpf_map *map)\n--\nkernel/bpf/bpf_insn_array.c-161-\nkernel/bpf/bpf_insn_array.c:162:static inline bool valid_offsets(const struct bpf_insn_array *insn_array,\nkernel/bpf/bpf_insn_array.c-163-\t\t\t\t const struct bpf_prog *prog)\n--\nkernel/bpf/bpf_insn_array.c-182-\nkernel/bpf/bpf_insn_array.c:183:int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog)\nkernel/bpf/bpf_insn_array.c-184-{\nkernel/bpf/bpf_insn_array.c:185:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c:186:\tstruct bpf_insn_array_value *values = insn_array-\u003evalues;\nkernel/bpf/bpf_insn_array.c-187-\tint i;\n--\nkernel/bpf/bpf_insn_array.c-215-\nkernel/bpf/bpf_insn_array.c:216:int bpf_insn_array_ready(struct bpf_map *map)\nkernel/bpf/bpf_insn_array.c-217-{\nkernel/bpf/bpf_insn_array.c:218:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-219-\tint i;\n--\nkernel/bpf/bpf_insn_array.c-230-\nkernel/bpf/bpf_insn_array.c:231:void bpf_insn_array_release(struct bpf_map *map)\nkernel/bpf/bpf_insn_array.c-232-{\nkernel/bpf/bpf_insn_array.c:233:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-234-\nkernel/bpf/bpf_insn_array.c:235:\t/* Paired with atomic_xchg() in bpf_insn_array_init(). */\nkernel/bpf/bpf_insn_array.c-236-\tatomic_set_release(\u0026insn_array-\u003eused, 0);\n--\nkernel/bpf/bpf_insn_array.c-238-\nkernel/bpf/bpf_insn_array.c:239:void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len)\nkernel/bpf/bpf_insn_array.c-240-{\nkernel/bpf/bpf_insn_array.c:241:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-242-\tint i;\n--\nkernel/bpf/bpf_insn_array.c-255-\nkernel/bpf/bpf_insn_array.c:256:void bpf_insn_array_adjust_after_remove(struct bpf_map *map, u32 off, u32 len)\nkernel/bpf/bpf_insn_array.c-257-{\nkernel/bpf/bpf_insn_array.c:258:\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\nkernel/bpf/bpf_insn_array.c-259-\tint i;\n--\nkernel/bpf/bpf_insn_array.c=279=void bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image)\nkernel/bpf/bpf_insn_array.c-280-{\nkernel/bpf/bpf_insn_array.c:281:\tstruct bpf_insn_array *insn_array;\nkernel/bpf/bpf_insn_array.c-282-\tstruct bpf_map *map;\n--\nkernel/bpf/cfg.c=204=static int copy_insn_array(struct bpf_map *map, u32 start, u32 end, u32 *items)\nkernel/bpf/cfg.c-205-{\nkernel/bpf/cfg.c:206:\tstruct bpf_insn_array_value *value;\nkernel/bpf/cfg.c-207-\tu32 i;\n--\nkernel/bpf/fixups.c=226=static void adjust_insn_aux_data(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-289-\t * actually the first instruction, so move it back. This also matches with the behavior\nkernel/bpf/fixups.c:290:\t * of bpf_insn_array_adjust(), which preserves xlated_off to point to the first new\nkernel/bpf/fixups.c-291-\t * instruction. For BPF_PATCH_MOVE_TARGET the original instruction is the last one,\n--\nkernel/bpf/fixups.c=314=static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 first, u32 len)\n--\nkernel/bpf/fixups.c-321-\tfor (i = 0; i \u003c env-\u003einsn_array_map_cnt; i++)\nkernel/bpf/fixups.c:322:\t\tbpf_insn_array_adjust(env-\u003einsn_array_maps[i], first, len);\nkernel/bpf/fixups.c-323-}\n--\nkernel/bpf/fixups.c=325=static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 off, u32 len)\n--\nkernel/bpf/fixups.c-329-\tfor (i = 0; i \u003c env-\u003einsn_array_map_cnt; i++)\nkernel/bpf/fixups.c:330:\t\tbpf_insn_array_adjust_after_remove(env-\u003einsn_array_maps[i], off, len);\nkernel/bpf/fixups.c-331-}\n--\nkernel/bpf/syscall.c=2904=static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog)\n--\nkernel/bpf/syscall.c-2912-\nkernel/bpf/syscall.c:2913:\t\terr = bpf_insn_array_ready(prog-\u003eaux-\u003eused_maps[i]);\nkernel/bpf/syscall.c-2914-\t\tif (err)\n--\nkernel/bpf/verifier.c=4847=static int check_map_kptr_access(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-4919- * Return the size of the memory region accessible from a pointer to map value.\nkernel/bpf/verifier.c:4920: * For INSN_ARRAY maps whole bpf_insn_array-\u003eips array is accessible.\nkernel/bpf/verifier.c-4921- */\n--\nkernel/bpf/verifier.c=20021=static int __add_used_map(struct bpf_verifier_env *env, struct bpf_map *map)\n--\nkernel/bpf/verifier.c-20062-\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY) {\nkernel/bpf/verifier.c:20063:\t\terr = bpf_insn_array_init(map, env-\u003eprog);\nkernel/bpf/verifier.c-20064-\t\tif (err) {\n--\nkernel/bpf/verifier.c=20605=static void release_insn_arrays(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-20609-\tfor (i = 0; i \u003c env-\u003einsn_array_map_cnt; i++)\nkernel/bpf/verifier.c:20610:\t\tbpf_insn_array_release(env-\u003einsn_array_maps[i]);\nkernel/bpf/verifier.c-20611-}\n"
} |
| 15/2 | 2026/09/27 14:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39582 output: 35 thoughts: 945 Thoughts: **Dissecting This BPF Patch: A Deep Dive**
Okay, let's break this down. It seems we're dealing with a patch focused on optimizing and refining the BPF (Berkeley Packet Filter) infrastructure. My initial impression is that this touches a significant chunk of the core verifier and related components.
1. **`bpf_insn_array.c`:** This focuses on the instruction array management. I see the initialization and release functions are being streamlined. The `atomic_set_release` call is interesting; this likely ties into a multi-threaded access pattern. The change in `bpf_insn_array_adjust`, shifting the condition from `<= off` to `< first`, hints at some subtle edge case or indexing boundary being corrected.
2. **`cfg.c` (Control Flow Graph):** This is where things get more involved. The key takeaway is the shift towards per-subprogram jump tables (`struct bpf_subprog_info::jt`) rather than per-instruction data. This should provide performance improvements as the common structure will require less overall memory and likely be faster to access during lookups. The addition of the `BPF_MAX_GOTOX_EDGES` check in `visit_gotox_insn` suggests a need to guard against excessive complexity in the indirect jump scenarios. The removal of `visit_abnormal_return_insn` and handling tail calls/loads within `bpf_insn_successors()` looks like an attempt to unify logic. The fix for recursion emulation in `bpf_compute_scc` using the `dfs_pos` array is a common optimization to prevent infinite loops in graph traversals. The addition of `bpf_free_subprog_jts()` indicates that we're dealing with a cleaner resource management scheme.
3. **`fixups.c`:** This is the heart of the patch, and the changes here look significant. The introduction of `enum bpf_patch_mode` with `BPF_PATCH_KEEP_TARGET` and `BPF_PATCH_MOVE_TARGET` signifies a more sophisticated patching mechanism. The modifications to `adjust_insn_aux_data()`, `adjust_insn_arrays()`, and `adjust_poke_descs()` indicate a concerted effort to adapt various data structures to this new patching mode. The handling of jump table freeing in `adjust_subprog_starts_after_remove` is expected and highlights the importance of keeping resources managed. Avoiding nop removal on `indirect_target` instructions with `bpf_opt_remove_nops` seems important for code integrity. Setting `info[cnt].exit_idx = prog->len - 1` in `add_hidden_subprog` will likely simplify the exit code handling. This is also key as we're now consistently using `BPF_PATCH_MOVE_TARGET` in `bpf_convert_ctx_accesses` and `bpf_do_misc_fixups`, so we can expect some code movement!
4. **`liveness.c`:** The update to `bpf_insn_successors()` is critical. Correctly tracking the successors of `gotox`, `tail_call`, and `ld_{abs,ind}` instructions is the basis for a sound verifier. The jump table lookup for gotox is now more streamlined and the adding of edge to subprog exit\_idx provides better coverage.
5. **`verifier.c`:** The verifier changes are expected. Setting `exit_idx = U32_MAX` in `add_subprog` and checking if an indirect jump leaves subprogram are standard. But the replacement of `bpf_clear_insn_aux_data()` with `bpf_free_subprog_jts(env)` in `bpf_check` is important for freeing up jump table resources.
Regarding configurations, `BPF_SYSCALL` is definitely the key here since the linked Makefile confirms it is the driver behind verifier and maps. The focus on `BPF_MAP_TYPE_INSN_ARRAY` hints that this patch is part of a larger plan with this BPF map.
I'll need to dig deeper into the actual code changes, and understand the implications of the new patch modes, but this initial analysis gives me a good foothold. I'm especially interested in how the new patching modes and jump table changes influence the performance of complex BPF programs.
|
| 16/2 | 2026/09/27 14:17 | tool | grepper |
0mArgs:{
"Expression": "BPF_MAP_TYPE_INSN_ARRAY",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/bpf_insn_array.c=157=static bool is_insn_array(const struct bpf_map *map)\nkernel/bpf/bpf_insn_array.c-158-{\nkernel/bpf/bpf_insn_array.c:159:\treturn map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY;\nkernel/bpf/bpf_insn_array.c-160-}\n--\nkernel/bpf/syscall.c=1394=static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_verifier_log *log,\n--\nkernel/bpf/syscall.c-1528-\tcase BPF_MAP_TYPE_ARENA:\nkernel/bpf/syscall.c:1529:\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/syscall.c-1530-\t\tif (!bpf_token_capable(token, CAP_BPF))\n--\nkernel/bpf/syscall.c=2904=static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog)\n--\nkernel/bpf/syscall.c-2909-\tfor (i = 0; i \u003c prog-\u003eaux-\u003eused_map_cnt; i++) {\nkernel/bpf/syscall.c:2910:\t\tif (prog-\u003eaux-\u003eused_maps[i]-\u003emap_type != BPF_MAP_TYPE_INSN_ARRAY)\nkernel/bpf/syscall.c-2911-\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=4922=static u32 map_mem_size(const struct bpf_map *map)\nkernel/bpf/verifier.c-4923-{\nkernel/bpf/verifier.c:4924:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY)\nkernel/bpf/verifier.c-4925-\t\treturn map-\u003emax_entries * sizeof(long);\n--\nkernel/bpf/verifier.c=5374=static int check_ptr_alignment(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-5395-\t\tpointer_desc = \"value \";\nkernel/bpf/verifier.c:5396:\t\tif (reg-\u003emap_ptr-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY)\nkernel/bpf/verifier.c-5397-\t\t\tstrict = true;\n--\nkernel/bpf/verifier.c=6034=int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val,\n--\nkernel/bpf/verifier.c-6040-\nkernel/bpf/verifier.c:6041:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY || map-\u003emap_type == BPF_MAP_TYPE_PERCPU_ARRAY)\nkernel/bpf/verifier.c-6042-\t\treturn -EINVAL;\n--\nkernel/bpf/verifier.c=6824=static int check_map_mem_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int off,\n--\nkernel/bpf/verifier.c-6832-\tswitch (map-\u003emap_type) {\nkernel/bpf/verifier.c:6833:\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/verifier.c-6834-\t\tif (bpf_size != BPF_DW) {\n--\nkernel/bpf/verifier.c=9173=static int check_arg_const_str(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-9185-\nkernel/bpf/verifier.c:9186:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY) {\nkernel/bpf/verifier.c-9187-\t\tverbose(env, \"%s points to insn_array map which cannot be used as const string\\n\",\n--\nkernel/bpf/verifier.c=10095=static int check_map_func_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-10222-\t\tbreak;\nkernel/bpf/verifier.c:10223:\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/verifier.c-10224-\t\tgoto error;\n--\nkernel/bpf/verifier.c=18222=static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-18315-\t\tdst_reg-\u003emap_ptr = map;\nkernel/bpf/verifier.c:18316:\t\tWARN_ON_ONCE(map-\u003emap_type != BPF_MAP_TYPE_INSN_ARRAY \u0026\u0026\nkernel/bpf/verifier.c-18317-\t\t\t map-\u003emax_entries != 1);\n--\nkernel/bpf/verifier.c=19270=static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-19292-\nkernel/bpf/verifier.c:19293:\tif (verifier_bug_if(map-\u003emap_type != BPF_MAP_TYPE_INSN_ARRAY, env,\nkernel/bpf/verifier.c-19294-\t\t\t \"R%d has incorrect map type %d\", insn-\u003edst_reg, map-\u003emap_type))\n--\nkernel/bpf/verifier.c=19887=static int check_map_prog_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-19953-\t\tcase BPF_MAP_TYPE_ARENA:\nkernel/bpf/verifier.c:19954:\t\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/verifier.c-19955-\t\tcase BPF_MAP_TYPE_PROG_ARRAY:\n--\nkernel/bpf/verifier.c=20021=static int __add_used_map(struct bpf_verifier_env *env, struct bpf_map *map)\n--\nkernel/bpf/verifier.c-20061-\nkernel/bpf/verifier.c:20062:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY) {\nkernel/bpf/verifier.c-20063-\t\terr = bpf_insn_array_init(map, env-\u003eprog);\n"
} |
| 17/2 | 2026/09/27 14:17 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43236 output: 33 thoughts: 100 Thoughts: **My BPF JIT Analysis** Okay, so I'm diving into this BPF JIT thing and my initial thought is that `BPF_MAP_TYPE_INSN_ARRAY` is the key for indirect jumps, or those "gotox" situations with jump tables. That feels right. Now, let's zoom out a bit and consider the bigger picture. I need to make sure I understand the interaction between `BPF_JIT` and other components. First, I need to verify that `BPF_SYSCALL` is essential, and the BPF verifier makes use of that, and from what I can see in `bpf_verifier.h`, `cfg.c`, and `fixups.c`, it definitely is. But is `BPF_JIT` itself dependent on anything? Or does it operate somewhat independently? To figure that out, I'm checking the `kernel/bpf/Kconfig` file. That's the best place to find dependencies and configuration options. Understanding how this module works is important. |
| 18/2 | 2026/09/27 14:18 | tool | grepper |
0mArgs:{
"Expression": "BPF_SYSCALL",
"PathPrefix": "kernel/bpf/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--\nkernel/bpf/Kconfig=62=config BPF_JIT_ALWAYS_ON\nkernel/bpf/Kconfig-63-\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\nkernel/bpf/Kconfig:64:\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\nkernel/bpf/Kconfig-65-\thelp\n--\nkernel/bpf/Kconfig=77=config BPF_UNPRIV_DEFAULT_OFF\n--\nkernel/bpf/Kconfig-79-\tdefault y\nkernel/bpf/Kconfig:80:\tdepends on BPF_SYSCALL\nkernel/bpf/Kconfig-81-\thelp\n--\nkernel/bpf/Kconfig=94=config BPF_CRYPTO\nkernel/bpf/Kconfig-95-\tdef_bool y\nkernel/bpf/Kconfig:96:\tdepends on BPF_SYSCALL\nkernel/bpf/Kconfig-97-\tselect CRYPTO_LIB_AES_CBC\n--\nkernel/bpf/Kconfig=105=config BPF_LSM\n--\nkernel/bpf/Kconfig-107-\tdepends on BPF_EVENTS\nkernel/bpf/Kconfig:108:\tdepends on BPF_SYSCALL\nkernel/bpf/Kconfig-109-\tdepends on SECURITY\n"
} |
| 19/2 | 2026/09/27 14:18 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44018 output: 37 thoughts: 802 Thoughts: **Focus Symbol Selection for BPF Patch Series** Okay, so I'm diving into this BPF patch series and need to figure out the right `FocusSymbols` to enable all the verifier and map logic since we're using `BPF_SYSCALL` which is standard for this. Time to choose wisely. I have these guidelines: avoid the super common, ubiquitous stuff and focus on what's specific to the new logic or the entrypoints of the feature. Static inline functions are a non-starter since they won't have a distinct symbol. Instead, I need to find the non-static callers that are specific to the `.c` files. Alright, let's see what we've got. Looking at the functions touched by this patch series, here's my assessment: 1. `bpf_insn_array_init`: This one's a non-static function in `bpf_insn_array.c`. It initializes `BPF_MAP_TYPE_INSN_ARRAY` maps. This *could* be a candidate. 2. `bpf_insn_array_adjust`: Another non-static from `bpf_insn_array.c`, adjusts jump target offsets. Still in the running. 3. `bpf_insn_array_release`: Yep, non-static from `bpf_insn_array.c` as well. 4. `bpf_free_subprog_jts`: Non-static function in `cfg.c`. This cleans up jump tables, which is likely involved in the CFG work. Definitely worth keeping an eye on. 5. `bpf_compute_scc`: Also in `cfg.c`, non-static and deals with strongly connected components in the CFG analysis. This is interesting as it's been modified to fix the `dfs_pos` tracking. 6. `bpf_check_cfg`: Another non-static from `cfg.c`, runs the CFG check, calls several functions. This seems *very* important. 7. `bpf_patch_insn_data`: Non-static from `fixups.c`. Patches instructions and calls several related functions like `adjust_insn_aux_data`. This is important for our focus. 8. `bpf_opt_remove_nops`: Non-static from `fixups.c`. This one has been modified to skip indirect targets, so we want to keep an eye on this as well. 9. `bpf_convert_ctx_accesses`: Non-static `fixups.c`. Rewrites context accesses, so another key function to follow. 10. `bpf_do_misc_fixups`: Non-static `fixups.c`. Miscellaneous fixups are being applied using `BPF_PATCH_MOVE_TARGET`. 11. `bpf_insn_successors`: Okay, this one's declared in `bpf_verifier.h` but it is *called* from `liveness.c` during CFG, const folding, and liveness analysis. I need to use `read-file` to check if this is static inline or a regular function. I'll check line 319-325 in `kernel/bpf/liveness.c`. Based on my understanding, functions in `kernel/bpf/cfg.c`, `kernel/bpf/fixups.c` and potentially, `kernel/bpf/liveness.c` are high-priority candidates for focus symbols. Functions in `kernel/bpf/bpf_insn_array.c` are related to the map type so it depends on the feature context. |
| 20/2 | 2026/09/27 14:18 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/liveness.c",
"FirstLine": 319,
"LineCount": 20
}Results: {
"Contents": " 319:\tinline struct bpf_iarray *\n 320:\tbpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n 321:\t{\n 322:\t\tstatic const struct opcode_info {\n 323:\t\t\tbool can_jump;\n 324:\t\t\tbool can_fallthrough;\n 325:\t\t} opcode_info_tbl[256] = {\n 326:\t\t\t[0 ... 255] = {.can_jump = false, .can_fallthrough = true},\n 327:\t\t#define _J(code, ...) \\\n 328:\t\t\t[BPF_JMP | code] = __VA_ARGS__, \\\n 329:\t\t\t[BPF_JMP32 | code] = __VA_ARGS__\n 330:\t\n 331:\t\t\t_J(BPF_EXIT, {.can_jump = false, .can_fallthrough = false}),\n 332:\t\t\t_J(BPF_JA, {.can_jump = true, .can_fallthrough = false}),\n 333:\t\t\t_J(BPF_JEQ, {.can_jump = true, .can_fallthrough = true}),\n 334:\t\t\t_J(BPF_JNE, {.can_jump = true, .can_fallthrough = true}),\n 335:\t\t\t_J(BPF_JLT, {.can_jump = true, .can_fallthrough = true}),\n 336:\t\t\t_J(BPF_JLE, {.can_jump = true, .can_fallthrough = true}),\n 337:\t\t\t_J(BPF_JGT, {.can_jump = true, .can_fallthrough = true}),\n 338:\t\t\t_J(BPF_JGE, {.can_jump = true, .can_fallthrough = true}),\n"
} |
| 21/2 | 2026/09/27 14:18 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46153 output: 29 thoughts: 193 Thoughts: **Analyzing BPF Function Declaration and Usage** Okay, so I've spotted something interesting with this `bpf_insn_successors` function. I see it's declared `inline` in a `.c` file, and crucially, it *isn't* declared `static`. In the C99 standard, and especially in GNU C, an `inline` function without `static` can actually produce an external definition if it's declared as `extern` elsewhere. And looking at `include/linux/bpf_verifier.h`, there it is, declared externally. This means it's an exported symbol, which is exactly what I would expect given it is called from `kernel/bpf/cfg.c` and `kernel/bpf/const_fold.c`! Seems legit. Now, let me quickly pivot to `check_indirect_jump`. I want to verify if it is declared `static`, which would define its visibility. So I'll go check its declaration in `kernel/bpf/verifier.c`. Yes, it's declared `static`. Good, just confirming my assumptions. |
| 22/2 | 2026/09/27 14:18 | tool | grepper |
0mArgs:{
"Expression": "check_indirect_jump",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/verifier.c=19256=static int reject_gotox_out_of_subprog(struct bpf_verifier_env *env, u32 target,\n--\nkernel/bpf/verifier.c-19269-/* gotox *dst_reg */\nkernel/bpf/verifier.c:19270:static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)\nkernel/bpf/verifier.c-19271-{\n--\nkernel/bpf/verifier.c=19348=static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)\n--\nkernel/bpf/verifier.c-19428-\t\t\tif (BPF_SRC(insn-\u003ecode) == BPF_X)\nkernel/bpf/verifier.c:19429:\t\t\t\treturn check_indirect_jump(env, insn);\nkernel/bpf/verifier.c-19430-\n"
} |
| 23/2 | 2026/09/27 14:18 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46884 output: 31 thoughts: 326 Thoughts: **My Analysis of BPF Verifier Entry Points** Okay, so `check_indirect_jump` is a static function, which is interesting. It's called by `do_check_insn`, which is in turn called by `do_check`, and that's ultimately invoked by `bpf_check`. Now, `bpf_check` is the main entry point to this whole verification process, though of course, it's specific to BPF program verification. I have to make sure I'm following the correct execution path here. However, I've got a list of other non-static functions that seem to be directly involved, and these are the ones I really need to drill down on: `bpf_insn_array_init`, `bpf_insn_array_adjust`, `bpf_free_subprog_jts`, `bpf_check_cfg`, `bpf_compute_scc`, `bpf_patch_insn_data`, `bpf_opt_remove_nops`, `bpf_convert_ctx_accesses`, `bpf_do_misc_fixups`, and `bpf_insn_successors`. My first immediate question is whether any of these functions are involved in core lifecycle management. Are they general memory allocators? Are they dealing with page faults, or core mutex locks? The answer, as I read them, is no. These functions are all pretty laser-focused on the BPF verifier itself, including patching and fixing BPF instructions, working with instruction array maps, and performing control flow graph (CFG) analysis. So, they're not general purpose, which is a good sign. Next I'll check if there are any other configuration dependencies. Is `CONFIG_BPF_SYSCALL` enough to cover everything, or am I missing something? I should check `BPF_SYSCALL` in the `init/Kconfig` or `kernel/bpf/Kconfig` files just to be sure. That's the next step. |
| 24/2 | 2026/09/27 14:20 | tool | grepper |
0mArgs:{
"Expression": "config BPF_SYSCALL",
"PathPrefix": "kernel/bpf"
}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"
} |
| 25/2 | 2026/09/27 14:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47764 output: 31 thoughts: 17 Thoughts: Here's my thought process, as requested: **Assessing the Landscape of `BPF_SYSCALL` Dependencies** Okay, so I need to understand this `BPF_SYSCALL` thing better. My immediate question is: What other bits of code, other kernel modules, or any other infrastructure actually *rely* on this? Understanding its dependencies is crucial. It's the first thing I do with any complex piece of code. I need to know the ripple effect of any changes I make, any potential impact. Are we talking about core kernel features or something more isolated? I want to see how broadly or narrowly it's used. The dependencies define the scope of my investigation. It's the essential first step before I can even start to really *get* what's going on. I'm going to start digging into the code and associated documentation to build a dependency map. |
| 26/2 | 2026/09/27 14:20 | tool | grepper |
0mError:no matches Args: {
"Expression": "config BPF_SYSCALL",
"PathPrefix": "init/Kconfig"
}Results: {
"error": "no matches"
} |
| 27/2 | 2026/09/27 14:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48015 output: 34 thoughts: 19 Thoughts: Okay, let's dive into this. Here's my perspective on understanding that `Kconfig` file, geared for an expert like myself: **Diving into the BPF Configuration** Alright, I need to get a handle on how BPF is configured within this kernel. Let's zero in on `kernel/bpf/Kconfig`, specifically around line 31. I'm assuming that's where the core BPF options are defined. My experience tells me that's the likely location for the main `config` statements that control whether BPF features are compiled into the kernel. I'm hoping to quickly identify the crucial options affecting its core functionality. It's likely these options are grouped logically, maybe separated into categories like "core BPF," "BPF helpers," or "BPF tracing." I'm also watching for any dependencies on other kernel subsystems. I want to spot those interconnections early, as that's often where the real complexity (and potential conflicts) lie. Understanding those dependencies is key to effective configuration. Let's see what we've got! |
| 28/2 | 2026/09/27 14:20 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 30,
"LineCount": 25
}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"
} |
| 29/2 | 2026/09/27 14:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48639 output: 35 thoughts: 91 Thoughts: Okay, here's my summary, considering my expertise: **Examining BPF Configuration Dependencies** Alright, let's break this down. From a dependency perspective, `BPF_SYSCALL` seems pretty straightforward; it's a fundamental selector for BPF itself, meaning nothing *precedes* it. However, I want to clarify how BPF JIT compilation plays into some of the more advanced BPF features I'm interested in, to make sure I don't overlook any subtleties. I'm thinking specifically about whether enabling `BPF_JIT` is a hard requirement for functionalities like `BPF_MAP_TYPE_INSN_ARRAY` or the `gotox` instruction. These are the kinds of things that can be easily missed if you're not paying attention to every detail of the build configuration. My next step will be to dive into the codebase and search for these specific features and see what, if anything, links `BPF_JIT` as a dependency. I need to ensure a clear understanding of the implications of enabling or disabling the JIT for the use cases I'm contemplating. |
| 30/2 | 2026/09/27 14:20 | tool | grepper |
0mArgs:{
"Expression": "BPF_MAP_TYPE_INSN_ARRAY",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/bpf_insn_array.c=157=static bool is_insn_array(const struct bpf_map *map)\nkernel/bpf/bpf_insn_array.c-158-{\nkernel/bpf/bpf_insn_array.c:159:\treturn map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY;\nkernel/bpf/bpf_insn_array.c-160-}\n--\nkernel/bpf/syscall.c=1394=static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_verifier_log *log,\n--\nkernel/bpf/syscall.c-1528-\tcase BPF_MAP_TYPE_ARENA:\nkernel/bpf/syscall.c:1529:\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/syscall.c-1530-\t\tif (!bpf_token_capable(token, CAP_BPF))\n--\nkernel/bpf/syscall.c=2904=static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog)\n--\nkernel/bpf/syscall.c-2909-\tfor (i = 0; i \u003c prog-\u003eaux-\u003eused_map_cnt; i++) {\nkernel/bpf/syscall.c:2910:\t\tif (prog-\u003eaux-\u003eused_maps[i]-\u003emap_type != BPF_MAP_TYPE_INSN_ARRAY)\nkernel/bpf/syscall.c-2911-\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=4922=static u32 map_mem_size(const struct bpf_map *map)\nkernel/bpf/verifier.c-4923-{\nkernel/bpf/verifier.c:4924:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY)\nkernel/bpf/verifier.c-4925-\t\treturn map-\u003emax_entries * sizeof(long);\n--\nkernel/bpf/verifier.c=5374=static int check_ptr_alignment(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-5395-\t\tpointer_desc = \"value \";\nkernel/bpf/verifier.c:5396:\t\tif (reg-\u003emap_ptr-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY)\nkernel/bpf/verifier.c-5397-\t\t\tstrict = true;\n--\nkernel/bpf/verifier.c=6034=int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val,\n--\nkernel/bpf/verifier.c-6040-\nkernel/bpf/verifier.c:6041:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY || map-\u003emap_type == BPF_MAP_TYPE_PERCPU_ARRAY)\nkernel/bpf/verifier.c-6042-\t\treturn -EINVAL;\n--\nkernel/bpf/verifier.c=6824=static int check_map_mem_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int off,\n--\nkernel/bpf/verifier.c-6832-\tswitch (map-\u003emap_type) {\nkernel/bpf/verifier.c:6833:\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/verifier.c-6834-\t\tif (bpf_size != BPF_DW) {\n--\nkernel/bpf/verifier.c=9173=static int check_arg_const_str(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-9185-\nkernel/bpf/verifier.c:9186:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY) {\nkernel/bpf/verifier.c-9187-\t\tverbose(env, \"%s points to insn_array map which cannot be used as const string\\n\",\n--\nkernel/bpf/verifier.c=10095=static int check_map_func_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-10222-\t\tbreak;\nkernel/bpf/verifier.c:10223:\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/verifier.c-10224-\t\tgoto error;\n--\nkernel/bpf/verifier.c=18222=static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-18315-\t\tdst_reg-\u003emap_ptr = map;\nkernel/bpf/verifier.c:18316:\t\tWARN_ON_ONCE(map-\u003emap_type != BPF_MAP_TYPE_INSN_ARRAY \u0026\u0026\nkernel/bpf/verifier.c-18317-\t\t\t map-\u003emax_entries != 1);\n--\nkernel/bpf/verifier.c=19270=static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-19292-\nkernel/bpf/verifier.c:19293:\tif (verifier_bug_if(map-\u003emap_type != BPF_MAP_TYPE_INSN_ARRAY, env,\nkernel/bpf/verifier.c-19294-\t\t\t \"R%d has incorrect map type %d\", insn-\u003edst_reg, map-\u003emap_type))\n--\nkernel/bpf/verifier.c=19887=static int check_map_prog_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-19953-\t\tcase BPF_MAP_TYPE_ARENA:\nkernel/bpf/verifier.c:19954:\t\tcase BPF_MAP_TYPE_INSN_ARRAY:\nkernel/bpf/verifier.c-19955-\t\tcase BPF_MAP_TYPE_PROG_ARRAY:\n--\nkernel/bpf/verifier.c=20021=static int __add_used_map(struct bpf_verifier_env *env, struct bpf_map *map)\n--\nkernel/bpf/verifier.c-20061-\nkernel/bpf/verifier.c:20062:\tif (map-\u003emap_type == BPF_MAP_TYPE_INSN_ARRAY) {\nkernel/bpf/verifier.c-20063-\t\terr = bpf_insn_array_init(map, env-\u003eprog);\n"
} |
| 31/2 | 2026/09/27 14:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50722 output: 267 thoughts: 777 Thoughts: **BPF Verifier Modification - Ready for Fuzzing** Alright, let's get this finalized. First, a quick check: even though the verifier (`bpf_check`) runs regardless of JIT enablement, enabling `BPF_JIT` is pretty much standard for our syzkaller BPF fuzzing efforts, so we'll ensure that's covered. Now, the key configs here: we need to ensure both `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT` are set. Next up, the crucial `FocusSymbols`. These are the functions directly affected by the patch and represent our target areas for fuzzing: `bpf_check_cfg`, `bpf_compute_scc`, `bpf_insn_array_init`, `bpf_insn_array_adjust`, `bpf_free_subprog_jts`, `bpf_patch_insn_data`, `bpf_opt_remove_nops`, `bpf_convert_ctx_accesses`, `bpf_do_misc_fixups`, and `bpf_insn_successors`. Now for the `set-results` parameters. `WorthFuzzing`? Absolutely, this patch fundamentally alters critical BPF verifier logic. I'll summarize the reasoning concisely: the patch impacts Control Flow Graph (CFG) analysis, how indirect jumps (gotox) are handled, jump tables shared per subprogram, the tracking of Strongly Connected Component (SCC) DFS traversal, how we patch instructions (specifically the `BPF_PATCH_KEEP_TARGET` vs. `BPF_PATCH_MOVE_TARGET` modes), and the lifecycle management of instruction array maps. The entry point is clearly the `bpf()` syscall. Finally, just a sanity check to make *absolutely* sure these symbols are in the kernel and aren't static. `bpf_check_cfg`, `bpf_compute_scc` and `bpf_free_subprog_jts` are non-static in `kernel/bpf/cfg.c`, `bpf_insn_array_init` and `bpf_insn_array_adjust` are in `kernel/bpf/bpf_insn_array.c`, `bpf_patch_insn_data`, `bpf_opt_remove_nops`, `bpf_convert_ctx_accesses`, `bpf_do_misc_fixups` are in `kernel/bpf/fixups.c`, and finally `bpf_insn_successors` is defined in `kernel/bpf/liveness.c` and declared in `include/linux/bpf_verifier.h`. Everything is kosher. With all of that verified, it's time to call `set-results`. We're good to go. |
| 32/2 | 2026/09/27 14:20 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_cfg",
"bpf_compute_scc",
"bpf_insn_array_init",
"bpf_insn_array_adjust",
"bpf_free_subprog_jts",
"bpf_patch_insn_data",
"bpf_opt_remove_nops",
"bpf_convert_ctx_accesses",
"bpf_do_misc_fixups",
"bpf_insn_successors"
],
"Reasoning": "The patch modifies core BPF verifier logic and data structures reachable via the bpf() system call. Key modifications include restructuring CFG indirect jump (gotox) table tracking so tables are shared per-subprogram, enforcing edge limits, adjusting DFS traversal in strongly connected component computation (bpf_compute_scc), adding instruction patching modes to preserve or shift jump targets during fixup passes, and resetting BPF instruction array map state. Fuzzing is warranted to uncover regressions or invariant violations in verification and instruction patching.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_cfg",
"bpf_compute_scc",
"bpf_insn_array_init",
"bpf_insn_array_adjust",
"bpf_free_subprog_jts",
"bpf_patch_insn_data",
"bpf_opt_remove_nops",
"bpf_convert_ctx_accesses",
"bpf_do_misc_fixups",
"bpf_insn_successors"
],
"Reasoning": "The patch modifies core BPF verifier logic and data structures reachable via the bpf() system call. Key modifications include restructuring CFG indirect jump (gotox) table tracking so tables are shared per-subprogram, enforcing edge limits, adjusting DFS traversal in strongly connected component computation (bpf_compute_scc), adding instruction patching modes to preserve or shift jump targets during fixup passes, and resetting BPF instruction array map state. Fuzzing is warranted to uncover regressions or invariant violations in verification and instruction patching.",
"WorthFuzzing": true
} |
| 33/1 | 2026/09/27 14:20 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 34/2 | 2026/09/27 14:20 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series refactors BPF verifier indirect jump (gotox) table handling, CFG edge bounds checking, SCC iterative DFS traversal, and instruction patching logic.\n\n1. Memory Initialization:\n- All new fields in `struct bpf_verifier_env` (`cfg.gotox_edges`, `cfg.subprog_jts_ready`) and `subprog_info` (`jt`) are zero-initialized at allocation time via `kvzalloc_obj()`.\n- `exit_idx` is explicitly initialized to `U32_MAX` in `add_subprog()` and updated in `check_subprogs()` / `add_hidden_subprog()`.\n- The new `dfs_pos` array in `bpf_compute_scc()` is allocated with `kvcalloc()`, ensuring zero-initialization.\n- In `bpf_insn_array_init()`, stale map entries from previous verification runs are explicitly reset (`jitted_off = 0` and `ips[i] = 0`).\n- Vacated slots in `subprog_info` during subprogram removal are explicitly zeroed with `memset()`.\n\n2. User-space Exposure and Info-leaks:\n- The changes operate purely on internal BPF verifier structures and CFG traversal bookkeeping. No uninitialized data or kernel structure padding is copied to user space.\n\n3. KASAN vs KMSAN Applicability:\n- Any risks introduced by these changes pertain to jump table lifetime management (use-after-free, double free in `bpf_free_subprog_jts()` or `adjust_subprog_starts_after_remove()`), out-of-bounds array accesses in CFG/SCC graph traversal, or memory leaks. These are memory safety bugs detected by KASAN, KMEMLEAK, and standard bug detectors.\n- There are no risks of reading uninitialized stack, heap, or page allocations that would warrant a dedicated KMSAN fuzzing session.",
"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 59d38d719edf105f6b1efc007088e85fcfe8fb0a
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Sep 27 14:17:06 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 8594c8aff745d..2cf04bba16bb7 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -4289,7 +4289,7 @@ struct bpf_prog *bpf_prog_find_from_stack(void);
int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog);
int bpf_insn_array_ready(struct bpf_map *map);
void bpf_insn_array_release(struct bpf_map *map);
-void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len);
+void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len);
void bpf_insn_array_adjust_after_remove(struct bpf_map *map, u32 off, u32 len);
#ifdef CONFIG_BPF_SYSCALL
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 646e447cebd84..c775bd7577063 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -660,7 +660,6 @@ struct bpf_insn_aux_data {
/* remember the offset of node field within type to rewrite */
u64 insert_off;
};
- struct bpf_iarray *jt; /* jump table for gotox or bpf_tailcall call instruction */
struct btf_struct_meta *kptr_struct_meta;
u64 map_key_state; /* constant (32 bit) key tracking for maps */
int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
@@ -815,7 +814,12 @@ struct bpf_subprog_info {
u32 start; /* insn idx of function entry point */
u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
u32 postorder_start; /* The idx to the env->cfg.insn_postorder */
- u32 exit_idx; /* Index of one of the BPF_EXIT instructions in this subprogram */
+ /*
+ * Index of one of the BPF_EXIT instructions in this subprogram, or
+ * U32_MAX when it has none.
+ */
+ u32 exit_idx;
+ struct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */
u16 stack_depth; /* max. stack depth used by this function */
u16 stack_extra;
u32 insns_total;
@@ -1004,6 +1008,8 @@ struct bpf_verifier_env {
int cur_stack;
/* current position in the insn_postorder vector */
int cur_postorder;
+ u32 gotox_edges;
+ bool subprog_jts_ready;
} cfg;
struct backtrack_state bt;
struct bpf_jmp_history_entry *cur_hist_ent;
@@ -1582,6 +1588,7 @@ bool bpf_is_throw_kfunc(struct bpf_insn *insn);
int bpf_compute_const_regs(struct bpf_verifier_env *env);
int bpf_prune_dead_branches(struct bpf_verifier_env *env);
int bpf_check_cfg(struct bpf_verifier_env *env);
+void bpf_free_subprog_jts(struct bpf_verifier_env *env);
int bpf_compute_postorder(struct bpf_verifier_env *env);
int bpf_compute_scc(struct bpf_verifier_env *env);
@@ -1800,7 +1807,6 @@ struct bpf_kfunc_desc_tab {
};
/* Functions exported from verifier.c, used by fixups.c */
-void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len);
void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog);
bool bpf_allow_tail_call_in_subprogs(struct bpf_verifier_env *env);
bool bpf_verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm);
diff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c
index a2f84afe6f7c6..92bbb71576ae3 100644
--- a/kernel/bpf/bpf_insn_array.c
+++ b/kernel/bpf/bpf_insn_array.c
@@ -199,12 +199,16 @@ int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog)
return -EBUSY;
/*
- * Reset all the map indexes to the original values. This is needed,
- * e.g., when a replay of verification with different log level should
- * be performed.
+ * Reset the map to its pre-verification state. The xlated and jitted
+ * offsets and the jitted target pointers are recomputed by the verifier
+ * and the JIT for this program, so any values left by a previous owner
+ * must be cleared here.
*/
- for (i = 0; i < map->max_entries; i++)
+ for (i = 0; i < map->max_entries; i++) {
values[i].xlated_off = values[i].orig_off;
+ values[i].jitted_off = 0;
+ insn_array->ips[i] = 0;
+ }
return 0;
}
@@ -228,10 +232,11 @@ void bpf_insn_array_release(struct bpf_map *map)
{
struct bpf_insn_array *insn_array = cast_insn_array(map);
- atomic_set(&insn_array->used, 0);
+ /* Paired with atomic_xchg() in bpf_insn_array_init(). */
+ atomic_set_release(&insn_array->used, 0);
}
-void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len)
+void bpf_insn_array_adjust(struct bpf_map *map, u32 first, u32 len)
{
struct bpf_insn_array *insn_array = cast_insn_array(map);
int i;
@@ -240,7 +245,7 @@ void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len)
return;
for (i = 0; i < map->max_entries; i++) {
- if (insn_array->values[i].xlated_off <= off)
+ if (insn_array->values[i].xlated_off < first)
continue;
if (insn_array->values[i].xlated_off == INSN_DELETED)
continue;
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 0de2f634ef679..b0bd9ba951dfb 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -9,6 +9,8 @@
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
+#define BPF_MAX_GOTOX_EDGES BPF_COMPLEXITY_LIMIT_INSNS
+
/* non-recursive DFS pseudo code
* 1 procedure DFS-iterative(G,v):
* 2 label v as discovered
@@ -284,15 +286,17 @@ static struct bpf_iarray *jt_from_map(struct bpf_map *map)
}
/*
- * Find and collect all maps which fit in the subprog. Return the result as one
- * combined jump table in jt->items (allocated with kvcalloc)
+ * Collect the jump table of every subprogram that has one, as the combined
+ * table of all maps whose targets land inside that subprogram. All gotox
+ * instructions of a subprogram share the same table, so this is done in a
+ * single pass over the maps rather than once per gotox.
*/
-static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,
- int subprog_start, int subprog_end)
+static int __compute_subprog_jts(struct bpf_verifier_env *env)
{
- struct bpf_iarray *jt = NULL;
+ struct bpf_subprog_info *subprog;
+ struct bpf_iarray *jt, *jt_cur;
struct bpf_map *map;
- struct bpf_iarray *jt_cur;
+ u32 old_cnt;
int i;
for (i = 0; i < env->insn_array_map_cnt; i++) {
@@ -303,73 +307,100 @@ static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,
map = env->insn_array_maps[i];
jt_cur = jt_from_map(map);
- if (IS_ERR(jt_cur)) {
- kvfree(jt);
- return jt_cur;
+ if (IS_ERR(jt_cur))
+ return PTR_ERR(jt_cur);
+
+ subprog = bpf_find_containing_subprog(env, jt_cur->items[0]);
+ if (verifier_bug_if(!subprog, env, "no subprog contains insn %u of an insn array map",
+ jt_cur->items[0])) {
+ kvfree(jt_cur);
+ return -EFAULT;
+ }
+ if (jt_cur->items[jt_cur->cnt - 1] >= (subprog + 1)->start) {
+ verbose(env, "jump table of subprog starting at %u spans multiple subprogs, map id=%d\n",
+ subprog->start, map->id);
+ bpf_diag_program_structure(
+ env, subprog->start, "jump table spans subprograms",
+ "Keep every entry of a jump table inside one subprogram.",
+ "The jump table in map id %d for the subprogram that starts at instruction %u reaches past its end at instruction %u.",
+ map->id, subprog->start, (subprog + 1)->start);
+ kvfree(jt_cur);
+ return -EINVAL;
}
- /*
- * This is enough to check one element. The full table is
- * checked to fit inside the subprog later in create_jt()
- */
- if (jt_cur->items[0] >= subprog_start && jt_cur->items[0] < subprog_end) {
- u32 old_cnt = jt ? jt->cnt : 0;
- jt = bpf_iarray_realloc(jt, old_cnt + jt_cur->cnt);
- if (!jt) {
- kvfree(jt_cur);
- return ERR_PTR(-ENOMEM);
- }
- memcpy(jt->items + old_cnt, jt_cur->items, jt_cur->cnt << 2);
+ old_cnt = subprog->jt ? subprog->jt->cnt : 0;
+ jt = bpf_iarray_realloc(subprog->jt, old_cnt + jt_cur->cnt);
+ if (!jt) {
+ subprog->jt = NULL;
+ kvfree(jt_cur);
+ return -ENOMEM;
}
+ memcpy(jt->items + old_cnt, jt_cur->items, jt_cur->cnt << 2);
+ subprog->jt = jt;
kvfree(jt_cur);
}
- if (!jt) {
- verbose(env, "no jump tables found for subprog starting at %u\n", subprog_start);
- bpf_diag_program_structure(
- env, subprog_start, "missing jump table",
- "Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms.",
- "No jump table was found for the subprogram that starts at instruction %u.",
- subprog_start);
- return ERR_PTR(-EINVAL);
+ for (i = 0; i < env->subprog_cnt; i++) {
+ jt = env->subprog_info[i].jt;
+ if (jt)
+ jt->cnt = sort_insn_array_uniq(jt->items, jt->cnt);
}
- jt->cnt = sort_insn_array_uniq(jt->items, jt->cnt);
- return jt;
+ env->cfg.subprog_jts_ready = true;
+ return 0;
+}
+
+/* Leave nothing behind on failure. */
+static int compute_subprog_jts(struct bpf_verifier_env *env)
+{
+ int err;
+
+ err = __compute_subprog_jts(env);
+ if (err)
+ bpf_free_subprog_jts(env);
+ return err;
+}
+
+void bpf_free_subprog_jts(struct bpf_verifier_env *env)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(env->subprog_info); i++) {
+ kvfree(env->subprog_info[i].jt);
+ env->subprog_info[i].jt = NULL;
+ }
+ env->cfg.subprog_jts_ready = false;
}
static struct bpf_iarray *
-create_jt(int t, struct bpf_verifier_env *env)
+subprog_jt(int t, struct bpf_verifier_env *env)
{
struct bpf_subprog_info *subprog;
- int subprog_start, subprog_end;
- struct bpf_iarray *jt;
- int i;
+ int subprog_start, err;
+
+ if (!env->cfg.subprog_jts_ready) {
+ err = compute_subprog_jts(env);
+ if (err)
+ return ERR_PTR(err);
+ }
subprog = bpf_find_containing_subprog(env, t);
+ if (verifier_bug_if(!subprog, env, "no subprog contains insn %d", t))
+ return ERR_PTR(-EFAULT);
subprog_start = subprog->start;
- subprog_end = (subprog + 1)->start;
- jt = jt_from_subprog(env, subprog_start, subprog_end);
- if (IS_ERR(jt))
- return jt;
- /* Check that the every element of the jump table fits within the given subprogram */
- for (i = 0; i < jt->cnt; i++) {
- if (jt->items[i] < subprog_start || jt->items[i] >= subprog_end) {
- verbose(env, "jump table for insn %d points outside of the subprog [%u,%u]\n",
- t, subprog_start, subprog_end);
- bpf_diag_program_structure(
- env, t, "jump table target out of range",
- "Keep every jump-table target inside the same subprogram.",
- "The jump table for instruction %d points outside subprogram range [%u,%u).",
- t, subprog_start, subprog_end);
- kvfree(jt);
- return ERR_PTR(-EINVAL);
- }
+ if (!subprog->jt) {
+ verbose(env, "no jump tables found for subprog starting at %u\n", subprog_start);
+ bpf_diag_program_structure(
+ env, subprog_start, "missing jump table",
+ "Make sure subprograms containing gotox instructions are accompanied by jump tables referencing these subprograms.",
+ "No jump table was found for the subprogram that starts at instruction %u.",
+ subprog_start);
+ return ERR_PTR(-EINVAL);
}
- return jt;
+ return subprog->jt;
}
/* "conditional jump with N edges" */
@@ -381,27 +412,39 @@ static int visit_gotox_insn(int t, struct bpf_verifier_env *env)
struct bpf_iarray *jt;
int i, w;
- jt = env->insn_aux_data[t].jt;
- if (!jt) {
- jt = create_jt(t, env);
- if (IS_ERR(jt))
- return PTR_ERR(jt);
+ /*
+ * The first visit pushes every target that still needs exploring, and
+ * those are all done by the time this insn is revisited, so there is
+ * nothing left to walk the jump table for.
+ */
+ if (insn_state[t] & BRANCH)
+ return DONE_EXPLORING;
- env->insn_aux_data[t].jt = jt;
+ jt = subprog_jt(t, env);
+ if (IS_ERR(jt))
+ return PTR_ERR(jt);
+
+ insn_state[t] |= BRANCH;
+
+ if (check_add_overflow(env->cfg.gotox_edges, jt->cnt,
+ &env->cfg.gotox_edges) ||
+ env->cfg.gotox_edges > BPF_MAX_GOTOX_EDGES) {
+ verbose(env, "number of indirect jump edges in the program exceeds %u\n",
+ BPF_MAX_GOTOX_EDGES);
+ bpf_diag_program_structure(
+ env, t, "too many indirect jump edges",
+ "Reduce the number of indirect jumps, or the number of distinct targets they can reach.",
+ "The program has more than %u indirect jump edges in total, counted over every gotox instruction.",
+ BPF_MAX_GOTOX_EDGES);
+ return -E2BIG;
}
mark_prune_point(env, t);
for (i = 0; i < jt->cnt; i++) {
w = jt->items[i];
- if (w < 0 || w >= env->prog->len) {
- verbose(env, "indirect jump out of range from insn %d to %d\n", t, w);
- bpf_diag_program_structure(
- env, t, "indirect jump out of range",
- "Keep indirect jump targets inside the program.",
- "Instruction %d can jump indirectly to instruction %d, but the program only contains instructions 0 through %d.",
- t, w, env->prog->len - 1);
- return -EINVAL;
- }
+ if (verifier_bug_if(w < 0 || w >= env->prog->len, env,
+ "indirect jump from insn %d to %d out of range", t, w))
+ return -EFAULT;
mark_jmp_point(env, w);
mark_jump_target(env, w);
@@ -491,30 +534,6 @@ static int visit_func_ptrs_insn(int t, struct bpf_verifier_env *env,
return DONE_EXPLORING;
}
-/*
- * Instructions that can abnormally return from a subprog (tail_call
- * upon success, ld_{abs,ind} upon load failure) have a hidden exit
- * that the verifier must account for.
- */
-static int visit_abnormal_return_insn(struct bpf_verifier_env *env, int t)
-{
- struct bpf_subprog_info *subprog;
- struct bpf_iarray *jt;
-
- if (env->insn_aux_data[t].jt)
- return 0;
-
- jt = bpf_iarray_realloc(NULL, 2);
- if (!jt)
- return -ENOMEM;
-
- subprog = bpf_find_containing_subprog(env, t);
- jt->items[0] = t + 1;
- jt->items[1] = subprog->exit_idx;
- env->insn_aux_data[t].jt = jt;
- return 0;
-}
-
/* Visits the instruction at index t and returns one of the following:
* < 0 - an error occurred
* DONE_EXPLORING - the instruction was fully explored
@@ -537,13 +556,6 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
/* All non-branch instructions have a single fall-through edge. */
if (BPF_CLASS(insn->code) != BPF_JMP &&
BPF_CLASS(insn->code) != BPF_JMP32) {
- if (BPF_CLASS(insn->code) == BPF_LD &&
- (BPF_MODE(insn->code) == BPF_ABS ||
- BPF_MODE(insn->code) == BPF_IND)) {
- ret = visit_abnormal_return_insn(env, t);
- if (ret)
- return ret;
- }
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
return push_insn(t, t + insn_sz, FALLTHROUGH, env);
}
@@ -588,11 +600,6 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
mark_subprog_might_sleep(env, t);
if (bpf_helper_changes_pkt_data(insn->imm))
mark_subprog_changes_pkt_data(env, t);
- if (insn->imm == BPF_FUNC_tail_call) {
- ret = visit_abnormal_return_insn(env, t);
- if (ret)
- return ret;
- }
} else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
struct bpf_call_arg_meta meta;
@@ -825,7 +832,7 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
struct bpf_insn_aux_data *aux = env->insn_aux_data;
const u32 insn_cnt = env->prog->len;
int stack_sz, dfs_sz, err = 0;
- u32 *stack, *pre, *low, *dfs;
+ u32 *stack, *pre, *low, *dfs, *dfs_pos;
u32 i, j, t, w;
u32 next_preorder_num;
u32 next_scc_id;
@@ -838,13 +845,16 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
* - 'stack' accumulates vertices in DFS order, see invariant comment below;
* - 'pre[t] == p' => preorder number of vertex 't' is 'p';
* - 'low[t] == n' => smallest preorder number of the vertex reachable from 't' is 'n';
- * - 'dfs' DFS traversal stack, used to emulate explicit recursion.
+ * - 'dfs' DFS traversal stack, used to emulate explicit recursion;
+ * - 'dfs_pos[k] == j' => the frame 'dfs[k]' resumes visiting its
+ * successors at index 'j'.
*/
stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
pre = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
low = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
dfs = kvcalloc(insn_cnt, sizeof(*dfs), GFP_KERNEL_ACCOUNT);
- if (!stack || !pre || !low || !dfs) {
+ dfs_pos = kvcalloc(insn_cnt, sizeof(*dfs_pos), GFP_KERNEL_ACCOUNT);
+ if (!stack || !pre || !low || !dfs || !dfs_pos) {
err = -ENOMEM;
goto exit;
}
@@ -927,6 +937,7 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
stack_sz = 0;
dfs_sz = 1;
dfs[0] = i;
+ dfs_pos[0] = 0;
dfs_continue:
while (dfs_sz) {
w = dfs[dfs_sz - 1];
@@ -936,13 +947,22 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
next_preorder_num++;
stack[stack_sz++] = w;
}
- /* Visit 'w' successors */
+ /* Visit remaining 'w' successors */
succ = bpf_insn_successors(env, w);
- for (j = 0; j < succ->cnt; ++j) {
+ for (j = dfs_pos[dfs_sz - 1]; j < succ->cnt; ++j) {
if (pre[succ->items[j]]) {
low[w] = min(low[w], low[succ->items[j]]);
} else {
- dfs[dfs_sz++] = succ->items[j];
+ /*
+ * Once DFS for succ->items[j] is complete,
+ * pre[succ->items[j]] is non-zero, hence
+ * resuming at 'j' allows to follow the
+ * low[w] = min(...) update branch above.
+ */
+ dfs_pos[dfs_sz - 1] = j;
+ dfs_pos[dfs_sz] = 0;
+ dfs[dfs_sz] = succ->items[j];
+ dfs_sz++;
goto dfs_continue;
}
}
@@ -992,5 +1012,6 @@ int bpf_compute_scc(struct bpf_verifier_env *env)
kvfree(pre);
kvfree(low);
kvfree(dfs);
+ kvfree(dfs_pos);
return err;
}
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index e9c2d6c06218f..37cf130ebb57b 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -214,13 +214,19 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
}
#endif
+enum bpf_patch_mode {
+ BPF_PATCH_KEEP_TARGET, /* targets of 'off' keep naming 'off', the first new insn */
+ BPF_PATCH_MOVE_TARGET, /* targets of 'off' follow the original insn to 'off + len - 1' */
+};
+
/* single env->prog->insni[off] instruction was replaced with the range
* insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
* [0, off) and [off, end) to new locations, so the patched range stays zero
*/
static void adjust_insn_aux_data(struct bpf_verifier_env *env,
struct bpf_prog *new_prog, u32 off, u32 cnt,
- struct bpf_insn *original_insn)
+ struct bpf_insn *original_insn,
+ enum bpf_patch_mode mode)
{
struct bpf_insn_aux_data *data = env->insn_aux_data;
struct bpf_insn *insn = new_prog->insnsi;
@@ -253,7 +259,12 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
/* Expand insni[off]'s seen count to the patched range. */
data[i].seen = old_seen;
data[i].zext_dst = bpf_insn_def32(new_prog, insn + i) >= 0;
- if (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
+ /*
+ * In BPF_PATCH_MOVE_TARGET mode the original insn is the last
+ * slot by construction.
+ */
+ if (mode == BPF_PATCH_KEEP_TARGET &&
+ !memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
data[off + cnt - 1].non_stack_access = false;
@@ -277,9 +288,10 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
* new instructions by the above memmove and memset, but the indirect jump target is
* actually the first instruction, so move it back. This also matches with the behavior
* of bpf_insn_array_adjust(), which preserves xlated_off to point to the first new
- * instruction.
+ * instruction. For BPF_PATCH_MOVE_TARGET the original instruction is the last one,
+ * so the flag already sits where needed.
*/
- if (data[off + cnt - 1].indirect_target) {
+ if (mode == BPF_PATCH_KEEP_TARGET && data[off + cnt - 1].indirect_target) {
data[off].indirect_target = 1;
data[off + cnt - 1].indirect_target = 0;
}
@@ -299,7 +311,7 @@ static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len
}
}
-static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 off, u32 len)
+static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 first, u32 len)
{
int i;
@@ -307,7 +319,7 @@ static void adjust_insn_arrays(struct bpf_verifier_env *env, u32 off, u32 len)
return;
for (i = 0; i < env->insn_array_map_cnt; i++)
- bpf_insn_array_adjust(env->insn_array_maps[i], off, len);
+ bpf_insn_array_adjust(env->insn_array_maps[i], first, len);
}
static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 off, u32 len)
@@ -318,7 +330,7 @@ static void adjust_insn_arrays_after_remove(struct bpf_verifier_env *env, u32 of
bpf_insn_array_adjust_after_remove(env->insn_array_maps[i], off, len);
}
-static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len)
+static void adjust_poke_descs(struct bpf_prog *prog, u32 first, u32 len)
{
struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab;
int i, sz = prog->aux->size_poke_tab;
@@ -326,7 +338,7 @@ static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len)
for (i = 0; i < sz; i++) {
desc = &tab[i];
- if (desc->insn_idx <= off)
+ if (desc->insn_idx < first)
continue;
desc->insn_idx += len - 1;
}
@@ -345,11 +357,14 @@ static bool bpf_rewrite_must_abort(void)
return false;
}
-struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
- const struct bpf_insn *patch, u32 len)
+static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
+ const struct bpf_insn *patch, u32 len,
+ enum bpf_patch_mode mode)
{
struct bpf_prog *new_prog;
struct bpf_insn_aux_data *new_data = NULL;
+ /* The first slot whose targets move. */
+ u32 first = mode == BPF_PATCH_MOVE_TARGET ? off : off + 1;
struct bpf_insn original_insn;
if (bpf_rewrite_must_abort())
@@ -375,14 +390,20 @@ struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
env->insn_aux_data[off].orig_idx);
return NULL;
}
- adjust_insn_aux_data(env, new_prog, off, len, &original_insn);
+ adjust_insn_aux_data(env, new_prog, off, len, &original_insn, mode);
adjust_subprog_starts(env, off, len);
- adjust_insn_arrays(env, off, len);
+ adjust_insn_arrays(env, first, len);
bpf_adjust_func_ptrs(env, off, len);
- adjust_poke_descs(new_prog, off, len);
+ adjust_poke_descs(new_prog, first, len);
return new_prog;
}
+struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
+ const struct bpf_insn *patch, u32 len)
+{
+ return __bpf_patch_insn_data(env, off, patch, len, BPF_PATCH_KEEP_TARGET);
+}
+
/*
* insn was moved down by delta insns inside its own patch. Operands relative
* to the pc that point in front of the old position did not move with it.
@@ -480,7 +501,11 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
if (j > i) {
struct bpf_prog_aux *aux = env->prog->aux;
- int move;
+ int move, k;
+
+ /* The removed subprogs own their jump tables. */
+ for (k = i; k < j; k++)
+ kvfree(env->subprog_info[k].jt);
/* move fake 'exit' subprog as well */
move = env->subprog_cnt + 1 - j;
@@ -488,6 +513,9 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
memmove(env->subprog_info + i,
env->subprog_info + j,
sizeof(*env->subprog_info) * move);
+ /* The vacated tail still holds copies of the moved entries. */
+ memset(env->subprog_info + i + move, 0,
+ sizeof(*env->subprog_info) * (j - i));
env->subprog_cnt -= j - i;
/* remove func_info and its aux */
@@ -506,6 +534,9 @@ static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
* in adjust_btf_func() - no need to adjust
*/
}
+ /* The exception callback is live, so it cannot sit inside the removed range. */
+ if (env->exception_callback_subprog >= j)
+ env->exception_callback_subprog -= j - i;
} else {
/* convert i from "first prog to remove" to "first to adjust" */
if (env->subprog_info[i].start == off)
@@ -583,23 +614,6 @@ static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
return 0;
}
-/*
- * Clean up dynamically allocated fields of aux data for instructions [start, ...]
- */
-void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len)
-{
- struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
- int end = start + len;
- int i;
-
- for (i = start; i < end; i++) {
- if (aux_data[i].jt) {
- kvfree(aux_data[i].jt);
- aux_data[i].jt = NULL;
- }
- }
-}
-
static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
{
struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
@@ -612,8 +626,6 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
if (bpf_prog_is_offloaded(env->prog->aux))
bpf_prog_offload_remove_insns(env, off, cnt);
- bpf_clear_insn_aux_data(env, off, cnt);
-
err = bpf_remove_insns(env->prog, off, cnt);
if (err)
return err;
@@ -736,6 +748,7 @@ int bpf_opt_remove_dead_code(struct bpf_verifier_env *env)
int bpf_opt_remove_nops(struct bpf_verifier_env *env)
{
+ struct bpf_insn_aux_data *aux = env->insn_aux_data;
struct bpf_insn *insn = env->prog->insnsi;
int insn_cnt = env->prog->len;
bool is_may_goto_0, is_ja;
@@ -747,6 +760,8 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
if (!is_may_goto_0 && !is_ja)
continue;
+ if (aux[i].indirect_target)
+ continue;
err = verifier_remove_insns(env, i, 1);
if (err)
@@ -889,7 +904,8 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_1,
-subprogs[0].stack_depth);
insn_buf[cnt++] = env->prog->insnsi[0];
- new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
+ new_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,
+ BPF_PATCH_MOVE_TARGET);
if (!new_prog)
return -ENOMEM;
env->prog = new_prog;
@@ -912,7 +928,8 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
verifier_bug(env, "prologue is too long");
return -EFAULT;
} else if (cnt) {
- new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
+ new_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,
+ BPF_PATCH_MOVE_TARGET);
if (!new_prog)
return -ENOMEM;
@@ -1685,6 +1702,8 @@ static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *pat
env->prog = prog;
info[cnt + 1].start = info[cnt].start;
info[cnt].start = prog->len - len + 1;
+ /* The patch ends with the exit. */
+ info[cnt].exit_idx = prog->len - 1;
env->subprog_cnt++;
env->hidden_subprog_cnt++;
return 0;
@@ -2670,7 +2689,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
}
cnt++;
- new_prog = bpf_patch_insn_data(env, subprog_start, insn_buf, cnt);
+ new_prog = __bpf_patch_insn_data(env, subprog_start, insn_buf, cnt,
+ BPF_PATCH_MOVE_TARGET);
if (!new_prog)
return -ENOMEM;
env->prog = prog = new_prog;
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 16fb7dc43726f..cd9523f69298e 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -350,14 +350,18 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
struct bpf_iarray *succ, *jt;
int insn_sz;
- jt = env->insn_aux_data[idx].jt;
- if (unlikely(jt))
- return jt;
-
/* pre-allocated array of size up to 2; reset cnt, as it may have been used already */
succ = env->succ;
succ->cnt = 0;
+ /* All gotox of a subprogram share its jump table, see compute_subprog_jts() */
+ if (unlikely(insn_is_gotox(insn))) {
+ jt = bpf_find_containing_subprog(env, idx)->jt;
+ if (verifier_bug_if(!jt, env, "no jump table for gotox insn %u", idx))
+ return succ;
+ return jt;
+ }
+
opcode_info = &opcode_info_tbl[BPF_CLASS(insn->code) | BPF_OP(insn->code)];
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
if (opcode_info->can_fallthrough)
@@ -366,6 +370,20 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
if (opcode_info->can_jump)
succ->items[succ->cnt++] = idx + bpf_jmp_offset(insn) + 1;
+ /*
+ * tail_call upon success and ld_{abs,ind} upon load failure return
+ * from the subprogram, which is a hidden edge to its exit. A subprogram
+ * without an exit insn has no exit to model.
+ */
+ if (unlikely((bpf_helper_call(insn) && insn->imm == BPF_FUNC_tail_call) ||
+ (BPF_CLASS(insn->code) == BPF_LD &&
+ (BPF_MODE(insn->code) == BPF_ABS || BPF_MODE(insn->code) == BPF_IND)))) {
+ u32 exit_idx = bpf_find_containing_subprog(env, idx)->exit_idx;
+
+ if (exit_idx != U32_MAX)
+ succ->items[succ->cnt++] = exit_idx;
+ }
+
return succ;
}
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index c495dc9070dea..c1d6d217e0e86 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -2470,6 +2470,7 @@ static int add_subprog(struct bpf_verifier_env *env, int off)
return -E2BIG;
}
/* determine subprog starts. The end is one before the next starts */
+ env->subprog_info[env->subprog_cnt].exit_idx = U32_MAX;
env->subprog_info[env->subprog_cnt++].start = off;
sort(env->subprog_info, env->subprog_cnt,
sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
@@ -19252,10 +19253,25 @@ static int indirect_jump_min_max_index(struct bpf_verifier_env *env,
return 0;
}
+static int reject_gotox_out_of_subprog(struct bpf_verifier_env *env, u32 target,
+ u32 subprog_start, u32 subprog_end)
+{
+ verbose(env, "indirect jump from insn %d to %u leaves the subprog [%u,%u)\n",
+ env->insn_idx, target, subprog_start, subprog_end);
+ bpf_diag_program_structure(
+ env, env->insn_idx, "indirect jump leaves subprogram",
+ "Keep every reachable jump-table target inside the subprogram of the indirect jump.",
+ "Instruction %d can jump indirectly to instruction %u, which is outside its own subprogram [%u,%u).",
+ env->insn_idx, target, subprog_start, subprog_end);
+ return -EINVAL;
+}
+
/* gotox *dst_reg */
static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)
{
struct bpf_verifier_state *other_branch;
+ struct bpf_subprog_info *subprog;
+ u32 subprog_start, subprog_end;
struct bpf_reg_state *dst_reg;
struct bpf_map *map;
u32 min_index, max_index;
@@ -19299,6 +19315,24 @@ static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *in
return -EINVAL;
}
+ subprog = bpf_find_containing_subprog(env, env->insn_idx);
+ if (verifier_bug_if(!subprog, env, "no subprog contains insn %d", env->insn_idx))
+ return -EFAULT;
+ subprog_start = subprog->start;
+ subprog_end = (subprog + 1)->start;
+
+ /*
+ * Maps are confined to a subprog, see compute_subprog_jts(), and the
+ * targets are sorted and free of duplicates, see sort_insn_array_uniq(),
+ * so either all of them belong to this subprog or none of them does.
+ */
+ if (env->gotox_tmp_buf->items[0] < subprog_start)
+ return reject_gotox_out_of_subprog(env, env->gotox_tmp_buf->items[0],
+ subprog_start, subprog_end);
+ if (env->gotox_tmp_buf->items[n - 1] >= subprog_end)
+ return reject_gotox_out_of_subprog(env, env->gotox_tmp_buf->items[n - 1],
+ subprog_start, subprog_end);
+
for (i = 0; i < n - 1; i++) {
mark_indirect_target(env, env->gotox_tmp_buf->items[i]);
other_branch = push_stack(env, env->gotox_tmp_buf->items[i],
@@ -22750,8 +22784,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
release_maps(env);
release_btfs(env);
err_free_env:
- if (env->insn_aux_data)
- bpf_clear_insn_aux_data(env, 0, env->insn_aux_data_len);
+ bpf_free_subprog_jts(env);
vfree(env->insn_aux_data);
kvfree(env->fd_array);
bpf_stack_liveness_free(env);
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c b/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c
index 0222a9a5d0761..bfedeed24ac54 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <bpf/bpf.h>
+#include <bpf/btf.h>
#include <test_progs.h>
#if defined(__x86_64__) || defined(__powerpc__) || defined(__aarch64__)
@@ -453,43 +454,672 @@ static void check_bpf_no_lookup(void)
close(map_fd);
}
+#define GOTOX_LOG_SZ (256 * 1024)
+
+/* Load and return the fd, which the caller owns. */
+static int gotox_prog_load_funcs_fd(struct bpf_insn *insns, __u32 insn_cnt,
+ int *fd_array, __u32 fd_array_cnt, char *log,
+ int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt)
+{
+ LIBBPF_OPTS(bpf_prog_load_opts, opts);
+
+ log[0] = 0;
+ opts.fd_array = fd_array;
+ opts.fd_array_cnt = fd_array_cnt;
+ opts.log_buf = log;
+ opts.log_size = GOTOX_LOG_SZ;
+ opts.log_level = 1;
+ if (fi_cnt) {
+ opts.prog_btf_fd = btf_fd;
+ opts.func_info = fi;
+ opts.func_info_cnt = fi_cnt;
+ opts.func_info_rec_size = sizeof(*fi);
+ }
+
+ return bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, &opts);
+}
+
+/* Load for the outcome only: 0 or the negative errno. */
+static int gotox_prog_load_funcs(struct bpf_insn *insns, __u32 insn_cnt,
+ int *fd_array, __u32 fd_array_cnt, char *log,
+ int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt)
+{
+ int prog_fd;
+
+ prog_fd = gotox_prog_load_funcs_fd(insns, insn_cnt, fd_array, fd_array_cnt, log,
+ btf_fd, fi, fi_cnt);
+ if (prog_fd < 0)
+ return prog_fd;
+ close(prog_fd);
+ return 0;
+}
+
+static int gotox_prog_load(struct bpf_insn *insns, __u32 insn_cnt,
+ int *fd_array, __u32 fd_array_cnt, char *log)
+{
+ return gotox_prog_load_funcs(insns, insn_cnt, fd_array, fd_array_cnt, log,
+ -1, NULL, 0);
+}
+
+static int gotox_jt_create_offs(const __u32 *offs, __u32 cnt)
+{
+ struct bpf_insn_array_value val = {};
+ int map_fd;
+ __u32 i;
+
+ map_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, cnt);
+ if (!ASSERT_GE(map_fd, 0, "map_create"))
+ return map_fd;
+
+ for (i = 0; i < cnt; i++) {
+ val.orig_off = offs[i];
+ if (!ASSERT_EQ(bpf_map_update_elem(map_fd, &i, &val, 0), 0,
+ "bpf_map_update_elem"))
+ goto err;
+ }
+
+ if (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, "bpf_map_freeze"))
+ goto err;
+
+ return map_fd;
+err:
+ close(map_fd);
+ return -1;
+}
+
+#define GOTOX_SUB_START 4
+#define GOTOX_MAIN_TGT 2
+#define GOTOX_SUB_TGT 8
+#define GOTOX_TWO_INSN_CNT 10
+
+static void gotox_two_subprogs_fill(struct bpf_insn *insns, __u32 jt_idx, __u32 jt_off)
+{
+ insns[0] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ GOTOX_SUB_START - 1 - 1);
+ insns[GOTOX_MAIN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[3] = BPF_EXIT_INSN();
+
+ /* r1 = &jt[0], by index 'jt_idx' into fd_array */
+ insns[GOTOX_SUB_START] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = jt_idx,
+ };
+ insns[GOTOX_SUB_START + 1] = (struct bpf_insn) { .imm = 0 };
+ insns[6] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, jt_off * 8);
+ insns[7] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_SUB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insns[9] = BPF_EXIT_INSN();
+}
+
+/*
+ * Load 'insns' with the two jump tables 'jt_a' and 'jt_b', in this order in
+ * fd_array, and check the outcome. 'fi' describes the subprograms when given.
+ */
+static void check_gotox_two_maps(struct bpf_insn *insns, __u32 insn_cnt,
+ const __u32 *jt_a, __u32 a_cnt,
+ const __u32 *jt_b, __u32 b_cnt,
+ int btf_fd, struct bpf_func_info *fi, __u32 fi_cnt,
+ int expect_err, const char *expect_msg)
+{
+ int map_fd[2] = { -1, -1 };
+ char *log;
+ int err;
+
+ log = calloc(1, GOTOX_LOG_SZ);
+ if (!ASSERT_OK_PTR(log, "calloc log"))
+ return;
+
+ map_fd[0] = gotox_jt_create_offs(jt_a, a_cnt);
+ if (map_fd[0] < 0)
+ goto free_log;
+ map_fd[1] = gotox_jt_create_offs(jt_b, b_cnt);
+ if (map_fd[1] < 0)
+ goto close_maps;
+
+ if (fi)
+ err = gotox_prog_load_funcs(insns, insn_cnt, map_fd, 2, log,
+ btf_fd, fi, fi_cnt);
+ else
+ err = gotox_prog_load(insns, insn_cnt, map_fd, 2, log);
+ if (expect_err)
+ ASSERT_EQ(err, expect_err, "program should have been rejected");
+ else
+ ASSERT_EQ(err, 0, "bpf(BPF_PROG_LOAD)");
+ if (expect_msg)
+ ASSERT_HAS_SUBSTR(log, expect_msg, "verifier log");
+
+close_maps:
+ close(map_fd[0]);
+ if (map_fd[1] >= 0)
+ close(map_fd[1]);
+free_log:
+ free(log);
+}
+
+static void check_gotox_target_other_subprog(void)
+{
+ const __u32 jt_main[] = { GOTOX_MAIN_TGT };
+ const __u32 jt_sub[] = { GOTOX_SUB_TGT };
+ struct bpf_insn insns[GOTOX_TWO_INSN_CNT];
+
+ gotox_two_subprogs_fill(insns, 0, 0);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,
+ -EINVAL, "indirect jump from insn 7 to 2 leaves the subprog [4,10)");
+}
+
+static void check_gotox_jt_per_subprog(void)
+{
+ const __u32 jt_main[] = { GOTOX_MAIN_TGT };
+ const __u32 jt_sub[] = { GOTOX_SUB_TGT };
+ struct bpf_insn insns[GOTOX_TWO_INSN_CNT];
+
+ gotox_two_subprogs_fill(insns, 1, 0);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,
+ 0, NULL);
+}
+
+#define GOTOX_DEAD_MAIN_LEN 7
+#define GOTOX_DEAD_SUB_LEN 6
+#define GOTOX_DEAD_INSN_CNT (GOTOX_DEAD_MAIN_LEN + 3 * GOTOX_DEAD_SUB_LEN)
+#define GOTOX_DEAD_LIVE_CNT 10
+
+/* r1 = &jt[0], by index 'jt_idx' into fd_array; r1 = *r1; gotox r1; r0 = 0; exit */
+static void gotox_sub_fill(struct bpf_insn *insns, __u32 start, __u32 jt_idx)
+{
+ insns[start] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = jt_idx,
+ };
+ insns[start + 1] = (struct bpf_insn) { .imm = 0 };
+ insns[start + 2] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+ insns[start + 3] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[start + 4] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[start + 5] = BPF_EXIT_INSN();
+}
+
+/*
+ * Two subprograms which are only called from dead code sit in front of the
+ * live one, and all three own a jump table. Dead code removal drops both in
+ * one go, which compacts subprog_info[] along with the tables.
+ */
+static void check_gotox_jt_dead_subprogs(void)
+{
+ const __u32 sub_start[3] = { GOTOX_DEAD_MAIN_LEN,
+ GOTOX_DEAD_MAIN_LEN + GOTOX_DEAD_SUB_LEN,
+ GOTOX_DEAD_MAIN_LEN + 2 * GOTOX_DEAD_SUB_LEN };
+ struct bpf_insn insns[GOTOX_DEAD_INSN_CNT];
+ int map_fd[3] = { -1, -1, -1 };
+ struct bpf_prog_info info = {};
+ __u32 len = sizeof(info), tgt, i;
+ int prog_fd;
+ char *log;
+
+ log = calloc(1, GOTOX_LOG_SZ);
+ if (!ASSERT_OK_PTR(log, "calloc log"))
+ return;
+
+ insns[0] = BPF_MOV64_IMM(BPF_REG_1, 0);
+ insns[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2);
+ insns[2] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ sub_start[0] - 2 - 1);
+ insns[3] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ sub_start[1] - 3 - 1);
+ insns[4] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ sub_start[2] - 4 - 1);
+ insns[5] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[6] = BPF_EXIT_INSN();
+
+ for (i = 0; i < ARRAY_SIZE(sub_start); i++) {
+ gotox_sub_fill(insns, sub_start[i], i);
+ tgt = sub_start[i] + 4;
+ map_fd[i] = gotox_jt_create_offs(&tgt, 1);
+ if (map_fd[i] < 0)
+ goto close_maps;
+ }
+
+ prog_fd = gotox_prog_load_funcs_fd(insns, ARRAY_SIZE(insns), map_fd,
+ ARRAY_SIZE(map_fd), log, -1, NULL, 0);
+ if (!ASSERT_GE(prog_fd, 0, "bpf(BPF_PROG_LOAD)"))
+ goto close_maps;
+
+ /* Both dead subprograms and the calls to them must be gone. */
+ if (ASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, &info, &len),
+ "bpf_prog_get_info_by_fd"))
+ ASSERT_EQ(info.xlated_prog_len / sizeof(struct bpf_insn),
+ GOTOX_DEAD_LIVE_CNT, "xlated insn count");
+ close(prog_fd);
+close_maps:
+ for (i = 0; i < ARRAY_SIZE(map_fd); i++)
+ if (map_fd[i] >= 0)
+ close(map_fd[i]);
+ free(log);
+}
+
+#define GOTOX_FWD_GOTOX 11
+#define GOTOX_FWD_OWN_TGT 12
+#define GOTOX_FWD_SUB_START 14
+#define GOTOX_FWD_INSN_CNT 16
+
+static void gotox_from_main_fill(struct bpf_insn *insns)
+{
+ insns[0] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
+ insns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ GOTOX_FWD_SUB_START - 1 - 1);
+ insns[2] = BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6,
+ offsetof(struct xdp_md, ingress_ifindex));
+ insns[3] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_2, 0, 4);
+
+ /* r1 = &jt_leaves[0], by index 1 into fd_array */
+ insns[4] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 1,
+ };
+ insns[5] = (struct bpf_insn) { .imm = 0 };
+ insns[6] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+ insns[7] = BPF_JMP_A(3);
+
+ /* r1 = &jt_own[0], by index 0 into fd_array */
+ insns[8] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 0,
+ };
+ insns[9] = (struct bpf_insn) { .imm = 0 };
+ insns[10] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+
+ insns[GOTOX_FWD_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_FWD_OWN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[13] = BPF_EXIT_INSN();
+ insns[GOTOX_FWD_SUB_START] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insns[15] = BPF_EXIT_INSN();
+}
+
+static void check_gotox_target_subprog_from_main(void)
+{
+ const __u32 jt_own[] = { GOTOX_FWD_OWN_TGT };
+ const __u32 jt_leaves[] = { GOTOX_FWD_SUB_START };
+ struct bpf_insn insns[GOTOX_FWD_INSN_CNT];
+
+ gotox_from_main_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_own, ARRAY_SIZE(jt_own),
+ jt_leaves, ARRAY_SIZE(jt_leaves), -1, NULL, 0,
+ -EINVAL, "indirect jump from insn 11 to 14 leaves the subprog [0,14)");
+}
+
+/*
+ * The only map of the subprog holding the gotox reaches past that subprog, so
+ * the subprog is left without a jump table at all.
+ */
+static void check_gotox_jt_spans_subprogs(void)
+{
+ const __u32 jt_span[] = { GOTOX_FWD_OWN_TGT, GOTOX_FWD_SUB_START };
+ const __u32 jt_leaves[] = { GOTOX_FWD_SUB_START };
+ struct bpf_insn insns[GOTOX_FWD_INSN_CNT];
+
+ gotox_from_main_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_span, ARRAY_SIZE(jt_span),
+ jt_leaves, ARRAY_SIZE(jt_leaves), -1, NULL, 0,
+ -EINVAL, "jump table of subprog starting at 0 spans multiple subprogs");
+}
+
+/*
+ * The subprog holding the gotox has a well formed jump table of its own and
+ * also collects a map that reaches past its end. The spanning map is still
+ * rejected, even though the subprog is not left without a table.
+ */
+static void check_gotox_jt_spans_with_own_table(void)
+{
+ const __u32 jt_own[] = { GOTOX_FWD_OWN_TGT };
+ const __u32 jt_span[] = { GOTOX_FWD_OWN_TGT, GOTOX_FWD_SUB_START };
+ struct bpf_insn insns[GOTOX_FWD_INSN_CNT];
+
+ gotox_from_main_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_own, ARRAY_SIZE(jt_own),
+ jt_span, ARRAY_SIZE(jt_span), -1, NULL, 0,
+ -EINVAL, "jump table of subprog starting at 0 spans multiple subprogs");
+}
+
+#define GOTOX_SLICE_SUB_START 6
+#define GOTOX_SLICE_GOTOX 14
+#define GOTOX_SLICE_SUB_TGT 15
+#define GOTOX_SLICE_INSN_CNT 17
+
+static void gotox_slice_fill(struct bpf_insn *insns)
+{
+ insns[0] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[1] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0,
+ GOTOX_SLICE_SUB_START - 1 - 1);
+ insns[2] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[3] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[4] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[5] = BPF_EXIT_INSN();
+
+ insns[GOTOX_SLICE_SUB_START] =
+ BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+ offsetof(struct xdp_md, ingress_ifindex));
+ insns[7] = BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 1);
+ insns[8] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1);
+ insns[9] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);
+
+ /* r1 = &jt_main[0], by index 0 into fd_array */
+ insns[10] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 0,
+ };
+ insns[11] = (struct bpf_insn) { .imm = 0 };
+ insns[12] = BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2);
+ insns[13] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+
+ insns[GOTOX_SLICE_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_SLICE_SUB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insns[16] = BPF_EXIT_INSN();
+}
+
+static void check_gotox_index_slice_other_subprog(void)
+{
+ const __u32 jt_main[] = { 2, 3, 4 };
+ const __u32 jt_sub[] = { GOTOX_SLICE_SUB_TGT };
+ struct bpf_insn insns[GOTOX_SLICE_INSN_CNT];
+
+ gotox_slice_fill(insns);
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), -1, NULL, 0,
+ -EINVAL, "indirect jump from insn 14 to 3 leaves the subprog [6,17)");
+}
+
+static int gotox_btf_create(const __u32 *starts, const __u8 *linkage, __u32 cnt,
+ struct bpf_func_info *fi, struct btf **pbtf)
+{
+ int int_id, proto_id, id;
+ struct btf *btf;
+ char name[24];
+ __u32 i;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "btf__new_empty"))
+ return -1;
+
+ int_id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
+ if (!ASSERT_GT(int_id, 0, "btf__add_int"))
+ goto err;
+
+ proto_id = btf__add_func_proto(btf, int_id);
+ if (!ASSERT_GT(proto_id, 0, "btf__add_func_proto"))
+ goto err;
+
+ for (i = 0; i < cnt; i++) {
+ snprintf(name, sizeof(name), "gotox_f%u", i);
+ id = btf__add_func(btf, name, linkage[i], proto_id);
+ if (!ASSERT_GT(id, 0, "btf__add_func"))
+ goto err;
+ fi[i].insn_off = starts[i];
+ fi[i].type_id = id;
+ }
+
+ if (!ASSERT_OK(btf__load_into_kernel(btf), "btf__load_into_kernel"))
+ goto err;
+
+ *pbtf = btf;
+ return btf__fd(btf);
+err:
+ btf__free(btf);
+ return -1;
+}
+
+static void check_gotox_target_other_global_subprog(void)
+{
+ const __u32 starts[] = { 0, GOTOX_SUB_START };
+ const __u8 linkage[] = { BTF_FUNC_GLOBAL, BTF_FUNC_GLOBAL };
+ const __u32 jt_main[] = { GOTOX_MAIN_TGT };
+ const __u32 jt_sub[] = { GOTOX_SUB_TGT };
+ struct bpf_insn insns[GOTOX_TWO_INSN_CNT];
+ struct bpf_func_info fi[2];
+ struct btf *btf = NULL;
+ int btf_fd;
+
+ gotox_two_subprogs_fill(insns, 0, 0);
+
+ btf_fd = gotox_btf_create(starts, linkage, ARRAY_SIZE(starts), fi, &btf);
+ if (btf_fd < 0)
+ return;
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_sub, ARRAY_SIZE(jt_sub), btf_fd, fi, ARRAY_SIZE(fi),
+ -EINVAL, "indirect jump from insn 7 to 2 leaves the subprog [4,10)");
+ btf__free(btf);
+}
+
+#define GOTOX_CB_MAIN_TGT 6
+#define GOTOX_CB_START 8
+#define GOTOX_CB_GOTOX 11
+#define GOTOX_CB_TGT 12
+#define GOTOX_CB_INSN_CNT 14
+
+static void gotox_callback_fill(struct bpf_insn *insns)
+{
+ insns[0] = BPF_MOV64_IMM(BPF_REG_1, 1);
+ /* r2 = &callback */
+ insns[1] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_2,
+ .src_reg = BPF_PSEUDO_FUNC,
+ .imm = GOTOX_CB_START - 1 - 1,
+ };
+ insns[2] = (struct bpf_insn) { .imm = 0 };
+ insns[3] = BPF_MOV64_IMM(BPF_REG_3, 0);
+ insns[4] = BPF_MOV64_IMM(BPF_REG_4, 0);
+ insns[5] = BPF_EMIT_CALL(BPF_FUNC_loop);
+ insns[GOTOX_CB_MAIN_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[7] = BPF_EXIT_INSN();
+
+ /* r1 = &jt_main[0], by index 0 into fd_array */
+ insns[GOTOX_CB_START] = (struct bpf_insn) {
+ .code = BPF_LD | BPF_DW | BPF_IMM,
+ .dst_reg = BPF_REG_1,
+ .src_reg = BPF_PSEUDO_MAP_IDX_VALUE,
+ .imm = 0,
+ };
+ insns[9] = (struct bpf_insn) { .imm = 0 };
+ insns[10] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
+ insns[GOTOX_CB_GOTOX] = BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0);
+ insns[GOTOX_CB_TGT] = BPF_MOV64_IMM(BPF_REG_0, 0);
+ insns[13] = BPF_EXIT_INSN();
+}
+
+static void check_gotox_callback_leaves_subprog(void)
+{
+ const __u32 starts[] = { 0, GOTOX_CB_START };
+ const __u8 linkage[] = { BTF_FUNC_GLOBAL, BTF_FUNC_STATIC };
+ const __u32 jt_main[] = { GOTOX_CB_MAIN_TGT };
+ const __u32 jt_cb[] = { GOTOX_CB_TGT };
+ struct bpf_insn insns[GOTOX_CB_INSN_CNT];
+ struct bpf_func_info fi[2];
+ struct btf *btf = NULL;
+ int btf_fd;
+
+ gotox_callback_fill(insns);
+
+ btf_fd = gotox_btf_create(starts, linkage, ARRAY_SIZE(starts), fi, &btf);
+ if (btf_fd < 0)
+ return;
+
+ check_gotox_two_maps(insns, ARRAY_SIZE(insns), jt_main, ARRAY_SIZE(jt_main),
+ jt_cb, ARRAY_SIZE(jt_cb), btf_fd, fi, ARRAY_SIZE(fi),
+ -EINVAL, "indirect jump from insn 11 to 6 leaves the subprog [8,14)");
+ btf__free(btf);
+}
+
static void check_bpf_side(void)
{
check_bpf_no_lookup();
}
+/* Return the xlated index of the first insn equal to 'insn', or -1. */
+static int xlated_insn_index(int prog_fd, const struct bpf_insn *insn)
+{
+ struct bpf_insn *xlated;
+ int idx = -1;
+ __u32 cnt, i;
+
+ if (!ASSERT_OK(get_xlated_program(prog_fd, &xlated, &cnt), "get_xlated_program"))
+ return -1;
+ for (i = 0; i < cnt; i++)
+ if (!memcmp(&xlated[i], insn, sizeof(*insn))) {
+ idx = i;
+ break;
+ }
+ free(xlated);
+ return idx;
+}
+
+/*
+ * Load 'insns' with a jump table naming insn 0, which a prologue prepend
+ * pushes down, and check that the slot followed it.
+ */
+static void check_gotox_target_prepend(enum bpf_prog_type prog_type,
+ struct bpf_insn *insns, __u32 insn_cnt)
+{
+ LIBBPF_OPTS(bpf_prog_load_opts, opts);
+ struct bpf_insn_array_value val = {};
+ int prog_fd = -1, map_fd, idx;
+ __u32 key = 0;
+
+ map_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, 1);
+ if (!ASSERT_GE(map_fd, 0, "map_create"))
+ return;
+
+ val.orig_off = 0;
+ if (!ASSERT_EQ(bpf_map_update_elem(map_fd, &key, &val, 0), 0, "bpf_map_update_elem"))
+ goto cleanup;
+ if (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, "bpf_map_freeze"))
+ goto cleanup;
+
+ opts.fd_array = &map_fd;
+ opts.fd_array_cnt = 1;
+ prog_fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insn_cnt, &opts);
+ if (!ASSERT_GE(prog_fd, 0, "bpf(BPF_PROG_LOAD)"))
+ goto cleanup;
+
+ idx = xlated_insn_index(prog_fd, &insns[0]);
+ if (!ASSERT_GT(idx, 0, "insn 0 was prepended to"))
+ goto cleanup;
+ if (!ASSERT_EQ(bpf_map_lookup_elem(map_fd, &key, &val), 0, "bpf_map_lookup_elem"))
+ goto cleanup;
+ ASSERT_EQ(val.xlated_off, (__u32)idx, "gotox target followed insn 0 past the prepend");
+cleanup:
+ if (prog_fd >= 0)
+ close(prog_fd);
+ close(map_fd);
+}
+
+static void check_gotox_target_prologue_shift(void)
+{
+ struct bpf_insn insns[] = {
+ /* insn 0: gotox target and subprog start */
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ /* may_goto +4 -> exit block, bounds the loop */
+ BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 4, 0),
+ /* r1 = &jt[0] (insns 2 and 3) */
+ BPF_LD_IMM64_RAW(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, 0),
+ BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0), /* r1 = ips[0] */
+ /* insn 5: gotox r1 -> insn 0 */
+ BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_1, 0, 0, 0),
+ BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), /* insn 6: exit block */
+ BPF_EXIT_INSN(),
+ };
+
+ check_gotox_target_prepend(BPF_PROG_TYPE_XDP, insns, ARRAY_SIZE(insns));
+}
+
+static void check_gotox_target_ctx_prologue_shift(void)
+{
+ struct bpf_insn insns[] = {
+ /* insn 0: gotox target and subprog start */
+ BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_10, -8),
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, 0x5a5a, 12), /* second visit exits */
+ BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x5a5a),
+ BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* r6 = ctx */
+ BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6, offsetof(struct __sk_buff, data)),
+ BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_6, offsetof(struct __sk_buff, data_end)),
+ BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 1),
+ BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 5),
+ /* insn 9: direct packet write -> tc unclone prologue prepended at insn 0 */
+ BPF_ST_MEM(BPF_B, BPF_REG_2, 0, 0),
+ /* r5 = &jt[0] (insns 10 and 11) */
+ BPF_LD_IMM64_RAW(BPF_REG_5, BPF_PSEUDO_MAP_IDX_VALUE, 0),
+ BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_5, 0), /* r5 = ips[0] */
+ /* insn 13: gotox r5 -> insn 0 */
+ BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_5, 0, 0, 0),
+ BPF_MOV64_IMM(BPF_REG_0, 0), /* insn 14: exit block */
+ BPF_EXIT_INSN(),
+ };
+
+ check_gotox_target_prepend(BPF_PROG_TYPE_SCHED_CLS, insns, ARRAY_SIZE(insns));
+}
+
static void __test_bpf_insn_array(void)
{
/* Test if offsets are adjusted properly */
-
if (test__start_subtest("one2one"))
check_one_to_one_mapping();
-
if (test__start_subtest("simple"))
check_simple();
-
if (test__start_subtest("deletions"))
check_deletions();
-
if (test__start_subtest("deletions-with-functions"))
check_deletions_with_functions();
-
if (test__start_subtest("blindness"))
check_blindness();
-
/* Check all kinds of operations and related restrictions */
-
if (test__start_subtest("incorrect-index"))
check_incorrect_index();
-
if (test__start_subtest("load-unfrozen-map"))
check_load_unfrozen_map();
-
if (test__start_subtest("no-map-reuse"))
check_no_map_reuse();
-
if (test__start_subtest("bpf-side-ops"))
check_bpf_side();
+ if (test__start_subtest("gotox-jt-spans-subprogs"))
+ check_gotox_jt_spans_subprogs();
+ if (test__start_subtest("gotox-jt-spans-with-own-table"))
+ check_gotox_jt_spans_with_own_table();
+ if (test__start_subtest("gotox-target-other-subprog"))
+ check_gotox_target_other_subprog();
+ if (test__start_subtest("gotox-jt-per-subprog"))
+ check_gotox_jt_per_subprog();
+ if (test__start_subtest("gotox-jt-dead-subprogs"))
+ check_gotox_jt_dead_subprogs();
+ if (test__start_subtest("gotox-target-subprog-from-main"))
+ check_gotox_target_subprog_from_main();
+ if (test__start_subtest("gotox-index-slice-other-subprog"))
+ check_gotox_index_slice_other_subprog();
+ if (test__start_subtest("gotox-target-other-global-subprog"))
+ check_gotox_target_other_global_subprog();
+ if (test__start_subtest("gotox-callback-leaves-subprog"))
+ check_gotox_callback_leaves_subprog();
+ if (test__start_subtest("gotox-target-prologue-shift"))
+ check_gotox_target_prologue_shift();
+ if (test__start_subtest("gotox-target-ctx-prologue-shift"))
+ check_gotox_target_ctx_prologue_shift();
}
#else
static void __test_bpf_insn_array(void)
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions.c b/tools/testing/selftests/bpf/prog_tests/exceptions.c
index 639866ce09a9f..3705db18c1bbc 100644
--- a/tools/testing/selftests/bpf/prog_tests/exceptions.c
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions.c
@@ -6,6 +6,7 @@
#include "exceptions_ext.skel.h"
#include "exceptions_fail.skel.h"
#include "exceptions_assert.skel.h"
+#include "exceptions_dead_subprog.skel.h"
static char log_buf[1024 * 1024];
@@ -410,9 +411,15 @@ static void test_exceptions_assertions(void)
RUN_TESTS(exceptions_assert);
}
+static void test_exceptions_dead_subprog(void)
+{
+ RUN_TESTS(exceptions_dead_subprog);
+}
+
void test_exceptions(void)
{
test_exceptions_success();
test_exceptions_failure();
test_exceptions_assertions();
+ test_exceptions_dead_subprog();
}
diff --git a/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c b/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c
new file mode 100644
index 0000000000000..4f8b82a85234f
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_dead_subprog.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+/*
+ * Read-only and frozen, so the verifier sees a constant zero, while clang
+ * cannot fold the volatile read and keeps the call below.
+ */
+const volatile int never;
+
+/* Only called from dead code, hence removed as a whole before the JIT runs. */
+static __noinline int dead_subprog(struct __sk_buff *ctx)
+{
+ return ctx->len;
+}
+
+__noinline int exception_cb_behind_dead_subprog_cb(u64 cookie)
+{
+ return 42;
+}
+
+/*
+ * libbpf appends the exception callback behind the subprograms reached by
+ * calls, so removing the dead one compacts subprog_info[] underneath the
+ * callback's index.
+ */
+SEC("?tc")
+__exception_cb(exception_cb_behind_dead_subprog_cb)
+__success __retval(42)
+int exception_cb_behind_dead_subprog(struct __sk_buff *ctx)
+{
+ if (never)
+ return dead_subprog(ctx);
+ bpf_throw(0);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_cfg.c b/tools/testing/selftests/bpf/progs/verifier_cfg.c
index 3c3bb03e8217a..07388a922e5b5 100644
--- a/tools/testing/selftests/bpf/progs/verifier_cfg.c
+++ b/tools/testing/selftests/bpf/progs/verifier_cfg.c
@@ -173,4 +173,42 @@ l1_%=: exit; \
" ::: __clobber_all);
}
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u32);
+} map_array SEC(".maps");
+
+extern void bpf_throw(__u64 cookie) __ksym;
+
+/*
+ * The subprogram leaves through the tail call or the exception and never
+ * reaches an exit insn, so the hidden edge of the tail call has no exit to
+ * point at. The stack access makes the liveness pass walk the subprogram,
+ * and the loop compiles to a single jump back onto itself.
+ */
+__noinline __used
+static int tail_call_or_throw_subprog(void *ctx)
+{
+ volatile __u32 idx = 0;
+
+ bpf_tail_call(ctx, &map_array, idx);
+ bpf_throw(0);
+ for (;;) {}
+}
+
+SEC("tc")
+__description("tail call in a subprogram without an exit")
+__arch_x86_64
+__arch_arm64
+__success
+__naked void tail_call_in_subprog_without_exit(void)
+{
+ asm volatile (" \
+ call tail_call_or_throw_subprog; \
+ exit; \
+" ::: __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_gotox.c b/tools/testing/selftests/bpf/progs/verifier_gotox.c
index 0e27c2c79c57e..f5a9878c7b8d4 100644
--- a/tools/testing/selftests/bpf/progs/verifier_gotox.c
+++ b/tools/testing/selftests/bpf/progs/verifier_gotox.c
@@ -366,7 +366,7 @@ __used static int test_subprog(void)
}
SEC("socket")
-__failure __msg("jump table for insn 4 points outside of the subprog [0,10]")
+__failure __msg("jump table of subprog starting at 0 spans multiple subprogs")
__naked void jump_table_outside_subprog(void)
{
asm volatile (" \
@@ -457,6 +457,129 @@ __naked void spill_fill_ptr_to_insn(void)
: __clobber_all);
}
+/*
+ * Emit each table entry next to the instruction it targets. Numeric labels
+ * can be reused by .rept, while %= keeps table symbols unique per asm block.
+ * BASE is the code section: libbpf relocates section-relative byte offsets
+ * to instruction offsets in the loaded program.
+ */
+#define GOTOX_TABLE_BEGIN(COUNT) \
+ ".pushsection .jumptables,\"\",@progbits;" \
+ "jt_%=:;" \
+ ".global jt_%=;" \
+ ".size jt_%=, 8 * (" #COUNT ");" \
+ ".popsection;" \
+ "r1 = jt_%= ll;"
+
+#define GOTOX_TABLE_ENTRY(LABEL, BASE) \
+ ".pushsection .jumptables,\"\",@progbits;" \
+ ".quad " LABEL " - " BASE ";" \
+ ".popsection;"
+
+/* N gotox instructions, each targeting all N gotox plus the final block. */
+#define GOTOX_SELF_TARGETS(N, BASE) \
+ GOTOX_TABLE_BEGIN(N + 1) \
+ "r0 = 0;" \
+ ".rept " #N ";" \
+ GOTOX_TABLE_ENTRY("1f", BASE) \
+ "1:; .8byte %[gotox];" \
+ ".endr;" \
+ GOTOX_TABLE_ENTRY("2f", BASE) \
+ "2:;"
+
+#define GOTOX_OPERAND \
+ __imm_insn(gotox, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0, 0))
+
+/*
+ * Count edges even when their targets are already discovered. These
+ * programs deliberately use a scalar: the CFG edge limit must reject them
+ * before symbolic verification reaches the invalid gotox operand.
+ */
+SEC("socket")
+__description("too-many-gotox-edges")
+__failure __msg("number of indirect jump edges in the program exceeds")
+__naked void too_many_gotox_edges(void)
+{
+ asm volatile (
+ /* 1000 * 1001 = 1,001,000 edges. */
+ GOTOX_SELF_TARGETS(1000, "socket")
+ "r0 = 0; exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+SEC("socket")
+__description("gotox-edges-at-limit")
+__success __retval(0)
+__naked void gotox_edges_at_limit(void)
+{
+ asm volatile (
+ /*
+ * 1000 gotox * 1000 targets = 1,000,000 CFG edges. At run
+ * time, each block loads the next table entry and jumps to
+ * the following block, so the program terminates.
+ */
+ GOTOX_TABLE_BEGIN(1000)
+ ".rept 1000;"
+ GOTOX_TABLE_ENTRY("1f", "socket")
+ "r0 = *(u64 *)(r1 + 0);"
+ "r1 += 8;"
+ ".8byte %[gotox];"
+ "1:;"
+ ".endr;"
+ "r0 = 0; exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+static __noinline __used __naked void gotox_edges_subprog(void)
+{
+ asm volatile (
+ GOTOX_SELF_TARGETS(750, ".text")
+ "r0 = 0; exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+SEC("socket")
+__description("gotox-edges-across-subprogs")
+__failure __msg("number of indirect jump edges in the program exceeds")
+__naked void gotox_edges_across_subprogs(void)
+{
+ asm volatile (
+ /* Each subprog has 563,250 edges; together, 1,126,500. */
+ GOTOX_SELF_TARGETS(750, "socket")
+ "call gotox_edges_subprog;"
+ "exit;"
+ : : GOTOX_OPERAND : __clobber_all);
+}
+
+/*
+ * The gotox target is a ja +0, which the nop removal pass must keep as the
+ * jump table still names it.
+ */
+SEC("socket")
+__success __retval(0)
+__xlated("goto pc+0")
+__naked void jump_table_target_nop(void)
+{
+ asm volatile (" \
+ .pushsection .jumptables,\"\",@progbits; \
+jt0_%=: \
+ .quad nop_%= - socket; \
+ .size jt0_%=, 8; \
+ .global jt0_%=; \
+ .popsection; \
+ \
+ r0 = jt0_%= ll; \
+ r0 = *(u64 *)(r0 + 0); \
+ .8byte %[gotox_r0]; \
+nop_%=: \
+ goto +0; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_insn(gotox_r0, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0, 0))
+ : __clobber_all);
+}
+
#endif /* __TARGET_ARCH_x86 || __TARGET_ARCH_arm64 || __TARGET_ARCH_powerpc*/
char _license[] SEC("license") = "GPL";
|
| 35/3 | 2026/09/27 14:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29253 output: 22 thoughts: 1826 Thoughts: **KMSAN Fuzzing Justification: BPF Patch Series Analysis** Alright, let's dive into this BPF patch series and determine if it warrants a dedicated KMSAN fuzzing session. My goal is to assess whether these changes introduce or expose potential vulnerabilities related to uninitialized memory reads or information leaks, beyond what KASAN already covers. First, I'll carefully scrutinize the diffs, paying close attention to the modified files, especially the BPF subsystem headers and core logic. The files touched, including `bpf.h`, `bpf_verifier.h`, and various C files related to instruction arrays, CFG, fixups, liveness, and the verifier itself, give me a good starting point. The presence of selftests is encouraging, as they should offer some test coverage of the changes. I'll go line by line through these changes, keeping in mind the KMSAN criteria. Specifically, I'm looking for: 1. **Kernel-to-User Interactions:** Are there any new kernel structures or modifications to existing ones that are being passed to userspace via `copy_to_user`, `put_user`, netlink, ioctl, or BPF buffers? This is a prime area for uninitialized memory exposure. 2. **Conditional Logic:** Are there any changes to conditional logic or branching that might depend on variables or struct fields that aren't fully initialized? Uninitialized variables in decision-making paths are a major red flag. 3. **Complex Data Structures:** Have complex data structures been allocated or modified, where uninitialized fields could be read later in the code? 4. **Bounds Checks & Logic:** Are there changes to bounds checks, lengths, or logic that could lead to accessing uninitialized bytes in existing buffers? I'm starting by looking at the data structures modifications: * The changes to `struct bpf_insn_aux_data` and `struct bpf_subprog_info` are notable. Removing `struct bpf_iarray *jt;` from `bpf_insn_aux_data` and adding it to `bpf_subprog_info` and adding new `gotox_edges` and `subprog_jts_ready` to `bpf_verifier_env.cfg` seem like a re-org, and I have to trace how the `jt` is used. * The function renames are important, like `bpf_insn_array_adjust`'s parameter rename, but don't seem like they directly lead to uninit issues. * The `bpf_insn_array_init`, `bpf_insn_array_release` and `bpf_insn_array_adjust` changes, in combination with the addition of `atomic_set_release` indicate the use of some kind of shared data structure. I'll need to confirm if it has any effect on uninit memory reads. * The changes to `cfg.c` appear to be significant, especially those related to jump tables (`jt`). The new functions `__compute_subprog_jts`, `compute_subprog_jts`, and `bpf_free_subprog_jts` are all interesting. Specifically, I'll need to check the `memcpy` for potential issues and how `jt` is initialized. I'll also check how the `dfs_pos` array is initialized and used in `bpf_compute_scc`. * The `fixups.c` changes, especially around `adjust_insn_aux_data` and the introduced `enum bpf_patch_mode`, are key, as they directly deal with moving instructions and potentially impacting data structures. The usage of `__bpf_patch_insn_data` is something to follow. * The logic in `liveness.c` and how it handles jump tables for gotox is important. Based on the initial scan, I'm most concerned with the changes related to jump tables (`jt`) and the allocation/deallocation of related structures, and the logic in `fixups.c`. The `bpf_iarray_realloc` function (which I'll use grepper to find) and how it's used within this patch series is the key. Are jump table entries properly initialized before being read or used? Does the `dfs_pos` array in `bpf_compute_scc` have any initialization concerns? Given these changes, a dedicated KMSAN fuzzing session might be justified if these modifications introduce the risk of reading uninitialized memory, or any user-space data structure. |
| 36/3 | 2026/09/27 14:20 | tool | grepper |
0mArgs:{
"Expression": "bpf_iarray_realloc"
}Results: {
"Output": "include/linux/bpf_verifier.h=1711=bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta);\ninclude/linux/bpf_verifier.h:1712:struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem);\ninclude/linux/bpf_verifier.h-1713-int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off);\n--\nkernel/bpf/cfg.c=163=static int visit_func_call_insn(int t, struct bpf_insn *insns,\n--\nkernel/bpf/cfg.c-187-\nkernel/bpf/cfg.c:188:struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem)\nkernel/bpf/cfg.c-189-{\n--\nkernel/bpf/cfg.c=261=static struct bpf_iarray *jt_from_map(struct bpf_map *map)\n--\nkernel/bpf/cfg.c-266-\nkernel/bpf/cfg.c:267:\tjt = bpf_iarray_realloc(NULL, map-\u003emax_entries);\nkernel/bpf/cfg.c-268-\tif (!jt)\n--\nkernel/bpf/cfg.c=294=static int __compute_subprog_jts(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-331-\t\told_cnt = subprog-\u003ejt ? subprog-\u003ejt-\u003ecnt : 0;\nkernel/bpf/cfg.c:332:\t\tjt = bpf_iarray_realloc(subprog-\u003ejt, old_cnt + jt_cur-\u003ecnt);\nkernel/bpf/cfg.c-333-\t\tif (!jt) {\n--\nkernel/bpf/verifier.c=19270=static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-19302-\tif (!env-\u003egotox_tmp_buf || env-\u003egotox_tmp_buf-\u003ecnt \u003c max_index - min_index + 1) {\nkernel/bpf/verifier.c:19303:\t\tenv-\u003egotox_tmp_buf = bpf_iarray_realloc(env-\u003egotox_tmp_buf,\nkernel/bpf/verifier.c-19304-\t\t\t\t\t\t max_index - min_index + 1);\n--\nkernel/bpf/verifier.c=22438=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22522-\t\tenv-\u003einsn_aux_data[i].orig_idx = i;\nkernel/bpf/verifier.c:22523:\tenv-\u003esucc = bpf_iarray_realloc(NULL, 2);\nkernel/bpf/verifier.c-22524-\tif (!env-\u003esucc)\n"
} |
| 37/3 | 2026/09/27 14:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32919 output: 36 thoughts: 25 Thoughts: Okay, here's my summary, taking on the persona you requested: **Diving into `bpf_iarray_realloc`** Right, let's take a look at `bpf_iarray_realloc` within the `kernel/bpf/cfg.c` file. I need to understand how this function handles the reallocation of the internal array used within the BPF instruction array. Given its name, it’s most likely involved in resizing the BPF instruction array dynamically. I need to see how it manages memory, handles potential failure, and ensures data integrity during the re-allocation process. Specifically, I'm interested in how it manages the copying or transfer of instruction data, edge cases with null or invalid inputs, and how it deals with the potential for memory allocation failures. I need to be sure it does this safely and efficiently to keep the BPF program execution streamlined. My focus will be on potential vulnerabilities or performance bottlenecks in this part of the BPF infrastructure. |
| 38/3 | 2026/09/27 14:20 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/cfg.c",
"FirstLine": 185,
"LineCount": 40
}Results: {
"Contents": " 185:\t\treturn ret;\n 186:\t}\n 187:\t\n 188:\tstruct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem)\n 189:\t{\n 190:\t\tsize_t new_size = sizeof(struct bpf_iarray) + n_elem * sizeof(old-\u003eitems[0]);\n 191:\t\tstruct bpf_iarray *new;\n 192:\t\n 193:\t\tnew = kvrealloc(old, new_size, GFP_KERNEL_ACCOUNT);\n 194:\t\tif (!new) {\n 195:\t\t\t/* this is what callers always want, so simplify the call site */\n 196:\t\t\tkvfree(old);\n 197:\t\t\treturn NULL;\n 198:\t\t}\n 199:\t\n 200:\t\tnew-\u003ecnt = n_elem;\n 201:\t\treturn new;\n 202:\t}\n 203:\t\n 204:\tstatic int copy_insn_array(struct bpf_map *map, u32 start, u32 end, u32 *items)\n 205:\t{\n 206:\t\tstruct bpf_insn_array_value *value;\n 207:\t\tu32 i;\n 208:\t\n 209:\t\tfor (i = start; i \u003c= end; i++) {\n 210:\t\t\tvalue = map-\u003eops-\u003emap_lookup_elem(map, \u0026i);\n 211:\t\t\t/*\n 212:\t\t\t * map_lookup_elem of an array map will never return an error,\n 213:\t\t\t * but not checking it makes some static analysers to worry\n 214:\t\t\t */\n 215:\t\t\tif (IS_ERR(value))\n 216:\t\t\t\treturn PTR_ERR(value);\n 217:\t\t\telse if (!value)\n 218:\t\t\t\treturn -EINVAL;\n 219:\t\t\titems[i - start] = value-\u003exlated_off;\n 220:\t\t}\n 221:\t\treturn 0;\n 222:\t}\n 223:\t\n 224:\tstatic int cmp_ptr_to_u32(const void *a, const void *b)\n"
} |
| 39/3 | 2026/09/27 14:20 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33793 output: 17 thoughts: 861 Thoughts: **Analyzing Code Initialization in the BPF Subprogram Compilation** Okay, let's break down this BPF subprogram compilation. First, looking at `__compute_subprog_jts`, I see how the jump table (`jt`) is managed. The allocation uses `bpf_iarray_realloc` to ensure the table grows appropriately, preserving previous entries and adding new ones. The `memcpy` call ensures the new jump table entries are populated correctly. Importantly, after the loop, the `sort_insn_array_uniq` function is called, suggesting these elements are initialized prior to the sort. Now, shifting gears to `bpf_compute_scc`, the allocation of `dfs_pos` with `kvcalloc` guarantees it is zero-initialized. This avoids the potential for garbage values, which is good practice. The subsequent `kvfree` call ensures proper cleanup. Next, I examined `bpf_insn_array.c` and found that `jitted_off` and `ips[i]` are explicitly initialized/zeroed, demonstrating a clear effort to prevent leftover values. I appreciate this explicit zeroing. Additionally, `adjust_subprog_starts_after_remove` explicitly zero-fills entries, ensuring no stale data remains in `subprog_info`. Finally, I checked the initialization of `exit_idx` in `add_subprog`. It's initialized to `U32_MAX`, which is a sentinel value. Now I'll use `grepper` to see where it's subsequently set within the codebase to better understand its purpose. I'm looking for the context of its use to determine where subprograms will actually exit. |
| 40/3 | 2026/09/27 14:21 | tool | grepper |
0mArgs:{
"Expression": "exit_idx"
}Results: {
"Output": "include/linux/bpf_verifier.h=812=struct bpf_subprog_info {\n--\ninclude/linux/bpf_verifier.h-820-\t */\ninclude/linux/bpf_verifier.h:821:\tu32 exit_idx;\ninclude/linux/bpf_verifier.h-822-\tstruct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */\n--\nkernel/bpf/fixups.c=1684=static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *patch, int len)\n--\nkernel/bpf/fixups.c-1705-\t/* The patch ends with the exit. */\nkernel/bpf/fixups.c:1706:\tinfo[cnt].exit_idx = prog-\u003elen - 1;\nkernel/bpf/fixups.c-1707-\tenv-\u003esubprog_cnt++;\n--\nkernel/bpf/liveness.c=320=bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n--\nkernel/bpf/liveness.c-380-\t\t (BPF_MODE(insn-\u003ecode) == BPF_ABS || BPF_MODE(insn-\u003ecode) == BPF_IND)))) {\nkernel/bpf/liveness.c:381:\t\tu32 exit_idx = bpf_find_containing_subprog(env, idx)-\u003eexit_idx;\nkernel/bpf/liveness.c-382-\nkernel/bpf/liveness.c:383:\t\tif (exit_idx != U32_MAX)\nkernel/bpf/liveness.c:384:\t\t\tsucc-\u003eitems[succ-\u003ecnt++] = exit_idx;\nkernel/bpf/liveness.c-385-\t}\n--\nkernel/bpf/verifier.c=2456=static int add_subprog(struct bpf_verifier_env *env, int off)\n--\nkernel/bpf/verifier.c-2472-\t/* determine subprog starts. The end is one before the next starts */\nkernel/bpf/verifier.c:2473:\tenv-\u003esubprog_info[env-\u003esubprog_cnt].exit_idx = U32_MAX;\nkernel/bpf/verifier.c-2474-\tenv-\u003esubprog_info[env-\u003esubprog_cnt++].start = off;\n--\nkernel/bpf/verifier.c=3110=static int check_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3138-\t\tif (BPF_OP(code) == BPF_EXIT) {\nkernel/bpf/verifier.c:3139:\t\t\tsubprog[cur_subprog].exit_idx = i;\nkernel/bpf/verifier.c-3140-\t\t\tgoto next;\n"
} |
| 41/3 | 2026/09/27 14:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35768 output: 29 thoughts: 39 Thoughts: Okay, here's my summary, taking the role of the expert: **Investigating `subprog_info` Allocation and Initialization** Alright, let's dig into this. I need to understand exactly how the `subprog_info` structure is handled. My primary concern here is likely memory management and initialization - particularly in the context of the BPF verifier. I'm thinking potential issues like uninitialized memory access, dangling pointers, or even memory leaks could be lurking if the allocation and setup aren't meticulously handled. Therefore, the first logical step is to pinpoint where this `subprog_info` variable is actually allocated and initialized within the kernel code. Specifically, I'll navigate to `kernel/bpf/verifier.c`. This file is the primary focus for anything verifier related, so that's where I will start my search to confirm how and when the variable is allocated and initialized. I need to see the context of its creation. Understanding the lifecycle of this struct is paramount to ensuring the integrity and security of the verifier. |
| 42/3 | 2026/09/27 14:21 | tool | grepper |
0mArgs:{
"Expression": "subprog_info",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "\nFull output is too long, showing 500 out of 626 lines.\nUse more precise expression if possible.\n\nkernel/bpf/btf.c=8387=static int btf_check_arg_slots(struct bpf_verifier_log *log, const char *tname,\nkernel/bpf/btf.c-8388-\t\t\t bool is_global, u32 slot_cnt,\nkernel/bpf/btf.c:8389:\t\t\t struct bpf_subprog_info *sub)\nkernel/bpf/btf.c-8390-{\n--\nkernel/bpf/btf.c=8418=int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)\n--\nkernel/bpf/btf.c-8420-\tbool is_global = subprog_aux(env, subprog)-\u003elinkage == BTF_FUNC_GLOBAL;\nkernel/bpf/btf.c:8421:\tstruct bpf_subprog_info *sub = subprog_info(env, subprog);\nkernel/bpf/btf.c-8422-\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\n--\nkernel/bpf/cfg.c=54=static void mark_subprog_changes_pkt_data(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-55-{\nkernel/bpf/cfg.c:56:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-57-\n--\nkernel/bpf/cfg.c=62=static void mark_subprog_might_sleep(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-63-{\nkernel/bpf/cfg.c:64:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-65-\n--\nkernel/bpf/cfg.c=70=static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-71-{\nkernel/bpf/cfg.c:72:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-73-\n--\nkernel/bpf/cfg.c=84=static void merge_callee_effects(struct bpf_verifier_env *env, int t, int w)\nkernel/bpf/cfg.c-85-{\nkernel/bpf/cfg.c:86:\tstruct bpf_subprog_info *caller, *callee;\nkernel/bpf/cfg.c-87-\n--\nkernel/bpf/cfg.c=294=static int __compute_subprog_jts(struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-295-{\nkernel/bpf/cfg.c:296:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-297-\tstruct bpf_iarray *jt, *jt_cur;\n--\nkernel/bpf/cfg.c-344-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/cfg.c:345:\t\tjt = env-\u003esubprog_info[i].jt;\nkernel/bpf/cfg.c-346-\t\tif (jt)\n--\nkernel/bpf/cfg.c=365=void bpf_free_subprog_jts(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-368-\nkernel/bpf/cfg.c:369:\tfor (i = 0; i \u003c ARRAY_SIZE(env-\u003esubprog_info); i++) {\nkernel/bpf/cfg.c:370:\t\tkvfree(env-\u003esubprog_info[i].jt);\nkernel/bpf/cfg.c:371:\t\tenv-\u003esubprog_info[i].jt = NULL;\nkernel/bpf/cfg.c-372-\t}\n--\nkernel/bpf/cfg.c=377=subprog_jt(int t, struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-378-{\nkernel/bpf/cfg.c:379:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-380-\tint subprog_start, err;\n--\nkernel/bpf/cfg.c=673=int bpf_check_cfg(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-691-\tex_insn_beg = env-\u003eexception_callback_subprog\nkernel/bpf/cfg.c:692:\t\t ? env-\u003esubprog_info[env-\u003eexception_callback_subprog].start\nkernel/bpf/cfg.c-693-\t\t : 0;\n--\nkernel/bpf/cfg.c-759-\tret = 0; /* cfg looks good */\nkernel/bpf/cfg.c:760:\tenv-\u003eprog-\u003eaux-\u003echanges_pkt_data = env-\u003esubprog_info[0].changes_pkt_data;\nkernel/bpf/cfg.c:761:\tenv-\u003eprog-\u003eaux-\u003emight_sleep = env-\u003esubprog_info[0].might_sleep;\nkernel/bpf/cfg.c-762-\n--\nkernel/bpf/cfg.c-771- * For each subprogram 'i' fill array env-\u003ecfg.insn_subprogram sub-range\nkernel/bpf/cfg.c:772: * [env-\u003esubprog_info[i].postorder_start, env-\u003esubprog_info[i+1].postorder_start)\nkernel/bpf/cfg.c-773- * with indices of 'i' instructions in postorder.\n--\nkernel/bpf/cfg.c=775=int bpf_compute_postorder(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-791-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/cfg.c:792:\t\tenv-\u003esubprog_info[i].postorder_start = cur_postorder;\nkernel/bpf/cfg.c:793:\t\tstack[0] = env-\u003esubprog_info[i].start;\nkernel/bpf/cfg.c-794-\t\tstack_sz = 1;\n--\nkernel/bpf/cfg.c-812-\t}\nkernel/bpf/cfg.c:813:\tenv-\u003esubprog_info[i].postorder_start = cur_postorder;\nkernel/bpf/cfg.c-814-\tenv-\u003ecfg.insn_postorder = postorder;\n--\nkernel/bpf/check_btf.c=10=static int check_abnormal_return(struct bpf_verifier_env *env)\n--\nkernel/bpf/check_btf.c-14-\tfor (i = 1; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/check_btf.c:15:\t\tif (env-\u003esubprog_info[i].has_ld_abs) {\nkernel/bpf/check_btf.c-16-\t\t\tverbose(env, \"LD_ABS is not allowed in subprogs without BTF\\n\");\n--\nkernel/bpf/check_btf.c-18-\t\t}\nkernel/bpf/check_btf.c:19:\t\tif (env-\u003esubprog_info[i].has_tail_call) {\nkernel/bpf/check_btf.c-20-\t\t\tverbose(env, \"tail_call is not allowed in subprogs without BTF\\n\");\n--\nkernel/bpf/check_btf.c=133=static int check_btf_func(struct bpf_verifier_env *env,\n--\nkernel/bpf/check_btf.c-174-\nkernel/bpf/check_btf.c:175:\t\tif (env-\u003esubprog_info[i].start != krecord[i].insn_off) {\nkernel/bpf/check_btf.c-176-\t\t\tverbose(env, \"func_info BTF section doesn't match subprog layout in BPF program\\n\");\n--\nkernel/bpf/check_btf.c-188-\t\t\tbtf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type);\nkernel/bpf/check_btf.c:189:\t\tif (i \u0026\u0026 !scalar_return \u0026\u0026 env-\u003esubprog_info[i].has_ld_abs) {\nkernel/bpf/check_btf.c-190-\t\t\tverbose(env, \"LD_ABS is only allowed in functions that return 'int'.\\n\");\n--\nkernel/bpf/check_btf.c-192-\t\t}\nkernel/bpf/check_btf.c:193:\t\tif (i \u0026\u0026 !scalar_return \u0026\u0026 env-\u003esubprog_info[i].has_tail_call) {\nkernel/bpf/check_btf.c-194-\t\t\tverbose(env, \"tail_call is only allowed in functions that return 'int'.\\n\");\n--\nkernel/bpf/check_btf.c-197-\nkernel/bpf/check_btf.c:198:\t\tenv-\u003esubprog_info[i].name = btf_name_by_offset(btf, type-\u003ename_off);\nkernel/bpf/check_btf.c-199-\t\tbpfptr_add(\u0026urecord, urec_size);\n--\nkernel/bpf/check_btf.c=213=static int check_btf_line(struct bpf_verifier_env *env,\n--\nkernel/bpf/check_btf.c-217-\tu32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;\nkernel/bpf/check_btf.c:218:\tstruct bpf_subprog_info *sub;\nkernel/bpf/check_btf.c-219-\tstruct bpf_line_info *linfo;\n--\nkernel/bpf/check_btf.c-248-\ts = 0;\nkernel/bpf/check_btf.c:249:\tsub = env-\u003esubprog_info;\nkernel/bpf/check_btf.c-250-\tulinfo = make_bpfptr(attr-\u003eline_info, uattr.is_kernel);\n--\nkernel/bpf/check_btf.c-279-\t\t * first sub also and the first sub must have\nkernel/bpf/check_btf.c:280:\t\t * subprog_info[0].start == 0.\nkernel/bpf/check_btf.c-281-\t\t */\n--\nkernel/bpf/const_fold.c=236=int bpf_compute_const_regs(struct bpf_verifier_env *env)\n--\nkernel/bpf/const_fold.c-254-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/const_fold.c:255:\t\tint start = env-\u003esubprog_info[i].start;\nkernel/bpf/const_fold.c-256-\n--\nkernel/bpf/core.c=1653=u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog)\nkernel/bpf/core.c-1654-{\nkernel/bpf/core.c:1655:\tconst struct bpf_subprog_info *sub;\nkernel/bpf/core.c-1656-\n--\nkernel/bpf/core.c-1658-\t\treturn 0;\nkernel/bpf/core.c:1659:\tsub = \u0026env-\u003esubprog_info[prog-\u003eaux-\u003efunc_idx];\nkernel/bpf/core.c-1660-\treturn sub-\u003estack_arg_cnt - bpf_in_stack_arg_cnt(sub);\n--\nkernel/bpf/diagnostics.c=797=static __printf(4, 5) void bpf_diag_source(struct bpf_verifier_env *env,\n--\nkernel/bpf/diagnostics.c-806-\tconst struct bpf_line_info *linfo;\nkernel/bpf/diagnostics.c:807:\tconst struct bpf_subprog_info *subprog;\nkernel/bpf/diagnostics.c-808-\tstruct btf *btf = env-\u003eprog-\u003eaux-\u003ebtf;\n--\nkernel/bpf/diagnostics.c-842-\tsubprog = bpf_find_containing_subprog(env, insn_idx);\nkernel/bpf/diagnostics.c:843:\tsubprogno = subprog ? subprog - env-\u003esubprog_info : -ENOENT;\nkernel/bpf/diagnostics.c-844-\tfunc = subprogno \u003e= 0 ? bpf_subprog_name(env, subprogno) : NULL;\n--\nkernel/bpf/diagnostics.c-861-\tlinfo_end = subprogno \u003e= 0 \u0026\u0026 subprogno + 1 \u003c env-\u003esubprog_cnt ?\nkernel/bpf/diagnostics.c:862:\t\t env-\u003esubprog_info[subprogno + 1].linfo_idx : env-\u003eprog-\u003eaux-\u003enr_linfo;\nkernel/bpf/diagnostics.c-863-\tfor (i = linfo_start; i \u003c linfo_end; i++) {\n--\nkernel/bpf/fixups.c=205=static int get_callee_stack_depth(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-212-\t\treturn -EFAULT;\nkernel/bpf/fixups.c:213:\treturn env-\u003esubprog_info[subprog].stack_depth;\nkernel/bpf/fixups.c-214-}\n--\nkernel/bpf/fixups.c=300=static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)\n--\nkernel/bpf/fixups.c-307-\tfor (i = 0; i \u003c= env-\u003esubprog_cnt; i++) {\nkernel/bpf/fixups.c:308:\t\tif (env-\u003esubprog_info[i].start \u003c= off)\nkernel/bpf/fixups.c-309-\t\t\tcontinue;\nkernel/bpf/fixups.c:310:\t\tenv-\u003esubprog_info[i].start += len - 1;\nkernel/bpf/fixups.c-311-\t}\n--\nkernel/bpf/fixups.c=483=static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-489-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++)\nkernel/bpf/fixups.c:490:\t\tif (env-\u003esubprog_info[i].start \u003e= off)\nkernel/bpf/fixups.c-491-\t\t\tbreak;\n--\nkernel/bpf/fixups.c-493-\tfor (j = i; j \u003c env-\u003esubprog_cnt; j++)\nkernel/bpf/fixups.c:494:\t\tif (env-\u003esubprog_info[j].start \u003e= off + cnt)\nkernel/bpf/fixups.c-495-\t\t\tbreak;\n--\nkernel/bpf/fixups.c-498-\t */\nkernel/bpf/fixups.c:499:\tif (env-\u003esubprog_info[j].start != off + cnt)\nkernel/bpf/fixups.c-500-\t\tj--;\n--\nkernel/bpf/fixups.c-507-\t\tfor (k = i; k \u003c j; k++)\nkernel/bpf/fixups.c:508:\t\t\tkvfree(env-\u003esubprog_info[k].jt);\nkernel/bpf/fixups.c-509-\n--\nkernel/bpf/fixups.c-512-\nkernel/bpf/fixups.c:513:\t\tmemmove(env-\u003esubprog_info + i,\nkernel/bpf/fixups.c:514:\t\t\tenv-\u003esubprog_info + j,\nkernel/bpf/fixups.c:515:\t\t\tsizeof(*env-\u003esubprog_info) * move);\nkernel/bpf/fixups.c-516-\t\t/* The vacated tail still holds copies of the moved entries. */\nkernel/bpf/fixups.c:517:\t\tmemset(env-\u003esubprog_info + i + move, 0,\nkernel/bpf/fixups.c:518:\t\t sizeof(*env-\u003esubprog_info) * (j - i));\nkernel/bpf/fixups.c-519-\t\tenv-\u003esubprog_cnt -= j - i;\n--\nkernel/bpf/fixups.c-541-\t\t/* convert i from \"first prog to remove\" to \"first to adjust\" */\nkernel/bpf/fixups.c:542:\t\tif (env-\u003esubprog_info[i].start == off)\nkernel/bpf/fixups.c-543-\t\t\ti++;\n--\nkernel/bpf/fixups.c-547-\tfor (; i \u003c= env-\u003esubprog_cnt; i++)\nkernel/bpf/fixups.c:548:\t\tenv-\u003esubprog_info[i].start -= cnt;\nkernel/bpf/fixups.c-549-\n--\nkernel/bpf/fixups.c=553=static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,\n--\nkernel/bpf/fixups.c-603-\tfor (i = 0; i \u003c= env-\u003esubprog_cnt; i++)\nkernel/bpf/fixups.c:604:\t\tif (env-\u003esubprog_info[i].linfo_idx \u003e l_off) {\nkernel/bpf/fixups.c-605-\t\t\t/* program may have started in the removed region but\n--\nkernel/bpf/fixups.c-607-\t\t\t */\nkernel/bpf/fixups.c:608:\t\t\tif (env-\u003esubprog_info[i].linfo_idx \u003e= l_off + l_cnt)\nkernel/bpf/fixups.c:609:\t\t\t\tenv-\u003esubprog_info[i].linfo_idx -= l_cnt;\nkernel/bpf/fixups.c-610-\t\t\telse\nkernel/bpf/fixups.c:611:\t\t\t\tenv-\u003esubprog_info[i].linfo_idx = l_off;\nkernel/bpf/fixups.c-612-\t\t}\n--\nkernel/bpf/fixups.c=879=int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\nkernel/bpf/fixups.c-880-{\nkernel/bpf/fixups.c:881:\tstruct bpf_subprog_info *subprogs = env-\u003esubprog_info;\nkernel/bpf/fixups.c-882-\tconst struct bpf_verifier_ops *ops = env-\u003eops;\n--\nkernel/bpf/fixups.c=1192=static u32 *bpf_dup_subprog_starts(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1198-\t\tfor (int i = 0; i \u003c env-\u003esubprog_cnt; i++)\nkernel/bpf/fixups.c:1199:\t\t\tstarts[i] = env-\u003esubprog_info[i].start;\nkernel/bpf/fixups.c-1200-\t}\n--\nkernel/bpf/fixups.c=1204=static void bpf_restore_subprog_starts(struct bpf_verifier_env *env, u32 *orig_starts)\n--\nkernel/bpf/fixups.c-1206-\tfor (int i = 0; i \u003c env-\u003esubprog_cnt; i++)\nkernel/bpf/fixups.c:1207:\t\tenv-\u003esubprog_info[i].start = orig_starts[i];\nkernel/bpf/fixups.c-1208-\t/* restore the start of fake 'exit' subprog as well */\nkernel/bpf/fixups.c:1209:\tenv-\u003esubprog_info[env-\u003esubprog_cnt].start = env-\u003eprog-\u003elen;\nkernel/bpf/fixups.c-1210-}\n--\nkernel/bpf/fixups.c=1265=static int jit_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1321-\t\tsubprog_start = subprog_end;\nkernel/bpf/fixups.c:1322:\t\tsubprog_end = env-\u003esubprog_info[i + 1].start;\nkernel/bpf/fixups.c-1323-\n--\nkernel/bpf/fixups.c-1361-\t\tfunc[i]-\u003eaux-\u003ename[0] = 'F';\nkernel/bpf/fixups.c:1362:\t\tfunc[i]-\u003eaux-\u003estack_depth = env-\u003esubprog_info[i].stack_depth;\nkernel/bpf/fixups.c:1363:\t\tif (env-\u003esubprog_info[i].priv_stack_mode == PRIV_STACK_ADAPTIVE)\nkernel/bpf/fixups.c-1364-\t\t\tfunc[i]-\u003eaux-\u003ejits_use_priv_stack = true;\n--\nkernel/bpf/fixups.c-1372-\t\tfunc[i]-\u003eaux-\u003ejited_linfo = prog-\u003eaux-\u003ejited_linfo;\nkernel/bpf/fixups.c:1373:\t\tfunc[i]-\u003eaux-\u003elinfo_idx = env-\u003esubprog_info[i].linfo_idx;\nkernel/bpf/fixups.c-1374-\t\tfunc[i]-\u003eaux-\u003earena = prog-\u003eaux-\u003earena;\n--\nkernel/bpf/fixups.c-1394-\t\tfunc[i]-\u003eaux-\u003enum_exentries = num_exentries;\nkernel/bpf/fixups.c:1395:\t\tfunc[i]-\u003eaux-\u003etail_call_reachable = env-\u003esubprog_info[i].tail_call_reachable;\nkernel/bpf/fixups.c:1396:\t\tfunc[i]-\u003eaux-\u003eexception_cb = env-\u003esubprog_info[i].is_exception_cb;\nkernel/bpf/fixups.c:1397:\t\tfunc[i]-\u003eaux-\u003echanges_pkt_data = env-\u003esubprog_info[i].changes_pkt_data;\nkernel/bpf/fixups.c:1398:\t\tfunc[i]-\u003eaux-\u003emight_sleep = env-\u003esubprog_info[i].might_sleep;\nkernel/bpf/fixups.c-1399-\t\tfunc[i]-\u003eaux-\u003etoken = prog-\u003eaux-\u003etoken;\n--\nkernel/bpf/fixups.c=1610=int bpf_fixup_call_args(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1619-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/fixups.c:1620:\t\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[i];\nkernel/bpf/fixups.c-1621-\t\tu16 outgoing = subprog-\u003estack_arg_cnt - bpf_in_stack_arg_cnt(subprog);\n--\nkernel/bpf/fixups.c-1644-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/fixups.c:1645:\t\tif (bpf_in_stack_arg_cnt(\u0026env-\u003esubprog_info[i])) {\nkernel/bpf/fixups.c-1646-\t\t\tverbose(env, \"stack args are not supported in non-JITed programs\\n\");\n--\nkernel/bpf/fixups.c=1684=static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *patch, int len)\nkernel/bpf/fixups.c-1685-{\nkernel/bpf/fixups.c:1686:\tstruct bpf_subprog_info *info = env-\u003esubprog_info;\nkernel/bpf/fixups.c-1687-\tint cnt = env-\u003esubprog_cnt;\n--\nkernel/bpf/fixups.c-1689-\nkernel/bpf/fixups.c:1690:\t/* We only reserve one slot for hidden subprogs in subprog_info. */\nkernel/bpf/fixups.c-1691-\tif (env-\u003ehidden_subprog_cnt) {\n--\nkernel/bpf/fixups.c=1739=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1752-\tint i, ret, cnt, delta = 0, cur_subprog = 0;\nkernel/bpf/fixups.c:1753:\tstruct bpf_subprog_info *subprogs = env-\u003esubprog_info;\nkernel/bpf/fixups.c-1754-\tu16 stack_depth = subprogs[cur_subprog].stack_depth;\n--\nkernel/bpf/fixups.c=2730=static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-2796-\t/* callback start is known only after patching */\nkernel/bpf/fixups.c:2797:\tcallback_start = env-\u003esubprog_info[callback_subprogno].start;\nkernel/bpf/fixups.c-2798-\t/* Note: insn_buf[12] is an offset of BPF_CALL_REL instruction */\n--\nkernel/bpf/fixups.c=2822=int bpf_optimize_bpf_loop(struct bpf_verifier_env *env)\nkernel/bpf/fixups.c-2823-{\nkernel/bpf/fixups.c:2824:\tstruct bpf_subprog_info *subprogs = env-\u003esubprog_info;\nkernel/bpf/fixups.c-2825-\tint i, cur_subprog = 0, cnt, delta = 0;\n--\nkernel/bpf/fixups.c-2861-\nkernel/bpf/fixups.c:2862:\tenv-\u003eprog-\u003eaux-\u003estack_depth = env-\u003esubprog_info[0].stack_depth;\nkernel/bpf/fixups.c-2863-\n--\nkernel/bpf/fixups.c=2870=int bpf_remove_fastcall_spills_fills(struct bpf_verifier_env *env)\nkernel/bpf/fixups.c-2871-{\nkernel/bpf/fixups.c:2872:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/fixups.c-2873-\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\n--\nkernel/bpf/liveness.c=53=struct func_instance {\n--\nkernel/bpf/liveness.c-57-\tu32 subprog;\t\t/* subprog index */\nkernel/bpf/liveness.c:58:\tu32 subprog_start;\t/* cached env-\u003esubprog_info[subprog].start */\nkernel/bpf/liveness.c-59-\tu32 insn_cnt;\t\t/* cached number of insns in the function */\n--\nkernel/bpf/liveness.c=103=static struct func_instance *call_instance(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-107-\tu32 depth = caller ? caller-\u003edepth + 1 : 0;\nkernel/bpf/liveness.c:108:\tu32 subprog_start = env-\u003esubprog_info[subprog].start;\nkernel/bpf/liveness.c-109-\tu32 lookup_key = depth \u003e 0 ? callsite : subprog_start;\n--\nkernel/bpf/liveness.c-123-\tf-\u003esubprog_start = subprog_start;\nkernel/bpf/liveness.c:124:\tf-\u003einsn_cnt = (env-\u003esubprog_info + subprog + 1)-\u003estart - subprog_start;\nkernel/bpf/liveness.c-125-\thash = instance_hash(lookup_key, depth);\n--\nkernel/bpf/liveness.c=130=static struct func_instance *lookup_instance(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-137-\nkernel/bpf/liveness.c:138:\tsubprog_start = env-\u003esubprog_info[st-\u003eframe[frameno]-\u003esubprogno].start;\nkernel/bpf/liveness.c-139-\tcallsite = frameno \u003e 0 ? st-\u003eframe[frameno]-\u003ecallsite : subprog_start;\n--\nkernel/bpf/liveness.c=435=static void update_instance(struct bpf_verifier_env *env, struct func_instance *instance)\n--\nkernel/bpf/liveness.c-438-\tint *insn_postorder = env-\u003ecfg.insn_postorder;\nkernel/bpf/liveness.c:439:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/liveness.c-440-\tbool changed;\n--\nkernel/bpf/liveness.c-442-\tinstance-\u003emust_write_initialized = true;\nkernel/bpf/liveness.c:443:\tsubprog = \u0026env-\u003esubprog_info[instance-\u003esubprog];\nkernel/bpf/liveness.c-444-\tpo_start = subprog-\u003epostorder_start;\n--\nkernel/bpf/liveness.c=555=static char *fmt_subprog(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/liveness.c-556-{\nkernel/bpf/liveness.c:557:\tconst char *name = env-\u003esubprog_info[subprog].name;\nkernel/bpf/liveness.c-558-\n--\nkernel/bpf/liveness.c=627=static void print_instance(struct bpf_verifier_env *env, struct func_instance *instance)\nkernel/bpf/liveness.c-628-{\nkernel/bpf/liveness.c:629:\tint start = env-\u003esubprog_info[instance-\u003esubprog].start;\nkernel/bpf/liveness.c-630-\tstruct bpf_insn *insns = env-\u003eprog-\u003einsnsi;\n--\nkernel/bpf/liveness.c=1730=static void print_subprog_arg_access(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-1735-\tstruct bpf_insn *insns = env-\u003eprog-\u003einsnsi;\nkernel/bpf/liveness.c:1736:\tint start = env-\u003esubprog_info[subprog].start;\nkernel/bpf/liveness.c-1737-\tint len = info-\u003elen;\n--\nkernel/bpf/liveness.c=1820=static int compute_subprog_args(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-1828-\tint depth = instance-\u003edepth;\nkernel/bpf/liveness.c:1829:\tint start = env-\u003esubprog_info[subprog].start;\nkernel/bpf/liveness.c:1830:\tint po_start = env-\u003esubprog_info[subprog].postorder_start;\nkernel/bpf/liveness.c:1831:\tint end = env-\u003esubprog_info[subprog + 1].start;\nkernel/bpf/liveness.c:1832:\tint po_end = env-\u003esubprog_info[subprog + 1].postorder_start;\nkernel/bpf/liveness.c-1833-\tint len = end - start;\n--\nkernel/bpf/liveness.c=2073=static int analyze_subprog(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-2081-\tstruct bpf_insn *insns = env-\u003eprog-\u003einsnsi;\nkernel/bpf/liveness.c:2082:\tint start = env-\u003esubprog_info[subprog].start;\nkernel/bpf/liveness.c:2083:\tint po_start = env-\u003esubprog_info[subprog].postorder_start;\nkernel/bpf/liveness.c:2084:\tint po_end = env-\u003esubprog_info[subprog + 1].postorder_start;\nkernel/bpf/liveness.c-2085-\tstruct func_instance *prev_instance = NULL;\n--\nkernel/bpf/liveness.c=2469=int bpf_compute_live_registers(struct bpf_verifier_env *env)\n--\nkernel/bpf/liveness.c-2504-\tfor (subprog = 0; subprog \u003c env-\u003esubprog_cnt; subprog++) {\nkernel/bpf/liveness.c:2505:\t\tstart = env-\u003esubprog_info[subprog].start;\nkernel/bpf/liveness.c:2506:\t\tend = env-\u003esubprog_info[subprog + 1].start;\nkernel/bpf/liveness.c-2507-\t\tret_reg_pair = bpf_ret_reg_pair(env, subprog);\n--\nkernel/bpf/verifier.c=437=static int bpf_compute_subprog_ret_regs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-460-\t\tif (ret_regs_cnt(size) \u003e 1) {\nkernel/bpf/verifier.c:461:\t\t\tsubprog_info(env, subprog)-\u003eret_reg_pair = true;\nkernel/bpf/verifier.c-462-\t\t\tenv-\u003eprog-\u003ejit_required = 1;\n--\nkernel/bpf/verifier.c=480=void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/verifier.c-481-{\nkernel/bpf/verifier.c:482:\tstruct bpf_subprog_info *info = subprog_info(env, subprog);\nkernel/bpf/verifier.c-483-\n--\nkernel/bpf/verifier.c=489=static bool subprog_is_exc_cb(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/verifier.c-490-{\nkernel/bpf/verifier.c:491:\treturn subprog_info(env, subprog)-\u003eis_exception_cb;\nkernel/bpf/verifier.c-492-}\n--\nkernel/bpf/verifier.c=1433=static int resize_reference_state(struct bpf_verifier_state *state, size_t n)\n--\nkernel/bpf/verifier.c-1444-/* Possibly update state-\u003eallocated_stack to be at least size bytes. Also\nkernel/bpf/verifier.c:1445: * possibly update the function's high-water mark in its bpf_subprog_info.\nkernel/bpf/verifier.c-1446- */\nkernel/bpf/verifier.c=1447=static int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)\n--\nkernel/bpf/verifier.c-1464-\t/* update known max for given subprogram */\nkernel/bpf/verifier.c:1465:\tif (env-\u003esubprog_info[state-\u003esubprogno].stack_depth \u003c size)\nkernel/bpf/verifier.c:1466:\t\tenv-\u003esubprog_info[state-\u003esubprogno].stack_depth = size;\nkernel/bpf/verifier.c-1467-\n--\nkernel/bpf/verifier.c=2418=static int cmp_subprogs(const void *a, const void *b)\nkernel/bpf/verifier.c-2419-{\nkernel/bpf/verifier.c:2420:\treturn ((struct bpf_subprog_info *)a)-\u003estart -\nkernel/bpf/verifier.c:2421:\t ((struct bpf_subprog_info *)b)-\u003estart;\nkernel/bpf/verifier.c-2422-}\n--\nkernel/bpf/verifier.c-2424-/* Find subprogram that contains instruction at 'off' */\nkernel/bpf/verifier.c:2425:struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *env, int off)\nkernel/bpf/verifier.c-2426-{\nkernel/bpf/verifier.c:2427:\tstruct bpf_subprog_info *vals = env-\u003esubprog_info;\nkernel/bpf/verifier.c-2428-\tint l, r, m;\n--\nkernel/bpf/verifier.c=2446=int bpf_find_subprog(struct bpf_verifier_env *env, int off)\nkernel/bpf/verifier.c-2447-{\nkernel/bpf/verifier.c:2448:\tstruct bpf_subprog_info *p;\nkernel/bpf/verifier.c-2449-\n--\nkernel/bpf/verifier.c-2452-\t\treturn -ENOENT;\nkernel/bpf/verifier.c:2453:\treturn p - env-\u003esubprog_info;\nkernel/bpf/verifier.c-2454-}\n--\nkernel/bpf/verifier.c=2456=static int add_subprog(struct bpf_verifier_env *env, int off)\n--\nkernel/bpf/verifier.c-2472-\t/* determine subprog starts. The end is one before the next starts */\nkernel/bpf/verifier.c:2473:\tenv-\u003esubprog_info[env-\u003esubprog_cnt].exit_idx = U32_MAX;\nkernel/bpf/verifier.c:2474:\tenv-\u003esubprog_info[env-\u003esubprog_cnt++].start = off;\nkernel/bpf/verifier.c:2475:\tsort(env-\u003esubprog_info, env-\u003esubprog_cnt,\nkernel/bpf/verifier.c:2476:\t sizeof(env-\u003esubprog_info[0]), cmp_subprogs, NULL);\nkernel/bpf/verifier.c-2477-\treturn env-\u003esubprog_cnt - 1;\n--\nkernel/bpf/verifier.c=2998=static int add_subprogs(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-2999-{\nkernel/bpf/verifier.c:3000:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3001-\tint i, ret, insn_cnt = env-\u003eprog-\u003elen, ex_cb_insn;\n--\nkernel/bpf/verifier.c-3063-\t\tfor (i = 1; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/verifier.c:3064:\t\t\tif (env-\u003esubprog_info[i].start != ex_cb_insn)\nkernel/bpf/verifier.c-3065-\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=3110=static int check_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3112-\tint i, subprog_start, subprog_end, off, cur_subprog = 0;\nkernel/bpf/verifier.c:3113:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3114-\tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n--\nkernel/bpf/verifier.c=3227=static int sort_subprogs_topo(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-3228-{\nkernel/bpf/verifier.c:3229:\tstruct bpf_subprog_info *si = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3230-\tint *insn_postorder = env-\u003ecfg.insn_postorder;\n--\nkernel/bpf/verifier.c=3616=static void check_fastcall_stack_contract(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3618-{\nkernel/bpf/verifier.c:3619:\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c-3620-\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\n--\nkernel/bpf/verifier.c=4342=static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4345-\tint max_stack_arg_regs = MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS;\nkernel/bpf/verifier.c:4346:\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c-4347-\tint spi = -off / BPF_REG_SIZE - 1;\n--\nkernel/bpf/verifier.c=4385=static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4387-{\nkernel/bpf/verifier.c:4388:\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c-4389-\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n--\nkernel/bpf/verifier.c=5484=static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,\n--\nkernel/bpf/verifier.c-5487-{\nkernel/bpf/verifier.c:5488:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-5489-\tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n--\nkernel/bpf/verifier.c=5734=static int check_max_stack_depth(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-5737-\tstruct bpf_subprog_call_depth_info *dinfo;\nkernel/bpf/verifier.c:5738:\tstruct bpf_subprog_info *si = env-\u003esubprog_info;\nkernel/bpf/verifier.c-5739-\tbool priv_stack_supported;\n--\nkernel/bpf/verifier.c=6149=void bpf_adjust_func_ptrs_after_remove(struct bpf_verifier_env *env, u32 off, u32 len)\n--\nkernel/bpf/verifier.c-6163-\t\tsubprog = bpf_find_subprog(env, p-\u003exlated_off);\nkernel/bpf/verifier.c:6164:\t\tif (subprog \u003e 0 \u0026\u0026 env-\u003esubprog_info[subprog + 1].start \u003e off + len)\nkernel/bpf/verifier.c-6165-\t\t\tp-\u003exlated_off = off;\n--\nkernel/bpf/verifier.c=10786=static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,\n--\nkernel/bpf/verifier.c-10789-{\nkernel/bpf/verifier.c:10790:\tstruct bpf_subprog_info *sub = subprog_info(env, subprog);\nkernel/bpf/verifier.c-10791-\tstruct bpf_func_state *caller = cur_func(env);\n--\nkernel/bpf/verifier.c=10964=static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-10980-\t */\nkernel/bpf/verifier.c:10981:\tenv-\u003esubprog_info[subprog].is_cb = true;\nkernel/bpf/verifier.c-10982-\tif (bpf_pseudo_kfunc_call(insn) \u0026\u0026\n--\nkernel/bpf/verifier.c-10997-\t\t/* there is no real recursion here. timer and workqueue callbacks are async */\nkernel/bpf/verifier.c:10998:\t\tenv-\u003esubprog_info[subprog].is_async_cb = true;\nkernel/bpf/verifier.c:10999:\t\tasync_cb = push_async_cb(env, env-\u003esubprog_info[subprog].start,\nkernel/bpf/verifier.c-11000-\t\t\t\t\t insn_idx, subprog,\n\n"
} |
| 43/3 | 2026/09/27 14:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47768 output: 35 thoughts: 43 Thoughts: Here's my summary, as if I were thinking it: **Diving into the `bpf_verifier_env` Structure** Okay, so I'm digging into this BPF verifier environment, specifically the `struct bpf_verifier_env`. The key here is to confirm how `subprog_info` is declared within that struct. Let me pull up that definition, and make sure my understanding of its role in the verifier is correct. I need to refresh my memory and pinpoint its exact structure to proceed. |
| 44/3 | 2026/09/27 14:22 | tool | grepper |
0mArgs:{
"Expression": "subprog_info\\[",
"PathPrefix": "include/linux/bpf_verifier.h"
}Results: {
"Output": "include/linux/bpf_verifier.h=935=struct bpf_verifier_env {\n--\ninclude/linux/bpf_verifier.h-978-\tstruct bpf_diag *diag;\ninclude/linux/bpf_verifier.h:979:\tstruct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 2]; /* max + 2 for the fake and exception subprogs */\ninclude/linux/bpf_verifier.h-980-\t/* subprog indices sorted in topological order: leaves first, callers last */\n--\ninclude/linux/bpf_verifier.h=1090=static inline struct bpf_subprog_info *subprog_info(struct bpf_verifier_env *env, int subprog)\ninclude/linux/bpf_verifier.h-1091-{\ninclude/linux/bpf_verifier.h:1092:\treturn \u0026env-\u003esubprog_info[subprog];\ninclude/linux/bpf_verifier.h-1093-}\n"
} |
| 45/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48222 output: 37 thoughts: 60 Thoughts: Here's my summary, as if I were thinking these thoughts: **Dissecting the BPF Verifier Environment** Okay, so I'm digging into this BPF verifier code and I need to understand how the `subprog_info` array fits into the picture. Right, it's part of the `struct bpf_verifier_env`, got it. Now, the next question immediately arises: How the heck is this `struct bpf_verifier_env` actually allocated and initialized? That's the crux of understanding its lifecycle and potential limitations. I need to trace this back. The logical place to start is where the verifier is actually *used*. Let's go to `kernel/bpf/verifier.c` and hunt down where `bpf_check` is called. That should give me the context I need to figure out the memory management around the environment. |
| 46/3 | 2026/09/27 14:22 | tool | grepper |
0mError:no matches Args: {
"Expression": "kzalloc\\(sizeof\\(\\*env\\)",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"error": "no matches"
} |
| 47/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48522 output: 38 thoughts: 10 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person: **BPF Verification: A Quick Audit** Alright, let's dive in. First, I'm thinking I should really get a solid grip on the `bpf_check` function. That's the gatekeeper, the fundamental layer that ensures everything's safe and sound. I need to understand exactly how it's validating the BPF program, what checks it's putting in place, and where the potential vulnerabilities might exist. I'm focusing on the heart of the verifier, looking for those key invariants. Is it covering all the bases? Are there any subtle edge cases it might be missing? I'm picturing the control flow graph building up in my mind, tracing potential execution paths through the program. I'm probably going to need to look at specific instruction sequences that tend to cause trouble. I want to immediately identify whether the program is safe and sound, given how that check is implemented. |
| 48/3 | 2026/09/27 14:22 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 22438,
"LineCount": 40
}Results: {
"Contents": "22438:\tint bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n22439:\t\t struct bpf_log_attr *attr_log)\n22440:\t{\n22441:\t\tu64 start_time = ktime_get_ns();\n22442:\t\tstruct bpf_verifier_env *env;\n22443:\t\tint i, len, ret = -EINVAL, err;\n22444:\t\tbool is_priv;\n22445:\t\n22446:\t\tBTF_TYPE_EMIT(enum bpf_features);\n22447:\t\n22448:\t\t/* no program is valid */\n22449:\t\tif (ARRAY_SIZE(bpf_verifier_ops) == 0)\n22450:\t\t\treturn -EINVAL;\n22451:\t\n22452:\t\t/* 'struct bpf_verifier_env' can be global, but since it's not small,\n22453:\t\t * allocate/free it every time bpf_check() is called\n22454:\t\t */\n22455:\t\tenv = kvzalloc_obj(struct bpf_verifier_env, GFP_KERNEL_ACCOUNT);\n22456:\t\tif (!env)\n22457:\t\t\treturn -ENOMEM;\n22458:\t\n22459:\t\tenv-\u003ebt.env = env;\n22460:\t\tenv-\u003eprog = *prog;\n22461:\t\tenv-\u003eops = bpf_verifier_ops[env-\u003eprog-\u003etype];\n22462:\t\tenv-\u003estack_limit = bpf_prog_stack_limit(env-\u003eprog);\n22463:\t\tif (bpf_prog_reenters_datapath(env-\u003eprog))\n22464:\t\t\tenv-\u003estack_limit = MAX_BPF_STACK;\n22465:\t\n22466:\t\tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\n22467:\t\tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\n22468:\t\tenv-\u003ebypass_spec_v1 = bpf_bypass_spec_v1(env-\u003eprog-\u003eaux-\u003etoken);\n22469:\t\tenv-\u003ebypass_spec_v4 = bpf_bypass_spec_v4(env-\u003eprog-\u003eaux-\u003etoken);\n22470:\t\tenv-\u003ebpf_capable = is_priv = bpf_token_capable(env-\u003eprog-\u003eaux-\u003etoken, CAP_BPF);\n22471:\t\tenv-\u003esignature = attr-\u003esignature;\n22472:\t\n22473:\t\t/* user could have requested verbose verifier output\n22474:\t\t * and supplied buffer to store the verification trace\n22475:\t\t */\n22476:\t\tret = bpf_vlog_init(\u0026env-\u003elog, attr_log-\u003elevel, attr_log-\u003eubuf, attr_log-\u003esize);\n22477:\t\tif (ret)\n"
} |
| 49/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49602 output: 39 thoughts: 1146 Thoughts: **Analyzing BPF Verifier Subprogram Jump Table Management** Okay, let's break this down. My initial observation is that the `env` structure, crucial for the BPF verifier, is allocated using `kvzalloc_obj`. This guarantees zero-initialization of the `struct bpf_verifier_env` right from the start. That's a good foundation. Then, I'm noting the mechanics of adding and removing subprograms. `add_subprog` sets `exit_idx` to `U32_MAX` and updates `start`. The removal process, in `adjust_subprog_starts_after_remove`, is particularly interesting. It explicitly `kvfree`s the jump tables (`jt`) of removed subprograms *and* uses `memset` to zero out the vacated tail of the `subprog_info` array. This is critical for preventing stale data leaks. It's good defensive programming. Next, I'm tracing `bpf_free_subprog_jts`. The code iterates through the entire `env->subprog_info` array, `kvfree`ing each `jt` and nulling it out, finally setting `cfg.subprog_jts_ready` to false. This reinforces the clean-up approach. It is called from both `compute_subprog_jts` if there's an error, and on `bpf_check` exit (at `err_free_env`). Now, let's step back and consider the *purpose* of this patch, the "bug/feature." It's centered around indirect jumps (`gotox`) and jump tables (`bpf_insn_array`). Previously, jump tables were stored per instruction (`insn_aux_data[t].jt`). This patch refactors to let all `gotox` instructions within a subprogram share the *same* jump table (`subprog->jt`). I'm seeing several key changes related to this refactoring: limiting the complexity with `BPF_MAX_GOTOX_EDGES`, and the `jitted_off` and `ips[i]` reset in `bpf_insn_array_init`. It looks like there's cleanup in `adjust_subprog_starts_after_remove` of `exception_callback_subprog` and freeing of the old `subprog_info[k].jt`. The successor tracking in `bpf_insn_successors` for `gotox` (using `subprog->jt`) and the use of `subprog->exit_idx` for abnormal return instructions are important. There's also some work in `bpf_compute_scc` using `dfs_pos` to resume traversal. Finally, `adjust_insn_aux_data` and `__bpf_patch_insn_data` got `mode` (`BPF_PATCH_KEEP_TARGET` vs `BPF_PATCH_MOVE_TARGET`). Now, I need to focus on security. A major concern in kernel code is leaking potentially uninitialized data to userspace. So, my main question is, does this patch involve *any* kernel structures that are copied to userspace, whether via `copy_to_user`, `put_user`, netlink, ioctls, or socket options? No. The structures involved, `bpf_subprog_info`, `bpf_iarray`, `dfs_pos`, and so on, seem to be purely internal to the verifier itself. They are not exposed directly to userspace. Wait! What about `bpf_insn_array`? Let's double-check the code in `bpf_insn_array.c`. |
| 50/3 | 2026/09/27 14:22 | tool | grepper |
0mArgs:{
"Expression": "map_lookup_elem",
"PathPrefix": "kernel/bpf/bpf_insn_array.c"
}Results: {
"Output": "kernel/bpf/bpf_insn_array.c=136=const struct bpf_map_ops insn_array_map_ops = {\n--\nkernel/bpf/bpf_insn_array.c-140-\t.map_get_next_key = bpf_array_get_next_key,\nkernel/bpf/bpf_insn_array.c:141:\t.map_lookup_elem = insn_array_lookup_elem,\nkernel/bpf/bpf_insn_array.c-142-\t.map_update_elem = insn_array_update_elem,\n"
} |
| 51/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51747 output: 42 thoughts: 31 Thoughts: Okay, here's the summary, framed as your thoughts: **Analyzing `insn_array_lookup_elem` in `kernel/bpf/bpf_insn_array.c`** Alright, let's dive into the `insn_array_lookup_elem` function within `kernel/bpf/bpf_insn_array.c`. I need to get a clear picture of how this lookup is implemented. I expect a careful handling of bounds checking and potential performance considerations, given this is core BPF infrastructure. I'm going to want to understand the key data structures involved, specifically how the instruction array itself is organized. Are we talking about a simple array, or something more sophisticated like a tree or hash table, particularly given potential for large instruction counts? I'll be looking for any potential optimization strategies – are there any specific access patterns that they exploit? The goal is to deeply grok how an instruction is actually retrieved when the BPF program needs to execute it. Any specific handling of different BPF instruction types will be of interest. Also, I need to make sure I understand any potential security implications that this function might have. My attention will be laser-focused on potential vulnerabilities, such as integer overflows, that could allow malicious code to bypass security constraints. |
| 52/3 | 2026/09/27 14:22 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/bpf_insn_array.c",
"FirstLine": 100,
"LineCount": 40
}Results: {
"Contents": " 100:\t\n 101:\tstatic int insn_array_check_btf(struct bpf_map *map,\n 102:\t\t\t\t const struct btf *btf,\n 103:\t\t\t\t const struct btf_type *key_type,\n 104:\t\t\t\t const struct btf_type *value_type)\n 105:\t{\n 106:\t\tif (!btf_type_is_i32(key_type))\n 107:\t\t\treturn -EINVAL;\n 108:\t\n 109:\t\tif (!btf_type_is_i64(value_type))\n 110:\t\t\treturn -EINVAL;\n 111:\t\n 112:\t\treturn 0;\n 113:\t}\n 114:\t\n 115:\tstatic u64 insn_array_mem_usage(const struct bpf_map *map)\n 116:\t{\n 117:\t\treturn insn_array_alloc_size(map-\u003emax_entries);\n 118:\t}\n 119:\t\n 120:\tstatic int insn_array_map_direct_value_addr(const struct bpf_map *map, u64 *imm, u32 off)\n 121:\t{\n 122:\t\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\n 123:\t\n 124:\t\tif ((off % sizeof(long)) != 0 ||\n 125:\t\t (off / sizeof(long)) \u003e= map-\u003emax_entries)\n 126:\t\t\treturn -EACCES;\n 127:\t\n 128:\t\t/* from BPF's point of view, this map is a jump table */\n 129:\t\t*imm = (unsigned long)insn_array-\u003eips;\n 130:\t\n 131:\t\treturn 0;\n 132:\t}\n 133:\t\n 134:\tBTF_ID_LIST_SINGLE(insn_array_btf_ids, struct, bpf_insn_array)\n 135:\t\n 136:\tconst struct bpf_map_ops insn_array_map_ops = {\n 137:\t\t.map_alloc_check = insn_array_alloc_check,\n 138:\t\t.map_alloc = insn_array_alloc,\n 139:\t\t.map_free = insn_array_free,\n"
} |
| 53/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52741 output: 41 thoughts: 29 Thoughts: Okay, let's dive into this. I'm going to focus specifically on lines 60-100 of `kernel/bpf/bpf_insn_array.c`. **Examining the BPF Instruction Array Code** Right, so I'm pulling up that section of the BPF code. I need to refresh my memory on the specifics of this area. Given the file name, `bpf_insn_array.c`, I'm expecting this section to deal with the array-like structures used to store BPF instructions within the kernel. I'm going to look for operations on this array - allocating, deallocating, accessing elements, inserting, removing... that sort of thing. I want to see how the kernel manages these instruction arrays from a structural point of view. I'm also curious to see how the kernel handles potential issues. Specifically, I want to see if the kernel handles edge cases well - maybe there's some sort of out-of-bounds error handling? Also, I want to check for potential race conditions. I want to make sure the code is safe. I'm really looking for how well the code handles the instruction arrays as data structures. Does the code account for memory constraints, or unexpected instruction counts? Is there any kind of validation being done? I'm particularly interested in any synchronization primitives used – mutexes, spinlocks, atomics. I need to understand how the code protects concurrent access to these instruction arrays, especially if they are shared or modified by multiple BPF programs or threads. I also hope to observe if there's any clever optimization or algorithmic tricks being used to make the BPF execution more efficient, especially in cases where the array might be large or frequently accessed. |
| 54/3 | 2026/09/27 14:22 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/bpf_insn_array.c",
"FirstLine": 60,
"LineCount": 40
}Results: {
"Contents": " 60:\t\n 61:\t\treturn \u0026insn_array-\u003emap;\n 62:\t}\n 63:\t\n 64:\tstatic void *insn_array_lookup_elem(struct bpf_map *map, void *key)\n 65:\t{\n 66:\t\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\n 67:\t\tu32 index = *(u32 *)key;\n 68:\t\n 69:\t\tif (unlikely(index \u003e= insn_array-\u003emap.max_entries))\n 70:\t\t\treturn NULL;\n 71:\t\n 72:\t\treturn \u0026insn_array-\u003evalues[index];\n 73:\t}\n 74:\t\n 75:\tstatic long insn_array_update_elem(struct bpf_map *map, void *key, void *value, u64 map_flags)\n 76:\t{\n 77:\t\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\n 78:\t\tu32 index = *(u32 *)key;\n 79:\t\tstruct bpf_insn_array_value val = {};\n 80:\t\n 81:\t\tif (unlikely(index \u003e= insn_array-\u003emap.max_entries))\n 82:\t\t\treturn -E2BIG;\n 83:\t\n 84:\t\tif (unlikely(map_flags \u0026 BPF_NOEXIST))\n 85:\t\t\treturn -EEXIST;\n 86:\t\n 87:\t\tcopy_map_value(map, \u0026val, value);\n 88:\t\tif (val.jitted_off || val.xlated_off)\n 89:\t\t\treturn -EINVAL;\n 90:\t\n 91:\t\tinsn_array-\u003evalues[index].orig_off = val.orig_off;\n 92:\t\n 93:\t\treturn 0;\n 94:\t}\n 95:\t\n 96:\tstatic long insn_array_delete_elem(struct bpf_map *map, void *key)\n 97:\t{\n 98:\t\treturn -EINVAL;\n 99:\t}\n"
} |
| 55/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53749 output: 41 thoughts: 409 Thoughts: **Analyzing the Changes in `bpf_insn_array.c`** Okay, so I'm looking at `bpf_insn_array.c` and specifically focusing on what's happening with the `values` array, which, as I recall, is of type `struct bpf_insn_array_value`. I need to understand the change introduced in the `bpf_insn_array_init` function. The code's comment mentions a reset of map indexes to a "pre-verification state". That makes sense. The verifier and JIT recompute offsets and target pointers, so any leftover values from a prior usage must be cleared. Previously, the code only reset `xlated_off` to `orig_off`. Now, however, the loop includes two additional crucial initializations. I'm seeing that `jitted_off` is explicitly initialized to `0`, and critically, `insn_array->ips[i]` is also being set to `0`. This is a significant detail – it means these fields are now *explicitly cleared* during initialization, which is a good safety precaution. I also need to verify what else is happening within this file. The next step is to examine the `insn_array_alloc` function. I will need to understand how the map is allocated, to ensure there are no other points of concern. |
| 56/3 | 2026/09/27 14:22 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/bpf_insn_array.c",
"FirstLine": 35,
"LineCount": 30
}Results: {
"Contents": " 35:\t}\n 36:\t\n 37:\tstatic void insn_array_free(struct bpf_map *map)\n 38:\t{\n 39:\t\tstruct bpf_insn_array *insn_array = cast_insn_array(map);\n 40:\t\n 41:\t\tbpf_map_area_free(insn_array);\n 42:\t}\n 43:\t\n 44:\tstatic struct bpf_map *insn_array_alloc(union bpf_attr *attr)\n 45:\t{\n 46:\t\tu64 size = insn_array_alloc_size(attr-\u003emax_entries);\n 47:\t\tstruct bpf_insn_array *insn_array;\n 48:\t\n 49:\t\tinsn_array = bpf_map_area_alloc(size, NUMA_NO_NODE);\n 50:\t\tif (!insn_array)\n 51:\t\t\treturn ERR_PTR(-ENOMEM);\n 52:\t\n 53:\t\t/* ips are allocated right after the insn_array-\u003evalues[] array */\n 54:\t\tinsn_array-\u003eips = (void *)\u0026insn_array-\u003evalues[attr-\u003emax_entries];\n 55:\t\n 56:\t\tbpf_map_init_from_attr(\u0026insn_array-\u003emap, attr);\n 57:\t\n 58:\t\t/* BPF programs aren't allowed to write to the map */\n 59:\t\tinsn_array-\u003emap.map_flags |= BPF_F_RDONLY_PROG;\n 60:\t\n 61:\t\treturn \u0026insn_array-\u003emap;\n 62:\t}\n 63:\t\n 64:\tstatic void *insn_array_lookup_elem(struct bpf_map *map, void *key)\n"
} |
| 57/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 54969 output: 31 thoughts: 737 Thoughts: **My Analysis of Potential Uninitialized Memory in BPF Code** Okay, so I've been looking at this patch and tracing the implications of `bpf_map_area_alloc` and its use of zeroed memory allocation. Now I need to do a thorough check for any uninitialized memory usages that might have crept in. I'm going through the affected files and functions methodically. First, `kernel/bpf/bpf_insn_array.c` looks straightforward. The changes to `atomic_set_release` and the boundary condition in `bpf_insn_array_adjust` don't seem to introduce any uninitialized memory concerns. Then, moving on to `kernel/bpf/cfg.c`, `BPF_MAX_GOTOX_EDGES` and the new bounds checking is a positive addition. `compute_subprog_jts` handles jump tables within subprograms, allocating the `jt` and making sure those jump table targets stay within boundaries seems safe. I'll need to double check how the data is handled when freed, in `bpf_free_subprog_jts`, to make sure everything's properly initialized before being freed. `bpf_compute_scc` is interesting because it uses `kvcalloc` for `dfs_pos`, which is good because that guarantees zero initialization. `kernel/bpf/fixups.c` introduces `BPF_PATCH_KEEP_TARGET` and `BPF_PATCH_MOVE_TARGET` with the enum and the `indirect_target` and target offsets. `adjust_subprog_starts_after_remove` seems fairly intricate, freeing `jt`, and uses `memmove` and `memset`. I'll want to carefully examine the `memmove` and `memset` calls, especially regarding potential overlaps and what data is being zeroed to ensure there are no issues. `bpf_opt_remove_nops` seems unlikely to introduce an issue, as it's skipping nops that are indirect jump targets. In `kernel/bpf/liveness.c`, `bpf_insn_successors` provides `subprog->jt` for gotox and adds `subprog->exit_idx` which could be an area to check for uninitialized memory usage if not set to `U32_MAX` for a normal exit. Finally, in `kernel/bpf/verifier.c`, `add_subprog` initializes `exit_idx = U32_MAX`, and `check_indirect_jump` is simply validation, which doesn't seem to be an issue. The `bpf_check` freeing of jump tables through `bpf_free_subprog_jts(env)` seems fine. The important areas to investigate for uninitialized memory usage are: * The initialization of `dfs_pos` in `bpf_compute_scc`. I've already confirmed it is allocated with `kvcalloc`, so it should be all zeros, which is good. * The initialization and use of `exit_idx`. I know `add_subprog` initializes it to `U32_MAX` and `add_hidden_subprog` initializes it correctly. But I'll need to check the logic in `check_subprogs`, to see if subprogram zero gets initialized or that `exit_idx` is guaranteed to be initialized in all scenarios. I need to confirm the allocation and proper zeroing or initialization everywhere, and then I need to check how the jump tables are freed to make sure there are no leaks of uninitialized data. |
| 58/3 | 2026/09/27 14:22 | tool | grepper |
0mArgs:{
"Expression": "exit_idx",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=2456=static int add_subprog(struct bpf_verifier_env *env, int off)\n--\nkernel/bpf/verifier.c-2472-\t/* determine subprog starts. The end is one before the next starts */\nkernel/bpf/verifier.c:2473:\tenv-\u003esubprog_info[env-\u003esubprog_cnt].exit_idx = U32_MAX;\nkernel/bpf/verifier.c-2474-\tenv-\u003esubprog_info[env-\u003esubprog_cnt++].start = off;\n--\nkernel/bpf/verifier.c=3110=static int check_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3138-\t\tif (BPF_OP(code) == BPF_EXIT) {\nkernel/bpf/verifier.c:3139:\t\t\tsubprog[cur_subprog].exit_idx = i;\nkernel/bpf/verifier.c-3140-\t\t\tgoto next;\n"
} |
| 59/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 56786 output: 33 thoughts: 38 Thoughts: Here's my summary, as if I were thinking these thoughts: **Subprogram 0 Initialization: A Deep Dive** Okay, time to get to the bottom of this. I need to understand how subprogram 0, the very first one, gets initialized. Specifically, I'm curious about how `subprog_info[0]` gets populated. Where's the source code that handles that? The obvious place to start is the BPF verifier. Let's head over to `kernel/bpf/verifier.c`. I expect to find the crucial logic there, buried amidst the details of BPF program verification. I need to trace the execution path and identify how this initial `subprog_info` entry is created and populated. This is a fundamental building block, and understanding its initialization is critical. |
| 60/3 | 2026/09/27 14:22 | tool | grepper |
0mArgs:{
"Expression": "add_subprog\\(",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=2446=int bpf_find_subprog(struct bpf_verifier_env *env, int off)\n--\nkernel/bpf/verifier.c-2455-\nkernel/bpf/verifier.c:2456:static int add_subprog(struct bpf_verifier_env *env, int off)\nkernel/bpf/verifier.c-2457-{\n--\nkernel/bpf/verifier.c=2998=static int add_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3005-\t/* Add entry function. */\nkernel/bpf/verifier.c:3006:\tret = add_subprog(env, 0);\nkernel/bpf/verifier.c-3007-\tif (ret)\n--\nkernel/bpf/verifier.c-3029-\nkernel/bpf/verifier.c:3030:\t\tret = add_subprog(env, i + insn-\u003eimm + 1);\nkernel/bpf/verifier.c-3031-\t\tif (ret \u003c 0)\n--\nkernel/bpf/verifier.c-3044-\t\tfor (i = 1; i \u003c aux-\u003efunc_info_cnt; i++) {\nkernel/bpf/verifier.c:3045:\t\t\tret = add_subprog(env, aux-\u003efunc_info[i].insn_off);\nkernel/bpf/verifier.c-3046-\t\t\tif (ret \u003c 0)\n--\nkernel/bpf/verifier.c-3059-\tif (ex_cb_insn) {\nkernel/bpf/verifier.c:3060:\t\tret = add_subprog(env, ex_cb_insn);\nkernel/bpf/verifier.c-3061-\t\tif (ret \u003c 0)\n"
} |
| 61/3 | 2026/09/27 14:22 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57508 output: 37 thoughts: 137 Thoughts: Okay, here's my breakdown of what's happening, from my expert perspective: **Deep Dive into BPF Subprogram Initialization** Alright, so `add_subprog(env, 0)` is the first step, and it's key. I see it's adding `subprog 0` to the environment (`env`). The critical detail here is that `exit_idx` is set to `U32_MAX`, which essentially flags it as not yet having a defined exit point. The environment itself, `env`, is allocated using `kvzalloc_obj`, which means kernel memory allocation with accounting, which is standard procedure. So far, so good – we're initializing our BPF verifier environment for the first subprogram, with a placeholder exit. Now, I need to really dig in and consider potential issues that I'm used to seeing: Specifically, I need to make sure the `subprog_info`, the jump table (`jt`), and any data within `bpf_patch_insn_data` and the related `adjust_insn_aux_data` aren't introducing subtle bugs. I've seen uninitialized memory problems and off-by-one errors creep in during these phases. Let's start with `adjust_insn_aux_data` and take a look at the code within `kernel/bpf/fixups.c`. That's where the magic happens and things can get tricky. I need to be sure any auxiliary data is properly initialized or handled to avoid any surprises down the line. |
| 62/3 | 2026/09/27 14:23 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/fixups.c",
"FirstLine": 220,
"LineCount": 75
}Results: {
"Contents": " 220:\t};\n 221:\t\n 222:\t/* single env-\u003eprog-\u003einsni[off] instruction was replaced with the range\n 223:\t * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying\n 224:\t * [0, off) and [off, end) to new locations, so the patched range stays zero\n 225:\t */\n 226:\tstatic void adjust_insn_aux_data(struct bpf_verifier_env *env,\n 227:\t\t\t\t\t struct bpf_prog *new_prog, u32 off, u32 cnt,\n 228:\t\t\t\t\t struct bpf_insn *original_insn,\n 229:\t\t\t\t\t enum bpf_patch_mode mode)\n 230:\t{\n 231:\t\tstruct bpf_insn_aux_data *data = env-\u003einsn_aux_data;\n 232:\t\tstruct bpf_insn *insn = new_prog-\u003einsnsi;\n 233:\t\tu32 old_seen = data[off].seen;\n 234:\t\tu32 prog_len;\n 235:\t\tint i;\n 236:\t\n 237:\t\t/* aux info at OFF always needs adjustment, no matter fast path\n 238:\t\t * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the\n 239:\t\t * original insn at old prog.\n 240:\t\t */\n 241:\t\tdata[off].zext_dst = bpf_insn_def32(new_prog, insn + off + cnt - 1) \u003e= 0;\n 242:\t\n 243:\t\tif (cnt == 1) {\n 244:\t\t\t/*\n 245:\t\t\t * A non-memory accessing insn could have been replaced by a\n 246:\t\t\t * memory accessing insn, systematically mark it for non-stack\n 247:\t\t\t * access\n 248:\t\t\t */\n 249:\t\t\tdata[off].non_stack_access = bpf_is_mem_insn(insn + off);\n 250:\t\t\treturn;\n 251:\t\t}\n 252:\t\tprog_len = new_prog-\u003elen;\n 253:\t\tenv-\u003einsn_aux_data_len = prog_len;\n 254:\t\n 255:\t\tmemmove(data + off + cnt - 1, data + off,\n 256:\t\t\tsizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));\n 257:\t\tmemset(data + off, 0, sizeof(struct bpf_insn_aux_data) * (cnt - 1));\n 258:\t\tfor (i = off; i \u003c off + cnt - 1; i++) {\n 259:\t\t\t/* Expand insni[off]'s seen count to the patched range. */\n 260:\t\t\tdata[i].seen = old_seen;\n 261:\t\t\tdata[i].zext_dst = bpf_insn_def32(new_prog, insn + i) \u003e= 0;\n 262:\t\t\t/*\n 263:\t\t\t * In BPF_PATCH_MOVE_TARGET mode the original insn is the last\n 264:\t\t\t * slot by construction.\n 265:\t\t\t */\n 266:\t\t\tif (mode == BPF_PATCH_KEEP_TARGET \u0026\u0026\n 267:\t\t\t !memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {\n 268:\t\t\t\tdata[i].non_stack_access =\n 269:\t\t\t\t\tdata[off + cnt - 1].non_stack_access;\n 270:\t\t\t\tdata[off + cnt - 1].non_stack_access = false;\n 271:\t\t\t} else if (bpf_is_mem_insn(insn + i)) {\n 272:\t\t\t\tdata[i].non_stack_access = true;\n 273:\t\t\t}\n 274:\t\t}\n 275:\t\n 276:\t\t/*\n 277:\t\t * Last slot instruction could be a newly generated\n 278:\t\t * BPF_ST/BPF_LDX/BPF_STX, systematically mark it for non-stack access\n 279:\t\t * if it is not the original instruction, otherwise keep the\n 280:\t\t * original marking\n 281:\t\t */\n 282:\t\tif (bpf_is_mem_insn(insn + off + cnt - 1) \u0026\u0026\n 283:\t\t memcmp(insn + off + cnt - 1, original_insn, sizeof(struct bpf_insn)))\n 284:\t\t\tdata[off + cnt - 1].non_stack_access = true;\n 285:\t\n 286:\t\t/*\n 287:\t\t * The indirect_target flag of the original instruction was moved to the last of the\n 288:\t\t * new instructions by the above memmove and memset, but the indirect jump target is\n 289:\t\t * actually the first instruction, so move it back. This also matches with the behavior\n 290:\t\t * of bpf_insn_array_adjust(), which preserves xlated_off to point to the first new\n 291:\t\t * instruction. For BPF_PATCH_MOVE_TARGET the original instruction is the last one,\n 292:\t\t * so the flag already sits where needed.\n 293:\t\t */\n 294:\t\tif (mode == BPF_PATCH_KEEP_TARGET \u0026\u0026 data[off + cnt - 1].indirect_target) {\n"
} |
| 63/3 | 2026/09/27 14:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 59451 output: 37 thoughts: 84 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into Instruction Data Zeroing** Okay, so I'm looking at `adjust_insn_aux_data` and immediately spot something that piques my interest: `memset(data + off, 0, sizeof(struct bpf_insn_aux_data) * (cnt - 1));`. It looks like the added auxiliary data entries are being zeroed out using `memset`. Interesting. This is a common pattern, but let's see *why* this is happening in this specific context. Now, my next step is to understand *how* these auxiliary entries are patched, and what are those entries used for. I'll need to dig into `__bpf_patch_insn_data` in `kernel/bpf/fixups.c`. That's where I need to focus my attention to piece this all together. Let's get to it. |
| 64/3 | 2026/09/27 14:23 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/fixups.c",
"FirstLine": 355,
"LineCount": 45
}Results: {
"Contents": " 355:\t\t\treturn true;\n 356:\t\tcond_resched();\n 357:\t\treturn false;\n 358:\t}\n 359:\t\n 360:\tstatic struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n 361:\t\t\t\t\t\t const struct bpf_insn *patch, u32 len,\n 362:\t\t\t\t\t\t enum bpf_patch_mode mode)\n 363:\t{\n 364:\t\tstruct bpf_prog *new_prog;\n 365:\t\tstruct bpf_insn_aux_data *new_data = NULL;\n 366:\t\t/* The first slot whose targets move. */\n 367:\t\tu32 first = mode == BPF_PATCH_MOVE_TARGET ? off : off + 1;\n 368:\t\tstruct bpf_insn original_insn;\n 369:\t\n 370:\t\tif (bpf_rewrite_must_abort())\n 371:\t\t\treturn NULL;\n 372:\t\n 373:\t\tif (len \u003e 1) {\n 374:\t\t\tnew_data = vrealloc(env-\u003einsn_aux_data,\n 375:\t\t\t\t\t array_size(env-\u003eprog-\u003elen + len - 1,\n 376:\t\t\t\t\t\t sizeof(struct bpf_insn_aux_data)),\n 377:\t\t\t\t\t GFP_KERNEL_ACCOUNT | __GFP_ZERO);\n 378:\t\t\tif (!new_data)\n 379:\t\t\t\treturn NULL;\n 380:\t\n 381:\t\t\tenv-\u003einsn_aux_data = new_data;\n 382:\t\t}\n 383:\t\n 384:\t\tmemcpy(\u0026original_insn, env-\u003eprog-\u003einsnsi + off, sizeof(struct bpf_insn));\n 385:\t\tnew_prog = bpf_patch_insn_single(env-\u003eprog, off, patch, len);\n 386:\t\tif (IS_ERR(new_prog)) {\n 387:\t\t\tif (PTR_ERR(new_prog) == -ERANGE)\n 388:\t\t\t\tverbose(env,\n 389:\t\t\t\t\t\"insn %d cannot be patched due to 16-bit range\\n\",\n 390:\t\t\t\t\tenv-\u003einsn_aux_data[off].orig_idx);\n 391:\t\t\treturn NULL;\n 392:\t\t}\n 393:\t\tadjust_insn_aux_data(env, new_prog, off, len, \u0026original_insn, mode);\n 394:\t\tadjust_subprog_starts(env, off, len);\n 395:\t\tadjust_insn_arrays(env, first, len);\n 396:\t\tbpf_adjust_func_ptrs(env, off, len);\n 397:\t\tadjust_poke_descs(new_prog, first, len);\n 398:\t\treturn new_prog;\n 399:\t}\n"
} |
| 65/3 | 2026/09/27 14:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 60560 output: 451 thoughts: 1338 Thoughts: **KMSAN Evaluation: A Detailed Assessment**
Okay, let's break this down. We have this `vrealloc` call and a `memcpy`, so the immediate concern is memory initialization. But, we're using `GFP_KERNEL_ACCOUNT | __GFP_ZERO`, so any newly allocated memory is zeroed. The `original_insn` is copied directly, so there's no risk of reading uninitialized data there either.
Now, we need to carefully examine each field and allocation to see if there are any subtle uninitialized variable issues or memory leaks.
* `cfg.gotox_edges`: This is part of the `struct bpf_verifier_env`, which is allocated with `kvzalloc_obj`. Therefore, it’s initialized to zero.
* `cfg.subprog_jts_ready`: A boolean. Since `env` is zero-allocated, it starts as `false`. `bpf_free_subprog_jts` explicitly sets it to `false`, and `__compute_subprog_jts` sets it to `true`, so this seems safe.
* `subprog_info[i].jt`: Initially `NULL` (due to zero-allocation). It's allocated and filled by `__compute_subprog_jts`, freed and set to `NULL` by `bpf_free_subprog_jts`, and freed and vacated entries are zeroed with `memset` in `adjust_subprog_starts_after_remove`. This seems robust.
* `subprog_info[i].exit_idx`: Initialized to `U32_MAX` in `add_subprog`, updated in `check_subprogs`, and set to `prog->len - 1` in `add_hidden_subprog`. Checked against `U32_MAX` in `bpf_insn_successors`. The initialization and use look well-defined.
* `dfs_pos`: This is allocated via `kvcalloc`, so it's zero-initialized.
Next, we hit the crux of the KMSAN decision. We *must* follow the guidelines: Does this patch introduce a *new* KMSAN risk that KASAN *wouldn't* catch?
Let's walk through the points where this patch could potentially create risk:
1. **Kernel Structures to User Space:** No, this patch isn't directly involved in sending kernel data to userspace or modifying user-facing structures. It's focused on internal BPF verifier logic. We're resetting values in the `bpf_insn_array_init`, but this prevents stale data from causing potential problems, which would be caught by other sanitizers or through testing.
2. **Conditional Logic/Branching:** No. Every variable used in conditional logic is initialized before use (zeroed, set to a sentinel value, or explicitly initialized based on a specific logic branch).
3. **Complex Data Structures:** No. All complex data structures have their fields properly zeroed or initialized. `kvzalloc`, `kvcalloc`, and explicit initializations are used.
4. **Bounds Checks/Uninitialized Bytes:** No, because the new code does not introduce new code for this purpose.
5. **Potential Bugs in this Patch Series**:
- Use-after-free or double-free of jump tables (`jt`) (e.g. freeing in `adjust_subprog_starts_after_remove` or `bpf_free_subprog_jts`).
- Out-of-bounds access during CFG analysis, array indexing, or DFS traversal.
- Memory leaks (if `jt` is not freed on failure).
- Deadlocks or locking bugs.
Critically, all these types of errors (UAF, double-free, OOB, memory leaks) are issues that are well-covered by KASAN, KMEMLEAK, and other standard sanitizers and debuggers. We're *not* looking at issues that *only* KMSAN would reveal, like data leaks due to the reading of uninitialized kernel memory being passed to user space.
The patch's changes, specifically in `bpf_insn_array_adjust`, `BPF_MAX_GOTOX_EDGES`, lifetime management of `jt` via `subprog_jts` and DFS traversal tracking with `dfs_pos` are all handled and protected by standard sanitizers such as KASAN.
Therefore, because this patch *does not* introduce any code changes that create risks specifically detectable by KMSAN that aren't also covered by KASAN or standard debuggers, we can safely conclude that **`NeedsKMSAN
|
| 66/3 | 2026/09/27 14:23 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series refactors BPF verifier indirect jump (gotox) table handling, CFG edge bounds checking, SCC iterative DFS traversal, and instruction patching logic.\n\n1. Memory Initialization:\n- All new fields in `struct bpf_verifier_env` (`cfg.gotox_edges`, `cfg.subprog_jts_ready`) and `subprog_info` (`jt`) are zero-initialized at allocation time via `kvzalloc_obj()`.\n- `exit_idx` is explicitly initialized to `U32_MAX` in `add_subprog()` and updated in `check_subprogs()` / `add_hidden_subprog()`.\n- The new `dfs_pos` array in `bpf_compute_scc()` is allocated with `kvcalloc()`, ensuring zero-initialization.\n- In `bpf_insn_array_init()`, stale map entries from previous verification runs are explicitly reset (`jitted_off = 0` and `ips[i] = 0`).\n- Vacated slots in `subprog_info` during subprogram removal are explicitly zeroed with `memset()`.\n\n2. User-space Exposure and Info-leaks:\n- The changes operate purely on internal BPF verifier structures and CFG traversal bookkeeping. No uninitialized data or kernel structure padding is copied to user space.\n\n3. KASAN vs KMSAN Applicability:\n- Any risks introduced by these changes pertain to jump table lifetime management (use-after-free, double free in `bpf_free_subprog_jts()` or `adjust_subprog_starts_after_remove()`), out-of-bounds array accesses in CFG/SCC graph traversal, or memory leaks. These are memory safety bugs detected by KASAN, KMEMLEAK, and standard bug detectors.\n- There are no risks of reading uninitialized stack, heap, or page allocations that would warrant a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series refactors BPF verifier indirect jump (gotox) table handling, CFG edge bounds checking, SCC iterative DFS traversal, and instruction patching logic.\n\n1. Memory Initialization:\n- All new fields in `struct bpf_verifier_env` (`cfg.gotox_edges`, `cfg.subprog_jts_ready`) and `subprog_info` (`jt`) are zero-initialized at allocation time via `kvzalloc_obj()`.\n- `exit_idx` is explicitly initialized to `U32_MAX` in `add_subprog()` and updated in `check_subprogs()` / `add_hidden_subprog()`.\n- The new `dfs_pos` array in `bpf_compute_scc()` is allocated with `kvcalloc()`, ensuring zero-initialization.\n- In `bpf_insn_array_init()`, stale map entries from previous verification runs are explicitly reset (`jitted_off = 0` and `ips[i] = 0`).\n- Vacated slots in `subprog_info` during subprogram removal are explicitly zeroed with `memset()`.\n\n2. User-space Exposure and Info-leaks:\n- The changes operate purely on internal BPF verifier structures and CFG traversal bookkeeping. No uninitialized data or kernel structure padding is copied to user space.\n\n3. KASAN vs KMSAN Applicability:\n- Any risks introduced by these changes pertain to jump table lifetime management (use-after-free, double free in `bpf_free_subprog_jts()` or `adjust_subprog_starts_after_remove()`), out-of-bounds array accesses in CFG/SCC graph traversal, or memory leaks. These are memory safety bugs detected by KASAN, KMEMLEAK, and standard bug detectors.\n- There are no risks of reading uninitialized stack, heap, or page allocations that would warrant a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|