Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
authorLinus Torvalds <torvalds@linux-foundation.org>
Wed, 17 Dec 2014 20:31:40 +0000 (12:31 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 17 Dec 2014 20:31:40 +0000 (12:31 -0800)
Pull user namespace related fixes from Eric Biederman:
 "As these are bug fixes almost all of thes changes are marked for
  backporting to stable.

  The first change (implicitly adding MNT_NODEV on remount) addresses a
  regression that was created when security issues with unprivileged
  remount were closed.  I go on to update the remount test to make it
  easy to detect if this issue reoccurs.

  Then there are a handful of mount and umount related fixes.

  Then half of the changes deal with the a recently discovered design
  bug in the permission checks of gid_map.  Unix since the beginning has
  allowed setting group permissions on files to less than the user and
  other permissions (aka ---rwx---rwx).  As the unix permission checks
  stop as soon as a group matches, and setgroups allows setting groups
  that can not later be dropped, results in a situtation where it is
  possible to legitimately use a group to assign fewer privileges to a
  process.  Which means dropping a group can increase a processes
  privileges.

  The fix I have adopted is that gid_map is now no longer writable
  without privilege unless the new file /proc/self/setgroups has been
  set to permanently disable setgroups.

  The bulk of user namespace using applications even the applications
  using applications using user namespaces without privilege remain
  unaffected by this change.  Unfortunately this ix breaks a couple user
  space applications, that were relying on the problematic behavior (one
  of which was tools/selftests/mount/unprivileged-remount-test.c).

  To hopefully prevent needing a regression fix on top of my security
  fix I rounded folks who work with the container implementations mostly
  like to be affected and encouraged them to test the changes.

    > So far nothing broke on my libvirt-lxc test bed. :-)
    > Tested with openSUSE 13.2 and libvirt 1.2.9.
    > Tested-by: Richard Weinberger <richard@nod.at>

    > Tested on Fedora20 with libvirt 1.2.11, works fine.
    > Tested-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>

    > Ok, thanks - yes, unprivileged lxc is working fine with your kernels.
    > Just to be sure I was testing the right thing I also tested using
    > my unprivileged nsexec testcases, and they failed on setgroup/setgid
    > as now expected, and succeeded there without your patches.
    > Tested-by: Serge Hallyn <serge.hallyn@ubuntu.com>

    > I tested this with Sandstorm.  It breaks as is and it works if I add
    > the setgroups thing.
    > Tested-by: Andy Lutomirski <luto@amacapital.net> # breaks things as designed :("

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace:
  userns: Unbreak the unprivileged remount tests
  userns; Correct the comment in map_write
  userns: Allow setting gid_maps without privilege when setgroups is disabled
  userns: Add a knob to disable setgroups on a per user namespace basis
  userns: Rename id_map_mutex to userns_state_mutex
  userns: Only allow the creator of the userns unprivileged mappings
  userns: Check euid no fsuid when establishing an unprivileged uid mapping
  userns: Don't allow unprivileged creation of gid mappings
  userns: Don't allow setgroups until a gid mapping has been setablished
  userns: Document what the invariant required for safe unprivileged mappings.
  groups: Consolidate the setgroups permission checks
  mnt: Clear mnt_expire during pivot_root
  mnt: Carefully set CL_UNPRIVILEGED in clone_mnt
  mnt: Move the clear of MNT_LOCKED from copy_tree to it's callers.
  umount: Do not allow unmounting rootfs.
  umount: Disallow unprivileged mount force
  mnt: Update unprivileged remount test
  mnt: Implicitly add MNT_NODEV on remount when it was implicitly added by mount

1  2 
fs/namespace.c
fs/proc/base.c
include/linux/user_namespace.h
kernel/user.c
kernel/user_namespace.c

diff --combined fs/namespace.c
@@@ -963,7 -963,8 +963,8 @@@ static struct mount *clone_mnt(struct m
        }
  
        /* Don't allow unprivileged users to reveal what is under a mount */
