blob: 58bb8781a7784e40b660bb091a103bbd365c296c [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{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080027 struct user_namespace *ns, *parent_ns = new->user_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;
30
Pavel Emelyanov61642812011-01-12 17:00:46 -080031 ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070032 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050033 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070034
35 kref_init(&ns->kref);
36
37 for (n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -070038 INIT_HLIST_HEAD(ns->uidhash_table + n);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070039
Serge Hallyn18b6e042008-10-15 16:38:45 -050040 /* Alloc new root user. */
41 root_user = alloc_uid(ns, 0);
42 if (!root_user) {
Pavel Emelyanov61642812011-01-12 17:00:46 -080043 kmem_cache_free(user_ns_cachep, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -050044 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070045 }
46
Serge Hallyn18b6e042008-10-15 16:38:45 -050047 /* set the new root user in the credentials under preparation */
48 ns->creator = new->user;
49 new->user = root_user;
50 new->uid = new->euid = new->suid = new->fsuid = 0;
51 new->gid = new->egid = new->sgid = new->fsgid = 0;
52 put_group_info(new->group_info);
53 new->group_info = get_group_info(&init_groups);
54#ifdef CONFIG_KEYS
55 key_put(new->request_key_auth);
56 new->request_key_auth = NULL;
57#endif
58 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070059
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080060 /* Leave the reference to our user_ns with the new cred */
61 new->user_ns = ns;
62
63 put_user_ns(parent_ns);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070064
Serge Hallyn18b6e042008-10-15 16:38:45 -050065 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070066}
67
David Howells51708362009-02-27 14:03:03 -080068/*
69 * Deferred destructor for a user namespace. This is required because
70 * free_user_ns() may be called with uidhash_lock held, but we need to call
71 * back to free_uid() which will want to take the lock again.
72 */
73static void free_user_ns_work(struct work_struct *work)
Cedric Le Goateracce2922007-07-15 23:40:59 -070074{
David Howells51708362009-02-27 14:03:03 -080075 struct user_namespace *ns =
76 container_of(work, struct user_namespace, destroyer);
Serge Hallyn18b6e042008-10-15 16:38:45 -050077 free_uid(ns->creator);
Pavel Emelyanov61642812011-01-12 17:00:46 -080078 kmem_cache_free(user_ns_cachep, ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -070079}
David Howells51708362009-02-27 14:03:03 -080080
81void free_user_ns(struct kref *kref)
82{
83 struct user_namespace *ns =
84 container_of(kref, struct user_namespace, kref);
85
86 INIT_WORK(&ns->destroyer, free_user_ns_work);
87 schedule_work(&ns->destroyer);
88}
Michael Halcrow6a3fd922008-04-29 00:59:52 -070089EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000090
91uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
92{
93 struct user_namespace *tmp;
94
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -080095 if (likely(to == cred->user_ns))
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000096 return uid;
97
98
99 /* Is cred->user the creator of the target user_ns
100 * or the creator of one of it's parents?
101 */
102 for ( tmp = to; tmp != &init_user_ns;
103 tmp = tmp->creator->user_ns ) {
104 if (cred->user == tmp->creator) {
105 return (uid_t)0;
106 }
107 }
108
109 /* No useful relationship so no mapping */
110 return overflowuid;
111}
112
113gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
114{
115 struct user_namespace *tmp;
116
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800117 if (likely(to == cred->user_ns))
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000118 return gid;
119
120 /* Is cred->user the creator of the target user_ns
121 * or the creator of one of it's parents?
122 */
123 for ( tmp = to; tmp != &init_user_ns;
124 tmp = tmp->creator->user_ns ) {
125 if (cred->user == tmp->creator) {
126 return (gid_t)0;
127 }
128 }
129
130 /* No useful relationship so no mapping */
131 return overflowgid;
132}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800133
134static __init int user_namespaces_init(void)
135{
136 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
137 return 0;
138}
139module_init(user_namespaces_init);