vfs: Fix crashes when reading /proc/mounts
Jan Kara
reviewercover vfs: Fix crashes when reading /proc/mounts
Hello,
resending this patch series since I got no reply for almost two months. This patch series aims at fixing possible races between functions formatting output for /proc/mounts and friends and remount. Especially ->show_options functions of filesystems are not counting with the fact that options can change under them and thus races can result in bogus output or outright crashes (like was the case with ext4 handling of quota mount option, or udf could crash when printing charset name, or xfs when printing log device name etc.).
Admittedly the patch 2/2 is somewhat ugly because namespace_sem ranks below sb->s_umount semaphore but I think it is bearable. Al, what do you think?
Honza
Jan Kara
reviewerPATCH 1/2 vfs: Provide function to grab superblock reference
Provide function to hold superblock reference when we are sure the superblock is active. This function will get used by code reading /proc/mounts and friends.
--- fs/internal.h | 1 + fs/super.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
fs/internal.h +1 -0
@@ -104,6 +104,7 @@ extern bool trylock_super(struct super_block *sb);extern struct dentry *mount_fs (struct file_system_type *,int ,const char *,void *);extern struct super_block *user_get_super (dev_t ); +extern void hold_sb (struct super_block *sb);/* * open.c
fs/super.c +16 -0
@@ -775,6 +775,22 @@ struct super_block *get_super_exclusive_thawed(struct block_device *bdev) }EXPORT_SYMBOL (get_super_exclusive_thawed); +/** +* hold_sb - get superblock reference for active superblock +* @sb: superblock to get reference for +* +* Get reference to superblock. The caller must make sure the superblock +* is currently active by other means (e.g. holding an active reference +* itself or holding namespace_sem preventing unmount). +*/ +void hold_sb (struct super_block *sb) +{ +spin_lock (&sb_lock); +WARN_ON_ONCE (!atomic_read (&sb->s_active)); + sb->s_count++; +spin_unlock (&sb_lock); +} +/** * get_active_super - get an active reference to the superblock of a device * @bdev: device to get the superblock for
Signature · 1 line
2.16.4
Jan Kara
reviewerPATCH 2/2 proc: Protect readers of /proc/mounts from remount
In reply to Jan KaraReaders 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.
--- fs/mount.h | 1 + fs/namespace.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 4 deletions(-)
fs/mount.h +1 -0
@@ -134,6 +134,7 @@ struct proc_mounts {void *cached_mount;u64 cached_event;loff_t cached_index; +struct super_block *locked_sb; };extern const struct seq_operations mounts_op;
fs/namespace.c +55 -4
88 lines not on this page · raw message
Signature · 1 line
2.16.4
Al Viro
maintainerRe: [PATCH 2/2] proc: Protect readers of /proc/mounts from remount
Quoted 9 lines · Jan Kara
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_umountsemaphore.
Umm... Which tree is it against and what exactly does your hold_sb() do?
Al Viro
maintainerRe: [PATCH 2/2] proc: Protect readers of /proc/mounts from remount
Quoted 8 lines · Al Viro
Quoted 7 lines · Jan Kara
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).
Quoted 4 lines
Quoted 2 lines
Fix the problem by protecting showing of mount information with
sb->s_umountsemaphore.Umm... Which tree is it against and what exactly does your
hold_sb()do?
D'oh... I need more coffee. Nevermind.
Al Viro
maintainerRe: [PATCH 2/2] proc: Protect readers of /proc/mounts from remount
In reply to Jan KaraQuoted 15 lines · Jan Kara
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_umountsemaphore.fs/namespace.c +5
-0+static bool mounts_trylock_super (struct proc_mounts *p,struct super_block *sb) +{ +if (p->locked_sb == sb) +return true; +if (p->locked_sb) {
fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1255
Quoted 10 lines
fs/namespace.c +10
-0+drop_super (p->locked_sb); + p->locked_sb =NULL ; + } +if (down_read_trylock (&sb->s_umount)) { +hold_sb (sb); + p->locked_sb = sb; +return true; + } +return false; +}
Bad calling conventions, and you are paying for those with making hold_sb() non-static (and having it, in the first place).
fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1293
Quoted 9 lines
fs/namespace.c +9
-0+if (mounts_trylock_super (p, sb)) +return p->cached_mount; +/* +* 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);
That. Just hoist that hold_sb() into your trylock (and put it before the down_read_trylock() there, while we are at it). And turn the other caller into
if (unlikely(!.....)) ret = -EAGAIN; else ret = p->show(m, &r->mnt); followed by unconditional drop_super(). And I would probably go for mount_trylock_super(&p->locked_super, sb)
while we are at it, so that it's isolated from proc_mounts and can be moved to fs/super.c
fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1301
Quoted 8 lines
fs/namespace.c +8
-0+up_read (&namespace_sem); +down_read (&sb->s_umount); +/* +* Sb may be dead by now but that just means we won't find it in any +* mount and drop lock & reference anyway. +*/ + p->locked_sb = sb; + goto restart;
No.
if (likely(sb->s_root)) p->locked_sb = sb; else drop_super(sb); goto restart;
Al Viro
maintainerRe: [PATCH 2/2] proc: Protect readers of /proc/mounts from remount
Quoted 32 lines · Al Viro
Quoted 5 lines
fs/namespace.c +5
-0+static bool mounts_trylock_super (struct proc_mounts *p,struct super_block *sb) +{ +if (p->locked_sb == sb) +return true; +if (p->locked_sb) {fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1255
Quoted 10 lines
fs/namespace.c +10
-0+drop_super (p->locked_sb); + p->locked_sb =NULL ; + } +if (down_read_trylock (&sb->s_umount)) { +hold_sb (sb); + p->locked_sb = sb; +return true; + } +return false; +}Bad calling conventions, and you are paying for those with making
hold_sb()non-static (and having it, in the first place).fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1293
Quoted 9 lines
fs/namespace.c +9
-0+if (mounts_trylock_super (p, sb)) +return p->cached_mount; +/* +* 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);That. Just hoist that
hold_sb()into your trylock (and put it before thedown_read_trylock()there, while we are at it). And turn the other caller into
Quoted 8 lines
if (unlikely(!.....)) ret = -EAGAIN; else ret = p->show(m, &r->mnt); followed by unconditional drop_super(). And I would probably go for mount_trylock_super(&p->locked_super, sb)while we are at it, so that it's isolated from
proc_mountsand can be moved tofs/super.c
Looking at it some more... I still hate it ;-/ Take a look at traverse() in fs/seq_file.c and think what kind of clusterfuck will it cause...
Jan Kara
reviewerRe: [PATCH 2/2] proc: Protect readers of /proc/mounts from remount
Quoted 43 lines · Al Viro
Quoted 32 lines · Al Viro
Quoted 5 lines
fs/namespace.c +5
-0+static bool mounts_trylock_super (struct proc_mounts *p,struct super_block *sb) +{ +if (p->locked_sb == sb) +return true; +if (p->locked_sb) {fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1255
Quoted 10 lines
fs/namespace.c +10
-0+drop_super (p->locked_sb); + p->locked_sb =NULL ; + } +if (down_read_trylock (&sb->s_umount)) { +hold_sb (sb); + p->locked_sb = sb; +return true; + } +return false; +}Bad calling conventions, and you are paying for those with making
hold_sb()non-static (and having it, in the first place).fs/namespace.c struct vfsmount *mnt_clone_internal(const struct path *path) · line 1293
Quoted 9 lines
fs/namespace.c +9
-0+if (mounts_trylock_super (p, sb)) +return p->cached_mount; +/* +* 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);That. Just hoist that
hold_sb()into your trylock (and put it before thedown_read_trylock()there, while we are at it). And turn the other caller intoQuoted 8 lines
if (unlikely(!.....)) ret = -EAGAIN; else ret = p->show(m, &r->mnt); followed by unconditional drop_super(). And I would probably go for mount_trylock_super(&p->locked_super, sb)while we are at it, so that it's isolated from
proc_mountsand can be moved tofs/super.c
Quoted 2 lines
Looking at it some more... I still hate it ;-/ Take a look at
traverse()infs/seq_file.cand think what kind of clusterfuck will it cause...
I guess you mean that in case we fail to lock s_umount semaphore, we'll return -EAGAIN and traverse() will abort? That is true but since we return -EAGAIN, callers will call into traverse() again - both do:
while ((err = traverse(m, *ppos)) == -EAGAIN) ;
and then in m_start() we will do the blocking lock on s_umount. I agree it is ugly and twisted but it should be rare...
Now looking at the code, maybe we could avoid this weird retry dance with traverse(). Something like following in m_show():
sb = mnt->mnt_sb; if (mount_trylock_super()) show and done get passive sb reference namespace_unlock(); down_read(&sb->s_umount); namespace_lock(); new_mnt = seq_list_start(); if (new_mnt != mnt) retry show and done
This could be handled completely inside m_show() so no strange retry dance with traverse(). Do you find this better?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR