BPF List
 help / color / mirror / Atom feed
* [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads
@ 2026-11-26  3:38 Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 1/4] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-26  3:38 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor, luis.gerhorst

From: Alexei Starovoitov <ast@kernel.org>

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/20261125040820.947816-1-alexei.starovoitov@gmail.com/
[1] https://lore.kernel.org/bpf/cover.1795625597.git.luis.gerhorst@fau.de/

Signed-off-by: Alexei Starovoitov <ast@kernel.org>

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
-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH bpf v2 1/4] bpf: Undo insn aux changes of a failed speculative path
  2026-11-26  3:38 [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
@ 2026-11-26  3:38 ` Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 2/4] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-26  3:38 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor, luis.gerhorst

From: Alexei Starovoitov <ast@kernel.org>

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")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/verifier.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d4b31990fcb9..c553e1533ace 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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;
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf v2 2/4] bpf: Treat NOT_INIT as a pointer in __is_pointer_value()
  2026-11-26  3:38 [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 1/4] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
@ 2026-11-26  3:38 ` Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 3/4] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack Alexei Starovoitov
  3 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-26  3:38 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor, luis.gerhorst

From: Alexei Starovoitov <ast@kernel.org>

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")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index c553e1533ace..5929478787d3 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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,
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf v2 3/4] bpf: Use get_unaligned() in bpf_map_direct_read()
  2026-11-26  3:38 [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 1/4] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 2/4] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
@ 2026-11-26  3:38 ` Alexei Starovoitov
  2026-11-26  3:38 ` [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack Alexei Starovoitov
  3 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-26  3:38 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor, luis.gerhorst

From: Alexei Starovoitov <ast@kernel.org>

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")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/verifier.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 5929478787d3..36441ed9f0ea 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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;
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack
  2026-11-26  3:38 [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
                   ` (2 preceding siblings ...)
  2026-11-26  3:38 ` [PATCH bpf v2 3/4] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
@ 2026-11-26  3:38 ` Alexei Starovoitov
  2026-11-26  4:27   ` bot+bpf-ci
  2026-11-26  7:59   ` Luis Gerhorst
  3 siblings, 2 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-26  3:38 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor, luis.gerhorst

From: Alexei Starovoitov <ast@kernel.org>

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")
Suggested-by: Luis Gerhorst <luis.gerhorst@fau.de>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/fixups.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 9d5f2774bf8f..8c3a17a05a4e 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -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;
 		}
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack
  2026-11-26  3:38 ` [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack Alexei Starovoitov
@ 2026-11-26  4:27   ` bot+bpf-ci
  2026-11-26  7:59   ` Luis Gerhorst
  1 sibling, 0 replies; 7+ messages in thread
From: bot+bpf-ci @ 2026-11-26  4:27 UTC (permalink / raw)
  To: alexei.starovoitov, bpf
  Cc: daniel, andrii, eddyz87, memxor, luis.gerhorst, ast, andrii,
	daniel, martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 1636 bytes --]

> 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.

> 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.
> Fixes: d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
> Fixes: 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")
> Suggested-by: Luis Gerhorst <luis.gerhorst@fau.de>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack
  2026-11-26  3:38 ` [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack Alexei Starovoitov
  2026-11-26  4:27   ` bot+bpf-ci
@ 2026-11-26  7:59   ` Luis Gerhorst
  1 sibling, 0 replies; 7+ messages in thread
From: Luis Gerhorst @ 2026-11-26  7:59 UTC (permalink / raw)
  To: Alexei Starovoitov; +Cc: bpf, daniel, andrii, eddyz87, memxor

Alexei Starovoitov <alexei.starovoitov@gmail.com> writes:

> From: Alexei Starovoitov <ast@kernel.org>
>
> 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")
> Suggested-by: Luis Gerhorst <luis.gerhorst@fau.de>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
>  kernel/bpf/fixups.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
> index 9d5f2774bf8f..8c3a17a05a4e 100644
> --- a/kernel/bpf/fixups.c
> +++ b/kernel/bpf/fixups.c
> @@ -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;
>  		}

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.

Acked-by: Luis Gerhorst <luis.gerhorst@fau.de>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-26  7:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  3:38 [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
2026-11-26  3:38 ` [PATCH bpf v2 1/4] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
2026-11-26  3:38 ` [PATCH bpf v2 2/4] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
2026-11-26  3:38 ` [PATCH bpf v2 3/4] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
2026-11-26  3:38 ` [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack Alexei Starovoitov
2026-11-26  4:27   ` bot+bpf-ci
2026-11-26  7:59   ` Luis Gerhorst

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox