blob: 7e8215d87b40e70a13166dcc0efa064ee19a94d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ingo Molnar4021cb22006-01-25 15:23:07 +010016#include <linux/interrupt.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070017#include <linux/module.h>
18#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define UIDHASH_MASK (UIDHASH_SZ - 1)
26#define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
Cedric Le Goateracce2922007-07-15 23:40:59 -070027#define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *uid_cachep;
Ingo Molnar4021cb22006-01-25 15:23:07 +010030
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.
Andrew Morton3fa97c92006-01-31 16:34:26 -080035 * 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..
Ingo Molnar4021cb22006-01-25 15:23:07 +010039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static DEFINE_SPINLOCK(uidhash_lock);
41
42struct 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 .mq_bytes = 0,
48 .locked_shm = 0,
49#ifdef CONFIG_KEYS
50 .uid_keyring = &root_user_keyring,
51 .session_keyring = &root_session_keyring,
52#endif
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020053#ifdef CONFIG_FAIR_USER_SCHED
Ingo Molnar4cf86d72007-10-15 17:00:14 +020054 .tg = &init_task_group,
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
Dhaval Giani5cb350b2007-10-15 17:00:14 +020058/*
59 * These routines must be called with the uidhash spinlock held!
60 */
61static inline void uid_hash_insert(struct user_struct *up,
62 struct hlist_head *hashent)
63{
64 hlist_add_head(&up->uidhash_node, hashent);
65}
66
67static inline void uid_hash_remove(struct user_struct *up)
68{
69 hlist_del_init(&up->uidhash_node);
70}
71
72static inline struct user_struct *uid_hash_find(uid_t uid,
73 struct hlist_head *hashent)
74{
75 struct user_struct *user;
76 struct hlist_node *h;
77
78 hlist_for_each_entry(user, h, hashent, uidhash_node) {
79 if (user->uid == uid) {
80 atomic_inc(&user->__count);
81 return user;
82 }
83 }
84
85 return NULL;
86}
87
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020088#ifdef CONFIG_FAIR_USER_SCHED
Dhaval Giani5cb350b2007-10-15 17:00:14 +020089
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020090static void sched_destroy_user(struct user_struct *up)
91{
92 sched_destroy_group(up->tg);
93}
94
95static int sched_create_user(struct user_struct *up)
96{
97 int rc = 0;
98
99 up->tg = sched_create_group();
100 if (IS_ERR(up->tg))
101 rc = -ENOMEM;
102
103 return rc;
104}
105
106static void sched_switch_user(struct task_struct *p)
107{
108 sched_move_task(p);
109}
110
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200111#else /* CONFIG_FAIR_USER_SCHED */
112
113static void sched_destroy_user(struct user_struct *up) { }
114static int sched_create_user(struct user_struct *up) { return 0; }
115static void sched_switch_user(struct task_struct *p) { }
116
117#endif /* CONFIG_FAIR_USER_SCHED */
118
119#if defined(CONFIG_FAIR_USER_SCHED) && defined(CONFIG_SYSFS)
120
121static struct kobject uids_kobject; /* represents /sys/kernel/uids directory */
122static DEFINE_MUTEX(uids_mutex);
123
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200124static inline void uids_mutex_lock(void)
125{
126 mutex_lock(&uids_mutex);
127}
128
129static inline void uids_mutex_unlock(void)
130{
131 mutex_unlock(&uids_mutex);
132}
133
134/* return cpu shares held by the user */
135ssize_t cpu_shares_show(struct kset *kset, char *buffer)
136{
137 struct user_struct *up = container_of(kset, struct user_struct, kset);
138
139 return sprintf(buffer, "%lu\n", sched_group_shares(up->tg));
140}
141
142/* modify cpu shares held by the user */
143ssize_t cpu_shares_store(struct kset *kset, const char *buffer, size_t size)
144{
145 struct user_struct *up = container_of(kset, struct user_struct, kset);
146 unsigned long shares;
147 int rc;
148
149 sscanf(buffer, "%lu", &shares);
150
151 rc = sched_group_set_shares(up->tg, shares);
152
153 return (rc ? rc : size);
154}
155
156static void user_attr_init(struct subsys_attribute *sa, char *name, int mode)
157{
158 sa->attr.name = name;
159 sa->attr.mode = mode;
160 sa->show = cpu_shares_show;
161 sa->store = cpu_shares_store;
162}
163
164/* Create "/sys/kernel/uids/<uid>" directory and
165 * "/sys/kernel/uids/<uid>/cpu_share" file for this user.
166 */
167static int user_kobject_create(struct user_struct *up)
168{
169 struct kset *kset = &up->kset;
170 struct kobject *kobj = &kset->kobj;
171 int error;
172
173 memset(kset, 0, sizeof(struct kset));
174 kobj->parent = &uids_kobject; /* create under /sys/kernel/uids dir */
175 kobject_set_name(kobj, "%d", up->uid);
176 kset_init(kset);
177 user_attr_init(&up->user_attr, "cpu_share", 0644);
178
179 error = kobject_add(kobj);
180 if (error)
181 goto done;
182
183 error = sysfs_create_file(kobj, &up->user_attr.attr);
184 if (error)
185 kobject_del(kobj);
186
Srivatsa Vaddagirifb7dde32007-10-15 17:00:18 +0200187 kobject_uevent(kobj, KOBJ_ADD);
188
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200189done:
190 return error;
191}
192
193/* create these in sysfs filesystem:
194 * "/sys/kernel/uids" directory
195 * "/sys/kernel/uids/0" directory (for root user)
196 * "/sys/kernel/uids/0/cpu_share" file (for root user)
197 */
198int __init uids_kobject_init(void)
199{
200 int error;
201
202 /* create under /sys/kernel dir */
203 uids_kobject.parent = &kernel_subsys.kobj;
Srivatsa Vaddagirifb7dde32007-10-15 17:00:18 +0200204 uids_kobject.kset = &kernel_subsys;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200205 kobject_set_name(&uids_kobject, "uids");
206 kobject_init(&uids_kobject);
207
208 error = kobject_add(&uids_kobject);
209 if (!error)
210 error = user_kobject_create(&root_user);
211
212 return error;
213}
214
215/* work function to remove sysfs directory for a user and free up
216 * corresponding structures.
217 */
218static void remove_user_sysfs_dir(struct work_struct *w)
219{
220 struct user_struct *up = container_of(w, struct user_struct, work);
221 struct kobject *kobj = &up->kset.kobj;
222 unsigned long flags;
223 int remove_user = 0;
224
225 /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
226 * atomic.
227 */
228 uids_mutex_lock();
229
230 local_irq_save(flags);
231
232 if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
233 uid_hash_remove(up);
234 remove_user = 1;
235 spin_unlock_irqrestore(&uidhash_lock, flags);
236 } else {
237 local_irq_restore(flags);
238 }
239
240 if (!remove_user)
241 goto done;
242
243 sysfs_remove_file(kobj, &up->user_attr.attr);
Srivatsa Vaddagirifb7dde32007-10-15 17:00:18 +0200244 kobject_uevent(kobj, KOBJ_REMOVE);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200245 kobject_del(kobj);
246
247 sched_destroy_user(up);
248 key_put(up->uid_keyring);
249 key_put(up->session_keyring);
250 kmem_cache_free(uid_cachep, up);
251
252done:
253 uids_mutex_unlock();
254}
255
256/* IRQs are disabled and uidhash_lock is held upon function entry.
257 * IRQ state (as stored in flags) is restored and uidhash_lock released
258 * upon function exit.
259 */
260static inline void free_user(struct user_struct *up, unsigned long flags)
261{
262 /* restore back the count */
263 atomic_inc(&up->__count);
264 spin_unlock_irqrestore(&uidhash_lock, flags);
265
266 INIT_WORK(&up->work, remove_user_sysfs_dir);
267 schedule_work(&up->work);
268}
269
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200270#else /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200271
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200272static inline int user_kobject_create(struct user_struct *up) { return 0; }
273static inline void uids_mutex_lock(void) { }
274static inline void uids_mutex_unlock(void) { }
275
276/* IRQs are disabled and uidhash_lock is held upon function entry.
277 * IRQ state (as stored in flags) is restored and uidhash_lock released
278 * upon function exit.
279 */
280static inline void free_user(struct user_struct *up, unsigned long flags)
281{
282 uid_hash_remove(up);
283 spin_unlock_irqrestore(&uidhash_lock, flags);
284 sched_destroy_user(up);
285 key_put(up->uid_keyring);
286 key_put(up->session_keyring);
287 kmem_cache_free(uid_cachep, up);
288}
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200289
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200290#endif
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * Locate the user_struct for the passed UID. If found, take a ref on it. The
294 * caller must undo that ref with free_uid().
295 *
296 * If the user_struct could not be found, return NULL.
297 */
298struct user_struct *find_user(uid_t uid)
299{
300 struct user_struct *ret;
Andrew Morton3fa97c92006-01-31 16:34:26 -0800301 unsigned long flags;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700302 struct user_namespace *ns = current->nsproxy->user_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Andrew Morton3fa97c92006-01-31 16:34:26 -0800304 spin_lock_irqsave(&uidhash_lock, flags);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700305 ret = uid_hash_find(uid, uidhashentry(ns, uid));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800306 spin_unlock_irqrestore(&uidhash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return ret;
308}
309
310void free_uid(struct user_struct *up)
311{
Andrew Morton3fa97c92006-01-31 16:34:26 -0800312 unsigned long flags;
313
Andrew Morton36f57412006-03-24 03:15:47 -0800314 if (!up)
315 return;
316
Andrew Morton3fa97c92006-01-31 16:34:26 -0800317 local_irq_save(flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200318 if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
319 free_user(up, flags);
320 else
Andrew Morton36f57412006-03-24 03:15:47 -0800321 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Cedric Le Goateracce2922007-07-15 23:40:59 -0700324struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Pavel Emelyanov735de222007-09-18 22:46:44 -0700326 struct hlist_head *hashent = uidhashentry(ns, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct user_struct *up;
328
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200329 /* Make uid_hash_find() + user_kobject_create() + uid_hash_insert()
330 * atomic.
331 */
332 uids_mutex_lock();
333
Andrew Morton3fa97c92006-01-31 16:34:26 -0800334 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 up = uid_hash_find(uid, hashent);
Andrew Morton3fa97c92006-01-31 16:34:26 -0800336 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!up) {
339 struct user_struct *new;
340
Christoph Lametere94b1762006-12-06 20:33:17 -0800341 new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!new)
343 return NULL;
344 new->uid = uid;
345 atomic_set(&new->__count, 1);
346 atomic_set(&new->processes, 0);
347 atomic_set(&new->files, 0);
348 atomic_set(&new->sigpending, 0);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700349#ifdef CONFIG_INOTIFY_USER
Robert Love0eeca282005-07-12 17:06:03 -0400350 atomic_set(&new->inotify_watches, 0);
351 atomic_set(&new->inotify_devs, 0);
352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 new->mq_bytes = 0;
355 new->locked_shm = 0;
356
Michael LeMayd7200242006-06-22 14:47:17 -0700357 if (alloc_uid_keyring(new, current) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 kmem_cache_free(uid_cachep, new);
359 return NULL;
360 }
361
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200362 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 return NULL;
367 }
368
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200369 if (user_kobject_create(new)) {
370 sched_destroy_user(new);
371 key_put(new->uid_keyring);
372 key_put(new->session_keyring);
373 kmem_cache_free(uid_cachep, new);
374 uids_mutex_unlock();
375 return NULL;
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Before adding this, check whether we raced
380 * on adding the same user already..
381 */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800382 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 up = uid_hash_find(uid, hashent);
384 if (up) {
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200385 /* This case is not possible when CONFIG_FAIR_USER_SCHED
386 * is defined, since we serialize alloc_uid() using
387 * uids_mutex. Hence no need to call
388 * sched_destroy_user() or remove_user_sysfs_dir().
389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 key_put(new->uid_keyring);
391 key_put(new->session_keyring);
392 kmem_cache_free(uid_cachep, new);
393 } else {
394 uid_hash_insert(new, hashent);
395 up = new;
396 }
Andrew Morton3fa97c92006-01-31 16:34:26 -0800397 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200400
401 uids_mutex_unlock();
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return up;
404}
405
406void switch_uid(struct user_struct *new_user)
407{
408 struct user_struct *old_user;
409
410 /* What if a process setreuid()'s and this brings the
411 * new uid over his NPROC rlimit? We can check this now
412 * cheaply with the new uid cache, so if it matters
413 * we should be checking for it. -DaveM
414 */
415 old_user = current->user;
416 atomic_inc(&new_user->processes);
417 atomic_dec(&old_user->processes);
418 switch_uid_keyring(new_user);
419 current->user = new_user;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200420 sched_switch_user(current);
Linus Torvalds45c18b02006-11-04 10:06:02 -0800421
422 /*
423 * We need to synchronize with __sigqueue_alloc()
424 * doing a get_uid(p->user).. If that saw the old
425 * user value, we need to wait until it has exited
426 * its critical region before we can free the old
427 * structure.
428 */
429 smp_mb();
430 spin_unlock_wait(&current->sighand->siglock);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 free_uid(old_user);
433 suid_keys(current);
434}
435
Pavel Emelyanov28f300d2007-09-18 22:46:45 -0700436void release_uids(struct user_namespace *ns)
437{
438 int i;
439 unsigned long flags;
440 struct hlist_head *head;
441 struct hlist_node *nd;
442
443 spin_lock_irqsave(&uidhash_lock, flags);
444 /*
445 * collapse the chains so that the user_struct-s will
446 * be still alive, but not in hashes. subsequent free_uid()
447 * will free them.
448 */
449 for (i = 0; i < UIDHASH_SZ; i++) {
450 head = ns->uidhash_table + i;
451 while (!hlist_empty(head)) {
452 nd = head->first;
453 hlist_del_init(nd);
454 }
455 }
456 spin_unlock_irqrestore(&uidhash_lock, flags);
457
458 free_uid(ns->root_user);
459}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461static int __init uid_cache_init(void)
462{
463 int n;
464
465 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +0900466 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 for(n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -0700469 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Insert the root user immediately (init already runs as root) */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800472 spin_lock_irq(&uidhash_lock);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700473 uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800474 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return 0;
477}
478
479module_init(uid_cache_init);