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 D8485C77B60 for ; Thu, 30 Mar 2023 20:48:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51C896B0074; Thu, 30 Mar 2023 16:48:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CC316B007D; Thu, 30 Mar 2023 16:48:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36C1C6B007E; Thu, 30 Mar 2023 16:48:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2438B6B0074 for ; Thu, 30 Mar 2023 16:48:07 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D7CE8141142 for ; Thu, 30 Mar 2023 20:48:06 +0000 (UTC) X-FDA: 80626751772.14.5D7C774 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf17.hostedemail.com (Postfix) with ESMTP id DA22F40016 for ; Thu, 30 Mar 2023 20:48:04 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amacapital-net.20210112.gappssmtp.com header.s=20210112 header.b=NXQHFDie; spf=pass (imf17.hostedemail.com: domain of luto@amacapital.net designates 209.85.214.178 as permitted sender) smtp.mailfrom=luto@amacapital.net; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680209285; 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:references:dkim-signature; bh=yHtvAvYwk+OhjqsGKAxXW2/s3HWisKfHheFojy3Y8AA=; b=zemnF8ApGoCnW1xFC69Qc+cPw01f8OtYHoDsJYWivNkdGWHumai2bTa3qSvDobl9vZJdCZ HZUGHumqEFHuMQh2gk8qwvcMzqS+gA7xOz8ySsn7RLR2M/Cq3m1A2/fFDZaEUJ+YUZWSRH q3ybfLZFx3lF077MjFYJMl9XdlML33s= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=amacapital-net.20210112.gappssmtp.com header.s=20210112 header.b=NXQHFDie; spf=pass (imf17.hostedemail.com: domain of luto@amacapital.net designates 209.85.214.178 as permitted sender) smtp.mailfrom=luto@amacapital.net; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680209285; a=rsa-sha256; cv=none; b=188I5v03+1Y1UitzsNygOONAu6FYpZOqLRON2rABWWCyEWHv/j+OU2HUBacvv1N70vKuLV P0vu16p/UplJ64IVhvbMP7JghnD/MDi9djWke8hq5bkFch+rW08NgEusORXMUpfBjVHrIU aOwSEEat1ffk8hWs3sKQwVLJwVsn5sc= Received: by mail-pl1-f178.google.com with SMTP id o2so19313628plg.4 for ; Thu, 30 Mar 2023 13:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20210112.gappssmtp.com; s=20210112; t=1680209283; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=yHtvAvYwk+OhjqsGKAxXW2/s3HWisKfHheFojy3Y8AA=; b=NXQHFDieUokj38zhwMXA0WsZ5C6fHk62gQwULTmLxQ+BbUnH0DN29jlwITrPvNw5Oc 4qck2RKfSDH4cKoVnj3UegXM6uDuDUOlQcjSYjN0CQQ39rl1PvVS509AqwJ6YUMphHli 9mon9KkF1vjoNufI6NV7KWX0LZXkVoCKqzMwJdoyvoZ/wRcG6HE6bQtB2RceLQrDZbB3 HEUKiPauZaght+a/i2LPEjQce46EpklQkiH3jT1mwxUVY4EzNmBwhOdVvekb4FcqpbE+ F0CvyEWpmhdX0vF4CeYNP+5QfYPPVS7VFxhDj4CVLq6OoIYbt7PjE7wivSrfMIujEjsz JcEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680209283; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yHtvAvYwk+OhjqsGKAxXW2/s3HWisKfHheFojy3Y8AA=; b=SZmbHG4qgvdE7m1hLrUs8RGhkF0DCU05zS+HGV/eq4JWkq7aTG4bFlmgy6H5j9KbA5 bwoZf+so0Mk2UYDtRlQsFTRfoaKbcweMx61608u331al7j7mdJteTHWEwItpBzZZqV5b NH3z9iFrI46FiyRethtxNEPGrvyRNKKgGbGMAQ4VOR3eL0e54Tz40dE1ndoROgqQhY2B t2QWBKnwXg4Pp8SgGPFTiX3pqkn68JIhseL/IlUbGq5VFCuAstiApoTxdCp7hfkgR4Z1 vRWPwDJsj4a3GaCyFfd85MCmtNQtuIWvjidIs8jgJKGyDNJtoMH30EoDz8L30+V0+Eq2 7NxQ== X-Gm-Message-State: AAQBX9c5LFUzaRp4LmZ0UMXtRef/fqkR4GR2eHkTCNjISZqB4M9W/Vll 4dVoeUi9CtCwWEdKf9JCcmPUhA== X-Google-Smtp-Source: AKy350a0n3fP0Bbyri4tgQbFuiaNErFnMu3T14WeVddrBfk3DsWEQjOH9H/wAtp1INVgBd/pDJ77Mw== X-Received: by 2002:a17:902:e0c3:b0:1a2:92a4:f38c with SMTP id e3-20020a170902e0c300b001a292a4f38cmr1786983pla.13.1680209283634; Thu, 30 Mar 2023 13:48:03 -0700 (PDT) Received: from smtpclient.apple ([216.9.110.9]) by smtp.gmail.com with ESMTPSA id y1-20020a63de41000000b0050bc4ca9024sm223279pgi.65.2023.03.30.13.48.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Mar 2023 13:48:03 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Andy Lutomirski Mime-Version: 1.0 (1.0) Subject: Re: [Bug 217238] New: Creating shared read-only map is denied after add write seal to a memfd Date: Thu, 30 Mar 2023 13:47:48 -0700 Message-Id: <6793EAB9-CF91-425A-B278-8A5D4415AD72@amacapital.net> References: Cc: Andrew Morton , linux-mm@kvack.org, David Herrmann , yshuiv7@gmail.com, bugzilla-daemon@kernel.org, linux-api@vger.kernel.org, linux-man@vger.kernel.org, Michael Kerrisk , Andy Lutomirski In-Reply-To: To: Lorenzo Stoakes X-Mailer: iPhone Mail (20D67) X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: DA22F40016 X-Stat-Signature: q8weuzmjq8u8fqbujxefjtear4kcxi94 X-Rspam-User: X-HE-Tag: 1680209284-720780 X-HE-Meta: U2FsdGVkX1/2uQ2vWIUuJqoodq5FSEDOmFMj1cPyorXCdmv0JT+mw8ovJTyXodmBksvkCvzV6qE6l+FDSlUF7+Mj1Ts/JUGnsmAhqyObFLnGB6Dj+Dw7eIKRzA1w5B+z00FKtB2kXiQ/JIEsdPLU7WebCPtwzMee+xvz8Yh1yRppAy2p0HbhUSPzemd4i2TruNCVk7Gjg6LXQKfFUIqV/thcY8hCsiVzC2Ugz0Xk1Ip6q25m86ywvlyng1+vWEdpm5RpKJEACfM/tSrjPYNNyVksOPlnki0tYWeNfxbisrKQwxXKtgLKS3AaNEjkjUyh0/kVJ/I5jL+vm6aM/6bkF1MYgAEhnrZ30yVYp5BkQ+yAEQfmFCKpaO5/DjJOfn+un/gGlgpPv8kDCNDyfyhzNlEALHmMWkLvHzF/pZRVyWIIaN0CvOpmTyJajJCjtUU17sHhmHZyRlhkRdLT5i8TVKfrrwYOBD7hepzNG8RgJv3N8mW3RRoaOsoRrAU1yX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h3yqRQpUr288QyjZSMfpYGBJJ88SGdKVrD7KmjJIFoKEj0IX+L6SJK8UmXPG5dajk 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: > On Mar 30, 2023, at 12:25 PM, Lorenzo Stoakes wrote: >=20 > =EF=BB=BFOn Sat, Mar 25, 2023 at 02:51:05PM +0000, Lorenzo Stoakes wrote: >>> On Fri, Mar 24, 2023 at 01:36:46PM -0700, Andrew Morton wrote: >>> (switched to email. Please respond via emailed reply-to-all, not via th= e >>> bugzilla web interface). >>>=20 >>>> On Fri, 24 Mar 2023 03:34:23 +0000 bugzilla-daemon@kernel.org wrote: >>>=20 >>>> https://bugzilla.kernel.org/show_bug.cgi?id=3D217238 >>>>=20 >>>> Bug ID: 217238 >>>> Summary: Creating shared read-only map is denied after add >>>> write seal to a memfd >>>> Product: Memory Management >>>> Version: 2.5 >>>> Kernel Version: 6.2.8 >>>> Hardware: All >>>> OS: Linux >>>> Tree: Mainline >>>> Status: NEW >>>> Severity: normal >>>> Priority: P1 >>>> Component: Other >>>> Assignee: akpm@linux-foundation.org >>>> Reporter: yshuiv7@gmail.com >>>> Regression: No >>>>=20 >>>> Test case: >>>>=20 >>>> int main() { >>>> int fd =3D memfd_create("test", MFD_ALLOW_SEALING); >>>> write(fd, "test", 4); >>>> fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE); >>>>=20 >>>> void *ret =3D mmap(NULL, 4, PROT_READ, MAP_SHARED, fd, 0); >>>> } >>>>=20 >>>> This fails with EPERM. This is in contradiction with what's described i= n the >>>> documentation of F_SEAL_WRITE. >>>>=20 >>>> -- >>>> You may reply to this email to add a comment. >>>>=20 >>>> You are receiving this mail because: >>>> You are the assignee for the bug. >>>=20 >>=20 >> This issue seems to be the result of the use of the memfd's shmem region'= s >> page cache object (struct address_space)'s i_mmap_writable field to denot= e >> whether it is write-sealed. >>=20 >> The kernel assumes that a VM_SHARED mapping might become writable at any >> time via mprotect(), therefore treats VM_SHARED mappings as if they were >> writable as far as i_mmap_writable is concerned (this field's primary use= >> is to determine whether, for architectures that require it, flushing must= >> occur if this is set to avoid aliasing, see filemap_read() for example). >>=20 >> In theory we could convert all such checks to VM_SHARED | VM_WRITE >> (importantly including on fork) and then update mprotect() to check >> mapping_map_writable() if a user tries to make unwritable memory >> writable. >>=20 Unless I=E2=80=99m missing something, we have VM_MAYWRITE for almost exactly= this purpose. Can=E2=80=99t we just make a shared mapping with both of the= se bits clear? >> I suspect however there are reasons relating to locking that make it >> unreasonable to try to do this, but I may be mistaken (others might have >> some insight on this). I also see some complexity around this in the >> security checks on marking shared writable mappings executable (e.g. in >> mmap_violation_check()). >>=20 >> In any case, it doesn't really make much sense to have a write-sealed >> shared mapping, since you're essentially saying 'nothing _at all_ can wri= te >> to this' so it may as well be private. The semantics are unfortunate here= , >> the memory will still be shared read-only by MAP_PRIVATE mappings. >>=20 >> A better choice here might be F_SEAL_FUTURE_WRITE (available from kernel >>> =3D5.1) which does permit shared read-only mappings as this is explicitl= y >> checked for in seal_check_future_write() invoked from shmem_mmap(). >>=20 >> Regardless, I think the conclusion is that this is not a bug, but rather >> that the documentation needs to be updated. >>=20 >=20 > Adding docs people to cc list (sorry didn't think to do this in first > reply).