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 555B3D44162 for ; Tue, 19 Nov 2024 14:26:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12ADB6B00B4; Tue, 19 Nov 2024 09:26:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 063AF6B00B6; Tue, 19 Nov 2024 09:26:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFD236B00B7; Tue, 19 Nov 2024 09:26:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BDB126B00B4 for ; Tue, 19 Nov 2024 09:26:22 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7036F80529 for ; Tue, 19 Nov 2024 14:26:22 +0000 (UTC) X-FDA: 82803068586.15.D87E058 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id 72D4DC0005 for ; Tue, 19 Nov 2024 14:25:58 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=Q22l1ljJ; spf=pass (imf10.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=1732026289; 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=Z5ud1SMEqYchgy62tfI/d75Uvwe27VSuxdfDG5qqQfs=; b=meamitsZkunuPjg4fsSzLtRY7FB+yKN+6qLi5d/4RDO0K05/gO+c3UpgzEM54EtGRHUht+ O1IGaGT9gPIOeqvlnXNlBA4k9FcEgXIgHe8emArwop5rSgFzuuwguWJmty9m/E1Lg+hnzA gYe5MvjQ1dIidDJDYHanr32/zSktEuI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732026289; a=rsa-sha256; cv=none; b=V7xHXwDL5yjoQs8x2+QiuAVjVyMlGHOrKeSMt7MDDD3rZapIFuqzjD33x2o7e3VZwvC17L I1Qd9y5VKte/xrPydWzZbvX7sMPFqcjdt8dX+Cmh+sdkxx5r4tNzCWVL1y1MHjYUJocQph K+UVaI8taELP/K4hT3P6dxqV+9Cgzu0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=Q22l1ljJ; spf=pass (imf10.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 DB3A25C0706; Tue, 19 Nov 2024 14:25:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76646C4CED8; Tue, 19 Nov 2024 14:26:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1732026379; bh=0f+inW4hbJsihnovHW94XGG2IzS5W+5Ig1RpQac4cwE=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=Q22l1ljJbMaBwF+AGm4TWur2TPdbFoxMx+b3fi7//cV/HgSE7/aO9K+yy+wqyLDLA vqbT0OnM84Gtcni49uF4+vgZvzahsbNqfl10bGR/qHWPxoE/+USsorQ8IPt+T2pJOz uE3saJjNLvTiTbvBP7S0ksdKG0z9sO76aSSzGA+o= Subject: Patch "mm: resolve faulty mmap_region() error path behaviour" has been added to the 6.6-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:46 +0100 In-Reply-To: Message-ID: <2024111946-smashing-keep-c558@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-Rspamd-Server: rspam10 X-Stat-Signature: bn59kp7mfcdrcjtguq4db15wtg5zbjsu X-Rspamd-Queue-Id: 72D4DC0005 X-Rspam-User: X-HE-Tag: 1732026358-885399 X-HE-Meta: U2FsdGVkX1/IQTEGO65tQG9qNzf6xMTjid5yjg+3CARwBTSZSE5Xd2NIXm+Y5u1n00KzoJzFyn7ie9mE7Ayl7ffPr5gGcZZ5uzH582IsX1ekNkNDwwBR/C0Kk90uPQRVu2SWe708mHCiXZs7t6cWgLu5q9PXMGqXHA3GL2zFltqpcJAU14Y1Y0TNyWKBboeLFNU0fF8WyhUqwsZ9HiWHqOKQskfN08y8eJmov6hwX0eeikg+vqLiBCo7wVeXfhlrVtRlbGUIvdoBtfZQrfYsmefY+dBKTMDUzR9Z6mKoLRwXJVuiMgPNMuBXyudJpYdXV5uPxigLXvRYkn80v4NCUA/u0ttt9nJucB2AIY4LkfXx8YpQE5PU5Y8Ld/IE1kIzKradE2k59P8UPoTCO3zTe04ohjahkMw5qGr4FdzHNFm+6E6wvGrzBzdYfjTBK1R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obuhX5c6A1DVSHNM+F8APP3AqYJo+IkA6NbTB5Yr1P4jfVatxW9hjSDGTe4fjmtPyVb/dYeH7KgBdRBBzeb3Vs7kcHgh2PBOpNDDRYX41sMg/ZpGRPywZA6m3R0zNmYFppMXMExgyiVZQrMFIugpgFE9EoUokaINY9bRJGX/x6/ky7JzwMw820yKBW53giK9e0ync48ktoYakRKLrw3rCYPi/CGtCJjTpOIkD2s/N421DvtNYD1GF2lVEkTa4xgk4SNCPp0rXmORwVZkU8wzAjvbXZUnuxAcBQ7sSUXu8p1Hs6N6KhjzE1DPyoi/rN5UmVFnixaK1PxsyMX9Np4JdpK/QMQ== 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.6-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.6 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From stable+bounces-93539-greg=kroah.com@vger.kernel.org Fri Nov 15 13:43:45 2024 From: Lorenzo Stoakes Date: Fri, 15 Nov 2024 12:41:58 +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 | 117 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 67 insertions(+), 50 deletions(-) --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2666,14 +2666,14 @@ int do_munmap(struct mm_struct *mm, unsi return do_vmi_munmap(&vmi, 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) { struct mm_struct *mm = current->mm; struct vm_area_struct *vma = NULL; struct vm_area_struct *next, *prev, *merge; - pgoff_t pglen = len >> PAGE_SHIFT; + pgoff_t pglen = PHYS_PFN(len); unsigned long charged = 0; unsigned long end = addr + len; unsigned long merge_start = addr, merge_end = end; @@ -2770,25 +2770,26 @@ 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 (vma_iter_prealloc(&vmi, vma)) { + 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. */ - error = -EINVAL; - if (WARN_ON((addr != vma->vm_start))) - goto close_and_free_vma; + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); vma_iter_config(&vmi, addr, end); /* @@ -2800,6 +2801,7 @@ cannot_expand: vma->vm_end, vma->vm_flags, NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL); + if (merge) { /* * ->mmap() can change vma->vm_file and fput @@ -2813,7 +2815,7 @@ cannot_expand: vma = merge; /* Update vm_flags to pick up the change. */ vm_flags = vma->vm_flags; - goto unmap_writable; + goto file_expanded; } } @@ -2821,24 +2823,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); } - if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) { - error = -EACCES; - goto close_and_free_vma; - } - - /* Allow architectures to sanity-check the vm_flags */ - error = -EINVAL; - if (!arch_validate_flags(vma->vm_flags)) - goto close_and_free_vma; - - error = -ENOMEM; - if (vma_iter_prealloc(&vmi, vma)) - goto close_and_free_vma; +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif /* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write(vma); @@ -2861,10 +2854,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; ksm_add_vma(vma); expanded: @@ -2894,33 +2884,60 @@ expanded: vma_set_page_prot(vma); - validate_mm(mm); return addr; -close_and_free_vma: - vma_close(vma); - - if (file || vma->vm_file) { -unmap_and_free_vma: - fput(vma->vm_file); - vma->vm_file = NULL; - - vma_iter_set(&vmi, vma->vm_end); - /* Undo any partial mapping done by a device driver. */ - unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, - vma->vm_end, vma->vm_end, true); - } - if (file && (vm_flags & VM_SHARED)) - mapping_unmap_writable(file->f_mapping); +unmap_and_free_file_vma: + fput(vma->vm_file); + vma->vm_file = NULL; + + vma_iter_set(&vmi, vma->vm_end); + /* Undo any partial mapping done by a device driver. */ + unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, + vma->vm_end, vma->vm_end, true); +free_iter_vma: + vma_iter_free(&vmi); 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; + + /* Check to see if MDWE is applicable. */ + if (map_deny_write_exec(vm_flags, vm_flags)) + return -EACCES; + + /* 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 unlock) { int ret; Patches currently in stable-queue which might be from lorenzo.stoakes@oracle.com are queue-6.6/mm-resolve-faulty-mmap_region-error-path-behaviour.patch queue-6.6/mm-refactor-arch_calc_vm_flag_bits-and-arm64-mte-handling.patch queue-6.6/mm-unconditionally-close-vmas-on-error.patch queue-6.6/mm-avoid-unsafe-vma-hook-invocation-when-error-arises-on-mmap-hook.patch queue-6.6/mm-refactor-map_deny_write_exec.patch