* [PATCH bpf 0/2] bpf: Fix stack slot index for Spectre v4 nospec checks
@ 2026-06-16 16:57 Nuoqi Gui
2026-06-16 16:57 ` [PATCH bpf 1/2] bpf: Fix stack slot index in " Nuoqi Gui
2026-06-16 16:57 ` [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing Nuoqi Gui
0 siblings, 2 replies; 7+ messages in thread
From: Nuoqi Gui @ 2026-06-16 16:57 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Luis Gerhorst, Shuah Khan, bpf,
linux-kselftest, linux-kernel, Nuoqi Gui
check_stack_write_fixed_off() uses one byte-indexing scheme when checking
whether a fixed-offset stack write needs Spectre v4 sanitization, and another
scheme when recording the write into slot_type[].
For sub-8-byte writes this can make the sanitization check look at bytes that
are not overwritten by the write. A zeroed lower half-slot followed by a write
to the upper half-slot can therefore miss the nospec barrier for the second
write.
Use the same stack-byte index for the sanitization check and the slot update,
and add a focused verifier selftest that expects both half-slot writes to emit
nospec when the loader has CAP_BPF but not CAP_PERFMON.
Bounded impact: this fixes verifier/JIT Spectre v4 mitigation emission for a
fixed-offset stack-write corner case. No architectural verifier memory-safety
bypass, exploit chain, CVE, embargo, or security escalation is claimed.
Fixes: e4f4db47794c ("bpf: Fix pointer-leak due to insufficient speculative store bypass mitigation")
Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
---
Nuoqi Gui (2):
bpf: Fix stack slot index in nospec checks
selftests/bpf: Cover stack nospec slot indexing
kernel/bpf/verifier.c | 3 ++-
.../testing/selftests/bpf/progs/verifier_unpriv.c | 23 ++++++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
---
base-commit: e4287bf34f97a88c7d9322f5bde828724c073a6b
change-id: 20260615-f01-11-stack-nospec-slot-index-e155b2acd587
Best regards,
--
Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf 1/2] bpf: Fix stack slot index in nospec checks
2026-06-16 16:57 [PATCH bpf 0/2] bpf: Fix stack slot index for Spectre v4 nospec checks Nuoqi Gui
@ 2026-06-16 16:57 ` Nuoqi Gui
2026-06-16 21:11 ` Emil Tsalapatis
2026-06-17 7:41 ` Luis Gerhorst
2026-06-16 16:57 ` [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing Nuoqi Gui
1 sibling, 2 replies; 7+ messages in thread
From: Nuoqi Gui @ 2026-06-16 16:57 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Luis Gerhorst, Shuah Khan, bpf,
linux-kselftest, linux-kernel, Nuoqi Gui
check_stack_write_fixed_off() computes the byte slot for a fixed-offset
stack write as -off - 1, and records each written byte in slot_type[] with
(slot - i) % BPF_REG_SIZE.
The Spectre v4 sanitization pre-check uses slot_type[i] instead. For a
4-byte write at fp-8 after the lower half of fp-8 has been zeroed, the
pre-check scans bytes 0..3 and sees STACK_ZERO while the actual write updates
bytes 7..4. That can leave the second half-slot write without nospec_result
even though the bytes being overwritten still require sanitization.
Use the same slot index in the sanitization pre-check that the write path uses
when updating slot_type[].
Fixes: e4f4db47794c ("bpf: Fix pointer-leak due to insufficient speculative store bypass mitigation")
Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
---
kernel/bpf/verifier.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 2abc79dbf281c..50e80dbbc1784 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -3479,7 +3479,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
bool sanitize = reg && is_spillable_regtype(reg->type);
for (i = 0; i < size; i++) {
- u8 type = state->stack[spi].slot_type[i];
+ u8 type = state->stack[spi].slot_type[(slot - i) %
+ BPF_REG_SIZE];
if (type != STACK_MISC && type != STACK_ZERO) {
sanitize = true;
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing
2026-06-16 16:57 [PATCH bpf 0/2] bpf: Fix stack slot index for Spectre v4 nospec checks Nuoqi Gui
2026-06-16 16:57 ` [PATCH bpf 1/2] bpf: Fix stack slot index in " Nuoqi Gui
@ 2026-06-16 16:57 ` Nuoqi Gui
2026-06-16 21:14 ` Emil Tsalapatis
2026-06-17 8:45 ` Luis Gerhorst
1 sibling, 2 replies; 7+ messages in thread
From: Nuoqi Gui @ 2026-06-16 16:57 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Luis Gerhorst, Shuah Khan, bpf,
linux-kselftest, linux-kernel, Nuoqi Gui
Add a verifier test for the fixed-offset stack write case where two 4-byte
stores initialize opposite halves of the same stack slot.
The test uses the CAP_BPF-without-CAP_PERFMON loader lane so Spectre v4
mitigation remains active. It expects both half-slot writes to emit nospec
in the translated program.
Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
---
.../testing/selftests/bpf/progs/verifier_unpriv.c | 23 ++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/verifier_unpriv.c b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
index c16f8382cf17d..9ebbd4b531df1 100644
--- a/tools/testing/selftests/bpf/progs/verifier_unpriv.c
+++ b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
@@ -976,4 +976,27 @@ l0_%=: exit; \
: __clobber_all);
}
+SEC("socket")
+__description("noperfmon: Spectre v4 stack write slot index")
+__success __success_unpriv
+__caps_unpriv(CAP_BPF)
+__retval(0)
+#ifdef SPEC_V4
+__xlated_unpriv("r0 = 0")
+__xlated_unpriv("*(u32 *)(r10 -4) = r0")
+__xlated_unpriv("nospec")
+__xlated_unpriv("*(u32 *)(r10 -8) = r0")
+__xlated_unpriv("nospec")
+__xlated_unpriv("exit")
+#endif
+__naked void stack_write_nospec_slot_index(void)
+{
+ asm volatile (" \
+ r0 = 0; \
+ *(u32 *)(r10 - 4) = r0; \
+ *(u32 *)(r10 - 8) = r0; \
+ exit; \
+" ::: __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH bpf 1/2] bpf: Fix stack slot index in nospec checks
2026-06-16 16:57 ` [PATCH bpf 1/2] bpf: Fix stack slot index in " Nuoqi Gui
@ 2026-06-16 21:11 ` Emil Tsalapatis
2026-06-17 7:41 ` Luis Gerhorst
1 sibling, 0 replies; 7+ messages in thread
From: Emil Tsalapatis @ 2026-06-16 21:11 UTC (permalink / raw)
To: Nuoqi Gui, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Luis Gerhorst, Shuah Khan, bpf,
linux-kselftest, linux-kernel
On Tue Jun 16, 2026 at 12:57 PM EDT, Nuoqi Gui wrote:
> check_stack_write_fixed_off() computes the byte slot for a fixed-offset
> stack write as -off - 1, and records each written byte in slot_type[] with
> (slot - i) % BPF_REG_SIZE.
>
> The Spectre v4 sanitization pre-check uses slot_type[i] instead. For a
> 4-byte write at fp-8 after the lower half of fp-8 has been zeroed, the
> pre-check scans bytes 0..3 and sees STACK_ZERO while the actual write updates
> bytes 7..4. That can leave the second half-slot write without nospec_result
> even though the bytes being overwritten still require sanitization.
>
> Use the same slot index in the sanitization pre-check that the write path uses
> when updating slot_type[].
>
> Fixes: e4f4db47794c ("bpf: Fix pointer-leak due to insufficient speculative store bypass mitigation")
> Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
> ---
> kernel/bpf/verifier.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2abc79dbf281c..50e80dbbc1784 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -3479,7 +3479,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
> bool sanitize = reg && is_spillable_regtype(reg->type);
>
> for (i = 0; i < size; i++) {
> - u8 type = state->stack[spi].slot_type[i];
> + u8 type = state->stack[spi].slot_type[(slot - i) %
> + BPF_REG_SIZE];
>
> if (type != STACK_MISC && type != STACK_ZERO) {
> sanitize = true;
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing
2026-06-16 16:57 ` [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing Nuoqi Gui
@ 2026-06-16 21:14 ` Emil Tsalapatis
2026-06-17 8:45 ` Luis Gerhorst
1 sibling, 0 replies; 7+ messages in thread
From: Emil Tsalapatis @ 2026-06-16 21:14 UTC (permalink / raw)
To: Nuoqi Gui, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Luis Gerhorst, Shuah Khan, bpf,
linux-kselftest, linux-kernel
On Tue Jun 16, 2026 at 12:57 PM EDT, Nuoqi Gui wrote:
> Add a verifier test for the fixed-offset stack write case where two 4-byte
> stores initialize opposite halves of the same stack slot.
>
> The test uses the CAP_BPF-without-CAP_PERFMON loader lane so Spectre v4
> mitigation remains active. It expects both half-slot writes to emit nospec
> in the translated program.
>
> Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Trivial style nits.
> ---
> .../testing/selftests/bpf/progs/verifier_unpriv.c | 23 ++++++++++++++++++++++
> 1 file changed, 23 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/verifier_unpriv.c b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> index c16f8382cf17d..9ebbd4b531df1 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> @@ -976,4 +976,27 @@ l0_%=: exit; \
> : __clobber_all);
> }
>
> +SEC("socket")
> +__description("noperfmon: Spectre v4 stack write slot index")
> +__success __success_unpriv
> +__caps_unpriv(CAP_BPF)
> +__retval(0)
> +#ifdef SPEC_V4
> +__xlated_unpriv("r0 = 0")
> +__xlated_unpriv("*(u32 *)(r10 -4) = r0")
> +__xlated_unpriv("nospec")
> +__xlated_unpriv("*(u32 *)(r10 -8) = r0")
No whitespace to the right of -
> +__xlated_unpriv("nospec")
> +__xlated_unpriv("exit")
> +#endif
> +__naked void stack_write_nospec_slot_index(void)
> +{
> + asm volatile (" \
> + r0 = 0; \
> + *(u32 *)(r10 - 4) = r0; \
> + *(u32 *)(r10 - 8) = r0; \
Unaligned \
> + exit; \
> +" ::: __clobber_all);
> +}
> +
> char _license[] SEC("license") = "GPL";
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf 1/2] bpf: Fix stack slot index in nospec checks
2026-06-16 16:57 ` [PATCH bpf 1/2] bpf: Fix stack slot index in " Nuoqi Gui
2026-06-16 21:11 ` Emil Tsalapatis
@ 2026-06-17 7:41 ` Luis Gerhorst
1 sibling, 0 replies; 7+ messages in thread
From: Luis Gerhorst @ 2026-06-17 7:41 UTC (permalink / raw)
To: Nuoqi Gui
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, John Fastabend,
Martin KaFai Lau, Shuah Khan, bpf, linux-kselftest, linux-kernel
Nuoqi Gui <gnq25@mails.tsinghua.edu.cn> writes:
> check_stack_write_fixed_off() computes the byte slot for a fixed-offset
> stack write as -off - 1, and records each written byte in slot_type[] with
> (slot - i) % BPF_REG_SIZE.
>
> The Spectre v4 sanitization pre-check uses slot_type[i] instead. For a
> 4-byte write at fp-8 after the lower half of fp-8 has been zeroed, the
> pre-check scans bytes 0..3 and sees STACK_ZERO while the actual write updates
> bytes 7..4. That can leave the second half-slot write without nospec_result
> even though the bytes being overwritten still require sanitization.
>
> Use the same slot index in the sanitization pre-check that the write path uses
> when updating slot_type[].
>
> Fixes: e4f4db47794c ("bpf: Fix pointer-leak due to insufficient speculative store bypass mitigation")
> Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
> ---
> kernel/bpf/verifier.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2abc79dbf281c..50e80dbbc1784 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -3479,7 +3479,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
> bool sanitize = reg && is_spillable_regtype(reg->type);
>
> for (i = 0; i < size; i++) {
> - u8 type = state->stack[spi].slot_type[i];
> + u8 type = state->stack[spi].slot_type[(slot - i) %
> + BPF_REG_SIZE];
>
> if (type != STACK_MISC && type != STACK_ZERO) {
> sanitize = true;
Acked-by: Luis Gerhorst <luis.gerhorst@fau.de>
I have briefly checked the other uses of slot_type[i] and they look
fine.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing
2026-06-16 16:57 ` [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing Nuoqi Gui
2026-06-16 21:14 ` Emil Tsalapatis
@ 2026-06-17 8:45 ` Luis Gerhorst
1 sibling, 0 replies; 7+ messages in thread
From: Luis Gerhorst @ 2026-06-17 8:45 UTC (permalink / raw)
To: Nuoqi Gui, Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
John Fastabend, Martin KaFai Lau, Shuah Khan, bpf,
linux-kselftest, linux-kernel
Nuoqi Gui <gnq25@mails.tsinghua.edu.cn> writes:
> Add a verifier test for the fixed-offset stack write case where two 4-byte
> stores initialize opposite halves of the same stack slot.
>
> The test uses the CAP_BPF-without-CAP_PERFMON loader lane so Spectre v4
> mitigation remains active. It expects both half-slot writes to emit nospec
> in the translated program.
>
> Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
> ---
> .../testing/selftests/bpf/progs/verifier_unpriv.c | 23 ++++++++++++++++++++++
> 1 file changed, 23 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/verifier_unpriv.c b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> index c16f8382cf17d..9ebbd4b531df1 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> @@ -976,4 +976,27 @@ l0_%=: exit; \
> : __clobber_all);
> }
>
> +SEC("socket")
> +__description("noperfmon: Spectre v4 stack write slot index")
> +__success __success_unpriv
> +__caps_unpriv(CAP_BPF)
Not sure if the conditions that led Kartikeya and Eduard to add
__caps_unpriv(CAP_BPF) explicitly for some tests also apply here.
It seems adding it to this test will not provide much benefit because
the other tests in verifier_unpriv.c already assume
sysctl_unprivileged_bpf_disabled=0.
Because this already affects the more restricted 'unpriv process under
sysctl_unprivileged_bpf_disabled=0 without CAP_BPF' environment, I
suggest dropping __caps_unpriv(CAP_BPF) it.
> +__retval(0)
> +#ifdef SPEC_V4
> +__xlated_unpriv("r0 = 0")
> +__xlated_unpriv("*(u32 *)(r10 -4) = r0")
> +__xlated_unpriv("nospec")
> +__xlated_unpriv("*(u32 *)(r10 -8) = r0")
> +__xlated_unpriv("nospec")
> +__xlated_unpriv("exit")
> +#endif
> +__naked void stack_write_nospec_slot_index(void)
> +{
> + asm volatile (" \
> + r0 = 0; \
> + *(u32 *)(r10 - 4) = r0; \
> + *(u32 *)(r10 - 8) = r0; \
> + exit; \
> +" ::: __clobber_all);
> +}
> +
> char _license[] SEC("license") = "GPL";
Acked-by: Luis Gerhorst <luis.gerhorst@fau.de>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-06-17 8:52 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-16 16:57 [PATCH bpf 0/2] bpf: Fix stack slot index for Spectre v4 nospec checks Nuoqi Gui
2026-06-16 16:57 ` [PATCH bpf 1/2] bpf: Fix stack slot index in " Nuoqi Gui
2026-06-16 21:11 ` Emil Tsalapatis
2026-06-17 7:41 ` Luis Gerhorst
2026-06-16 16:57 ` [PATCH bpf 2/2] selftests/bpf: Cover stack nospec slot indexing Nuoqi Gui
2026-06-16 21:14 ` Emil Tsalapatis
2026-06-17 8:45 ` Luis Gerhorst
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox