Merge branch 'core-hweight-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.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>
1da177e4 19
aee16ce7
PE
20struct user_namespace init_user_ns = {
21 .kref = {
1d1e9756 22 .refcount = ATOMIC_INIT(2),
aee16ce7 23 },
18b6e041 24 .creator = &root_user,
aee16ce7
PE
25};
26EXPORT_SYMBOL_GPL(init_user_ns);
27
1da177e4
LT
28/*
29 * UID task count cache, to get fast user lookup in "alloc_uid"
30 * when changing user ID's (ie setuid() and friends).
31 */
32
1da177e4
LT
33#define UIDHASH_MASK (UIDHASH_SZ - 1)
34#define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
acce292c 35#define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
1da177e4 36
e18b890b 37static struct kmem_cache *uid_cachep;
4021cb27
IM
38
39/*
40 * The uidhash_lock is mostly taken from process context, but it is
41 * occasionally also taken from softirq/tasklet context, when
42 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
3fa97c9d
AM
43 * But free_uid() is also called with local interrupts disabled, and running
44 * local_bh_enable() with local interrupts disabled is an error - we'll run
45 * softirq callbacks, and they can unconditionally enable interrupts, and
46 * the caller of free_uid() didn't expect that..
4021cb27 47 */
1da177e4
LT
48static DEFINE_SPINLOCK(uidhash_lock);
49
18b6e041 50/* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->creator */
1da177e4 51struct user_struct root_user = {
18b6e041 52 .__count = ATOMIC_INIT(2),
1da177e4
LT
53 .processes = ATOMIC_INIT(1),
54 .files = ATOMIC_INIT(0),
55 .sigpending = ATOMIC_INIT(0),
1da177e4 56 .locked_shm = 0,
18b6e041 57 .user_ns = &init_user_ns,
1da177e4
LT
58};
59
5cb350ba
DG
60/*
61 * These routines must be called with the uidhash spinlock held!
62 */
40aeb400 63static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350ba
DG
64{
65 hlist_add_head(&up->uidhash_node, hashent);
66}
67
40aeb400 68static void uid_hash_remove(struct user_struct *up)
5cb350ba
DG
69{
70 hlist_del_init(&up->uidhash_node);
fb5ae64f 71 put_user_ns(up->user_ns);
5cb350ba
DG
72}
73
3959214f
KS
74static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
75{
76 struct user_struct *user;
77 struct hlist_node *h;
78
79 hlist_for_each_entry(user, h, hashent, uidhash_node) {
80 if (user->uid == uid) {
81 atomic_inc(&user->__count);
82 return user;
83 }
84 }
85
86 return NULL;
87}
88
5cb350ba
DG
89/* IRQs are disabled and uidhash_lock is held upon function entry.
90 * IRQ state (as stored in flags) is restored and uidhash_lock released
91 * upon function exit.
92 */
18b6e041 93static void free_user(struct user_struct *up, unsigned long flags)
5cb350ba
DG
94{
95 uid_hash_remove(up);
96 spin_unlock_irqrestore(&uidhash_lock, flags);
5cb350ba
DG
97 key_put(up->uid_keyring);
98 key_put(up->session_keyring);
99 kmem_cache_free(uid_cachep, up);
100}
101
1da177e4
LT
102/*
103 * Locate the user_struct for the passed UID. If found, take a ref on it. The
104 * caller must undo that ref with free_uid().
105 *
106 * If the user_struct could not be found, return NULL.
107 */
108struct user_struct *find_user(uid_t uid)
109{
110 struct user_struct *ret;
3fa97c9d 111 unsigned long flags;
6ded6ab9 112 struct user_namespace *ns = current_user_ns();
1da177e4 113
3fa97c9d 114 spin_lock_irqsave(&uidhash_lock, flags);
acce292c 115 ret = uid_hash_find(uid, uidhashentry(ns, uid));
3fa97c9d 116 spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4
LT
117 return ret;
118}
119
120void free_uid(struct user_struct *up)
121{
3fa97c9d
AM
122 unsigned long flags;
123
36f57413
AM
124 if (!up)
125 return;
126
3fa97c9d 127 local_irq_save(flags);
5cb350ba
DG
128 if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
129 free_user(up, flags);
130 else
36f57413 131 local_irq_restore(flags);
1da177e4
LT
132}
133
354a1f4d 134struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
1da177e4 135{
735de223 136 struct hlist_head *hashent = uidhashentry(ns, uid);
8eb703e4 137 struct user_struct *up, *new;
1da177e4 138
3fa97c9d 139 spin_lock_irq(&uidhash_lock);
1da177e4 140 up = uid_hash_find(uid, hashent);
3fa97c9d 141 spin_unlock_irq(&uidhash_lock);
1da177e4
LT
142
143 if (!up) {
354a1f4d 144 new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4
PE
145 if (!new)
146 goto out_unlock;
5e8869bb 147
1da177e4
LT
148 new->uid = uid;
149 atomic_set(&new->__count, 1);
1da177e4 150
18b6e041
SH
151 new->user_ns = get_user_ns(ns);
152
1da177e4
LT
153 /*
154 * Before adding this, check whether we raced
155 * on adding the same user already..
156 */
3fa97c9d 157 spin_lock_irq(&uidhash_lock);
1da177e4
LT
158 up = uid_hash_find(uid, hashent);
159 if (up) {
160 key_put(new->uid_keyring);
161 key_put(new->session_keyring);
162 kmem_cache_free(uid_cachep, new);
163 } else {
164 uid_hash_insert(new, hashent);
165 up = new;
166 }
3fa97c9d 167 spin_unlock_irq(&uidhash_lock);
1da177e4 168 }
5cb350ba 169
1da177e4 170 return up;
8eb703e4 171
8eb703e4 172out_unlock:
8eb703e4 173 return NULL;
1da177e4
LT
174}
175
1da177e4
LT
176static int __init uid_cache_init(void)
177{
178 int n;
179
180 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
20c2df83 181 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4
LT
182
183 for(n = 0; n < UIDHASH_SZ; ++n)
735de223 184 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
1da177e4
LT
185
186 /* Insert the root user immediately (init already runs as root) */
3fa97c9d 187 spin_lock_irq(&uidhash_lock);
acce292c 188 uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
3fa97c9d 189 spin_unlock_irq(&uidhash_lock);
1da177e4
LT
190
191 return 0;
192}
193
194module_init(uid_cache_init);