All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Al Viro <viro@ZenIV.linux.org.uk>
Cc: <linux-fsdevel@vger.kernel.org>, Jan Kara <jack@suse.cz>
Subject: [PATCH 2/2] proc: Protect readers of /proc/mounts from remount
Date: Tue, 18 Dec 2018 14:46:42 +0100	[thread overview]
Message-ID: <20181218134642.21219-3-jack@suse.cz> (raw)
In-Reply-To: <20181218134642.21219-1-jack@suse.cz>

Readers of /proc/mounts (and similar files) are currently unprotected
from concurrently running remount on the filesystem they are reporting.
This can not only result in bogus reported information but also in
confusion in filesystems ->show_options callbacks resulting in
use-after-free issues or similar (for example ext4 handling of quota
file names is prone to such races).

Fix the problem by protecting showing of mount information with
sb->s_umount semaphore.

Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/namespace.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index a7f91265ea67..3b7cd4465658 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -1263,7 +1263,7 @@ static void *m_next(struct seq_file *m, void *v, loff_t *pos)
 {
 	struct proc_mounts *p = m->private;
 
-	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
+	p->cached_mount = seq_list_next(p->cached_mount, &p->ns->list, pos);
 	p->cached_index = *pos;
 	return p->cached_mount;
 }
@@ -1276,8 +1276,38 @@ static void m_stop(struct seq_file *m, void *v)
 static int m_show(struct seq_file *m, void *v)
 {
 	struct proc_mounts *p = m->private;
-	struct mount *r = list_entry(v, struct mount, mnt_list);
-	return p->show(m, &r->mnt);
+	struct mount *r;
+	struct super_block *sb;
+	int ret;
+
+restart:
+	r = list_entry(v, struct mount, mnt_list);
+	sb = r->mnt.mnt_sb;
+
+	/* Protect show function against racing remount */
+	if (mount_trylock_super(sb))
+		goto show;
+	/*
+	 * Trylock failed. Since namepace_sem ranks below s_umount (through
+	 * sb->s_umount > dir->i_rwsem > namespace_sem in the mount path), we
+	 * have to drop it, wait for s_umount and then try again to guarantee
+	 * forward progress.
+	 */
+	hold_sb(sb);
+	up_read(&namespace_sem);
+	down_read(&sb->s_umount);
+	down_read(&namespace_sem);
+	v = seq_list_start(&p->ns->list, p->cached_index);
+	if (!sb->s_root || v != p->cached_mount) {
+		drop_super(sb);
+		p->cached_event = p->ns->event;
+		p->cached_mount = v;
+		goto restart;
+	}
+show:
+	ret = p->show(m, &r->mnt);
+	drop_super(sb);
+	return ret;
 }
 
 const struct seq_operations mounts_op = {
-- 
2.16.4

  parent reply	other threads:[~2018-12-18 13:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-18 13:46 [PATCH 0/2 v2] vfs: Fix crashes when reading /proc/mounts Jan Kara
2018-12-18 13:46 ` [PATCH 1/2] vfs: Provide function to grab superblock reference Jan Kara
2018-12-18 13:46 ` Jan Kara [this message]
2019-01-21 16:58 ` [PATCH 0/2 v2] vfs: Fix crashes when reading /proc/mounts Jan Kara
  -- strict thread matches above, loose matches on Subject: below --
2018-12-11 17:24 [PATCH 0/2 RESEND] " Jan Kara
2018-12-11 17:24 ` [PATCH 2/2] proc: Protect readers of /proc/mounts from remount Jan Kara
2018-12-11 18:36   ` Al Viro
2018-12-11 18:37     ` Al Viro
2018-12-11 18:58   ` Al Viro
2018-12-11 19:14     ` Al Viro
2018-12-12 12:56       ` Jan Kara
2018-10-18 13:17 [PATCH 0/2] vfs: Fix crashes when reading /proc/mounts Jan Kara
2018-10-18 13:17 ` [PATCH 2/2] proc: Protect readers of /proc/mounts from remount Jan Kara
2018-10-18 13:17   ` Jan Kara

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=20181218134642.21219-3-jack@suse.cz \
    --to=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@ZenIV.linux.org.uk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.