From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E52C1D44162 for ; Tue, 19 Nov 2024 14:25:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7DE4A6B009F; Tue, 19 Nov 2024 09:25:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 78F466B00A0; Tue, 19 Nov 2024 09:25:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 656BD6B00A1; Tue, 19 Nov 2024 09:25:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 46E366B009F for ; Tue, 19 Nov 2024 09:25:30 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DEC6A804ED for ; Tue, 19 Nov 2024 14:25:29 +0000 (UTC) X-FDA: 82803065016.13.2B058E1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id 970D7A0002 for ; Tue, 19 Nov 2024 14:24:36 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=oBCN5ljU; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf15.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732026237; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:dkim-signature; bh=FfKCUslkT6WaU68pK+pUyQdy4wuOSLWwqybytKrFcLk=; b=YmdYH8PHI4kYHUTNYdSayEBcfseTraVAE925IPsxCVAKQ6eOqiDTwUh2XbEKqiA1Xju9zv fGG4DIz1ZuqkmUTb9MgHYP0wn3JskT46V176cPr+fFYxYVyRln95lcYKMHrJjV2yJBN3Ci oNFYKO3VnUdg614hdAJGEdDX/8GHKqg= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=oBCN5ljU; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf15.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732026237; a=rsa-sha256; cv=none; b=L8+1aHH5EX5G5L3Bn5LwXS5iTyGG3tguQquLVzMXgOasjgobooxF+XzB2ysHueHAJZRB8o /983IurJ7t4OBbKAgtGunNZ8u6hIzkQeF77e60ZmF1CwfP6HTb4DrnGnke9hVcA+lWYtFf aprNKn7iJACcq+1Y9033W5BJaqm48kw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 5D26B5C044A; Tue, 19 Nov 2024 14:24:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 085D4C4CECF; Tue, 19 Nov 2024 14:25:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1732026327; bh=ktxKpCEHllI2yq6Y+BuNEscdzuToG7LK3RrtfXNqD+E=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=oBCN5ljU96Txbv//pPwA2SWNrZJlfP95KQcgXZcn1g7d7VFaDI/DrtC7LH8ZbzvCk Qe7OuaMfrOKXl0gARbaiCRfiDNuzsXww97FY+/IBE9uVILqn3Rj2Y/N14k1CEjbYcA rGjoRC7e2rp4VRhtgcyll7W5j9rwtxmFL/nKsYIc= Subject: Patch "mm: avoid unsafe VMA hook invocation when error arises on mmap hook" has been added to the 5.15-stable tree To: James.Bottomley@HansenPartnership.com,Liam.Howlett@oracle.com,akpm@linux-foundation.org,andreas@gaisler.com,broonie@kernel.org,catalin.marinas@arm.com,davem@davemloft.net,deller@gmx.de,gregkh@linuxfoundation.org,jannh@google.com,linux-mm@kvack.org,lorenzo.stoakes@oracle.com,peterx@redhat.com,torvalds@linux-foundation.org,vbabka@suse.cz,will@kernel.org Cc: From: Date: Tue, 19 Nov 2024 15:25:03 +0100 In-Reply-To: <2eb0f68593bf50f79cd346269fefe596cf0a6801.1731667436.git.lorenzo.stoakes@oracle.com> Message-ID: <2024111903-agony-dictator-cae1@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 970D7A0002 X-Stat-Signature: z55m5dskr1f3ddity399axgyead6zf1a X-HE-Tag: 1732026276-437384 X-HE-Meta: U2FsdGVkX1/46GBDhMxfS8k9fzravs18OV3RJVqbq7M2irqpsFSQ3GbIBX5OWA3m7nnRAsBA8jKp8/XY+lMw9rC0rV+PLI57CVZir0H3Djy0yrF7lwQRy4wzETv7LhCKprRD7gRP6lBQ9jmmQnBqSuZL0gUp0FJDI3iQmyD3S0fCw9YLI6vf0RWdzay7D/hVTyJEAnzHTwv0XOf6MErvBeEAbKtzISlqqL0CtGM0m1T/xAieDg0vgC85nuugWSZsijNuPMm8Lq9GU0IA6wlSw+ET9MFuKGC6L5L8kOnBlggQc98f22wwmWu2YFbkZ7yYOUea6h2XG8qNesD0bRhK7IIi8zpMSb/TvwnG8a76PbYOPTXb1MDtZWb3zmnqwgVb2O6VbbOWra0gEE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ADetQ9ZQhPM51ueqASAyRkDpll30/Obrq59/gbPOAllo9UIE91y6waS+CkRyWJPxBTs098HtdloQe3VTe/ZHILH9N7GnBMM/8ht8fb7esQoQ0V1+2JZ8E60bApfBkOuiVAA/Bspkr/8/F1g2HixzKY3ZZ8OmAxer/cPYeCQaEqEPmx9TovQ8ahMT1nhKaiCl1c05WV+9c8pB63QnDIRcPeJrsjb7pQk4aIlwF+/YYBN5kiOYvoT8jsrMqxOl8d7od9O0hGKHsuAIguvzqfw9vHn4/biap5gsB3ym2dzqaXY3VAQqXu231YVhuOSs1pzxFl620lk3HS4142zWicK30L6XP8HUnWti2Qa4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This is a note to let you know that I've just added the patch titled mm: avoid unsafe VMA hook invocation when error arises on mmap hook to the 5.15-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: mm-avoid-unsafe-vma-hook-invocation-when-error-arises-on-mmap-hook.patch and it can be found in the queue-5.15 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From stable+bounces-93525-greg=kroah.com@vger.kernel.org Fri Nov 15 13:39:41 2024 From: Lorenzo Stoakes Date: Fri, 15 Nov 2024 12:38:13 +0000 Subject: mm: avoid unsafe VMA hook invocation when error arises on mmap hook To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Message-ID: <2eb0f68593bf50f79cd346269fefe596cf0a6801.1731667436.git.lorenzo.stoakes@oracle.com> From: Lorenzo Stoakes [ Upstream commit 3dd6ed34ce1f2356a77fb88edafb5ec96784e3cf ] Patch series "fix error handling in mmap_region() and refactor (hotfixes)", v4. mmap_region() is somewhat terrifying, with spaghetti-like control flow and numerous means by which issues can arise and incomplete state, memory leaks and other unpleasantness can occur. A large amount of the complexity arises from trying to handle errors late in the process of mapping a VMA, which forms the basis of recently observed issues with resource leaks and observable inconsistent state. This series goes to great lengths to simplify how mmap_region() works and to avoid unwinding errors late on in the process of setting up the VMA for the new mapping, and equally avoids such operations occurring while the VMA is in an inconsistent state. The patches in this series comprise the minimal changes required to resolve existing issues in mmap_region() error handling, in order that they can be hotfixed and backported. There is additionally a follow up series which goes further, separated out from the v1 series and sent and updated separately. This patch (of 5): After an attempted mmap() fails, we are no longer in a situation where we can safely interact with VMA hooks. This is currently not enforced, meaning that we need complicated handling to ensure we do not incorrectly call these hooks. We can avoid the whole issue by treating the VMA as suspect the moment that the file->f_ops->mmap() function reports an error by replacing whatever VMA operations were installed with a dummy empty set of VMA operations. We do so through a new helper function internal to mm - mmap_file() - which is both more logically named than the existing call_mmap() function and correctly isolates handling of the vm_op reassignment to mm. All the existing invocations of call_mmap() outside of mm are ultimately nested within the call_mmap() from mm, which we now replace. It is therefore safe to leave call_mmap() in place as a convenience function (and to avoid churn). The invokers are: ovl_file_operations -> mmap -> ovl_mmap() -> backing_file_mmap() coda_file_operations -> mmap -> coda_file_mmap() shm_file_operations -> shm_mmap() shm_file_operations_huge -> shm_mmap() dma_buf_fops -> dma_buf_mmap_internal -> i915_dmabuf_ops -> i915_gem_dmabuf_mmap() None of these callers interact with vm_ops or mappings in a problematic way on error, quickly exiting out. Link: https://lkml.kernel.org/r/cover.1730224667.git.lorenzo.stoakes@oracle.com Link: https://lkml.kernel.org/r/d41fd763496fd0048a962f3fd9407dc72dd4fd86.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka Reviewed-by: Jann Horn Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes Signed-off-by: Greg Kroah-Hartman --- mm/internal.h | 12 ++++++++++++ mm/mmap.c | 4 ++-- mm/nommu.c | 4 ++-- mm/util.c | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) --- a/mm/internal.h +++ b/mm/internal.h @@ -34,6 +34,18 @@ void page_writeback_init(void); +/* + * This is a file-backed mapping, and is about to be memory mapped - invoke its + * mmap hook and safely handle error conditions. On error, VMA hooks will be + * mutated. + * + * @file: File which backs the mapping. + * @vma: VMA which we are mapping. + * + * Returns: 0 if success, error otherwise. + */ +int mmap_file(struct file *file, struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1788,7 +1788,7 @@ unsigned long mmap_region(struct file *f } vma->vm_file = get_file(file); - error = call_mmap(file, vma); + error = mmap_file(file, vma); if (error) goto unmap_and_free_vma; @@ -1803,7 +1803,7 @@ unsigned long mmap_region(struct file *f addr = vma->vm_start; - /* If vm_flags changed after call_mmap(), we should try merge vma again + /* If vm_flags changed after mmap_file(), we should try merge vma again * as we may succeed this time. */ if (unlikely(vm_flags != vma->vm_flags && prev)) { --- a/mm/nommu.c +++ b/mm/nommu.c @@ -942,7 +942,7 @@ static int do_mmap_shared_file(struct vm { int ret; - ret = call_mmap(vma->vm_file, vma); + ret = mmap_file(vma->vm_file, vma); if (ret == 0) { vma->vm_region->vm_top = vma->vm_region->vm_end; return 0; @@ -973,7 +973,7 @@ static int do_mmap_private(struct vm_are * - VM_MAYSHARE will be set if it may attempt to share */ if (capabilities & NOMMU_MAP_DIRECT) { - ret = call_mmap(vma->vm_file, vma); + ret = mmap_file(vma->vm_file, vma); if (ret == 0) { /* shouldn't return success if we're not sharing */ BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); --- a/mm/util.c +++ b/mm/util.c @@ -1086,6 +1086,24 @@ int __weak memcmp_pages(struct page *pag return ret; } +int mmap_file(struct file *file, struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops = {}; + int err = call_mmap(file, vma); + + if (likely(!err)) + return 0; + + /* + * OK, we tried to call the file hook for mmap(), but an error + * arose. The mapping is in an inconsistent state and we most not invoke + * any further hooks on it. + */ + vma->vm_ops = &dummy_vm_ops; + + return err; +} + #ifdef CONFIG_PRINTK /** * mem_dump_obj - Print available provenance information Patches currently in stable-queue which might be from lorenzo.stoakes@oracle.com are queue-5.15/mm-resolve-faulty-mmap_region-error-path-behaviour.patch queue-5.15/mm-refactor-arch_calc_vm_flag_bits-and-arm64-mte-handling.patch queue-5.15/mm-unconditionally-close-vmas-on-error.patch queue-5.15/mm-avoid-unsafe-vma-hook-invocation-when-error-arises-on-mmap-hook.patch