Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 279F0364935 for ; Tue, 13 Oct 2026 00:58:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791853127; cv=none; b=KLMDXPgkeBG0YaYmrEzRVTQAiNFvj50EY8xjflab0T8EGz0qqG8gZcyQLmbDXZynx+BI6Yh//vx3cRQEVMzM7IF67MKWk8gdz8jpcYWkOlqOYnomwJFMMNW4s786pmddb2mq4qKBiC5VfjFTGlDZFD50ZxFS7YM38uYwxsKixZs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791853127; c=relaxed/simple; bh=heWcCHPmqXzlQ5yQRsra+9y1HwWq7w4MR38QyLtFc44=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fl2AegSvADLQpJz3rSI7PhtS4OMUDAGUbwOnKYlkfwfopkzOMz7UFLZEwBSMpHe79Xu5SaV84WIARV8ETOkgOFwzyFL4UquXEpc0mqawXvzokltWRlAV8JZhO/wNAyylYnqpGHLDCUrepieDP6UWkn8+jH2MhCrz9S/5ocrYwhE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WMFnafwh; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WMFnafwh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6921D1F0089B; Tue, 13 Oct 2026 00:58:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1791853125; bh=lS+noAx85Og/x7eKYSiZng6mGQ0RegZIQQSA9qUfR+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WMFnafwhW2J89v1rgl+YSOKy9Ux6Fzy1IZq81wXBRdgNmo48Qj3mavedIBfKaELd6 vDXRUY+FJmWGq2QROtsgSd5B61GrlCLX7Rr8ZONpc8ozYADpSItGSCRveS1HrafQLm 1kQEAotgnFXWvwKdIpwXsc5ziT2YJ53T/x2AnVE8+XzSrmsVc0dVeNEK2q3cLUSiXE cS6Gw+qjc+KB7SOEyD2HymXS71g6iLqgaUKInqJYyboRLRUn/ty6FheJwSVLRV0SEn HwQrM6oliIw24b+RFUSmbi8LaEwub6MJMU2LyffuM4O+rJb3LJnQSLuE5oewdG4Bkc nO5HUe3ZDiT3Q== From: Sasha Levin To: stable@vger.kernel.org Cc: "Lorenzo Stoakes (ARM)" , Andrew Morton , Pedro Falcato , "Kiryl Shutsemau (Meta)" , "Jose A. Perez de Azpillaga" , Anirudh Srinivasan , "Liam R. Howlett" , Vlastimil Babka , Jann Horn , Brian Geffon , Minchan Kim , Sasha Levin Subject: [PATCH 5.10.y] mm/mremap: fix locked_vm leak by splitting VMA for MREMAP_DONTUNMAP Date: Mon, 12 Oct 2026 20:58:43 -0400 Message-ID: <20261013005814.2346821-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026101247-outage-wrinkly-2ff5@gregkh> References: <2026101247-outage-wrinkly-2ff5@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Lorenzo Stoakes (ARM)" [ Upstream commit 597e4b9ab48a3f841080f627722064853d3f650e ] The MREMAP_DONTUNMAP feature is highly unusual in that it permits mremap() operations that keep the original VMA in place. Historically this has led to a lot of bugs where non-obvious interactions occur between existing mremap() operations and the original VMA. Fix another of these - partial copies. The long-standing mremap() partial VMA logic has the baked-in assumption that the originating VMA is unmapped and thus moved. However MREMAP_DONTUNMAP defeats this by performing a partial copy instead since it keeps the source VMA around. An mremap(..., MREMAP_DONTUNMAP) operation disallows resizing of the VMA, but the operation can be performed partially: |-----------------| | | | v <------> <------> .new_sz. new_sz |--.------.--| |------| | .source. | | dest | |--.------.--| |------| <------------> old_sz The page tables in the specified range are moved, but the original VMA is kept intact. This interacts poorly with mlock()'d VMAs, as the VMA_LOCKED_BIT flag is cleared for the entire source VMA and set for the entire destination VMA. This results in an mm->locked_vm leak as the change is therefore not accounted correctly. The clear solution here is to make the portion of the source VMA which is mremap()'d distinct from the rest of it, a.k.a. split it. Therefore resolve this issue by splitting it ahead of the rest of the mremap() operation if the VMA is mlock()'d. This is valid, as the source VMA will lose its VMA_LOCKED_BIT flag, so if a partial remap it will become distinct from the rest of the VMA. In order to make this change re-expose split_vma() in vma.h for CONFIG_MMU (nommu doesn't compile mremap.c and uses a static helper instead). Finally, update the sys_map_count check to account for this case. Note that the early check does not use needs_pre_split() - this is because the VMA has not been looked up by this point, so be conservative and assume that the VMA is mlock()'d in this case for the purposes of the sys_map_count check. Link: https://lore.kernel.org/20260930-fix-dontunmap-partial-self-merge-v2-2-f388985a0f0a@kernel.org Fixes: e346b3813067 ("mm/mremap: add MREMAP_DONTUNMAP to mremap()") Signed-off-by: Lorenzo Stoakes (ARM) Signed-off-by: Andrew Morton Reviewed-by: Pedro Falcato Acked-by: Kiryl Shutsemau (Meta) Reviewed-by: Jose A. Perez de Azpillaga Tested-by: Anirudh Srinivasan Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Jann Horn Cc: Brian Geffon Cc: Minchan Kim Cc: [ sashal: Replaced upstream VMA preparation helpers with inline logic in move_vma() using the existing split_vma(mm, ...) API. ] Signed-off-by: Sasha Levin --- mm/mremap.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mm/mremap.c b/mm/mremap.c index 3334c40222101..8d8b39f821380 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -366,6 +366,19 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (err) return err; + /* + * To account mlock()'d pages correctly in the MREMAP_DONTUNMAP + * case perform any split ahead of time for an mlock()'d VMA. + */ + if ((flags & MREMAP_DONTUNMAP) && (vma->vm_flags & VM_LOCKED)) { + if (vma->vm_start != old_addr) + err = split_vma(mm, vma, old_addr, 1); + if (!err && vma->vm_end != old_addr + old_len) + err = split_vma(mm, vma, old_addr + old_len, 0); + if (err) + return err; + } + new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, &need_rmap_locks); -- 2.53.0