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