blob: 4e31ac9f01a4af325ca98f14402c43ceb086b938 [file] [log] [blame]
Cedric Le Goateracce2922007-07-15 23:40:59 -07001/*
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 Gortmaker9984de12011-05-23 14:51:41 -04008#include <linux/export.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -07009#include <linux/nsproxy.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070010#include <linux/slab.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070011#include <linux/user_namespace.h>
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000012#include <linux/highuid.h>
Serge Hallyn18b6e042008-10-15 16:38:45 -050013#include <linux/cred.h>
Jin Qian908651f2015-07-20 11:33:07 -070014#include <linux/proc_fs.h>
Eric W. Biedermand9281922011-11-17 01:59:07 -080015#include <linux/securebits.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070016
Pavel Emelyanov61642812011-01-12 17:00:46 -080017static struct kmem_cache *user_ns_cachep __read_mostly;
18
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070019/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050020 * Create a new user namespace, deriving the creator from the user in the
21 * passed credentials, and replacing that user with the new root user for the
22 * new namespace.
23 *
24 * This is called by copy_creds(), which will finish setting the target task's
25 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070026 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050027int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070028{
29 struct user_namespace *ns;
Serge Hallyn18b6e042008-10-15 16:38:45 -050030 struct user_struct *root_user;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070031 int n;
Eric W. Biederman75d9a222011-06-15 10:21:48 -070032 int ret;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070033
Pavel Emelyanov61642812011-01-12 17:00:46 -080034 ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070035 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050036 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070037
Eric W. Biederman75d9a222011-06-15 10:21:48 -070038 ret = proc_alloc_inum(&ns->proc_inum);
39 if (ret) {
40 kmem_cache_free(user_ns_cachep, ns);
41 return ret;
42 }
43
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070044 kref_init(&ns->kref);
45
46 for (n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -070047 INIT_HLIST_HEAD(ns->uidhash_table + n);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070048
Serge Hallyn18b6e042008-10-15 16:38:45 -050049 /* Alloc new root user. */
50 root_user = alloc_uid(ns, 0);
51 if (!root_user) {
Pavel Emelyanov61642812011-01-12 17:00:46 -080052 kmem_cache_free(user_ns_cachep, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -050053 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070054 }
55
Serge Hallyn18b6e042008-10-15 16:38:45 -050056 /* set the new root user in the credentials under preparation */
57 ns->creator = new->user;
58 new->user = root_user;
59 new->uid = new->euid = new->suid = new->fsuid = 0;
60 new->gid = new->egid = new->sgid = new->fsgid = 0;
61 put_group_info(new->group_info);
62 new->group_info = get_group_info(&init_groups);
Eric W. Biedermand9281922011-11-17 01:59:07 -080063 /* Start with the same capabilities as init but useless for doing
64 * anything as the capabilities are bound to the new user namespace.
65 */
66 new->securebits = SECUREBITS_DEFAULT;
67 new->cap_inheritable = CAP_EMPTY_SET;
68 new->cap_permitted = CAP_FULL_SET;
69 new->cap_effective = CAP_FULL_SET;
70 new->cap_bset = CAP_FULL_SET;
Serge Hallyn18b6e042008-10-15 16:38:45 -050071#ifdef CONFIG_KEYS
72 key_put(new->request_key_auth);
73 new->request_key_auth = NULL;
74#endif
75 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070076
NeilBrowndb1afff2010-03-16 15:14:51 +110077 /* root_user holds a reference to ns, our reference can be dropped */
78 put_user_ns(ns);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070079
Serge Hallyn18b6e042008-10-15 16:38:45 -050080 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070081}
82
David Howells51708362009-02-27 14:03:03 -080083/*
84 * Deferred destructor for a user namespace. This is required because
85 * free_user_ns() may be called with uidhash_lock held, but we need to call
86 * back to free_uid() which will want to take the lock again.
87 */
88static void free_user_ns_work(struct work_struct *work)
Cedric Le Goateracce2922007-07-15 23:40:59 -070089{
David Howells51708362009-02-27 14:03:03 -080090 struct user_namespace *ns =
91 container_of(work, struct user_namespace, destroyer);
Serge Hallyn18b6e042008-10-15 16:38:45 -050092 free_uid(ns->creator);
Eric W. Biederman75d9a222011-06-15 10:21:48 -070093 proc_free_inum(ns->proc_inum);
Pavel Emelyanov61642812011-01-12 17:00:46 -080094 kmem_cache_free(user_ns_cachep, ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -070095}
David Howells51708362009-02-27 14:03:03 -080096
97void free_user_ns(struct kref *kref)
98{
99 struct user_namespace *ns =
100 container_of(kref, struct user_namespace, kref);
101
102 INIT_WORK(&ns->destroyer, free_user_ns_work);
103 schedule_work(&ns->destroyer);
104}
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700105EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000106
107uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
108{
109 struct user_namespace *tmp;
110
111 if (likely(to == cred->user->user_ns))
112 return uid;
113
114
115 /* Is cred->user the creator of the target user_ns
116 * or the creator of one of it's parents?
117 */
118 for ( tmp = to; tmp != &init_user_ns;
119 tmp = tmp->creator->user_ns ) {
120 if (cred->user == tmp->creator) {
121 return (uid_t)0;
122 }
123 }
124
125 /* No useful relationship so no mapping */
126 return overflowuid;
127}
128
129gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
130{
131 struct user_namespace *tmp;
132
133 if (likely(to == cred->user->user_ns))
134 return gid;
135
136 /* Is cred->user the creator of the target user_ns
137 * or the creator of one of it's parents?
138 */
139 for ( tmp = to; tmp != &init_user_ns;
140 tmp = tmp->creator->user_ns ) {
141 if (cred->user == tmp->creator) {
142 return (gid_t)0;
143 }
144 }
145
146 /* No useful relationship so no mapping */
147 return overflowgid;
148}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800149
150static __init int user_namespaces_init(void)
151{
152 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
153 return 0;
154}
155module_init(user_namespaces_init);