linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Matthew Wilcox <matthew@wil.cx>, Ryan Lortie <desrt@desrt.ca>,
	Hugh Dickins <hughd@google.com>,
	Johannes Weiner <hannes@cmpxchg.org>, Kay Sievers <kay@vrfy.org>,
	dri-devel@lists.freedesktop.org, Daniel Mack <zonque@gmail.com>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	Karol Lewandowski <k.lewandowsk@samsung.com>,
	Lennart Poettering <lennart@poettering.net>,
	Greg Kroah-Hartman <greg@kroah.com>, Tejun Heo <tj@kernel.org>,
	"Michael Kerrisk \(man-pages\)" <mtk.manpages@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>
Subject: [PATCH 1/6] fs: fix i_writecount on shmem and friends
Date: Wed, 19 Mar 2014 20:06:46 +0100	[thread overview]
Message-ID: <1395256011-2423-2-git-send-email-dh.herrmann@gmail.com> (raw)
In-Reply-To: <1395256011-2423-1-git-send-email-dh.herrmann@gmail.com>

VM_DENYWRITE currently relies on i_writecount. Unless there's an active
writable reference to an inode, VM_DENYWRITE is not allowed.
Unfortunately, alloc_file() does not increase i_writecount, therefore,
does not prevent a following VM_DENYWRITE even though the new file might
have been opened with FMODE_WRITE. However, callers of alloc_file() expect
the file object to be fully instantiated so they can call fput() on it. We
could now either fix all callers to do an get_write_access() if opened
with FMODE_WRITE, or simply fix alloc_file() to do that. I chose the
latter.

Note that this bug allows some rather subtle misbehavior. The following
sequence of calls should work just fine, but currently fails:
    int p[2], orig, ro, rw;
    char buf[128];

    pipe(p);
    sprintf(buf, "/proc/self/fd/%d", p[1]);
    ro = open(buf, O_RDONLY);
    close(p[1]);
    sprintf(buf, "/proc/self/fd/%d", ro);
    rw = open(buf, O_RDWR);

The final open() cannot succeed as close(p[1]) caused an integer underflow
on i_writecount, effectively causing VM_DENYWRITE on the inode. The open
will fail with -ETXTBUSY.

It's a rather odd sequence of calls and given that open() doesn't use
alloc_file() (and thus not affected by this bug), it's rather unlikely
that this is a serious issue. But stuff like anon_inode shares a *single*
inode across a huge set of interfaces. If any of these is broken like
pipe(), it will affect all of these (ranging from dma-buf to epoll).

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
---
Hi

This patch is only included for reference. It was submitted to fs-devel
separately and is being worked on. However, this bug must be fixed in order to
make use of memfd_create(), so I decided to include it here.

David

 fs/file_table.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/fs/file_table.c b/fs/file_table.c
