1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * The "user cache".
4 *
5 * (C) Copyright 1991-2000 Linus Torvalds
6 *
7 * We have a per-user structure to keep track of how many
8 * processes, files etc the user has claimed, in order to be
9 * able to have per-user limits for system resources.
10 */
11
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/bitops.h>
16 #include <linux/key.h>
17 #include <linux/sched/user.h>
18 #include <linux/interrupt.h>
19 #include <linux/export.h>
20 #include <linux/user_namespace.h>
21 #include <linux/proc_ns.h>
22
23 #include <trace/hooks/user.h>
24
25 /*
26 * userns count is 1 for root user, 1 for init_uts_ns,
27 * and 1 for... ?
28 */
29 struct user_namespace init_user_ns = {
30 .uid_map = {
31 .nr_extents = 1,
32 {
33 .extent[0] = {
34 .first = 0,
35 .lower_first = 0,
36 .count = 4294967295U,
37 },
38 },
39 },
40 .gid_map = {
41 .nr_extents = 1,
42 {
43 .extent[0] = {
44 .first = 0,
45 .lower_first = 0,
46 .count = 4294967295U,
47 },
48 },
49 },
50 .projid_map = {
51 .nr_extents = 1,
52 {
53 .extent[0] = {
54 .first = 0,
55 .lower_first = 0,
56 .count = 4294967295U,
57 },
58 },
59 },
60 .ns.count = REFCOUNT_INIT(3),
61 .owner = GLOBAL_ROOT_UID,
62 .group = GLOBAL_ROOT_GID,
63 .ns.inum = PROC_USER_INIT_INO,
64 #ifdef CONFIG_USER_NS
65 .ns.ops = &userns_operations,
66 #endif
67 .flags = USERNS_INIT_FLAGS,
68 #ifdef CONFIG_KEYS
69 .keyring_name_list = LIST_HEAD_INIT(init_user_ns.keyring_name_list),
70 .keyring_sem = __RWSEM_INITIALIZER(init_user_ns.keyring_sem),
71 #endif
72 };
73 EXPORT_SYMBOL_GPL(init_user_ns);
74
75 /*
76 * UID task count cache, to get fast user lookup in "alloc_uid"
77 * when changing user ID's (ie setuid() and friends).
78 */
79
80 #define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 7)
81 #define UIDHASH_SZ (1 << UIDHASH_BITS)
82 #define UIDHASH_MASK (UIDHASH_SZ - 1)
83 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
84 #define uidhashentry(uid) (uidhash_table + __uidhashfn((__kuid_val(uid))))
85
86 static struct kmem_cache *uid_cachep;
87 static struct hlist_head uidhash_table[UIDHASH_SZ];
88
89 /*
90 * The uidhash_lock is mostly taken from process context, but it is
91 * occasionally also taken from softirq/tasklet context, when
92 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
93 * But free_uid() is also called with local interrupts disabled, and running
94 * local_bh_enable() with local interrupts disabled is an error - we'll run
95 * softirq callbacks, and they can unconditionally enable interrupts, and
96 * the caller of free_uid() didn't expect that..
97 */
98 static DEFINE_SPINLOCK(uidhash_lock);
99
100 /* root_user.__count is 1, for init task cred */
101 struct ext_user_struct ext_root_user = {
102 .user.__count = REFCOUNT_INIT(1),
103 .user.uid = GLOBAL_ROOT_UID,
104 .user.ratelimit = RATELIMIT_STATE_INIT(root_user.ratelimit, 0, 0),
105 };
106 struct user_struct *root_user = &ext_root_user.user;
107
108 /*
109 * These routines must be called with the uidhash spinlock held!
110 */
uid_hash_insert(struct user_struct * up,struct hlist_head * hashent)111 static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
112 {
113 hlist_add_head(&up->uidhash_node, hashent);
114 }
115
uid_hash_remove(struct user_struct * up)116 static void uid_hash_remove(struct user_struct *up)
117 {
118 hlist_del_init(&up->uidhash_node);
119 }
120
uid_hash_find(kuid_t uid,struct hlist_head * hashent)121 static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent)
122 {
123 struct user_struct *user;
124
125 hlist_for_each_entry(user, hashent, uidhash_node) {
126 if (uid_eq(user->uid, uid)) {
127 refcount_inc(&user->__count);
128 return user;
129 }
130 }
131
132 return NULL;
133 }
134
user_epoll_alloc(struct user_struct * up)135 static int user_epoll_alloc(struct user_struct *up)
136 {
137 #ifdef CONFIG_EPOLL
138 return percpu_counter_init(&up->epoll_watches, 0, GFP_KERNEL);
139 #else
140 return 0;
141 #endif
142 }
143
user_epoll_free(struct user_struct * up)144 static void user_epoll_free(struct user_struct *up)
145 {
146 #ifdef CONFIG_EPOLL
147 percpu_counter_destroy(&up->epoll_watches);
148 #endif
149 }
150
151 /* IRQs are disabled and uidhash_lock is held upon function entry.
152 * IRQ state (as stored in flags) is restored and uidhash_lock released
153 * upon function exit.
154 */
free_user(struct user_struct * up,unsigned long flags)155 static void free_user(struct user_struct *up, unsigned long flags)
156 __releases(&uidhash_lock)
157 {
158 struct ext_user_struct *ext_user;
159
160 ext_user = container_of(up, struct ext_user_struct, user);
161 trace_android_vh_free_user(up);
162 uid_hash_remove(up);
163 spin_unlock_irqrestore(&uidhash_lock, flags);
164 user_epoll_free(up);
165 kmem_cache_free(uid_cachep, ext_user);
166 }
167
168 /*
169 * Locate the user_struct for the passed UID. If found, take a ref on it. The
170 * caller must undo that ref with free_uid().
171 *
172 * If the user_struct could not be found, return NULL.
173 */
find_user(kuid_t uid)174 struct user_struct *find_user(kuid_t uid)
175 {
176 struct user_struct *ret;
177 unsigned long flags;
178
179 spin_lock_irqsave(&uidhash_lock, flags);
180 ret = uid_hash_find(uid, uidhashentry(uid));
181 spin_unlock_irqrestore(&uidhash_lock, flags);
182 return ret;
183 }
184 EXPORT_SYMBOL_GPL(find_user);
185
free_uid(struct user_struct * up)186 void free_uid(struct user_struct *up)
187 {
188 unsigned long flags;
189
190 if (!up)
191 return;
192
193 if (refcount_dec_and_lock_irqsave(&up->__count, &uidhash_lock, &flags))
194 free_user(up, flags);
195 }
196 EXPORT_SYMBOL_GPL(free_uid);
197
alloc_uid(kuid_t uid)198 struct user_struct *alloc_uid(kuid_t uid)
199 {
200 struct hlist_head *hashent = uidhashentry(uid);
201 struct user_struct *up, *new;
202 struct ext_user_struct *ext_user;
203
204 spin_lock_irq(&uidhash_lock);
205 up = uid_hash_find(uid, hashent);
206 spin_unlock_irq(&uidhash_lock);
207
208 if (!up) {
209 ext_user = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
210 if (!ext_user)
211 return NULL;
212
213 new = &ext_user->user;
214 new->uid = uid;
215 refcount_set(&new->__count, 1);
216 trace_android_vh_alloc_uid(new);
217 if (user_epoll_alloc(new)) {
218 kmem_cache_free(uid_cachep, new);
219 return NULL;
220 }
221 ratelimit_state_init(&new->ratelimit, HZ, 100);
222 ratelimit_set_flags(&new->ratelimit, RATELIMIT_MSG_ON_RELEASE);
223
224 /*
225 * Before adding this, check whether we raced
226 * on adding the same user already..
227 */
228 spin_lock_irq(&uidhash_lock);
229 up = uid_hash_find(uid, hashent);
230 if (up) {
231 user_epoll_free(new);
232 kmem_cache_free(uid_cachep, new);
233 } else {
234 uid_hash_insert(new, hashent);
235 up = new;
236 }
237 spin_unlock_irq(&uidhash_lock);
238 }
239
240 return up;
241 }
242
uid_cache_init(void)243 static int __init uid_cache_init(void)
244 {
245 int n;
246
247 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct ext_user_struct),
248 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
249
250 for(n = 0; n < UIDHASH_SZ; ++n)
251 INIT_HLIST_HEAD(uidhash_table + n);
252
253 if (user_epoll_alloc(root_user))
254 panic("root_user epoll percpu counter alloc failed");
255
256 /* Insert the root user immediately (init already runs as root) */
257 spin_lock_irq(&uidhash_lock);
258 uid_hash_insert(root_user, uidhashentry(GLOBAL_ROOT_UID));
259 spin_unlock_irq(&uidhash_lock);
260
261 return 0;
262 }
263 subsys_initcall(uid_cache_init);
264