* [PATCH bpf 1/4] bpf: Add bpf_insert_insns() to insert insns next to an insn
2026-11-25 17:12 ` [PATCH bpf 0/4] bpf: Emit nospec after every insn that stores to the stack Luis Gerhorst
@ 2026-11-25 17:12 ` Luis Gerhorst
2026-11-25 18:17 ` bot+bpf-ci
2026-11-25 17:12 ` [PATCH bpf 2/4] selftests/bpf: Test the insns that bpf_insert_insns() inserts Luis Gerhorst
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Luis Gerhorst @ 2026-11-25 17:12 UTC (permalink / raw)
To: Alexei Starovoitov, bpf
Cc: Luis Gerhorst, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
John Fastabend, Shuah Khan, Henriette Herzog, linux-kselftest
bpf_patch_insn_data() replaces an insn with a patch and moves the aux
data of the insn to the last insn of the patch. To insert insns in front
of or behind an insn, the callers put a copy of the insn into the patch.
This has a number of consequences:
- The copy is not the last insn of the patch if the insns are inserted
behind it. adjust_insn_aux_data() finds it by comparing all insns of
the patch with a copy of the original insn, and moves non_stack_access
back to it. The other fields stay at the last insn, so the insn is
left with zeroed aux data for the passes that follow.
- The copy moves down inside the patch if insns are inserted in front of
it. The callers have to adjust it with bpf_adj_moved_insn().
- The callers that insert in front of the insn have to say if the
targets of the insn follow it, with BPF_PATCH_MOVE_TARGET. This only
covers the indirect targets, they have to move the jumps to the insn
with adjust_jmp_off() themselves.
Add bpf_insert_insns() which takes only the new insns and where they
go: in front of the insn, with a jump to the insn running them or
skipping them, or behind the insn. It builds the patch, adjusts the
copy, moves the jumps to the insn if they are to skip the new insns, and
tells adjust_insn_aux_data() where the copy is. The insn keeps
all of its aux data. Every other insn of a patch is new and is marked
for non-stack access if it accesses memory, which no longer needs the
comparison.
Use it where a patch only inserts:
- the zero extension and the randomization of the upper 32 bits in
bpf_opt_subreg_zext_lo32_rnd_hi32()
- the prologue, the save of the ctx for the epilogue and the
initialization of the may_goto counter, which used
BPF_PATCH_MOVE_TARGET
- the per-CPU address of a ld_imm64 in bpf_do_misc_fixups()
- the nospec in front of and behind an insn in
bpf_convert_ctx_accesses()
bpf_insert_insns() returns an ERR_PTR(), so that the callers still
return -ERANGE if the insn can not be adjusted.
bpf_patch_insn_data() is left for the patches that replace the insn.
Where such a patch still contains the insn, the insn now counts as new
and is marked for non-stack access if it accesses memory. This makes no
difference, because none of these patches contains a stack access.
No functional change.
Assisted-by: Claude:claude-mythos-5-2
Signed-off-by: Luis Gerhorst <luis.gerhorst@fau.de>
---
include/linux/bpf_verifier.h | 1 +
kernel/bpf/fixups.c | 284 +++++++++++++++++------------------
2 files changed, 141 insertions(+), 144 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index b43eb97bcd44..2f9fa9d889a4 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1217,6 +1217,7 @@ struct bpf_verifier_env {
char tmp_str_buf[TMP_STR_BUF_LEN];
char tmp_arg_name[32];
struct bpf_insn insn_buf[INSN_BUF_SIZE];
+ struct bpf_insn insert_buf[INSN_BUF_SIZE];
struct bpf_insn epilogue_buf[INSN_BUF_SIZE];
struct bpf_scc_callchain callchain_buf;
struct bpf_liveness *liveness;
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index a656634fa9e4..485feffe0536 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -170,82 +170,60 @@ enum bpf_patch_mode {
BPF_PATCH_MOVE_TARGET, /* targets of 'off' follow the original insn to 'off + len - 1' */
};
+/* The patch does not contain the insn that it replaces. */
+#define BPF_PATCH_NO_ORIG -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
+ *
+ * The aux data of the original insn stays with the insn if the range still
+ * contains it, at index 'orig'. Otherwise 'orig' is BPF_PATCH_NO_ORIG and the
+ * aux data moves to the last insn of the range.
*/
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,
- enum bpf_patch_mode mode)
+ int orig, enum bpf_patch_mode mode)
{
struct bpf_insn_aux_data *data = env->insn_aux_data;
struct bpf_insn *insn = new_prog->insnsi;
+ u32 last = off + cnt - 1;
+ u32 keep = orig == BPF_PATCH_NO_ORIG ? last : off + orig;
+ u32 target = mode == BPF_PATCH_MOVE_TARGET ? keep : off;
+ bool indirect_target = data[off].indirect_target;
u32 old_seen = data[off].seen;
- u32 prog_len;
+ u32 prog_len = new_prog->len;
int i;
- /* aux info at OFF always needs adjustment, no matter fast path
- * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the
- * original insn at old prog.
- */
- data[off].zext_dst = bpf_insn_def32(new_prog, insn + off + cnt - 1) >= 0;
-
- if (cnt == 1) {
- /*
- * A non-memory accessing insn could have been replaced by a
- * memory accessing insn, systematically mark it for non-stack
- * access
- */
- data[off].non_stack_access = bpf_is_mem_insn(insn + off);
- return;
+ if (cnt > 1) {
+ env->insn_aux_data_len = prog_len;
+ memmove(data + last, data + off,
+ sizeof(struct bpf_insn_aux_data) * (prog_len - last));
+ /* The aux data of the original insn is at 'off' and 'last' now. */
+ data[keep] = data[last];
+ for (i = off; i <= last; i++)
+ if (i != keep)
+ memset(data + i, 0, sizeof(struct bpf_insn_aux_data));
}
- prog_len = new_prog->len;
- env->insn_aux_data_len = prog_len;
- memmove(data + off + cnt - 1, data + off,
- sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
- memset(data + off, 0, sizeof(struct bpf_insn_aux_data) * (cnt - 1));
- for (i = off; i < off + cnt - 1; i++) {
+ for (i = off; i <= last; i++) {
/* 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;
/*
- * In BPF_PATCH_MOVE_TARGET mode the original insn is the last
- * slot by construction.
+ * A new insn can access memory. Systematically mark it for
+ * non-stack access, only the original insn keeps its marking.
*/
- 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;
- } else if (bpf_is_mem_insn(insn + i)) {
- data[i].non_stack_access = true;
- }
+ if (orig == BPF_PATCH_NO_ORIG || i != keep)
+ data[i].non_stack_access = bpf_is_mem_insn(insn + i);
}
/*
- * Last slot instruction could be a newly generated
- * BPF_ST/BPF_LDX/BPF_STX, systematically mark it for non-stack access
- * if it is not the original instruction, otherwise keep the
- * original marking
+ * An indirect jump to 'off' goes where the other targets of 'off' go.
+ * This matches bpf_insn_array_adjust().
*/
- if (bpf_is_mem_insn(insn + off + cnt - 1) &&
- memcmp(insn + off + cnt - 1, original_insn, sizeof(struct bpf_insn)))
- data[off + cnt - 1].non_stack_access = true;
-
- /*
- * The indirect_target flag of the original instruction was moved to the last of the
- * 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. For BPF_PATCH_MOVE_TARGET the original instruction is the last one,
- * so the flag already sits where needed.
- */
- if (mode == BPF_PATCH_KEEP_TARGET && data[off + cnt - 1].indirect_target) {
- data[off].indirect_target = 1;
- data[off + cnt - 1].indirect_target = 0;
- }
+ data[keep].indirect_target = 0;
+ data[target].indirect_target = indirect_target;
}
static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
@@ -310,13 +288,12 @@ static bool bpf_rewrite_must_abort(void)
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)
+ int orig, 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())
return NULL;
@@ -332,7 +309,6 @@ static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32
env->insn_aux_data = new_data;
}
- memcpy(&original_insn, env->prog->insnsi + off, sizeof(struct bpf_insn));
new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
if (IS_ERR(new_prog)) {
if (PTR_ERR(new_prog) == -ERANGE)
@@ -341,7 +317,7 @@ static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32
env->insn_aux_data[off].orig_idx);
return NULL;
}
- adjust_insn_aux_data(env, new_prog, off, len, &original_insn, mode);
+ adjust_insn_aux_data(env, new_prog, off, len, orig, mode);
adjust_subprog_starts(env, off, len);
adjust_insn_arrays(env, first, len);
bpf_adjust_func_ptrs(env, off, len);
@@ -352,7 +328,8 @@ static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32
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);
+ return __bpf_patch_insn_data(env, off, patch, len, BPF_PATCH_NO_ORIG,
+ BPF_PATCH_KEEP_TARGET);
}
/*
@@ -431,6 +408,62 @@ static int adjust_jmp_off(struct bpf_prog *prog, u32 tgt_idx, u32 delta)
return 0;
}
+/* Where bpf_insert_insns() puts the new insns relative to the insn at 'off'. */
+enum bpf_insert_pos {
+ BPF_INSERT_BEFORE, /* in front of the insn, a jump to the insn runs them too */
+ BPF_INSERT_ENTRY, /* in front of the insn, a jump to the insn skips them */
+ BPF_INSERT_AFTER, /* behind the insn */
+};
+
+/*
+ * Insert 'cnt' insns next to env->prog->insnsi[off]. Unlike with
+ * bpf_patch_insn_data(), the insn is not replaced: it keeps its aux data,
+ * wherever it ends up. Returns the new prog or an ERR_PTR().
+ */
+static struct bpf_prog *bpf_insert_insns(struct bpf_verifier_env *env, u32 off,
+ const struct bpf_insn *insns, u32 cnt,
+ enum bpf_insert_pos pos)
+{
+ enum bpf_patch_mode mode = pos == BPF_INSERT_ENTRY ? BPF_PATCH_MOVE_TARGET :
+ BPF_PATCH_KEEP_TARGET;
+ struct bpf_insn *patch = env->insert_buf;
+ struct bpf_prog *new_prog;
+ /* The index of the insn in the patch. */
+ u32 orig;
+
+ if (verifier_bug_if(cnt >= INSN_BUF_SIZE, env, "%u insns inserted", cnt))
+ return ERR_PTR(-EFAULT);
+
+ if (pos == BPF_INSERT_AFTER) {
+ orig = 0;
+ memcpy(patch + 1, insns, cnt * sizeof(*insns));
+ } else {
+ orig = cnt;
+ memcpy(patch, insns, cnt * sizeof(*insns));
+ }
+ patch[orig] = env->prog->insnsi[off];
+
+ /* The insn moved down by 'orig' insns. */
+ if (bpf_adj_moved_insn(&patch[orig], orig)) {
+ verbose(env, "insn %d cannot be patched due to 16-bit range\n",
+ env->insn_aux_data[off].orig_idx);
+ return ERR_PTR(-ERANGE);
+ }
+
+ new_prog = __bpf_patch_insn_data(env, off, patch, cnt + 1, orig, mode);
+ if (!new_prog)
+ return ERR_PTR(-ENOMEM);
+
+ /*
+ * The jumps to the insn still name 'off'. Adjustment will succeed
+ * because __bpf_patch_insn_data() didn't fail.
+ */
+ if (pos == BPF_INSERT_ENTRY)
+ WARN_ON(adjust_jmp_off(new_prog, off, cnt));
+
+ return new_prog;
+}
+
static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
u32 off, u32 cnt)
{
@@ -757,7 +790,7 @@ int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
struct bpf_insn *patch;
/* use env->insn_buf as two independent buffers */
struct bpf_insn *zext_patch = env->insn_buf;
- struct bpf_insn *rnd_hi32_patch = &env->insn_buf[2];
+ struct bpf_insn *rnd_hi32_patch = &env->insn_buf[1];
struct bpf_insn_aux_data *aux = env->insn_aux_data;
int i, patch_len, delta = 0, len = env->prog->len;
struct bpf_insn *insns = env->prog->insnsi;
@@ -765,10 +798,10 @@ int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
bool rnd_hi32;
rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32;
- zext_patch[1] = BPF_ZEXT_REG(0);
- rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0);
- rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
- rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX);
+ zext_patch[0] = BPF_ZEXT_REG(0);
+ rnd_hi32_patch[0] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0);
+ rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
+ rnd_hi32_patch[2] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX);
for (i = 0; i < len; i++) {
int adj_idx = i + delta;
struct bpf_insn insn;
@@ -801,11 +834,10 @@ int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
continue;
imm_rnd = get_random_u32();
- rnd_hi32_patch[0] = insn;
- rnd_hi32_patch[1].imm = imm_rnd;
- rnd_hi32_patch[3].dst_reg = load_reg;
+ rnd_hi32_patch[0].imm = imm_rnd;
+ rnd_hi32_patch[2].dst_reg = load_reg;
patch = rnd_hi32_patch;
- patch_len = 4;
+ patch_len = 3;
goto apply_patch_buffer;
}
@@ -830,19 +862,18 @@ int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
"zext_dst is set, but no reg is defined"))
return -EFAULT;
- zext_patch[0] = insn;
- zext_patch[1].dst_reg = load_reg;
- zext_patch[1].src_reg = load_reg;
+ zext_patch[0].dst_reg = load_reg;
+ zext_patch[0].src_reg = load_reg;
patch = zext_patch;
- patch_len = 2;
+ patch_len = 1;
apply_patch_buffer:
- new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
- if (!new_prog)
- return -ENOMEM;
+ new_prog = bpf_insert_insns(env, adj_idx, patch, patch_len, BPF_INSERT_AFTER);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
env->prog = new_prog;
insns = new_prog->insnsi;
aux = env->insn_aux_data;
- delta += patch_len - 1;
+ delta += patch_len;
}
return 0;
@@ -909,6 +940,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
const struct bpf_verifier_ops *ops = env->ops;
int i, cnt, size, ctx_field_size, ret, delta = 0, epilogue_cnt = 0;
const int insn_cnt = env->prog->len;
+ const struct bpf_insn nospec = BPF_ST_NOSPEC();
struct bpf_insn *epilogue_buf = env->epilogue_buf;
struct bpf_insn *insn_buf = env->insn_buf;
struct bpf_insn *insn;
@@ -930,13 +962,11 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
subprogs[0].stack_depth += 8;
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,
- BPF_PATCH_MOVE_TARGET);
- if (!new_prog)
- return -ENOMEM;
+ new_prog = bpf_insert_insns(env, 0, insn_buf, cnt, BPF_INSERT_ENTRY);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
env->prog = new_prog;
- delta += cnt - 1;
+ delta += cnt;
ret = add_kfunc_in_insns(env, epilogue_buf, epilogue_cnt - 1);
if (ret < 0)
@@ -967,10 +997,11 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
env->prog->aux->attach_btf_id))
return -EINVAL;
env->prog->aux->has_prologue = true;
- new_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,
- BPF_PATCH_MOVE_TARGET);
- if (!new_prog)
- return -ENOMEM;
+ /* the prologue ends with a copy of the first insn */
+ new_prog = bpf_insert_insns(env, 0, insn_buf, cnt - 1,
+ BPF_INSERT_ENTRY);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
env->prog = new_prog;
delta += cnt - 1;
@@ -981,9 +1012,6 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
}
}
- if (delta)
- WARN_ON(adjust_jmp_off(env->prog, 0, delta));
-
if (bpf_prog_is_offloaded(env->prog->aux))
return 0;
@@ -996,29 +1024,20 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
if (env->insn_aux_data[i + delta].nospec) {
WARN_ON_ONCE(env->insn_aux_data[i + delta].alu_state);
- struct bpf_insn *patch = insn_buf;
- *patch++ = BPF_ST_NOSPEC();
- *patch = *insn;
- ret = bpf_adj_moved_insn(patch++, 1);
- if (ret) {
- verbose(env, "insn %d cannot be patched due to 16-bit range\n",
- env->insn_aux_data[i + delta].orig_idx);
- return ret;
- }
- cnt = patch - insn_buf;
- new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
- if (!new_prog)
- return -ENOMEM;
+ new_prog = bpf_insert_insns(env, i + delta, &nospec, 1,
+ BPF_INSERT_BEFORE);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
- delta += cnt - 1;
+ delta += 1;
env->prog = new_prog;
insn = new_prog->insnsi + i + delta;
/* This can not be easily merged with the
* nospec_result-case, because an insn may require a
* nospec before and after itself. Therefore also do not
* 'continue' here but potentially apply further
- * patching to insn. *insn should equal patch[1] now.
+ * patching to insn.
*/
}
@@ -1090,16 +1109,12 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
/* nospec_result is only used to mitigate Spectre v4 and
* to limit verification-time for Spectre v1.
*/
- struct bpf_insn *patch = insn_buf;
-
- *patch++ = *insn;
- *patch++ = BPF_ST_NOSPEC();
- cnt = patch - insn_buf;
- new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
- if (!new_prog)
- return -ENOMEM;
+ new_prog = bpf_insert_insns(env, i + delta, &nospec, 1,
+ BPF_INSERT_AFTER);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
- delta += cnt - 1;
+ delta += 1;
env->prog = new_prog;
insn = new_prog->insnsi + i + delta;
continue;
@@ -2192,23 +2207,20 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
/*
* We are *skipping* first half of ld_imm64 insn
- * with 'i++;', patching over second half of it
- * with that same half + mov64_percpu_reg insn.
- * All because bpf_patch_insn_data() can only
- * replace one 8-byte insn, which does not work
- * well for ld_imm64 insn.
+ * with 'i++;', to insert the mov64_percpu_reg insn
+ * behind the second half of it.
*/
- insn_buf[0] = insn[1];
- insn_buf[1] = BPF_MOV64_PERCPU_REG(insn->dst_reg, insn->dst_reg);
- cnt = 2;
+ insn_buf[0] = BPF_MOV64_PERCPU_REG(insn->dst_reg, insn->dst_reg);
+ cnt = 1;
i++;
- new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
- if (!new_prog)
- return -ENOMEM;
+ new_prog = bpf_insert_insns(env, i + delta, insn_buf, cnt,
+ BPF_INSERT_AFTER);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
- delta += cnt - 1;
+ delta += cnt;
env->prog = prog = new_prog;
insn = new_prog->insnsi + i + delta;
goto next_insn;
@@ -2818,28 +2830,12 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
BPF_MAX_LOOPS);
}
- /* Copy first actual insn to preserve it */
- insn_buf[cnt] = env->prog->insnsi[subprog_start];
- ret = bpf_adj_moved_insn(&insn_buf[cnt], cnt);
- if (ret) {
- verbose(env, "insn %d cannot be patched due to 16-bit range\n",
- env->insn_aux_data[subprog_start].orig_idx);
- return ret;
- }
- cnt++;
- new_prog = __bpf_patch_insn_data(env, subprog_start, insn_buf, cnt,
- BPF_PATCH_MOVE_TARGET);
- if (!new_prog)
- return -ENOMEM;
+ new_prog = bpf_insert_insns(env, subprog_start, insn_buf, cnt,
+ BPF_INSERT_ENTRY);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
env->prog = prog = new_prog;
- /*
- * If may_goto is a first insn of a prog there could be a jmp
- * insn that points to it, hence adjust all such jmps to point
- * to insn after BPF_ST that inits may_goto count.
- * Adjustment will succeed because bpf_patch_insn_data() didn't fail.
- */
- WARN_ON(adjust_jmp_off(env->prog, subprog_start, cnt - 1));
}
/* Since poke tab is now finalized, publish aux to tracker. */
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH bpf 3/4] bpf: Emit nospec after every insn with nospec_result
2026-11-25 17:12 ` [PATCH bpf 0/4] bpf: Emit nospec after every insn that stores to the stack Luis Gerhorst
2026-11-25 17:12 ` [PATCH bpf 1/4] bpf: Add bpf_insert_insns() to insert insns next to an insn Luis Gerhorst
2026-11-25 17:12 ` [PATCH bpf 2/4] selftests/bpf: Test the insns that bpf_insert_insns() inserts Luis Gerhorst
@ 2026-11-25 17:12 ` Luis Gerhorst
2026-11-25 18:17 ` bot+bpf-ci
2026-11-25 17:12 ` [PATCH bpf 4/4] selftests/bpf: Test nospec after stack writes of calls and atomics Luis Gerhorst
3 siblings, 1 reply; 7+ messages in thread
From: Luis Gerhorst @ 2026-11-25 17:12 UTC (permalink / raw)
To: Alexei Starovoitov, bpf
Cc: Luis Gerhorst, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
John Fastabend, Shuah Khan, Henriette Herzog, linux-kselftest
check_stack_write_fixed_off() sets nospec_result of the insn that writes
to the stack if the store must not be bypassed speculatively. Besides a
BPF_MEM store, the insn can be a BPF_ATOMIC insn or a call: a helper or
kfunc that initializes a dynptr, an iterator or the flags of
bpf_local_irq_save() on the stack. But bpf_convert_ctx_accesses() emits
the nospec only after BPF_MEM stores.
This has two consequences for the other insns:
- Spectre v4: If the store is bypassed, what reads the slot behind the
insn gets the old content of the slot: a load in the program, or a
helper that is passed the slot as a dynptr.
- Spectre v1: do_check() ends a speculative path at an insn with
nospec_result, because a nospec follows that insn. Without the
nospec, the insns behind it are not verified for that path.
Emit the nospec after every insn with nospec_result, whatever the insn
is. Do this in a pass of its own, bpf_emit_nospec(), which runs before
bpf_convert_ctx_accesses(), and move the nospec in front of an insn
there too. The conversion of an insn no longer depends on nospec_result:
bpf_convert_ctx_accesses() used to skip an insn after it emitted the
nospec for it.
Both are inserted with bpf_insert_insns(), so that the insn keeps its
aux data for bpf_convert_ctx_accesses() and bpf_do_misc_fixups().
The pass is skipped if both Spectre v1 and v4 mitigations are bypassed,
because neither nospec nor nospec_result is set then. It only runs for
programs that are loaded without CAP_PERFMON: there it took at most
8.4 ms in our measurements, for a program of the maximum size of 1M
insns without any nospec, which is 0.4% of the 2.1 s that the program
takes to verify.
An insn that is only reached speculatively is rewritten to 'goto -1' by
sanitize_dead_code() and keeps nospec_result. It gets a nospec behind
it now, like it already gets one in front of it for nospec.
Fixes: d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
Assisted-by: Claude:claude-mythos-5-2
Signed-off-by: Luis Gerhorst <luis.gerhorst@fau.de>
---
include/linux/bpf_verifier.h | 1 +
kernel/bpf/fixups.c | 91 ++++++++++++++++++++++--------------
kernel/bpf/verifier.c | 3 ++
3 files changed, 59 insertions(+), 36 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 2f9fa9d889a4..7dd71cef3f3a 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -2029,6 +2029,7 @@ void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env);
int bpf_opt_remove_dead_code(struct bpf_verifier_env *env);
int bpf_opt_remove_nops(struct bpf_verifier_env *env);
int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr);
+int bpf_emit_nospec(struct bpf_verifier_env *env);
int bpf_convert_ctx_accesses(struct bpf_verifier_env *env);
int bpf_jit_subprogs(struct bpf_verifier_env *env);
int bpf_fixup_call_args(struct bpf_verifier_env *env);
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 485feffe0536..d5faaf3233c8 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -940,7 +940,6 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
const struct bpf_verifier_ops *ops = env->ops;
int i, cnt, size, ctx_field_size, ret, delta = 0, epilogue_cnt = 0;
const int insn_cnt = env->prog->len;
- const struct bpf_insn nospec = BPF_ST_NOSPEC();
struct bpf_insn *epilogue_buf = env->epilogue_buf;
struct bpf_insn *insn_buf = env->insn_buf;
struct bpf_insn *insn;
@@ -1022,25 +1021,6 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
enum bpf_reg_type ptr_type;
u8 mode;
- if (env->insn_aux_data[i + delta].nospec) {
- WARN_ON_ONCE(env->insn_aux_data[i + delta].alu_state);
-
- new_prog = bpf_insert_insns(env, i + delta, &nospec, 1,
- BPF_INSERT_BEFORE);
- if (IS_ERR(new_prog))
- return PTR_ERR(new_prog);
-
- delta += 1;
- env->prog = new_prog;
- insn = new_prog->insnsi + i + delta;
- /* This can not be easily merged with the
- * nospec_result-case, because an insn may require a
- * nospec before and after itself. Therefore also do not
- * 'continue' here but potentially apply further
- * patching to insn.
- */
- }
-
if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
@@ -1104,22 +1084,6 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
continue;
}
- if (type == BPF_WRITE &&
- env->insn_aux_data[i + delta].nospec_result) {
- /* nospec_result is only used to mitigate Spectre v4 and
- * to limit verification-time for Spectre v1.
- */
- new_prog = bpf_insert_insns(env, i + delta, &nospec, 1,
- BPF_INSERT_AFTER);
- if (IS_ERR(new_prog))
- return PTR_ERR(new_prog);
-
- delta += 1;
- env->prog = new_prog;
- insn = new_prog->insnsi + i + delta;
- continue;
- }
-
ptr_type = env->insn_aux_data[i + delta].ptr_type;
switch ((int)ptr_type) {
case PTR_TO_CTX:
@@ -1862,6 +1826,61 @@ static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,
return cnt;
}
+/*
+ * Emit the speculation barriers that do_check() asked for:
+ *
+ * - nospec: a nospec in front of the insn. It is set if the insn failed a
+ * check on a speculative path.
+ *
+ * - nospec_result: a nospec behind the insn. It is only used to mitigate
+ * Spectre v4 and to limit verification-time for Spectre v1. It is set for a
+ * store to the stack, whatever the insn that stores is: BPF_ST, BPF_STX
+ * including BPF_ATOMIC, or a call of a helper or kfunc.
+ *
+ * An insn may require both. The insn keeps its aux data, so this has to run
+ * before the passes that rewrite the insn itself: what they emit for the insn
+ * stays between the two nospec.
+ */
+int bpf_emit_nospec(struct bpf_verifier_env *env)
+{
+ const struct bpf_insn nospec = BPF_ST_NOSPEC();
+ struct bpf_prog *new_prog;
+ int i;
+
+ /* Neither of the two is set without the mitigations. */
+ if (env->bypass_spec_v1 && env->bypass_spec_v4)
+ return 0;
+
+ if (bpf_prog_is_offloaded(env->prog->aux))
+ return 0;
+
+ for (i = 0; i < env->prog->len; i++) {
+ if (env->insn_aux_data[i].nospec) {
+ WARN_ON_ONCE(env->insn_aux_data[i].alu_state);
+
+ new_prog = bpf_insert_insns(env, i, &nospec, 1, BPF_INSERT_BEFORE);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
+
+ env->prog = new_prog;
+ /* the insn and its aux data are behind the nospec now */
+ i++;
+ }
+
+ if (env->insn_aux_data[i].nospec_result) {
+ new_prog = bpf_insert_insns(env, i, &nospec, 1, BPF_INSERT_AFTER);
+ if (IS_ERR(new_prog))
+ return PTR_ERR(new_prog);
+
+ env->prog = new_prog;
+ /* skip the nospec */
+ i++;
+ }
+ }
+
+ return 0;
+}
+
/* Do various post-verification rewrites in a single program pass.
* These rewrites simplify JIT and interpreter implementations.
*/
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 13c53df386bc..aa73c3326d39 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -24220,6 +24220,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
sanitize_dead_code(env);
}
+ if (ret == 0)
+ ret = bpf_emit_nospec(env);
+
if (ret == 0)
/* program is valid, convert *(u32*)(ctx + off) accesses */
ret = bpf_convert_ctx_accesses(env);
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread