Commit | Line | Data |
---|---|---|
76181c13 | 1 | /* Basic authentication token and access key management |
1da177e4 | 2 | * |
69664cf1 | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
4 | * Written by David Howells (dhowells@redhat.com) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
a7807a32 | 14 | #include <linux/poison.h> |
1da177e4 LT |
15 | #include <linux/sched.h> |
16 | #include <linux/slab.h> | |
29db9190 | 17 | #include <linux/security.h> |
1da177e4 | 18 | #include <linux/workqueue.h> |
e51f6d34 | 19 | #include <linux/random.h> |
1da177e4 | 20 | #include <linux/err.h> |
1d1e9756 | 21 | #include <linux/user_namespace.h> |
1da177e4 LT |
22 | #include "internal.h" |
23 | ||
e18b890b | 24 | static struct kmem_cache *key_jar; |
1da177e4 LT |
25 | struct rb_root key_serial_tree; /* tree of keys indexed by serial */ |
26 | DEFINE_SPINLOCK(key_serial_lock); | |
27 | ||
28 | struct rb_root key_user_tree; /* tree of quota records indexed by UID */ | |
29 | DEFINE_SPINLOCK(key_user_lock); | |
30 | ||
0b77f5bf DH |
31 | unsigned int key_quota_root_maxkeys = 200; /* root's key count quota */ |
32 | unsigned int key_quota_root_maxbytes = 20000; /* root's key space quota */ | |
33 | unsigned int key_quota_maxkeys = 200; /* general key count quota */ | |
34 | unsigned int key_quota_maxbytes = 20000; /* general key space quota */ | |
35 | ||
1da177e4 LT |
36 | static LIST_HEAD(key_types_list); |
37 | static DECLARE_RWSEM(key_types_sem); | |
38 | ||
65f27f38 DH |
39 | static void key_cleanup(struct work_struct *work); |
40 | static DECLARE_WORK(key_cleanup_task, key_cleanup); | |
1da177e4 LT |
41 | |
42 | /* we serialise key instantiation and link */ | |
76181c13 | 43 | DEFINE_MUTEX(key_construction_mutex); |
1da177e4 LT |
44 | |
45 | /* any key who's type gets unegistered will be re-typed to this */ | |
1ae8f407 | 46 | static struct key_type key_type_dead = { |
1da177e4 LT |
47 | .name = "dead", |
48 | }; | |
49 | ||
50 | #ifdef KEY_DEBUGGING | |
51 | void __key_check(const struct key *key) | |
52 | { | |
53 | printk("__key_check: key %p {%08x} should be {%08x}\n", | |
54 | key, key->magic, KEY_DEBUG_MAGIC); | |
55 | BUG(); | |
56 | } | |
57 | #endif | |
58 | ||
1da177e4 LT |
59 | /* |
60 | * get the key quota record for a user, allocating a new record if one doesn't | |
61 | * already exist | |
62 | */ | |
1d1e9756 | 63 | struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns) |
1da177e4 LT |
64 | { |
65 | struct key_user *candidate = NULL, *user; | |
66 | struct rb_node *parent = NULL; | |
67 | struct rb_node **p; | |
68 | ||
69 | try_again: | |
70 | p = &key_user_tree.rb_node; | |
71 | spin_lock(&key_user_lock); | |
72 | ||
73 | /* search the tree for a user record with a matching UID */ | |
74 | while (*p) { | |
75 | parent = *p; | |
76 | user = rb_entry(parent, struct key_user, node); | |
77 | ||
78 | if (uid < user->uid) | |
79 | p = &(*p)->rb_left; | |
80 | else if (uid > user->uid) | |
81 | p = &(*p)->rb_right; | |
1d1e9756 SH |
82 | else if (user_ns < user->user_ns) |
83 | p = &(*p)->rb_left; | |
84 | else if (user_ns > user->user_ns) | |
85 | p = &(*p)->rb_right; | |
1da177e4 LT |
86 | else |
87 | goto found; | |
88 | } | |
89 | ||
90 | /* if we get here, we failed to find a match in the tree */ | |
91 | if (!candidate) { | |
92 | /* allocate a candidate user record if we don't already have | |
93 | * one */ | |
94 | spin_unlock(&key_user_lock); | |
95 | ||
96 | user = NULL; | |
97 | candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL); | |
98 | if (unlikely(!candidate)) | |
99 | goto out; | |
100 | ||
101 | /* the allocation may have scheduled, so we need to repeat the | |
102 | * search lest someone else added the record whilst we were | |
103 | * asleep */ | |
104 | goto try_again; | |
105 | } | |
106 | ||
107 | /* if we get here, then the user record still hadn't appeared on the | |
108 | * second pass - so we use the candidate record */ | |
109 | atomic_set(&candidate->usage, 1); | |
110 | atomic_set(&candidate->nkeys, 0); | |
111 | atomic_set(&candidate->nikeys, 0); | |
112 | candidate->uid = uid; | |
1d1e9756 | 113 | candidate->user_ns = get_user_ns(user_ns); |
1da177e4 LT |
114 | candidate->qnkeys = 0; |
115 | candidate->qnbytes = 0; | |
116 | spin_lock_init(&candidate->lock); | |
76181c13 | 117 | mutex_init(&candidate->cons_lock); |
1da177e4 LT |
118 | |
119 | rb_link_node(&candidate->node, parent, p); | |
120 | rb_insert_color(&candidate->node, &key_user_tree); | |
121 | spin_unlock(&key_user_lock); | |
122 | user = candidate; | |
123 | goto out; | |
124 | ||
125 | /* okay - we found a user record for this UID */ | |
126 | found: | |
127 | atomic_inc(&user->usage); | |
128 | spin_unlock(&key_user_lock); | |
a7f988ba | 129 | kfree(candidate); |
1da177e4 LT |
130 | out: |
131 | return user; | |
a8b17ed0 | 132 | } |
1da177e4 | 133 | |
1da177e4 LT |
134 | /* |
135 | * dispose of a user structure | |
136 | */ | |
137 | void key_user_put(struct key_user *user) | |
138 | { | |
139 | if (atomic_dec_and_lock(&user->usage, &key_user_lock)) { | |
140 | rb_erase(&user->node, &key_user_tree); | |
141 | spin_unlock(&key_user_lock); | |
1d1e9756 | 142 | put_user_ns(user->user_ns); |
1da177e4 LT |
143 | |
144 | kfree(user); | |
145 | } | |
a8b17ed0 | 146 | } |
1da177e4 | 147 | |
1da177e4 LT |
148 | /* |
149 | * assign a key the next unique serial number | |
e51f6d34 ML |
150 | * - these are assigned randomly to avoid security issues through covert |
151 | * channel problems | |
1da177e4 LT |
152 | */ |
153 | static inline void key_alloc_serial(struct key *key) | |
154 | { | |
155 | struct rb_node *parent, **p; | |
156 | struct key *xkey; | |
157 | ||
e51f6d34 | 158 | /* propose a random serial number and look for a hole for it in the |
1da177e4 | 159 | * serial number tree */ |
e51f6d34 ML |
160 | do { |
161 | get_random_bytes(&key->serial, sizeof(key->serial)); | |
162 | ||
163 | key->serial >>= 1; /* negative numbers are not permitted */ | |
164 | } while (key->serial < 3); | |
165 | ||
166 | spin_lock(&key_serial_lock); | |
1da177e4 | 167 | |
9ad0830f | 168 | attempt_insertion: |
1da177e4 LT |
169 | parent = NULL; |
170 | p = &key_serial_tree.rb_node; | |
171 | ||
172 | while (*p) { | |
173 | parent = *p; | |
174 | xkey = rb_entry(parent, struct key, serial_node); | |
175 | ||
176 | if (key->serial < xkey->serial) | |
177 | p = &(*p)->rb_left; | |
178 | else if (key->serial > xkey->serial) | |
179 | p = &(*p)->rb_right; | |
180 | else | |
181 | goto serial_exists; | |
182 | } | |
9ad0830f DH |
183 | |
184 | /* we've found a suitable hole - arrange for this key to occupy it */ | |
185 | rb_link_node(&key->serial_node, parent, p); | |
186 | rb_insert_color(&key->serial_node, &key_serial_tree); | |
187 | ||
188 | spin_unlock(&key_serial_lock); | |
189 | return; | |
1da177e4 LT |
190 | |
191 | /* we found a key with the proposed serial number - walk the tree from | |
192 | * that point looking for the next unused serial number */ | |
e51f6d34 | 193 | serial_exists: |
1da177e4 | 194 | for (;;) { |
e51f6d34 | 195 | key->serial++; |
9ad0830f DH |
196 | if (key->serial < 3) { |
197 | key->serial = 3; | |
198 | goto attempt_insertion; | |
199 | } | |
1da177e4 LT |
200 | |
201 | parent = rb_next(parent); | |
202 | if (!parent) | |
9ad0830f | 203 | goto attempt_insertion; |
1da177e4 LT |
204 | |
205 | xkey = rb_entry(parent, struct key, serial_node); | |
206 | if (key->serial < xkey->serial) | |
9ad0830f | 207 | goto attempt_insertion; |
1da177e4 | 208 | } |
a8b17ed0 | 209 | } |
1da177e4 | 210 | |
1da177e4 LT |
211 | /* |
212 | * allocate a key of the specified type | |
213 | * - update the user's quota to reflect the existence of the key | |
8d9067bd DH |
214 | * - called from a key-type operation with key_types_sem read-locked by |
215 | * key_create_or_update() | |
216 | * - this prevents unregistration of the key type | |
1da177e4 LT |
217 | * - upon return the key is as yet uninstantiated; the caller needs to either |
218 | * instantiate the key or discard it before returning | |
219 | */ | |
220 | struct key *key_alloc(struct key_type *type, const char *desc, | |
d84f4f99 | 221 | uid_t uid, gid_t gid, const struct cred *cred, |
7e047ef5 | 222 | key_perm_t perm, unsigned long flags) |
1da177e4 LT |
223 | { |
224 | struct key_user *user = NULL; | |
225 | struct key *key; | |
226 | size_t desclen, quotalen; | |
29db9190 | 227 | int ret; |
1da177e4 LT |
228 | |
229 | key = ERR_PTR(-EINVAL); | |
230 | if (!desc || !*desc) | |
231 | goto error; | |
232 | ||
233 | desclen = strlen(desc) + 1; | |
234 | quotalen = desclen + type->def_datalen; | |
235 | ||
236 | /* get hold of the key tracking for this user */ | |
1d1e9756 | 237 | user = key_user_lookup(uid, cred->user->user_ns); |
1da177e4 LT |
238 | if (!user) |
239 | goto no_memory_1; | |
240 | ||
241 | /* check that the user's quota permits allocation of another key and | |
242 | * its description */ | |
7e047ef5 | 243 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { |
0b77f5bf DH |
244 | unsigned maxkeys = (uid == 0) ? |
245 | key_quota_root_maxkeys : key_quota_maxkeys; | |
246 | unsigned maxbytes = (uid == 0) ? | |
247 | key_quota_root_maxbytes : key_quota_maxbytes; | |
248 | ||
1da177e4 | 249 | spin_lock(&user->lock); |
7e047ef5 | 250 | if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) { |
0b77f5bf DH |
251 | if (user->qnkeys + 1 >= maxkeys || |
252 | user->qnbytes + quotalen >= maxbytes || | |
253 | user->qnbytes + quotalen < user->qnbytes) | |
7e047ef5 DH |
254 | goto no_quota; |
255 | } | |
1da177e4 LT |
256 | |
257 | user->qnkeys++; | |
258 | user->qnbytes += quotalen; | |
259 | spin_unlock(&user->lock); | |
260 | } | |
261 | ||
262 | /* allocate and initialise the key and its description */ | |
e94b1766 | 263 | key = kmem_cache_alloc(key_jar, GFP_KERNEL); |
1da177e4 LT |
264 | if (!key) |
265 | goto no_memory_2; | |
266 | ||
267 | if (desc) { | |
48ad504e | 268 | key->description = kmemdup(desc, desclen, GFP_KERNEL); |
1da177e4 LT |
269 | if (!key->description) |
270 | goto no_memory_3; | |
1da177e4 LT |
271 | } |
272 | ||
273 | atomic_set(&key->usage, 1); | |
1da177e4 LT |
274 | init_rwsem(&key->sem); |
275 | key->type = type; | |
276 | key->user = user; | |
277 | key->quotalen = quotalen; | |
278 | key->datalen = type->def_datalen; | |
279 | key->uid = uid; | |
280 | key->gid = gid; | |
281 | key->perm = perm; | |
282 | key->flags = 0; | |
283 | key->expiry = 0; | |
284 | key->payload.data = NULL; | |
29db9190 | 285 | key->security = NULL; |
1da177e4 | 286 | |
7e047ef5 | 287 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) |
76d8aeab | 288 | key->flags |= 1 << KEY_FLAG_IN_QUOTA; |
1da177e4 LT |
289 | |
290 | memset(&key->type_data, 0, sizeof(key->type_data)); | |
291 | ||
292 | #ifdef KEY_DEBUGGING | |
293 | key->magic = KEY_DEBUG_MAGIC; | |
294 | #endif | |
295 | ||
29db9190 | 296 | /* let the security module know about the key */ |
d84f4f99 | 297 | ret = security_key_alloc(key, cred, flags); |
29db9190 DH |
298 | if (ret < 0) |
299 | goto security_error; | |
300 | ||
1da177e4 LT |
301 | /* publish the key by giving it a serial number */ |
302 | atomic_inc(&user->nkeys); | |
303 | key_alloc_serial(key); | |
304 | ||
29db9190 | 305 | error: |
1da177e4 LT |
306 | return key; |
307 | ||
29db9190 DH |
308 | security_error: |
309 | kfree(key->description); | |
1da177e4 | 310 | kmem_cache_free(key_jar, key); |
7e047ef5 | 311 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { |
1da177e4 LT |
312 | spin_lock(&user->lock); |
313 | user->qnkeys--; | |
314 | user->qnbytes -= quotalen; | |
315 | spin_unlock(&user->lock); | |
316 | } | |
317 | key_user_put(user); | |
29db9190 DH |
318 | key = ERR_PTR(ret); |
319 | goto error; | |
320 | ||
321 | no_memory_3: | |
322 | kmem_cache_free(key_jar, key); | |
323 | no_memory_2: | |
7e047ef5 | 324 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { |
29db9190 DH |
325 | spin_lock(&user->lock); |
326 | user->qnkeys--; | |
327 | user->qnbytes -= quotalen; | |
328 | spin_unlock(&user->lock); | |
329 | } | |
330 | key_user_put(user); | |
331 | no_memory_1: | |
1da177e4 LT |
332 | key = ERR_PTR(-ENOMEM); |
333 | goto error; | |
334 | ||
29db9190 | 335 | no_quota: |
1da177e4 LT |
336 | spin_unlock(&user->lock); |
337 | key_user_put(user); | |
338 | key = ERR_PTR(-EDQUOT); | |
339 | goto error; | |
a8b17ed0 | 340 | } |
1da177e4 LT |
341 | |
342 | EXPORT_SYMBOL(key_alloc); | |
343 | ||
1da177e4 LT |
344 | /* |
345 | * reserve an amount of quota for the key's payload | |
346 | */ | |
347 | int key_payload_reserve(struct key *key, size_t datalen) | |
348 | { | |
c5b60b5e | 349 | int delta = (int)datalen - key->datalen; |
1da177e4 LT |
350 | int ret = 0; |
351 | ||
352 | key_check(key); | |
353 | ||
354 | /* contemplate the quota adjustment */ | |
76d8aeab | 355 | if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
0b77f5bf DH |
356 | unsigned maxbytes = (key->user->uid == 0) ? |
357 | key_quota_root_maxbytes : key_quota_maxbytes; | |
358 | ||
1da177e4 LT |
359 | spin_lock(&key->user->lock); |
360 | ||
361 | if (delta > 0 && | |
0b77f5bf DH |
362 | (key->user->qnbytes + delta >= maxbytes || |
363 | key->user->qnbytes + delta < key->user->qnbytes)) { | |
1da177e4 LT |
364 | ret = -EDQUOT; |
365 | } | |
366 | else { | |
367 | key->user->qnbytes += delta; | |
368 | key->quotalen += delta; | |
369 | } | |
370 | spin_unlock(&key->user->lock); | |
371 | } | |
372 | ||
373 | /* change the recorded data length if that didn't generate an error */ | |
374 | if (ret == 0) | |
375 | key->datalen = datalen; | |
376 | ||
377 | return ret; | |
a8b17ed0 | 378 | } |
1da177e4 LT |
379 | |
380 | EXPORT_SYMBOL(key_payload_reserve); | |
381 | ||
1da177e4 LT |
382 | /* |
383 | * instantiate a key and link it into the target keyring atomically | |
384 | * - called with the target keyring's semaphore writelocked | |
385 | */ | |
386 | static int __key_instantiate_and_link(struct key *key, | |
387 | const void *data, | |
388 | size_t datalen, | |
3e30148c | 389 | struct key *keyring, |
f70e2e06 DH |
390 | struct key *authkey, |
391 | struct keyring_list **_prealloc) | |
1da177e4 LT |
392 | { |
393 | int ret, awaken; | |
394 | ||
395 | key_check(key); | |
396 | key_check(keyring); | |
397 | ||
398 | awaken = 0; | |
399 | ret = -EBUSY; | |
400 | ||
76181c13 | 401 | mutex_lock(&key_construction_mutex); |
1da177e4 LT |
402 | |
403 | /* can't instantiate twice */ | |
76d8aeab | 404 | if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
1da177e4 LT |
405 | /* instantiate the key */ |
406 | ret = key->type->instantiate(key, data, datalen); | |
407 | ||
408 | if (ret == 0) { | |
409 | /* mark the key as being instantiated */ | |
1da177e4 | 410 | atomic_inc(&key->user->nikeys); |
76d8aeab | 411 | set_bit(KEY_FLAG_INSTANTIATED, &key->flags); |
1da177e4 | 412 | |
76d8aeab | 413 | if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) |
1da177e4 | 414 | awaken = 1; |
1da177e4 LT |
415 | |
416 | /* and link it into the destination keyring */ | |
417 | if (keyring) | |
f70e2e06 | 418 | __key_link(keyring, key, _prealloc); |
3e30148c DH |
419 | |
420 | /* disable the authorisation key */ | |
d84f4f99 DH |
421 | if (authkey) |
422 | key_revoke(authkey); | |
1da177e4 LT |
423 | } |
424 | } | |
425 | ||
76181c13 | 426 | mutex_unlock(&key_construction_mutex); |
1da177e4 LT |
427 | |
428 | /* wake up anyone waiting for a key to be constructed */ | |
429 | if (awaken) | |
76181c13 | 430 | wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); |
1da177e4 LT |
431 | |
432 | return ret; | |
a8b17ed0 | 433 | } |
1da177e4 | 434 | |
1da177e4 LT |
435 | /* |
436 | * instantiate a key and link it into the target keyring atomically | |
437 | */ | |
438 | int key_instantiate_and_link(struct key *key, | |
439 | const void *data, | |
440 | size_t datalen, | |
3e30148c | 441 | struct key *keyring, |
d84f4f99 | 442 | struct key *authkey) |
1da177e4 | 443 | { |
f70e2e06 | 444 | struct keyring_list *prealloc; |
1da177e4 LT |
445 | int ret; |
446 | ||
f70e2e06 DH |
447 | if (keyring) { |
448 | ret = __key_link_begin(keyring, key->type, key->description, | |
449 | &prealloc); | |
450 | if (ret < 0) | |
451 | return ret; | |
452 | } | |
1da177e4 | 453 | |
f70e2e06 DH |
454 | ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey, |
455 | &prealloc); | |
1da177e4 LT |
456 | |
457 | if (keyring) | |
f70e2e06 | 458 | __key_link_end(keyring, key->type, prealloc); |
1da177e4 LT |
459 | |
460 | return ret; | |
a8b17ed0 | 461 | } |
1da177e4 LT |
462 | |
463 | EXPORT_SYMBOL(key_instantiate_and_link); | |
464 | ||
1da177e4 LT |
465 | /* |
466 | * negatively instantiate a key and link it into the target keyring atomically | |
467 | */ | |
468 | int key_negate_and_link(struct key *key, | |
469 | unsigned timeout, | |
3e30148c | 470 | struct key *keyring, |
d84f4f99 | 471 | struct key *authkey) |
1da177e4 | 472 | { |
f70e2e06 | 473 | struct keyring_list *prealloc; |
1da177e4 | 474 | struct timespec now; |
f70e2e06 | 475 | int ret, awaken, link_ret = 0; |
1da177e4 LT |
476 | |
477 | key_check(key); | |
478 | key_check(keyring); | |
479 | ||
480 | awaken = 0; | |
481 | ret = -EBUSY; | |
482 | ||
483 | if (keyring) | |
f70e2e06 DH |
484 | link_ret = __key_link_begin(keyring, key->type, |
485 | key->description, &prealloc); | |
1da177e4 | 486 | |
76181c13 | 487 | mutex_lock(&key_construction_mutex); |
1da177e4 LT |
488 | |
489 | /* can't instantiate twice */ | |
76d8aeab | 490 | if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
1da177e4 | 491 | /* mark the key as being negatively instantiated */ |
1da177e4 | 492 | atomic_inc(&key->user->nikeys); |
76d8aeab DH |
493 | set_bit(KEY_FLAG_NEGATIVE, &key->flags); |
494 | set_bit(KEY_FLAG_INSTANTIATED, &key->flags); | |
1da177e4 LT |
495 | now = current_kernel_time(); |
496 | key->expiry = now.tv_sec + timeout; | |
c08ef808 | 497 | key_schedule_gc(key->expiry + key_gc_delay); |
1da177e4 | 498 | |
76d8aeab | 499 | if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) |
1da177e4 | 500 | awaken = 1; |
1da177e4 | 501 | |
1da177e4 LT |
502 | ret = 0; |
503 | ||
504 | /* and link it into the destination keyring */ | |
f70e2e06 DH |
505 | if (keyring && link_ret == 0) |
506 | __key_link(keyring, key, &prealloc); | |
3e30148c DH |
507 | |
508 | /* disable the authorisation key */ | |
d84f4f99 DH |
509 | if (authkey) |
510 | key_revoke(authkey); | |
1da177e4 LT |
511 | } |
512 | ||
76181c13 | 513 | mutex_unlock(&key_construction_mutex); |
1da177e4 LT |
514 | |
515 | if (keyring) | |
f70e2e06 | 516 | __key_link_end(keyring, key->type, prealloc); |
1da177e4 LT |
517 | |
518 | /* wake up anyone waiting for a key to be constructed */ | |
519 | if (awaken) | |
76181c13 | 520 | wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); |
1da177e4 | 521 | |
f70e2e06 | 522 | return ret == 0 ? link_ret : ret; |
a8b17ed0 | 523 | } |
1da177e4 LT |
524 | |
525 | EXPORT_SYMBOL(key_negate_and_link); | |
526 | ||
1da177e4 LT |
527 | /* |
528 | * do cleaning up in process context so that we don't have to disable | |
529 | * interrupts all over the place | |
530 | */ | |
65f27f38 | 531 | static void key_cleanup(struct work_struct *work) |
1da177e4 LT |
532 | { |
533 | struct rb_node *_n; | |
534 | struct key *key; | |
535 | ||
536 | go_again: | |
537 | /* look for a dead key in the tree */ | |
538 | spin_lock(&key_serial_lock); | |
539 | ||
540 | for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { | |
541 | key = rb_entry(_n, struct key, serial_node); | |
542 | ||
543 | if (atomic_read(&key->usage) == 0) | |
544 | goto found_dead_key; | |
545 | } | |
546 | ||
547 | spin_unlock(&key_serial_lock); | |
548 | return; | |
549 | ||
550 | found_dead_key: | |
551 | /* we found a dead key - once we've removed it from the tree, we can | |
552 | * drop the lock */ | |
553 | rb_erase(&key->serial_node, &key_serial_tree); | |
554 | spin_unlock(&key_serial_lock); | |
555 | ||
76d8aeab DH |
556 | key_check(key); |
557 | ||
29db9190 DH |
558 | security_key_free(key); |
559 | ||
1da177e4 | 560 | /* deal with the user's key tracking and quota */ |
76d8aeab | 561 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
1da177e4 LT |
562 | spin_lock(&key->user->lock); |
563 | key->user->qnkeys--; | |
564 | key->user->qnbytes -= key->quotalen; | |
565 | spin_unlock(&key->user->lock); | |
566 | } | |
567 | ||
568 | atomic_dec(&key->user->nkeys); | |
76d8aeab | 569 | if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) |
1da177e4 LT |
570 | atomic_dec(&key->user->nikeys); |
571 | ||
572 | key_user_put(key->user); | |
573 | ||
574 | /* now throw away the key memory */ | |
575 | if (key->type->destroy) | |
576 | key->type->destroy(key); | |
577 | ||
578 | kfree(key->description); | |
579 | ||
580 | #ifdef KEY_DEBUGGING | |
581 | key->magic = KEY_DEBUG_MAGIC_X; | |
582 | #endif | |
583 | kmem_cache_free(key_jar, key); | |
584 | ||
585 | /* there may, of course, be more than one key to destroy */ | |
586 | goto go_again; | |
a8b17ed0 | 587 | } |
1da177e4 | 588 | |
1da177e4 LT |
589 | /* |
590 | * dispose of a reference to a key | |
591 | * - when all the references are gone, we schedule the cleanup task to come and | |
592 | * pull it out of the tree in definite process context | |
593 | */ | |
594 | void key_put(struct key *key) | |
595 | { | |
596 | if (key) { | |
597 | key_check(key); | |
598 | ||
599 | if (atomic_dec_and_test(&key->usage)) | |
600 | schedule_work(&key_cleanup_task); | |
601 | } | |
a8b17ed0 | 602 | } |
1da177e4 LT |
603 | |
604 | EXPORT_SYMBOL(key_put); | |
605 | ||
1da177e4 LT |
606 | /* |
607 | * find a key by its serial number | |
608 | */ | |
609 | struct key *key_lookup(key_serial_t id) | |
610 | { | |
611 | struct rb_node *n; | |
612 | struct key *key; | |
613 | ||
614 | spin_lock(&key_serial_lock); | |
615 | ||
616 | /* search the tree for the specified key */ | |
617 | n = key_serial_tree.rb_node; | |
618 | while (n) { | |
619 | key = rb_entry(n, struct key, serial_node); | |
620 | ||
621 | if (id < key->serial) | |
622 | n = n->rb_left; | |
623 | else if (id > key->serial) | |
624 | n = n->rb_right; | |
625 | else | |
626 | goto found; | |
627 | } | |
628 | ||
629 | not_found: | |
630 | key = ERR_PTR(-ENOKEY); | |
631 | goto error; | |
632 | ||
633 | found: | |
5593122e DH |
634 | /* pretend it doesn't exist if it is awaiting deletion */ |
635 | if (atomic_read(&key->usage) == 0) | |
1da177e4 LT |
636 | goto not_found; |
637 | ||
638 | /* this races with key_put(), but that doesn't matter since key_put() | |
639 | * doesn't actually change the key | |
640 | */ | |
641 | atomic_inc(&key->usage); | |
642 | ||
643 | error: | |
644 | spin_unlock(&key_serial_lock); | |
645 | return key; | |
a8b17ed0 | 646 | } |
1da177e4 | 647 | |
1da177e4 LT |
648 | /* |
649 | * find and lock the specified key type against removal | |
650 | * - we return with the sem readlocked | |
651 | */ | |
652 | struct key_type *key_type_lookup(const char *type) | |
653 | { | |
654 | struct key_type *ktype; | |
655 | ||
656 | down_read(&key_types_sem); | |
657 | ||
658 | /* look up the key type to see if it's one of the registered kernel | |
659 | * types */ | |
660 | list_for_each_entry(ktype, &key_types_list, link) { | |
661 | if (strcmp(ktype->name, type) == 0) | |
662 | goto found_kernel_type; | |
663 | } | |
664 | ||
665 | up_read(&key_types_sem); | |
666 | ktype = ERR_PTR(-ENOKEY); | |
667 | ||
668 | found_kernel_type: | |
669 | return ktype; | |
a8b17ed0 | 670 | } |
1da177e4 | 671 | |
1da177e4 LT |
672 | /* |
673 | * unlock a key type | |
674 | */ | |
675 | void key_type_put(struct key_type *ktype) | |
676 | { | |
677 | up_read(&key_types_sem); | |
a8b17ed0 | 678 | } |
1da177e4 | 679 | |
1da177e4 LT |
680 | /* |
681 | * attempt to update an existing key | |
682 | * - the key has an incremented refcount | |
683 | * - we need to put the key if we get an error | |
684 | */ | |
664cceb0 DH |
685 | static inline key_ref_t __key_update(key_ref_t key_ref, |
686 | const void *payload, size_t plen) | |
1da177e4 | 687 | { |
664cceb0 | 688 | struct key *key = key_ref_to_ptr(key_ref); |
1da177e4 LT |
689 | int ret; |
690 | ||
691 | /* need write permission on the key to update it */ | |
29db9190 DH |
692 | ret = key_permission(key_ref, KEY_WRITE); |
693 | if (ret < 0) | |
1da177e4 LT |
694 | goto error; |
695 | ||
696 | ret = -EEXIST; | |
697 | if (!key->type->update) | |
698 | goto error; | |
699 | ||
700 | down_write(&key->sem); | |
701 | ||
702 | ret = key->type->update(key, payload, plen); | |
76d8aeab | 703 | if (ret == 0) |
1da177e4 | 704 | /* updating a negative key instantiates it */ |
76d8aeab | 705 | clear_bit(KEY_FLAG_NEGATIVE, &key->flags); |
1da177e4 LT |
706 | |
707 | up_write(&key->sem); | |
708 | ||
709 | if (ret < 0) | |
710 | goto error; | |
664cceb0 DH |
711 | out: |
712 | return key_ref; | |
1da177e4 | 713 | |
664cceb0 | 714 | error: |
1da177e4 | 715 | key_put(key); |
664cceb0 | 716 | key_ref = ERR_PTR(ret); |
1da177e4 | 717 | goto out; |
a8b17ed0 | 718 | } |
1da177e4 | 719 | |
1da177e4 LT |
720 | /* |
721 | * search the specified keyring for a key of the same description; if one is | |
722 | * found, update it, otherwise add a new one | |
723 | */ | |
664cceb0 DH |
724 | key_ref_t key_create_or_update(key_ref_t keyring_ref, |
725 | const char *type, | |
726 | const char *description, | |
727 | const void *payload, | |
728 | size_t plen, | |
6b79ccb5 | 729 | key_perm_t perm, |
7e047ef5 | 730 | unsigned long flags) |
1da177e4 | 731 | { |
f70e2e06 | 732 | struct keyring_list *prealloc; |
d84f4f99 | 733 | const struct cred *cred = current_cred(); |
1da177e4 | 734 | struct key_type *ktype; |
664cceb0 | 735 | struct key *keyring, *key = NULL; |
664cceb0 | 736 | key_ref_t key_ref; |
1da177e4 LT |
737 | int ret; |
738 | ||
1da177e4 LT |
739 | /* look up the key type to see if it's one of the registered kernel |
740 | * types */ | |
741 | ktype = key_type_lookup(type); | |
742 | if (IS_ERR(ktype)) { | |
664cceb0 | 743 | key_ref = ERR_PTR(-ENODEV); |
1da177e4 LT |
744 | goto error; |
745 | } | |
746 | ||
664cceb0 | 747 | key_ref = ERR_PTR(-EINVAL); |
1da177e4 LT |
748 | if (!ktype->match || !ktype->instantiate) |
749 | goto error_2; | |
750 | ||
664cceb0 DH |
751 | keyring = key_ref_to_ptr(keyring_ref); |
752 | ||
753 | key_check(keyring); | |
754 | ||
c3a9d654 DH |
755 | key_ref = ERR_PTR(-ENOTDIR); |
756 | if (keyring->type != &key_type_keyring) | |
757 | goto error_2; | |
758 | ||
f70e2e06 DH |
759 | ret = __key_link_begin(keyring, ktype, description, &prealloc); |
760 | if (ret < 0) | |
761 | goto error_2; | |
664cceb0 DH |
762 | |
763 | /* if we're going to allocate a new key, we're going to have | |
764 | * to modify the keyring */ | |
29db9190 DH |
765 | ret = key_permission(keyring_ref, KEY_WRITE); |
766 | if (ret < 0) { | |
767 | key_ref = ERR_PTR(ret); | |
664cceb0 | 768 | goto error_3; |
29db9190 | 769 | } |
664cceb0 | 770 | |
1d9b7d97 DH |
771 | /* if it's possible to update this type of key, search for an existing |
772 | * key of the same type and description in the destination keyring and | |
773 | * update that instead if possible | |
1da177e4 | 774 | */ |
1d9b7d97 DH |
775 | if (ktype->update) { |
776 | key_ref = __keyring_search_one(keyring_ref, ktype, description, | |
777 | 0); | |
778 | if (!IS_ERR(key_ref)) | |
779 | goto found_matching_key; | |
780 | } | |
1da177e4 | 781 | |
6b79ccb5 AR |
782 | /* if the client doesn't provide, decide on the permissions we want */ |
783 | if (perm == KEY_PERM_UNDEF) { | |
784 | perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR; | |
785 | perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR; | |
1da177e4 | 786 | |
6b79ccb5 AR |
787 | if (ktype->read) |
788 | perm |= KEY_POS_READ | KEY_USR_READ; | |
1da177e4 | 789 | |
6b79ccb5 AR |
790 | if (ktype == &key_type_keyring || ktype->update) |
791 | perm |= KEY_USR_WRITE; | |
792 | } | |
1da177e4 LT |
793 | |
794 | /* allocate a new key */ | |
d84f4f99 DH |
795 | key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred, |
796 | perm, flags); | |
1da177e4 | 797 | if (IS_ERR(key)) { |
e231c2ee | 798 | key_ref = ERR_CAST(key); |
1da177e4 LT |
799 | goto error_3; |
800 | } | |
801 | ||
802 | /* instantiate it and link it into the target keyring */ | |
f70e2e06 DH |
803 | ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL, |
804 | &prealloc); | |
1da177e4 LT |
805 | if (ret < 0) { |
806 | key_put(key); | |
664cceb0 DH |
807 | key_ref = ERR_PTR(ret); |
808 | goto error_3; | |
1da177e4 LT |
809 | } |
810 | ||
664cceb0 DH |
811 | key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); |
812 | ||
1da177e4 | 813 | error_3: |
f70e2e06 | 814 | __key_link_end(keyring, ktype, prealloc); |
1da177e4 LT |
815 | error_2: |
816 | key_type_put(ktype); | |
817 | error: | |
664cceb0 | 818 | return key_ref; |
1da177e4 LT |
819 | |
820 | found_matching_key: | |
821 | /* we found a matching key, so we're going to try to update it | |
822 | * - we can drop the locks first as we have the key pinned | |
823 | */ | |
f70e2e06 | 824 | __key_link_end(keyring, ktype, prealloc); |
1da177e4 LT |
825 | key_type_put(ktype); |
826 | ||
664cceb0 | 827 | key_ref = __key_update(key_ref, payload, plen); |
1da177e4 | 828 | goto error; |
a8b17ed0 | 829 | } |
1da177e4 LT |
830 | |
831 | EXPORT_SYMBOL(key_create_or_update); | |
832 | ||
1da177e4 LT |
833 | /* |
834 | * update a key | |
835 | */ | |
664cceb0 | 836 | int key_update(key_ref_t key_ref, const void *payload, size_t plen) |
1da177e4 | 837 | { |
664cceb0 | 838 | struct key *key = key_ref_to_ptr(key_ref); |
1da177e4 LT |
839 | int ret; |
840 | ||
841 | key_check(key); | |
842 | ||
843 | /* the key must be writable */ | |
29db9190 DH |
844 | ret = key_permission(key_ref, KEY_WRITE); |
845 | if (ret < 0) | |
1da177e4 LT |
846 | goto error; |
847 | ||
848 | /* attempt to update it if supported */ | |
849 | ret = -EOPNOTSUPP; | |
850 | if (key->type->update) { | |
851 | down_write(&key->sem); | |
1da177e4 | 852 | |
29db9190 | 853 | ret = key->type->update(key, payload, plen); |
76d8aeab | 854 | if (ret == 0) |
1da177e4 | 855 | /* updating a negative key instantiates it */ |
76d8aeab | 856 | clear_bit(KEY_FLAG_NEGATIVE, &key->flags); |
1da177e4 LT |
857 | |
858 | up_write(&key->sem); | |
859 | } | |
860 | ||
861 | error: | |
862 | return ret; | |
a8b17ed0 | 863 | } |
1da177e4 LT |
864 | |
865 | EXPORT_SYMBOL(key_update); | |
866 | ||
1da177e4 LT |
867 | /* |
868 | * revoke a key | |
869 | */ | |
870 | void key_revoke(struct key *key) | |
871 | { | |
5d135440 DH |
872 | struct timespec now; |
873 | time_t time; | |
874 | ||
1da177e4 LT |
875 | key_check(key); |
876 | ||
76181c13 DH |
877 | /* make sure no one's trying to change or use the key when we mark it |
878 | * - we tell lockdep that we might nest because we might be revoking an | |
879 | * authorisation key whilst holding the sem on a key we've just | |
880 | * instantiated | |
881 | */ | |
882 | down_write_nested(&key->sem, 1); | |
883 | if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) && | |
884 | key->type->revoke) | |
04c567d9 DH |
885 | key->type->revoke(key); |
886 | ||
5d135440 DH |
887 | /* set the death time to no more than the expiry time */ |
888 | now = current_kernel_time(); | |
889 | time = now.tv_sec; | |
890 | if (key->revoked_at == 0 || key->revoked_at > time) { | |
891 | key->revoked_at = time; | |
c08ef808 | 892 | key_schedule_gc(key->revoked_at + key_gc_delay); |
5d135440 DH |
893 | } |
894 | ||
1da177e4 | 895 | up_write(&key->sem); |
a8b17ed0 | 896 | } |
1da177e4 LT |
897 | |
898 | EXPORT_SYMBOL(key_revoke); | |
899 | ||
1da177e4 LT |
900 | /* |
901 | * register a type of key | |
902 | */ | |
903 | int register_key_type(struct key_type *ktype) | |
904 | { | |
905 | struct key_type *p; | |
906 | int ret; | |
907 | ||
908 | ret = -EEXIST; | |
909 | down_write(&key_types_sem); | |
910 | ||
911 | /* disallow key types with the same name */ | |
912 | list_for_each_entry(p, &key_types_list, link) { | |
913 | if (strcmp(p->name, ktype->name) == 0) | |
914 | goto out; | |
915 | } | |
916 | ||
917 | /* store the type */ | |
918 | list_add(&ktype->link, &key_types_list); | |
919 | ret = 0; | |
920 | ||
921 | out: | |
922 | up_write(&key_types_sem); | |
923 | return ret; | |
a8b17ed0 | 924 | } |
1da177e4 LT |
925 | |
926 | EXPORT_SYMBOL(register_key_type); | |
927 | ||
1da177e4 LT |
928 | /* |
929 | * unregister a type of key | |
930 | */ | |
931 | void unregister_key_type(struct key_type *ktype) | |
932 | { | |
933 | struct rb_node *_n; | |
934 | struct key *key; | |
935 | ||
936 | down_write(&key_types_sem); | |
937 | ||
938 | /* withdraw the key type */ | |
939 | list_del_init(&ktype->link); | |
940 | ||
76d8aeab | 941 | /* mark all the keys of this type dead */ |
1da177e4 LT |
942 | spin_lock(&key_serial_lock); |
943 | ||
944 | for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { | |
945 | key = rb_entry(_n, struct key, serial_node); | |
946 | ||
f041ae2f | 947 | if (key->type == ktype) { |
76d8aeab | 948 | key->type = &key_type_dead; |
f041ae2f DH |
949 | set_bit(KEY_FLAG_DEAD, &key->flags); |
950 | } | |
76d8aeab DH |
951 | } |
952 | ||
953 | spin_unlock(&key_serial_lock); | |
954 | ||
955 | /* make sure everyone revalidates their keys */ | |
b2b18660 | 956 | synchronize_rcu(); |
76d8aeab DH |
957 | |
958 | /* we should now be able to destroy the payloads of all the keys of | |
959 | * this type with impunity */ | |
960 | spin_lock(&key_serial_lock); | |
1da177e4 | 961 | |
76d8aeab DH |
962 | for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { |
963 | key = rb_entry(_n, struct key, serial_node); | |
1da177e4 | 964 | |
76d8aeab DH |
965 | if (key->type == ktype) { |
966 | if (ktype->destroy) | |
967 | ktype->destroy(key); | |
a7807a32 | 968 | memset(&key->payload, KEY_DESTROY, sizeof(key->payload)); |
76d8aeab | 969 | } |
1da177e4 LT |
970 | } |
971 | ||
972 | spin_unlock(&key_serial_lock); | |
973 | up_write(&key_types_sem); | |
974 | ||
5d135440 | 975 | key_schedule_gc(0); |
a8b17ed0 | 976 | } |
1da177e4 LT |
977 | |
978 | EXPORT_SYMBOL(unregister_key_type); | |
979 | ||
1da177e4 LT |
980 | /* |
981 | * initialise the key management stuff | |
982 | */ | |
983 | void __init key_init(void) | |
984 | { | |
985 | /* allocate a slab in which we can store keys */ | |
986 | key_jar = kmem_cache_create("key_jar", sizeof(struct key), | |
20c2df83 | 987 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
1da177e4 LT |
988 | |
989 | /* add the special key types */ | |
990 | list_add_tail(&key_type_keyring.link, &key_types_list); | |
991 | list_add_tail(&key_type_dead.link, &key_types_list); | |
992 | list_add_tail(&key_type_user.link, &key_types_list); | |
993 | ||
994 | /* record the root user tracking */ | |
995 | rb_link_node(&root_key_user.node, | |
996 | NULL, | |
997 | &key_user_tree.rb_node); | |
998 | ||
999 | rb_insert_color(&root_key_user.node, | |
1000 | &key_user_tree); | |
a8b17ed0 | 1001 | } |