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