Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace

Pull userns vfs updates from Eric Biederman:
 "This tree contains some very long awaited work on generalizing the
  user namespace support for mounting filesystems to include filesystems
  with a backing store.  The real world target is fuse but the goal is
  to update the vfs to allow any filesystem to be supported.  This
  patchset is based on a lot of code review and testing to approach that
  goal.

  While looking at what is needed to support the fuse filesystem it
  became clear that there were things like xattrs for security modules
  that needed special treatment.  That the resolution of those concerns
  would not be fuse specific.  That sorting out these general issues
  made most sense at the generic level, where the right people could be
  drawn into the conversation, and the issues could be solved for
  everyone.

  At a high level what this patchset does a couple of simple things:

   - Add a user namespace owner (s_user_ns) to struct super_block.

   - Teach the vfs to handle filesystem uids and gids not mapping into
     to kuids and kgids and being reported as INVALID_UID and
     INVALID_GID in vfs data structures.

  By assigning a user namespace owner filesystems that are mounted with
  only user namespace privilege can be detected.  This allows security
  modules and the like to know which mounts may not be trusted.  This
  also allows the set of uids and gids that are communicated to the
  filesystem to be capped at the set of kuids and kgids that are in the
  owning user namespace of the filesystem.

  One of the crazier corner casees this handles is the case of inodes
  whose i_uid or i_gid are not mapped into the vfs.  Most of the code
  simply doesn't care but it is easy to confuse the inode writeback path
  so no operation that could cause an inode write-back is permitted for
  such inodes (aka only reads are allowed).

  This set of changes starts out by cleaning up the code paths involved
  in user namespace permirted mounts.  Then when things are clean enough
  adds code that cleanly sets s_user_ns.  Then additional restrictions
  are added that are possible now that the filesystem superblock
  contains owner information.

  These changes should not affect anyone in practice, but there are some
  parts of these restrictions that are changes in behavior.

   - Andy's restriction on suid executables that does not honor the
     suid bit when the path is from another mount namespace (think
     /proc/[pid]/fd/) or when the filesystem was mounted by a less
     privileged user.

   - The replacement of the user namespace implicit setting of MNT_NODEV
     with implicitly setting SB_I_NODEV on the filesystem superblock
     instead.

     Using SB_I_NODEV is a stronger form that happens to make this state
     user invisible.  The user visibility can be managed but it caused
     problems when it was introduced from applications reasonably
     expecting mount flags to be what they were set to.

  There is a little bit of work remaining before it is safe to support
  mounting filesystems with backing store in user namespaces, beyond
  what is in this set of changes.

   - Verifying the mounter has permission to read/write the block device
     during mount.

   - Teaching the integrity modules IMA and EVM to handle filesystems
     mounted with only user namespace root and to reduce trust in their
     security xattrs accordingly.

   - Capturing the mounters credentials and using that for permission
     checks in d_automount and the like.  (Given that overlayfs already
     does this, and we need the work in d_automount it make sense to
     generalize this case).

  Furthermore there are a few changes that are on the wishlist:

   - Get all filesystems supporting posix acls using the generic posix
     acls so that posix_acl_fix_xattr_from_user and
     posix_acl_fix_xattr_to_user may be removed.  [Maintainability]

   - Reducing the permission checks in places such as remount to allow
     the superblock owner to perform them.

   - Allowing the superblock owner to chown files with unmapped uids and
     gids to something that is mapped so the files may be treated
     normally.

  I am not considering even obvious relaxations of permission checks
  until it is clear there are no more corner cases that need to be
  locked down and handled generically.

  Many thanks to Seth Forshee who kept this code alive, and putting up
  with me rewriting substantial portions of what he did to handle more
  corner cases, and for his diligent testing and reviewing of my
  changes"

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace: (30 commits)
  fs: Call d_automount with the filesystems creds
  fs: Update i_[ug]id_(read|write) to translate relative to s_user_ns
  evm: Translate user/group ids relative to s_user_ns when computing HMAC
  dquot: For now explicitly don't support filesystems outside of init_user_ns
  quota: Handle quota data stored in s_user_ns in quota_setxquota
  quota: Ensure qids map to the filesystem
  vfs: Don't create inodes with a uid or gid unknown to the vfs
  vfs: Don't modify inodes with a uid or gid unknown to the vfs
  cred: Reject inodes with invalid ids in set_create_file_as()
  fs: Check for invalid i_uid in may_follow_link()
  vfs: Verify acls are valid within superblock's s_user_ns.
  userns: Handle -1 in k[ug]id_has_mapping when !CONFIG_USER_NS
  fs: Refuse uid/gid changes which don't map into s_user_ns
  selinux: Add support for unprivileged mounts from user namespaces
  Smack: Handle labels consistently in untrusted mounts
  Smack: Add support for unprivileged mounts from user namespaces
  fs: Treat foreign mounts as nosuid
  fs: Limit file caps to the user namespace of the super block
  userns: Remove the now unnecessary FS_USERNS_DEV_MOUNT flag
  userns: Remove implicit MNT_NODEV fragility.
  ...
This commit is contained in:
Linus Torvalds 2016-07-29 15:54:19 -07:00
commit a867d7349e
36 changed files with 418 additions and 213 deletions

View File

@ -415,7 +415,7 @@ static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
return rc; return rc;
} }
rc = posix_acl_valid(acl); rc = posix_acl_valid(&init_user_ns, acl);
if (rc) { if (rc) {
CERROR("validate acl: %d\n", rc); CERROR("validate acl: %d\n", rc);
posix_acl_release(acl); posix_acl_release(acl);

View File

@ -266,7 +266,7 @@ static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
if (IS_ERR(acl)) if (IS_ERR(acl))
return PTR_ERR(acl); return PTR_ERR(acl);
else if (acl) { else if (acl) {
retval = posix_acl_valid(acl); retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
if (retval) if (retval)
goto err_out; goto err_out;
} }

View File

@ -255,6 +255,25 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de
if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
return 0; return 0;
/*
* Verify that uid/gid changes are valid in the target
* namespace of the superblock.
*/
if (ia_valid & ATTR_UID &&
!kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
return -EOVERFLOW;
if (ia_valid & ATTR_GID &&
!kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
return -EOVERFLOW;
/* Don't allow modifications of files with invalid uids or
* gids unless those uids & gids are being made valid.
*/
if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
return -EOVERFLOW;
if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
return -EOVERFLOW;
error = security_inode_setattr(dentry, attr); error = security_inode_setattr(dentry, attr);
if (error) if (error)
return error; return error;

View File

@ -1846,7 +1846,7 @@ struct block_device *lookup_bdev(const char *pathname)
if (!S_ISBLK(inode->i_mode)) if (!S_ISBLK(inode->i_mode))
goto fail; goto fail;
error = -EACCES; error = -EACCES;
if (path.mnt->mnt_flags & MNT_NODEV) if (!may_open_dev(&path))
goto fail; goto fail;
error = -ENOMEM; error = -ENOMEM;
bdev = bd_acquire(inode); bdev = bd_acquire(inode);

View File

@ -396,6 +396,7 @@ devpts_fill_super(struct super_block *s, void *data, int silent)
{ {
struct inode *inode; struct inode *inode;
s->s_iflags &= ~SB_I_NODEV;
s->s_blocksize = 1024; s->s_blocksize = 1024;
s->s_blocksize_bits = 10; s->s_blocksize_bits = 10;
s->s_magic = DEVPTS_SUPER_MAGIC; s->s_magic = DEVPTS_SUPER_MAGIC;
@ -480,7 +481,7 @@ static struct file_system_type devpts_fs_type = {
.name = "devpts", .name = "devpts",
.mount = devpts_mount, .mount = devpts_mount,
.kill_sb = devpts_kill_sb, .kill_sb = devpts_kill_sb,
.fs_flags = FS_USERNS_MOUNT | FS_USERNS_DEV_MOUNT, .fs_flags = FS_USERNS_MOUNT,
}; };
/* /*

View File

@ -1411,7 +1411,7 @@ static void bprm_fill_uid(struct linux_binprm *bprm)
bprm->cred->euid = current_euid(); bprm->cred->euid = current_euid();
bprm->cred->egid = current_egid(); bprm->cred->egid = current_egid();
if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) if (!mnt_may_suid(bprm->file->f_path.mnt))
return; return;
if (task_no_new_privs(current)) if (task_no_new_privs(current))

View File

@ -1619,6 +1619,13 @@ bool atime_needs_update(const struct path *path, struct inode *inode)
if (inode->i_flags & S_NOATIME) if (inode->i_flags & S_NOATIME)
return false; return false;
/* Atime updates will likely cause i_uid and i_gid to be written
* back improprely if their true value is unknown to the vfs.
*/
if (HAS_UNMAPPED_ID(inode))
return false;
if (IS_NOATIME(inode)) if (IS_NOATIME(inode))
return false; return false;
if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)) if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))

View File

@ -152,6 +152,8 @@ static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
struct dentry *root; struct dentry *root;
info->sb = sb; info->sb = sb;
/* Userspace would break if executables or devices appear on sysfs */
sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
sb->s_blocksize = PAGE_SIZE; sb->s_blocksize = PAGE_SIZE;
sb->s_blocksize_bits = PAGE_SHIFT; sb->s_blocksize_bits = PAGE_SHIFT;
sb->s_magic = magic; sb->s_magic = magic;
@ -241,7 +243,8 @@ struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
info->root = root; info->root = root;
info->ns = ns; info->ns = ns;
sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info); sb = sget_userns(fs_type, kernfs_test_super, kernfs_set_super, flags,
&init_user_ns, info);
if (IS_ERR(sb) || sb->s_fs_info != info) if (IS_ERR(sb) || sb->s_fs_info != info)
kfree(info); kfree(info);
if (IS_ERR(sb)) if (IS_ERR(sb))

