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 D647DD44162 for ; Tue, 19 Nov 2024 14:25:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 71C9C6B00A5; Tue, 19 Nov 2024 09:25:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CAE66B00A6; Tue, 19 Nov 2024 09:25:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5BA8E6B00A7; Tue, 19 Nov 2024 09:25:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3D37E6B00A5 for ; Tue, 19 Nov 2024 09:25:52 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 23F9D1204C5 for ; Tue, 19 Nov 2024 14:25:51 +0000 (UTC) X-FDA: 82803066990.24.72C26AB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id EAC531C0009 for ; Tue, 19 Nov 2024 14:25:26 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=eXy5oD1P; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf18.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=1732026165; a=rsa-sha256; cv=none; b=HpICDBKlLeeSFhRjEuXbubGlOzRSwFXZQg9YnRLD2hr0vpIkEdHOac69Xbnk0QLo77b+/v a7l/dPCOHSPhas3WB4g+1LNPxmdvfvnwMNb7N/NVIebKNb7gy/XhRkDkVjmwOyD8jFNu8h /SUpDzemsJRa36x/+ZWBW5aRODIfIhM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=eXy5oD1P; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf18.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=1732026165; 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=jsM6LcGVdPhQzOBUVmckJkiV4d3X2Opa7ZGol1xl2Mo=; b=eeGZTcdhbR3NMggePpy4Ym1Vn9+yA2dP6lq5jH9lcKRtbt0OfSAmDuLwG5h7KYylqlQwwH x+iEg1N4AILj50Q5VoidNI3hAAqXbWT+eAhfoB9KrIdS6+c40qz/EaRQRnWrUBjisGwCYq yfc2urCCk6+2/8Dydkra7cmuRVXPDtk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id A1CCB5C3F29; Tue, 19 Nov 2024 14:25:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6896AC4CED2; Tue, 19 Nov 2024 14:25:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1732026348; bh=OM8EywD4MwDaLAhJXCY0awnsUA5WWjgHvF8vf8Mt6cg=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=eXy5oD1PvxTUpMNd0Try/q5YMPBAUUNlxiHp4VnRu+xSY0qATjJWlb6N9X3wehHQr osHqcIdDkDc+dgcYF7NvBeCV1iKnqnXYHKYC47cnwKNTBV5i3bSigJk+djAaHD4FdK cIqXVw7e0ZMslSzon8bdHpguel6UChUd0DTL0U1c= Subject: Patch "mm: avoid unsafe VMA hook invocation when error arises on mmap hook" 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:24 +0100 In-Reply-To: Message-ID: <2024111924-broadly-deskwork-84d5@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-Stat-Signature: shsbm68acdaq816c4mse83xnc4n4bqyt X-Rspamd-Queue-Id: EAC531C0009 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1732026326-496814 X-HE-Meta: U2FsdGVkX1+KfnoPwVhsfOYinJRSut4aK2t7kcSCrWTcJ5YNCTzkCWjunrb5zRMbFI3btwvegC1d2OvseUVKf0kW8JB9ijhlJsqC2ZmkZXoxMu2PV3sVdvo8ZIDtbF51QBxUSJZt7Ts1d80VfTcjYX3z6zbRbodXJIHtWPo0q3yQ2+pm517cI3eSC/g9WgBe07+8wCFWS8SDgWgqiQzdjp3tFRygGM2UWSdTKN3oEDBwC/Dy5eVkPMZZmUjZJ1kTQqtZkRGU+UJZ5NKXaymtJz+NrRFPskAFQmUaWSOhGicxmmg4wtWw9K7dCNV+AB84Mlqfqy4XB9lRBxDuvrP9tjJM3DbyUmC4Xe5isGmaKsmaSF6RDJ+kCoHkuTP8ulN6vtCinIr3K4JdyeeqNyEfz4Ba29gfWUNkaHv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qjVSB1gIKCthIwzlpmYBZvkjFF2/XdLTu8rHwo2jlnRWl37IvMzaIzarr08mBGRPLeDllta8xMA3deUs/ZBuCIfacQ9Wuwi3tYs2vOqG7uM2R0XSgXl5FXg6IuLP+D1ZRIV4ukHNtYvMjirSryQ0asJZVmzbypr1M/qiXSIorvSlavK2PHkbmfqV4do6gGGkn0jYncWGTRGnBh7IRM2PkFnO2k53FkXcEzKAkPcWynWGq0LmRa2WJlWyRYc3rFrw44+RS1dkOxzKqk2fcuZp/Vu0bgmL8KMYiq8L/8z0diNz/DfTKEZOf1sdPfaYqQvunxHEAq8gluIYHnrUM= 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 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-avoid-unsafe-vma-hook-invocation-when-error-arises-on-mmap-hook.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:25 +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: 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 @@ -52,6 +52,18 @@ struct folio_batch; 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); + static inline void *folio_raw_mapping(struct folio *folio) { unsigned long mapping = (unsigned long)folio->mapping; --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2760,7 +2760,7 @@ cannot_expand: } vma->vm_file = get_file(file); - error = call_mmap(file, vma); + error = mmap_file(file, vma); if (error) goto unmap_and_free_vma; @@ -2775,7 +2775,7 @@ cannot_expand: mas_reset(&mas); /* - * If vm_flags changed after call_mmap(), we should try merge + * 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 @@ -939,7 +939,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; @@ -970,7 +970,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 @@ -1103,6 +1103,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-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