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 F1F3FD44163 for ; Tue, 19 Nov 2024 14:26:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7467D6B00A9; Tue, 19 Nov 2024 09:26:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E3186B00AA; Tue, 19 Nov 2024 09:26:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 435B76B00AB; Tue, 19 Nov 2024 09:26:01 -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 23D4D6B00A9 for ; Tue, 19 Nov 2024 09:26:01 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D06C81404AE for ; Tue, 19 Nov 2024 14:26:00 +0000 (UTC) X-FDA: 82803066318.13.47B7D81 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id 12A4C2001E for ; Tue, 19 Nov 2024 14:25:05 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=T1a11W55; spf=pass (imf13.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732026210; 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=1dWDFWIXaaePLYnZKaOckzUlkQ8TQaGKAhHVpHqX4Rs=; b=whxdyN6bo52MLUfqYqY09ZHHRJoBXkRD+Gsit0SQrQLEdNrLLklPcJydzp87UeK7ovTCX7 Qka/UJ3eQkMirr3/ItGr0T0oU6EPWaAHw0mVHIkalMEngc7uUFd0rVqHgX2rL+O6XP6yFc sbV+likFySbjGvhV/Mwa9H9Rbmb5uf4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732026210; a=rsa-sha256; cv=none; b=lT9G4CTd6aZkJTX1P58Bc9IXcPOERV0PoxZfxAR2T2QzzkMXn2/Kk/S/eVbtRoGxFKBZBH Xjow3u7jITtxXqEMsXKpduE1hqfWWK01hwim+rIqrbs3VhqYFWlRevZ/1NPSIUIGj9cDEq aRoLkuf3jj1CU7M98/xXMDpJEH2IV5s= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=T1a11W55; spf=pass (imf13.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 68ABF5C0513; Tue, 19 Nov 2024 14:25:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14C51C4CECF; Tue, 19 Nov 2024 14:25:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1732026358; bh=9QJLGzcJZSWJTZMYNIjLD8y3R4pb8LGOEY2XR6RZFLc=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=T1a11W55LxSf4QORXE+vSOzbyC3pt1RMIvnu62G4lj9A/KNCPspsZJjrIAo5IUoPj vLJ6nLaEDoyN6VsbpFaUj21goulTMckiFTHE8b0ZFPxcyplyrJrNuB/OAtOwpgLzWY 82ZVbhGBuea/OqqWWpAxFwqooA3igRmlkovcP7aU= Subject: Patch "mm: resolve faulty mmap_region() error path behaviour" has been added to the 6.1-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:25 +0100 In-Reply-To: Message-ID: <2024111925-overtly-steerable-f6c8@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: rspam12 X-Rspamd-Queue-Id: 12A4C2001E X-Stat-Signature: xb7ks7dtrf9sb9nm3towcct7g8t8hmet X-HE-Tag: 1732026305-200894 X-HE-Meta: U2FsdGVkX1/vBbjdkLKx65KsL6UeE8fNqkBwq9THOYzlZMSnB/q6pzDVanCZrXAcZLmDgBehnDnCYea+mz2onEIeVZ4exdsV6lMWObaNtrn9XQ9SUPSgt9Zwg7r4aLELBa9GF67mFke+gEfeJTsSDNIWP8H5Z5izfWTHt3hl9RqHXkvhWJc2js09JDTELDWoaR0aHwWwW65cvvjNxuxL4niLwWYMHFXhR16M+x0ep3gQk+OWrWg9/Mx/rcaklXHi6TCxOM+byF6x/ib9UDUoIAPeD/QlxHF/l6iUVwiH8S/aQqkI3M9ttNSzgHb6kGvkxDoIDLN33D5xNV013Zk/XfHhG/8DKnGovc5bLyPtaNdjjGKwczCtVQGqVus1YdL9ScX25qk9WzMU/L5qggCxwucu65yCsPk47uO+PECO5lh9zYocWpUQptr3hnP5XSZOV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gqD8DGxdWdenqxSD5Iv4AieOiVjGZUkkYfWlKloILmbZb2E6Hi/3DsBkhEO4JSvk8Y6cty1mjdPIgXong33Fnypx9roDLlSdyc38UNWJGBixKNYHJBxA8AysXTlps+RePAJAqUWDuTRuwlS3GXGMOuIRB3S4PivTYzAaFVGr44VHBnHDtTqxN1zi9Bz49JhITK7QnhD/2Ad7Cz3Sv5PDOtzZWRlRV41bamdmzXX+PwUxIjVDIaujp3vTVNrPNwF9dJTB+C+pRkOnzoEkCwO7DRzCDPPyCIKsfj6qDLTcH+ZBMYvHc+xEoo5qC++DOo7/szbA4lKvmFsvD08bSxsryRVQ5ikw== 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: resolve faulty mmap_region() error path behaviour to the 6.1-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-resolve-faulty-mmap_region-error-path-behaviour.patch and it can be found in the queue-6.1 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lorenzo Stoakes Date: Mon, 18 Nov 2024 16:17:28 +0000 Subject: mm: resolve faulty mmap_region() error path behaviour 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: From: Lorenzo Stoakes [ Upstream commit 5de195060b2e251a835f622759550e6202167641 ] The mmap_region() function 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. Taking advantage of previous patches in this series we move a number of checks earlier in the code, simplifying things by moving the core of the logic into a static internal function __mmap_region(). Doing this allows us to perform a number of checks up front before we do any real work, and allows us to unwind the writable unmap check unconditionally as required and to perform a CONFIG_DEBUG_VM_MAPLE_TREE validation unconditionally also. We move a number of things here: 1. We preallocate memory for the iterator before we call the file-backed memory hook, allowing us to exit early and avoid having to perform complicated and error-prone close/free logic. We carefully free iterator state on both success and error paths. 2. The enclosing mmap_region() function handles the mapping_map_writable() logic early. Previously the logic had the mapping_map_writable() at the point of mapping a newly allocated file-backed VMA, and a matching mapping_unmap_writable() on success and error paths. We now do this unconditionally if this is a file-backed, shared writable mapping. If a driver changes the flags to eliminate VM_MAYWRITE, however doing so does not invalidate the seal check we just performed, and we in any case always decrement the counter in the wrapper. We perform a debug assert to ensure a driver does not attempt to do the opposite. 3. We also move arch_validate_flags() up into the mmap_region() function. This is only relevant on arm64 and sparc64, and the check is only meaningful for SPARC with ADI enabled. We explicitly add a warning for this arch if a driver invalidates this check, though the code ought eventually to be fixed to eliminate the need for this. With all of these measures in place, we no longer need to explicitly close the VMA on error paths, as we place all checks which might fail prior to a call to any driver mmap hook. This eliminates an entire class of errors, makes the code easier to reason about and more robust. Link: https://lkml.kernel.org/r/6e0becb36d2f5472053ac5d544c0edfe9b899e25.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 Tested-by: Mark Brown Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes Signed-off-by: Greg Kroah-Hartman --- mm/mmap.c | 104 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 57 insertions(+), 47 deletions(-) --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2652,7 +2652,7 @@ int do_munmap(struct mm_struct *mm, unsi return do_mas_munmap(&mas, mm, start, len, uf, false); } -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { @@ -2750,26 +2750,28 @@ cannot_expand: vma->vm_page_prot = vm_get_page_prot(vm_flags); vma->vm_pgoff = pgoff; - if (file) { - if (vm_flags & VM_SHARED) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + if (mas_preallocate(&mas, vma, GFP_KERNEL)) { + error = -ENOMEM; + goto free_vma; + } + if (file) { vma->vm_file = get_file(file); error = mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; + + /* Drivers cannot alter the address of the VMA. */ + WARN_ON_ONCE(addr != vma->vm_start); /* - * Expansion is handled above, merging is handled below. - * Drivers should not alter the address of the VMA. + * Drivers should not permit writability when previously it was + * disallowed. */ - if (WARN_ON((addr != vma->vm_start))) { - error = -EINVAL; - goto close_and_free_vma; - } + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + mas_reset(&mas); /* @@ -2792,7 +2794,8 @@ cannot_expand: vma = merge; /* Update vm_flags to pick up the change. */ vm_flags = vma->vm_flags; - goto unmap_writable; + mas_destroy(&mas); + goto file_expanded; } } @@ -2800,31 +2803,15 @@ cannot_expand: } else if (vm_flags & VM_SHARED) { error = shmem_zero_setup(vma); if (error) - goto free_vma; + goto free_iter_vma; } else { vma_set_anonymous(vma); } - /* Allow architectures to sanity-check the vm_flags */ - if (!arch_validate_flags(vma->vm_flags)) { - error = -EINVAL; - if (file) - goto close_and_free_vma; - else if (vma->vm_file) - goto unmap_and_free_vma; - else - goto free_vma; - } - - if (mas_preallocate(&mas, vma, GFP_KERNEL)) { - error = -ENOMEM; - if (file) - goto close_and_free_vma; - else if (vma->vm_file) - goto unmap_and_free_vma; - else - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif if (vma->vm_file) i_mmap_lock_write(vma->vm_file->f_mapping); @@ -2847,10 +2834,7 @@ cannot_expand: */ khugepaged_enter_vma(vma, vma->vm_flags); - /* Once vma denies write, undo our temporary denial count */ -unmap_writable: - if (file && vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); +file_expanded: file = vma->vm_file; expanded: perf_event_mmap(vma); @@ -2879,28 +2863,54 @@ expanded: vma_set_page_prot(vma); - validate_mm(mm); return addr; -close_and_free_vma: - vma_close(vma); -unmap_and_free_vma: +unmap_and_free_file_vma: fput(vma->vm_file); vma->vm_file = NULL; /* Undo any partial mapping done by a device driver. */ unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end); - if (file && (vm_flags & VM_SHARED)) - mapping_unmap_writable(file->f_mapping); +free_iter_vma: + mas_destroy(&mas); free_vma: vm_area_free(vma); unacct_error: if (charged) vm_unacct_memory(charged); - validate_mm(mm); return error; } +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf) +{ + unsigned long ret; + bool writable_file_mapping = false; + + /* Allow architectures to sanity-check the vm_flags. */ + if (!arch_validate_flags(vm_flags)) + return -EINVAL; + + /* Map writable and ensure this isn't a sealed memfd. */ + if (file && (vm_flags & VM_SHARED)) { + int error = mapping_map_writable(file->f_mapping); + + if (error) + return error; + writable_file_mapping = true; + } + + ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf); + + /* Clear our write mapping regardless of error. */ + if (writable_file_mapping) + mapping_unmap_writable(file->f_mapping); + + validate_mm(current->mm); + return ret; +} + static int __vm_munmap(unsigned long start, size_t len, bool downgrade) { int ret; Patches currently in stable-queue which might be from lorenzo.stoakes@oracle.com are queue-6.1/mm-resolve-faulty-mmap_region-error-path-behaviour.patch queue-6.1/mm-refactor-arch_calc_vm_flag_bits-and-arm64-mte-handling.patch queue-6.1/mm-unconditionally-close-vmas-on-error.patch queue-6.1/mm-avoid-unsafe-vma-hook-invocation-when-error-arises-on-mmap-hook.patch