4 * (C) Copyright 1991-2000 Linus Torvalds
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.
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>
21 * UID task count cache, to get fast user lookup in "alloc_uid"
22 * when changing user ID's (ie setuid() and friends).
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)))
29 static struct kmem_cache
*uid_cachep
;
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..
40 static DEFINE_SPINLOCK(uidhash_lock
);
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),
50 .uid_keyring
= &root_user_keyring
,
51 .session_keyring
= &root_session_keyring
,
53 #ifdef CONFIG_FAIR_USER_SCHED
58 #ifdef CONFIG_FAIR_USER_SCHED
59 static void sched_destroy_user(struct user_struct
*up
)
61 sched_destroy_group(up
->tg
);
64 static int sched_create_user(struct user_struct
*up
)
68 up
->tg
= sched_create_group();
75 static void sched_switch_user(struct task_struct
*p
)
80 #else /* CONFIG_FAIR_USER_SCHED */
82 static void sched_destroy_user(struct user_struct
*up
) { }
83 static int sched_create_user(struct user_struct
*up
) { return 0; }
84 static void sched_switch_user(struct task_struct
*p
) { }
86 #endif /* CONFIG_FAIR_USER_SCHED */
89 * These routines must be called with the uidhash spinlock held!
91 static inline void uid_hash_insert(struct user_struct
*up
, struct hlist_head
*hashent
)
93 hlist_add_head(&up
->uidhash_node
, hashent
);
96 static inline void uid_hash_remove(struct user_struct
*up
)
98 hlist_del_init(&up
->uidhash_node
);
101 static inline struct user_struct
*uid_hash_find(uid_t uid
, struct hlist_head
*hashent
)
103 struct user_struct
*user
;
104 struct hlist_node
*h
;
106 hlist_for_each_entry(user
, h
, hashent
, uidhash_node
) {
107 if(user
->uid
== uid
) {
108 atomic_inc(&user
->__count
);
117 * Locate the user_struct for the passed UID. If found, take a ref on it. The
118 * caller must undo that ref with free_uid().
120 * If the user_struct could not be found, return NULL.
122 struct user_struct
*find_user(uid_t uid
)
124 struct user_struct
*ret
;
126 struct user_namespace
*ns
= current
->nsproxy
->user_ns
;
128 spin_lock_irqsave(&uidhash_lock
, flags
);
129 ret
= uid_hash_find(uid
, uidhashentry(ns
, uid
));
130 spin_unlock_irqrestore(&uidhash_lock
, flags
);
134 void free_uid(struct user_struct
*up
)
141 local_irq_save(flags
);
142 if (atomic_dec_and_lock(&up
->__count
, &uidhash_lock
)) {
144 spin_unlock_irqrestore(&uidhash_lock
, flags
);
145 sched_destroy_user(up
);
146 key_put(up
->uid_keyring
);
147 key_put(up
->session_keyring
);
148 kmem_cache_free(uid_cachep
, up
);
150 local_irq_restore(flags
);
154 struct user_struct
* alloc_uid(struct user_namespace
*ns
, uid_t uid
)
156 struct hlist_head
*hashent
= uidhashentry(ns
, uid
);
157 struct user_struct
*up
;
159 spin_lock_irq(&uidhash_lock
);
160 up
= uid_hash_find(uid
, hashent
);
161 spin_unlock_irq(&uidhash_lock
);
164 struct user_struct
*new;
166 new = kmem_cache_alloc(uid_cachep
, GFP_KERNEL
);
170 atomic_set(&new->__count
, 1);
171 atomic_set(&new->processes
, 0);
172 atomic_set(&new->files
, 0);
173 atomic_set(&new->sigpending
, 0);
174 #ifdef CONFIG_INOTIFY_USER
175 atomic_set(&new->inotify_watches
, 0);
176 atomic_set(&new->inotify_devs
, 0);
182 if (alloc_uid_keyring(new, current
) < 0) {
183 kmem_cache_free(uid_cachep
, new);
187 if (sched_create_user(new) < 0) {
188 key_put(new->uid_keyring
);
189 key_put(new->session_keyring
);
190 kmem_cache_free(uid_cachep
, new);
195 * Before adding this, check whether we raced
196 * on adding the same user already..
198 spin_lock_irq(&uidhash_lock
);
199 up
= uid_hash_find(uid
, hashent
);
201 sched_destroy_user(new);
202 key_put(new->uid_keyring
);
203 key_put(new->session_keyring
);
204 kmem_cache_free(uid_cachep
, new);
206 uid_hash_insert(new, hashent
);
209 spin_unlock_irq(&uidhash_lock
);
215 void switch_uid(struct user_struct
*new_user
)
217 struct user_struct
*old_user
;
219 /* What if a process setreuid()'s and this brings the
220 * new uid over his NPROC rlimit? We can check this now
221 * cheaply with the new uid cache, so if it matters
222 * we should be checking for it. -DaveM
224 old_user
= current
->user
;
225 atomic_inc(&new_user
->processes
);
226 atomic_dec(&old_user
->processes
);
227 switch_uid_keyring(new_user
);
228 current
->user
= new_user
;
229 sched_switch_user(current
);
232 * We need to synchronize with __sigqueue_alloc()
233 * doing a get_uid(p->user).. If that saw the old
234 * user value, we need to wait until it has exited
235 * its critical region before we can free the old
239 spin_unlock_wait(¤t
->sighand
->siglock
);
245 void release_uids(struct user_namespace
*ns
)
249 struct hlist_head
*head
;
250 struct hlist_node
*nd
;
252 spin_lock_irqsave(&uidhash_lock
, flags
);
254 * collapse the chains so that the user_struct-s will
255 * be still alive, but not in hashes. subsequent free_uid()
258 for (i
= 0; i
< UIDHASH_SZ
; i
++) {
259 head
= ns
->uidhash_table
+ i
;
260 while (!hlist_empty(head
)) {
265 spin_unlock_irqrestore(&uidhash_lock
, flags
);
267 free_uid(ns
->root_user
);
270 static int __init
uid_cache_init(void)
274 uid_cachep
= kmem_cache_create("uid_cache", sizeof(struct user_struct
),
275 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
277 for(n
= 0; n
< UIDHASH_SZ
; ++n
)
278 INIT_HLIST_HEAD(init_user_ns
.uidhash_table
+ n
);
280 /* Insert the root user immediately (init already runs as root) */
281 spin_lock_irq(&uidhash_lock
);
282 uid_hash_insert(&root_user
, uidhashentry(&init_user_ns
, 0));
283 spin_unlock_irq(&uidhash_lock
);
288 module_init(uid_cache_init
);