Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ingo Molnar | 4021cb2 | 2006-01-25 15:23:07 +0100 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/user_namespace.h> |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 19 | #include "cred-internals.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Pavel Emelyanov | aee16ce | 2008-02-08 04:18:23 -0800 | [diff] [blame] | 21 | struct user_namespace init_user_ns = { |
| 22 | .kref = { |
| 23 | .refcount = ATOMIC_INIT(2), |
| 24 | }, |
| 25 | .root_user = &root_user, |
| 26 | }; |
| 27 | EXPORT_SYMBOL_GPL(init_user_ns); |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define UIDHASH_MASK (UIDHASH_SZ - 1) |
| 35 | #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK) |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 36 | #define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 38 | static struct kmem_cache *uid_cachep; |
Ingo Molnar | 4021cb2 | 2006-01-25 15:23:07 +0100 | [diff] [blame] | 39 | |
| 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 Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 44 | * 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 Molnar | 4021cb2 | 2006-01-25 15:23:07 +0100 | [diff] [blame] | 48 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static DEFINE_SPINLOCK(uidhash_lock); |
| 50 | |
| 51 | struct user_struct root_user = { |
| 52 | .__count = ATOMIC_INIT(1), |
| 53 | .processes = ATOMIC_INIT(1), |
| 54 | .files = ATOMIC_INIT(0), |
| 55 | .sigpending = ATOMIC_INIT(0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | .locked_shm = 0, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_USER_SCHED |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 58 | .tg = &init_task_group, |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 59 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 62 | /* |
| 63 | * These routines must be called with the uidhash spinlock held! |
| 64 | */ |
Alexey Dobriyan | 40aeb40 | 2007-10-16 23:30:09 -0700 | [diff] [blame] | 65 | static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 66 | { |
| 67 | hlist_add_head(&up->uidhash_node, hashent); |
| 68 | } |
| 69 | |
Alexey Dobriyan | 40aeb40 | 2007-10-16 23:30:09 -0700 | [diff] [blame] | 70 | static void uid_hash_remove(struct user_struct *up) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 71 | { |
| 72 | hlist_del_init(&up->uidhash_node); |
| 73 | } |
| 74 | |
Alexey Dobriyan | 40aeb40 | 2007-10-16 23:30:09 -0700 | [diff] [blame] | 75 | static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 76 | { |
| 77 | struct user_struct *user; |
| 78 | struct hlist_node *h; |
| 79 | |
| 80 | hlist_for_each_entry(user, h, hashent, uidhash_node) { |
| 81 | if (user->uid == uid) { |
| 82 | atomic_inc(&user->__count); |
| 83 | return user; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 90 | #ifdef CONFIG_USER_SCHED |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 91 | |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 92 | static void sched_destroy_user(struct user_struct *up) |
| 93 | { |
| 94 | sched_destroy_group(up->tg); |
| 95 | } |
| 96 | |
| 97 | static int sched_create_user(struct user_struct *up) |
| 98 | { |
| 99 | int rc = 0; |
| 100 | |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 101 | up->tg = sched_create_group(&root_task_group); |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 102 | if (IS_ERR(up->tg)) |
| 103 | rc = -ENOMEM; |
| 104 | |
| 105 | return rc; |
| 106 | } |
| 107 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 108 | #else /* CONFIG_USER_SCHED */ |
Dhaval Giani | b1a8c17 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 109 | |
| 110 | static void sched_destroy_user(struct user_struct *up) { } |
| 111 | static int sched_create_user(struct user_struct *up) { return 0; } |
Dhaval Giani | b1a8c17 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 112 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 113 | #endif /* CONFIG_USER_SCHED */ |
Dhaval Giani | b1a8c17 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 114 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 115 | #if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS) |
Dhaval Giani | b1a8c17 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 116 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 117 | static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */ |
Dhaval Giani | b1a8c17 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 118 | static DEFINE_MUTEX(uids_mutex); |
| 119 | |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 120 | static inline void uids_mutex_lock(void) |
| 121 | { |
| 122 | mutex_lock(&uids_mutex); |
| 123 | } |
| 124 | |
| 125 | static inline void uids_mutex_unlock(void) |
| 126 | { |
| 127 | mutex_unlock(&uids_mutex); |
| 128 | } |
| 129 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 130 | /* uid directory attributes */ |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 131 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 132 | static ssize_t cpu_shares_show(struct kobject *kobj, |
| 133 | struct kobj_attribute *attr, |
| 134 | char *buf) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 135 | { |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 136 | struct user_struct *up = container_of(kobj, struct user_struct, kobj); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 137 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 138 | return sprintf(buf, "%lu\n", sched_group_shares(up->tg)); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 141 | static ssize_t cpu_shares_store(struct kobject *kobj, |
| 142 | struct kobj_attribute *attr, |
| 143 | const char *buf, size_t size) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 144 | { |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 145 | struct user_struct *up = container_of(kobj, struct user_struct, kobj); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 146 | unsigned long shares; |
| 147 | int rc; |
| 148 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 149 | sscanf(buf, "%lu", &shares); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 150 | |
| 151 | rc = sched_group_set_shares(up->tg, shares); |
| 152 | |
| 153 | return (rc ? rc : size); |
| 154 | } |
| 155 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 156 | static struct kobj_attribute cpu_share_attr = |
| 157 | __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store); |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 158 | #endif |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 159 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 160 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 161 | static ssize_t cpu_rt_runtime_show(struct kobject *kobj, |
| 162 | struct kobj_attribute *attr, |
| 163 | char *buf) |
| 164 | { |
| 165 | struct user_struct *up = container_of(kobj, struct user_struct, kobj); |
| 166 | |
Peter Zijlstra | af4491e | 2008-08-19 12:33:02 +0200 | [diff] [blame] | 167 | return sprintf(buf, "%ld\n", sched_group_rt_runtime(up->tg)); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static ssize_t cpu_rt_runtime_store(struct kobject *kobj, |
| 171 | struct kobj_attribute *attr, |
| 172 | const char *buf, size_t size) |
| 173 | { |
| 174 | struct user_struct *up = container_of(kobj, struct user_struct, kobj); |
| 175 | unsigned long rt_runtime; |
| 176 | int rc; |
| 177 | |
Peter Zijlstra | af4491e | 2008-08-19 12:33:02 +0200 | [diff] [blame] | 178 | sscanf(buf, "%ld", &rt_runtime); |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 179 | |
| 180 | rc = sched_group_set_rt_runtime(up->tg, rt_runtime); |
| 181 | |
| 182 | return (rc ? rc : size); |
| 183 | } |
| 184 | |
| 185 | static struct kobj_attribute cpu_rt_runtime_attr = |
| 186 | __ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 187 | |
| 188 | static ssize_t cpu_rt_period_show(struct kobject *kobj, |
| 189 | struct kobj_attribute *attr, |
| 190 | char *buf) |
| 191 | { |
| 192 | struct user_struct *up = container_of(kobj, struct user_struct, kobj); |
| 193 | |
| 194 | return sprintf(buf, "%lu\n", sched_group_rt_period(up->tg)); |
| 195 | } |
| 196 | |
| 197 | static ssize_t cpu_rt_period_store(struct kobject *kobj, |
| 198 | struct kobj_attribute *attr, |
| 199 | const char *buf, size_t size) |
| 200 | { |
| 201 | struct user_struct *up = container_of(kobj, struct user_struct, kobj); |
| 202 | unsigned long rt_period; |
| 203 | int rc; |
| 204 | |
| 205 | sscanf(buf, "%lu", &rt_period); |
| 206 | |
| 207 | rc = sched_group_set_rt_period(up->tg, rt_period); |
| 208 | |
| 209 | return (rc ? rc : size); |
| 210 | } |
| 211 | |
| 212 | static struct kobj_attribute cpu_rt_period_attr = |
| 213 | __ATTR(cpu_rt_period, 0644, cpu_rt_period_show, cpu_rt_period_store); |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 214 | #endif |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 215 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 216 | /* default attributes per uid directory */ |
| 217 | static struct attribute *uids_attributes[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 218 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 219 | &cpu_share_attr.attr, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 220 | #endif |
| 221 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 222 | &cpu_rt_runtime_attr.attr, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 223 | &cpu_rt_period_attr.attr, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 224 | #endif |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 225 | NULL |
| 226 | }; |
| 227 | |
| 228 | /* the lifetime of user_struct is not managed by the core (now) */ |
| 229 | static void uids_release(struct kobject *kobj) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 230 | { |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 231 | return; |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 234 | static struct kobj_type uids_ktype = { |
| 235 | .sysfs_ops = &kobj_sysfs_ops, |
| 236 | .default_attrs = uids_attributes, |
| 237 | .release = uids_release, |
| 238 | }; |
| 239 | |
| 240 | /* create /sys/kernel/uids/<uid>/cpu_share file for this user */ |
| 241 | static int uids_user_create(struct user_struct *up) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 242 | { |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 243 | struct kobject *kobj = &up->kobj; |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 244 | int error; |
| 245 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 246 | memset(kobj, 0, sizeof(struct kobject)); |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 247 | kobj->kset = uids_kset; |
Greg Kroah-Hartman | cf15126 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 248 | error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid); |
| 249 | if (error) { |
| 250 | kobject_put(kobj); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 251 | goto done; |
Greg Kroah-Hartman | cf15126 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 252 | } |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 253 | |
Srivatsa Vaddagiri | fb7dde3 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 254 | kobject_uevent(kobj, KOBJ_ADD); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 255 | done: |
| 256 | return error; |
| 257 | } |
| 258 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 259 | /* create these entries in sysfs: |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 260 | * "/sys/kernel/uids" directory |
| 261 | * "/sys/kernel/uids/0" directory (for root user) |
| 262 | * "/sys/kernel/uids/0/cpu_share" file (for root user) |
| 263 | */ |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 264 | int __init uids_sysfs_init(void) |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 265 | { |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 266 | uids_kset = kset_create_and_add("uids", NULL, kernel_kobj); |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 267 | if (!uids_kset) |
| 268 | return -ENOMEM; |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 269 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 270 | return uids_user_create(&root_user); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* work function to remove sysfs directory for a user and free up |
| 274 | * corresponding structures. |
| 275 | */ |
| 276 | static void remove_user_sysfs_dir(struct work_struct *w) |
| 277 | { |
| 278 | struct user_struct *up = container_of(w, struct user_struct, work); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 279 | unsigned long flags; |
| 280 | int remove_user = 0; |
| 281 | |
| 282 | /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del() |
| 283 | * atomic. |
| 284 | */ |
| 285 | uids_mutex_lock(); |
| 286 | |
| 287 | local_irq_save(flags); |
| 288 | |
| 289 | if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) { |
| 290 | uid_hash_remove(up); |
| 291 | remove_user = 1; |
| 292 | spin_unlock_irqrestore(&uidhash_lock, flags); |
| 293 | } else { |
| 294 | local_irq_restore(flags); |
| 295 | } |
| 296 | |
| 297 | if (!remove_user) |
| 298 | goto done; |
| 299 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 300 | kobject_uevent(&up->kobj, KOBJ_REMOVE); |
| 301 | kobject_del(&up->kobj); |
| 302 | kobject_put(&up->kobj); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 303 | |
| 304 | sched_destroy_user(up); |
| 305 | key_put(up->uid_keyring); |
| 306 | key_put(up->session_keyring); |
| 307 | kmem_cache_free(uid_cachep, up); |
| 308 | |
| 309 | done: |
| 310 | uids_mutex_unlock(); |
| 311 | } |
| 312 | |
| 313 | /* IRQs are disabled and uidhash_lock is held upon function entry. |
| 314 | * IRQ state (as stored in flags) is restored and uidhash_lock released |
| 315 | * upon function exit. |
| 316 | */ |
| 317 | static inline void free_user(struct user_struct *up, unsigned long flags) |
| 318 | { |
| 319 | /* restore back the count */ |
| 320 | atomic_inc(&up->__count); |
| 321 | spin_unlock_irqrestore(&uidhash_lock, flags); |
| 322 | |
| 323 | INIT_WORK(&up->work, remove_user_sysfs_dir); |
| 324 | schedule_work(&up->work); |
| 325 | } |
| 326 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 327 | #else /* CONFIG_USER_SCHED && CONFIG_SYSFS */ |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 328 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 329 | int uids_sysfs_init(void) { return 0; } |
| 330 | static inline int uids_user_create(struct user_struct *up) { return 0; } |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 331 | static inline void uids_mutex_lock(void) { } |
| 332 | static inline void uids_mutex_unlock(void) { } |
| 333 | |
| 334 | /* IRQs are disabled and uidhash_lock is held upon function entry. |
| 335 | * IRQ state (as stored in flags) is restored and uidhash_lock released |
| 336 | * upon function exit. |
| 337 | */ |
| 338 | static inline void free_user(struct user_struct *up, unsigned long flags) |
| 339 | { |
| 340 | uid_hash_remove(up); |
| 341 | spin_unlock_irqrestore(&uidhash_lock, flags); |
| 342 | sched_destroy_user(up); |
| 343 | key_put(up->uid_keyring); |
| 344 | key_put(up->session_keyring); |
| 345 | kmem_cache_free(uid_cachep, up); |
| 346 | } |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 347 | |
Dhaval Giani | b1a8c17 | 2007-10-17 16:55:11 +0200 | [diff] [blame] | 348 | #endif |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | * Locate the user_struct for the passed UID. If found, take a ref on it. The |
| 352 | * caller must undo that ref with free_uid(). |
| 353 | * |
| 354 | * If the user_struct could not be found, return NULL. |
| 355 | */ |
| 356 | struct user_struct *find_user(uid_t uid) |
| 357 | { |
| 358 | struct user_struct *ret; |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 359 | unsigned long flags; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 360 | struct user_namespace *ns = current->nsproxy->user_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 362 | spin_lock_irqsave(&uidhash_lock, flags); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 363 | ret = uid_hash_find(uid, uidhashentry(ns, uid)); |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 364 | spin_unlock_irqrestore(&uidhash_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | void free_uid(struct user_struct *up) |
| 369 | { |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 370 | unsigned long flags; |
| 371 | |
Andrew Morton | 36f5741 | 2006-03-24 03:15:47 -0800 | [diff] [blame] | 372 | if (!up) |
| 373 | return; |
| 374 | |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 375 | local_irq_save(flags); |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 376 | if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) |
| 377 | free_user(up, flags); |
| 378 | else |
Andrew Morton | 36f5741 | 2006-03-24 03:15:47 -0800 | [diff] [blame] | 379 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Andrew Morton | 354a1f4 | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 382 | struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Pavel Emelyanov | 735de22 | 2007-09-18 22:46:44 -0700 | [diff] [blame] | 384 | struct hlist_head *hashent = uidhashentry(ns, uid); |
Pavel Emelyanov | 8eb703e | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 385 | struct user_struct *up, *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 387 | /* Make uid_hash_find() + uids_user_create() + uid_hash_insert() |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 388 | * atomic. |
| 389 | */ |
| 390 | uids_mutex_lock(); |
| 391 | |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 392 | spin_lock_irq(&uidhash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | up = uid_hash_find(uid, hashent); |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 394 | spin_unlock_irq(&uidhash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | if (!up) { |
Andrew Morton | 354a1f4 | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 397 | new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL); |
Pavel Emelyanov | 8eb703e | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 398 | if (!new) |
| 399 | goto out_unlock; |
Pavel Emelyanov | 5e8869b | 2007-11-26 21:21:49 +0100 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | new->uid = uid; |
| 402 | atomic_set(&new->__count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Pavel Emelyanov | 8eb703e | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 404 | if (sched_create_user(new) < 0) |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 405 | goto out_free_user; |
Srivatsa Vaddagiri | 24e377a | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 406 | |
Pavel Emelyanov | 8eb703e | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 407 | if (uids_user_create(new)) |
| 408 | goto out_destoy_sched; |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* |
| 411 | * Before adding this, check whether we raced |
| 412 | * on adding the same user already.. |
| 413 | */ |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 414 | spin_lock_irq(&uidhash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | up = uid_hash_find(uid, hashent); |
| 416 | if (up) { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 417 | /* This case is not possible when CONFIG_USER_SCHED |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 418 | * is defined, since we serialize alloc_uid() using |
| 419 | * uids_mutex. Hence no need to call |
| 420 | * sched_destroy_user() or remove_user_sysfs_dir(). |
| 421 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | key_put(new->uid_keyring); |
| 423 | key_put(new->session_keyring); |
| 424 | kmem_cache_free(uid_cachep, new); |
| 425 | } else { |
| 426 | uid_hash_insert(new, hashent); |
| 427 | up = new; |
| 428 | } |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 429 | spin_unlock_irq(&uidhash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | } |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 432 | |
| 433 | uids_mutex_unlock(); |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return up; |
Pavel Emelyanov | 8eb703e | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 436 | |
| 437 | out_destoy_sched: |
| 438 | sched_destroy_user(new); |
Pavel Emelyanov | 8eb703e | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 439 | out_free_user: |
| 440 | kmem_cache_free(uid_cachep, new); |
| 441 | out_unlock: |
| 442 | uids_mutex_unlock(); |
| 443 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Pavel Emelyanov | aee16ce | 2008-02-08 04:18:23 -0800 | [diff] [blame] | 446 | #ifdef CONFIG_USER_NS |
Pavel Emelyanov | 28f300d | 2007-09-18 22:46:45 -0700 | [diff] [blame] | 447 | void release_uids(struct user_namespace *ns) |
| 448 | { |
| 449 | int i; |
| 450 | unsigned long flags; |
| 451 | struct hlist_head *head; |
| 452 | struct hlist_node *nd; |
| 453 | |
| 454 | spin_lock_irqsave(&uidhash_lock, flags); |
| 455 | /* |
| 456 | * collapse the chains so that the user_struct-s will |
| 457 | * be still alive, but not in hashes. subsequent free_uid() |
| 458 | * will free them. |
| 459 | */ |
| 460 | for (i = 0; i < UIDHASH_SZ; i++) { |
| 461 | head = ns->uidhash_table + i; |
| 462 | while (!hlist_empty(head)) { |
| 463 | nd = head->first; |
| 464 | hlist_del_init(nd); |
| 465 | } |
| 466 | } |
| 467 | spin_unlock_irqrestore(&uidhash_lock, flags); |
| 468 | |
| 469 | free_uid(ns->root_user); |
| 470 | } |
Pavel Emelyanov | aee16ce | 2008-02-08 04:18:23 -0800 | [diff] [blame] | 471 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | static int __init uid_cache_init(void) |
| 474 | { |
| 475 | int n; |
| 476 | |
| 477 | uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 478 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | for(n = 0; n < UIDHASH_SZ; ++n) |
Pavel Emelyanov | 735de22 | 2007-09-18 22:46:44 -0700 | [diff] [blame] | 481 | INIT_HLIST_HEAD(init_user_ns.uidhash_table + n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
| 483 | /* Insert the root user immediately (init already runs as root) */ |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 484 | spin_lock_irq(&uidhash_lock); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 485 | uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0)); |
Andrew Morton | 3fa97c9 | 2006-01-31 16:34:26 -0800 | [diff] [blame] | 486 | spin_unlock_irq(&uidhash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | module_init(uid_cache_init); |