BPF List
 help / color / mirror / Atom feed
From: Vineet Gupta <vineet.gupta@linux.dev>
To: sashiko-reviews@lists.linux.dev
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 12/13] bpf: track low-32 scalar equality across narrowing stack spills
Date: Tue, 15 Sep 2026 15:08:22 -0700	[thread overview]
Message-ID: <b1f31312-0f2d-4965-9bd4-d27a11b9d9fb@linux.dev> (raw)
In-Reply-To: <20260910170600.0DFBA1F00893@smtp.kernel.org>

On 9/10/26 10:05 AM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Critical] Sign-extending loads from 32-bit narrowing spills incorrectly inherit and preserve a `SUBREG_ZEXT` link, causing a critical discrepancy between verifier bounds and runtime values.
> --
>
> commit 3396576ba1c56571ccd9a040716058594f558da8
> Author: Vineet Gupta <vineet.gupta@linux.dev>
>
> bpf: track low-32 scalar equality across narrowing stack spills
>
> This commit allows the verifier to track a SUBREG_ZEXT relationship when a
> 64-bit scalar is narrowed during a 4-byte stack spill. This preserves the
> knowledge of the lower 32 bits for subsequent loads.
>
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index 6cb35fc0d0fb0..9e63eddb9023b 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
> [ ... ]
>> @@ -3678,8 +3687,15 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
>>   			 * it came from.
>>   			 */
>>   			coerce_reg_to_size(&state->stack[spi].spilled_ptr, size);
>> -			/* Break the relation on a narrowing spill. */
>> -			clear_scalar_id(&state->stack[spi].spilled_ptr);
>> +			if (subreg_link && reg->id)
>> +				state->stack[spi].spilled_ptr.subreg = SUBREG_ZEXT;
> [Severity: Critical]
> Can this lead to verifier state corruption when the spilled value is later
> read with a 4-byte sign-extending load (BPF_MEMSX)?
>
> If a BPF program performs 64-bit arithmetic to create an unknown scalar and
> spills it to the stack via a 4-byte narrowing store, it records a SUBREG_ZEXT
> link here and coerces its bounds to 32 bits.
>
> If it then reads it back via a 4-byte sign-extending load, when processing
> the read in check_stack_read_fixed_off(), narrowing evaluates to false since
> the bounds were already truncated during the spill:
>
> 	bool narrowing = get_reg_width(reg) > size * BITS_PER_BYTE;
>
> As a result, subreg_link is false, and the verifier skips the
> is_ldsx ? SUBREG_SEXT : SUBREG_ZEXT correction logic. The destination
> register blindly inherits the stack slot's SUBREG_ZEXT link.
>
> In check_mem_access(), coerce_reg_to_size_sx() properly sign-extends the
> loaded value's bounds, but because the subreg link is non-zero, it skips
> breaking the scalar ID:
>
> 	if (!no_sext && !regs[value_regno].subreg)
> 		clear_scalar_id(&regs[value_regno]);
>
> The register now has sign-extended bounds but a SUBREG_ZEXT link to the
> original 64-bit scalar.
>
> If the original scalar is later refined, sync_linked_regs() will invoke
> reconstruct_zext32(), completely overwriting the sign-extended bounds with
> zero-extended bounds. The verifier will incorrectly assume the register holds
> a positive value, while at runtime it holds a negative sign-extended value.
>
> Does this permit arbitrary out-of-bounds kernel memory accesses?

Yes there's a bug here. A narrowing spill records SUBREG_ZEXT, 
truncating the slot, and a later 4-byte ldsx has narrowing == false, 
inherits the slot's SUBREG_ZEXT  leaving sign-extended bounds under a 
zero-extending link.

Fixed by making the sign-extension path drop any link that is not 
already SUBREG_SEXT:

   -   if (!no_sext && !regs[value_regno].subreg)
   +   if (!no_sext && regs[value_regno].subreg != SUBREG_SEXT)
               clear_scalar_id(&regs[value_regno]);

Also added a test to that effect.

Thx,
-Vineet

>
>> +			else
>> +				/*
>> +				 * Nothing to relate: either the source has no
>> +				 * id to share, or the store is narrower than
>> +				 * the 32 bits a link can describe.
>> +				 */
>> +				clear_scalar_id(&state->stack[spi].spilled_ptr);
>>   		}


  reply	other threads:[~2026-09-15 22:08 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10 16:46 [PATCH bpf-next v2 00/13] bpf: track scalar equality across the low 32 bits Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 01/13] bpf: move linked-scalar flags out of bpf_reg_state->id [NFC] Vineet Gupta
2026-09-10 17:00   ` sashiko-bot
2026-09-11  6:56     ` Vineet Gupta
2026-09-10 17:52   ` bot+bpf-ci
2026-09-14 18:02     ` Vineet Gupta
2026-09-16 21:03       ` Alexei Starovoitov
2026-09-16 21:21         ` Vineet Gupta
2026-09-18 23:38         ` Vineet Gupta
2026-09-12 18:50   ` Alexei Starovoitov
2026-09-15  1:17     ` Vineet Gupta
2026-09-16 21:02       ` Alexei Starovoitov
2026-09-16 21:24         ` Vineet Gupta
2026-09-17  0:36           ` Alexei Starovoitov
2026-10-02  8:03             ` Vineet Gupta
2026-10-02 11:23               ` Alexei Starovoitov
2026-09-18 23:13       ` Eduard Zingerman
2026-09-21 18:44         ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 02/13] bpf: compare linked-scalar kinds in regs_exact() Vineet Gupta
2026-09-12 18:51   ` Alexei Starovoitov
2026-09-15  1:11     ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 03/13] bpf: track low-32 scalar equality across zero-extending movs Vineet Gupta
2026-09-10 17:52   ` bot+bpf-ci
2026-09-11  9:29     ` Vineet Gupta
2026-09-12 18:59   ` Alexei Starovoitov
2026-09-15 20:31     ` Vineet Gupta
2026-09-16  4:23       ` Alexei Starovoitov
2026-09-17  0:08         ` Vineet Gupta
2026-09-17  0:30           ` Alexei Starovoitov
2026-09-21 17:28             ` Eduard Zingerman
2026-09-21 18:59               ` Alexei Starovoitov
2026-09-21 19:10                 ` Eduard Zingerman
2026-09-21 19:27                   ` Alexei Starovoitov
2026-09-21 19:44                     ` Eduard Zingerman
2026-09-21 21:55                       ` Alexei Starovoitov
2026-09-21 22:17                         ` Eduard Zingerman
2026-09-21 22:25                           ` Alexei Starovoitov
2026-10-02 13:59                             ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 04/13] selftests/bpf: cover the low-32 link for " Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 05/13] bpf: keep the range across a sign extension that cannot change it Vineet Gupta
2026-09-10 17:08   ` sashiko-bot
2026-09-15 21:52     ` Vineet Gupta
2026-09-10 17:52   ` bot+bpf-ci
2026-09-11 10:37     ` Vineet Gupta
2026-09-12 19:02   ` Alexei Starovoitov
2026-09-15 21:18     ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 06/13] selftests/bpf: cover sign extensions that cannot change the range Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 07/13] bpf: track low-32 scalar equality across sign-extending movs Vineet Gupta
2026-09-10 17:52   ` bot+bpf-ci
2026-09-11 10:00     ` Vineet Gupta
2026-09-12 19:09   ` Alexei Starovoitov
2026-09-15 20:39     ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 08/13] selftests/bpf: cover the low-32 link for " Vineet Gupta
2026-09-10 17:52   ` bot+bpf-ci
2026-09-11  8:00     ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 09/13] bpf: track low-32 scalar equality across narrowing stack fills Vineet Gupta
2026-09-10 17:04   ` sashiko-bot
2026-09-11  6:07     ` Vineet Gupta
2026-09-11  6:43       ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 10/13] selftests/bpf: cover the low-32 link for " Vineet Gupta
2026-09-10 17:00   ` sashiko-bot
2026-09-11  5:34     ` Vineet Gupta
2026-09-10 17:31   ` bot+bpf-ci
2026-09-11  5:07     ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 11/13] bpf: record what a narrowing spill actually stores Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 12/13] bpf: track low-32 scalar equality across narrowing stack spills Vineet Gupta
2026-09-10 17:05   ` sashiko-bot
2026-09-15 22:08     ` Vineet Gupta [this message]
2026-09-10 16:46 ` [PATCH bpf-next v2 13/13] selftests/bpf: cover the low-32 link for " Vineet Gupta

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=b1f31312-0f2d-4965-9bd4-d27a11b9d9fb@linux.dev \
    --to=vineet.gupta@linux.dev \
    --cc=bpf@vger.kernel.org \
    --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