Skip to contentSkip to conversation

bpf: Fixes for speculation barriers and constant reads

PATCH Applied Alexei Starovoitov 4 participants 8 messages Nov 26

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

v2 cover bpf: Fixes for speculation barriers and constant reads

From: Alexei Starovoitov <[email protected]>

These patches fix bugs in the verifier:

  patch  area
  -----  ------------------------------------------------------------------
  1      insn aux data after a failed check on a speculative path
  2      spills of invalid registers
  3      unaligned reads of read-only maps
  4      the barrier after a store to the stack by an atomic or a call

v1 -> v2:
- Replace patches 4 and 5 with one patch that emits the nospec after calls too (Luis). Patch 5 rejected bounded loops with such a call.

Patch 4 takes the idea of Luis's series [1] and inserts the nospec in another way. bpf_patch_insn_data() moves the aux data to the last insn of a patch, so "call; nospec" takes it from the call. [1] adds bpf_insert_insns() and a new pass for that. Patch 4 leaves the call as it is and makes "nospec; insn" of the next insn, with the code that is there already. The price: a jump to the next insn runs the nospec too. bpf-next has replaced bpf_patch_insn_data() with the patch list, so the new helper would go away at the merge.

v1: https://lore.kernel.org/bpf/[email protected]/
[1] https://lore.kernel.org/bpf/[email protected]/

Alexei Starovoitov (4):
bpf: Undo insn aux changes of a failed speculative path
bpf: Treat NOT_INIT as a pointer in __is_pointer_value()
bpf: Use get_unaligned() in bpf_map_direct_read()
bpf: Emit nospec after calls and atomics that write to the stack

 kernel/bpf/fixups.c   | 11 +++++++++--
 kernel/bpf/verifier.c | 16 +++++++++++-----
 2 files changed, 20 insertions(+), 7 deletions(-)

base-commit: 3db9d99ba7b8d5a4a520f5abf785e17406f518a5

Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/4 bpf: Undo insn aux changes of a failed speculative path

From: Alexei Starovoitov <[email protected]>

If a check fails on a speculative path, do_check() adds a nospec before the insn and ends the path. The check can have written nospec_result and ctx_field_size of the insn already. The other paths can use the insn with another type of pointer, so these values do not apply to them.

Save both fields before the check and restore them.

Fixes d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
---
 kernel/bpf/verifier.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +6 -1
