Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2771763imu; Sun, 11 Nov 2018 00:12:29 -0800 (PST) X-Google-Smtp-Source: AJdET5fWKRgmAg9Z6OZ6nlEDXqmz6eP4HuEc3ZyYIW0iyibYwxMhoDejXN+o8348YXHhqUNb1yPq X-Received: by 2002:a17:902:74c9:: with SMTP id f9-v6mr15022021plt.144.1541923949586; Sun, 11 Nov 2018 00:12:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541923949; cv=none; d=google.com; s=arc-20160816; b=nDtLx2MX7C1GEZ9RUVshg4YZPxcKMQSk+7i1yGpCt17cUDWTIqMQIA0pFxJm7Dr41z g21tTVcsyhaQAVA/r6ofXalyBsaJAQoD3mzrYCi06Ls4A1aqXtDAxleo9XbAvgMD/R63 3Dpz6qZHITiW7Qj1/SnwURbSoyGdq+IYvHVdswOXq3NiHtK1dZnLsDA5ypeVkIvHtldk 82Khj1GV3jlKkii7txroV+zLRtIeiXoIDljNlVhL+0t2zD43OnoC7c3LM3+e5EX+v3is 5286AQrSVbGZOJ8LUYJCLfJpKTLCMJsKAJ44O2FubtgC0NzbYfi7PGQLQbhYukAQpr9c WUpQ== 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-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=nMvC/wNYlO0B6TOz+wd5rvRIysnAzdwKx+ZUX+JpDag=; b=BtqoYy+lqe8+zr5PEZD/7ADpRNa9VVuFQaYkr7IlHW32gAutDpV1SmVvtNiBOIeoAL dswzHm6NYqsYRf6lNTHkaCTpKGp5L+OXbt8Zu0jAIngqTCyImQOR9RgqCDPP2LCCPLLa +Dm+QwLWC9GsJ5x2XpwL81O829+8kMar8M+HNPCX5s4hD74WuGsHSjiLawuzpQcA+x86 5/ViszUHZjHoziZv+0NBR1NWzlXG1d0JFcVQ6X09oZ/IWxJjG55UvQy7voAkQz6DePx7 wjcRR5FSQc6XVNpXTEkphQInpjKANZYp1D1r0qc2wg3F95NofADVOmdELxazR0+1K4fl EZWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=YRRgzfV2; 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 1-v6si14124145plw.81.2018.11.11.00.12.14; Sun, 11 Nov 2018 00:12:29 -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=YRRgzfV2; 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 S1727490AbeKKR5k (ORCPT + 99 others); Sun, 11 Nov 2018 12:57:40 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:40758 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727344AbeKKR5k (ORCPT ); Sun, 11 Nov 2018 12:57:40 -0500 Received: by mail-pg1-f194.google.com with SMTP id z10so2666506pgp.7 for ; Sun, 11 Nov 2018 00:09:48 -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:content-transfer-encoding:in-reply-to :user-agent; bh=nMvC/wNYlO0B6TOz+wd5rvRIysnAzdwKx+ZUX+JpDag=; b=YRRgzfV2dqUl5Dta9lHErm2XXtY8uTFKdG5KOSvykhYJ8igsu63uTwiVzJ5yipSiny B+lCWxmk93agn3iAkyag7eY7U9YSlMi/KR8RAIj84cdc8Qaxe4YgZfoHMbRSOOoZQCRK F2jnWtOw3pxh5YMuvGSEYUuA1aOcqVPEWCzcU= 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:content-transfer-encoding :in-reply-to:user-agent; bh=nMvC/wNYlO0B6TOz+wd5rvRIysnAzdwKx+ZUX+JpDag=; b=FbJ9VMbSRl8WCfxrOPyXSU8E+V768Nb7DvVzvBXNPEcVIjJJLSco9+zmBtGG4h9w3C QJeQBywCm7nZaRcwsaVY9AYqeGR6tIKq3oO4p0YY3htQegGJPeHGXVueU5D4VRZNsUXb Klhztuc15JSErZFaeAi3YT4dx7gMYpDtjZBrcJxMNrOtrxrKjst7XhOhUkBb1VAweszA GG5txK6oRFcNhwq55t8fkgyrcEdnzgc7HFmXrrEAUecWpG3O/6Hi3pbrVaLv+CWfzFwm zcTjv+Ngx87GnEun2DcXFymipOHMF90O3hoKwGdN0C6CZitO5e9BUeRUmc0kGe9xWU0T eP2A== X-Gm-Message-State: AGRZ1gLFlx0IPhJdQkR92T70QRqiOlihtaINVLibbGqe7iHT9t89iH+Q U0ZtrlFZyLTdZ1MxbuYpn/oMyA== X-Received: by 2002:a62:d452:: with SMTP id u18-v6mr7407811pfl.32.1541923787348; Sun, 11 Nov 2018 00:09:47 -0800 (PST) Received: from localhost ([2620:0:1000:1601:3aef:314f:b9ea:889f]) by smtp.gmail.com with ESMTPSA id g7-v6sm13029361pfo.139.2018.11.11.00.09.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 11 Nov 2018 00:09:46 -0800 (PST) Date: Sun, 11 Nov 2018 00:09:45 -0800 From: Joel Fernandes To: Andy Lutomirski Cc: Daniel Colascione , Jann Horn , kernel list , John Reck , John Stultz , Todd Kjos , Greg Kroah-Hartman , Christoph Hellwig , Al Viro , Andrew Morton , Bruce Fields , Jeff Layton , Khalid Aziz , Lei.Yang@windriver.com, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Linux-MM , marcandre.lureau@redhat.com, Mike Kravetz , Minchan Kim , Shuah Khan , Valdis Kletnieks , Hugh Dickins , Linux API Subject: Re: [PATCH v3 resend 1/2] mm: Add an F_SEAL_FUTURE_WRITE seal to memfd Message-ID: <20181111080945.GA78191@google.com> References: <20181110032005.GA22238@google.com> <69CE06CC-E47C-4992-848A-66EB23EE6C74@amacapital.net> <20181110182405.GB242356@google.com> <20181110220933.GB96924@google.com> <907D942E-E321-4BD7-BED7-ACD1D96A3643@amacapital.net> <20181111023808.GA174670@google.com> <543A5181-3A16-438E-B372-97BEC48A74F8@amacapital.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <543A5181-3A16-438E-B372-97BEC48A74F8@amacapital.net> 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 Sat, Nov 10, 2018 at 07:40:10PM -0800, Andy Lutomirski wrote: [...] > >>>>>>> I see two reasonable solutions: > >>>>>>> > >>>>>>> 1. Don’t fiddle with the struct file at all. Instead make the inode flag > >>>>>>> work by itself. > >>>>>> > >>>>>> Currently, the various VFS paths check only the struct file's f_mode to deny > >>>>>> writes of already opened files. This would mean more checking in all those > >>>>>> paths (and modification of all those paths). > >>>>>> > >>>>>> Anyway going with that idea, we could > >>>>>> 1. call deny_write_access(file) from the memfd's seal path which decrements > >>>>>> the inode::i_writecount. > >>>>>> 2. call get_write_access(inode) in the various VFS paths in addition to > >>>>>> checking for FMODE_*WRITE and deny the write (incase i_writecount is negative) > >>>>>> > >>>>>> That will prevent both reopens, and writes from succeeding. However I worry a > >>>>>> bit about 2 not being too familiar with VFS internals, about what the > >>>>>> consequences of doing that may be. > >>>>> > >>>>> IMHO, modifying both the inode and the struct file separately is fine, > >>>>> since they mean different things. In regular filesystems, it's fine to > >>>>> have a read-write open file description for a file whose inode grants > >>>>> write permission to nobody. Speaking of which: is fchmod enough to > >>>>> prevent this attack? > >>>> > >>>> Well, yes and no. fchmod does prevent reopening the file RW, but > >>>> anyone with permissions (owner, CAP_FOWNER) can just fchmod it back. A > >>>> seal is supposed to be irrevocable, so fchmod-as-inode-seal probably > >>>> isn't sufficient by itself. While it might be good enough for Android > >>>> (in the sense that it'll prevent RW-reopens from other security > >>>> contexts to which we send an open memfd file), it's still conceptually > >>>> ugly, IMHO. Let's go with the original approach of just tweaking the > >>>> inode so that open-for-write is permanently blocked. > >>> > >>> Agreed with the idea of modifying both file and inode flags. I was thinking > >>> modifying i_mode may do the trick but as you pointed it probably could be > >>> reverted by chmod or some other attribute setting calls. > >>> > >>> OTOH, I don't think deny_write_access(file) can be reverted from any > >>> user-facing path so we could do that from the seal to prevent the future > >>> opens in write mode. I'll double check and test that out tomorrow. > >>> > >>> > >> > >> This seems considerably more complicated and more fragile than needed. Just > >> add a new F_SEAL_WRITE_FUTURE. Grep for F_SEAL_WRITE and make the _FUTURE > >> variant work exactly like it with two exceptions: > >> > >> - shmem_mmap and maybe its hugetlbfs equivalent should check for it and act > >> accordingly. > > > > There's more to it than that, we also need to block future writes through > > write syscall, so we have to hook into the write path too once the seal is > > set, not just the mmap. That means we have to add code in mm/shmem.c to do > > that in all those handlers, to check for the seal (and hope we didn't miss a > > file_operations handler). Is that what you are proposing? > > The existing code already does this. That’s why I suggested grepping :) > > > > > Also, it means we have to keep CONFIG_TMPFS enabled so that the > > shmem_file_operations write handlers like write_iter are hooked up. Currently > > memfd works even with !CONFIG_TMPFS. > > If so, that sounds like it may already be a bug. > > > > >> - add_seals won’t need the wait_for_pins and mapping_deny_write logic. > >> > >> That really should be all that’s needed. > > > > It seems a fair idea what you're saying. But I don't see how its less > > complex.. IMO its far more simple to have VFS do the denial of the operations > > based on the flags of its datastructures.. and if it works (which I will test > > to be sure it will), then we should be good. > > I agree it’s complicated, but the code is already written. You should just > need to adjust some masks. > Its actually not that bad and a great idea, I did something like the following and it works pretty well. I would say its cleaner than the old approach for sure (and I also added a /proc/pid/fd/N reopen test to the selftest and made sure that issue goes away). Side note: One subtelty I discovered from the existing selftests is once the F_SEAL_WRITE are active, an mmap of PROT_READ and MAP_SHARED region is expected to fail. This is also evident from this code in mmap_region: if (vm_flags & VM_SHARED) { error = mapping_map_writable(file->f_mapping); if (error) goto allow_write_and_free_vma; } ---8<----------------------- From: "Joel Fernandes (Google)" Subject: [PATCH] mm/memfd: implement future write seal using shmem ops Signed-off-by: Joel Fernandes (Google) --- fs/hugetlbfs/inode.c | 2 +- mm/memfd.c | 19 ------------------- mm/shmem.c | 13 ++++++++++--- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 32920a10100e..1978581abfdf 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -530,7 +530,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) inode_lock(inode); /* protected by i_mutex */ - if (info->seals & F_SEAL_WRITE) { + if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { inode_unlock(inode); return -EPERM; } diff --git a/mm/memfd.c b/mm/memfd.c index 5ba9804e9515..a9ece5fab439 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -220,25 +220,6 @@ 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; diff --git a/mm/shmem.c b/mm/shmem.c index 446942677cd4..7dad7efd8b99 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2163,6 +2163,12 @@ int shmem_lock(struct file *file, int lock, struct user_struct *user) static int shmem_mmap(struct file *file, struct vm_area_struct *vma) { + struct shmem_inode_info *info = SHMEM_I(file_inode(file)); + + /* New shared mmaps are not allowed when "future write" seal active. */ + if ((vma->vm_flags & VM_SHARED) && (info->seals & F_SEAL_FUTURE_WRITE)) + return -EPERM; + file_accessed(file); vma->vm_ops = &shmem_vm_ops; if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && @@ -2391,8 +2397,9 @@ shmem_write_begin(struct file *file, struct address_space *mapping, pgoff_t index = pos >> PAGE_SHIFT; /* i_mutex is held by caller */ - if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { - if (info->seals & F_SEAL_WRITE) + if (unlikely(info->seals & (F_SEAL_GROW | + F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { + if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) return -EPERM; if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) return -EPERM; @@ -2657,7 +2664,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); /* protected by i_mutex */ - if (info->seals & F_SEAL_WRITE) { + if (info->seals & F_SEAL_WRITE || info->seals & F_SEAL_FUTURE_WRITE) { error = -EPERM; goto out; } -- 2.19.1.930.g4563a0d9d0-goog