-       if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
+       if ((flag & CL_UNPRIVILEGED) &&
+           (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
                mnt->mnt.mnt_flags |= MNT_LOCKED;
  
        atomic_inc(&sb->s_active);
@@@ -1544,6 -1545,9 +1545,9 @@@ SYSCALL_DEFINE2(umount, char __user *, 
                goto dput_and_out;
        if (mnt->mnt.mnt_flags & MNT_LOCKED)
                goto dput_and_out;
+       retval = -EPERM;
+       if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
+               goto dput_and_out;
  
        retval = do_umount(mnt, flags);
  dput_and_out:
@@@ -1569,13 -1573,17 +1573,13 @@@ SYSCALL_DEFINE1(oldumount, char __user 
  static bool is_mnt_ns_file(struct dentry *dentry)
  {
        /* Is this a proxy for a mount namespace? */
 -      struct inode *inode = dentry->d_inode;
 -      struct proc_ns *ei;
 -
 -      if (!proc_ns_inode(inode))
 -              return false;
 -
 -      ei = get_proc_ns(inode);
 -      if (ei->ns_ops != &mntns_operations)
 -              return false;
 +      return dentry->d_op == &ns_dentry_operations &&
 +             dentry->d_fsdata == &mntns_operations;
 +}
  
 -      return true;
 +struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
 +{
 +      return container_of(ns, struct mnt_namespace, ns);
  }
  
  static bool mnt_ns_loop(struct dentry *dentry)
        if (!is_mnt_ns_file(dentry))
                return false;
  
 -      mnt_ns = get_proc_ns(dentry->d_inode)->ns;
 +      mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
        return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
  }
  
@@@ -1606,7 -1614,6 +1610,6 @@@ struct mount *copy_tree(struct mount *m
        if (IS_ERR(q))
                return q;
  
-       q->mnt.mnt_flags &= ~MNT_LOCKED;
        q->mnt_mountpoint = mnt->mnt_mountpoint;
  
        p = mnt;
@@@ -2016,10 -2023,7 +2019,10 @@@ static int do_loopback(struct path *pat
        if (IS_MNT_UNBINDABLE(old))
                goto out2;
  
 -      if (!check_mnt(parent) || !check_mnt(old))
 +      if (!check_mnt(parent))
 +              goto out2;
 +
 +      if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
                goto out2;
  
        if (!recurse && has_locked_children(old, old_path.dentry))
@@@ -2097,7 -2101,13 +2100,13 @@@ static int do_remount(struct path *path
        }
        if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
            !(mnt_flags & MNT_NODEV)) {
-               return -EPERM;
+               /* Was the nodev implicitly added in mount? */
+               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) &&
            !(mnt_flags & MNT_NOSUID)) {
@@@ -2639,7 -2649,7 +2648,7 @@@ dput_out
  
  static void free_mnt_ns(struct mnt_namespace *ns)
  {
 -      proc_free_inum(ns->proc_inum);
 +      ns_free_inum(&ns->ns);
        put_user_ns(ns->user_ns);
        kfree(ns);
  }
@@@ -2661,12 -2671,11 +2670,12 @@@ static struct mnt_namespace *alloc_mnt_
        new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
        if (!new_ns)
                return ERR_PTR(-ENOMEM);
 -      ret = proc_alloc_inum(&new_ns->proc_inum);
 +      ret = ns_alloc_inum(&new_ns->ns);
        if (ret) {
                kfree(new_ns);
                return ERR_PTR(ret);
        }
 +      new_ns->ns.ops = &mntns_operations;
        new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
        atomic_set(&new_ns->count, 1);
        new_ns->root = NULL;
@@@ -2958,6 -2967,8 +2967,8 @@@ SYSCALL_DEFINE2(pivot_root, const char 
        /* mount new_root on / */
        attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
        touch_mnt_namespace(current->nsproxy->mnt_ns);
+       /* A moved mount should not expire automatically */
+       list_del_init(&new_mnt->mnt_expire);
        unlock_mount_hash();
        chroot_fs_refs(&root, &new);
        put_mountpoint(root_mp);
@@@ -3002,6 -3013,7 +3013,7 @@@ static void __init init_mount_tree(void
  
        root.mnt = mnt;
        root.dentry = mnt->mnt_root;
+       mnt->mnt_flags |= MNT_LOCKED;
  
        set_fs_pwd(current->fs, &root);
        set_fs_root(current->fs, &root);
@@@ -3144,31 -3156,31 +3156,31 @@@ found
        return visible;
  }
  
 -static void *mntns_get(struct task_struct *task)
 +static struct ns_common *mntns_get(struct task_struct *task)
  {
 -      struct mnt_namespace *ns = NULL;
 +      struct ns_common *ns = NULL;
        struct nsproxy *nsproxy;
  
        task_lock(task);
        nsproxy = task->nsproxy;
        if (nsproxy) {
 -              ns = nsproxy->mnt_ns;
 -              get_mnt_ns(ns);
 +              ns = &nsproxy->mnt_ns->ns;
 +              get_mnt_ns(to_mnt_ns(ns));
        }
        task_unlock(task);
  
        return ns;
  }
  
 -static void mntns_put(void *ns)
 +static void mntns_put(struct ns_common *ns)
  {
 -      put_mnt_ns(ns);
 +      put_mnt_ns(to_mnt_ns(ns));
  }
  
 -static int mntns_install(struct nsproxy *nsproxy, void *ns)
 +static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  {
        struct fs_struct *fs = current->fs;
 -      struct mnt_namespace *mnt_ns = ns;
 +      struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
        struct path root;
  
        if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
        return 0;
  }
  
 -static unsigned int mntns_inum(void *ns)
 -{
 -      struct mnt_namespace *mnt_ns = ns;
 -      return mnt_ns->proc_inum;
 -}
 -
  const struct proc_ns_operations mntns_operations = {
        .name           = "mnt",
        .type           = CLONE_NEWNS,
        .get            = mntns_get,
        .put            = mntns_put,
        .install        = mntns_install,
 -      .inum           = mntns_inum,
  };
diff --combined fs/proc/base.c
@@@ -2464,6 -2464,57 +2464,57 @@@ static const struct file_operations pro
        .llseek         = seq_lseek,
        .release        = proc_id_map_release,
  };
+ static int proc_setgroups_open(struct inode *inode, struct file *file)
+ {
+       struct user_namespace *ns = NULL;
+       struct task_struct *task;
+       int ret;
+       ret = -ESRCH;
+       task = get_proc_task(inode);
+       if (task) {
+               rcu_read_lock();
+               ns = get_user_ns(task_cred_xxx(task, user_ns));
+               rcu_read_unlock();
+               put_task_struct(task);
+       }
+       if (!ns)
+               goto err;
+       if (file->f_mode & FMODE_WRITE) {
+               ret = -EACCES;
+               if (!ns_capable(ns, CAP_SYS_ADMIN))
+                       goto err_put_ns;
+       }
+       ret = single_open(file, &proc_setgroups_show, ns);
+       if (ret)
+               goto err_put_ns;
+       return 0;
+ err_put_ns:
+       put_user_ns(ns);
+ err:
+       return ret;
+ }
+ static int proc_setgroups_release(struct inode *inode, struct file *file)
+ {
+       struct seq_file *seq = file->private_data;
+       struct user_namespace *ns = seq->private;
+       int ret = single_release(inode, file);
+       put_user_ns(ns);
+       return ret;
+ }
+ static const struct file_operations proc_setgroups_operations = {
+       .open           = proc_setgroups_open,
+       .write          = proc_setgroups_write,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = proc_setgroups_release,
+ };
  #endif /* CONFIG_USER_NS */
  
  static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
@@@ -2572,6 -2623,7 +2623,7 @@@ static const struct pid_entry tgid_base
        REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
        REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
        REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
+       REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
  #endif
  #ifdef CONFIG_CHECKPOINT_RESTORE
        REG("timers",     S_IRUGO, proc_timers_operations),
@@@ -2618,9 -2670,6 +2670,9 @@@ static void proc_flush_task_mnt(struct 
                dput(dentry);
        }
  
 +      if (pid == tgid)
 +              return;
 +
        name.name = buf;
        name.len = snprintf(buf, sizeof(buf), "%d", tgid);
        leader = d_hash_and_lookup(mnt->mnt_root, &name);
@@@ -2792,7 -2841,7 +2844,7 @@@ retry
  int proc_pid_readdir(struct file *file, struct dir_context *ctx)
  {
        struct tgid_iter iter;
 -      struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
 +      struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
        loff_t pos = ctx->pos;
  
        if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
@@@ -2916,6 -2965,7 +2968,7 @@@ static const struct pid_entry tid_base_
        REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
        REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
        REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
+       REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
  #endif
  };
  
@@@ -3098,7 -3148,7 +3151,7 @@@ static int proc_task_readdir(struct fil
        /* f_version caches the tgid value that the last readdir call couldn't
         * return. lseek aka telldir automagically resets f_version to 0.
         */
 -      ns = file->f_dentry->d_sb->s_fs_info;
 +      ns = inode->i_sb->s_fs_info;
        tid = (int)file->f_version;
        file->f_version = 0;
        for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
@@@ -3,7 -3,6 +3,7 @@@
  
  #include <linux/kref.h>
  #include <linux/nsproxy.h>
 +#include <linux/ns_common.h>
  #include <linux/sched.h>
  #include <linux/err.h>
  
@@@ -18,6 -17,10 +18,10 @@@ struct uid_gid_map {        /* 64 bytes -- 1 c
        } extent[UID_GID_MAP_MAX_EXTENTS];
  };
  
+ #define USERNS_SETGROUPS_ALLOWED 1UL
+ #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
  struct user_namespace {
        struct uid_gid_map      uid_map;
        struct uid_gid_map      gid_map;
@@@ -27,7 -30,8 +31,8 @@@
        int                     level;
        kuid_t                  owner;
        kgid_t                  group;
 -      unsigned int            proc_inum;
 +      struct ns_common        ns;
+       unsigned long           flags;
  
        /* Register of per-UID persistent keyrings for this namespace */
  #ifdef CONFIG_PERSISTENT_KEYRINGS
@@@ -64,6 -68,9 +69,9 @@@ extern const struct seq_operations proc
  extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
  extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
  extern ssize_t proc_projid_map_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 bool userns_may_setgroups(const struct user_namespace *ns);
  #else
  
  static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
@@@ -88,6 -95,10 +96,10 @@@ static inline void put_user_ns(struct u
  {
  }
  
+ static inline bool userns_may_setgroups(const struct user_namespace *ns)
+ {
+       return true;
+ }
  #endif
  
  #endif /* _LINUX_USER_H */
diff --combined kernel/user.c
@@@ -50,10 -50,8 +50,11 @@@ struct user_namespace init_user_ns = 
        .count = ATOMIC_INIT(3),
        .owner = GLOBAL_ROOT_UID,
        .group = GLOBAL_ROOT_GID,
 -      .proc_inum = PROC_USER_INIT_INO,
 +      .ns.inum = PROC_USER_INIT_INO,
 +#ifdef CONFIG_USER_NS
 +      .ns.ops = &userns_operations,
 +#endif
+       .flags = USERNS_INIT_FLAGS,
  #ifdef CONFIG_PERSISTENT_KEYRINGS
        .persistent_keyring_register_sem =
        __RWSEM_INITIALIZER(init_user_ns.persistent_keyring_register_sem),
diff --combined kernel/user_namespace.c
@@@ -24,6 -24,7 +24,7 @@@
  #include <linux/fs_struct.h>
  
  static struct kmem_cache *user_ns_cachep __read_mostly;
+ static DEFINE_MUTEX(userns_state_mutex);
  
  static bool new_idmap_permitted(const struct file *file,
                                struct user_namespace *ns, int cap_setid,
@@@ -86,12 -87,11 +87,12 @@@ int create_user_ns(struct cred *new
        if (!ns)
                return -ENOMEM;
  
 -      ret = proc_alloc_inum(&ns->proc_inum);
 +      ret = ns_alloc_inum(&ns->ns);
        if (ret) {
                kmem_cache_free(user_ns_cachep, ns);
                return ret;
        }
 +      ns->ns.ops = &userns_operations;
  
        atomic_set(&ns->count, 1);
        /* Leave the new->user_ns reference with the new user namespace. */
        ns->owner = owner;
        ns->group = group;
  
+       /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
+       mutex_lock(&userns_state_mutex);
+       ns->flags = parent_ns->flags;
+       mutex_unlock(&userns_state_mutex);
        set_cred_user_ns(new, ns);
  
  #ifdef CONFIG_PERSISTENT_KEYRINGS
@@@ -137,7 -142,7 +143,7 @@@ void free_user_ns(struct user_namespac
  #ifdef CONFIG_PERSISTENT_KEYRINGS
                key_put(ns->persistent_keyring_register);
  #endif
 -              proc_free_inum(ns->proc_inum);
 +              ns_free_inum(&ns->ns);
                kmem_cache_free(user_ns_cachep, ns);
                ns = parent;
        } while (atomic_dec_and_test(&parent->count));
@@@ -584,9 -589,6 +590,6 @@@ static bool mappings_overlap(struct uid
        return false;
  }
  
- static DEFINE_MUTEX(id_map_mutex);
  static ssize_t map_write(struct file *file, const char __user *buf,
                         size_t count, loff_t *ppos,
                         int cap_setid,
        ssize_t ret = -EINVAL;
  
        /*
-        * The id_map_mutex serializes all writes to any given map.
+        * The userns_state_mutex serializes all writes to any given map.
         *
         * Any map is only ever written once.
         *
         * order and smp_rmb() is guaranteed that we don't have crazy
         * architectures returning stale data.
         */
-       mutex_lock(&id_map_mutex);
+       mutex_lock(&userns_state_mutex);
  
        ret = -EPERM;
        /* Only allow one successful write to the map */
        if (!page)
                goto out;
  
-       /* Only allow <= page size writes at the beginning of the file */
+       /* Only allow < page size writes at the beginning of the file */
        ret = -EINVAL;
        if ((*ppos != 0) || (count >= PAGE_SIZE))
                goto out;
        *ppos = count;
        ret = count;
  out:
-       mutex_unlock(&id_map_mutex);
+       mutex_unlock(&userns_state_mutex);
        if (page)
                free_page(page);
        return ret;
@@@ -813,16 -815,21 +816,21 @@@ static bool new_idmap_permitted(const s
                                struct user_namespace *ns, int cap_setid,
                                struct uid_gid_map *new_map)
  {
-       /* Allow mapping to your own filesystem ids */
-       if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
+       const struct cred *cred = file->f_cred;
+       /* Don't allow mappings that would allow anything that wouldn't
+        * be allowed without the establishment of unprivileged mappings.
+        */
+       if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
+           uid_eq(ns->owner, cred->euid)) {
                u32 id = new_map->extent[0].lower_first;
                if (cap_setid == CAP_SETUID) {
                        kuid_t uid = make_kuid(ns->parent, id);
-                       if (uid_eq(uid, file->f_cred->fsuid))
+                       if (uid_eq(uid, cred->euid))
                                return true;
                } else if (cap_setid == CAP_SETGID) {
                        kgid_t gid = make_kgid(ns->parent, id);
-                       if (gid_eq(gid, file->f_cred->fsgid))
+                       if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
+                           gid_eq(gid, cred->egid))
                                return true;
                }
        }
        return false;
  }
  
 -static void *userns_get(struct task_struct *task)
+ int proc_setgroups_show(struct seq_file *seq, void *v)
+ {
+       struct user_namespace *ns = seq->private;
+       unsigned long userns_flags = ACCESS_ONCE(ns->flags);
+       seq_printf(seq, "%s\n",
+                  (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
+                  "allow" : "deny");
+       return 0;
+ }
+ ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
+                            size_t count, loff_t *ppos)
+ {
+       struct seq_file *seq = file->private_data;
+       struct user_namespace *ns = seq->private;
+       char kbuf[8], *pos;
+       bool setgroups_allowed;
+       ssize_t ret;
+       /* Only allow a very narrow range of strings to be written */
+       ret = -EINVAL;
+       if ((*ppos != 0) || (count >= sizeof(kbuf)))
+               goto out;
+       /* What was written? */
+       ret = -EFAULT;
+       if (copy_from_user(kbuf, buf, count))
+               goto out;
+       kbuf[count] = '\0';
+       pos = kbuf;
+       /* What is being requested? */
+       ret = -EINVAL;
+       if (strncmp(pos, "allow", 5) == 0) {
+               pos += 5;
+               setgroups_allowed = true;
+       }
+       else if (strncmp(pos, "deny", 4) == 0) {
+               pos += 4;
+               setgroups_allowed = false;
+       }
+       else
+               goto out;
+       /* Verify there is not trailing junk on the line */
+       pos = skip_spaces(pos);
+       if (*pos != '\0')
+               goto out;
+       ret = -EPERM;
+       mutex_lock(&userns_state_mutex);
+       if (setgroups_allowed) {
+               /* Enabling setgroups after setgroups has been disabled
+                * is not allowed.
+                */
+               if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
+                       goto out_unlock;
+       } else {
+               /* Permanently disabling setgroups after setgroups has
+                * been enabled by writing the gid_map is not allowed.
+                */
+               if (ns->gid_map.nr_extents != 0)
+                       goto out_unlock;
+               ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
+       }
+       mutex_unlock(&userns_state_mutex);
+       /* Report a successful write */
+       *ppos = count;
+       ret = count;
+ out:
+       return ret;
+ out_unlock:
+       mutex_unlock(&userns_state_mutex);
+       goto out;
+ }
+ bool userns_may_setgroups(const struct user_namespace *ns)
+ {
+       bool allowed;
+       mutex_lock(&userns_state_mutex);
+       /* It is not safe to use setgroups until a gid mapping in
+        * the user namespace has been established.
+        */
+       allowed = ns->gid_map.nr_extents != 0;
+       /* Is setgroups allowed? */
+       allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
+       mutex_unlock(&userns_state_mutex);
+       return allowed;
+ }
 +static inline struct user_namespace *to_user_ns(struct ns_common *ns)
 +{
 +      return container_of(ns, struct user_namespace, ns);
 +}
 +
 +static struct ns_common *userns_get(struct task_struct *task)
  {
        struct user_namespace *user_ns;
  
        user_ns = get_user_ns(__task_cred(task)->user_ns);
        rcu_read_unlock();
  
 -      return user_ns;
 +      return user_ns ? &user_ns->ns : NULL;
  }
  
 -static void userns_put(void *ns)
 +static void userns_put(struct ns_common *ns)
  {
 -      put_user_ns(ns);
 +      put_user_ns(to_user_ns(ns));
  }
  
 -static int userns_install(struct nsproxy *nsproxy, void *ns)
 +static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  {
 -      struct user_namespace *user_ns = ns;
 +      struct user_namespace *user_ns = to_user_ns(ns);
        struct cred *cred;
  
        /* Don't allow gaining capabilities by reentering
        return commit_creds(cred);
  }
  
 -static unsigned int userns_inum(void *ns)
 -{
 -      struct user_namespace *user_ns = ns;
 -      return user_ns->proc_inum;
 -}
 -
  const struct proc_ns_operations userns_operations = {
        .name           = "user",
        .type           = CLONE_NEWUSER,
        .get            = userns_get,
        .put            = userns_put,
        .install        = userns_install,
 -      .inum           = userns_inum,
  };
  
  static __init int user_namespaces_init(void)