Skip to content

Commit 8654df4

Browse files
committed
mnt: Refactor fs_fully_visible into mount_too_revealing
Replace the call of fs_fully_visible in do_new_mount from before the new superblock is allocated with a call of mount_too_revealing after the superblock is allocated. This winds up being a much better location for maintainability of the code. The first change this enables is the replacement of FS_USERNS_VISIBLE with SB_I_USERNS_VISIBLE. Moving the flag from struct filesystem_type to sb_iflags on the superblock. Unfortunately mount_too_revealing fundamentally needs to touch mnt_flags adding several MNT_LOCKED_XXX flags at the appropriate times. If the mnt_flags did not need to be touched the code could be easily moved into the filesystem specific mount code. Acked-by: Seth Forshee <seth.forshee@canonical.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 695e9df commit 8654df4

5 files changed

Lines changed: 32 additions & 17 deletions

File tree

‎fs/namespace.c‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
23752375
return err;
23762376
}
23772377

2378-
static bool fs_fully_visible(struct file_system_type *fs_type, int *new_mnt_flags);
2378+
static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
23792379

23802380
/*
23812381
* create a new mount for userspace and request it to be added into the
@@ -2408,12 +2408,6 @@ static int do_new_mount(struct path *path, const char *fstype, int flags,
24082408
flags |= MS_NODEV;
24092409
mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
24102410
}
2411-
if (type->fs_flags & FS_USERNS_VISIBLE) {
2412-
if (!fs_fully_visible(type, &mnt_flags)) {
2413-
put_filesystem(type);
2414-
return -EPERM;
2415-
}
2416-
}
24172411
}
24182412

24192413
mnt = vfs_kern_mount(type, flags, name, data);
@@ -2425,6 +2419,11 @@ static int do_new_mount(struct path *path, const char *fstype, int flags,
24252419
if (IS_ERR(mnt))
24262420
return PTR_ERR(mnt);
24272421

2422+
if (mount_too_revealing(mnt, &mnt_flags)) {
2423+
mntput(mnt);
2424+
return -EPERM;
2425+
}
2426+
24282427
err = do_add_mount(real_mount(mnt), path, mnt_flags);
24292428
if (err)
24302429
mntput(mnt);
@@ -3216,22 +3215,19 @@ bool current_chrooted(void)
32163215
return chrooted;
32173216
}
32183217

3219-
static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
3218+
static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
3219+
int *new_mnt_flags)
32203220
{
3221-
struct mnt_namespace *ns = current->nsproxy->mnt_ns;
32223221
int new_flags = *new_mnt_flags;
32233222
struct mount *mnt;
32243223
bool visible = false;
32253224

3226-
if (unlikely(!ns))
3227-
return false;
3228-
32293225
down_read(&namespace_sem);
32303226
list_for_each_entry(mnt, &ns->list, mnt_list) {
32313227
struct mount *child;
32323228
int mnt_flags;
32333229

3234-
if (mnt->mnt.mnt_sb->s_type != type)
3230+
if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
32353231
continue;
32363232

32373233
/* This mount is not fully visible if it's root directory
@@ -3298,6 +3294,22 @@ static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
32983294
return visible;
32993295
}
33003296

3297+
static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
3298+
{
3299+
struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3300+
unsigned long s_iflags;
3301+
3302+
if (ns->user_ns == &init_user_ns)
3303+
return false;
3304+
3305+
/* Can this filesystem be too revealing? */
3306+
s_iflags = mnt->mnt_sb->s_iflags;
3307+
if (!(s_iflags & SB_I_USERNS_VISIBLE))
3308+
return false;
3309+
3310+
return !mnt_already_visible(ns, mnt, new_mnt_flags);
3311+
}
3312+
33013313
static struct ns_common *mntns_get(struct task_struct *task)
33023314
{
33033315
struct ns_common *ns = NULL;

‎fs/proc/inode.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ int proc_fill_super(struct super_block *s)
462462
struct inode *root_inode;
463463
int ret;
464464

465+
s->s_iflags |= SB_I_USERNS_VISIBLE;
465466
s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
466467
s->s_blocksize = 1024;
467468
s->s_blocksize_bits = 10;

‎fs/proc/root.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static struct file_system_type proc_fs_type = {
158158
.name = "proc",
159159
.mount = proc_mount,
160160
.kill_sb = proc_kill_sb,
161-
.fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
161+
.fs_flags = FS_USERNS_MOUNT,
162162
};
163163

164164
void __init proc_root_init(void)

‎fs/sysfs/mount.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
4242
kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
4343
else if (new_sb)
4444
/* Userspace would break if executables appear on sysfs */
45-
root->d_sb->s_iflags |= SB_I_NOEXEC;
45+
root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC;
4646

4747
return root;
4848
}
@@ -59,7 +59,7 @@ static struct file_system_type sysfs_fs_type = {
5959
.name = "sysfs",
6060
.mount = sysfs_mount,
6161
.kill_sb = sysfs_kill_sb,
62-
.fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
62+
.fs_flags = FS_USERNS_MOUNT,
6363
};
6464

6565
int __init sysfs_init(void)

‎include/linux/fs.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,9 @@ struct mm_struct;
13281328
#define SB_I_CGROUPWB 0x00000001 /* cgroup-aware writeback enabled */
13291329
#define SB_I_NOEXEC 0x00000002 /* Ignore executables on this fs */
13301330

1331+
/* sb->s_iflags to limit user namespace mounts */
1332+
#define SB_I_USERNS_VISIBLE 0x00000010 /* fstype already mounted */
1333+
13311334
/* Possible states of 'frozen' field */
13321335
enum {
13331336
SB_UNFROZEN = 0, /* FS is unfrozen */
@@ -2011,7 +2014,6 @@ struct file_system_type {
20112014
#define FS_HAS_SUBTYPE 4
20122015
#define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
20132016
#define FS_USERNS_DEV_MOUNT 16 /* A userns mount does not imply MNT_NODEV */
2014-
#define FS_USERNS_VISIBLE 32 /* FS must already be visible */
20152017
#define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
20162018
struct dentry *(*mount) (struct file_system_type *, int,
20172019
const char *, void *);

0 commit comments

Comments
 (0)