Commit 0c55cfc4 authored by Eric W. Biederman's avatar Eric W. Biederman

vfs: Allow unprivileged manipulation of the mount namespace.

- Add a filesystem flag to mark filesystems that are safe to mount as
  an unprivileged user.

- Add a filesystem flag to mark filesystems that don't need MNT_NODEV
  when mounted by an unprivileged user.

- Relax the permission checks to allow unprivileged users that have
  CAP_SYS_ADMIN permissions in the user namespace referred to by the
  current mount namespace to be allowed to mount, unmount, and move
  filesystems.
Acked-by: default avatar"Serge E. Hallyn" <serge@hallyn.com>
Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
parent 7a472ef4
...@@ -1269,7 +1269,7 @@ SYSCALL_DEFINE2(umount, char __user *, name, int, flags) ...@@ -1269,7 +1269,7 @@ SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
goto dput_and_out; goto dput_and_out;
retval = -EPERM; retval = -EPERM;
if (!capable(CAP_SYS_ADMIN)) if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
goto dput_and_out; goto dput_and_out;
retval = do_umount(mnt, flags); retval = do_umount(mnt, flags);
...@@ -1295,7 +1295,7 @@ SYSCALL_DEFINE1(oldumount, char __user *, name) ...@@ -1295,7 +1295,7 @@ SYSCALL_DEFINE1(oldumount, char __user *, name)
static int mount_is_safe(struct path *path) static int mount_is_safe(struct path *path)
{ {
if (capable(CAP_SYS_ADMIN)) if (ns_capable(real_mount(path->mnt)->mnt_ns->user_ns, CAP_SYS_ADMIN))
return 0; return 0;
return -EPERM; return -EPERM;
#ifdef notyet #ifdef notyet
...@@ -1633,7 +1633,7 @@ static int do_change_type(struct path *path, int flag) ...@@ -1633,7 +1633,7 @@ static int do_change_type(struct path *path, int flag)
int type; int type;
int err = 0; int err = 0;
if (!capable(CAP_SYS_ADMIN)) if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
if (path->dentry != path->mnt->mnt_root) if (path->dentry != path->mnt->mnt_root)
...@@ -1797,7 +1797,7 @@ static int do_move_mount(struct path *path, const char *old_name) ...@@ -1797,7 +1797,7 @@ static int do_move_mount(struct path *path, const char *old_name)
struct mount *p; struct mount *p;
struct mount *old; struct mount *old;
int err = 0; int err = 0;
if (!capable(CAP_SYS_ADMIN)) if (!ns_capable(real_mount(path->mnt)->mnt_ns->user_ns, CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
if (!old_name || !*old_name) if (!old_name || !*old_name)
return -EINVAL; return -EINVAL;
...@@ -1884,21 +1884,6 @@ static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) ...@@ -1884,21 +1884,6 @@ static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
return ERR_PTR(err); return ERR_PTR(err);
} }
static struct vfsmount *
do_kern_mount(const char *fstype, int flags, const char *name, void *data)
{
struct file_system_type *type = get_fs_type(fstype);
struct vfsmount *mnt;
if (!type)
return ERR_PTR(-ENODEV);
mnt = vfs_kern_mount(type, flags, name, data);
if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
!mnt->mnt_sb->s_subtype)
mnt = fs_set_subtype(mnt, fstype);
put_filesystem(type);
return mnt;
}
/* /*
* add a mount into a namespace's mount tree * add a mount into a namespace's mount tree
*/ */
...@@ -1944,20 +1929,46 @@ static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) ...@@ -1944,20 +1929,46 @@ static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
* create a new mount for userspace and request it to be added into the * create a new mount for userspace and request it to be added into the
* namespace's tree * namespace's tree
*/ */
static int do_new_mount(struct path *path, const char *type, int flags, static int do_new_mount(struct path *path, const char *fstype, int flags,
int mnt_flags, const char *name, void *data) int mnt_flags, const char *name, void *data)
{ {
struct file_system_type *type;
struct user_namespace *user_ns;
struct vfsmount *mnt; struct vfsmount *mnt;
int err; int err;
if (!type) if (!fstype)
return -EINVAL; return -EINVAL;
/* we need capabilities... */ /* we need capabilities... */
if (!capable(CAP_SYS_ADMIN)) user_ns = real_mount(path->mnt)->mnt_ns->user_ns;
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
return -EPERM;
type = get_fs_type(fstype);
if (!type)
return -ENODEV;
if (user_ns != &init_user_ns) {
if (!(type->fs_flags & FS_USERNS_MOUNT)) {
put_filesystem(type);
return -EPERM; return -EPERM;
}
/* Only in special cases allow devices from mounts
* created outside the initial user namespace.
*/
if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
flags |= MS_NODEV;
mnt_flags |= MNT_NODEV;
}
}
mnt = do_kern_mount(type, flags, name, data); mnt = vfs_kern_mount(type, flags, name, data);
if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
!mnt->mnt_sb->s_subtype)
mnt = fs_set_subtype(mnt, fstype);
put_filesystem(type);
if (IS_ERR(mnt)) if (IS_ERR(mnt))
return PTR_ERR(mnt); return PTR_ERR(mnt);
...@@ -2549,7 +2560,7 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, ...@@ -2549,7 +2560,7 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
struct mount *new_mnt, *root_mnt; struct mount *new_mnt, *root_mnt;
int error; int error;
if (!capable(CAP_SYS_ADMIN)) if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
error = user_path_dir(new_root, &new); error = user_path_dir(new_root, &new);
...@@ -2631,8 +2642,13 @@ static void __init init_mount_tree(void) ...@@ -2631,8 +2642,13 @@ static void __init init_mount_tree(void)
struct vfsmount *mnt; struct vfsmount *mnt;
struct mnt_namespace *ns; struct mnt_namespace *ns;
struct path root; struct path root;
struct file_system_type *type;
mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); type = get_fs_type("rootfs");
if (!type)
panic("Can't find rootfs type");
mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
put_filesystem(type);
if (IS_ERR(mnt)) if (IS_ERR(mnt))
panic("Can't create rootfs"); panic("Can't create rootfs");
...@@ -2757,7 +2773,8 @@ static int mntns_install(struct nsproxy *nsproxy, void *ns) ...@@ -2757,7 +2773,8 @@ static int mntns_install(struct nsproxy *nsproxy, void *ns)
struct mnt_namespace *mnt_ns = ns; struct mnt_namespace *mnt_ns = ns;
struct path root; struct path root;
if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_CHROOT)) if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
!nsown_capable(CAP_SYS_CHROOT))
return -EINVAL; return -EINVAL;
if (fs->users != 1) if (fs->users != 1)
......
...@@ -1812,6 +1812,8 @@ struct file_system_type { ...@@ -1812,6 +1812,8 @@ struct file_system_type {
#define FS_REQUIRES_DEV 1 #define FS_REQUIRES_DEV 1
#define FS_BINARY_MOUNTDATA 2 #define FS_BINARY_MOUNTDATA 2
#define FS_HAS_SUBTYPE 4 #define FS_HAS_SUBTYPE 4
#define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
#define FS_USERNS_DEV_MOUNT 16 /* A userns mount does not imply MNT_NODEV */
#define FS_REVAL_DOT 16384 /* Check the paths ".", ".." for staleness */ #define FS_REVAL_DOT 16384 /* Check the paths ".", ".." for staleness */
#define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */ #define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
struct dentry *(*mount) (struct file_system_type *, int, struct dentry *(*mount) (struct file_system_type *, int,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment