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