Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1137258rdb; Sat, 7 Oct 2023 13:52:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEi9dzR5rdgUyJLeV1/oXhC0QC2AubPFsJY52w1/40rml80QE3b74rxm9wL6cVNZ7PLmfxZ X-Received: by 2002:a17:902:d4c7:b0:1c7:5776:a30f with SMTP id o7-20020a170902d4c700b001c75776a30fmr11005091plg.12.1696711920581; Sat, 07 Oct 2023 13:52:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696711920; cv=none; d=google.com; s=arc-20160816; b=VHasba9mq2ClkLD9qXbDMnq6kPLtOkNEMvyS1sf5IgpxEd3UTN6g6kyPMjA/Nrx9/h qNC7f5PF/7QapfBKQhRqkkFp9Q61jy4w9UnS989VO04CYw+ChDgTANk2puz1Lk0KURuc 99t9HJ5H+TQYi+mSrZoGC0MB/vCT3c70z/xMUo2DTA7ssQZzWE7K4k62lPfg/ZJUO5Jc 1VsCBpvsVofxdFwgldN/t9akvxkT0Spt1Z6ZHZj/t9vtaZ2bWsYdtbpRQbWtUfPoFMO8 OQUFRnJpC8sr/IpCzgmX1fxr/fudKS9N5rNhYHNBdumwAQQrDcEOWodrFOJ9yGZ77Luv NtMA== 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=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; fh=On8i7MftmLnii/x6szJvRZF6tmYBE7/T6K0SDqpksdA=; b=gyQqaGpdsQB+0wzREHapt9zMbAeA+v6EKBT4rGSvgWpTzpykrBvBEdGHRc/ygAwfL9 v1/qzT5xsmopoigwEbPbgfKUU3ZEO6zjAVP4mpEmUCV5jxsmbSzhsqNvG7jWRn1krQiM VRcZue8V43zfNNguGmt4lEy6ExhitGocndm2D/REEKPZOiYmhPH5exC8UDfgGuZ3Bht8 IoO6fopeknGRe2ujwqgMkFyzrRpeCgvIYH3rOC/ktcpWO5NKZ5sMthoKXFde4XpnlHxB 1pMux+ButXNEFi1SNgMyPBEJRY5Y2aF97C9NXWlnAcQfib/jGk7HXrNl4gxrNGWg6rL4 x+3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="cZfbV49/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id b13-20020a170902d50d00b001c4749ee72csi6896374plg.503.2023.10.07.13.52.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:52:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="cZfbV49/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 3F2128050D28; Sat, 7 Oct 2023 13:51:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344217AbjJGUv0 (ORCPT + 99 others); Sat, 7 Oct 2023 16:51:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344180AbjJGUvQ (ORCPT ); Sat, 7 Oct 2023 16:51:16 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89FE893; Sat, 7 Oct 2023 13:51:14 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-32003aae100so2663451f8f.0; Sat, 07 Oct 2023 13:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696711873; x=1697316673; darn=vger.kernel.org; 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=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=cZfbV49/owq2IZis6MxULDn/VPdCTmrw6fvoNcQwk9O6jniLVimzHHp5rf2k0h9qI6 +37ciYjDill9y9n+PFryG9O5PJ9MqxUpVJz0kOFjB9/POOuXqFiO1iQBbVPiiWNqrgMd RkeDDTnAQE4BMT2TRwEfHS7QgvCGrNddiHHtoCzoIOu2Lddcx362S5xn4RP2PQ/6c6k0 pqAblOpX3xDWbsv01txeebfO+dhr8mg2EegTUKaVhpwp0RiSQazhBIZ8rJBtRgP6t5Jh YlGMb9CaebZlBQUktQ8Rqzt50uFuov8LcMfRA0lE1S0j/zmMVMuRnrLWPKDykrcccfZP GwRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696711873; x=1697316673; 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=2R/SPrp+9X8K4zqdkQ2nu1IGtJt6e/ubH1aV+LRG8uo=; b=uWKhNqdnKRzLDiV5j+1a+chbm0WJmYTUhBTVHDQsbWWle6V/8Bs+Xac/QEz5RrKzxC E7F21Imo2ra+LBW4k9nZVZp6TEUtBSbxPEOlra3kwz/7mZ7f7ANPcczZT10+bxWIcHzQ o1VXetFsULFgPb8nXW+zXBF5nOLpd0SsYhx6GvuNYlnWU6d/XMJWMlt/aDn/s/7Y4cCO EfTSYqMzccXXo5OYbZrAEzrfb1qKrjtvW+//p1Q/Rg88MHpQFcY9V/8yljJU+BI8msUc YgBrYDG58HiAlJ4v6ZpfBZVzL0Hqa00Rchzz8ihnwjkRQKO4yUkHyB63I79CkCP0M8Zj YW3g== X-Gm-Message-State: AOJu0Yy8tSHdS/Py9vIxau+EVfpKIvthFFjj2RkZBXaGAZAaDdwg60GE y6hIWGe+Y+vyn/TuQYHa3UQ= X-Received: by 2002:a5d:4805:0:b0:314:3369:df57 with SMTP id l5-20020a5d4805000000b003143369df57mr6970810wrq.5.1696711872784; Sat, 07 Oct 2023 13:51:12 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id g7-20020adfe407000000b003232d122dbfsm5120550wrm.66.2023.10.07.13.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 13:51:11 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v3 3/3] mm: enforce the mapping_map_writable() check after call_mmap() Date: Sat, 7 Oct 2023 21:51:01 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sat, 07 Oct 2023 13:51:58 -0700 (PDT) X-Spam-Level: ** In order for an F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE in seal_check_write() we must be able to invoke either the shmem_mmap() or hugetlbfs_file_mmap() f_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to clear VM_MAYWRITE. However, the existing logic in mmap_region() performs this check BEFORE calling call_mmap() (which invokes file->f_ops->mmap()). We must enforce this check AFTER the function call. In order to avoid any risk of breaking call_mmap() handlers which assume this will have been done first, we continue to mark the file writable first, simply deferring enforcement of it failing until afterwards. This enables mmap(..., PROT_READ, MAP_SHARED, fd, 0) mappings for memfd's sealed via F_SEAL_WRITE to succeed, whereas previously they were not permitted. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 6f6856b3267a..9fbee92aaaee 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2767,17 +2767,25 @@ 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; - } + int writable_error = 0; + + if (vma_is_shared_maywrite(vma)) + writable_error = mapping_map_writable(file->f_mapping); vma->vm_file = get_file(file); error = call_mmap(file, vma); if (error) goto unmap_and_free_vma; + /* + * call_mmap() may have changed VMA flags, so retry this check + * if it failed before. + */ + if (writable_error && vma_is_shared_maywrite(vma)) { + error = writable_error; + goto close_and_free_vma; + } + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. -- 2.42.0