blob: 2c000e7132acec2201a872e992ca0fc791894528 [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>
David Howellsd84f4f92008-11-14 10:39:23 +110019#include "cred-internals.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Pavel Emelyanovaee16ce2008-02-08 04:18:23 -080021struct user_namespace init_user_ns = {
22 .kref = {
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060023 .refcount = ATOMIC_INIT(2),
Pavel Emelyanovaee16ce2008-02-08 04:18:23 -080024 },
Serge Hallyn18b6e042008-10-15 16:38:45 -050025 .creator = &root_user,
Pavel Emelyanovaee16ce2008-02-08 04:18:23 -080026};
27EXPORT_SYMBOL_GPL(init_user_ns);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define UIDHASH_MASK (UIDHASH_SZ - 1)
35#define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
Cedric Le Goateracce2922007-07-15 23:40:59 -070036#define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Lametere18b8902006-12-06 20:33:20 -080038static struct kmem_cache *uid_cachep;
Ingo Molnar4021cb22006-01-25 15:23:07 +010039
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.
Andrew Morton3fa97c92006-01-31 16:34:26 -080044 * 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..
Ingo Molnar4021cb22006-01-25 15:23:07 +010048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static DEFINE_SPINLOCK(uidhash_lock);
50
Serge Hallyn18b6e042008-10-15 16:38:45 -050051/* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->creator */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct user_struct root_user = {
Serge Hallyn18b6e042008-10-15 16:38:45 -050053 .__count = ATOMIC_INIT(2),
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .processes = ATOMIC_INIT(1),
55 .files = ATOMIC_INIT(0),
56 .sigpending = ATOMIC_INIT(0),
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .locked_shm = 0,
Serge Hallyn18b6e042008-10-15 16:38:45 -050058 .user_ns = &init_user_ns,
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010059#ifdef CONFIG_USER_SCHED
Ingo Molnar4cf86d72007-10-15 17:00:14 +020060 .tg = &init_task_group,
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020061#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Dhaval Giani5cb350b2007-10-15 17:00:14 +020064/*
65 * These routines must be called with the uidhash spinlock held!
66 */
Alexey Dobriyan40aeb402007-10-16 23:30:09 -070067static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
Dhaval Giani5cb350b2007-10-15 17:00:14 +020068{
69 hlist_add_head(&up->uidhash_node, hashent);
70}
71
Alexey Dobriyan40aeb402007-10-16 23:30:09 -070072static void uid_hash_remove(struct user_struct *up)
Dhaval Giani5cb350b2007-10-15 17:00:14 +020073{
74 hlist_del_init(&up->uidhash_node);
Serge E. Hallynfb5ae642009-02-13 14:04:21 +000075 put_user_ns(up->user_ns);
Dhaval Giani5cb350b2007-10-15 17:00:14 +020076}
77
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010078#ifdef CONFIG_USER_SCHED
Dhaval Giani5cb350b2007-10-15 17:00:14 +020079
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020080static void sched_destroy_user(struct user_struct *up)
81{
82 sched_destroy_group(up->tg);
83}
84
85static int sched_create_user(struct user_struct *up)
86{
87 int rc = 0;
88
Peter Zijlstraeff766a2008-04-19 19:45:00 +020089 up->tg = sched_create_group(&root_task_group);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020090 if (IS_ERR(up->tg))
91 rc = -ENOMEM;
92
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +053093 set_tg_uid(up);
94
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020095 return rc;
96}
97
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010098#else /* CONFIG_USER_SCHED */
Dhaval Gianib1a8c172007-10-17 16:55:11 +020099
100static void sched_destroy_user(struct user_struct *up) { }
101static int sched_create_user(struct user_struct *up) { return 0; }
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200102
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100103#endif /* CONFIG_USER_SCHED */
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200104
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100105#if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS)
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200106
Kay Sievers39592142009-03-24 15:43:30 +0100107static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
108{
109 struct user_struct *user;
110 struct hlist_node *h;
111
112 hlist_for_each_entry(user, h, hashent, uidhash_node) {
113 if (user->uid == uid) {
114 /* possibly resurrect an "almost deleted" object */
115 if (atomic_inc_return(&user->__count) == 1)
116 cancel_delayed_work(&user->work);
117 return user;
118 }
119 }
120
121 return NULL;
122}
123
Kay Sieverseb41d942007-11-02 13:47:53 +0100124static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200125static DEFINE_MUTEX(uids_mutex);
126
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200127static inline void uids_mutex_lock(void)
128{
129 mutex_lock(&uids_mutex);
130}
131
132static inline void uids_mutex_unlock(void)
133{
134 mutex_unlock(&uids_mutex);
135}
136
Kay Sieverseb41d942007-11-02 13:47:53 +0100137/* uid directory attributes */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100138#ifdef CONFIG_FAIR_GROUP_SCHED
Kay Sieverseb41d942007-11-02 13:47:53 +0100139static ssize_t cpu_shares_show(struct kobject *kobj,
140 struct kobj_attribute *attr,
141 char *buf)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200142{
Kay Sieverseb41d942007-11-02 13:47:53 +0100143 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200144
Kay Sieverseb41d942007-11-02 13:47:53 +0100145 return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200146}
147
Kay Sieverseb41d942007-11-02 13:47:53 +0100148static ssize_t cpu_shares_store(struct kobject *kobj,
149 struct kobj_attribute *attr,
150 const char *buf, size_t size)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200151{
Kay Sieverseb41d942007-11-02 13:47:53 +0100152 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200153 unsigned long shares;
154 int rc;
155
Kay Sieverseb41d942007-11-02 13:47:53 +0100156 sscanf(buf, "%lu", &shares);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200157
158 rc = sched_group_set_shares(up->tg, shares);
159
160 return (rc ? rc : size);
161}
162
Kay Sieverseb41d942007-11-02 13:47:53 +0100163static struct kobj_attribute cpu_share_attr =
164 __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100165#endif
Kay Sieverseb41d942007-11-02 13:47:53 +0100166
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100167#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100168static ssize_t cpu_rt_runtime_show(struct kobject *kobj,
169 struct kobj_attribute *attr,
170 char *buf)
171{
172 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
173
Peter Zijlstraaf4491e2008-08-19 12:33:02 +0200174 return sprintf(buf, "%ld\n", sched_group_rt_runtime(up->tg));
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100175}
176
177static ssize_t cpu_rt_runtime_store(struct kobject *kobj,
178 struct kobj_attribute *attr,
179 const char *buf, size_t size)
180{
181 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
182 unsigned long rt_runtime;
183 int rc;
184
Peter Zijlstraaf4491e2008-08-19 12:33:02 +0200185 sscanf(buf, "%ld", &rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100186
187 rc = sched_group_set_rt_runtime(up->tg, rt_runtime);
188
189 return (rc ? rc : size);
190}
191
192static struct kobj_attribute cpu_rt_runtime_attr =
193 __ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200194
195static ssize_t cpu_rt_period_show(struct kobject *kobj,
196 struct kobj_attribute *attr,
197 char *buf)
198{
199 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
200
201 return sprintf(buf, "%lu\n", sched_group_rt_period(up->tg));
202}
203
204static ssize_t cpu_rt_period_store(struct kobject *kobj,
205 struct kobj_attribute *attr,
206 const char *buf, size_t size)
207{
208 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
209 unsigned long rt_period;
210 int rc;
211
212 sscanf(buf, "%lu", &rt_period);
213
214 rc = sched_group_set_rt_period(up->tg, rt_period);
215
216 return (rc ? rc : size);
217}
218
219static struct kobj_attribute cpu_rt_period_attr =
220 __ATTR(cpu_rt_period, 0644, cpu_rt_period_show, cpu_rt_period_store);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100221#endif
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100222
Kay Sieverseb41d942007-11-02 13:47:53 +0100223/* default attributes per uid directory */
224static struct attribute *uids_attributes[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100225#ifdef CONFIG_FAIR_GROUP_SCHED
Kay Sieverseb41d942007-11-02 13:47:53 +0100226 &cpu_share_attr.attr,
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100227#endif
228#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100229 &cpu_rt_runtime_attr.attr,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200230 &cpu_rt_period_attr.attr,
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100231#endif
Kay Sieverseb41d942007-11-02 13:47:53 +0100232 NULL
233};
234
235/* the lifetime of user_struct is not managed by the core (now) */
236static void uids_release(struct kobject *kobj)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200237{
Kay Sieverseb41d942007-11-02 13:47:53 +0100238 return;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200239}
240
Kay Sieverseb41d942007-11-02 13:47:53 +0100241static struct kobj_type uids_ktype = {
242 .sysfs_ops = &kobj_sysfs_ops,
243 .default_attrs = uids_attributes,
244 .release = uids_release,
245};
246
Serge E. Hallyn94d6a5f2008-12-08 15:52:21 -0600247/*
248 * Create /sys/kernel/uids/<uid>/cpu_share file for this user
249 * We do not create this file for users in a user namespace (until
250 * sysfs tagging is implemented).
251 *
252 * See Documentation/scheduler/sched-design-CFS.txt for ramifications.
253 */
Kay Sieverseb41d942007-11-02 13:47:53 +0100254static int uids_user_create(struct user_struct *up)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200255{
Kay Sieverseb41d942007-11-02 13:47:53 +0100256 struct kobject *kobj = &up->kobj;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200257 int error;
258
Kay Sieverseb41d942007-11-02 13:47:53 +0100259 memset(kobj, 0, sizeof(struct kobject));
Serge E. Hallync37bbb02008-12-03 13:17:06 -0600260 if (up->user_ns != &init_user_ns)
261 return 0;
Kay Sieverseb41d942007-11-02 13:47:53 +0100262 kobj->kset = uids_kset;
Greg Kroah-Hartmancf151262007-12-17 23:05:35 -0700263 error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
264 if (error) {
265 kobject_put(kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200266 goto done;
Greg Kroah-Hartmancf151262007-12-17 23:05:35 -0700267 }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200268
Srivatsa Vaddagirifb7dde32007-10-15 17:00:18 +0200269 kobject_uevent(kobj, KOBJ_ADD);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200270done:
271 return error;
272}
273
Kay Sieverseb41d942007-11-02 13:47:53 +0100274/* create these entries in sysfs:
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200275 * "/sys/kernel/uids" directory
276 * "/sys/kernel/uids/0" directory (for root user)
277 * "/sys/kernel/uids/0/cpu_share" file (for root user)
278 */
Kay Sieverseb41d942007-11-02 13:47:53 +0100279int __init uids_sysfs_init(void)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200280{
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800281 uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
Kay Sieverseb41d942007-11-02 13:47:53 +0100282 if (!uids_kset)
283 return -ENOMEM;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200284
Kay Sieverseb41d942007-11-02 13:47:53 +0100285 return uids_user_create(&root_user);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200286}
287
Kay Sievers39592142009-03-24 15:43:30 +0100288/* delayed work function to remove sysfs directory for a user and free up
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200289 * corresponding structures.
290 */
Dhaval Gianibe50b8342009-03-10 12:55:56 -0700291static void cleanup_user_struct(struct work_struct *w)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200292{
Kay Sievers39592142009-03-24 15:43:30 +0100293 struct user_struct *up = container_of(w, struct user_struct, work.work);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200294 unsigned long flags;
295 int remove_user = 0;
296
297 /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
298 * atomic.
299 */
300 uids_mutex_lock();
301
Kay Sievers39592142009-03-24 15:43:30 +0100302 spin_lock_irqsave(&uidhash_lock, flags);
303 if (atomic_read(&up->__count) == 0) {
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200304 uid_hash_remove(up);
305 remove_user = 1;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200306 }
Kay Sievers39592142009-03-24 15:43:30 +0100307 spin_unlock_irqrestore(&uidhash_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200308
309 if (!remove_user)
310 goto done;
311
Dhaval Gianibe50b8342009-03-10 12:55:56 -0700312 if (up->user_ns == &init_user_ns) {
313 kobject_uevent(&up->kobj, KOBJ_REMOVE);
314 kobject_del(&up->kobj);
315 kobject_put(&up->kobj);
316 }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200317
318 sched_destroy_user(up);
319 key_put(up->uid_keyring);
320 key_put(up->session_keyring);
321 kmem_cache_free(uid_cachep, up);
322
323done:
324 uids_mutex_unlock();
325}
326
327/* IRQs are disabled and uidhash_lock is held upon function entry.
328 * IRQ state (as stored in flags) is restored and uidhash_lock released
329 * upon function exit.
330 */
Serge Hallyn18b6e042008-10-15 16:38:45 -0500331static void free_user(struct user_struct *up, unsigned long flags)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200332{
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200333 spin_unlock_irqrestore(&uidhash_lock, flags);
Kay Sievers39592142009-03-24 15:43:30 +0100334 INIT_DELAYED_WORK(&up->work, cleanup_user_struct);
335 schedule_delayed_work(&up->work, msecs_to_jiffies(1000));
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200336}
337
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100338#else /* CONFIG_USER_SCHED && CONFIG_SYSFS */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200339
Kay Sievers39592142009-03-24 15:43:30 +0100340static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
341{
342 struct user_struct *user;
343 struct hlist_node *h;
344
345 hlist_for_each_entry(user, h, hashent, uidhash_node) {
346 if (user->uid == uid) {
347 atomic_inc(&user->__count);
348 return user;
349 }
350 }
351
352 return NULL;
353}
354
Kay Sieverseb41d942007-11-02 13:47:53 +0100355int uids_sysfs_init(void) { return 0; }
356static inline int uids_user_create(struct user_struct *up) { return 0; }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200357static inline void uids_mutex_lock(void) { }
358static inline void uids_mutex_unlock(void) { }
359
360/* IRQs are disabled and uidhash_lock is held upon function entry.
361 * IRQ state (as stored in flags) is restored and uidhash_lock released
362 * upon function exit.
363 */
Serge Hallyn18b6e042008-10-15 16:38:45 -0500364static void free_user(struct user_struct *up, unsigned long flags)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200365{
366 uid_hash_remove(up);
367 spin_unlock_irqrestore(&uidhash_lock, flags);
368 sched_destroy_user(up);
369 key_put(up->uid_keyring);
370 key_put(up->session_keyring);
371 kmem_cache_free(uid_cachep, up);
372}
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200373
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200374#endif
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200375
Dhaval Giani54e99122009-02-27 15:13:54 +0530376#if defined(CONFIG_RT_GROUP_SCHED) && defined(CONFIG_USER_SCHED)
377/*
378 * We need to check if a setuid can take place. This function should be called
379 * before successfully completing the setuid.
380 */
381int task_can_switch_user(struct user_struct *up, struct task_struct *tsk)
382{
383
384 return sched_rt_can_attach(up->tg, tsk);
385
386}
387#else
388int task_can_switch_user(struct user_struct *up, struct task_struct *tsk)
389{
390 return 1;
391}
392#endif
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * Locate the user_struct for the passed UID. If found, take a ref on it. The
396 * caller must undo that ref with free_uid().
397 *
398 * If the user_struct could not be found, return NULL.
399 */
400struct user_struct *find_user(uid_t uid)
401{
402 struct user_struct *ret;
Andrew Morton3fa97c92006-01-31 16:34:26 -0800403 unsigned long flags;
Serge Hallyn6ded6ab2008-11-24 16:24:10 -0500404 struct user_namespace *ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Andrew Morton3fa97c92006-01-31 16:34:26 -0800406 spin_lock_irqsave(&uidhash_lock, flags);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700407 ret = uid_hash_find(uid, uidhashentry(ns, uid));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800408 spin_unlock_irqrestore(&uidhash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return ret;
410}
411
412void free_uid(struct user_struct *up)
413{
Andrew Morton3fa97c92006-01-31 16:34:26 -0800414 unsigned long flags;
415
Andrew Morton36f57412006-03-24 03:15:47 -0800416 if (!up)
417 return;
418
Andrew Morton3fa97c92006-01-31 16:34:26 -0800419 local_irq_save(flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200420 if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
421 free_user(up, flags);
422 else
Andrew Morton36f57412006-03-24 03:15:47 -0800423 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Andrew Morton354a1f42008-04-30 00:54:54 -0700426struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Pavel Emelyanov735de222007-09-18 22:46:44 -0700428 struct hlist_head *hashent = uidhashentry(ns, uid);
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100429 struct user_struct *up, *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Kay Sieverseb41d942007-11-02 13:47:53 +0100431 /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200432 * atomic.
433 */
434 uids_mutex_lock();
435
Andrew Morton3fa97c92006-01-31 16:34:26 -0800436 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 up = uid_hash_find(uid, hashent);
Andrew Morton3fa97c92006-01-31 16:34:26 -0800438 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!up) {
Andrew Morton354a1f42008-04-30 00:54:54 -0700441 new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100442 if (!new)
443 goto out_unlock;
Pavel Emelyanov5e8869b2007-11-26 21:21:49 +0100444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 new->uid = uid;
446 atomic_set(&new->__count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100448 if (sched_create_user(new) < 0)
David Howells69664cf2008-04-29 01:01:31 -0700449 goto out_free_user;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200450
Serge Hallyn18b6e042008-10-15 16:38:45 -0500451 new->user_ns = get_user_ns(ns);
452
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100453 if (uids_user_create(new))
454 goto out_destoy_sched;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * Before adding this, check whether we raced
458 * on adding the same user already..
459 */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800460 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 up = uid_hash_find(uid, hashent);
462 if (up) {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100463 /* This case is not possible when CONFIG_USER_SCHED
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200464 * is defined, since we serialize alloc_uid() using
465 * uids_mutex. Hence no need to call
466 * sched_destroy_user() or remove_user_sysfs_dir().
467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 key_put(new->uid_keyring);
469 key_put(new->session_keyring);
470 kmem_cache_free(uid_cachep, new);
471 } else {
472 uid_hash_insert(new, hashent);
473 up = new;
474 }
Andrew Morton3fa97c92006-01-31 16:34:26 -0800475 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200477
478 uids_mutex_unlock();
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return up;
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100481
482out_destoy_sched:
483 sched_destroy_user(new);
Serge Hallyn18b6e042008-10-15 16:38:45 -0500484 put_user_ns(new->user_ns);
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100485out_free_user:
486 kmem_cache_free(uid_cachep, new);
487out_unlock:
488 uids_mutex_unlock();
489 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static int __init uid_cache_init(void)
493{
494 int n;
495
496 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +0900497 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 for(n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -0700500 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* Insert the root user immediately (init already runs as root) */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800503 spin_lock_irq(&uidhash_lock);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700504 uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800505 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 return 0;
508}
509
510module_init(uid_cache_init);