Sched_ext development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ziyang Men" <ziyang.meme@gmail.com>
Cc: tj@kernel.org, bpf@vger.kernel.org, mkoutny@suse.com,
	sched-ext@lists.linux.dev, dmitry.torokhov@gmail.com,
	linux-input@vger.kernel.org, hannes@cmpxchg.org
Subject: Re: [PATCH v4 2/4] selftests/cgroup: add memcg_stat_cross_cpu correctness test for flush
Date: Wed, 09 Sep 2026 00:05:27 +0000	[thread overview]
Message-ID: <20260909000528.531611F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260908235552.2610256-3-ziyang.meme@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing bounds check for negative enum values in memcg BPF kfuncs (`bpf_mem_cgroup_memory_events` and `bpf_mem_cgroup_vm_events`), leading to potential out-of-bounds array reads.
--

commit 75dfb2e14e4c5552066e9fdeb8f96969135f991d
Author: Ziyang Men <ziyang.meme@gmail.com>

selftests/cgroup: add memcg_stat_cross_cpu correctness test for flush

Add test_memcg_stat_cross_cpu, which verifies that flushing a memcg
subtree produces the same statistics through the BPF memcg kfuncs and
cgroupfs.

> diff --git a/tools/testing/selftests/cgroup/memcg_stat_cross_cpu.bpf.c b/tools/testing/selftests/cgroup/memcg_stat_cross_cpu.bpf.c
> new file mode 100644
> index 0000000000000..c3c69ecd888f0
> --- /dev/null
> +++ b/tools/testing/selftests/cgroup/memcg_stat_cross_cpu.bpf.c
> @@ -0,0 +1,95 @@
[ ... ]
> +int memcg_kfuncs_probe(struct bpf_iter__cgroup *ctx)
> +{
> +	struct cgroup *cgrp = ctx->cgroup;
> +	struct mem_cgroup *memcg;
> +
> +	if (!cgrp)
> +		return 0;
> +
> +	memcg = bpf_get_mem_cgroup(&cgrp->self);
> +	if (!memcg)
> +		return 0;
> +
> +	bpf_mem_cgroup_flush_stats(memcg);
> +	bpf_mem_cgroup_page_state(memcg, 0);
> +	bpf_mem_cgroup_vm_events(memcg, 0);

[Severity: High]
This isn't a bug introduced by this patch, but can this kfunc and related
memcg kfuncs be exploited by passing negative enum values to read
out-of-bounds memory?

In mm/bpf_memcontrol.c, the bpf_mem_cgroup_vm_events() kfunc takes an enum
event parameter:

mm/bpf_memcontrol.c:bpf_mem_cgroup_vm_events() {
	if (unlikely(!memcg_vm_event_item_valid(event)))
		return (unsigned long)-1;
	...
}

The helper memcg_vm_event_item_valid() in mm/memcontrol.c uses a signed
comparison that doesn't catch negative values:

mm/memcontrol.c:memcg_vm_event_item_valid() {
	if (idx >= NR_VM_EVENT_ITEMS)
		return false;

	return !BAD_STAT_IDX(memcg_events_index(idx));
}

Similarly, bpf_mem_cgroup_memory_events() lacks a negative check:

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

Since the BPF verifier treats enums as plain integers and doesn't enforce
their valid ranges, a negative index can bypass these validations and lead to
a backward out-of-bounds read from the target array.

> +	bpf_put_mem_cgroup(memcg);
> +	return 0;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260908235552.2610256-1-ziyang.meme@gmail.com?part=2

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

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-08 23:55 [PATCH v4 0/4] selftests: add shared Makefile for BPF selftests and new memcg test Ziyang Men
2026-09-08 23:55 ` [PATCH v4 1/4] selftests: add shared lib.bpf.mk to build BPF progs and skeletons Ziyang Men
2026-09-09  0:02   ` sashiko-bot
2026-09-09 18:26   ` Tejun Heo
2026-09-08 23:55 ` [PATCH v4 2/4] selftests/cgroup: add memcg_stat_cross_cpu correctness test for flush Ziyang Men
2026-09-09  0:05   ` sashiko-bot [this message]
2026-09-08 23:55 ` [PATCH v4 3/4] selftests/hid: build the BPF program via the shared lib.bpf.mk Ziyang Men
2026-09-08 23:55 ` [PATCH v4 4/4] selftests/sched_ext: build BPF schedulers " Ziyang Men

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=20260909000528.531611F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-input@vger.kernel.org \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=ziyang.meme@gmail.com \
    /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