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