Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp491366imu; Fri, 9 Nov 2018 00:50:10 -0800 (PST) X-Google-Smtp-Source: AJdET5fbBwhh6eaiZRdDHTKjiSiKZxdnSopdWd5BNhpEdH+G5GgB/Ncrme104V2HtG4Hwi/XjkE+ X-Received: by 2002:a17:902:4827:: with SMTP id s36-v6mr7771094pld.226.1541753410463; Fri, 09 Nov 2018 00:50:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541753410; cv=none; d=google.com; s=arc-20160816; b=E63sV16DP0KaQ/V2PlGsJn27aK5VAspaUsZ2o0VEak2LOAZrwxS1JnUVOA/ZV/nQns 0scCrJ1Uhi2rKwC2E4/dadUCf4n0kNY6xZFqPkfT6yjkqIpQFEmBed8/4X0acBAe8qVH UfeLRWNhqsO4hWtzv5bEKuS3kdPGdU348oRJggnNXn8zTHPuNdB3GYdjMVqtSqO+CJuA odlEdWqElgVUSry94dMXcI5Qv+M4kLoCtxxUGhtUzZpMM2JWLKFFcsP6fO3/Tk6paqSF VZO0T47AEc0IyrEOvAOOQ9udi5B9ydeECArQmEFMXtv0U1z5WJYhNkNrpg4l5pXK7dgX cbvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=R/hrbUm35PJ6nZfWdm8oo3/XINMe68XP0KMb7dR8xDI=; b=gLcsP5RoeBIlZ2FhB3eFubrNYc5V7S+EVS5m9m5JNV3giYa1ypWZNvtmOObbHV0KyJ jvdoI3ArFG8MzTBeRKdo2k9hRH1j/i9dvEIICjghNt6SnwZ5Ob2p8wBeryI7/UGJZOmu Qy3Df7Paw+fxOvr/7per3rCAF6qklUjRmJtzKE1SZYUXOFpVb0qY9sDHKzWUf1bMSiML dA+run8nMghU6vsxEpeJV+KSXx97ZHDalSh+aL4nPadTsY3ayfMp8Pv+pJ279IFc7Pui Dz8TId62dZLVxWd08w51oyNIhW1pdodRi7RpHWkxSc+bVnJ8LEYjPFKB+dHewQvfJJ7Z Z2cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=LqZn1tLp; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cd11-v6si7986895plb.59.2018.11.09.00.49.54; Fri, 09 Nov 2018 00:50:10 -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=@joelfernandes.org header.s=google header.b=LqZn1tLp; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727755AbeKIS3H (ORCPT + 99 others); Fri, 9 Nov 2018 13:29:07 -0500 Received: from mail-pl1-f195.google.com ([209.85.214.195]:33061 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727598AbeKIS3G (ORCPT ); Fri, 9 Nov 2018 13:29:06 -0500 Received: by mail-pl1-f195.google.com with SMTP id w22-v6so658053plk.0 for ; Fri, 09 Nov 2018 00:49:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=R/hrbUm35PJ6nZfWdm8oo3/XINMe68XP0KMb7dR8xDI=; b=LqZn1tLpeg5592FM9YEnqXtA3xZpTqiB6IcNGqAAsyadn83l2+VfaQu4sdVeBJpKtz kCwN5wAQEGRI1Xrl7hLcShwVOMG3l23FQTyf7fHWc0Ab/YZPdyRFO4QxXHJ5SWIbqNnq DKpFIYTngk8clLF3D+vMZTHf4S/dPj89jZS/c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=R/hrbUm35PJ6nZfWdm8oo3/XINMe68XP0KMb7dR8xDI=; b=LoUEpvZkZt6gWsnJ4iEv6ckNjXsJp9kLgiRsBLNQHoFvltCMx5JocVFQ/E09w6XykM JSRA0Zetja4QLuECjBBsGthzDdx37QBBbilVAWwf6y4PcmwAjy9F2H7mtQtRmDb0LKI7 2pvr/qxQe36713iKp2ymmdscQQatQjcgpCk/WPG7veoB4XME91+7hLFo3O1elCRwAE2Y PwIUurY5Q4EvH1/vnjDmcwpi++RDhbR0gWc1zZyedDafwKDcjUm55xl6t756p1qv7ORn mZrqadCBHds+M9Lp1mUWffzRJ53k2K52UTgHtvxR01PiCpb53tABaA3OfDTbbhds7nd+ 0qAw== X-Gm-Message-State: AGRZ1gK6LsGFJaK+5OqF0MJY7dd43rnlDjcavYvjhXh9tOCwjSp5LFTM zVKtlkJsfbPxf44UIvJwlPoVJlvQ2xU= X-Received: by 2002:a17:902:6a87:: with SMTP id n7-v6mr5078510plk.86.1541753370499; Fri, 09 Nov 2018 00:49:30 -0800 (PST) Received: from localhost ([2620:0:1000:1601:3aef:314f:b9ea:889f]) by smtp.gmail.com with ESMTPSA id b5-v6sm5754812pfe.60.2018.11.09.00.49.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 09 Nov 2018 00:49:29 -0800 (PST) Date: Fri, 9 Nov 2018 00:49:28 -0800 From: Joel Fernandes To: linux-kernel@vger.kernel.org, hch@infradead.org Cc: jreck@google.com, john.stultz@linaro.org, tkjos@google.com, gregkh@linuxfoundation.org, Al Viro , Andrew Morton , dancol@google.com, "J. Bruce Fields" , Jeff Layton , Khalid Aziz , Lei Yang , linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, =?iso-8859-1?Q?Marc-Andr=E9?= Lureau , Mike Kravetz , minchan@kernel.org, Shuah Khan , valdis.kletnieks@vt.edu Subject: Re: [PATCH v3 resend 1/2] mm: Add an F_SEAL_FUTURE_WRITE seal to memfd Message-ID: <20181109084928.GA37614@google.com> References: <20181108041537.39694-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181108041537.39694-1-joel@joelfernandes.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 07, 2018 at 08:15:36PM -0800, 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. > See CursorWindow documentation in Android for more details: > https://developer.android.com/reference/android/database/CursorWindow > > This usecase cannot be implemented with the existing F_SEAL_WRITE seal. > To support the usecase, this patch adds a new F_SEAL_FUTURE_WRITE seal > which prevents any future mmap and write syscalls from succeeding while > keeping the existing mmap active. The following program shows the seal > working in action: > [...] > The output of running this program is as follows: > ret=3 > map 0 passed > write passed > map 1 prot-write passed as expected > future-write seal now active > write failed as expected due to future-write seal > map 2 prot-write failed as expected due to seal > : Permission denied > map 3 prot-read passed as expected > > Cc: jreck@google.com > Cc: john.stultz@linaro.org > Cc: tkjos@google.com > Cc: gregkh@linuxfoundation.org > Cc: hch@infradead.org > Reviewed-by: John Stultz > Signed-off-by: Joel Fernandes (Google) > --- > v1->v2: No change, just added selftests to the series. manpages are > ready and I'll submit them once the patches are accepted. > > v2->v3: Updated commit message to have more support code (John Stultz) > Renamed seal from F_SEAL_FS_WRITE to F_SEAL_FUTURE_WRITE > (Christoph Hellwig) > Allow for this seal only if grow/shrink seals are also > either previous set, or are requested along with this seal. > (Christoph Hellwig) > Added locking to synchronize access to file->f_mode. > (Christoph Hellwig) Christoph, do the patches look Ok to you now? If so, then could you give an Acked-by or Reviewed-by tag? Thanks a lot, - Joel > include/uapi/linux/fcntl.h | 1 + > mm/memfd.c | 22 +++++++++++++++++++++- > 2 files changed, 22 insertions(+), 1 deletion(-) > > diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h > index 6448cdd9a350..a2f8658f1c55 100644 > --- a/include/uapi/linux/fcntl.h > +++ b/include/uapi/linux/fcntl.h > @@ -41,6 +41,7 @@ > #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ > #define F_SEAL_GROW 0x0004 /* prevent file from growing */ > #define F_SEAL_WRITE 0x0008 /* prevent writes */ > +#define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped */ > /* (1U << 31) is reserved for signed error codes */ > > /* > diff --git a/mm/memfd.c b/mm/memfd.c > index 2bb5e257080e..5ba9804e9515 100644 > --- a/mm/memfd.c > +++ b/mm/memfd.c > @@ -150,7 +150,8 @@ static unsigned int *memfd_file_seals_ptr(struct file *file) > #define F_ALL_SEALS (F_SEAL_SEAL | \ > F_SEAL_SHRINK | \ > F_SEAL_GROW | \ > - F_SEAL_WRITE) > + F_SEAL_WRITE | \ > + F_SEAL_FUTURE_WRITE) > > static int memfd_add_seals(struct file *file, unsigned int seals) > { > @@ -219,6 +220,25 @@ static int memfd_add_seals(struct file *file, unsigned int seals) > } > } > > + if ((seals & F_SEAL_FUTURE_WRITE) && > + !(*file_seals & F_SEAL_FUTURE_WRITE)) { > + /* > + * The FUTURE_WRITE seal also prevents growing and shrinking > + * so we need them to be already set, or requested now. > + */ > + int test_seals = (seals | *file_seals) & > + (F_SEAL_GROW | F_SEAL_SHRINK); > + > + if (test_seals != (F_SEAL_GROW | F_SEAL_SHRINK)) { > + error = -EINVAL; > + goto unlock; > + } > + > + spin_lock(&file->f_lock); > + file->f_mode &= ~(FMODE_WRITE | FMODE_PWRITE); > + spin_unlock(&file->f_lock); > + } > + > *file_seals |= seals; > error = 0; > > -- > 2.19.1.930.g4563a0d9d0-goog