Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/mfd-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / user_namespace.c
CommitLineData
acce292c
CLG
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
8#include <linux/module.h>
acce292c 9#include <linux/nsproxy.h>
1aeb272c 10#include <linux/slab.h>
acce292c 11#include <linux/user_namespace.h>
5c1469de 12#include <linux/highuid.h>
18b6e041 13#include <linux/cred.h>
acce292c 14
6164281a
PE
15static struct kmem_cache *user_ns_cachep __read_mostly;
16
77ec739d 17/*
18b6e041
SH
18 * 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.
77ec739d 24 */
18b6e041 25int create_user_ns(struct cred *new)
77ec739d
SH
26{
27 struct user_namespace *ns;
18b6e041 28 struct user_struct *root_user;
77ec739d
SH
29 int n;
30
6164281a 31 ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
77ec739d 32 if (!ns)
18b6e041 33 return -ENOMEM;
77ec739d
SH
34
35 kref_init(&ns->kref);
36
37 for (n = 0; n < UIDHASH_SZ; ++n)
735de223 38 INIT_HLIST_HEAD(ns->uidhash_table + n);
77ec739d 39
18b6e041
SH
40 /* Alloc new root user. */
41 root_user = alloc_uid(ns, 0);
42 if (!root_user) {
6164281a 43 kmem_cache_free(user_ns_cachep, ns);
18b6e041 44 return -ENOMEM;
77ec739d
SH
45 }
46
18b6e041
SH
47 /* 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 */
77ec739d 59
db1afffa
N
60 /* root_user holds a reference to ns, our reference can be dropped */
61 put_user_ns(ns);
77ec739d 62
18b6e041 63 return 0;
acce292c
CLG
64}
65
51708366
DH
66/*
67 * Deferred destructor for a user namespace. This is required because
68 * free_user_ns() may be called with uidhash_lock held, but we need to call
69 * back to free_uid() which will want to take the lock again.
70 */
71static void free_user_ns_work(struct work_struct *work)
acce292c 72{
51708366
DH
73 struct user_namespace *ns =
74 container_of(work, struct user_namespace, destroyer);
18b6e041 75 free_uid(ns->creator);
6164281a 76 kmem_cache_free(user_ns_cachep, ns);
acce292c 77}
51708366
DH
78
79void free_user_ns(struct kref *kref)
80{
81 struct user_namespace *ns =
82 container_of(kref, struct user_namespace, kref);
83
84 INIT_WORK(&ns->destroyer, free_user_ns_work);
85 schedule_work(&ns->destroyer);
86}
6a3fd92e 87EXPORT_SYMBOL(free_user_ns);
5c1469de
EB
88
89uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
90{
91 struct user_namespace *tmp;
92
93 if (likely(to == cred->user->user_ns))
94 return uid;
95
96
97 /* Is cred->user the creator of the target user_ns
98 * or the creator of one of it's parents?
99 */
100 for ( tmp = to; tmp != &init_user_ns;
101 tmp = tmp->creator->user_ns ) {
102 if (cred->user == tmp->creator) {
103 return (uid_t)0;
104 }
105 }
106
107 /* No useful relationship so no mapping */
108 return overflowuid;
109}
110
111gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
112{
113 struct user_namespace *tmp;
114
115 if (likely(to == cred->user->user_ns))
116 return gid;
117
118 /* Is cred->user the creator of the target user_ns
119 * or the creator of one of it's parents?
120 */
121 for ( tmp = to; tmp != &init_user_ns;
122 tmp = tmp->creator->user_ns ) {
123 if (cred->user == tmp->creator) {
124 return (gid_t)0;
125 }
126 }
127
128 /* No useful relationship so no mapping */
129 return overflowgid;
130}
6164281a
PE
131
132static __init int user_namespaces_init(void)
133{
134 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
135 return 0;
136}
137module_init(user_namespaces_init);