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 user_struct root_user = {
102 .__count = REFCOUNT_INIT(1),
103 .uid = GLOBAL_ROOT_UID,
104 .ratelimit = RATELIMIT_STATE_INIT(root_user.ratelimit, 0, 0),
105 };
106
107 /*
108 * These routines must be called with the uidhash spinlock held!
109 */
uid_hash_insert(struct user_struct * up,struct hlist_head * hashent)110 static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
111 {
112 hlist_add_head(&up->uidhash_node, hashent);
113 }
114
uid_hash_remove(struct user_struct * up)115 static void uid_hash_remove(struct user_struct *up)
116 {
117 hlist_del_init(&up->uidhash_node);
118 }
119
uid_hash_find(kuid_t uid,struct hlist_head * hashent)120 static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent)
121 {
122 struct user_struct *user;
123
124 hlist_for_each_entry(user, hashent, uidhash_node) {
125 if (uid_eq(user->uid, uid)) {
126 refcount_inc(&user->__count);
127 return user;
128 }
129 }
130
131 return NULL;
132 }
133
user_epoll_alloc(struct user_struct * up)134 static int user_epoll_alloc(struct user_struct *up)
135 {
136 #ifdef CONFIG_EPOLL
137 return percpu_counter_init(&up->epoll_watches, 0, GFP_KERNEL);
138 #else
139 return 0;
140 #endif
141 }
142
user_epoll_free(struct user_struct * up)143 static void user_epoll_free(struct user_struct *up)
144 {
145 #ifdef CONFIG_EPOLL
146 percpu_counter_destroy(&up->epoll_watches);
147 #endif
148 }
149
150 /* IRQs are disabled and uidhash_lock is held upon function entry.
151 * IRQ state (as stored in flags) is restored and uidhash_lock released
152 * upon function exit.
153 */
free_user(struct user_struct * up,unsigned long flags)154 static void free_user(struct user_struct *up, unsigned long flags)
155 __releases(&uidhash_lock)
156 {
157 trace_android_vh_free_user(up);
158 uid_hash_remove(up);
159 spin_unlock_irqrestore(&uidhash_lock, flags);
160 user_epoll_free(up);
161 kmem_cache_free(uid_cachep, up);
162 }
163
164 /*
165 * Locate the user_struct for the passed UID. If found, take a ref on it. The
166 * caller must undo that ref with free_uid().
167 *
168 * If the user_struct could not be found, return NULL.
169 */
find_user(kuid_t uid)170 struct user_struct *find_user(kuid_t uid)
171 {
172 struct user_struct *ret;
173 unsigned long flags;
174
175 spin_lock_irqsave(&uidhash_lock, flags);
176 ret = uid_hash_find(uid, uidhashentry(uid));
177 spin_unlock_irqrestore(&uidhash_lock, flags);
178 return ret;
179 }
180 EXPORT_SYMBOL_GPL(find_user);
181
free_uid(struct user_struct * up)182 void free_uid(struct user_struct *up)
183 {
184 unsigned long flags;
185
186 if (!up)
187 return;
188
189 if (refcount_dec_and_lock_irqsave(&up->__count, &uidhash_lock, &flags))
190 free_user(up, flags);
191 }
192 EXPORT_SYMBOL_GPL(free_uid);
193
alloc_uid(kuid_t uid)194 struct user_struct *alloc_uid(kuid_t uid)
195 {
196 struct hlist_head *hashent = uidhashentry(uid);
197 struct user_struct *up, *new;
198
199 spin_lock_irq(&uidhash_lock);
200 up = uid_hash_find(uid, hashent);
201 spin_unlock_irq(&uidhash_lock);
202
203 if (!up) {
204 new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
205 if (!new)
206 return NULL;
207
208 new->uid = uid;
209 refcount_set(&new->__count, 1);
210 trace_android_vh_alloc_uid(new);
211 if (user_epoll_alloc(new)) {
212 kmem_cache_free(uid_cachep, new);
213 return NULL;
214 }
215 ratelimit_state_init(&new->ratelimit, HZ, 100);
216 ratelimit_set_flags(&new->ratelimit, RATELIMIT_MSG_ON_RELEASE);
217
218 /*
219 * Before adding this, check whether we raced
220 * on adding the same user already..
221 */
222 spin_lock_irq(&uidhash_lock);
223 up = uid_hash_find(uid, hashent);
224 if (up) {
225 user_epoll_free(new);
226 kmem_cache_free(uid_cachep, new);
227 } else {
228 uid_hash_insert(new, hashent);
229 up = new;
230 }
231 spin_unlock_irq(&uidhash_lock);
232 }
233
234 return up;
235 }
236
uid_cache_init(void)237 static int __init uid_cache_init(void)
238 {
239 int n;
240
241 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
242 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
243
244 for(n = 0; n < UIDHASH_SZ; ++n)
245 INIT_HLIST_HEAD(uidhash_table + n);
246
247 if (user_epoll_alloc(&root_user))
248 panic("root_user epoll percpu counter alloc failed");
249
250 /* Insert the root user immediately (init already runs as root) */
251 spin_lock_irq(&uidhash_lock);
252 uid_hash_insert(&root_user, uidhashentry(GLOBAL_ROOT_UID));
253 spin_unlock_irq(&uidhash_lock);
254
255 return 0;
256 }
257 subsys_initcall(uid_cache_init);
258