View File

@ -36,6 +36,7 @@
#include <linux/posix_acl.h> #include <linux/posix_acl.h>
#include <linux/hash.h> #include <linux/hash.h>
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/init_task.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include "internal.h" #include "internal.h"
@ -410,6 +411,14 @@ int __inode_permission(struct inode *inode, int mask)
*/ */
if (IS_IMMUTABLE(inode)) if (IS_IMMUTABLE(inode))
return -EACCES; return -EACCES;
/*
* Updating mtime will likely cause i_uid and i_gid to be
* written back improperly if their true value is unknown
* to the vfs.
*/
if (HAS_UNMAPPED_ID(inode))
return -EACCES;
} }
retval = do_inode_permission(inode, mask); retval = do_inode_permission(inode, mask);
@ -901,6 +910,7 @@ static inline int may_follow_link(struct nameidata *nd)
{ {
const struct inode *inode; const struct inode *inode;
const struct inode *parent; const struct inode *parent;
kuid_t puid;
if (!sysctl_protected_symlinks) if (!sysctl_protected_symlinks)
return 0; return 0;
@ -916,7 +926,8 @@ static inline int may_follow_link(struct nameidata *nd)
return 0; return 0;
/* Allowed if parent directory and link owner match. */ /* Allowed if parent directory and link owner match. */
if (uid_eq(parent->i_uid, inode->i_uid)) puid = parent->i_uid;
if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
return 0; return 0;
if (nd->flags & LOOKUP_RCU) if (nd->flags & LOOKUP_RCU)
@ -1089,6 +1100,7 @@ static int follow_automount(struct path *path, struct nameidata *nd,
bool *need_mntput) bool *need_mntput)
{ {
struct vfsmount *mnt; struct vfsmount *mnt;
const struct cred *old_cred;
int err; int err;
if (!path->dentry->d_op || !path->dentry->d_op->d_automount) if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
@ -1110,11 +1122,16 @@ static int follow_automount(struct path *path, struct nameidata *nd,
path->dentry->d_inode) path->dentry->d_inode)
return -EISDIR; return -EISDIR;
if (path->dentry->d_sb->s_user_ns != &init_user_ns)
return -EACCES;
nd->total_link_count++; nd->total_link_count++;
if (nd->total_link_count >= 40) if (nd->total_link_count >= 40)
return -ELOOP; return -ELOOP;
old_cred = override_creds(&init_cred);
mnt = path->dentry->d_op->d_automount(path); mnt = path->dentry->d_op->d_automount(path);
revert_creds(old_cred);
if (IS_ERR(mnt)) { if (IS_ERR(mnt)) {
/* /*
* The filesystem is allowed to return -EISDIR here to indicate * The filesystem is allowed to return -EISDIR here to indicate
@ -2741,10 +2758,11 @@ EXPORT_SYMBOL(__check_sticky);
* c. have CAP_FOWNER capability * c. have CAP_FOWNER capability
* 6. If the victim is append-only or immutable we can't do antyhing with * 6. If the victim is append-only or immutable we can't do antyhing with
* links pointing to it. * links pointing to it.
* 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. * 7. If the victim has an unknown uid or gid we can't change the inode.
* 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
* 9. We can't remove a root or mountpoint. * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
* 10. We don't allow removal of NFS sillyrenamed files; it's handled by * 10. We can't remove a root or mountpoint.
* 11. We don't allow removal of NFS sillyrenamed files; it's handled by
* nfs_async_unlink(). * nfs_async_unlink().
*/ */
static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
@ -2766,7 +2784,7 @@ static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
return -EPERM; return -EPERM;
if (check_sticky(dir, inode) || IS_APPEND(inode) || if (check_sticky(dir, inode) || IS_APPEND(inode) ||
IS_IMMUTABLE(inode) || IS_SWAPFILE(inode)) IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
return -EPERM; return -EPERM;
if (isdir) { if (isdir) {
if (!d_is_dir(victim)) if (!d_is_dir(victim))
@ -2787,16 +2805,22 @@ static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
* 1. We can't do it if child already exists (open has special treatment for * 1. We can't do it if child already exists (open has special treatment for
* this case, but since we are inlined it's OK) * this case, but since we are inlined it's OK)
* 2. We can't do it if dir is read-only (done in permission()) * 2. We can't do it if dir is read-only (done in permission())
* 3. We should have write and exec permissions on dir * 3. We can't do it if the fs can't represent the fsuid or fsgid.
* 4. We can't do it if dir is immutable (done in permission()) * 4. We should have write and exec permissions on dir
* 5. We can't do it if dir is immutable (done in permission())
*/ */
static inline int may_create(struct inode *dir, struct dentry *child) static inline int may_create(struct inode *dir, struct dentry *child)
{ {
struct user_namespace *s_user_ns;
audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
if (child->d_inode) if (child->d_inode)
return -EEXIST; return -EEXIST;
if (IS_DEADDIR(dir)) if (IS_DEADDIR(dir))
return -ENOENT; return -ENOENT;
s_user_ns = dir->i_sb->s_user_ns;
if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
!kgid_has_mapping(s_user_ns, current_fsgid()))
return -EOVERFLOW;
return inode_permission(dir, MAY_WRITE | MAY_EXEC); return inode_permission(dir, MAY_WRITE | MAY_EXEC);
} }
@ -2865,6 +2889,12 @@ int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
} }
EXPORT_SYMBOL(vfs_create); EXPORT_SYMBOL(vfs_create);
bool may_open_dev(const struct path *path)
{
return !(path->mnt->mnt_flags & MNT_NODEV) &&
!(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
}
static int may_open(struct path *path, int acc_mode, int flag) static int may_open(struct path *path, int acc_mode, int flag)
{ {
struct dentry *dentry = path->dentry; struct dentry *dentry = path->dentry;
@ -2883,7 +2913,7 @@ static int may_open(struct path *path, int acc_mode, int flag)
break; break;
case S_IFBLK: case S_IFBLK:
case S_IFCHR: case S_IFCHR:
if (path->mnt->mnt_flags & MNT_NODEV) if (!may_open_dev(path))
return -EACCES; return -EACCES;
/*FALLTHRU*/ /*FALLTHRU*/
case S_IFIFO: case S_IFIFO:
@ -4135,6 +4165,13 @@ int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_de
*/ */
if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
return -EPERM; return -EPERM;
/*
* Updating the link count will likely cause i_uid and i_gid to
* be writen back improperly if their true value is unknown to
* the vfs.
*/
if (HAS_UNMAPPED_ID(inode))
return -EPERM;
if (!dir->i_op->link) if (!dir->i_op->link)
return -EPERM; return -EPERM;
if (S_ISDIR(inode->i_mode)) if (S_ISDIR(inode->i_mode))

View File

@ -2186,13 +2186,7 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
} }
if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
!(mnt_flags & MNT_NODEV)) { !(mnt_flags & MNT_NODEV)) {
/* Was the nodev implicitly added in mount? */ return -EPERM;
if ((mnt->mnt_ns->user_ns != &init_user_ns) &&
!(sb->s_type->fs_flags & FS_USERNS_DEV_MOUNT)) {
mnt_flags |= MNT_NODEV;
} else {
return -EPERM;
}
} }
if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
!(mnt_flags & MNT_NOSUID)) { !(mnt_flags & MNT_NOSUID)) {
@ -2376,7 +2370,7 @@ static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
return err; return err;
} }
static bool fs_fully_visible(struct file_system_type *fs_type, int *new_mnt_flags); static bool mount_too_revealing(struct vfsmount *mnt, int *new_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
@ -2386,7 +2380,6 @@ 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 file_system_type *type;
struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
struct vfsmount *mnt; struct vfsmount *mnt;
int err; int err;
@ -2397,26 +2390,6 @@ static int do_new_mount(struct path *path, const char *fstype, int flags,
if (!type) if (!type)
return -ENODEV; return -ENODEV;
if (user_ns != &init_user_ns) {
if (!(type->fs_flags & FS_USERNS_MOUNT)) {
put_filesystem(type);
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_LOCK_NODEV;
}
if (type->fs_flags & FS_USERNS_VISIBLE) {
if (!fs_fully_visible(type, &mnt_flags)) {
put_filesystem(type);
return -EPERM;
}
}
}
mnt = vfs_kern_mount(type, flags, name, data); mnt = vfs_kern_mount(type, flags, name, data);
if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
!mnt->mnt_sb->s_subtype) !mnt->mnt_sb->s_subtype)
@ -2426,6 +2399,11 @@ static int do_new_mount(struct path *path, const char *fstype, int flags,
if (IS_ERR(mnt)) if (IS_ERR(mnt))
return PTR_ERR(mnt); return PTR_ERR(mnt);
if (mount_too_revealing(mnt, &mnt_flags)) {
mntput(mnt);
return -EPERM;
}
err = do_add_mount(real_mount(mnt), path, mnt_flags); err = do_add_mount(real_mount(mnt), path, mnt_flags);
if (err) if (err)
mntput(mnt); mntput(mnt);
@ -3217,22 +3195,19 @@ bool current_chrooted(void)
return chrooted; return chrooted;
} }
static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags) static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
int *new_mnt_flags)
{ {
struct mnt_namespace *ns = current->nsproxy->mnt_ns;
int new_flags = *new_mnt_flags; int new_flags = *new_mnt_flags;
struct mount *mnt; struct mount *mnt;
bool visible = false; bool visible = false;
if (unlikely(!ns))
return false;
down_read(&namespace_sem); down_read(&namespace_sem);
list_for_each_entry(mnt, &ns->list, mnt_list) { list_for_each_entry(mnt, &ns->list, mnt_list) {
struct mount *child; struct mount *child;
int mnt_flags; int mnt_flags;
if (mnt->mnt.mnt_sb->s_type != type) if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
continue; continue;
/* This mount is not fully visible if it's root directory /* This mount is not fully visible if it's root directory
@ -3241,12 +3216,8 @@ static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
continue; continue;
/* Read the mount flags and filter out flags that /* A local view of the mount flags */
* may safely be ignored.
*/
mnt_flags = mnt->mnt.mnt_flags; mnt_flags = mnt->mnt.mnt_flags;
if (mnt->mnt.mnt_sb->s_iflags & SB_I_NOEXEC)
mnt_flags &= ~(MNT_LOCK_NOSUID | MNT_LOCK_NOEXEC);
/* Don't miss readonly hidden in the superblock flags */ /* Don't miss readonly hidden in the superblock flags */
if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY) if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
@ -3258,15 +3229,6 @@ static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
if ((mnt_flags & MNT_LOCK_READONLY) && if ((mnt_flags & MNT_LOCK_READONLY) &&
!(new_flags & MNT_READONLY)) !(new_flags & MNT_READONLY))
continue; continue;
if ((mnt_flags & MNT_LOCK_NODEV) &&
!(new_flags & MNT_NODEV))
continue;
if ((mnt_flags & MNT_LOCK_NOSUID) &&
!(new_flags & MNT_NOSUID))
continue;
if ((mnt_flags & MNT_LOCK_NOEXEC) &&
!(new_flags & MNT_NOEXEC))
continue;
if ((mnt_flags & MNT_LOCK_ATIME) && if ((mnt_flags & MNT_LOCK_ATIME) &&
((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
continue; continue;
@ -3286,9 +3248,6 @@ static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
} }
/* Preserve the locked attributes */ /* Preserve the locked attributes */
*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
MNT_LOCK_NODEV | \
MNT_LOCK_NOSUID | \
MNT_LOCK_NOEXEC | \
MNT_LOCK_ATIME); MNT_LOCK_ATIME);
visible = true; visible = true;
goto found; goto found;
@ -3299,6 +3258,42 @@ static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
return visible; return visible;
} }
static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
{
const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
struct mnt_namespace *ns = current->nsproxy->mnt_ns;
unsigned long s_iflags;
if (ns->user_ns == &init_user_ns)
return false;
/* Can this filesystem be too revealing? */
s_iflags = mnt->mnt_sb->s_iflags;
if (!(s_iflags & SB_I_USERNS_VISIBLE))
return false;
if ((s_iflags & required_iflags) != required_iflags) {
WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
required_iflags);
return true;
}
return !mnt_already_visible(ns, mnt, new_mnt_flags);
}
bool mnt_may_suid(struct vfsmount *mnt)
{
/*
* Foreign mounts (accessed via fchdir or through /proc
* symlinks) are always treated as if they are nosuid. This
* prevents namespaces from trusting potentially unsafe
* suid/sgid bits, file caps, or security labels that originate
* in other namespaces.
*/
return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
current_in_userns(mnt->mnt_sb->s_user_ns);
}
static struct ns_common *mntns_get(struct task_struct *task) static struct ns_common *mntns_get(struct task_struct *task)
{ {
struct ns_common *ns = NULL; struct ns_common *ns = NULL;

View File

@ -1151,20 +1151,15 @@ static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
#endif #endif
/* last one */ {""} /* last one */ {""}
}; };
struct net *net = data; get_net(sb->s_fs_info);
int ret; return simple_fill_super(sb, 0x6e667364, nfsd_files);
ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
if (ret)
return ret;
sb->s_fs_info = get_net(net);
return 0;
} }
static struct dentry *nfsd_mount(struct file_system_type *fs_type, static struct dentry *nfsd_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data) int flags, const char *dev_name, void *data)
{ {
return mount_ns(fs_type, flags, current->nsproxy->net_ns, nfsd_fill_super); struct net *net = current->nsproxy->net_ns;
return mount_ns(fs_type, flags, data, net, net->user_ns, nfsd_fill_super);
} }
static void nfsd_umount(struct super_block *sb) static void nfsd_umount(struct super_block *sb)

