blob: c6387fac932d0021436742d39c9dbf147133d772 [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
54 .tg = &init_task_grp,
55#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020058#ifdef CONFIG_FAIR_USER_SCHED
59static void sched_destroy_user(struct user_struct *up)
60{
61 sched_destroy_group(up->tg);
62}
63
64static int sched_create_user(struct user_struct *up)
65{
66 int rc = 0;
67
68 up->tg = sched_create_group();
69 if (IS_ERR(up->tg))
70 rc = -ENOMEM;
71
72 return rc;
73}
74
75static void sched_switch_user(struct task_struct *p)
76{
77 sched_move_task(p);
78}
79
80#else /* CONFIG_FAIR_USER_SCHED */
81
82static void sched_destroy_user(struct user_struct *up) { }
83static int sched_create_user(struct user_struct *up) { return 0; }
84static void sched_switch_user(struct task_struct *p) { }
85
86#endif /* CONFIG_FAIR_USER_SCHED */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * These routines must be called with the uidhash spinlock held!
90 */
Pavel Emelyanov735de222007-09-18 22:46:44 -070091static inline void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Pavel Emelyanov735de222007-09-18 22:46:44 -070093 hlist_add_head(&up->uidhash_node, hashent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static inline void uid_hash_remove(struct user_struct *up)
97{
Pavel Emelyanov28f300d2007-09-18 22:46:45 -070098 hlist_del_init(&up->uidhash_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Pavel Emelyanov735de222007-09-18 22:46:44 -0700101static inline struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Matthias Kaehlcked8a48212007-09-18 22:46:43 -0700103 struct user_struct *user;
Pavel Emelyanov735de222007-09-18 22:46:44 -0700104 struct hlist_node *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Pavel Emelyanov735de222007-09-18 22:46:44 -0700106 hlist_for_each_entry(user, h, hashent, uidhash_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if(user->uid == uid) {
108 atomic_inc(&user->__count);
109 return user;
110 }
111 }
112
113 return NULL;
114}
115
116/*
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().
119 *
120 * If the user_struct could not be found, return NULL.
121 */
122struct user_struct *find_user(uid_t uid)
123{
124 struct user_struct *ret;
Andrew Morton3fa97c92006-01-31 16:34:26 -0800125 unsigned long flags;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700126 struct user_namespace *ns = current->nsproxy->user_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Andrew Morton3fa97c92006-01-31 16:34:26 -0800128 spin_lock_irqsave(&uidhash_lock, flags);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700129 ret = uid_hash_find(uid, uidhashentry(ns, uid));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800130 spin_unlock_irqrestore(&uidhash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return ret;
132}
133
134void free_uid(struct user_struct *up)
135{
Andrew Morton3fa97c92006-01-31 16:34:26 -0800136 unsigned long flags;
137
Andrew Morton36f57412006-03-24 03:15:47 -0800138 if (!up)
139 return;
140
Andrew Morton3fa97c92006-01-31 16:34:26 -0800141 local_irq_save(flags);
Andrew Morton36f57412006-03-24 03:15:47 -0800142 if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 uid_hash_remove(up);
Andrew Morton36f57412006-03-24 03:15:47 -0800144 spin_unlock_irqrestore(&uidhash_lock, flags);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200145 sched_destroy_user(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 key_put(up->uid_keyring);
147 key_put(up->session_keyring);
148 kmem_cache_free(uid_cachep, up);
Andrew Morton36f57412006-03-24 03:15:47 -0800149 } else {
150 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152}
153
Cedric Le Goateracce2922007-07-15 23:40:59 -0700154struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Pavel Emelyanov735de222007-09-18 22:46:44 -0700156 struct hlist_head *hashent = uidhashentry(ns, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct user_struct *up;
158
Andrew Morton3fa97c92006-01-31 16:34:26 -0800159 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 up = uid_hash_find(uid, hashent);
Andrew Morton3fa97c92006-01-31 16:34:26 -0800161 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (!up) {
164 struct user_struct *new;
165
Christoph Lametere94b1762006-12-06 20:33:17 -0800166 new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!new)
168 return NULL;
169 new->uid = uid;
170 atomic_set(&new->__count, 1);
171 atomic_set(&new->processes, 0);
172 atomic_set(&new->files, 0);
173 atomic_set(&new->sigpending, 0);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700174#ifdef CONFIG_INOTIFY_USER
Robert Love0eeca282005-07-12 17:06:03 -0400175 atomic_set(&new->inotify_watches, 0);
176 atomic_set(&new->inotify_devs, 0);
177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 new->mq_bytes = 0;
180 new->locked_shm = 0;
181
Michael LeMayd7200242006-06-22 14:47:17 -0700182 if (alloc_uid_keyring(new, current) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kmem_cache_free(uid_cachep, new);
184 return NULL;
185 }
186
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200187 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);
191 return NULL;
192 }
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
195 * Before adding this, check whether we raced
196 * on adding the same user already..
197 */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800198 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 up = uid_hash_find(uid, hashent);
200 if (up) {
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200201 sched_destroy_user(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 key_put(new->uid_keyring);
203 key_put(new->session_keyring);
204 kmem_cache_free(uid_cachep, new);
205 } else {
206 uid_hash_insert(new, hashent);
207 up = new;
208 }
Andrew Morton3fa97c92006-01-31 16:34:26 -0800209 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 }
212 return up;
213}
214
215void switch_uid(struct user_struct *new_user)
216{
217 struct user_struct *old_user;
218
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
223 */
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;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200229 sched_switch_user(current);
Linus Torvalds45c18b02006-11-04 10:06:02 -0800230
231 /*
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
236 * structure.
237 */
238 smp_mb();
239 spin_unlock_wait(&current->sighand->siglock);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 free_uid(old_user);
242 suid_keys(current);
243}
244
Pavel Emelyanov28f300d2007-09-18 22:46:45 -0700245void release_uids(struct user_namespace *ns)
246{
247 int i;
248 unsigned long flags;
249 struct hlist_head *head;
250 struct hlist_node *nd;
251
252 spin_lock_irqsave(&uidhash_lock, flags);
253 /*
254 * collapse the chains so that the user_struct-s will
255 * be still alive, but not in hashes. subsequent free_uid()
256 * will free them.
257 */
258 for (i = 0; i < UIDHASH_SZ; i++) {
259 head = ns->uidhash_table + i;
260 while (!hlist_empty(head)) {
261 nd = head->first;
262 hlist_del_init(nd);
263 }
264 }
265 spin_unlock_irqrestore(&uidhash_lock, flags);
266
267 free_uid(ns->root_user);
268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270static int __init uid_cache_init(void)
271{
272 int n;
273
274 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +0900275 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 for(n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -0700278 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* Insert the root user immediately (init already runs as root) */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800281 spin_lock_irq(&uidhash_lock);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700282 uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800283 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return 0;
286}
287
288module_init(uid_cache_init);