Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License as |
| 4 | * published by the Free Software Foundation, version 2 of the |
| 5 | * License. |
| 6 | */ |
| 7 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 9 | #include <linux/nsproxy.h> |
Robert P. J. Day | 1aeb272 | 2008-04-29 00:59:25 -0700 | [diff] [blame] | 10 | #include <linux/slab.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 11 | #include <linux/user_namespace.h> |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 12 | #include <linux/highuid.h> |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 13 | #include <linux/cred.h> |
Eric W. Biederman | 973c591 | 2011-11-17 01:59:07 -0800 | [diff] [blame^] | 14 | #include <linux/securebits.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 15 | |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 16 | static struct kmem_cache *user_ns_cachep __read_mostly; |
| 17 | |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 18 | /* |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 19 | * Create a new user namespace, deriving the creator from the user in the |
| 20 | * passed credentials, and replacing that user with the new root user for the |
| 21 | * new namespace. |
| 22 | * |
| 23 | * This is called by copy_creds(), which will finish setting the target task's |
| 24 | * credentials. |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 25 | */ |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 26 | int create_user_ns(struct cred *new) |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 27 | { |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 28 | struct user_namespace *ns, *parent_ns = new->user_ns; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 29 | struct user_struct *root_user; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 30 | int n; |
| 31 | |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 32 | ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL); |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 33 | if (!ns) |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 34 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 35 | |
| 36 | kref_init(&ns->kref); |
| 37 | |
| 38 | for (n = 0; n < UIDHASH_SZ; ++n) |
Pavel Emelyanov | 735de22 | 2007-09-18 22:46:44 -0700 | [diff] [blame] | 39 | INIT_HLIST_HEAD(ns->uidhash_table + n); |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 40 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 41 | /* Alloc new root user. */ |
| 42 | root_user = alloc_uid(ns, 0); |
| 43 | if (!root_user) { |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 44 | kmem_cache_free(user_ns_cachep, ns); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 45 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 48 | /* set the new root user in the credentials under preparation */ |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 49 | ns->parent = parent_ns; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 50 | ns->creator = new->user; |
| 51 | new->user = root_user; |
| 52 | new->uid = new->euid = new->suid = new->fsuid = 0; |
| 53 | new->gid = new->egid = new->sgid = new->fsgid = 0; |
| 54 | put_group_info(new->group_info); |
| 55 | new->group_info = get_group_info(&init_groups); |
Eric W. Biederman | 973c591 | 2011-11-17 01:59:07 -0800 | [diff] [blame^] | 56 | /* Start with the same capabilities as init but useless for doing |
| 57 | * anything as the capabilities are bound to the new user namespace. |
| 58 | */ |
| 59 | new->securebits = SECUREBITS_DEFAULT; |
| 60 | new->cap_inheritable = CAP_EMPTY_SET; |
| 61 | new->cap_permitted = CAP_FULL_SET; |
| 62 | new->cap_effective = CAP_FULL_SET; |
| 63 | new->cap_bset = CAP_FULL_SET; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 64 | #ifdef CONFIG_KEYS |
| 65 | key_put(new->request_key_auth); |
| 66 | new->request_key_auth = NULL; |
| 67 | #endif |
| 68 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 69 | |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 70 | /* Leave the reference to our user_ns with the new cred */ |
| 71 | new->user_ns = ns; |
| 72 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 73 | return 0; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 74 | } |
| 75 | |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 76 | /* |
| 77 | * Deferred destructor for a user namespace. This is required because |
| 78 | * free_user_ns() may be called with uidhash_lock held, but we need to call |
| 79 | * back to free_uid() which will want to take the lock again. |
| 80 | */ |
| 81 | static void free_user_ns_work(struct work_struct *work) |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 82 | { |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 83 | struct user_namespace *parent, *ns = |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 84 | container_of(work, struct user_namespace, destroyer); |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 85 | parent = ns->parent; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 86 | free_uid(ns->creator); |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 87 | kmem_cache_free(user_ns_cachep, ns); |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 88 | put_user_ns(parent); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 89 | } |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 90 | |
| 91 | void free_user_ns(struct kref *kref) |
| 92 | { |
| 93 | struct user_namespace *ns = |
| 94 | container_of(kref, struct user_namespace, kref); |
| 95 | |
| 96 | INIT_WORK(&ns->destroyer, free_user_ns_work); |
| 97 | schedule_work(&ns->destroyer); |
| 98 | } |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 99 | EXPORT_SYMBOL(free_user_ns); |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 100 | |
| 101 | uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid) |
| 102 | { |
| 103 | struct user_namespace *tmp; |
| 104 | |
Eric W. Biederman | c4a4d60 | 2011-11-16 23:15:31 -0800 | [diff] [blame] | 105 | if (likely(to == cred->user_ns)) |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 106 | return uid; |
| 107 | |
| 108 | |
| 109 | /* Is cred->user the creator of the target user_ns |
| 110 | * or the creator of one of it's parents? |
| 111 | */ |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 112 | for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) { |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 113 | if (cred->user == tmp->creator) { |
| 114 | return (uid_t)0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* No useful relationship so no mapping */ |
| 119 | return overflowuid; |
| 120 | } |
| 121 | |
| 122 | gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid) |
| 123 | { |
| 124 | struct user_namespace *tmp; |
| 125 | |
Eric W. Biederman | c4a4d60 | 2011-11-16 23:15:31 -0800 | [diff] [blame] | 126 | if (likely(to == cred->user_ns)) |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 127 | return gid; |
| 128 | |
| 129 | /* Is cred->user the creator of the target user_ns |
| 130 | * or the creator of one of it's parents? |
| 131 | */ |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 132 | for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) { |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 133 | if (cred->user == tmp->creator) { |
| 134 | return (gid_t)0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* No useful relationship so no mapping */ |
| 139 | return overflowgid; |
| 140 | } |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 141 | |
| 142 | static __init int user_namespaces_init(void) |
| 143 | { |
| 144 | user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC); |
| 145 | return 0; |
| 146 | } |
| 147 | module_init(user_namespaces_init); |