BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Luis Gerhorst" <luis.gerhorst@fau.de>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf 4/4] selftests/bpf: Test nospec after stack writes of calls and atomics
Date: Wed, 25 Nov 2026 17:45:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-233854@kernel.org> (raw)
In-Reply-To: <254382ea54ff319c0dcf8c93642f5850b276b549.1795625597.git.luis.gerhorst@fau.de>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selftests/bpf: missing SPEC_V1/SPEC_V4 guards on nospec jump
  range tests
--

commit 6fc5e1de114ef93cc1978b1912c33448c16a6ee8
Author: Luis Gerhorst <luis.gerhorst@fau.de>

selftests/bpf: Test nospec after stack writes of calls and atomics

This commit adds BPF verifier selftests covering Spectre v1 and v4 nospec
insertion after helper and atomic stack writes, dead speculative stack
writes, backward jumps, and 16-bit jump offset overflow when inserting
nospec instructions.

> diff --git a/tools/testing/selftests/bpf/progs/verifier_unpriv.c b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> index a54b5a58b945b..12d5e9e668909 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_unpriv.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_unpriv.c
[ ... ]
> @@ -1072,4 +1137,140 @@ __naked void nospec_backward_call(void)
>  "	::: __clobber_all);
>  }
>  
> +/*
> + * The jump is reached with a pointer in r6 on a speculative path only, so a
> + * nospec is required in front of it. Its offset is the smallest one that a
> + * jump can have, and the nospec is between the jump and its target: the offset
> + * can not be adjusted for the nospec.
> + *
> + * The program is valid, it is only rejected because the verifier can not widen
> + * a jump when it patches a program. Any insn that is inserted between such a
> + * jump and its target has the same effect. Without the nospec the program
> + * would not be safe, so it must not be loaded. If jumps can be widened one day,
> + * this program has to be accepted.
> + *
> + * The insns that are only executed speculatively are removed as dead code, so
> + * they must not be between the jump and its target.
> + */
> +SEC("socket")
> +__description("unpriv: nospec does not fit in front of a jump")
> +__success __failure_unpriv
> +__msg_unpriv("cannot be patched due to 16-bit range")
> +__caps_unpriv(CAP_BPF)
> +__retval(0)
> +__naked void nospec_jump_out_of_range(void)
> +{
[ ... ]
> +}
> +
> +/*
> + * Like above, but for the nospec behind a store to the stack that is between
> + * the jump and its target.
> + */
> +SEC("socket")
> +__description("unpriv: nospec does not fit behind a store")
> +__success __failure_unpriv
> +__msg_unpriv("cannot be patched due to 16-bit range")
> +__caps_unpriv(CAP_BPF)
> +__retval(0)
> +__naked void nospec_result_jump_out_of_range(void)

[Severity: Medium]
Should nospec_jump_out_of_range() and nospec_result_jump_out_of_range()
guard their __failure_unpriv expectations with #ifdef SPEC_V1 and
#ifdef SPEC_V4 respectively?

Because __caps_unpriv(CAP_BPF) is set, the unprivileged subtests run on
every architecture with CONFIG_CPU_MITIGATIONS=y.  The backward jump in
each test has an initial offset of -32768 and only overflows 16 bits when
the verifier inserts a nospec instruction.

In nospec_result_jump_out_of_range(), check_stack_write_fixed_off() sets
nospec_result only when !env->bypass_spec_v4.  On architectures where
bpf_jit_bypass_spec_v4() returns true (such as arm64 and loongarch), no
nospec is inserted, so the unprivileged program loads and fails the test
with unexpected_load_success.  Similarly, nospec_jump_out_of_range()
relies on a Spectre v1 nospec that is skipped when
bpf_jit_bypass_spec_v1() returns true (such as on loongarch).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795625597.git.luis.gerhorst@fau.de?part=4

  reply	other threads:[~2026-11-25 17:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  4:08 [PATCH bpf 0/5] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 1/5] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 2/5] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 3/5] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 4/5] bpf: Emit nospec after BPF_ATOMIC stores to the stack Alexei Starovoitov
2026-11-25 17:12   ` Luis Gerhorst
2026-11-25 17:12     ` [PATCH bpf 0/4] bpf: Emit nospec after every insn that " 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:51         ` sashiko-bot
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
2026-11-25 17:39         ` sashiko-bot
2026-11-25 17:12       ` [PATCH bpf 3/4] bpf: Emit nospec after every insn with nospec_result 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
2026-11-25 17:45         ` sashiko-bot [this message]
2026-11-26  3:40     ` [PATCH bpf 4/5] bpf: Emit nospec after BPF_ATOMIC stores to the stack Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 5/5] bpf: Do not end a speculative path at a call Alexei Starovoitov
2026-11-25  5:00   ` bot+bpf-ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233854@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=luis.gerhorst@fau.de \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox