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 05/13] bpf: keep the range across a sign extension that cannot change it
Date: Tue, 15 Sep 2026 14:52:02 -0700	[thread overview]
Message-ID: <d2f54ff4-436a-4513-90b7-d1c9e1f76298@linux.dev> (raw)
In-Reply-To: <20260910170830.DFC551F000FF@smtp.kernel.org>

On 9/10/26 10:08 AM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
>
> New issues:
> - [Medium] Incomplete fix for bounds precision loss on zero-straddling sign extensions in the verifier.
>
> Pre-existing issues:
> - [High] Out-of-bounds array read in BPF memory cgroup kfuncs due to bypassable enum bounds check.
> --
>
> commit 971a50869c707d4ac0627b912bd2423344987acc
> Author: Vineet Gupta <vineet.gupta@linux.dev>
>
> bpf: keep the range across a sign extension that cannot change it
>
> This commit improves the verifier's handling of sign extensions in
> coerce_reg_to_size_sx() by recognizing when a register's bounds already fit
> within the target field. This prevents unnecessary precision loss when a value
> range straddles zero, allowing valid programs to pass verification.
>
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index 58e788f53ae58..eb093194e2a36 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
> [ ... ]
>
>> @@ -5773,6 +5774,27 @@ static void coerce_reg_to_size_sx(struct bpf_reg_state *reg, int size)
>>   		return;
>>   	}
>>   
>> +	if (size == 1) {
>> +		field_smin = S8_MIN;
>> +		field_smax = S8_MAX;
>> +	} else if (size == 2) {
>> +		field_smin = S16_MIN;
>> +		field_smax = S16_MAX;
>> +	} else {
>> +		/* size == 4 */
>> +		field_smin = S32_MIN;
>> +		field_smax = S32_MAX;
>> +	}
>> +
>> +	/*
>> +	 * The range already fits the field, so (sN)v == v for every value the
>> +	 * register can hold and the sign extension changes nothing. The tests
>> +	 * below cannot reach this case once smin is negative: a negative smin
>> +	 * and a non-negative smax never share their high bits.
>> +	 */
>> +	if (reg_smin(reg) >= field_smin && reg_smax(reg) <= field_smax)
>> +		return;
>> +
>>   	top_smax_value = ((u64)reg_smax(reg) >> num_bits) << num_bits;
>>   	top_smin_value = ((u64)reg_smin(reg) >> num_bits) << num_bits;
> [Severity: Medium]
> Does the 32-bit counterpart function coerce_subreg_to_size_sx() need the same
> fix?
>
> It seems to retain the same precision loss flaw when handling 32-bit sign
> extensions on ranges that straddle zero (e.g., [-4095, 0]). Valid BPF programs
> using 32-bit MOVSX on such ranges will needlessly lose bounds precision and
> might be incorrectly rejected by the verifier.

Good catch. Fixed now. It would only need to handle sizes 1 and 2 though.


> [Severity: High]
> This is a pre-existing issue, but I noticed a potential out-of-bounds array
> read in the BPF memory cgroup kfuncs.
>
> Enums are treated as scalars by the verifier and can be negative. The bounds
> checks in these functions use single-sided signed comparisons, which bypass
> negative values:
>
> mm/bpf_memcontrol.c:bpf_mem_cgroup_memory_events() {

True signature has enum type

   __bpf_kfunc unsigned long bpf_mem_cgroup_memory_events(struct 
mem_cgroup *memcg,
                                                          enum 
memcg_memory_event event)

>      ...
> 	if (unlikely(event >= MEMCG_NR_MEMORY_EVENTS))
> 		return (unsigned long)-1;
>
> 	return atomic_long_read(&memcg->memory_events[event]);
> }
>
> If a BPF program passes -1, the check evaluates to false, leading to an
> out-of-bounds read on memcg->memory_events[-1].

Not an issue. enum doesn't have any negative values and with enum tye in 
signature above it will treated as unsigned int so -1 will come out as 
large number not -1.

> A similar issue exists in bpf_mem_cgroup_vm_events(), where the bounds check
> delegates to memcg_vm_event_item_valid():
>
> mm/memcontrol.c:memcg_vm_event_item_valid() {
>      ...
> 	if (idx >= NR_VM_EVENT_ITEMS)
> 		return false;
>      ...
> }
>
> This also performs a single-sided check and fails to catch negative indices.
> Should these be updated to explicitly check for negative values as well?

Same as above since this enum doesn't have negative entries either.

Thx,
-Vineet

  reply	other threads:[~2026-09-15 21:52 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 [this message]
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
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=d2f54ff4-436a-4513-90b7-d1c9e1f76298@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