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 DE71AD44161 for ; Tue, 19 Nov 2024 14:25:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F67E6B0088; Tue, 19 Nov 2024 09:25:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6086D6B00A0; Tue, 19 Nov 2024 09:25:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D11D6B00A1; Tue, 19 Nov 2024 09:25:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2EE806B0088 for ; Tue, 19 Nov 2024 09:25:39 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DC546C04DE for ; Tue, 19 Nov 2024 14:25:38 +0000 (UTC) X-FDA: 82803066780.15.A608E25 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf29.hostedemail.com (Postfix) with ESMTP id F420012001E for ; Tue, 19 Nov 2024 14:24:28 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=DAb+EMeV; spf=pass (imf29.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=1732026135; 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=IXpSkhFKT2Na5+SxGT6KOAEI6wQKWj9dkR/RfSL8YB4=; b=n2T4qGmzVgvzY22Y6L4Ox6rL9QgDTqKSPj0Js0Fm6MgWdg0thPVs8QrAAo5+aRKEmVQb8D iOaV5VbQpP8ogrB6bl6yNtH1QMAYVEiH3rbSvwO0kkEzBQE6WiQ5/KsBAWIbLYiRlyFVne razV97M9Vy4jUjJrfFy5YYiURV58nUI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=DAb+EMeV; spf=pass (imf29.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732026135; a=rsa-sha256; cv=none; b=grQb2CSL+Tj6KZUma9M1zQo4l8gO33IxW1IRHg70uO8GEtbSfC4QckJJlQHRM8EqJkO/6m lfi6gG/qF6sw8Uk/RMx+uB2rAFyiE65F0AVTA7m+f9S3vjSFabjN6BqMpoD3GPp1Tm4CDF SJOdQwqoE03b6PDtTk8vmJloM+WQy4I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 395215C3DF2; Tue, 19 Nov 2024 14:24:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1CA7C4CED8; Tue, 19 Nov 2024 14:25:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1732026336; bh=PHl39jt/4dOw7luwXlq6YgZkjO8znfmqINt7fTKCQTM=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=DAb+EMeVl1DuEkNQXZVyuBP9iurSUd3RyhXJnsGsvzLooumWkmyTceWl6HAnLjKi9 ypfq4km9ctnv4L+o5pUDMdKOeW1s7N+dMiDDSfAAAD8GXTMn6PZxld6LJlxjAxgi0H VbnsJB/SChU830BQ/59iLMnYrg2pGAAY8ejY4or0= Subject: Patch "mm: resolve faulty mmap_region() error path behaviour" 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: <03d88781b023104684f07a920a3e3b205461e419.1731667436.git.lorenzo.stoakes@oracle.com> Message-ID: <2024111903-tattling-icky-43da@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: rspam07 X-Rspamd-Queue-Id: F420012001E X-Stat-Signature: 34r4gyifk1ryg44b3hx4f3iy4313f8id X-Rspam-User: X-HE-Tag: 1732026268-216234 X-HE-Meta: U2FsdGVkX1+YkRdanTvVEOmB75nljFQCyPzhZuaLG954wQ9vgua+zKMZXgQN77+L0zxYiIOGyC9p8Iv2o0HO0/5YEW6e0aimrEfWUUbLp2uVIwtpp7rFcv+TF4uXdS4mLXcx/4iEXqIaf1go91nbbJI2KiMPnkH1T9cKOm4kOSs/FiI1dKJkK83h9Q7512LPDdu3VZyLdow/HfGlyY2c8KZk1YZwfOTKFDX5zrdiQgn2l+GTmrn1vWig1xyyDdV7MOk9gjBkwvEwiRB4MH+mKGUtBixSbPFspSNTB9TRNKRJDjeMGBdz2ynp9GxghdfViQLVdadX9ePRxRYOIaa+Ic8o/3lGVPjEJEGOhp5ICUrH5KfK5MkG5E5cep6oqKh1MQAdb6DoozcB1FJ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tdweix/arsoWu13VeW2vjgly9ggP9GxTNnM5ejlqhoOHd9yzBoKVDcS0L6i6wIFRaWIOZU06xg+TJdPE+Z0QU6adH7B3/MC6o6nfImPU18WKvcmIdP7T3eOsD0cC9TiqW277R8GXbFCm6ZsxgHecERHaz+obIzh1uYSWpd11InV6mVt72mH2P+ClfviySh/Hbh7h7kSfbQHH+GANocDX8+kAaBTVy64/cesBFE4CxZFbgCgmHVGIDuSsX4V+O7Sk4+n4vxciOWJuEDTml8iAxVPYVbgMSg4k0HFdpLALSgr21Jn3ohNwJ8aj+lK6mfe0KhpNimdhWgmHjSyjoKeg1Fng+1kyY0pKALzmkgWIUqyCRb658/5GTuE4N4/w3erk+dF+oVbQ== 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 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-resolve-faulty-mmap_region-error-path-behaviour.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-93528-greg=kroah.com@vger.kernel.org Fri Nov 15 13:40:25 2024 From: Lorenzo Stoakes Date: Fri, 15 Nov 2024 12:38:16 +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: <03d88781b023104684f07a920a3e3b205461e419.1731667436.git.lorenzo.stoakes@oracle.com> 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 | 73 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 26 deletions(-) --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1716,7 +1716,7 @@ static inline int accountable_mapping(st return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; } -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) { @@ -1780,16 +1780,10 @@ unsigned long mmap_region(struct file *f vma->vm_pgoff = pgoff; if (file) { - if (vm_flags & VM_SHARED) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } - vma->vm_file = get_file(file); error = mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; /* Can addr have changed?? * @@ -1800,6 +1794,14 @@ unsigned long mmap_region(struct file *f */ WARN_ON_ONCE(addr != vma->vm_start); + /* + * Drivers should not permit writability when previously it was + * disallowed. + */ + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + addr = vma->vm_start; /* If vm_flags changed after mmap_file(), we should try merge vma again @@ -1818,7 +1820,7 @@ unsigned long mmap_region(struct file *f vma = merge; /* Update vm_flags to pick up the change. */ vm_flags = vma->vm_flags; - goto unmap_writable; + goto file_expanded; } } @@ -1831,20 +1833,13 @@ unsigned long mmap_region(struct file *f 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 - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif vma_link(mm, vma, prev, rb_link, rb_parent); - /* 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; out: perf_event_mmap(vma); @@ -1875,16 +1870,12 @@ out: 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, vma, prev, vma->vm_start, vma->vm_end); - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); unacct_error: @@ -2907,6 +2898,36 @@ int do_munmap(struct mm_struct *mm, unsi return __do_munmap(mm, start, len, uf, false); } +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-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