Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5874665rwr; Mon, 1 May 2023 12:11:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5fOM8YPCzvPDMzMIKeCuiOx0fQEw7j5vJfuVBVgSkQOEaZJFH8cIemkE2HPr0gyzFDxv5s X-Received: by 2002:a17:902:ec92:b0:1a9:b8c3:c2c2 with SMTP id x18-20020a170902ec9200b001a9b8c3c2c2mr20304774plg.37.1682968284046; Mon, 01 May 2023 12:11:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682968284; cv=none; d=google.com; s=arc-20160816; b=Pzq8FbLLuGL5jrTs3mRo1uMjgUxoVNPyWpGBeITLYwRmjwe4bUpT1HbVLAwKcUPVn8 TPvcGFUAw6XPIxL1ZIJ6aV3YYP3JhqiGk7ttelesN9zux0jve3ISOi56831zsMlXYYYF PjZMMc0paz4ICGrzyG9K3nu8Tbo12KopFf90+/EAI+STyxrcuHE+DXAsxCDMGejC+gU9 Q0UjoKdDqNCD+gwQmByI6wJyy8ULdLsum1iMw1Y3+Dzj8ManPU61S0IJhaFaNBMIwEy/ 1nO0ekHE6gaMTYKrMcFOr+KB2B2G2qW7ZxOdrHPh27WIbbJHbvUG2Jya3fGF1BUzcMBs 990A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=HICSmpGafqaE9OdhnAdEgmNhbQ0DpCtSJgSvbJqQ0Lc=; b=hUb2MbVsxnYIfL2H1VCay8AKSP3qFH8QsWgCAjatTLWltRG2mkbkZKXK1RHnoSTq+r 8LpJ0MrxT3NzRXiZ20jG9Lc11iaYy1XzWc/ur5Jk9FNNsyTCisr7fHH2ku9ncDtZ0/jZ Acgb4zrRUx84N6JVBp1DFFUL9YeWnv/7u/39VgJOyLPIaULhwvnte3PS7qV6ow1dHqkj sapkojkcz3HlqwQGq+aMDYw3Fr1cSwp4xjTTcAo11obZZjUzdo2cJWlfjSCBHN1IzIOO LTILeJa5EI7gpLmYuuyWNVck8HFjtFtT/VDREjeAPG/xsktnAwLqv2ApklOTGHs+zjTt Uiew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=W4LOzGRL; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g11-20020a1709026b4b00b0019e95180a08si28564089plt.59.2023.05.01.12.11.09; Mon, 01 May 2023 12:11:24 -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=@kernel.org header.s=k20201202 header.b=W4LOzGRL; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232763AbjEATCS (ORCPT + 99 others); Mon, 1 May 2023 15:02:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232434AbjEATCQ (ORCPT ); Mon, 1 May 2023 15:02:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 647931708 for ; Mon, 1 May 2023 12:02:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D4CB661EC2 for ; Mon, 1 May 2023 19:02:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D9B4C433A1 for ; Mon, 1 May 2023 19:02:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682967734; bh=9QPEkjebC4OaH1v6v+RGKCi2h6hrTwDOyzCb/vnJn5I=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=W4LOzGRL1mrQlQrZscoCvoVjxeshoZzoRSTj02giG8+P7TZrJbYs6vV3ZikRwjQWw m3TgWWHcCWuhFVvva5I1ksVvQ8GFK+ddPO0K2nmYZSd9P9cvhZwJRKgIviuBemi2nl i9FtIuyDq5edtLMfLjVkIvi071hO6twem7cnkOQtXdw8Fy8LJW1XZiH2i7zo0J4BbM TfcEXy9J9/TYMWr3idHC8nqd2vZJQOZRuvg++dcpih9FhBTF00zouPwmpXnPTKv0QS 2n6hSweoc7pxn99WlwS2emcO/BKtPtHah1387Wc2TocdoRTWObFk2caxd4LLJE76/g Sibsf6I5Bb9PA== Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-50bcb4a81ceso1091866a12.2 for ; Mon, 01 May 2023 12:02:14 -0700 (PDT) X-Gm-Message-State: AC+VfDyGfwBcrK7v78/oEAnX2bMIf+jJnZxnU5wNz1T1/IlBAV9SkIwA nrSxgktkm512mwX1NdYruZVbMZyD1QIT06E0CTgogA== X-Received: by 2002:aa7:c3d9:0:b0:4fe:19cb:4788 with SMTP id l25-20020aa7c3d9000000b004fe19cb4788mr5988608edr.42.1682967732371; Mon, 01 May 2023 12:02:12 -0700 (PDT) MIME-Version: 1.0 References: <6f3aea05c9cc46094b029cbd1138d163c1ae7f9d.1682890156.git.lstoakes@gmail.com> In-Reply-To: <6f3aea05c9cc46094b029cbd1138d163c1ae7f9d.1682890156.git.lstoakes@gmail.com> From: Andy Lutomirski Date: Mon, 1 May 2023 12:02:00 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 3/3] mm: perform the mapping_map_writable() check after call_mmap() To: Lorenzo Stoakes Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Matthew Wilcox , Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, Jan Kara , Hugh Dickins Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Sun, Apr 30, 2023 at 3:26=E2=80=AFPM Lorenzo Stoakes wrote: > > In order for a F_SEAL_WRITE sealed memfd mapping to have an opportunity t= o > 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() chec= k > before we had the opportunity to avoid it. Is there any reason this can't go before patch 3? If I'm understanding correctly, a comment like the following might make this a lot more comprehensible: > > 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=3D217238 > 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 646e34e95a37..1608d7f5a293 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -2642,17 +2642,17 @@ unsigned long mmap_region(struct file *file, unsi= gned long addr, > vma->vm_pgoff =3D pgoff; > > if (file) { > - if (is_shared_maywrite(vm_flags)) { > - error =3D mapping_map_writable(file->f_mapping); > - if (error) > - goto free_vma; > - } > - > vma->vm_file =3D get_file(file); > error =3D call_mmap(file, vma); > if (error) > goto unmap_and_free_vma; > /* vm_ops->mmap() may have changed vma->flags. Check for writability now. = */ > + if (vma_is_shared_maywrite(vma)) { > + error =3D mapping_map_writable(file->f_mapping); > + if (error) > + goto close_and_free_vma; > + } > + Alternatively, if anyone is nervous about the change in ordering here, there could be a whole new vm_op like adjust_vma_flags() that happens before any of this. --Andy