Skip to contentSkip to conversation

code for raceless /sys/fs/foofs/*

PATCH-RFC Nikita Danilov 5 participants 8 messages May 5, 2004 – May 6, 2004 VFS

lore
Share & tools
lore
#0 lore raw

Hello,

attached patch adds code necessary to safely export per-super-block information in /sys/fs and /proc/fs.

Common problem with exporting file system information in procfs or sysfs is a race between method that inputs/outputs data and concurrent umount of the super-block involved.

To this end patch introduces new struct sb_entry. sb_entries are embedded into sysfs/procfs objects. When such object is created its sb_entry is added to the per-super-block list &sb->s_entries protected by sb->s_umount semaphore. During umount sb->s_entries is scanned and all entries are marked "dead".

To avoid races with umount, code has to be bracketed between get_sb_entry() and put_sb_entry(). get_sb_entry() uses sget() to verify that sb_entry's super-block is still valid, and to pin it in memory. Super-block is guaranteed to remain valid until corresponding call to put_sb_entry(). This is the same approach as in Alexander Viro's fix of fs/reiserfs/procfs.c code.

But there is one more race left: call to put_sb_entry() may cause file-system module to be unloaded while calling thread is still executing file-system code. To avoid this (in sysfs case) struct fs_kobject and struct fs_kattr are added that wrap file-system ->store() and ->show() methods with calls to {get,put}_sb_entry() done from the generic code.

Note: there is no in-tree users of this currently, but reiser4 wants to exports information in /sys/fs/reiser4, hence RFC.

Nikita.

include/linux/fs.h renamed from include/linux/fs.h~kobject-umount-race +66 -0
diff -puN -b include/linux/fs.h~kobject-umount-race include/linux/fs.h
@@ -679,6 +679,28 @@ extern int send_sigurg(struct fown_struc
 extern struct list_head super_blocks;
 extern spinlock_t sb_lock;
 
+/*
+ * sb_entry is attached to some super block (register_sb_entry()). It
+ * can be detached manually (unregister_sb_entry()), or automatically by
+ * umount (see deactivate_super()). {get,put}_sb_entry() can be used to
+ * guarantee liveness of entry. Typical usage is to have sb_entry along
+ * with kobject or proc_dir_entry; see struct fs_kobject.
+ */
+
+struct sb_entry {
+	/* linkage into &s->entries */
+	struct list_head linkage;
+	/* true is entry is attached to @s */
+	int live;
+	/* file system type for this entry. This is necessary, because
+	 * entry liveness should be checkable in the situation when ->s
+	 * may already be invalid. */
+	struct file_system_type *type;
+	/* superblock this entry is attached to. Can only be inspected
+	 * after successful call to get_sb_entry() */
+	struct super_block *s;
+};
+
 #define sb_entry(list)	list_entry((list), struct super_block, s_list)
 #define S_BIAS (1<<30)
 struct super_block {
@@ -724,8 +746,52 @@ struct super_block {
 	 * even looking at it. You had been warned.
 	 */
 	struct semaphore s_vfs_rename_sem;	/* Kludge */
+	struct list_head s_entries;             /* list of
+						 * sb_entries. Protected
+						 * by ->s_umount. */
+};
+
+/*
+ * file system kobject.
+ *
+ * fs_kobject is used to export per-super-block information in sysfs
+ * while providing synchronization against concurrent umount. To this
+ * end it includes struct sb_entry that is attached to the super block
+ * by fs_kobject_register() and detached by fs_kobject_unregister().
+ *
+ * We need these wrappers (see fs/super.c:fs_kattr_{show,store}()),
+ * because it's impossible to handle module unloading races properly
+ * from within file-system code. Viz get_sb_entry() avoids umount races
+ * by acquiring reference to the super block (through sget()), but this
+ * may very well be the _last_ reference to the file-system, and
+ * put_sb_entry() will trigger module unload, which means that
+ * put_sb_entry() should be called from the generic code rather than
+ * from file-system module.
+ */
+struct fs_kobject {
+	struct sb_entry entry;
+	struct kobject  kobj; /* it should be kobj for compatibility
+			       * with silly kobject.h macros */
+};
+
+/*
+ * file system kattr. See struct fs_kobject.
+ */
+struct fs_kattr {
+	struct attribute kattr;
+	ssize_t (*show) (struct super_block *,
+			 struct fs_kobject *, struct fs_kattr *, char *);
+	ssize_t (*store) (struct super_block *, struct fs_kobject *,
+			  struct fs_kattr *, const char *, size_t);
 };
 
+/* in fs/super.c */
+
+int fs_kobject_register(struct super_block *s, struct fs_kobject * fskobj);
+void fs_kobject_unregister(struct fs_kobject * fskobj);
+
+extern struct sysfs_ops fs_attr_ops;
+
 /*
  * Superblock locking.
  */
fs/super.c renamed from fs/super.c~kobject-umount-race +202 -0

222 lines not on this page · raw message

_

#1 lore raw
Quoted 8 lines · Nikita Danilov

Hello,

attached patch adds code necessary to safely export per-super-block information in /sys/fs and /proc/fs.

Common problem with exporting file system information in procfs or sysfs is a race between method that inputs/outputs data and concurrent umount of the super-block involved.

Aside of the implementation questions (will comment later), there is an interface problem here. We end up allowing anyone who has sysfs mounted (in chroot jail, in limited namespace, etc.) to pin down _any_ reiser4 superblock, whether they have the thing itself mounted or not.

#2 lore raw
Quoted 6 lines · On Wed, May 05, 2004 at 05:28:03PM +0100…
Quoted 5 lines · Nikita Danilov

Hello,

attached patch adds code necessary to safely export per-super-block information in /sys/fs and /proc/fs.

Quoted 8 lines
Quoted 3 lines

Common problem with exporting file system information in procfs or sysfs is a race between method that inputs/outputs data and concurrent umount of the super-block involved.

Aside of the implementation questions (will comment later), there is an interface problem here. We end up allowing anyone who has sysfs mounted (in chroot jail, in limited namespace, etc.) to pin down _any_ reiser4 superblock, whether they have the thing itself mounted or not.

[sorry about truncated message - -ENOCOFFEE hits]

We also allow anyone with sysfs mounted to see which filesystems are currently mounted on the box - again, regardless of being able to see them in the chroot jail/restricted namespace/etc. It can easily become an issue in setups where such information is sensitive.

#3 lore raw
Quoted 4 lines · On Wed, 2004-05-05 at 12:36, [email protected] wrote:

We also allow anyone with sysfs mounted to see which filesystems are currently mounted on the box - again, regardless of being able to see them in the chroot jail/restricted namespace/etc. It can easily become an issue in setups where such information is sensitive.

...but are you *really* likely to be mounting sysfs in a chrooted jail or restricted namespace?

...and if you do, aren't you more likely to simply 'mount --bind' those minimal parts of sysfs that you actually need for the given process that is gaoled?

Cheers,
Trond

#4 lore raw
Quoted 8 lines · Trond Myklebust
Quoted 4 lines · On Wed, 2004-05-05 at 12:36, [email protected] wrote:

We also allow anyone with sysfs mounted to see which filesystems are currently mounted on the box - again, regardless of being able to see them in the chroot jail/restricted namespace/etc. It can easily become an issue in setups where such information is sensitive.

Quoted 6 lines

...but are you *really* likely to be mounting sysfs in a chrooted jail or restricted namespace?

...and if you do, aren't you more likely to simply 'mount --bind' those minimal parts of sysfs that you actually need for the given process that is gaoled?

With the way things are going on, I suspect that it will be hard to maintain. For one thing, more and more is put there. For another, we certainly _do_ need a way to export per-superblock data and "all or nothing" approach will be more and more painful as new applications of that show up.

Don't get me wrong - I absolutely agree that there is a need of safe way to export per-fs data in some way; it is true for NFS, it is about the only sane way to do online defragmentation, etc.

This is going to be used much wider than reiser4 and as soon as mechanism goes in we'll see a bunch of applications. The only real question is what sort of API would be the right one, and that's why I don't like the solutions along the lines of "you'll never want to use that in restricted setups".

#5 lore raw
In reply to Trond Myklebust

Replying to Trond Myklebust:

Quoted 3 lines

...and if you do, aren't you more likely to simply 'mount --bind' those minimal parts of sysfs that you actually need for the given process that is gaoled?

selinux comes on my mind ...

Signature · 2 lines
Paul P 'Stingray' Komkoff Jr // http://stingr.net/key <- my pgp key
 This message represents the official view of the voices in my head
#6 lore raw
In reply to [email protected]
Quoted 16 lines · [email protected] writes:
Quoted 6 lines · On Wed, May 05, 2004 at 05:28:03PM +0100…
Quoted 5 lines · Nikita Danilov

Hello,

attached patch adds code necessary to safely export per-super-block information in /sys/fs and /proc/fs.

Quoted 8 lines
Quoted 3 lines

Common problem with exporting file system information in procfs or sysfs is a race between method that inputs/outputs data and concurrent umount of the super-block involved.

Aside of the implementation questions (will comment later), there is an interface problem here. We end up allowing anyone who has sysfs mounted (in chroot jail, in limited namespace, etc.) to pin down _any_ reiser4 superblock, whether they have the thing itself mounted or not.

Quoted 6 lines

[sorry about truncated message - -ENOCOFFEE hits]

We also allow anyone with sysfs mounted to see which filesystems are currently mounted on the box - again, regardless of being able to see them in the chroot jail/restricted namespace/etc. It can easily become an issue in setups where such information is sensitive.

But isn't this a problem with sysfs in general? Restricted process still observes all devices, busses, etc. through /sys. If such information is sensitive, shouldn't there be some way to selectively mount only portions of kobject trees? For example, when file system is mounted, its /sys/fs/foofs/sb-id tree is mounted in the same namespace. That is, we need something like

    int sysfs_mount(struct kobject *root, char *mountpoint);

What I am concerned about is that building special foofs-meta file-system on top of fs/libfs.c for each file-system foofs would lead to the code duplication, while sysfs/kobjects already have all the paraphernalia in place.

Nikita.

#7 lore raw
Quoted 4 lines · Nikita Danilov

But isn't this a problem with sysfs in general? Restricted process still observes all devices, busses, etc. through /sys. If such information is sensitive, shouldn't there be some way to selectively mount only portions of kobject trees?

Alternatively, there's a nice security module infrastructure - use that to restrict who can view given subtrees of /sys. Currently, SELinux is able to slice-n-dice the /proc filesystem for different accesses, but code would need to be written to do it for /sys.