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