Skip to contentSkip to conversation

bpf: verifier: restrict insn_array_maps to jump tables

PATCH New Adith-Joshua 2 participants 2 messages Apr 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: verifier: restrict insn_array_maps to jump tables

jt_from_subprog() currently iterates over all insn_array_maps and treats them as jump tables. However, this may include maps that are not actual jump tables, such as static keys or maps used for indirect calls.

Restrict processing to BPF_MAP_TYPE_INSN_ARRAY maps with multiple entries, which correspond to jump tables.

This improves correctness by avoiding unrelated maps during jump table collection while keeping the logic simple.

---
 kernel/bpf/verifier.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +8 -4
index e3814152b52f..e2583dfd7bf2 100644
@@ -18693,12 +18693,16 @@ static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,
 	int i;
 
 	for (i = 0; i < env->insn_array_map_cnt; i++) {
-		/*
-		 * TODO (when needed): collect only jump tables, not static keys
-		 * or maps for indirect calls
-		 */
 		map = env->insn_array_maps[i];
 
+		/* Only consider instruction array maps with multiple entries.
+		 * These correspond to jump tables. Skip others (e.g. static keys,
+		 * indirect call maps).
+		 */
+		if (map->map_type != BPF_MAP_TYPE_INSN_ARRAY ||
+		    map->max_entries <= 1)
+			continue;
+
 		jt_cur = jt_from_map(map);
 		if (IS_ERR(jt_cur)) {
 			kvfree(jt);
Signature · 1 line
2.53.0
#1 lore raw
Is the map->map_type check needed here? The insn_array_maps[] array is populated exclusively in __add_used_map():
Quoted 7 lines
kernel/bpf/verifier.c +0 -0
index 84699a428077..83566d2f446e 100644
@@ -19098,12 +19098,16 @@ static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env,
 	int i;
 

kernel/bpf/verifier.c static struct bpf_iarray *jt_from_subprog(struct bpf_verifier_env *env, · line 19109

Quoted 10 lines
kernel/bpf/verifier.c +7 -0
 	for (i = 0; i < env->insn_array_map_cnt; i++) {
 		map = env->insn_array_maps[i];
 
+		/* Only consider instruction array maps with multiple entries.
+		 * These correspond to jump tables. Skip others (e.g. static keys,
+		 * indirect call maps).
+		 */
+		if (map->map_type != BPF_MAP_TYPE_INSN_ARRAY ||
+		    map->max_entries <= 1)
+			continue;

Is the map->map_type check needed here? The insn_array_maps[] array is populated exclusively in __add_used_map():

    if (map->map_type == BPF_MAP_TYPE_INSN_ARRAY) {
        ...
        env->insn_array_maps[env->insn_array_map_cnt++] = map;
    }

Only BPF_MAP_TYPE_INSN_ARRAY maps can appear in this array, so map->map_type != BPF_MAP_TYPE_INSN_ARRAY is always false. This makes the check dead code, and the comment "Only consider instruction array maps" suggests the array could contain other types, which it cannot.

The effective filter is just max_entries <= 1. The condition could be simplified to:

    if (map->max_entries <= 1)
        continue;

and the comment adjusted to reflect that.

Similarly, the commit message says "Restrict processing to BPF_MAP_TYPE_INSN_ARRAY maps with multiple entries" but processing was already restricted to BPF_MAP_TYPE_INSN_ARRAY maps by construction of the array.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24029846149