xref: /linux-6.15/kernel/user.c (revision 40efcb05)
1 /*
2  * The "user cache".
3  *
4  * (C) Copyright 1991-2000 Linus Torvalds
5  *
6  * We have a per-user structure to keep track of how many
7  * processes, files etc the user has claimed, in order to be
8  * able to have per-user limits for system resources.
9  */
10 
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/bitops.h>
15 #include <linux/key.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/user_namespace.h>
19 
20 /*
21  * UID task count cache, to get fast user lookup in "alloc_uid"
22  * when changing user ID's (ie setuid() and friends).
23  */
24 
25 #define UIDHASH_MASK		(UIDHASH_SZ - 1)
26 #define __uidhashfn(uid)	(((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
27 #define uidhashentry(ns, uid)	((ns)->uidhash_table + __uidhashfn((uid)))
28 
29 static struct kmem_cache *uid_cachep;
30 
31 /*
32  * The uidhash_lock is mostly taken from process context, but it is
33  * occasionally also taken from softirq/tasklet context, when
34  * task-structs get RCU-freed. Hence all locking must be softirq-safe.
35  * But free_uid() is also called with local interrupts disabled, and running
36  * local_bh_enable() with local interrupts disabled is an error - we'll run
37  * softirq callbacks, and they can unconditionally enable interrupts, and
38  * the caller of free_uid() didn't expect that..
39  */
40 static DEFINE_SPINLOCK(uidhash_lock);
41 
42 struct user_struct root_user = {
43 	.__count	= ATOMIC_INIT(1),
44 	.processes	= ATOMIC_INIT(1),
45 	.files		= ATOMIC_INIT(0),
46 	.sigpending	= ATOMIC_INIT(0),
47 	.locked_shm     = 0,
48 #ifdef CONFIG_KEYS
49 	.uid_keyring	= &root_user_keyring,
50 	.session_keyring = &root_session_keyring,
51 #endif
52 #ifdef CONFIG_FAIR_USER_SCHED
53 	.tg		= &init_task_group,
54 #endif
55 };
56 
57 /*
58  * These routines must be called with the uidhash spinlock held!
59  */
60 static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
61 {
62 	hlist_add_head(&up->uidhash_node, hashent);
63 }
64 
65 static void uid_hash_remove(struct user_struct *up)
66 {
67 	hlist_del_init(&up->uidhash_node);
68 }
69 
70 static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
71 {
72 	struct user_struct *user;
73 	struct hlist_node *h;
74 
75 	hlist_for_each_entry(user, h, hashent, uidhash_node) {
76 		if (user->uid == uid) {
77 			atomic_inc(&user->__count);
78 			return user;
79 		}
80 	}
81 
82 	return NULL;
83 }
84 
85 #ifdef CONFIG_FAIR_USER_SCHED
86 
87 static void sched_destroy_user(struct user_struct *up)
88 {
89 	sched_destroy_group(up->tg);
90 }
91 
92 static int sched_create_user(struct user_struct *up)
93 {
94 	int rc = 0;
95 
96 	up->tg = sched_create_group();
97 	if (IS_ERR(up->tg))
98 		rc = -ENOMEM;
99 
100 	return rc;
101 }
102 
103 static void sched_switch_user(struct task_struct *p)
104 {
105 	sched_move_task(p);
106 }
107 
108 #else	/* CONFIG_FAIR_USER_SCHED */
109 
110 static void sched_destroy_user(struct user_struct *up) { }
111 static int sched_create_user(struct user_struct *up) { return 0; }
112 static void sched_switch_user(struct task_struct *p) { }
113 
114 #endif	/* CONFIG_FAIR_USER_SCHED */
115 
116 #if defined(CONFIG_FAIR_USER_SCHED) && defined(CONFIG_SYSFS)
117 
118 static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
119 static DEFINE_MUTEX(uids_mutex);
120 
121 static inline void uids_mutex_lock(void)
122 {
123 	mutex_lock(&uids_mutex);
124 }
125 
126 static inline void uids_mutex_unlock(void)
127 {
128 	mutex_unlock(&uids_mutex);
129 }
130 
131 /* uid directory attributes */
132 static ssize_t cpu_shares_show(struct kobject *kobj,
133 			       struct kobj_attribute *attr,
134 			       char *buf)
135 {
136 	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
137 
138 	return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
139 }
140 
141 static ssize_t cpu_shares_store(struct kobject *kobj,
142 				struct kobj_attribute *attr,
143 				const char *buf, size_t size)
144 {
145 	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
146 	unsigned long shares;
147 	int rc;
148 
149 	sscanf(buf, "%lu", &shares);
150 
151 	rc = sched_group_set_shares(up->tg, shares);
152 
153 	return (rc ? rc : size);
154 }
155 
156 static struct kobj_attribute cpu_share_attr =
157 	__ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
158 
159 /* default attributes per uid directory */
160 static struct attribute *uids_attributes[] = {
161 	&cpu_share_attr.attr,
162 	NULL
163 };
164 
165 /* the lifetime of user_struct is not managed by the core (now) */
166 static void uids_release(struct kobject *kobj)
167 {
168 	return;
169 }
170 
171 static struct kobj_type uids_ktype = {
172 	.sysfs_ops = &kobj_sysfs_ops,
173 	.default_attrs = uids_attributes,
174 	.release = uids_release,
175 };
176 
177 /* create /sys/kernel/uids/<uid>/cpu_share file for this user */
178 static int uids_user_create(struct user_struct *up)
179 {
180 	struct kobject *kobj = &up->kobj;
181 	int error;
182 
183 	memset(kobj, 0, sizeof(struct kobject));
184 	kobj->kset = uids_kset;
185 	error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
186 	if (error) {
187 		kobject_put(kobj);
188 		goto done;
189 	}
190 
191 	kobject_uevent(kobj, KOBJ_ADD);
192 done:
193 	return error;
194 }
195 
196 /* create these entries in sysfs:
197  * 	"/sys/kernel/uids" directory
198  * 	"/sys/kernel/uids/0" directory (for root user)
199  * 	"/sys/kernel/uids/0/cpu_share" file (for root user)
200  */
201 int __init uids_sysfs_init(void)
202 {
203 	uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
204 	if (!uids_kset)
205 		return -ENOMEM;
206 
207 	return uids_user_create(&root_user);
208 }
209 
210 /* work function to remove sysfs directory for a user and free up
211  * corresponding structures.
212  */
213 static void remove_user_sysfs_dir(struct work_struct *w)
214 {
215 	struct user_struct *up = container_of(w, struct user_struct, work);
216 	unsigned long flags;
217 	int remove_user = 0;
218 
219 	/* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
220 	 * atomic.
221 	 */
222 	uids_mutex_lock();
223 
224 	local_irq_save(flags);
225 
226 	if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
227 		uid_hash_remove(up);
228 		remove_user = 1;
229 		spin_unlock_irqrestore(&uidhash_lock, flags);
230 	} else {
231 		local_irq_restore(flags);
232 	}
233 
234 	if (!remove_user)
235 		goto done;
236 
237 	kobject_uevent(&up->kobj, KOBJ_REMOVE);
238 	kobject_del(&up->kobj);
239 	kobject_put(&up->kobj);
240 
241 	sched_destroy_user(up);
242 	key_put(up->uid_keyring);
243 	key_put(up->session_keyring);
244 	kmem_cache_free(uid_cachep, up);
245 
246 done:
247 	uids_mutex_unlock();
248 }
249 
250 /* IRQs are disabled and uidhash_lock is held upon function entry.
251  * IRQ state (as stored in flags) is restored and uidhash_lock released
252  * upon function exit.
253  */
254 static inline void free_user(struct user_struct *up, unsigned long flags)
255 {
256 	/* restore back the count */
257 	atomic_inc(&up->__count);
258 	spin_unlock_irqrestore(&uidhash_lock, flags);
259 
260 	INIT_WORK(&up->work, remove_user_sysfs_dir);
261 	schedule_work(&up->work);
262 }
263 
264 #else	/* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
265 
266 int uids_sysfs_init(void) { return 0; }
267 static inline int uids_user_create(struct user_struct *up) { return 0; }
268 static inline void uids_mutex_lock(void) { }
269 static inline void uids_mutex_unlock(void) { }
270 
271 /* IRQs are disabled and uidhash_lock is held upon function entry.
272  * IRQ state (as stored in flags) is restored and uidhash_lock released
273  * upon function exit.
274  */
275 static inline void free_user(struct user_struct *up, unsigned long flags)
276 {
277 	uid_hash_remove(up);
278 	spin_unlock_irqrestore(&uidhash_lock, flags);
279 	sched_destroy_user(up);
280 	key_put(up->uid_keyring);
281 	key_put(up->session_keyring);
282 	kmem_cache_free(uid_cachep, up);
283 }
284 
285 #endif
286 
287 /*
288  * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
289  * caller must undo that ref with free_uid().
290  *
291  * If the user_struct could not be found, return NULL.
292  */
293 struct user_struct *find_user(uid_t uid)
294 {
295 	struct user_struct *ret;
296 	unsigned long flags;
297 	struct user_namespace *ns = current->nsproxy->user_ns;
298 
299 	spin_lock_irqsave(&uidhash_lock, flags);
300 	ret = uid_hash_find(uid, uidhashentry(ns, uid));
301 	spin_unlock_irqrestore(&uidhash_lock, flags);
302 	return ret;
303 }
304 
305 void free_uid(struct user_struct *up)
306 {
307 	unsigned long flags;
308 
309 	if (!up)
310 		return;
311 
312 	local_irq_save(flags);
313 	if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
314 		free_user(up, flags);
315 	else
316 		local_irq_restore(flags);
317 }
318 
319 struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
320 {
321 	struct hlist_head *hashent = uidhashentry(ns, uid);
322 	struct user_struct *up;
323 
324 	/* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
325 	 * atomic.
326 	 */
327 	uids_mutex_lock();
328 
329 	spin_lock_irq(&uidhash_lock);
330 	up = uid_hash_find(uid, hashent);
331 	spin_unlock_irq(&uidhash_lock);
332 
333 	if (!up) {
334 		struct user_struct *new;
335 
336 		new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
337 		if (!new) {
338 			uids_mutex_unlock();
339 			return NULL;
340 		}
341 
342 		new->uid = uid;
343 		atomic_set(&new->__count, 1);
344 		atomic_set(&new->processes, 0);
345 		atomic_set(&new->files, 0);
346 		atomic_set(&new->sigpending, 0);
347 #ifdef CONFIG_INOTIFY_USER
348 		atomic_set(&new->inotify_watches, 0);
349 		atomic_set(&new->inotify_devs, 0);
350 #endif
351 #ifdef CONFIG_POSIX_MQUEUE
352 		new->mq_bytes = 0;
353 #endif
354 		new->locked_shm = 0;
355 
356 		if (alloc_uid_keyring(new, current) < 0) {
357 			kmem_cache_free(uid_cachep, new);
358 			uids_mutex_unlock();
359 			return NULL;
360 		}
361 
362 		if (sched_create_user(new) < 0) {
363 			key_put(new->uid_keyring);
364 			key_put(new->session_keyring);
365 			kmem_cache_free(uid_cachep, new);
366 			uids_mutex_unlock();
367 			return NULL;
368 		}
369 
370 		if (uids_user_create(new)) {
371 			sched_destroy_user(new);
372 			key_put(new->uid_keyring);
373 			key_put(new->session_keyring);
374 			kmem_cache_free(uid_cachep, new);
375 			uids_mutex_unlock();
376 			return NULL;
377 		}
378 
379 		/*
380 		 * Before adding this, check whether we raced
381 		 * on adding the same user already..
382 		 */
383 		spin_lock_irq(&uidhash_lock);
384 		up = uid_hash_find(uid, hashent);
385 		if (up) {
386 			/* This case is not possible when CONFIG_FAIR_USER_SCHED
387 			 * is defined, since we serialize alloc_uid() using
388 			 * uids_mutex. Hence no need to call
389 			 * sched_destroy_user() or remove_user_sysfs_dir().
390 			 */
391 			key_put(new->uid_keyring);
392 			key_put(new->session_keyring);
393 			kmem_cache_free(uid_cachep, new);
394 		} else {
395 			uid_hash_insert(new, hashent);
396 			up = new;
397 		}
398 		spin_unlock_irq(&uidhash_lock);
399 
400 	}
401 
402 	uids_mutex_unlock();
403 
404 	return up;
405 }
406 
407 void switch_uid(struct user_struct *new_user)
408 {
409 	struct user_struct *old_user;
410 
411 	/* What if a process setreuid()'s and this brings the
412 	 * new uid over his NPROC rlimit?  We can check this now
413 	 * cheaply with the new uid cache, so if it matters
414 	 * we should be checking for it.  -DaveM
415 	 */
416 	old_user = current->user;
417 	atomic_inc(&new_user->processes);
418 	atomic_dec(&old_user->processes);
419 	switch_uid_keyring(new_user);
420 	current->user = new_user;
421 	sched_switch_user(current);
422 
423 	/*
424 	 * We need to synchronize with __sigqueue_alloc()
425 	 * doing a get_uid(p->user).. If that saw the old
426 	 * user value, we need to wait until it has exited
427 	 * its critical region before we can free the old
428 	 * structure.
429 	 */
430 	smp_mb();
431 	spin_unlock_wait(&current->sighand->siglock);
432 
433 	free_uid(old_user);
434 	suid_keys(current);
435 }
436 
437 void release_uids(struct user_namespace *ns)
438 {
439 	int i;
440 	unsigned long flags;
441 	struct hlist_head *head;
442 	struct hlist_node *nd;
443 
444 	spin_lock_irqsave(&uidhash_lock, flags);
445 	/*
446 	 * collapse the chains so that the user_struct-s will
447 	 * be still alive, but not in hashes. subsequent free_uid()
448 	 * will free them.
449 	 */
450 	for (i = 0; i < UIDHASH_SZ; i++) {
451 		head = ns->uidhash_table + i;
452 		while (!hlist_empty(head)) {
453 			nd = head->first;
454 			hlist_del_init(nd);
455 		}
456 	}
457 	spin_unlock_irqrestore(&uidhash_lock, flags);
458 
459 	free_uid(ns->root_user);
460 }
461 
462 static int __init uid_cache_init(void)
463 {
464 	int n;
465 
466 	uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
467 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
468 
469 	for(n = 0; n < UIDHASH_SZ; ++n)
470 		INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
471 
472 	/* Insert the root user immediately (init already runs as root) */
473 	spin_lock_irq(&uidhash_lock);
474 	uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
475 	spin_unlock_irq(&uidhash_lock);
476 
477 	return 0;
478 }
479 
480 module_init(uid_cache_init);
481