BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hongfu Li" <hongfu.li@linux.dev>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"JP Kobryn" <jp.kobryn@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
	"Hongfu Li" <lihongfu@kylinos.cn>
Subject: Re: [PATCH] mm/bpf: Add bpf_mem_cgroup_memory_events_local() kfunc
Date: Wed, 11 Nov 2026 07:06:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-208494@kernel.org> (raw)
In-Reply-To: <20261111-for-bpf_memctl-1111-v1-1-8b56784d9e5a@kylinos.cn>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] mm: out-of-bounds read in bpf_mem_cgroup_memory_events()
  [1]
--

commit 19b0cb61e45dc1f33944548e84f3a699079d3e85
Author: Hongfu Li <lihongfu@kylinos.cn>

mm/bpf: Add bpf_mem_cgroup_memory_events_local() kfunc

This commit adds the bpf_mem_cgroup_memory_events_local() kfunc to allow BPF
programs to read a memory cgroup's local memory event counters.

> diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c
> index d8f579c285600..826f711718dbe 100644
> --- a/mm/bpf_memcontrol.c
> +++ b/mm/bpf_memcontrol.c
> @@ -133,6 +133,23 @@ __bpf_kfunc unsigned long bpf_mem_cgroup_memory_events(struct mem_cgroup *memcg,
>  	return atomic_long_read(&memcg->memory_events[event]);
>  }
>  
> +/**
> + * bpf_mem_cgroup_memory_events_local - Read memory cgroup's local memory event
> + * @memcg: memory cgroup
> + * @event: memory event id
> + *
> + * Return: The current value of the local memory event counter.
> + */
> +__bpf_kfunc unsigned long
> +bpf_mem_cgroup_memory_events_local(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_local[event]);
> +}

[Severity: Critical]
This problem wasn't introduced by this patch, but in the existing
bpf_mem_cgroup_memory_events() right above [1] (and in the new
bpf_mem_cgroup_memory_events_local() here), can a negative event value pass
the bounds check and read out of bounds of memory_events[]?

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]);
}

The BPF verifier does not enforce enum bounds on kfunc arguments, so a BPF
program can pass a negative value for event.  If enum memcg_memory_event is a
signed type, event >= MEMCG_NR_MEMORY_EVENTS performs a signed comparison that
evaluates to false for negative numbers, indexing before the start of
memcg->memory_events[] and disclosing adjacent kernel memory or crashing on an
unmapped page.  Checking event < 0 || event >= MEMCG_NR_MEMORY_EVENTS (or
casting event to unsigned int) would reject negative values.

[1] https://sashiko.dev/#/bug/linux-77d600fe-b74d-4e53-9ee2-707d37cb8052

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261111-for-bpf_memctl-1111-v1-1-8b56784d9e5a@kylinos.cn?part=1

  reply	other threads:[~2026-11-11  7:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  6:56 [PATCH] mm/bpf: Add bpf_mem_cgroup_memory_events_local() kfunc Hongfu Li
2026-11-11  7:06 ` sashiko-bot [this message]
2026-11-11 15:44 ` Alexei Starovoitov

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-208494@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bpf@vger.kernel.org \
    --cc=hongfu.li@linux.dev \
    --cc=jp.kobryn@linux.dev \
    --cc=lihongfu@kylinos.cn \
    --cc=linux-mm@kvack.org \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@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