From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65ED014F9FB for ; Sat, 11 Apr 2026 17:57:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775930267; cv=none; b=MRkSZAU2AIRoQi3BkvgW4RZxKAC7MvoCxXtxpuXY4wojWtcC66Lysp4TET4dg3vej+/JZpUK+yJbiTBCPAxv7Qcn/S9dXcsMCZJfbo6NekngA+OcnApHOr4SPEmH3gj/33je3WK89bEdF5ibf5ZquNeOOQ1JWF56DIvfaQI1SGk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775930267; c=relaxed/simple; bh=E/oCcltPyzlPnCkL55rjOxGmdoQRnDYMKn4gBhhUPf4=; h=Mime-Version:Content-Type:Date:Message-Id:To:Cc:Subject:From: References:In-Reply-To; b=qrxm8pOVgkodE/mNAGA7f+DLBV0vTaStWoLze/d+6Br2rfp/w9TQ+5Sn3VApF1Qi9uNTb0/cjfCxuO9VboRHtwOPysES+5pjxQMUZh7RAq1sTOoc0pvNj5cFj5Zhq2tHsCguDIdpIp0KTF/+FnQ34VGA1K81aq/WYcCKIdU0qL8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com; spf=pass smtp.mailfrom=etsalapatis.com; dkim=pass (2048-bit key) header.d=etsalapatis-com.20251104.gappssmtp.com header.i=@etsalapatis-com.20251104.gappssmtp.com header.b=S1rKNtzQ; arc=none smtp.client-ip=209.85.214.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=etsalapatis-com.20251104.gappssmtp.com header.i=@etsalapatis-com.20251104.gappssmtp.com header.b="S1rKNtzQ" Received: by mail-pl1-f196.google.com with SMTP id d9443c01a7336-2addb31945aso19025935ad.1 for ; Sat, 11 Apr 2026 10:57:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=etsalapatis-com.20251104.gappssmtp.com; s=20251104; t=1775930266; x=1776535066; darn=vger.kernel.org; h=in-reply-to:references:from:subject:cc:to:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=X51QS7PASdXj1x6AvsFaKG/XafN68EBd8f1SSAhlh8E=; b=S1rKNtzQxmrqIzSFG4qvllUXxfXoDzxMvbDdR15xFffohvqK03YyL8q7+sdLBFyTjH sl2Z0Y+Pi0+Tlh6CHIpaFs/fvP9resdjFf/+0+ZA7PNZHSN7JN2ANhLxWm6W++ff+In9 RzEDWQpJBUmQyhPzyn7hr31sPOWfa5CuSA6qRQKFfqSjh26p+XaCry6YLQ5KcQL35J4L wuTmELDqfEBeTJd4AxdCZoC90+Ejs15Sj25qSp2ktW2fbFEfN/oQbkgh4nRl7Eg3l8Hb e9BDkx0sRFHQ3CDeHjrvlMnEbJEIlA5ae9p3B2vAYP+VbTlXKNCE99uTvribgqigu8IV HfBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775930266; x=1776535066; h=in-reply-to:references:from:subject:cc:to:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=X51QS7PASdXj1x6AvsFaKG/XafN68EBd8f1SSAhlh8E=; b=k4qBb1XOlDVSPIC3r4Prs4KU6B1V6IkdLeWwTTBes2ZQdxRjlndNe4Qo2yW/syukrT 3Uyq7xBhuDrjGTa88JF3/wHLCD0P9Fvaad60N4gvvv3Ems1y2Wj+3dHrbSV4cG+mnHL9 jYxEKhSU+C31bZmv4vvBxF4B1VWc99+nl7k5pptIINxeBbu2vQOzD50PiYcfLa6fKqdr oy0uf6oZ+jfZ07pwZ/q55EuXzsP6cSvgZwaBovYuuw5HNYa3Lsomyh1wCjkqZcSqZc4I LmkMFXSpfJnZkAQ2WXbj2o/e+jylRBqe8VtdwHYZv/XDo1+1IhJJOtBB8vy/szNOzwz5 KpQQ== X-Forwarded-Encrypted: i=1; AFNElJ+CBYLv1pMxjCEKVtKmuMl0W6954SseMDhV0DzDY3eRwrhcCoCr4upU2aIWlqGUdrHPIULU+vrqUJ/XrA0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1qx62lZABzJFRHK8BScWY7SpRZuueth0c1IMYCORd0g5w1SEx Fp06MgSWkacxwavzh6mVcLR93+u5iXVC/X6H6J7WtLnSNt1kELSY/9EnP7rckA2qtII= X-Gm-Gg: AeBDievDDjoRP64genGgx3/3LsHGTaqd4KKgucVeEXkP5LSq/ePw4LR04fK56ERzfvb IZPzAj/GRmEI/wcskdNUntIbTJTrOwo/+mHyeqy+MAx4VoeQryNDWd7r6ZTZUz7VpYlnDdfNXwG iUQnD7vG9RiFVh85bn39T8Qyemtq6hRZr1bbUXB1DNDfWhytZSpShyHBBH51Rf4vsr4CXcTaydq y0odtnLmiS9nvyhdGae7vTWP9DAy7FX9ziIN2KSVLZZYDXdI6neAsDEwMfogeF3iS8yzF5MBYTS 8zsqe0AlsgVFKaPzCTQuYaSwfOjMgZt3anorwlMtemH40kwU3v6NIKZslT3VdFLt6D8wcO92RBv cfsxTe1E5tK/l7lJiiAfO7LKo0rT7aj7dCX4QTxWp4AGg0KahqlxEgCETBUNUXfS/Ku8W6kb8o6 kmx9FiOW8= X-Received: by 2002:a17:902:8348:b0:2b0:c59f:3b58 with SMTP id d9443c01a7336-2b2d5944c41mr56964315ad.9.1775930265731; Sat, 11 Apr 2026 10:57:45 -0700 (PDT) Received: from localhost ([2604:3d08:487d:cd00::5517]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2d4f43994sm66371055ad.80.2026.04.11.10.57.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 11 Apr 2026 10:57:45 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sat, 11 Apr 2026 13:57:44 -0400 Message-Id: To: "Weiming Shi" , "Alexei Starovoitov" , "Daniel Borkmann" , "Andrii Nakryiko" Cc: "Martin KaFai Lau" , "Eduard Zingerman" , "Song Liu" , "Yonghong Song" , "John Fastabend" , "KP Singh" , "Stanislav Fomichev" , "Hao Luo" , "Jiri Olsa" , "Barret Rhoden" , , , "Xiang Mei" Subject: Re: [PATCH bpf v2 1/2] bpf: Fix use-after-free of arena VMA on fork From: "Emil Tsalapatis" X-Mailer: aerc 0.21.0-0-g5549850facc2 References: <20260411112944.1455470-1-bestswngs@gmail.com> <20260411112944.1455470-2-bestswngs@gmail.com> In-Reply-To: <20260411112944.1455470-2-bestswngs@gmail.com> 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:22= 34) > Call Trace: > > 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 > > > 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 !=3D > 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 > Signed-off-by: Weiming Shi > --- > 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.=20 > }; > =20 > 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, stru= ct vm_area_struct *vma) > vml =3D kmalloc_obj(*vml); > if (!vml) > return -ENOMEM; > - refcount_set(&vml->mmap_count, 1); > vma->vm_private_data =3D vml; > vml->vma =3D vma; > list_add(&vml->head, &arena->vma_list); > @@ -336,9 +334,28 @@ static int remember_vma(struct bpf_arena *arena, str= uct vm_area_struct *vma) > =20 > static void arena_vm_open(struct vm_area_struct *vma) > { > + struct bpf_map *map =3D vma->vm_file->private_data; > + struct bpf_arena *arena =3D container_of(map, struct bpf_arena, map); > struct vma_list *vml =3D vma->vm_private_data; > =20 > - 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 !=3D vma) > + vma->vm_private_data =3D NULL; > + > + if (vma->vm_private_data) > + return; > + > + vml =3D kmalloc_obj(*vml); > + if (!vml) > + return; The following is exactly remember_vma()'s code, reuse it. > + vml->vma =3D vma; > + vma->vm_private_data =3D vml; > + guard(mutex)(&arena->lock); > + list_add(&vml->head, &arena->vma_list); > } > =20 > [snip] Also go to sashiko.dev and address/explain the feedback from there, it seems valid.=20