Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795054613; b=lJDIldryQED8/BNSvPrAch7lqbDNnIEmclP5hyaf3x1kqIVKByoOY6fWSCBrryBRh7b1 6ujPUrY8jT2My6wpw3vQfK+IgAY3CxbsuALXuXTlAMWq5wUHR1ELO13h1OxgRtLe4pJ/j xhDwei/qd2RTBqEYDB19HqeS4BDpwWQ0pjgdMYI9EM05JuO2O7BctshQJXjM9ZAXawxxf eRoFfwSemLzcOFI4BTt6hE/iBNNdG4j2E2o1XddtYukqOB6nmqwadHd42pU1VM22UT+hV gaw3rN2IhIzcZXgjWJ4+l2Uo4/+rYFj/kPvld+9o3OivXg7IkZBN41SHOlOrPkASPDw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795054613; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=3d0GjGdWpSEJJyW0HuVhgS+W41VxKzqJ3N81K/GLMQE=; b=nOOxCwl7DjIJ3v9qFH4DaqcOLMYHEBIQnP8+5Pmfu3GcilCORnkVgQ/FY6nKSaq1NfxD PKJwD03zGxisxwp01CpJqhggtc8utyvzfe/3ts2tzsgP3PtmTH+KULy5iyi0ZpidJNnAq wXBKqLvLieDIPoEPcGWzKR71JRk5aJbo3wefrKEgSqeJ3/6Mx4hw5CFhZmazqwePiMJM2 pew4O1Gt+MBo+FXA/V97s6tWVk0IKUMyPZd44myQpP5j76tuW8Qzc/VqsB92hFnKT0Abu l3sDBerOE/FBZ0avszYmvNwf064ZNwfiYR8GbVIvVZq0puEDmFmC9AGskKxe4UvMhjw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=zv4eUX0l; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76434606A31C; Thu, 19 Nov 2026 02:16:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=zv4eUX0l Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCD101F000C4; Thu, 19 Nov 2026 02:16:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795054613; bh=3d0GjGdWpSEJJyW0HuVhgS+W41VxKzqJ3N81K/GLMQE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=zv4eUX0l4ggM162yEKNjzeSk8XdGQeBgi/7O5zunWR6b9k0dx3Zux+P++AoLyyL60 FacvDBHLb5Ri3xDzaqvoxAIMxzcQvtu0IEV6olX+R/+zcfZi1CnOcHZo6nXbYFaOnH qI9Py3Y+FXl+iJiTZMGx7E2UWQ5XJtQEdfNRUi1E= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Josef Bacik , "Lorenzo Stoakes (ARM)" , Rik van Riel , "Vlastimil Babka (SUSE)" , Barry Song , Pedro Falcato , "Harry Yoo (Meta)" , "Kiryl Shutsemau (Meta)" , SJ Park , Muhammad Usama Anjum , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 5.15 1287/2214] fork: take mmap_lock before copying mm_struct Date: Wed, 18 Nov 2026 15:49:23 +0100 Message-ID: <20261118150103.060392377@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150009.797899913@linuxfoundation.org> References: <20261118150009.797899913@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) [ Upstream commit ef694e2d210111cc08c52fd25da9ab130170bfa3 ] dup_mm() copies the parent's mm_struct before dup_mmap() takes the mmap_lock. A concurrent brk() can leave the child with a stale mm->brk and newer heap mappings, causing later brk() requests to fail. Other operations (e.g., PR_SET_MM) on the parent's mm_struct can similarly cause inconsistencies. As a simple and robust fix, grab the parent's mmap_lock earlier. This way we don't need to update individual mm fields in dup_mmap() and keep missing ones. Stop updating the 4 existing members (total_vm, data_vm, exec_vm, stack_vm), as that is now no longer required. Keep releasing the parent's mmap_lock in dup_mmap(), before userfaultfd completion, to avoid moving the userfaultfd handling into the caller. This should get cleaned up later. Move uprobe_start_dup_mmap() to preserve the lock ordering. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Reported-by: Josef Bacik Reviewed-by: Lorenzo Stoakes (ARM) Reviewed-by: Rik van Riel Reviewed-by: Vlastimil Babka (SUSE) Reviewed-by: Barry Song Reviewed-by: Pedro Falcato Reviewed-by: Harry Yoo (Meta) Reviewed-by: Kiryl Shutsemau (Meta) Reviewed-by: SJ Park Tested-by: Muhammad Usama Anjum Link: https://patch.msgid.link/20261031-fixes_core-v3-2-d6437171ecc3@kernel.org Signed-off-by: David Hildenbrand (Arm) [ sashal: Adjusted caller cleanup to avoid a second uprobe release after dup_mmap() has already released it. ] Signed-off-by: Sasha Levin --- kernel/fork.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/kernel/fork.c b/kernel/fork.c index 87c08297fbee6..1f7f0f9c821c8 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -501,11 +501,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, unsigned long charge; LIST_HEAD(uf); - uprobe_start_dup_mmap(); - if (mmap_write_lock_killable(oldmm)) { - retval = -EINTR; - goto fail_uprobe_end; - } + mmap_assert_write_locked(oldmm); flush_cache_dup_mm(oldmm); uprobe_dup_mmap(oldmm, mm); /* @@ -516,11 +512,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, /* No ordering required: file already has been exposed. */ dup_mm_exe_file(mm, oldmm); - mm->total_vm = oldmm->total_vm; - mm->data_vm = oldmm->data_vm; - mm->exec_vm = oldmm->exec_vm; - mm->stack_vm = oldmm->stack_vm; - rb_link = &mm->mm_rb.rb_node; rb_parent = NULL; pprev = &mm->mmap; @@ -628,7 +619,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, flush_tlb_mm(oldmm); mmap_write_unlock(oldmm); dup_userfaultfd_complete(&uf); -fail_uprobe_end: uprobe_end_dup_mmap(); return retval; fail_nomem_anon_vma_fork: @@ -656,7 +646,7 @@ static inline void mm_free_pgd(struct mm_struct *mm) #else static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) { - mmap_write_lock(oldmm); + mmap_assert_write_locked(oldmm); dup_mm_exe_file(mm, oldmm); mmap_write_unlock(oldmm); return 0; @@ -1467,10 +1457,18 @@ static struct mm_struct *dup_mm(struct task_struct *tsk, if (!mm) goto fail_nomem; + uprobe_start_dup_mmap(); + if (mmap_write_lock_killable(oldmm)) { + free_mm(mm); + goto fail_uprobe; + } + memcpy(mm, oldmm, sizeof(*mm)); - if (!mm_init(mm, tsk, mm->user_ns)) - goto fail_nomem; + if (!mm_init(mm, tsk, mm->user_ns)) { + mmap_write_unlock(oldmm); + goto fail_uprobe; + } err = dup_mmap(mm, oldmm); if (err) @@ -1489,7 +1487,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk, mm->binfmt = NULL; mm_init_owner(mm, NULL); mmput(mm); + return NULL; +fail_uprobe: + uprobe_end_dup_mmap(); fail_nomem: return NULL; } -- 2.53.0