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