blob: c14b7b9fe4190dad92344215f4cd8a0621a07d68 [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>
Cedric Le Goateracce2922007-07-15 23:40:59 -070014
Pavel Emelyanov61642812011-01-12 17:00:46 -080015static struct kmem_cache *user_ns_cachep __read_mostly;
16
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070017/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050018 * Create a new user namespace, deriving the creator from the user in the
19 * passed credentials, and replacing that user with the new root user for the
20 * new namespace.
21 *
22 * This is called by copy_creds(), which will finish setting the target task's
23 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070024 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050025int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070026{
27 struct user_namespace *ns;
Serge Hallyn18b6e042008-10-15 16:38:45 -050028 struct user_struct *root_user;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070029 int n;
Eric W. Biederman75d9a222011-06-15 10:21:48 -070030 int ret;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070031
Pavel Emelyanov61642812011-01-12 17:00:46 -080032 ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070033 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050034 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070035
Eric W. Biederman75d9a222011-06-15 10:21:48 -070036 ret = proc_alloc_inum(&ns->proc_inum);
37 if (ret) {
38 kmem_cache_free(user_ns_cachep, ns);
39 return ret;
40 }
41
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070042 kref_init(&ns->kref);
43
44 for (n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -070045 INIT_HLIST_HEAD(ns->uidhash_table + n);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070046
Serge Hallyn18b6e042008-10-15 16:38:45 -050047 /* Alloc new root user. */
48 root_user = alloc_uid(ns, 0);
49 if (!root_user) {
Pavel Emelyanov61642812011-01-12 17:00:46 -080050 kmem_cache_free(user_ns_cachep, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -050051 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070052 }
53
Serge Hallyn18b6e042008-10-15 16:38:45 -050054 /* set the new root user in the credentials under preparation */
55 ns->creator = new->user;
56 new->user = root_user;
57 new->uid = new->euid = new->suid = new->fsuid = 0;
58 new->gid = new->egid = new->sgid = new->fsgid = 0;
59 put_group_info(new->group_info);
60 new->group_info = get_group_info(&init_groups);
61#ifdef CONFIG_KEYS
62 key_put(new->request_key_auth);
63 new->request_key_auth = NULL;
64#endif
65 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070066
NeilBrowndb1afff2010-03-16 15:14:51 +110067 /* root_user holds a reference to ns, our reference can be dropped */
68 put_user_ns(ns);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070069
Serge Hallyn18b6e042008-10-15 16:38:45 -050070 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070071}
72
David Howells51708362009-02-27 14:03:03 -080073/*
74 * Deferred destructor for a user namespace. This is required because
75 * free_user_ns() may be called with uidhash_lock held, but we need to call
76 * back to free_uid() which will want to take the lock again.
77 */
78static void free_user_ns_work(struct work_struct *work)
Cedric Le Goateracce2922007-07-15 23:40:59 -070079{
David Howells51708362009-02-27 14:03:03 -080080 struct user_namespace *ns =
81 container_of(work, struct user_namespace, destroyer);
Serge Hallyn18b6e042008-10-15 16:38:45 -050082 free_uid(ns->creator);
Eric W. Biederman75d9a222011-06-15 10:21:48 -070083 proc_free_inum(ns->proc_inum);
Pavel Emelyanov61642812011-01-12 17:00:46 -080084 kmem_cache_free(user_ns_cachep, ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -070085}
David Howells51708362009-02-27 14:03:03 -080086
87void free_user_ns(struct kref *kref)
88{
89 struct user_namespace *ns =
90 container_of(kref, struct user_namespace, kref);
91
92 INIT_WORK(&ns->destroyer, free_user_ns_work);
93 schedule_work(&ns->destroyer);
94}
Michael Halcrow6a3fd922008-04-29 00:59:52 -070095EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000096
97uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
98{
99 struct user_namespace *tmp;
100
101 if (likely(to == cred->user->user_ns))
102 return uid;
103
104
105 /* Is cred->user the creator of the target user_ns
106 * or the creator of one of it's parents?
107 */
108 for ( tmp = to; tmp != &init_user_ns;
109 tmp = tmp->creator->user_ns ) {
110 if (cred->user == tmp->creator) {
111 return (uid_t)0;
112 }
113 }
114
115 /* No useful relationship so no mapping */
116 return overflowuid;
117}
118
119gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
120{
121 struct user_namespace *tmp;
122
123 if (likely(to == cred->user->user_ns))
124 return gid;
125
126 /* Is cred->user the creator of the target user_ns
127 * or the creator of one of it's parents?
128 */
129 for ( tmp = to; tmp != &init_user_ns;
130 tmp = tmp->creator->user_ns ) {
131 if (cred->user == tmp->creator) {
132 return (gid_t)0;
133 }
134 }
135
136 /* No useful relationship so no mapping */
137 return overflowgid;
138}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800139
140static __init int user_namespaces_init(void)
141{
142 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
143 return 0;
144}
145module_init(user_namespaces_init);