View File

@ -205,7 +205,7 @@ posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
* Check if an acl is valid. Returns 0 if it is, or -E... otherwise. * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
*/ */
int int
posix_acl_valid(const struct posix_acl *acl) posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
{ {
const struct posix_acl_entry *pa, *pe; const struct posix_acl_entry *pa, *pe;
int state = ACL_USER_OBJ; int state = ACL_USER_OBJ;
@ -225,7 +225,7 @@ posix_acl_valid(const struct posix_acl *acl)
case ACL_USER: case ACL_USER:
if (state != ACL_USER) if (state != ACL_USER)
return -EINVAL; return -EINVAL;
if (!uid_valid(pa->e_uid)) if (!kuid_has_mapping(user_ns, pa->e_uid))
return -EINVAL; return -EINVAL;
needs_mask = 1; needs_mask = 1;
break; break;
@ -240,7 +240,7 @@ posix_acl_valid(const struct posix_acl *acl)
case ACL_GROUP: case ACL_GROUP:
if (state != ACL_GROUP) if (state != ACL_GROUP)
return -EINVAL; return -EINVAL;
if (!gid_valid(pa->e_gid)) if (!kgid_has_mapping(user_ns, pa->e_gid))
return -EINVAL; return -EINVAL;
needs_mask = 1; needs_mask = 1;
break; break;
@ -834,7 +834,7 @@ set_posix_acl(struct inode *inode, int type, struct posix_acl *acl)
return -EPERM; return -EPERM;
if (acl) { if (acl) {
int ret = posix_acl_valid(acl); int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
if (ret) if (ret)
return ret; return ret;
} }

View File

@ -457,17 +457,30 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
return inode; return inode;
} }
int proc_fill_super(struct super_block *s) int proc_fill_super(struct super_block *s, void *data, int silent)
{ {
struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
struct inode *root_inode; struct inode *root_inode;
int ret; int ret;
if (!proc_parse_options(data, ns))
return -EINVAL;
/* User space would break if executables or devices appear on proc */
s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC; s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
s->s_blocksize = 1024; s->s_blocksize = 1024;
s->s_blocksize_bits = 10; s->s_blocksize_bits = 10;
s->s_magic = PROC_SUPER_MAGIC; s->s_magic = PROC_SUPER_MAGIC;
s->s_op = &proc_sops; s->s_op = &proc_sops;
s->s_time_gran = 1; s->s_time_gran = 1;
/*
* procfs isn't actually a stacking filesystem; however, there is
* too much magic going on inside it to permit stacking things on
* top of it
*/
s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
pde_get(&proc_root); pde_get(&proc_root);
root_inode = proc_get_inode(s, &proc_root); root_inode = proc_get_inode(s, &proc_root);

View File

@ -212,7 +212,7 @@ extern const struct inode_operations proc_pid_link_inode_operations;
extern void proc_init_inodecache(void); extern void proc_init_inodecache(void);
extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *); extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
extern int proc_fill_super(struct super_block *); extern int proc_fill_super(struct super_block *, void *data, int flags);
extern void proc_entry_rundown(struct proc_dir_entry *); extern void proc_entry_rundown(struct proc_dir_entry *);
/* /*
@ -268,6 +268,7 @@ static inline void proc_tty_init(void) {}
* root.c * root.c
*/ */
extern struct proc_dir_entry proc_root; extern struct proc_dir_entry proc_root;
extern int proc_parse_options(char *options, struct pid_namespace *pid);
extern void proc_self_init(void); extern void proc_self_init(void);
extern int proc_remount(struct super_block *, int *, char *); extern int proc_remount(struct super_block *, int *, char *);

View File

@ -23,21 +23,6 @@
#include "internal.h" #include "internal.h"
static int proc_test_super(struct super_block *sb, void *data)
{
return sb->s_fs_info == data;
}
static int proc_set_super(struct super_block *sb, void *data)
{
int err = set_anon_super(sb, NULL);
if (!err) {
struct pid_namespace *ns = (struct pid_namespace *)data;
sb->s_fs_info = get_pid_ns(ns);
}
return err;
}
enum { enum {
Opt_gid, Opt_hidepid, Opt_err, Opt_gid, Opt_hidepid, Opt_err,
}; };
@ -48,7 +33,7 @@ static const match_table_t tokens = {
{Opt_err, NULL}, {Opt_err, NULL},
}; };
static int proc_parse_options(char *options, struct pid_namespace *pid) int proc_parse_options(char *options, struct pid_namespace *pid)
{ {
char *p; char *p;
substring_t args[MAX_OPT_ARGS]; substring_t args[MAX_OPT_ARGS];
@ -100,52 +85,16 @@ int proc_remount(struct super_block *sb, int *flags, char *data)
static struct dentry *proc_mount(struct file_system_type *fs_type, static struct dentry *proc_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data) int flags, const char *dev_name, void *data)
{ {
int err;
struct super_block *sb;
struct pid_namespace *ns; struct pid_namespace *ns;
char *options;
if (flags & MS_KERNMOUNT) { if (flags & MS_KERNMOUNT) {
ns = (struct pid_namespace *)data; ns = data;
options = NULL; data = NULL;
} else { } else {
ns = task_active_pid_ns(current); ns = task_active_pid_ns(current);
options = data;
/* Does the mounter have privilege over the pid namespace? */
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
return ERR_PTR(-EPERM);
} }
sb = sget(fs_type, proc_test_super, proc_set_super, flags, ns); return mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
if (IS_ERR(sb))
return ERR_CAST(sb);
/*
* procfs isn't actually a stacking filesystem; however, there is
* too much magic going on inside it to permit stacking things on
* top of it
*/
sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
if (!proc_parse_options(options, ns)) {
deactivate_locked_super(sb);
return ERR_PTR(-EINVAL);
}
if (!sb->s_root) {
err = proc_fill_super(sb);
if (err) {
deactivate_locked_super(sb);
return ERR_PTR(err);
}
sb->s_flags |= MS_ACTIVE;
/* User space would break if executables appear on proc */
sb->s_iflags |= SB_I_NOEXEC;
}
return dget(sb->s_root);
} }
static void proc_kill_sb(struct super_block *sb) static void proc_kill_sb(struct super_block *sb)
@ -165,7 +114,7 @@ static struct file_system_type proc_fs_type = {
.name = "proc", .name = "proc",
.mount = proc_mount, .mount = proc_mount,
.kill_sb = proc_kill_sb, .kill_sb = proc_kill_sb,
.fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT, .fs_flags = FS_USERNS_MOUNT,
}; };
void __init proc_root_init(void) void __init proc_root_init(void)

