* [PATCH bpf v3 0/2] bpf: bound arena allocations to the user VMA
@ 2026-11-22 11:28 YanXin Li via B4 Relay
2026-11-22 11:28 ` [PATCH bpf v3 1/2] " YanXin Li via B4 Relay
2026-11-22 11:28 ` [PATCH bpf v3 2/2] selftests/bpf: test arena allocation after a short mmap YanXin Li via B4 Relay
0 siblings, 2 replies; 4+ messages in thread
From: YanXin Li via B4 Relay @ 2026-11-22 11:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable
When map_extra is omitted, a short first mmap() can leave free arena
pages beyond the user VMA. Automatic allocation can select those pages;
near a 4 GiB boundary, the kernel mapping can extend past the arena's
vmalloc reservation. Patch 1 confines the range tree to the VMA, and
patch 2 adds a regression selftest.
---
Changes in v3:
- Correct the 288-page boundary calculation and describe the vmalloc guard.
- Distinguish out-of-VMA allocations from 32-bit wrap and limit claims to
what the local test establishes.
- Remove the unreachable arena_adjust_tree() bound check.
- Point Fixes at the range-tree conversion and identify the test commit.
- Split the regression selftest into patch 2/2 and skip it when arena
program loading returns -EOPNOTSUPP with JIT disabled.
- Link to v2: https://lore.kernel.org/r/20261122-b4-03-bpf-arena-vmalloc-oob-v2-5f6209c1a20e-v2-1-b2a9906e9676@pm.me
---
YanXin Li (2):
bpf: bound arena allocations to the user VMA
selftests/bpf: test arena allocation after a short mmap
kernel/bpf/arena.c | 25 ++++++++++
.../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++++++
.../testing/selftests/bpf/progs/arena_mmap_alloc.c | 32 +++++++++++++
3 files changed, 110 insertions(+)
---
base-commit: c44104f3642da720164042623e4832836aefdd8e
change-id: 20261122-b4-03-bpf-arena-vmalloc-oob-v3-da2dc21683c6-16baca6b3f21
Best regards,
--
YanXin Li <fadouse@pm.me>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf v3 1/2] bpf: bound arena allocations to the user VMA
2026-11-22 11:28 [PATCH bpf v3 0/2] bpf: bound arena allocations to the user VMA YanXin Li via B4 Relay
@ 2026-11-22 11:28 ` YanXin Li via B4 Relay
2026-11-22 11:28 ` [PATCH bpf v3 2/2] selftests/bpf: test arena allocation after a short mmap YanXin Li via B4 Relay
1 sibling, 0 replies; 4+ messages in thread
From: YanXin Li via B4 Relay @ 2026-11-22 11:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable
From: YanXin Li <fadouse@pm.me>
An arena map initializes its free range tree to max_entries pages. When
map_extra is zero, the first mmap() may cover fewer pages. Fixed-address
allocations are bounded by the VMA size, but automatic allocations use
range_tree_find() across the whole tree and can select a range beyond
the VMA. arena_free_pages() clips frees to the VMA, so these pages
cannot be released through the arena kfunc.
With 4 KiB pages, set max_entries to 1 << 20 and mmap() a 288-page VMA
at 0xffee0000, ending at the next 4 GiB boundary. A 287-page
automatic allocation takes offsets [0, 287). A subsequent 288-page
allocation starts at offset 287, so its kernel mapping starts at
kern_vm_start + 0xfffff000. One page lies within the 4 GiB data band,
eight lie in the arena's upper half-guard, and 279 lie beyond its
requested vmalloc area. The first of those 279 is the vmalloc guard
page. The remaining 278 lie beyond the full reservation. The current
apply_to_page_range() path can install PTEs there. An unpatched local
test returned a non-NULL pointer for this allocation. Another run
triggered a WARNING in apply_range_set_cb() on a non-scratch PTE.
Out-of-VMA offsets need not wrap the low 32 bits. For example, when
the user VMA starts at a 4 GiB aligned address, they can still map
inside the arena's kernel area. They remain invalid because the VMA
does not cover them. The range-tree conversion replaced the bounded
mtree_alloc_range() search with range_tree_find() over max_entries.
The later switch from vm_area_map_pages() to apply_to_page_range()
removed the vm_struct bounds check, allowing these selections to
reach beyond the arena's vmalloc reservation.
On the first mmap() without map_extra, remove [vma_pages, max_entries)
from the free range tree before publishing the VMA bounds. The VMA
size is fixed after this mmap(), and the fault, reserve and free paths
do not add offsets outside it. Automatic allocations consequently
stay inside the VMA.
This issue was found through LLM-assisted code audit and manual code
inspection of kernel/bpf/arena.c, then checked with a Z3 SMT model and
a custom BPF test in an x86-64 QEMU/KVM guest. The test kernel
identified itself as 7.4.0-rc3-84074-gc44104f3642d. KASAN, UBSAN,
lockdep and DEBUG_OBJECTS were enabled. After the patch, the
288-page request returns NULL, the 287-page in-VMA request succeeds,
and the test reports no kernel warnings.
Fixes: b795379757eb ("bpf: Introduce range_tree data structure and use it in bpf arena")
Cc: stable@vger.kernel.org
Signed-off-by: YanXin Li <fadouse@pm.me>
---
kernel/bpf/arena.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index ec2bda1a6eb..94321dec638 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -694,6 +694,31 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
if (remember_vma(arena, vma))
return -ENOMEM;
+ if (!arena->user_vm_start && vma_pages(vma) < map->max_entries) {
+ /*
+ * Without map_extra, the first mmap() can be shorter than
+ * max_entries. Remove offsets outside the VMA from the free
+ * range tree so automatic allocations stay within the VMA.
+ */
+ unsigned long flags;
+ int ret = -EBUSY;
+
+ if (!raw_res_spin_lock_irqsave(&arena->spinlock, flags)) {
+ ret = range_tree_clear(&arena->rt, vma_pages(vma),
+ map->max_entries - vma_pages(vma));
+ raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+ WARN_ON_ONCE(ret);
+ }
+ if (ret) {
+ struct vma_list *vml = vma->vm_private_data;
+
+ list_del(&vml->head);
+ vma->vm_private_data = NULL;
+ kfree(vml);
+ return ret;
+ }
+ }
+
arena->user_vm_start = vma->vm_start;
arena->user_vm_end = vma->vm_end;
/*
--
Git-157)
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf v3 2/2] selftests/bpf: test arena allocation after a short mmap
2026-11-22 11:28 [PATCH bpf v3 0/2] bpf: bound arena allocations to the user VMA YanXin Li via B4 Relay
2026-11-22 11:28 ` [PATCH bpf v3 1/2] " YanXin Li via B4 Relay
@ 2026-11-22 11:28 ` YanXin Li via B4 Relay
2026-11-22 12:11 ` bot+bpf-ci
1 sibling, 1 reply; 4+ messages in thread
From: YanXin Li via B4 Relay @ 2026-11-22 11:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel
From: YanXin Li <fadouse@pm.me>
Without map_extra, the first mmap of an arena can be shorter than the
map's max_entries. Automatic allocations must stay within that VMA.
Create an eight-page arena and mmap only four pages before loading a BPF
program. Reuse the map FD so libbpf does not mmap the full arena. The
program occupies offset zero and allocates two more pages, leaving one
page within the VMA. A two-page request must fail, and a final one-page
request must succeed. Before the preceding fix, the two-page request
succeeds outside the VMA.
When JIT is disabled, arena map creation can succeed while loading a
program that uses the map returns -EOPNOTSUPP. Skip the subtest in that
case.
Signed-off-by: YanXin Li <fadouse@pm.me>
---
.../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++++++
.../testing/selftests/bpf/progs/arena_mmap_alloc.c | 32 +++++++++++++
2 files changed, 85 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c
index f55d950720f..ff1fe0a6f22 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c
@@ -2,6 +2,7 @@
#include <test_progs.h>
#include <sys/mman.h>
+#include "arena_mmap_alloc.skel.h"
#define ARENA_PAGES 8
@@ -34,6 +35,56 @@ static void test_refused(__u64 map_extra, __u64 addr, int pages, int flags, int
close(fd);
}
+static void test_short_mmap_alloc(void)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, map_opts, .map_flags = BPF_F_MMAPABLE);
+ LIBBPF_OPTS(bpf_test_run_opts, run_opts);
+ struct arena_mmap_alloc *skel = NULL;
+ size_t size = 4 * getpagesize();
+ void *area;
+ int fd, err;
+
+ fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0,
+ ARENA_PAGES, &map_opts);
+ if (fd == -EOPNOTSUPP) {
+ test__skip();
+ return;
+ }
+ if (!ASSERT_GE(fd, 0, "bpf_map_create"))
+ return;
+
+ /* The short mapping must be the first mmap() of this arena. */
+ area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (!ASSERT_NEQ(area, MAP_FAILED, "short_mmap"))
+ goto out_close;
+
+ /* Reuse the map so libbpf does not mmap all ARENA_PAGES on load. */
+ skel = arena_mmap_alloc__open();
+ if (!ASSERT_OK_PTR(skel, "arena_mmap_alloc__open"))
+ goto out_unmap;
+ err = bpf_map__reuse_fd(skel->maps.arena, fd);
+ if (!ASSERT_OK(err, "reuse_arena_fd"))
+ goto out_destroy;
+ err = arena_mmap_alloc__load(skel);
+ if (err == -EOPNOTSUPP) {
+ test__skip();
+ goto out_destroy;
+ }
+ if (!ASSERT_OK(err, "arena_mmap_alloc__load"))
+ goto out_destroy;
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.alloc_beyond_vma),
+ &run_opts);
+ if (ASSERT_OK(err, "alloc_beyond_vma"))
+ ASSERT_OK(run_opts.retval, "alloc_beyond_vma_retval");
+
+out_destroy:
+ arena_mmap_alloc__destroy(skel);
+out_unmap:
+ munmap(area, size);
+out_close:
+ close(fd);
+}
+
void serial_test_arena_mmap(void)
{
__u64 page = getpagesize(), last = -(ARENA_PAGES * page);
@@ -50,4 +101,6 @@ void serial_test_arena_mmap(void)
test_refused(0, -page, ARENA_PAGES, MAP_FIXED, 0);
if (test__start_subtest("map_extra_last_pages"))
test_refused(last, last, ARENA_PAGES, MAP_FIXED, 0);
+ if (test__start_subtest("short_mmap_alloc"))
+ test_short_mmap_alloc();
}
diff --git a/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c
new file mode 100644
index 00000000000..3854afbebc4
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_arena_common.h"
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 8);
+} arena SEC(".maps");
+
+SEC("syscall")
+int alloc_beyond_vma(void *ctx)
+{
+ void __arena *page;
+
+ /* A successful allocation at offset zero can return a NULL pointer. */
+ bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0);
+ if (!page)
+ return 1;
+ /* Only one of the four mmap()ed pages remains available. */
+ page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0);
+ if (page)
+ return 2;
+ page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ return page ? 0 : 3;
+}
+
+char _license[] SEC("license") = "GPL";
--
Git-157)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH bpf v3 2/2] selftests/bpf: test arena allocation after a short mmap
2026-11-22 11:28 ` [PATCH bpf v3 2/2] selftests/bpf: test arena allocation after a short mmap YanXin Li via B4 Relay
@ 2026-11-22 12:11 ` bot+bpf-ci
0 siblings, 0 replies; 4+ messages in thread
From: bot+bpf-ci @ 2026-11-22 12:11 UTC (permalink / raw)
To: fadouse, ast, daniel, andrii, eddyz87, memxor
Cc: martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai, bpf,
linux-kernel, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1957 bytes --]
> selftests/bpf: test arena allocation after a short mmap
TL;DR: Test-only nit: the comment in arena_mmap_alloc.c claiming an
offset-zero allocation can return NULL looks inaccurate, so the first
allocation's result could be checked instead of ignored.
> diff --git a/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c
> new file mode 100644
> index 000000000000..3854afbebc45
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c
[ ... ]
> +SEC("syscall")
> +int alloc_beyond_vma(void *ctx)
> +{
> + void __arena *page;
> +
> + /* A successful allocation at offset zero can return a NULL pointer. */
> + bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
> + page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0);
This isn't a bug, but is the comment about offset zero accurate?
arena_alloc_pages() returns the full user address:
addr = clear_lo32(arena->user_vm_start) + uaddr32;
For pgoff 0 this is arena->user_vm_start, which cannot be zero since
arena_map_mmap() sets it from vma->vm_start. The kfunc return is treated
as an unknown scalar by the verifier and the JIT does not truncate it, so
"if (!page)" is a full 64-bit compare.
The offset-0-looks-like-NULL behavior seems to apply to nullable kfunc
arguments (see progs/arena_kfunc.c), not to this return value. Also,
prog_tests/arena_mem_usage.c asserts ASSERT_OK_PTR() on an offset-0
allocation.
Could the first result be checked like the others, for example with
"if (!page) return 4;", so an allocation failure here is distinguishable
from a VMA-bound regression, and the comment dropped or corrected?
> + if (!page)
> + return 1;
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45018621389
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-22 12:11 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 11:28 [PATCH bpf v3 0/2] bpf: bound arena allocations to the user VMA YanXin Li via B4 Relay
2026-11-22 11:28 ` [PATCH bpf v3 1/2] " YanXin Li via B4 Relay
2026-11-22 11:28 ` [PATCH bpf v3 2/2] selftests/bpf: test arena allocation after a short mmap YanXin Li via B4 Relay
2026-11-22 12:11 ` bot+bpf-ci
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®