From patchwork Sun Apr 30 22:26:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13227331 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 C75E7C77B73 for ; Sun, 30 Apr 2023 22:26:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1502F6B0078; Sun, 30 Apr 2023 18:26:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1006E6B007B; Sun, 30 Apr 2023 18:26:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE38B6B007D; Sun, 30 Apr 2023 18:26:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E0E3E6B0078 for ; Sun, 30 Apr 2023 18:26:48 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AF5CFAD7EE for ; Sun, 30 Apr 2023 22:26:48 +0000 (UTC) X-FDA: 80739493296.28.727E88E Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by imf17.hostedemail.com (Postfix) with ESMTP id E31E84000E for ; Sun, 30 Apr 2023 22:26:46 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=VXNNdRhL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682893607; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gU5klj4JuCtxBh6JR6WjnJXM2yce7ELIzVSpcWZL9RY=; b=yHPOThmfd2D8TtZRCnpDrveZremX6nlm2R0lY6/NaDe4+HC+ZbL7WQ5MC4SCSJs0DWVYSy FCAlojmJWFKjrkZb12jL4CK08m7djYy2tmC5ER+zQ1zjJh6JgXqP50MnD5kTley9PKwy8A xbM+XptBwttaG/x1KQSsiOMQo8r6SZw= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=VXNNdRhL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682893607; a=rsa-sha256; cv=none; b=2DHoGhDc0ZTpKNX2PxpnvYfODiJ8CUWkTq/1VpAkMbhKqD771DhochjKZQgpsc4IhLGdbe 8giXeGo4kqtPEi00KPRsbnflNG9o1uidGNUN/zRIalnYB4psyGNLvflqD8a+s1OBnLB42d V/vLW1nKvu9VjApe2JLLwobizzjsa8Y= Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-2f27a9c7970so1733065f8f.2 for ; Sun, 30 Apr 2023 15:26:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682893605; x=1685485605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gU5klj4JuCtxBh6JR6WjnJXM2yce7ELIzVSpcWZL9RY=; b=VXNNdRhLqJDygRRWBenMw/2sboDt9lMxswc9Klc7vyZ7DUUY8PsRrW4VYJXOKcH18t qlCJf2sRfxtVrN7OzpZbclacR5lQtGV6yIVIuJ2NYAMZlpbayLltwRM8D6ieUfN6+jkC FjJMWW2TRyK4N4hEBJ0pllZrGFeL40h545zok0J472IxJ/QO7vIlbUB716nHnopqBlYT AU9clybB/l7GCTu17p2ip6WlBxT18xR8ZMTJtAAINBnTkPXLceuKXkyumwE13HR/zkw0 FQBZimSmFtGej5L5shtV9NLrlZgp5R7ELWLyd7VcsucF2yrHASTmMy4DhE48LvmZWGDv Hfpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682893605; x=1685485605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gU5klj4JuCtxBh6JR6WjnJXM2yce7ELIzVSpcWZL9RY=; b=cjtoH7A4lUAqtEvdXrSGIjM7sj0xcJuL+WaYvGir5MM1k+tYBjyNVolfjYFCPF+Uq4 r3GP818+Kzp29vnzaH+QJWx8/PFyTOHyqErhWpM3x7L2W3xBy59xzw27MgNkCVwFMx/g VgBEr04J7MwVoleAnf89e6P+f89jqUHv6lSozh3B3eBtxQwc+TfFygt+WiKBafuVBDEY Plz9l+Ibc/UcwDyZJUJEwSYEh1SnvJZCa0e6ZrYC+L3vTIuf4e4oq7XcQNjbn/X+P5jV qKsGRPn/U4e3qDkN4vFRhh/ejt72bP04wLQlamSWoBeShpeAiD1FGaMT4dJYcdG9FThj KD9A== X-Gm-Message-State: AC+VfDz/KBpRow46cFcbKtwljD7a/4rjVRS5EftJQQ0kfB5iKfUhNEGS B564KC4R+clY7QtU5Nld22pYY3CZVvq/dQ== X-Google-Smtp-Source: ACHHUZ5v8qXYw3hgXK16jHy+ni2ikKLOkyTRyqZ3qFWlrvR4D0x5Vzd2irHeayWjLHt2m0/NfaYC3w== X-Received: by 2002:adf:db8e:0:b0:2fe:f2d1:dcab with SMTP id u14-20020adfdb8e000000b002fef2d1dcabmr8764207wri.58.1682893605108; Sun, 30 Apr 2023 15:26:45 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id g2-20020a5d5402000000b002da75c5e143sm26699865wrv.29.2023.04.30.15.26.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 15:26:44 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Matthew Wilcox , Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Andy Lutomirski , linux-fsdevel@vger.kernel.org, Jan Kara , Hugh Dickins , Lorenzo Stoakes Subject: [PATCH v2 3/3] mm: perform the mapping_map_writable() check after call_mmap() Date: Sun, 30 Apr 2023 23:26:07 +0100 Message-Id: <6f3aea05c9cc46094b029cbd1138d163c1ae7f9d.1682890156.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E31E84000E X-Stat-Signature: oy6mcr3xtgwszgddxohydrn1z3c4qtpd X-HE-Tag: 1682893606-965816 X-HE-Meta: U2FsdGVkX1/8RNhQMeFodblYQpwOXUvQvoY6ode92hUrL4M1dck+tThEfAOEayeRnpSQvYOJQpDM8t7eSVQGqIIS0TkqSzMxcN2a/l9I1mR0Utllz8eOBKQgu1KcSAXPf6VMUCOSur69h2AYbrvJYtWu2/Gx0+HdyKUK7rUgPdYEDmBCo3v2tktd0ZAEUL7Fh0pczjXXCnSd2+Lky1DxP6RZ4vxtp8OkRtH2AH/wpyMQW61UD7OWmLVQVwx8gIo6gzm7I7qHN/XbxOxdiHs+FyG/E2i1Rfn6B67HNPkKm1j4Qb2fCM7sj6eQiVZm4ozEcc0tkDfpiEWaVx0R0Ih0WrcJDkDdQJrWzbXHf4tAORDNF72p8n2nsO2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AUgThLU6cm+2R4pKhuNMK8x6zR/RZDxj8ChLWJV7SGVWTA7Uh9T4mppgtNtMXVSxCuqMpJxlI9r 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: In order for a F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE, we must be able to invoke the appropriate vm_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to avoid it. This patch moves this check after the call_mmap() invocation. Only memfd actively denies write access causing a potential failure here (in memfd_add_seals()), so there should be no impact on non-memfd cases. This patch makes the userland-visible change that MAP_SHARED, PROT_READ mappings of an F_SEAL_WRITE sealed memfd mapping will now succeed. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) -- 2.40.1 diff --git a/mm/mmap.c b/mm/mmap.c index 646e34e95a37..1608d7f5a293 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2642,17 +2642,17 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (is_shared_maywrite(vm_flags)) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } - vma->vm_file = get_file(file); error = call_mmap(file, vma); if (error) goto unmap_and_free_vma; + if (vma_is_shared_maywrite(vma)) { + error = mapping_map_writable(file->f_mapping); + if (error) + goto close_and_free_vma; + } + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA.