Skip to content

Commit 6e4eab5

Browse files
committed
fs: Add user namespace member to struct super_block
Start marking filesystems with a user namespace owner, s_user_ns. In this change this is only used for permission checks of who may mount a filesystem. Ultimately s_user_ns will be used for translating ids and checking capabilities for filesystems mounted from user namespaces. The default policy for setting s_user_ns is implemented in sget(), which arranges for s_user_ns to be set to current_user_ns() and to ensure that the mounter of the filesystem has CAP_SYS_ADMIN in that user_ns. The guts of sget are split out into another function sget_userns(). The function sget_userns calls alloc_super with the specified user namespace or it verifies the existing superblock that was found has the expected user namespace, and fails with EBUSY when it is not. This failing prevents users with the wrong privileges mounting a filesystem. The reason for the split of sget_userns from sget is that in some cases such as mount_ns and kernfs_mount_ns a different policy for permission checking of mounts and setting s_user_ns is necessary, and the existence of sget_userns() allows those policies to be implemented. The helper mount_ns is expected to be used for filesystems such as proc and mqueuefs which present per namespace information. The function mount_ns is modified to call sget_userns instead of sget to ensure the user namespace owner of the namespace whose information is presented by the filesystem is used on the superblock. For sysfs and cgroup the appropriate permission checks are already in place, and kernfs_mount_ns is modified to call sget_userns so that the init_user_ns is the only user namespace used. For the cgroup filesystem cgroup namespace mounts are bind mounts of a subset of the full cgroup filesystem and as such s_user_ns must be the same for all of them as there is only a single superblock. Mounts of sysfs that vary based on the network namespace could in principle change s_user_ns but it keeps the analysis and implementation of kernfs simpler if that is not supported, and at present there appear to be no benefits from supporting a different s_user_ns on any sysfs mount. Getting the details of setting s_user_ns correct has been a long process. Thanks to Pavel Tikhorirorv who spotted a leak in sget_userns. Thanks to Seth Forshee who has kept the work alive. Thanks-to: Seth Forshee <seth.forshee@canonical.com> Thanks-to: Pavel Tikhomirov <ptikhomirov@virtuozzo.com> Acked-by: Seth Forshee <seth.forshee@canonical.com> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent e94591d commit 6e4eab5

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎fs/kernfs/mount.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
241241
info->root = root;
242242
info->ns = ns;
243243

244-
sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
244+
sb = sget_userns(fs_type, kernfs_test_super, kernfs_set_super, flags,
245+
&init_user_ns, info);
245246
if (IS_ERR(sb) || sb->s_fs_info != info)
246247
kfree(info);
247248
if (IS_ERR(sb))

‎fs/super.c‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/cleancache.h>
3434
#include <linux/fsnotify.h>
3535
#include <linux/lockdep.h>
36+
#include <linux/user_namespace.h>
3637
#include "internal.h"
3738

3839

@@ -165,6 +166,7 @@ static void destroy_super(struct super_block *s)
165166
list_lru_destroy(&s->s_inode_lru);
166167
security_sb_free(s);
167168
WARN_ON(!list_empty(&s->s_mounts));
169+
put_user_ns(s->s_user_ns);
168170
kfree(s->s_subtype);
169171
kfree(s->s_options);
170172
call_rcu(&s->rcu, destroy_super_rcu);
@@ -174,11 +176,13 @@ static void destroy_super(struct super_block *s)
174176
* alloc_super - create new superblock
175177
* @type: filesystem type superblock should belong to
176178
* @flags: the mount flags
179+
* @user_ns: User namespace for the super_block
177180
*
178181
* Allocates and initializes a new &struct super_block. alloc_super()
179182
* returns a pointer new superblock or %NULL if allocation had failed.
180183
*/
181-
static struct super_block *alloc_super(struct file_system_type *type, int flags)
184+
static struct super_block *alloc_super(struct file_system_type *type, int flags,
185+
struct user_namespace *user_ns)
182186
{
183187
struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
184188
static const struct super_operations default_op;
@@ -188,6 +192,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
188192
return NULL;
189193

190194
INIT_LIST_HEAD(&s->s_mounts);
195+
s->s_user_ns = get_user_ns(user_ns);
191196

192197
if (security_sb_alloc(s))
193198
goto fail;
@@ -443,17 +448,18 @@ void generic_shutdown_super(struct super_block *sb)
443448
EXPORT_SYMBOL(generic_shutdown_super);
444449

445450
/**
446-
* sget - find or create a superblock
451+
* sget_userns - find or create a superblock
447452
* @type: filesystem type superblock should belong to
448453
* @test: comparison callback
449454
* @set: setup callback
450455
* @flags: mount flags
456+
* @user_ns: User namespace for the super_block
451457
* @data: argument to each of them
452458
*/
453-
struct super_block *sget(struct file_system_type *type,
459+
struct super_block *sget_userns(struct file_system_type *type,
454460
int (*test)(struct super_block *,void *),
455461
int (*set)(struct super_block *,void *),
456-
int flags,
462+
int flags, struct user_namespace *user_ns,
457463
void *data)
458464
{
459465
struct super_block *s = NULL;
@@ -466,6 +472,14 @@ struct super_block *sget(struct file_system_type *type,
466472
hlist_for_each_entry(old, &type->fs_supers, s_instances) {
467473
if (!test(old, data))
468474
continue;
475+
if (user_ns != old->s_user_ns) {
476+
spin_unlock(&sb_lock);
477+
if (s) {
478+
up_write(&s->s_umount);
479+
destroy_super(s);
480+
}
481+
return ERR_PTR(-EBUSY);
482+
}
469483
if (!grab_super(old))
470484
goto retry;
471485
if (s) {
@@ -478,7 +492,7 @@ struct super_block *sget(struct file_system_type *type,
478492
}
479493
if (!s) {
480494
spin_unlock(&sb_lock);
481-
s = alloc_super(type, flags);
495+
s = alloc_super(type, flags, user_ns);
482496
if (!s)
483497
return ERR_PTR(-ENOMEM);
484498
goto retry;
@@ -501,6 +515,31 @@ struct super_block *sget(struct file_system_type *type,
501515
return s;
502516
}
503517

518+
EXPORT_SYMBOL(sget_userns);
519+
520+
/**
521+
* sget - find or create a superblock
522+
* @type: filesystem type superblock should belong to
523+
* @test: comparison callback
524+
* @set: setup callback
525+
* @flags: mount flags
526+
* @data: argument to each of them
527+
*/
528+
struct super_block *sget(struct file_system_type *type,
529+
int (*test)(struct super_block *,void *),
530+
int (*set)(struct super_block *,void *),
531+
int flags,
532+
void *data)
533+
{
534+
struct user_namespace *user_ns = current_user_ns();
535+
536+
/* Ensure the requestor has permissions over the target filesystem */
537+
if (!(flags & MS_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
538+
return ERR_PTR(-EPERM);
539+
540+
return sget_userns(type, test, set, flags, user_ns, data);
541+
}
542+
504543
EXPORT_SYMBOL(sget);
505544

506545
void drop_super(struct super_block *sb)
@@ -930,7 +969,8 @@ struct dentry *mount_ns(struct file_system_type *fs_type,
930969
if (!(flags & MS_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
931970
return ERR_PTR(-EPERM);
932971

933-
sb = sget(fs_type, ns_test_super, ns_set_super, flags, ns);
972+
sb = sget_userns(fs_type, ns_test_super, ns_set_super, flags,
973+
user_ns, ns);
934974
if (IS_ERR(sb))
935975
return ERR_CAST(sb);
936976

‎include/linux/fs.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,13 @@ struct super_block {
14321432
struct workqueue_struct *s_dio_done_wq;
14331433
struct hlist_head s_pins;
14341434

1435+
/*
1436+
* Owning user namespace and default context in which to
1437+
* interpret filesystem uids, gids, quotas, device nodes,
1438+
* xattrs and security labels.
1439+
*/
1440+
struct user_namespace *s_user_ns;
1441+
14351442
/*
14361443
* Keep the lru lists last in the structure so they always sit on their
14371444
* own individual cachelines.
@@ -2056,6 +2063,11 @@ void deactivate_locked_super(struct super_block *sb);
20562063
int set_anon_super(struct super_block *s, void *data);
20572064
int get_anon_bdev(dev_t *);
20582065
void free_anon_bdev(dev_t);
2066+
struct super_block *sget_userns(struct file_system_type *type,
2067+
int (*test)(struct super_block *,void *),
2068+
int (*set)(struct super_block *,void *),
2069+
int flags, struct user_namespace *user_ns,
2070+
void *data);
20592071
struct super_block *sget(struct file_system_type *type,
20602072
int (*test)(struct super_block *,void *),
20612073
int (*set)(struct super_block *,void *),

0 commit comments

Comments
 (0)