Blame view
kernel/user.c
5.09 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 = { |
1d1e97562
|
23 |
.refcount = ATOMIC_INIT(2), |
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, |
1da177e4c
|
56 |
}; |
5cb350baf
|
57 58 59 |
/* * These routines must be called with the uidhash spinlock held! */ |
40aeb400f
|
60 |
static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent) |
5cb350baf
|
61 62 63 |
{ hlist_add_head(&up->uidhash_node, hashent); } |
40aeb400f
|
64 |
static void uid_hash_remove(struct user_struct *up) |
5cb350baf
|
65 66 |
{ hlist_del_init(&up->uidhash_node); |
fb5ae64fd
|
67 |
put_user_ns(up->user_ns); |
5cb350baf
|
68 |
} |
3959214f9
|
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent) { 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; } |
5cb350baf
|
83 84 85 86 |
/* 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
|
87 |
static void free_user(struct user_struct *up, unsigned long flags) |
5cb350baf
|
88 89 90 |
{ uid_hash_remove(up); spin_unlock_irqrestore(&uidhash_lock, flags); |
5cb350baf
|
91 92 93 94 |
key_put(up->uid_keyring); key_put(up->session_keyring); kmem_cache_free(uid_cachep, up); } |
1da177e4c
|
95 96 97 98 99 100 101 102 103 |
/* * 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
|
104 |
unsigned long flags; |
6ded6ab9b
|
105 |
struct user_namespace *ns = current_user_ns(); |
1da177e4c
|
106 |
|
3fa97c9db
|
107 |
spin_lock_irqsave(&uidhash_lock, flags); |
acce292c8
|
108 |
ret = uid_hash_find(uid, uidhashentry(ns, uid)); |
3fa97c9db
|
109 |
spin_unlock_irqrestore(&uidhash_lock, flags); |
1da177e4c
|
110 111 112 113 114 |
return ret; } void free_uid(struct user_struct *up) { |
3fa97c9db
|
115 |
unsigned long flags; |
36f574135
|
116 117 |
if (!up) return; |
3fa97c9db
|
118 |
local_irq_save(flags); |
5cb350baf
|
119 120 121 |
if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) free_user(up, flags); else |
36f574135
|
122 |
local_irq_restore(flags); |
1da177e4c
|
123 |
} |
354a1f4d9
|
124 |
struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid) |
1da177e4c
|
125 |
{ |
735de2230
|
126 |
struct hlist_head *hashent = uidhashentry(ns, uid); |
8eb703e4f
|
127 |
struct user_struct *up, *new; |
1da177e4c
|
128 |
|
eb41d9465
|
129 |
/* Make uid_hash_find() + uids_user_create() + uid_hash_insert() |
5cb350baf
|
130 131 |
* atomic. */ |
3fa97c9db
|
132 |
spin_lock_irq(&uidhash_lock); |
1da177e4c
|
133 |
up = uid_hash_find(uid, hashent); |
3fa97c9db
|
134 |
spin_unlock_irq(&uidhash_lock); |
1da177e4c
|
135 136 |
if (!up) { |
354a1f4d9
|
137 |
new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL); |
8eb703e4f
|
138 139 |
if (!new) goto out_unlock; |
5e8869bb6
|
140 |
|
1da177e4c
|
141 142 |
new->uid = uid; atomic_set(&new->__count, 1); |
1da177e4c
|
143 |
|
18b6e0414
|
144 |
new->user_ns = get_user_ns(ns); |
1da177e4c
|
145 146 147 148 |
/* * Before adding this, check whether we raced * on adding the same user already.. */ |
3fa97c9db
|
149 |
spin_lock_irq(&uidhash_lock); |
1da177e4c
|
150 151 |
up = uid_hash_find(uid, hashent); if (up) { |
052f1dc7e
|
152 |
/* This case is not possible when CONFIG_USER_SCHED |
5cb350baf
|
153 154 155 156 |
* is defined, since we serialize alloc_uid() using * uids_mutex. Hence no need to call * sched_destroy_user() or remove_user_sysfs_dir(). */ |
1da177e4c
|
157 158 159 160 161 162 163 |
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
|
164 |
spin_unlock_irq(&uidhash_lock); |
1da177e4c
|
165 |
} |
5cb350baf
|
166 |
|
1da177e4c
|
167 |
return up; |
8eb703e4f
|
168 |
|
18b6e0414
|
169 |
put_user_ns(new->user_ns); |
8eb703e4f
|
170 171 |
kmem_cache_free(uid_cachep, new); out_unlock: |
8eb703e4f
|
172 |
return NULL; |
1da177e4c
|
173 |
} |
1da177e4c
|
174 175 176 177 178 |
static int __init uid_cache_init(void) { int n; uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct), |
20c2df83d
|
179 |
0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
1da177e4c
|
180 181 |
for(n = 0; n < UIDHASH_SZ; ++n) |
735de2230
|
182 |
INIT_HLIST_HEAD(init_user_ns.uidhash_table + n); |
1da177e4c
|
183 184 |
/* Insert the root user immediately (init already runs as root) */ |
3fa97c9db
|
185 |
spin_lock_irq(&uidhash_lock); |
acce292c8
|
186 |
uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0)); |
3fa97c9db
|
187 |
spin_unlock_irq(&uidhash_lock); |
1da177e4c
|
188 189 190 191 192 |
return 0; } module_init(uid_cache_init); |