index 5b24008..8059d68 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -168,6 +168,7 @@ struct file *alloc_file(struct path *path, fmode_t mode,
 		const struct file_operations *fop)
 {
 	struct file *file;
+	int error;
 
 	file = get_empty_filp();
 	if (IS_ERR(file))
@@ -179,15 +180,23 @@ struct file *alloc_file(struct path *path, fmode_t mode,
 	file->f_mode = mode;
 	file->f_op = fop;
 
-	/*
-	 * These mounts don't really matter in practice
-	 * for r/o bind mounts.  They aren't userspace-
-	 * visible.  We do this for consistency, and so
-	 * that we can do debugging checks at __fput()
-	 */
-	if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
-		file_take_write(file);
-		WARN_ON(mnt_clone_write(path->mnt));
+	if (mode & FMODE_WRITE) {
+		error = get_write_access(path->dentry->d_inode);
+		if (error) {
+			put_filp(file);
+			return ERR_PTR(error);
+		}
+
+		/*
+		 * These mounts don't really matter in practice
+		 * for r/o bind mounts.  They aren't userspace-
+		 * visible.  We do this for consistency, and so
+		 * that we can do debugging checks at __fput()
+		 */
+		if (!special_file(path->dentry->d_inode->i_mode)) {
+			file_take_write(file);
+			WARN_ON(mnt_clone_write(path->mnt));
+		}
 	}
 	if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
 		i_readcount_inc(path->dentry->d_inode);
-- 
1.9.0

  reply	other threads:[~2014-03-19 19:06 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-19 19:06 [PATCH 0/6] File Sealing & memfd_create() David Herrmann
2014-03-19 19:06 ` David Herrmann [this message]
2014-03-19 19:06 ` [PATCH 2/6] shm: add sealing API David Herrmann
2014-03-19 19:06 ` [PATCH 3/6] shm: add memfd_create() syscall David Herrmann
2014-03-20  8:47   ` Cyrill Gorcunov
2014-03-20  9:01     ` Pavel Emelyanov
2014-03-20 11:29       ` David Herrmann
2014-03-20 11:50         ` Pavel Emelyanov
2014-03-20 19:22   ` John Stultz
2014-04-02 13:38   ` Konstantin Khlebnikov
2014-04-02 14:18     ` David Herrmann
2014-04-02 14:52       ` Konstantin Khlebnikov
2014-04-10 19:07     ` Andy Lutomirski
2014-03-19 19:06 ` [PATCH 4/6] selftests: add memfd_create() + sealing tests David Herrmann
2014-03-19 19:06 ` [PATCH man-pages 5/6] fcntl.2: document SHMEM_SET/GET_SEALS commands David Herrmann
2014-03-19 19:06 ` [PATCH man-pages 6/6] memfd_create.2: add memfd_create() man-page David Herrmann
2014-03-20  2:55 ` [PATCH 0/6] File Sealing & memfd_create() Greg Kroah-Hartman
2014-03-20  3:49 ` Linus Torvalds
2014-03-20  8:07   ` David Herrmann
2014-03-20 14:41     ` One Thousand Gnomes
2014-03-20 15:12       ` David Herrmann
2014-03-20 15:26         ` One Thousand Gnomes
2014-03-20 15:32 ` tytso
2014-03-20 15:39   ` One Thousand Gnomes
2014-03-20 15:48   ` David Herrmann
2014-03-20 16:38     ` tytso
2014-04-10 19:14       ` Andy Lutomirski
2014-04-10 20:32         ` Theodore Ts'o
2014-04-10 20:37           ` Andy Lutomirski
2014-04-10 20:49             ` David Herrmann
2014-04-10 21:16               ` Andy Lutomirski
2014-04-10 22:57                 ` David Herrmann
2014-04-10 23:05                   ` Andy Lutomirski
2014-04-10 23:16                     ` David Herrmann
2014-04-10 23:32                       ` Andy Lutomirski
2014-04-20 15:03             ` Pavel Machek
2014-06-17  9:48             ` Florian Weimer
2014-06-17 16:21               ` Andy Lutomirski
2014-04-10 14:45   ` Colin Walters
2014-04-10 19:15     ` Andy Lutomirski
2014-04-10 19:45       ` Colin Walters
2014-04-11  6:09         ` Alex Elsayed
2014-04-08 13:00 ` Florian Weimer
2014-04-09 21:31   ` David Herrmann
2014-04-22  9:10     ` Florian Weimer
2014-04-22 11:55       ` David Herrmann
2014-04-22 12:44         ` Florian Weimer
2014-04-22 12:55           ` David Herrmann
2014-04-10 19:17   ` Andy Lutomirski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1395256011-2423-2-git-send-email-dh.herrmann@gmail.com \
    --to=dh.herrmann@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=desrt@desrt.ca \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=greg@kroah.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=k.lewandowsk@samsung.com \
    --cc=kay@vrfy.org \
    --cc=lennart@poettering.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=matthew@wil.cx \
    --cc=mtk.manpages@gmail.com \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zonque@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).