userns: Simplify the user_namespace by making userns->creator a kuid.
[deliverable/linux.git] / kernel / user_namespace.c
index 3b906e98b1db5a6085cb8a115899a997f4863221..ed08836558e038bd287ab14be81eca78482ac6e0 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/user_namespace.h>
 #include <linux/highuid.h>
 #include <linux/cred.h>
+#include <linux/securebits.h>
 
 static struct kmem_cache *user_ns_cachep __read_mostly;
 
@@ -24,9 +25,18 @@ static struct kmem_cache *user_ns_cachep __read_mostly;
  */
 int create_user_ns(struct cred *new)
 {
-       struct user_namespace *ns;
+       struct user_namespace *ns, *parent_ns = new->user_ns;
        struct user_struct *root_user;
-       int n;
+       kuid_t owner = make_kuid(new->user_ns, new->euid);
+       kgid_t group = make_kgid(new->user_ns, new->egid);
+
+       /* The creator needs a mapping in the parent user namespace
+        * or else we won't be able to reasonably tell userspace who
+        * created a user_namespace.
+        */
+       if (!kuid_has_mapping(parent_ns, owner) ||
+           !kgid_has_mapping(parent_ns, group))
+               return -EPERM;
 
        ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
        if (!ns)
@@ -34,55 +44,52 @@ int create_user_ns(struct cred *new)
 
        kref_init(&ns->kref);
 
-       for (n = 0; n < UIDHASH_SZ; ++n)
-               INIT_HLIST_HEAD(ns->uidhash_table + n);
-
        /* Alloc new root user.  */
-       root_user = alloc_uid(ns, 0);
+       root_user = alloc_uid(make_kuid(ns, 0));
        if (!root_user) {
                kmem_cache_free(user_ns_cachep, ns);
                return -ENOMEM;
        }
 
        /* set the new root user in the credentials under preparation */
-       ns->creator = new->user;
+       ns->parent = parent_ns;
+       ns->owner = owner;
+       ns->group = group;
+       free_uid(new->user);
        new->user = root_user;
        new->uid = new->euid = new->suid = new->fsuid = 0;
        new->gid = new->egid = new->sgid = new->fsgid = 0;
        put_group_info(new->group_info);
        new->group_info = get_group_info(&init_groups);
+       /* Start with the same capabilities as init but useless for doing
+        * anything as the capabilities are bound to the new user namespace.
+        */
+       new->securebits = SECUREBITS_DEFAULT;
+       new->cap_inheritable = CAP_EMPTY_SET;
+       new->cap_permitted = CAP_FULL_SET;
+       new->cap_effective = CAP_FULL_SET;
+       new->cap_bset = CAP_FULL_SET;
 #ifdef CONFIG_KEYS
        key_put(new->request_key_auth);
        new->request_key_auth = NULL;
 #endif
        /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
 
-       /* root_user holds a reference to ns, our reference can be dropped */
-       put_user_ns(ns);
+       /* Leave the new->user_ns reference with the new user namespace. */
+       /* Leave the reference to our user_ns with the new cred. */
+       new->user_ns = ns;
 
        return 0;
 }
 
-/*
- * Deferred destructor for a user namespace.  This is required because
- * free_user_ns() may be called with uidhash_lock held, but we need to call
- * back to free_uid() which will want to take the lock again.
- */
-static void free_user_ns_work(struct work_struct *work)
-{
-       struct user_namespace *ns =
-               container_of(work, struct user_namespace, destroyer);
-       free_uid(ns->creator);
-       kmem_cache_free(user_ns_cachep, ns);
-}
-
 void free_user_ns(struct kref *kref)
 {
-       struct user_namespace *ns =
+       struct user_namespace *parent, *ns =
                container_of(kref, struct user_namespace, kref);
 
-       INIT_WORK(&ns->destroyer, free_user_ns_work);
-       schedule_work(&ns->destroyer);
+       parent = ns->parent;
+       kmem_cache_free(user_ns_cachep, ns);
+       put_user_ns(parent);
 }
 EXPORT_SYMBOL(free_user_ns);
 
@@ -90,16 +97,14 @@ uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t
 {
        struct user_namespace *tmp;
 
-       if (likely(to == cred->user->user_ns))
+       if (likely(to == cred->user_ns))
                return uid;
 
-
        /* Is cred->user the creator of the target user_ns
         * or the creator of one of it's parents?
         */
-       for ( tmp = to; tmp != &init_user_ns;
-             tmp = tmp->creator->user_ns ) {
-               if (cred->user == tmp->creator) {
+       for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
+               if (uid_eq(cred->user->uid, tmp->owner)) {
                        return (uid_t)0;
                }
        }
@@ -112,15 +117,14 @@ gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t
 {
        struct user_namespace *tmp;
 
-       if (likely(to == cred->user->user_ns))
+       if (likely(to == cred->user_ns))
                return gid;
 
        /* Is cred->user the creator of the target user_ns
         * or the creator of one of it's parents?
         */
-       for ( tmp = to; tmp != &init_user_ns;
-             tmp = tmp->creator->user_ns ) {
-               if (cred->user == tmp->creator) {
+       for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
+               if (uid_eq(cred->user->uid, tmp->owner)) {
                        return (gid_t)0;
                }
        }
This page took 0.031293 seconds and 5 git commands to generate.