Skip to contentSkip to conversation

vfs: Fix crashes when reading /proc/mounts

PATCH Superseded Jan Kara 1 participant 3 messages Oct 18, 2018 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author reviewer
#1 lore raw

PATCH 1/2 vfs: Provide function to grab superblock reference

In reply to Jan Kara

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
index d410186bc369..608f4f276ba5 100644
@@ -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
index f3a8c008e164..f60a07f794cc 100644
@@ -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

author reviewer
#0 lore raw

cover vfs: Fix crashes when reading /proc/mounts

Hello,

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

author reviewer
#2 lore raw

PATCH 2/2 proc: Protect readers of /proc/mounts from remount

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.

---
 fs/mount.h     |  1 +
 fs/namespace.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 56 insertions(+), 4 deletions(-)
fs/mount.h +1 -0
index f39bc9da4d73..5ca0a9e714b3 100644
@@ -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