Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1411268imu; Fri, 9 Nov 2018 16:29:39 -0800 (PST) X-Google-Smtp-Source: AJdET5fTyT+JfUcwBcdBGAj/docGW14TqLjWpFly1bueNHuglKyNfqv3Jj5O3dcvm+4bHdXlA3OT X-Received: by 2002:a17:902:8608:: with SMTP id f8-v6mr10750259plo.95.1541809779181; Fri, 09 Nov 2018 16:29:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541809779; cv=none; d=google.com; s=arc-20160816; b=qGggMbFBt6D1yvbbNnt82zIr3egzInS91+hIf/AMxqgVkFYqBAZ94qYDdQemloBI8Q FZkTsG8V/GqHV4yGuc0PvRAHm4clIPQsqAtZ0qE7V8+7UDtNurCn0KU07yXoEdAzAC9M x4HEbMMI8w/Rz3SWr/GFZ2/jYRD5bJr4XglbPBbESoULbZpRgCXTnXDDHEWwySSEofej PM9eYfj+YP8RWkRoQ6gXObUABJTm7WgnPnqEdYzjSSe/HpcsS+UmOsLyiUsgw6NvrLhH QEyLbYfavPncNo4IGUrpKGEnlJiuPsMa0fhLRakePOYCLpHXnBb3V/gC4eQqldUMOPo+ L7QA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=rSGiimb4wwQ0/s+NRUDVwpBe1mUBs+4hLpom055mtdg=; b=xaaYJVKzwTe2gr3XOVJZMh3+lLSCZtzwGQWh8eXG4mnye+oeBZ5xKDaMYbgFxLU9uT NOGgJSo3QdKjcrn1a+z7sJCEPQ3O42P22tras5K3VeeVP5pWHOvidT6rPhb+Aeqo90by qXFqu+cwi9D8mRfMxAVZYRM0dyf10NvjxOBOIEI74JLIiJJck4YA8gaDS6q2NSEPXdsl erdM0Hiwt9jjtelmLLHp4efQl5xXBg5kaB7oAQFhKQWHymy8SpGHi/XPbVe3mXApdtpj 38SvIenCF3MuhVeXt80sQJxBokIJo7u7l0SIHivr24vi1hiugB3r7WFHUdeCNM9LGKqY NjOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=dEWXr1tW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e13si8055740pgh.251.2018.11.09.16.29.18; Fri, 09 Nov 2018 16:29:39 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=dEWXr1tW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728361AbeKJKLz (ORCPT + 99 others); Sat, 10 Nov 2018 05:11:55 -0500 Received: from mail-oi1-f196.google.com ([209.85.167.196]:39559 "EHLO mail-oi1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727210AbeKJKLz (ORCPT ); Sat, 10 Nov 2018 05:11:55 -0500 Received: by mail-oi1-f196.google.com with SMTP id 192-v6so2987172oii.6; Fri, 09 Nov 2018 16:28:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rSGiimb4wwQ0/s+NRUDVwpBe1mUBs+4hLpom055mtdg=; b=dEWXr1tWeu1cFQXd1B+yz7BpPPlcUXnx0ludlx5wd1xf7YrDsYtWK6vybi28WTs/kX HYkf/AKePyd941zdHQJN3Nh/UmZu0pp9QUxIUhvxae/x0JuQL/G/uwAwBB7Ahz0CDC/8 lml/9GE9KlpcVb/Qu4mvHCjj9ZK/H84GyMTfVK+bacI649qreHi2wxjw19Zda+g98ybt WDXMAcA8K7w8fbqNWKZ3Qw3qLkzJgOT2nq1/FoPJ+/J2NDy9KydfSYrepwjF1KS9qn4V oIsIdrxQ0Lg2OqwXsoG+OIeT1Ga3SJ8WUsAkoMLSU8SUdWfOw2AuoT9tspQBJ3THg9UZ t4aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rSGiimb4wwQ0/s+NRUDVwpBe1mUBs+4hLpom055mtdg=; b=mjdx2ESrsSS1iwp14ahj1bGbnc0KZ0WgNrfYZ0xMZwDk014AzI/OjVZ7cTXMSLWD8H uXia5AyiYfw87x+4hpnA4hS0f9u6OdSPtUQQO97shY+YQgIKjQJYK5Orfq8rZuC5Rc9C MizbuC/Dhr8lRyqAttlCmBdv8jyQ7fM5z4kjVinZM/YueRaDyhIxB3DQhJhVhibqdSzN vICa100w6h7gkxFPShVPvUhE4/AFYtNTe5LkXYsYhy+cIsrMgbO6Y8NgxGW7lBAE0HlV BO2BV3QFeuNFmAvm+4C6Ktkte9hK4sLm8sonPc8IfcX0NPwucpG+0cM9VUGxiiLpha4+ AASg== X-Gm-Message-State: AGRZ1gIYlxNzEcmNIAep75q+GwYW+4Y9rxBcDivvaxxvySPPSnHs1297 5x366jhOzuibtO3aVfpE7RUyG/9CcwjRT+5axrk= X-Received: by 2002:aca:d886:: with SMTP id p128-v6mr6289558oig.346.1541809733286; Fri, 09 Nov 2018 16:28:53 -0800 (PST) MIME-Version: 1.0 References: <20181108041537.39694-1-joel@joelfernandes.org> In-Reply-To: From: Michael Tirado Date: Fri, 9 Nov 2018 20:02:14 +0000 Message-ID: Subject: Re: [PATCH v3 resend 1/2] mm: Add an F_SEAL_FUTURE_WRITE seal to memfd To: Andy Lutomirski Cc: Jann Horn , joel@joelfernandes.org, LKML , jreck@google.com, john.stultz@linaro.org, tkjos@google.com, gregkh@linuxfoundation.org, hch@infradead.org, viro@zeniv.linux.org.uk, Andrew Morton , dancol@google.com, bfields@fieldses.org, jlayton@kernel.org, khalid.aziz@oracle.com, Lei.Yang@windriver.com, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, marcandre.lureau@redhat.com, mike.kravetz@oracle.com, minchan@kernel.org, shuah@kernel.org, valdis.kletnieks@vt.edu, hughd@google.com, linux-api@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 9, 2018 at 9:41 PM Andy Lutomirski wrote: > > > > > On Nov 9, 2018, at 1:06 PM, Jann Horn wrote: > > > > +linux-api for API addition > > +hughd as FYI since this is somewhat related to mm/shmem > > > > On Fri, Nov 9, 2018 at 9:46 PM Joel Fernandes (Google) > > wrote: > >> Android uses ashmem for sharing memory regions. We are looking forward > >> to migrating all usecases of ashmem to memfd so that we can possibly > >> remove the ashmem driver in the future from staging while also > >> benefiting from using memfd and contributing to it. Note staging drivers > >> are also not ABI and generally can be removed at anytime. > >> > >> One of the main usecases Android has is the ability to create a region > >> and mmap it as writeable, then add protection against making any > >> "future" writes while keeping the existing already mmap'ed > >> writeable-region active. This allows us to implement a usecase where > >> receivers of the shared memory buffer can get a read-only view, while > >> the sender continues to write to the buffer. Oh I remember trying this years ago with a new seal, F_SEAL_WRITE_PEER, or something like that. > > > > So you're fiddling around with the file, but not the inode? How are > > you preventing code like the following from re-opening the file as > > writable? > > > > $ cat memfd.c > > #define _GNU_SOURCE > > #include > > #include > > #include > > #include > > #include > > #include > > > > int main(void) { > > int fd = syscall(__NR_memfd_create, "testfd", 0); > > if (fd == -1) err(1, "memfd"); > > char path[100]; > > sprintf(path, "/proc/self/fd/%d", fd); > > int fd2 = open(path, O_RDWR); > > if (fd2 == -1) err(1, "reopen"); > > printf("reopen successful: %d\n", fd2); > > } > > $ gcc -o memfd memfd.c > > $ ./memfd > > reopen successful: 4 > > $ > > The race condition between memfd_create and applying seals in fcntl? I think it would be possible to block new write mappings from peer processes if there is a new memfd_create api that accepts seals. Allowing caller to set a seal like the one I proposed years ago, though in a race-free manner. Then also consider how to properly handle blocking inherited +W mapping through clone/fork. Maybe I'm forgetting some other pitfalls? > > That aside: I wonder whether a better API would be something that > > allows you to create a new readonly file descriptor, instead of > > fiddling with the writability of an existing fd. > > Every now and then I try to write a patch to prevent using proc to reopen a file with greater permission than the original open. > > I like your idea to have a clean way to reopen a a memfd with reduced permissions. But I would make it a syscall instead and maybe make it only work for memfd at first. And the proc issue would need to be fixed, too. IMO the best solution would handle the issue at memfd creation time by removing the race condition.