Re: [PATCH bpf v2 1/2] bpf: Fix use-after-free of arena VMA on fork

Emil Tsalapatis
2026-04-11T17:57:46Z

On Sat Apr 11, 2026 at 7:29 AM EDT, Weiming Shi wrote:
> arena_vm_open() only increments a refcount on the shared vma_list entry
> but never registers the new VMA or updates the stored vma pointer. When
> the original VMA is unmapped while a forked/split copy still exists,
> arena_vm_close() drops the refcount without freeing the vma_list entry.
> The entry's vma pointer now refers to a freed vm_area_struct. A
> subsequent bpf_arena_free_pages() call iterates vma_list and passes
> the dangling pointer to zap_page_range_single(), causing a
> use-after-free.
>
> The bug is reachable by any process with CAP_BPF and CAP_PERFMON that
> can create a BPF_MAP_TYPE_ARENA, mmap it, and fork. It triggers
> deterministically -- no race condition is involved.
>
> BUG: KASAN: slab-use-after-free in zap_page_range_single (mm/memory.c:2234)
> Call Trace:
> <TASK>
> zap_page_range_single+0x101/0x110 mm/memory.c:2234
> zap_pages+0x80/0xf0 kernel/bpf/arena.c:658
> arena_free_pages+0x67a/0x860 kernel/bpf/arena.c:712
> bpf_prog_test_run_syscall+0x3da net/bpf/test_run.c:1640
> __sys_bpf+0x1662/0x50b0 kernel/bpf/syscall.c:6267
> __x64_sys_bpf+0x73/0xb0 kernel/bpf/syscall.c:6360
> do_syscall_64+0xf1/0x530 arch/x86/entry/syscall_64.c:63
> entry_SYSCALL_64_after_hwframe+0x77 arch/x86/entry/entry_64.S:130
> </TASK>
>
> Fix this by giving each VMA its own vma_list entry, following the
> HugeTLB vma_lock pattern (hugetlb_vm_op_open). arena_vm_open() now

I'm not a fan of this framing. We're not "following the HugeTLB vma_lock
pattern", we're (reasonably) tracking the VMA of the child separately by
registering it when arena_vm_open is called for it during fork().

> detects an inherited vm_private_data pointer via the vma_lock->vma !=
> vma check, clears it, and allocates a fresh entry for the new VMA.
> arena_vm_close() unconditionally removes and frees the entry. The
> shared refcount is no longer needed and is removed.
>
> Fixes: b90d77e5fd78 ("bpf: Fix remap of arena.")
> Reported-by: Xiang Mei <xmei5@asu.edu>
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>
> ---
> Changes since v1:
> - Added missing Reported-by tag
>
> kernel/bpf/arena.c | 26 +++++++++++++++++++++-----
> 1 file changed, 21 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> index f355cf1c1a16..3a156ec473a8 100644
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c
> @@ -317,7 +317,6 @@ static u64 arena_map_mem_usage(const struct bpf_map *map)
> struct vma_list {
> struct vm_area_struct *vma;
> struct list_head head;
> - refcount_t mmap_count;

This is reasonable for arenas if we track each VMA separately.

> };
>
> static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
> @@ -327,7 +326,6 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
> vml = kmalloc_obj(*vml);
> if (!vml)
> return -ENOMEM;
> - refcount_set(&vml->mmap_count, 1);
> vma->vm_private_data = vml;
> vml->vma = vma;
> list_add(&vml->head, &arena->vma_list);
> @@ -336,9 +334,28 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
>
> static void arena_vm_open(struct vm_area_struct *vma)
> {
> + struct bpf_map *map = vma->vm_file->private_data;
> + struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
> struct vma_list *vml = vma->vm_private_data;
>
> - refcount_inc(&vml->mmap_count);
> + /*
> + * If vm_private_data points to a vma_list for a different VMA, it was
> + * inherited via vm_area_dup (fork or split). Clear it and allocate a

Arena mappings should never be split because that throws all the
arithmetic in arena_vm_fault() off (the pgoff for the mapping to
the right is now counted from the split point). We need to implement
may_split() for the vma struct ops and just return -EINVAL to prevent it.

> + * fresh entry for this VMA, following the HugeTLB vma_lock pattern.

Remove references to the "HugeTLB pattern".

> + */
> + if (vml && vml->vma != vma)
> + vma->vm_private_data = NULL;
> +
> + if (vma->vm_private_data)
> + return;
> +
> + vml = kmalloc_obj(*vml);
> + if (!vml)
> + return;

The following is exactly remember_vma()'s code, reuse it.

> + vml->vma = vma;
> + vma->vm_private_data = vml;
> + guard(mutex)(&arena->lock);
> + list_add(&vml->head, &arena->vma_list);
> }
>

> [snip]

Also go to sashiko.dev and address/explain the feedback from there, it
seems valid.