BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vineet Gupta" <vineet.gupta@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: Thu, 10 Sep 2026 17:08:30 +0000	[thread overview]
Message-ID: <20260910170830.DFC551F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260910164635.459558-6-vineet.gupta@linux.dev>

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.

[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() {
    ...
	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].

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?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260910164635.459558-1-vineet.gupta@linux.dev?part=5

  reply	other threads:[~2026-09-10 17:08 UTC|newest]

Thread overview: 39+ 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-12 18:50   ` Alexei Starovoitov
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-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-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 [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-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-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-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=20260910170830.DFC551F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vineet.gupta@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