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