Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5170064rwl; Mon, 3 Apr 2023 15:47:15 -0700 (PDT) X-Google-Smtp-Source: AKy350bDInwdMEFyTXsK7WkykCOeL3pxQrc9kjbRJCHmouvsL6O0Zcapbyg/GXWN17QU+l51t34/ X-Received: by 2002:a17:902:e292:b0:1a2:1a33:6feb with SMTP id o18-20020a170902e29200b001a21a336febmr498092plc.34.1680562035124; Mon, 03 Apr 2023 15:47:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680562035; cv=none; d=google.com; s=arc-20160816; b=R0NLDKrdCaIVSHax7V8XgNwjQeU9z2Mn4gA1/IF37+fP3YtSYiIdYmhGEpGymWjCfO /rffEB7fWgIiuRaDLoKxzFmUU14MCYYJA3irQvLx2gLxenvSb0WEtxg21I8BBVH6fAcg r52K6ttUKe6GdJGyfPOCRy5dARiKPj41sjKl3tgunXs0MOs3c6sPlgiCFfp2+N8sOwNa yUf7d/JcKa1i7xQSGeMNpk+1kPYNxfN2AvZJi/ihf+ih6MmJIoEJjrJBGdjGwOk554/6 wXbn4eGtVX3DfwB2bTDWsxj14JOBIX7vr6zEQ5Z0OW2ZfRGaeep+8rSV4+acdEcxZcDg r0bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D59V0g1YvYLKaUmx+LEoYjgFSrxRUczBZA1IoL7nlYU=; b=EtKqWz+5+3yAo4Kxai6hnP2EDL58QgPx+CVum4C/tS63HqFs/8/9ZhGfqzvjN9Q5y3 QPwfMNYpU2UjKxPQitYwbN5Q5yQJOT+yTFGAlPW2zdKte+6OCn+xXqrKJ82XMzB83p9F MXDsdy5W7skgcA4x98GtuiHvtlrP1KEUfYtOAHMnw7UPpLvgepuX6xVMJouriwXp6R7O 1Mz2UDHXaZljpDovvDV6aGuX6qg5w8U9bc8xA0SkQhmclvMOXElh8cE97QBX0QXek67f 2HmYw7mXvHiIagYHHuPORND4JWDny+vu7PbmVXnTlEoBR6Fi4nKJK40blxpmQRUop0sG +wzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ETUrq2dY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t3-20020a170902e84300b0019e698b3cefsi8717830plg.311.2023.04.03.15.47.00; Mon, 03 Apr 2023 15:47:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ETUrq2dY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233798AbjDCW3A (ORCPT + 99 others); Mon, 3 Apr 2023 18:29:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233731AbjDCW2r (ORCPT ); Mon, 3 Apr 2023 18:28:47 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E7B130F5; Mon, 3 Apr 2023 15:28:46 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id j18-20020a05600c1c1200b003ee5157346cso20730041wms.1; Mon, 03 Apr 2023 15:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680560925; 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=D59V0g1YvYLKaUmx+LEoYjgFSrxRUczBZA1IoL7nlYU=; b=ETUrq2dY/Th4/JHEziKKqYHg+tGm411FZ0fvvHq7s2C38in0y6R3FaxLNn9Kj4sfr5 hMJgMJxsbvF+BQgibzC+VXG3bGSBAb1bW4IvDlePIroB7DzmvbD6AWL2l9Z+RxPcY2WH e3sRnk04MJf9JH+FTi70FLSupZ6xGpBWo6zhyM9EZJ3bUjmIhDmWb96l+t7TFGW18cS4 XhCagKRR19FhXNYIQfhhdh3fFJuSMOK5zHlYLX6Y/ZnRF/FdaP4FCYLzfxbyA7ummcqS lt4TjRqAoKPJLEu67p5KRYcTNockAGGP5JK2eJ2/xlawuQsKJgzPEOO4uoLNh7wk8jwv q2GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680560925; 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=D59V0g1YvYLKaUmx+LEoYjgFSrxRUczBZA1IoL7nlYU=; b=UIOGUYrI5cptsyBsVxcEDel0+U9d7fDepWhniWn/D2I04MIXfGYsH1vsKugXnJY33x aiWfasUTSRsAIKtHsJiqj97xsYD+PuU534JUpqoAZQevDHq35qFH9e4rMUqRh7COEGyD sPYzjbU5fwST190Z4pBnfKZa7aWGkV2yab7RECsoFQ7HGEY3EwZtkbzOq9zWdp6yJE4C 5wIXV0tlP6owbBtWVI/AztI0aS7vO4wpMeodfwp96ycsX3lykM/oB3VLxtKCtKmC47bf qJNqspofckPKIEhjxJhftsKlXPUFS4bIUwAl4l9TVh5hp0ffM3RviAWldIFQkmqRLHah y6rw== X-Gm-Message-State: AAQBX9dTRWt/OJ7StJXg7zTIQ1H4xi4T9EpofWusQ9fT9e4U+ff3KNnL ctEeJe1er3IRrh3mshaqTGoDMDtpwLI= X-Received: by 2002:a1c:6a19:0:b0:3ed:2ae9:6c75 with SMTP id f25-20020a1c6a19000000b003ed2ae96c75mr593443wmc.37.1680560925406; Mon, 03 Apr 2023 15:28:45 -0700 (PDT) Received: from lucifer.home (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.googlemail.com with ESMTPSA id u17-20020a05600c19d100b003dd1bd0b915sm20731309wmq.22.2023.04.03.15.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Apr 2023 15:28:44 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Andrew Morton Cc: Matthew Wilcox , Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Andy Lutomirski , Lorenzo Stoakes Subject: [RFC PATCH 3/3] mm: perform the mapping_map_writable() check after call_mmap() Date: Mon, 3 Apr 2023 23:28:32 +0100 Message-Id: X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index c96dcce90772..a166e9f3c474 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2596,17 +2596,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 unmap_and_free_vma; + } + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. -- 2.40.0