View File

@ -841,6 +841,9 @@ struct dquot *dqget(struct super_block *sb, struct kqid qid)
unsigned int hashent = hashfn(sb, qid); unsigned int hashent = hashfn(sb, qid);
struct dquot *dquot, *empty = NULL; struct dquot *dquot, *empty = NULL;
if (!qid_has_mapping(sb->s_user_ns, qid))
return ERR_PTR(-EINVAL);
if (!sb_has_quota_active(sb, qid.type)) if (!sb_has_quota_active(sb, qid.type))
return ERR_PTR(-ESRCH); return ERR_PTR(-ESRCH);
we_slept: we_slept:
@ -2268,6 +2271,11 @@ static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
error = -EINVAL; error = -EINVAL;
goto out_fmt; goto out_fmt;
} }
/* Filesystems outside of init_user_ns not yet supported */
if (sb->s_user_ns != &init_user_ns) {
error = -EINVAL;
goto out_fmt;
}
/* Usage always has to be set... */ /* Usage always has to be set... */
if (!(flags & DQUOT_USAGE_ENABLED)) { if (!(flags & DQUOT_USAGE_ENABLED)) {
error = -EINVAL; error = -EINVAL;

View File

@ -211,7 +211,7 @@ static int quota_getquota(struct super_block *sb, int type, qid_t id,
if (!sb->s_qcop->get_dqblk) if (!sb->s_qcop->get_dqblk)
return -ENOSYS; return -ENOSYS;
qid = make_kqid(current_user_ns(), type, id); qid = make_kqid(current_user_ns(), type, id);
if (!qid_valid(qid)) if (!qid_has_mapping(sb->s_user_ns, qid))
return -EINVAL; return -EINVAL;
ret = sb->s_qcop->get_dqblk(sb, qid, &fdq); ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
if (ret) if (ret)
@ -237,7 +237,7 @@ static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
if (!sb->s_qcop->get_nextdqblk) if (!sb->s_qcop->get_nextdqblk)
return -ENOSYS; return -ENOSYS;
qid = make_kqid(current_user_ns(), type, id); qid = make_kqid(current_user_ns(), type, id);
if (!qid_valid(qid)) if (!qid_has_mapping(sb->s_user_ns, qid))
return -EINVAL; return -EINVAL;
ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq); ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
if (ret) if (ret)
@ -288,7 +288,7 @@ static int quota_setquota(struct super_block *sb, int type, qid_t id,
if (!sb->s_qcop->set_dqblk) if (!sb->s_qcop->set_dqblk)
return -ENOSYS; return -ENOSYS;
qid = make_kqid(current_user_ns(), type, id); qid = make_kqid(current_user_ns(), type, id);
if (!qid_valid(qid)) if (!qid_has_mapping(sb->s_user_ns, qid))
return -EINVAL; return -EINVAL;
copy_from_if_dqblk(&fdq, &idq); copy_from_if_dqblk(&fdq, &idq);
return sb->s_qcop->set_dqblk(sb, qid, &fdq); return sb->s_qcop->set_dqblk(sb, qid, &fdq);
@ -581,10 +581,10 @@ static int quota_setxquota(struct super_block *sb, int type, qid_t id,
if (!sb->s_qcop->set_dqblk) if (!sb->s_qcop->set_dqblk)
return -ENOSYS; return -ENOSYS;
qid = make_kqid(current_user_ns(), type, id); qid = make_kqid(current_user_ns(), type, id);
if (!qid_valid(qid)) if (!qid_has_mapping(sb->s_user_ns, qid))
return -EINVAL; return -EINVAL;
/* Are we actually setting timer / warning limits for all users? */ /* Are we actually setting timer / warning limits for all users? */
if (from_kqid(&init_user_ns, qid) == 0 && if (from_kqid(sb->s_user_ns, qid) == 0 &&
fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) { fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
struct qc_info qinfo; struct qc_info qinfo;
int ret; int ret;
@ -642,7 +642,7 @@ static int quota_getxquota(struct super_block *sb, int type, qid_t id,
if (!sb->s_qcop->get_dqblk) if (!sb->s_qcop->get_dqblk)
return -ENOSYS; return -ENOSYS;
qid = make_kqid(current_user_ns(), type, id); qid = make_kqid(current_user_ns(), type, id);
if (!qid_valid(qid)) if (!qid_has_mapping(sb->s_user_ns, qid))
return -EINVAL; return -EINVAL;
ret = sb->s_qcop->get_dqblk(sb, qid, &qdq); ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
if (ret) if (ret)
@ -669,7 +669,7 @@ static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
if (!sb->s_qcop->get_nextdqblk) if (!sb->s_qcop->get_nextdqblk)
return -ENOSYS; return -ENOSYS;
qid = make_kqid(current_user_ns(), type, id); qid = make_kqid(current_user_ns(), type, id);
if (!qid_valid(qid)) if (!qid_has_mapping(sb->s_user_ns, qid))
return -EINVAL; return -EINVAL;
ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq); ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
if (ret) if (ret)

View File

@ -33,6 +33,7 @@
#include <linux/cleancache.h> #include <linux/cleancache.h>
#include <linux/fsnotify.h> #include <linux/fsnotify.h>
#include <linux/lockdep.h> #include <linux/lockdep.h>
#include <linux/user_namespace.h>
#include "internal.h" #include "internal.h"
@ -165,6 +166,7 @@ static void destroy_super(struct super_block *s)
list_lru_destroy(&s->s_inode_lru); list_lru_destroy(&s->s_inode_lru);
security_sb_free(s); security_sb_free(s);
WARN_ON(!list_empty(&s->s_mounts)); WARN_ON(!list_empty(&s->s_mounts));
put_user_ns(s->s_user_ns);
kfree(s->s_subtype); kfree(s->s_subtype);
kfree(s->s_options); kfree(s->s_options);
call_rcu(&s->rcu, destroy_super_rcu); call_rcu(&s->rcu, destroy_super_rcu);
@ -174,11 +176,13 @@ static void destroy_super(struct super_block *s)
* alloc_super - create new superblock * alloc_super - create new superblock
* @type: filesystem type superblock should belong to * @type: filesystem type superblock should belong to
* @flags: the mount flags * @flags: the mount flags
* @user_ns: User namespace for the super_block
* *
* Allocates and initializes a new &struct super_block. alloc_super() * Allocates and initializes a new &struct super_block. alloc_super()
* returns a pointer new superblock or %NULL if allocation had failed. * returns a pointer new superblock or %NULL if allocation had failed.
*/ */
static struct super_block *alloc_super(struct file_system_type *type, int flags) static struct super_block *alloc_super(struct file_system_type *type, int flags,
struct user_namespace *user_ns)
{ {
struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER); struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
static const struct super_operations default_op; static const struct super_operations default_op;
@ -188,6 +192,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
return NULL; return NULL;
INIT_LIST_HEAD(&s->s_mounts); INIT_LIST_HEAD(&s->s_mounts);
s->s_user_ns = get_user_ns(user_ns);
if (security_sb_alloc(s)) if (security_sb_alloc(s))
goto fail; goto fail;
@ -201,6 +206,8 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
init_waitqueue_head(&s->s_writers.wait_unfrozen); init_waitqueue_head(&s->s_writers.wait_unfrozen);
s->s_bdi = &noop_backing_dev_info; s->s_bdi = &noop_backing_dev_info;
s->s_flags = flags; s->s_flags = flags;
if (s->s_user_ns != &init_user_ns)
s->s_iflags |= SB_I_NODEV;
INIT_HLIST_NODE(&s->s_instances); INIT_HLIST_NODE(&s->s_instances);
INIT_HLIST_BL_HEAD(&s->s_anon); INIT_HLIST_BL_HEAD(&s->s_anon);
mutex_init(&s->s_sync_lock); mutex_init(&s->s_sync_lock);
@ -445,29 +452,42 @@ void generic_shutdown_super(struct super_block *sb)
EXPORT_SYMBOL(generic_shutdown_super); EXPORT_SYMBOL(generic_shutdown_super);
/** /**
* sget - find or create a superblock * sget_userns - find or create a superblock
* @type: filesystem type superblock should belong to * @type: filesystem type superblock should belong to
* @test: comparison callback * @test: comparison callback
* @set: setup callback * @set: setup callback
* @flags: mount flags * @flags: mount flags
* @user_ns: User namespace for the super_block
* @data: argument to each of them * @data: argument to each of them
*/ */
struct super_block *sget(struct file_system_type *type, struct super_block *sget_userns(struct file_system_type *type,
int (*test)(struct super_block *,void *), int (*test)(struct super_block *,void *),
int (*set)(struct super_block *,void *), int (*set)(struct super_block *,void *),
int flags, int flags, struct user_namespace *user_ns,
void *data) void *data)
{ {
struct super_block *s = NULL; struct super_block *s = NULL;
struct super_block *old; struct super_block *old;
int err; int err;
if (!(flags & MS_KERNMOUNT) &&
!(type->fs_flags & FS_USERNS_MOUNT) &&
!capable(CAP_SYS_ADMIN))
return ERR_PTR(-EPERM);
retry: retry:
spin_lock(&sb_lock); spin_lock(&sb_lock);
if (test) { if (test) {
hlist_for_each_entry(old, &type->fs_supers, s_instances) { hlist_for_each_entry(old, &type->fs_supers, s_instances) {
if (!test(old, data)) if (!test(old, data))
continue; continue;
if (user_ns != old->s_user_ns) {
spin_unlock(&sb_lock);
if (s) {
up_write(&s->s_umount);
destroy_super(s);
}
return ERR_PTR(-EBUSY);
}
if (!grab_super(old)) if (!grab_super(old))
goto retry; goto retry;
if (s) { if (s) {
@ -480,7 +500,7 @@ struct super_block *sget(struct file_system_type *type,
} }
if (!s) { if (!s) {
spin_unlock(&sb_lock); spin_unlock(&sb_lock);
s = alloc_super(type, flags); s = alloc_super(type, flags, user_ns);
if (!s) if (!s)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
goto retry; goto retry;
@ -503,6 +523,31 @@ struct super_block *sget(struct file_system_type *type,
return s; return s;
} }
EXPORT_SYMBOL(sget_userns);
/**
* sget - find or create a superblock
* @type: filesystem type superblock should belong to
* @test: comparison callback
* @set: setup callback
* @flags: mount flags
* @data: argument to each of them
*/
struct super_block *sget(struct file_system_type *type,
int (*test)(struct super_block *,void *),
int (*set)(struct super_block *,void *),
int flags,
void *data)
{
struct user_namespace *user_ns = current_user_ns();
/* Ensure the requestor has permissions over the target filesystem */
if (!(flags & MS_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
return ERR_PTR(-EPERM);
return sget_userns(type, test, set, flags, user_ns, data);
}
EXPORT_SYMBOL(sget); EXPORT_SYMBOL(sget);
void drop_super(struct super_block *sb) void drop_super(struct super_block *sb)
@ -920,12 +965,20 @@ static int ns_set_super(struct super_block *sb, void *data)
return set_anon_super(sb, NULL); return set_anon_super(sb, NULL);
} }
struct dentry *mount_ns(struct file_system_type *fs_type, int flags, struct dentry *mount_ns(struct file_system_type *fs_type,
void *data, int (*fill_super)(struct super_block *, void *, int)) int flags, void *data, void *ns, struct user_namespace *user_ns,
int (*fill_super)(struct super_block *, void *, int))
{ {
struct super_block *sb; struct super_block *sb;
sb = sget(fs_type, ns_test_super, ns_set_super, flags, data); /* Don't allow mounting unless the caller has CAP_SYS_ADMIN
* over the namespace.
*/
if (!(flags & MS_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
return ERR_PTR(-EPERM);
sb = sget_userns(fs_type, ns_test_super, ns_set_super, flags,
user_ns, ns);
if (IS_ERR(sb)) if (IS_ERR(sb))
return ERR_CAST(sb); return ERR_CAST(sb);

View File

@ -41,8 +41,7 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
if (IS_ERR(root) || !new_sb) if (IS_ERR(root) || !new_sb)
kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
else if (new_sb) else if (new_sb)
/* Userspace would break if executables appear on sysfs */ root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
root->d_sb->s_iflags |= SB_I_NOEXEC;
return root; return root;
} }
@ -59,7 +58,7 @@ static struct file_system_type sysfs_fs_type = {
.name = "sysfs", .name = "sysfs",
.mount = sysfs_mount, .mount = sysfs_mount,
.kill_sb = sysfs_kill_sb, .kill_sb = sysfs_kill_sb,
.fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT, .fs_flags = FS_USERNS_MOUNT,
}; };
int __init sysfs_init(void) int __init sysfs_init(void)

View File

@ -38,6 +38,13 @@ xattr_permission(struct inode *inode, const char *name, int mask)
if (mask & MAY_WRITE) { if (mask & MAY_WRITE) {
if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
return -EPERM; return -EPERM;
/*
* Updating an xattr will likely cause i_uid and i_gid
* to be writen back improperly if their true value is
* unknown to the vfs.
*/
if (HAS_UNMAPPED_ID(inode))
return -EPERM;
} }
/* /*

View File

@ -829,31 +829,6 @@ static inline void i_size_write(struct inode *inode, loff_t i_size)
#endif #endif
} }
/* Helper functions so that in most cases filesystems will
* not need to deal directly with kuid_t and kgid_t and can
* instead deal with the raw numeric values that are stored
* in the filesystem.
*/
static inline uid_t i_uid_read(const struct inode *inode)
{
return from_kuid(&init_user_ns, inode->i_uid);
}
static inline gid_t i_gid_read(const struct inode *inode)
{
return from_kgid(&init_user_ns, inode->i_gid);
}
static inline void i_uid_write(struct inode *inode, uid_t uid)
{
inode->i_uid = make_kuid(&init_user_ns, uid);
}
static inline void i_gid_write(struct inode *inode, gid_t gid)
{
inode->i_gid = make_kgid(&init_user_ns, gid);
}
static inline unsigned iminor(const struct inode *inode) static inline unsigned iminor(const struct inode *inode)
{ {
return MINOR(inode->i_rdev); return MINOR(inode->i_rdev);
@ -1320,6 +1295,10 @@ struct mm_struct;
/* sb->s_iflags */ /* sb->s_iflags */
#define SB_I_CGROUPWB 0x00000001 /* cgroup-aware writeback enabled */ #define SB_I_CGROUPWB 0x00000001 /* cgroup-aware writeback enabled */
#define SB_I_NOEXEC 0x00000002 /* Ignore executables on this fs */ #define SB_I_NOEXEC 0x00000002 /* Ignore executables on this fs */
#define SB_I_NODEV 0x00000004 /* Ignore devices on this fs */
/* sb->s_iflags to limit user namespace mounts */
#define SB_I_USERNS_VISIBLE 0x00000010 /* fstype already mounted */
/* Possible states of 'frozen' field */ /* Possible states of 'frozen' field */
enum { enum {
@ -1422,6 +1401,13 @@ struct super_block {
struct workqueue_struct *s_dio_done_wq; struct workqueue_struct *s_dio_done_wq;
struct hlist_head s_pins; struct hlist_head s_pins;
/*
* Owning user namespace and default context in which to
* interpret filesystem uids, gids, quotas, device nodes,
* xattrs and security labels.
*/
struct user_namespace *s_user_ns;
/* /*
* Keep the lru lists last in the structure so they always sit on their * Keep the lru lists last in the structure so they always sit on their
* own individual cachelines. * own individual cachelines.
@ -1446,6 +1432,31 @@ struct super_block {
struct list_head s_inodes_wb; /* writeback inodes */ struct list_head s_inodes_wb; /* writeback inodes */
}; };
/* Helper functions so that in most cases filesystems will
* not need to deal directly with kuid_t and kgid_t and can
* instead deal with the raw numeric values that are stored
* in the filesystem.
*/
static inline uid_t i_uid_read(const struct inode *inode)
{
return from_kuid(inode->i_sb->s_user_ns, inode->i_uid);
}
static inline gid_t i_gid_read(const struct inode *inode)
{
return from_kgid(inode->i_sb->s_user_ns, inode->i_gid);
}
static inline void i_uid_write(struct inode *inode, uid_t uid)
{
inode->i_uid = make_kuid(inode->i_sb->s_user_ns, uid);
}
static inline void i_gid_write(struct inode *inode, gid_t gid)
{
inode->i_gid = make_kgid(inode->i_sb->s_user_ns, gid);
}
extern struct timespec current_fs_time(struct super_block *sb); extern struct timespec current_fs_time(struct super_block *sb);
/* /*
@ -1588,6 +1599,7 @@ extern int vfs_whiteout(struct inode *, struct dentry *);
*/ */
extern void inode_init_owner(struct inode *inode, const struct inode *dir, extern void inode_init_owner(struct inode *inode, const struct inode *dir,
umode_t mode); umode_t mode);
extern bool may_open_dev(const struct path *path);
/* /*
* VFS FS_IOC_FIEMAP helper definitions. * VFS FS_IOC_FIEMAP helper definitions.
*/ */
@ -1858,6 +1870,11 @@ struct super_operations {
#define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \ #define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \
(inode)->i_rdev == WHITEOUT_DEV) (inode)->i_rdev == WHITEOUT_DEV)
static inline bool HAS_UNMAPPED_ID(struct inode *inode)
{
return !uid_valid(inode->i_uid) || !gid_valid(inode->i_gid);
}
/* /*
* Inode state bits. Protected by inode->i_lock * Inode state bits. Protected by inode->i_lock
* *
@ -2006,8 +2023,6 @@ struct file_system_type {
#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_MOUNT 8 /* Can be mounted by userns root */
#define FS_USERNS_DEV_MOUNT 16 /* A userns mount does not imply MNT_NODEV */
#define FS_USERNS_VISIBLE 32 /* FS must already be visible */
#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,
const char *, void *); const char *, void *);
@ -2028,8 +2043,9 @@ struct file_system_type {
#define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME) #define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME)
extern struct dentry *mount_ns(struct file_system_type *fs_type, int flags, extern struct dentry *mount_ns(struct file_system_type *fs_type,
void *data, int (*fill_super)(struct super_block *, void *, int)); int flags, void *data, void *ns, struct user_namespace *user_ns,
int (*fill_super)(struct super_block *, void *, int));
extern struct dentry *mount_bdev(struct file_system_type *fs_type, extern struct dentry *mount_bdev(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, int flags, const char *dev_name, void *data,
int (*fill_super)(struct super_block *, void *, int)); int (*fill_super)(struct super_block *, void *, int));
@ -2049,6 +2065,11 @@ void deactivate_locked_super(struct super_block *sb);
int set_anon_super(struct super_block *s, void *data); int set_anon_super(struct super_block *s, void *data);
int get_anon_bdev(dev_t *); int get_anon_bdev(dev_t *);
void free_anon_bdev(dev_t); void free_anon_bdev(dev_t);
struct super_block *sget_userns(struct file_system_type *type,
int (*test)(struct super_block *,void *),
int (*set)(struct super_block *,void *),
int flags, struct user_namespace *user_ns,
void *data);
struct super_block *sget(struct file_system_type *type, struct super_block *sget(struct file_system_type *type,
int (*test)(struct super_block *,void *), int (*test)(struct super_block *,void *),
int (*set)(struct super_block *,void *), int (*set)(struct super_block *,void *),

View File

@ -81,6 +81,7 @@ extern void mntput(struct vfsmount *mnt);
extern struct vfsmount *mntget(struct vfsmount *mnt); extern struct vfsmount *mntget(struct vfsmount *mnt);
extern struct vfsmount *mnt_clone_internal(struct path *path); extern struct vfsmount *mnt_clone_internal(struct path *path);
extern int __mnt_is_readonly(struct vfsmount *mnt); extern int __mnt_is_readonly(struct vfsmount *mnt);
extern bool mnt_may_suid(struct vfsmount *mnt);
struct path; struct path;
extern struct vfsmount *clone_private_mount(struct path *path); extern struct vfsmount *clone_private_mount(struct path *path);

View File

@ -79,7 +79,7 @@ posix_acl_release(struct posix_acl *acl)
extern void posix_acl_init(struct posix_acl *, int); extern void posix_acl_init(struct posix_acl *, int);
extern struct posix_acl *posix_acl_alloc(int, gfp_t); extern struct posix_acl *posix_acl_alloc(int, gfp_t);
extern int posix_acl_valid(const struct posix_acl *); extern int posix_acl_valid(struct user_namespace *, const struct posix_acl *);
extern int posix_acl_permission(struct inode *, const struct posix_acl *, int); extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t); extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t);
extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *); extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *);

View File

@ -179,6 +179,16 @@ static inline struct kqid make_kqid_projid(kprojid_t projid)
return kqid; return kqid;
} }
/**
* qid_has_mapping - Report if a qid maps into a user namespace.
* @ns: The user namespace to see if a value maps into.
* @qid: The kernel internal quota identifier to test.
*/
static inline bool qid_has_mapping(struct user_namespace *ns, struct kqid qid)
{
return from_kqid(ns, qid) != (qid_t) -1;
}
extern spinlock_t dq_data_lock; extern spinlock_t dq_data_lock;

View File

@ -177,12 +177,12 @@ static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid) static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
{ {
return true; return uid_valid(uid);
} }
static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid) static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
{ {
return true; return gid_valid(gid);
} }
#endif /* CONFIG_USER_NS */ #endif /* CONFIG_USER_NS */

View File

@ -72,6 +72,7 @@ extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t,
extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
extern int proc_setgroups_show(struct seq_file *m, void *v); extern int proc_setgroups_show(struct seq_file *m, void *v);
extern bool userns_may_setgroups(const struct user_namespace *ns); extern bool userns_may_setgroups(const struct user_namespace *ns);
extern bool current_in_userns(const struct user_namespace *target_ns);
#else #else
static inline struct user_namespace *get_user_ns(struct user_namespace *ns) static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
@ -100,6 +101,11 @@ static inline bool userns_may_setgroups(const struct user_namespace *ns)
{ {
return true; return true;
} }
static inline bool current_in_userns(const struct user_namespace *target_ns)
{
return true;
}
#endif #endif
#endif /* _LINUX_USER_H */ #endif /* _LINUX_USER_H */

View File

@ -305,8 +305,9 @@ static struct inode *mqueue_get_inode(struct super_block *sb,
static int mqueue_fill_super(struct super_block *sb, void *data, int silent) static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
{ {
struct inode *inode; struct inode *inode;
struct ipc_namespace *ns = data; struct ipc_namespace *ns = sb->s_fs_info;
sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
sb->s_blocksize = PAGE_SIZE; sb->s_blocksize = PAGE_SIZE;
sb->s_blocksize_bits = PAGE_SHIFT; sb->s_blocksize_bits = PAGE_SHIFT;
sb->s_magic = MQUEUE_MAGIC; sb->s_magic = MQUEUE_MAGIC;
@ -326,17 +327,14 @@ static struct dentry *mqueue_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, int flags, const char *dev_name,
void *data) void *data)
{ {
if (!(flags & MS_KERNMOUNT)) { struct ipc_namespace *ns;
struct ipc_namespace *ns = current->nsproxy->ipc_ns; if (flags & MS_KERNMOUNT) {
/* Don't allow mounting unless the caller has CAP_SYS_ADMIN ns = data;
* over the ipc namespace. data = NULL;
*/ } else {
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) ns = current->nsproxy->ipc_ns;
return ERR_PTR(-EPERM);
data = ns;
} }
return mount_ns(fs_type, flags, data, mqueue_fill_super); return mount_ns(fs_type, flags, data, ns, ns->user_ns, mqueue_fill_super);
} }
static void init_once(void *foo) static void init_once(void *foo)

View File

@ -34,8 +34,11 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
ns->ns.ops = &ipcns_operations; ns->ns.ops = &ipcns_operations;
atomic_set(&ns->count, 1); atomic_set(&ns->count, 1);
ns->user_ns = get_user_ns(user_ns);
err = mq_init_ns(ns); err = mq_init_ns(ns);
if (err) { if (err) {
put_user_ns(ns->user_ns);
ns_free_inum(&ns->ns); ns_free_inum(&ns->ns);
kfree(ns); kfree(ns);
return ERR_PTR(err); return ERR_PTR(err);
@ -46,8 +49,6 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
msg_init_ns(ns); msg_init_ns(ns);
shm_init_ns(ns); shm_init_ns(ns);
ns->user_ns = get_user_ns(user_ns);
return ns; return ns;
} }

View File

@ -689,6 +689,8 @@ EXPORT_SYMBOL(set_security_override_from_ctx);
*/ */
int set_create_files_as(struct cred *new, struct inode *inode) int set_create_files_as(struct cred *new, struct inode *inode)
{ {
if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
return -EINVAL;
new->fsuid = inode->i_uid; new->fsuid = inode->i_uid;
new->fsgid = inode->i_gid; new->fsgid = inode->i_gid;
return security_kernel_create_files_as(new, inode); return security_kernel_create_files_as(new, inode);

View File

@ -938,6 +938,20 @@ bool userns_may_setgroups(const struct user_namespace *ns)
return allowed; return allowed;
} }
/*
* Returns true if @ns is the same namespace as or a descendant of
* @target_ns.
*/
bool current_in_userns(const struct user_namespace *target_ns)
{
struct user_namespace *ns;
for (ns = current_user_ns(); ns; ns = ns->parent) {
if (ns == target_ns)
return true;
}
return false;
}
static inline struct user_namespace *to_user_ns(struct ns_common *ns) static inline struct user_namespace *to_user_ns(struct ns_common *ns)
{ {
return container_of(ns, struct user_namespace, ns); return container_of(ns, struct user_namespace, ns);

View File

@ -1386,7 +1386,7 @@ rpc_fill_super(struct super_block *sb, void *data, int silent)
{ {
struct inode *inode; struct inode *inode;
struct dentry *root, *gssd_dentry; struct dentry *root, *gssd_dentry;
struct net *net = data; struct net *net = get_net(sb->s_fs_info);
struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
int err; int err;
@ -1419,7 +1419,6 @@ rpc_fill_super(struct super_block *sb, void *data, int silent)
sb); sb);
if (err) if (err)
goto err_depopulate; goto err_depopulate;
sb->s_fs_info = get_net(net);
mutex_unlock(&sn->pipefs_sb_lock); mutex_unlock(&sn->pipefs_sb_lock);
return 0; return 0;
@ -1448,7 +1447,8 @@ static struct dentry *
rpc_mount(struct file_system_type *fs_type, rpc_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data) int flags, const char *dev_name, void *data)
{ {
return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super); struct net *net = current->nsproxy->net_ns;
return mount_ns(fs_type, flags, data, net, net->user_ns, rpc_fill_super);
} }
static void rpc_kill_sb(struct super_block *sb) static void rpc_kill_sb(struct super_block *sb)
@ -1468,9 +1468,9 @@ static void rpc_kill_sb(struct super_block *sb)
RPC_PIPEFS_UMOUNT, RPC_PIPEFS_UMOUNT,
sb); sb);
mutex_unlock(&sn->pipefs_sb_lock); mutex_unlock(&sn->pipefs_sb_lock);
put_net(net);
out: out:
kill_litter_super(sb); kill_litter_super(sb);
put_net(net);
} }
static struct file_system_type rpc_pipe_fs_type = { static struct file_system_type rpc_pipe_fs_type = {

View File

@ -453,7 +453,15 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c
if (!file_caps_enabled) if (!file_caps_enabled)
return 0; return 0;
if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) if (!mnt_may_suid(bprm->file->f_path.mnt))
return 0;
/*
* This check is redundant with mnt_may_suid() but is kept to make
* explicit that capability bits are limited to s_user_ns and its
* descendants.
*/
if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
return 0; return 0;
rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps); rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);

View File

@ -151,8 +151,8 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
memset(&hmac_misc, 0, sizeof(hmac_misc)); memset(&hmac_misc, 0, sizeof(hmac_misc));
hmac_misc.ino = inode->i_ino; hmac_misc.ino = inode->i_ino;
hmac_misc.generation = inode->i_generation; hmac_misc.generation = inode->i_generation;
hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid); hmac_misc.uid = from_kuid(inode->i_sb->s_user_ns, inode->i_uid);
hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid); hmac_misc.gid = from_kgid(inode->i_sb->s_user_ns, inode->i_gid);
hmac_misc.mode = inode->i_mode; hmac_misc.mode = inode->i_mode;
crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc)); crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
if (evm_hmac_attrs & EVM_ATTR_FSUUID) if (evm_hmac_attrs & EVM_ATTR_FSUUID)

View File

@ -830,6 +830,28 @@ static int selinux_set_mnt_opts(struct super_block *sb,
goto out; goto out;
} }
} }
/*
* If this is a user namespace mount, no contexts are allowed
* on the command line and security labels must be ignored.
*/
if (sb->s_user_ns != &init_user_ns) {
if (context_sid || fscontext_sid || rootcontext_sid ||
defcontext_sid) {
rc = -EACCES;
goto out;
}
if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
rc = security_transition_sid(current_sid(), current_sid(),
SECCLASS_FILE, NULL,
&sbsec->mntpoint_sid);
if (rc)
goto out;
}
goto out_set_opts;
}
/* sets the context of the superblock for the fs being mounted. */ /* sets the context of the superblock for the fs being mounted. */
if (fscontext_sid) { if (fscontext_sid) {
rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
@ -898,6 +920,7 @@ static int selinux_set_mnt_opts(struct super_block *sb,
sbsec->def_sid = defcontext_sid; sbsec->def_sid = defcontext_sid;
} }
out_set_opts:
rc = sb_finish_set_opts(sb); rc = sb_finish_set_opts(sb);
out: out:
mutex_unlock(&sbsec->lock); mutex_unlock(&sbsec->lock);
@ -2259,7 +2282,7 @@ static int check_nnp_nosuid(const struct linux_binprm *bprm,
const struct task_security_struct *new_tsec) const struct task_security_struct *new_tsec)
{ {
int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
int nosuid = (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID); int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
int rc; int rc;
if (!nnp && !nosuid) if (!nnp && !nosuid)

View File

@ -90,9 +90,15 @@ struct superblock_smack {
struct smack_known *smk_floor; struct smack_known *smk_floor;
struct smack_known *smk_hat; struct smack_known *smk_hat;
struct smack_known *smk_default; struct smack_known *smk_default;
int smk_initialized; int smk_flags;
}; };
/*
* Superblock flags
*/
#define SMK_SB_INITIALIZED 0x01
#define SMK_SB_UNTRUSTED 0x02
struct socket_smack { struct socket_smack {
struct smack_known *smk_out; /* outbound label */ struct smack_known *smk_out; /* outbound label */
struct smack_known *smk_in; /* inbound label */ struct smack_known *smk_in; /* inbound label */

View File

@ -549,7 +549,7 @@ static int smack_sb_alloc_security(struct super_block *sb)
sbsp->smk_floor = &smack_known_floor; sbsp->smk_floor = &smack_known_floor;
sbsp->smk_hat = &smack_known_hat; sbsp->smk_hat = &smack_known_hat;
/* /*
* smk_initialized will be zero from kzalloc. * SMK_SB_INITIALIZED will be zero from kzalloc.
*/ */
sb->s_security = sbsp; sb->s_security = sbsp;
@ -766,10 +766,10 @@ static int smack_set_mnt_opts(struct super_block *sb,
int num_opts = opts->num_mnt_opts; int num_opts = opts->num_mnt_opts;
int transmute = 0; int transmute = 0;
if (sp->smk_initialized) if (sp->smk_flags & SMK_SB_INITIALIZED)
return 0; return 0;
sp->smk_initialized = 1; sp->smk_flags |= SMK_SB_INITIALIZED;
for (i = 0; i < num_opts; i++) { for (i = 0; i < num_opts; i++) {
switch (opts->mnt_opts_flags[i]) { switch (opts->mnt_opts_flags[i]) {
@ -821,6 +821,17 @@ static int smack_set_mnt_opts(struct super_block *sb,
skp = smk_of_current(); skp = smk_of_current();
sp->smk_root = skp; sp->smk_root = skp;
sp->smk_default = skp; sp->smk_default = skp;
/*
* For a handful of fs types with no user-controlled
* backing store it's okay to trust security labels
* in the filesystem. The rest are untrusted.
*/
if (sb->s_user_ns != &init_user_ns &&
sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
sb->s_magic != RAMFS_MAGIC) {
transmute = 1;
sp->smk_flags |= SMK_SB_UNTRUSTED;
}
} }
/* /*
@ -908,6 +919,7 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
struct inode *inode = file_inode(bprm->file); struct inode *inode = file_inode(bprm->file);
struct task_smack *bsp = bprm->cred->security; struct task_smack *bsp = bprm->cred->security;
struct inode_smack *isp; struct inode_smack *isp;
struct superblock_smack *sbsp;
int rc; int rc;
if (bprm->cred_prepared) if (bprm->cred_prepared)
@ -917,6 +929,11 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
return 0; return 0;
sbsp = inode->i_sb->s_security;
if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
isp->smk_task != sbsp->smk_root)
return 0;
if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
struct task_struct *tracer; struct task_struct *tracer;
rc = 0; rc = 0;
@ -1203,6 +1220,7 @@ static int smack_inode_rename(struct inode *old_inode,
*/ */
static int smack_inode_permission(struct inode *inode, int mask) static int smack_inode_permission(struct inode *inode, int mask)
{ {
struct superblock_smack *sbsp = inode->i_sb->s_security;
struct smk_audit_info ad; struct smk_audit_info ad;
int no_block = mask & MAY_NOT_BLOCK; int no_block = mask & MAY_NOT_BLOCK;
int rc; int rc;
@ -1214,6 +1232,11 @@ static int smack_inode_permission(struct inode *inode, int mask)
if (mask == 0) if (mask == 0)
return 0; return 0;
if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
if (smk_of_inode(inode) != sbsp->smk_root)
return -EACCES;
}
/* May be droppable after audit */ /* May be droppable after audit */
if (no_block) if (no_block)
return -ECHILD; return -ECHILD;
@ -1708,6 +1731,7 @@ static int smack_mmap_file(struct file *file,
struct task_smack *tsp; struct task_smack *tsp;
struct smack_known *okp; struct smack_known *okp;
struct inode_smack *isp; struct inode_smack *isp;
struct superblock_smack *sbsp;
int may; int may;
int mmay; int mmay;
int tmay; int tmay;
@ -1719,6 +1743,10 @@ static int smack_mmap_file(struct file *file,
isp = file_inode(file)->i_security; isp = file_inode(file)->i_security;
if (isp->smk_mmap == NULL) if (isp->smk_mmap == NULL)
return 0; return 0;
sbsp = file_inode(file)->i_sb->s_security;
if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
isp->smk_mmap != sbsp->smk_root)
return -EACCES;
mkp = isp->smk_mmap; mkp = isp->smk_mmap;
tsp = current_security(); tsp = current_security();