BPF List
 help / color / mirror / Atom feed
From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: <sashiko-reviews@lists.linux.dev>
Cc: <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf v1 1/2] bpf: Reject non-scalar bpf_loop iteration counts
Date: Sat, 05 Sep 2026 07:56:20 +0200	[thread overview]
Message-ID: <DL75GB02N34V.3IAIUPZPJO4YW@gmail.com> (raw)
In-Reply-To: <20260905015702.5ED241F00A3D@smtp.kernel.org>

On Sat Sep 5, 2026 at 3:57 AM CEST, sashiko-bot wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>
> Pre-existing issues:
> - [High] Missing negative bounds checks for enum array indices in memcg BPF kfuncs (bpf_mem_cgroup_memory_events and bpf_mem_cgroup_vm_events).
> --
>
> commit 3f8d69cede626ae022617ccb30f54b8f41f90edd
> Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
>
> bpf: Reject non-scalar bpf_loop iteration counts
>
> This commit fixes a verifier warning and subsequent panic triggered when
> pointer values are passed to the bpf_loop() nr_loops argument. It
> introduces ARG_SCALAR to ensure generic helper argument validation
> rejects pointers before loop inlining and precision processing.
>
> Link: https://lore.kernel.org/bpf/6a9ad24c.b5d4176b.238c3e.0001.GAE@google.com/
>
>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
>> index 3a7eb2185c354..591dfab3e706c 100644
>> --- a/include/linux/bpf.h
>> +++ b/include/linux/bpf.h
>> @@ -894,6 +894,7 @@ enum bpf_arg_type {
>>
>>  	ARG_PTR_TO_CTX,		/* pointer to context */
>>  	ARG_ANYTHING,		/* any (initialized) argument is ok */
>> +	ARG_SCALAR,		/* scalar argument */
>
> [Severity: High]
> This isn't a bug introduced by this patch, but while reviewing the addition
> of ARG_SCALAR and how the verifier handles scalar arguments, a pre-existing
> missing bounds check was noticed in the memory cgroup kfuncs.
>
> Could negative enum values passed by untrusted BPF programs cause an
> out-of-bounds read in the memcg BPF kfuncs?
>
> Looking at 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]);
>
> And similarly in mm/bpf_memcontrol.c:bpf_mem_cgroup_vm_events(), which
> exposes validation logic from mm/memcontrol.c:memcg_vm_event_item_valid():
>
> 	if (idx >= NR_VM_EVENT_ITEMS)
> 		return false;
>
> 	return !BAD_STAT_IDX(memcg_events_index(idx));
>
> Because the BPF verifier enforces scalar types but does not validate enum
> ranges, a BPF program can pass a negative integer as the event parameter.
> The bounds checks use signed comparisons without a lower bound (< 0) check,
> so negative values will bypass the validation (e.g., -1 >= 4 evaluates to
> false).
>

My understanding is that since enum values are non-neggative, the enum type is
treated unsigned by default. But I think it might make sense to just cast
explicitly since Sashiko keeps repeating this concern and it is probably clearer
for a reader (I forgot enum values would be unsigned when all enum constants are
non-negative myself).

> Can this lead to an out-of-bounds array access on kernel memory when the
> negative value is used as an index into memcg->memory_events?


  reply	other threads:[~2026-09-05  5:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-05  1:47 [PATCH bpf v1 0/2] Fix bpf_loop syzbot report Kumar Kartikeya Dwivedi
2026-09-05  1:47 ` [PATCH bpf v1 1/2] bpf: Reject non-scalar bpf_loop iteration counts Kumar Kartikeya Dwivedi
2026-09-05  1:57   ` sashiko-bot
2026-09-05  5:56     ` Kumar Kartikeya Dwivedi [this message]
2026-09-06  3:46   ` Eduard Zingerman
2026-09-05  1:47 ` [PATCH bpf v1 2/2] selftests/bpf: Test pointer bpf_loop iteration count rejection Kumar Kartikeya Dwivedi
2026-09-06  3:45   ` Eduard Zingerman
2026-09-06  4:10 ` [PATCH bpf v1 0/2] Fix bpf_loop syzbot report patchwork-bot+netdevbpf

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=DL75GB02N34V.3IAIUPZPJO4YW@gmail.com \
    --to=memxor@gmail.com \
    --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