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