From: sashiko-bot@kernel.org
To: "Suchit Karunakaran" <suchitkarunakaran@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH] selftests/bpf: Fix map_lookup_percpu_elem on sparse CPU IDs
Date: Tue, 01 Sep 2026 14:36:29 +0000 [thread overview]
Message-ID: <20260901143629.A64601F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260901141954.29449-1-suchitkarunakaran@gmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The BPF helper bpf_map_lookup_percpu_elem and its map-specific implementations do not verify that the requested CPU is a possible CPU, allowing out-of-bounds kernel memory reads. The patch exposes this preexisting kernel bug by changing the test to loop over `nr_cpu_ids` and call the helper on non-possible CPUs, leading to arbitrary memory reads and test failures.
--
commit c6b22dcf947502ea445cd52bb9052f6450168c31
Author: Suchit Karunakaran <suchitkarunakaran@gmail.com>
selftests/bpf: Fix map_lookup_percpu_elem on sparse CPU IDs
This commit updates the map_lookup_percpu_elem selftest to handle sparse CPU
topologies by reading the possible CPU mask and explicitly iterating up to
nr_cpu_ids instead of the total count of possible CPUs.
> diff --git a/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
> index ca827b1092daa..d8da0696b97cf 100644
> --- a/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
> +++ b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
[ ... ]
> @@ -57,17 +57,17 @@ int sysenter_getuid(const void *ctx)
>
> map_ctx.map = &percpu_array_map;
> map_ctx.sum = 0;
> - bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
> + bpf_loop(nr_cpu_ids, read_percpu_elem_callback, &map_ctx, 0);
[Severity: High]
This is a pre-existing issue, but does changing this loop to iterate up to
nr_cpu_ids expose a kernel bug on sparse topologies?
When the BPF program loops over non-possible CPU IDs, it passes them into
bpf_map_lookup_percpu_elem(). The map-specific lookup implementations, such
as percpu_array_map_lookup_percpu_elem() in kernel/bpf/arraymap.c and the
hash map equivalents in kernel/bpf/hashtab.c, check if the CPU exceeds
nr_cpu_ids, but they do not verify if the CPU is actually possible:
kernel/bpf/arraymap.c:percpu_array_map_lookup_percpu_elem() {
...
if (cpu >= nr_cpu_ids)
return NULL;
...
}
Without a cpu_possible(cpu) check, passing a non-possible CPU ID causes the
kernel helper to retrieve an unmapped base pointer.
When read_percpu_elem_callback() receives this pointer, it expects a NULL
return for non-possible CPUs. Instead, it gets a valid-looking pointer to
unmapped memory. Can dereferencing this pointer crash the kernel?
Should the kernel helpers be updated to validate cpu_possible() before the
selftest is modified to intentionally trigger this path?
> percpu_array_elem_sum = map_ctx.sum;
>
> map_ctx.map = &percpu_hash_map;
> map_ctx.sum = 0;
> - bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
> + bpf_loop(nr_cpu_ids, read_percpu_elem_callback, &map_ctx, 0);
> percpu_hash_elem_sum = map_ctx.sum;
>
> map_ctx.map = &percpu_lru_hash_map;
> map_ctx.sum = 0;
> - bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
> + bpf_loop(nr_cpu_ids, read_percpu_elem_callback, &map_ctx, 0);
> percpu_lru_hash_elem_sum = map_ctx.sum;
>
> return 0;
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260901141954.29449-1-suchitkarunakaran@gmail.com?part=1
next prev parent reply other threads:[~2026-09-01 14:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-01 14:19 [PATCH] selftests/bpf: Fix map_lookup_percpu_elem on sparse CPU IDs Suchit Karunakaran
2026-09-01 14:36 ` sashiko-bot [this message]
2026-09-01 15:34 ` bot+bpf-ci
2026-09-01 16:42 ` Suchit Karunakaran
2026-09-03 0:32 ` Andrii Nakryiko
2026-09-03 5:29 ` Suchit Karunakaran
2026-09-03 16:01 ` Andrii Nakryiko
2026-09-03 16:57 ` Suchit Karunakaran
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=20260901143629.A64601F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=suchitkarunakaran@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.