index d4b31990fcb9..c553e1533ace 100644
@@ -20649,7 +20649,8 @@ static int do_check(struct bpf_verifier_env *env)
 	for (;;) {
 		struct bpf_insn *insn;
 		struct bpf_insn_aux_data *insn_aux;
-		int err;
+		int err, ctx_field_size;
+		bool nospec_result;
 
 		if (signal_pending(current))
 			return -EAGAIN;
@@ -20802,6 +20803,8 @@ static int do_check(struct bpf_verifier_env *env)
 		if (state->speculative && insn_aux->nospec)
 			goto process_bpf_exit;
 
+		nospec_result = insn_aux->nospec_result;
+		ctx_field_size = insn_aux->ctx_field_size;
 		err = do_check_insn(env, &do_print_state);
 		if (error_recoverable_with_nospec(err) && state->speculative) {
 			/* Prevent this speculative path from ever reaching the
@@ -20812,6 +20815,8 @@ static int do_check(struct bpf_verifier_env *env)
 			 * markings for alu sanitization.
 			 */
 			insn_aux->alu_state = 0;
+			insn_aux->nospec_result = nospec_result;
+			insn_aux->ctx_field_size = ctx_field_size;
 			goto process_bpf_exit;
 		} else if (err < 0) {
 			return err;
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/4 bpf: Treat NOT_INIT as a pointer in __is_pointer_value()

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The blamed commit made __is_pointer_value() use is_pointer_regtype(), which is false for NOT_INIT. mark_reg_invalid() can set a spilled register to NOT_INIT, and check_stack_read_fixed_off() must not handle such a spill like a spilled scalar.

Go back to the check for SCALAR_VALUE.

Fixes 1f737e46ca6a ("bpf: Remove artificial limitations on pointer types eligible for spilling")
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index c553e1533ace..5929478787d3 100644
@@ -3825,7 +3825,7 @@ static bool __is_pointer_value(bool allow_ptr_leaks,
 	if (allow_ptr_leaks)
 		return false;
 
-	return is_pointer_regtype(reg->type);
+	return reg->type != SCALAR_VALUE;
 }
 
 static void assign_scalar_id_before_mov(struct bpf_verifier_env *env,
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 3/4 bpf: Use get_unaligned() in bpf_map_direct_read()

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

const_reg_xfer() calls bpf_map_direct_read() before the main pass checks the alignment of the load. So the offset is not always a multiple of the size.

Fixes f1606dd0ac49 ("bpf: Add bpf_compute_const_regs() and bpf_prune_dead_branches() passes")
---
 kernel/bpf/verifier.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +4 -3
index 5929478787d3..36441ed9f0ea 100644
@@ -32,6 +32,7 @@
 #include <linux/cpumask.h>
 #include <linux/cnum.h>
 #include <linux/gcd.h>
+#include <linux/unaligned.h>
 #include <linux/bpf_mem_alloc.h>
 #include <net/xdp.h>
 #include <linux/trace_events.h>
@@ -6386,13 +6387,13 @@ int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val,
 		*val = is_ldsx ? (s64)*(s8 *)ptr : (u64)*(u8 *)ptr;
 		break;
 	case sizeof(u16):
-		*val = is_ldsx ? (s64)*(s16 *)ptr : (u64)*(u16 *)ptr;
+		*val = is_ldsx ? (s64)get_unaligned((s16 *)ptr) : (u64)get_unaligned((u16 *)ptr);
 		break;
 	case sizeof(u32):
-		*val = is_ldsx ? (s64)*(s32 *)ptr : (u64)*(u32 *)ptr;
+		*val = is_ldsx ? (s64)get_unaligned((s32 *)ptr) : (u64)get_unaligned((u32 *)ptr);
 		break;
 	case sizeof(u64):
-		*val = *(u64 *)ptr;
+		*val = get_unaligned((u64 *)ptr);
 		break;
 	default:
 		return -EINVAL;
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 4/4 bpf: Emit nospec after calls and atomics that write to the stack

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

check_stack_write_fixed_off() sets nospec_result for BPF_ATOMIC insns and for calls of helpers and kfuncs too. do_check() ends a speculative path there, but bpf_convert_ctx_accesses() emits the nospec only after BPF_MEM stores.

Later passes rewrite a call and need its aux data. Put the nospec in front of the next insn.

Fixes d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
Fixes 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")
---
 kernel/bpf/fixups.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/bpf/fixups.c +9 -2
index 9d5f2774bf8f..8c3a17a05a4e 100644
@@ -911,6 +911,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
 	const int insn_cnt = env->prog->len;
 	struct bpf_insn *epilogue_buf = env->epilogue_buf;
 	struct bpf_insn *insn_buf = env->insn_buf;
+	bool nospec, nospec_next = false;
 	struct bpf_insn *insn;
 	u32 target_size, size_default, off;
 	struct bpf_prog *new_prog;
@@ -994,8 +995,13 @@ 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);
+		/* A call or an atomic gets its nospec in front of the next insn */
+		nospec = env->insn_aux_data[i + delta].nospec;
+		WARN_ON_ONCE(nospec && env->insn_aux_data[i + delta].alu_state);
+		nospec |= nospec_next;
+		nospec_next = env->insn_aux_data[i + delta].nospec_result;
+
+		if (nospec) {
 			struct bpf_insn *patch = insn_buf;
 
 			*patch++ = BPF_ST_NOSPEC();
@@ -1102,6 +1108,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
 			delta    += cnt - 1;
 			env->prog = new_prog;
 			insn      = new_prog->insnsi + i + delta;
+			nospec_next = false;
 			continue;
 		}
 
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack

TL;DR: Commit message only: the second Fixes: tag (880442305a39, load-acquire/store-release) may be wrong, since it predates nospec_result and d6f1c85f2253 already covers the problem.
Quoted 1 line

bpf: Emit nospec after calls and atomics that write to the stack

TL;DR: Commit message only: the second Fixes: tag (880442305a39, load-acquire/store-release) may be wrong, since it predates nospec_result and d6f1c85f2253 already covers the problem.

Quoted 8 lines

bpf: Emit nospec after calls and atomics that write to the stack

check_stack_write_fixed_off() sets nospec_result for BPF_ATOMIC insns and for calls of helpers and kfuncs too. do_check() ends a speculative path there, but bpf_convert_ctx_accesses() emits the nospec only after BPF_MEM stores.
Later passes rewrite a call and need its aux data. Put the nospec in front of the next insn.

Quoted 4 lines
Fixes d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
Fixes 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")

Is the second Fixes: tag correct?

Fixes 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")

That commit predates the nospec_result mechanism. The problem described here, nospec_result being set for atomics and calls while bpf_convert_ctx_accesses() emits the nospec only after BPF_MEM stores, comes from d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1"), which is already listed in the first Fixes: tag. Should the 880442305a39 tag be dropped?

---
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/45752115987

Acked-by on v2 4/4
#6 lore raw

Re: [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack

In reply to Alexei Starovoitov
Quoted 47 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

check_stack_write_fixed_off() sets nospec_result for BPF_ATOMIC insns and for calls of helpers and kfuncs too. do_check() ends a speculative path there, but bpf_convert_ctx_accesses() emits the nospec only after BPF_MEM stores.

Later passes rewrite a call and need its aux data. Put the nospec in front of the next insn.

Fixes d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
Fixes 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")
---
 kernel/bpf/fixups.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/bpf/fixups.c +8 -2
index 9d5f2774bf8f..8c3a17a05a4e 100644
@@ -911,6 +911,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
 	const int insn_cnt = env->prog->len;
 	struct bpf_insn *epilogue_buf = env->epilogue_buf;
 	struct bpf_insn *insn_buf = env->insn_buf;
+	bool nospec, nospec_next = false;
 	struct bpf_insn *insn;
 	u32 target_size, size_default, off;
 	struct bpf_prog *new_prog;
@@ -994,8 +995,13 @@ 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);
+		/* A call or an atomic gets its nospec in front of the next insn */
+		nospec = env->insn_aux_data[i + delta].nospec;
+		WARN_ON_ONCE(nospec && env->insn_aux_data[i + delta].alu_state);
+		nospec |= nospec_next;
+		nospec_next = env->insn_aux_data[i + delta].nospec_result;
+
+		if (nospec) {
 			struct bpf_insn *patch = insn_buf;
 
 			*patch++ = BPF_ST_NOSPEC();

kernel/bpf/fixups.c int bpf_convert_ctx_accesses(struct bpf_verifier_env *env) · line 1113

Quoted 6 lines
kernel/bpf/fixups.c +1 -0
@@ -1102,6 +1108,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
 			delta    += cnt - 1;
 			env->prog = new_prog;
 			insn      = new_prog->insnsi + i + delta;
+			nospec_next = false;
 			continue;
 		}

This looks much better than my proposal, thank you!

The bot appears to not have realized that nospec_result is mostly a rename of sanitize_stack_spill.

#7 lore raw
In reply to Alexei Starovoitov
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 4 lines · On Thu, 26 Nov 2026 03:38:12 +0000 you wrote:

From: Alexei Starovoitov <[email protected]>

These patches fix bugs in the verifier:

Quoted 8 lines
  patch  area
  -----  ------------------------------------------------------------------
  1      insn aux data after a failed check on a speculative path
  2      spills of invalid registers
  3      unaligned reads of read-only maps
  4      the barrier after a store to the stack by an atomic or a call

[...]

Here is the summary with links:
- [bpf,v2,1/4] bpf: Undo insn aux changes of a failed speculative path

    https://git.kernel.org/bpf/bpf/c/fa84f87e42af
  - [bpf,v2,2/4] bpf: Treat NOT_INIT as a pointer in __is_pointer_value()
    https://git.kernel.org/bpf/bpf/c/bbe2012ece4a
  - [bpf,v2,3/4] bpf: Use get_unaligned() in bpf_map_direct_read()
    https://git.kernel.org/bpf/bpf/c/599e0ce412d5
  - [bpf,v2,4/4] bpf: Emit nospec after calls and atomics that write to the stack
    https://git.kernel.org/bpf/bpf/c/1879b5e505de

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html