Merge branch 'bkl/procfs' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.git] / security / keys / keyring.c
CommitLineData
69664cf1 1/* Keyring handling
1da177e4 2 *
69664cf1 3 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
29db9190 16#include <linux/security.h>
1da177e4
LT
17#include <linux/seq_file.h>
18#include <linux/err.h>
e9e349b0 19#include <keys/keyring-type.h>
1da177e4
LT
20#include <asm/uaccess.h>
21#include "internal.h"
22
f0641cba
DH
23#define rcu_dereference_locked_keyring(keyring) \
24 (rcu_dereference_protected( \
25 (keyring)->payload.subscriptions, \
26 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
27
1da177e4
LT
28/*
29 * when plumbing the depths of the key tree, this sets a hard limit set on how
30 * deep we're willing to go
31 */
32#define KEYRING_SEARCH_MAX_DEPTH 6
33
34/*
35 * we keep all named keyrings in a hash to speed looking them up
36 */
37#define KEYRING_NAME_HASH_SIZE (1 << 5)
38
39static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
40static DEFINE_RWLOCK(keyring_name_lock);
41
42static inline unsigned keyring_hash(const char *desc)
43{
44 unsigned bucket = 0;
45
46 for (; *desc; desc++)
47 bucket += (unsigned char) *desc;
48
49 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
50}
51
52/*
53 * the keyring type definition
54 */
55static int keyring_instantiate(struct key *keyring,
56 const void *data, size_t datalen);
1da177e4 57static int keyring_match(const struct key *keyring, const void *criterion);
31204ed9 58static void keyring_revoke(struct key *keyring);
1da177e4
LT
59static void keyring_destroy(struct key *keyring);
60static void keyring_describe(const struct key *keyring, struct seq_file *m);
61static long keyring_read(const struct key *keyring,
62 char __user *buffer, size_t buflen);
63
64struct key_type key_type_keyring = {
65 .name = "keyring",
66 .def_datalen = sizeof(struct keyring_list),
67 .instantiate = keyring_instantiate,
1da177e4 68 .match = keyring_match,
31204ed9 69 .revoke = keyring_revoke,
1da177e4
LT
70 .destroy = keyring_destroy,
71 .describe = keyring_describe,
72 .read = keyring_read,
73};
74
7318226e
DH
75EXPORT_SYMBOL(key_type_keyring);
76
1da177e4
LT
77/*
78 * semaphore to serialise link/link calls to prevent two link calls in parallel
79 * introducing a cycle
80 */
1ae8f407 81static DECLARE_RWSEM(keyring_serialise_link_sem);
1da177e4
LT
82
83/*****************************************************************************/
84/*
85 * publish the name of a keyring so that it can be found by name (if it has
86 * one)
87 */
69664cf1 88static void keyring_publish_name(struct key *keyring)
1da177e4
LT
89{
90 int bucket;
91
92 if (keyring->description) {
93 bucket = keyring_hash(keyring->description);
94
95 write_lock(&keyring_name_lock);
96
97 if (!keyring_name_hash[bucket].next)
98 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
99
100 list_add_tail(&keyring->type_data.link,
101 &keyring_name_hash[bucket]);
102
103 write_unlock(&keyring_name_lock);
104 }
105
106} /* end keyring_publish_name() */
107
108/*****************************************************************************/
109/*
110 * initialise a keyring
111 * - we object if we were given any data
112 */
113static int keyring_instantiate(struct key *keyring,
114 const void *data, size_t datalen)
115{
116 int ret;
117
118 ret = -EINVAL;
119 if (datalen == 0) {
120 /* make the keyring available by name if it has one */
121 keyring_publish_name(keyring);
122 ret = 0;
123 }
124
125 return ret;
126
127} /* end keyring_instantiate() */
128
1da177e4
LT
129/*****************************************************************************/
130/*
131 * match keyrings on their name
132 */
133static int keyring_match(const struct key *keyring, const void *description)
134{
135 return keyring->description &&
136 strcmp(keyring->description, description) == 0;
137
138} /* end keyring_match() */
139
140/*****************************************************************************/
141/*
142 * dispose of the data dangling from the corpse of a keyring
143 */
144static void keyring_destroy(struct key *keyring)
145{
146 struct keyring_list *klist;
147 int loop;
148
149 if (keyring->description) {
150 write_lock(&keyring_name_lock);
94efe72f
DH
151
152 if (keyring->type_data.link.next != NULL &&
153 !list_empty(&keyring->type_data.link))
154 list_del(&keyring->type_data.link);
155
1da177e4
LT
156 write_unlock(&keyring_name_lock);
157 }
158
e7b0a61b
PM
159 klist = rcu_dereference_check(keyring->payload.subscriptions,
160 rcu_read_lock_held() ||
161 atomic_read(&keyring->usage) == 0);
1da177e4
LT
162 if (klist) {
163 for (loop = klist->nkeys - 1; loop >= 0; loop--)
164 key_put(klist->keys[loop]);
165 kfree(klist);
166 }
167
168} /* end keyring_destroy() */
169
170/*****************************************************************************/
171/*
172 * describe the keyring
173 */
174static void keyring_describe(const struct key *keyring, struct seq_file *m)
175{
176 struct keyring_list *klist;
177
178 if (keyring->description) {
179 seq_puts(m, keyring->description);
180 }
181 else {
182 seq_puts(m, "[anon]");
183 }
184
76d8aeab
DH
185 rcu_read_lock();
186 klist = rcu_dereference(keyring->payload.subscriptions);
1da177e4
LT
187 if (klist)
188 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
189 else
190 seq_puts(m, ": empty");
76d8aeab 191 rcu_read_unlock();
1da177e4
LT
192
193} /* end keyring_describe() */
194
195/*****************************************************************************/
196/*
197 * read a list of key IDs from the keyring's contents
76d8aeab 198 * - the keyring's semaphore is read-locked
1da177e4
LT
199 */
200static long keyring_read(const struct key *keyring,
201 char __user *buffer, size_t buflen)
202{
203 struct keyring_list *klist;
204 struct key *key;
205 size_t qty, tmp;
206 int loop, ret;
207
208 ret = 0;
f0641cba 209 klist = rcu_dereference_locked_keyring(keyring);
1da177e4
LT
210 if (klist) {
211 /* calculate how much data we could return */
212 qty = klist->nkeys * sizeof(key_serial_t);
213
214 if (buffer && buflen > 0) {
215 if (buflen > qty)
216 buflen = qty;
217
218 /* copy the IDs of the subscribed keys into the
219 * buffer */
220 ret = -EFAULT;
221
222 for (loop = 0; loop < klist->nkeys; loop++) {
223 key = klist->keys[loop];
224
225 tmp = sizeof(key_serial_t);
226 if (tmp > buflen)
227 tmp = buflen;
228
229 if (copy_to_user(buffer,
230 &key->serial,
231 tmp) != 0)
232 goto error;
233
234 buflen -= tmp;
235 if (buflen == 0)
236 break;
237 buffer += tmp;
238 }
239 }
240
241 ret = qty;
242 }
243
244 error:
245 return ret;
246
247} /* end keyring_read() */
248
249/*****************************************************************************/
250/*
251 * allocate a keyring and link into the destination keyring
252 */
253struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
d84f4f99 254 const struct cred *cred, unsigned long flags,
d720024e 255 struct key *dest)
1da177e4
LT
256{
257 struct key *keyring;
258 int ret;
259
260 keyring = key_alloc(&key_type_keyring, description,
d84f4f99 261 uid, gid, cred,
29db9190 262 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
7e047ef5 263 flags);
1da177e4
LT
264
265 if (!IS_ERR(keyring)) {
3e30148c 266 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
1da177e4
LT
267 if (ret < 0) {
268 key_put(keyring);
269 keyring = ERR_PTR(ret);
270 }
271 }
272
273 return keyring;
274
275} /* end keyring_alloc() */
276
277/*****************************************************************************/
278/*
279 * search the supplied keyring tree for a key that matches the criterion
280 * - perform a breadth-then-depth search up to the prescribed limit
281 * - we only find keys on which we have search permission
282 * - we use the supplied match function to see if the description (or other
283 * feature of interest) matches
3e30148c 284 * - we rely on RCU to prevent the keyring lists from disappearing on us
1da177e4
LT
285 * - we return -EAGAIN if we didn't find any matching key
286 * - we return -ENOKEY if we only found negative matching keys
664cceb0 287 * - we propagate the possession attribute from the keyring ref to the key ref
1da177e4 288 */
664cceb0 289key_ref_t keyring_search_aux(key_ref_t keyring_ref,
d84f4f99 290 const struct cred *cred,
664cceb0
DH
291 struct key_type *type,
292 const void *description,
293 key_match_func_t match)
1da177e4
LT
294{
295 struct {
76d8aeab 296 struct keyring_list *keylist;
1da177e4
LT
297 int kix;
298 } stack[KEYRING_SEARCH_MAX_DEPTH];
299
300 struct keyring_list *keylist;
301 struct timespec now;
dceba994 302 unsigned long possessed, kflags;
664cceb0
DH
303 struct key *keyring, *key;
304 key_ref_t key_ref;
1da177e4 305 long err;
76d8aeab 306 int sp, kix;
1da177e4 307
664cceb0
DH
308 keyring = key_ref_to_ptr(keyring_ref);
309 possessed = is_key_possessed(keyring_ref);
1da177e4
LT
310 key_check(keyring);
311
312 /* top keyring must have search permission to begin the search */
d84f4f99 313 err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
29db9190
DH
314 if (err < 0) {
315 key_ref = ERR_PTR(err);
1da177e4 316 goto error;
29db9190 317 }
1da177e4 318
664cceb0 319 key_ref = ERR_PTR(-ENOTDIR);
1da177e4
LT
320 if (keyring->type != &key_type_keyring)
321 goto error;
322
664cceb0
DH
323 rcu_read_lock();
324
1da177e4
LT
325 now = current_kernel_time();
326 err = -EAGAIN;
327 sp = 0;
328
dceba994
KC
329 /* firstly we should check to see if this top-level keyring is what we
330 * are looking for */
331 key_ref = ERR_PTR(-EAGAIN);
332 kflags = keyring->flags;
333 if (keyring->type == type && match(keyring, description)) {
334 key = keyring;
335
336 /* check it isn't negative and hasn't expired or been
337 * revoked */
338 if (kflags & (1 << KEY_FLAG_REVOKED))
339 goto error_2;
340 if (key->expiry && now.tv_sec >= key->expiry)
341 goto error_2;
342 key_ref = ERR_PTR(-ENOKEY);
343 if (kflags & (1 << KEY_FLAG_NEGATIVE))
344 goto error_2;
345 goto found;
346 }
347
348 /* otherwise, the top keyring must not be revoked, expired, or
349 * negatively instantiated if we are to search it */
350 key_ref = ERR_PTR(-EAGAIN);
351 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
352 (keyring->expiry && now.tv_sec >= keyring->expiry))
353 goto error_2;
354
1da177e4 355 /* start processing a new keyring */
664cceb0 356descend:
76d8aeab 357 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1da177e4
LT
358 goto not_this_keyring;
359
76d8aeab 360 keylist = rcu_dereference(keyring->payload.subscriptions);
1da177e4
LT
361 if (!keylist)
362 goto not_this_keyring;
363
364 /* iterate through the keys in this keyring first */
365 for (kix = 0; kix < keylist->nkeys; kix++) {
366 key = keylist->keys[kix];
dceba994 367 kflags = key->flags;
1da177e4
LT
368
369 /* ignore keys not of this type */
370 if (key->type != type)
371 continue;
372
373 /* skip revoked keys and expired keys */
dceba994 374 if (kflags & (1 << KEY_FLAG_REVOKED))
1da177e4
LT
375 continue;
376
377 if (key->expiry && now.tv_sec >= key->expiry)
378 continue;
379
380 /* keys that don't match */
381 if (!match(key, description))
382 continue;
383
384 /* key must have search permissions */
29db9190 385 if (key_task_permission(make_key_ref(key, possessed),
d84f4f99 386 cred, KEY_SEARCH) < 0)
1da177e4
LT
387 continue;
388
dceba994
KC
389 /* we set a different error code if we pass a negative key */
390 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
1da177e4
LT
391 err = -ENOKEY;
392 continue;
393 }
394
395 goto found;
396 }
397
398 /* search through the keyrings nested in this one */
399 kix = 0;
664cceb0 400ascend:
76d8aeab 401 for (; kix < keylist->nkeys; kix++) {
1da177e4
LT
402 key = keylist->keys[kix];
403 if (key->type != &key_type_keyring)
76d8aeab 404 continue;
1da177e4
LT
405
406 /* recursively search nested keyrings
407 * - only search keyrings for which we have search permission
408 */
409 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
76d8aeab 410 continue;
1da177e4 411
0f6ed7c2 412 if (key_task_permission(make_key_ref(key, possessed),
d84f4f99 413 cred, KEY_SEARCH) < 0)
76d8aeab 414 continue;
1da177e4
LT
415
416 /* stack the current position */
76d8aeab 417 stack[sp].keylist = keylist;
1da177e4
LT
418 stack[sp].kix = kix;
419 sp++;
420
421 /* begin again with the new keyring */
422 keyring = key;
423 goto descend;
1da177e4
LT
424 }
425
426 /* the keyring we're looking at was disqualified or didn't contain a
427 * matching key */
664cceb0 428not_this_keyring:
1da177e4
LT
429 if (sp > 0) {
430 /* resume the processing of a keyring higher up in the tree */
431 sp--;
76d8aeab 432 keylist = stack[sp].keylist;
1da177e4
LT
433 kix = stack[sp].kix + 1;
434 goto ascend;
435 }
436
664cceb0
DH
437 key_ref = ERR_PTR(err);
438 goto error_2;
1da177e4
LT
439
440 /* we found a viable match */
664cceb0 441found:
1da177e4 442 atomic_inc(&key->usage);
1da177e4 443 key_check(key);
664cceb0
DH
444 key_ref = make_key_ref(key, possessed);
445error_2:
76d8aeab 446 rcu_read_unlock();
664cceb0
DH
447error:
448 return key_ref;
1da177e4
LT
449
450} /* end keyring_search_aux() */
451
452/*****************************************************************************/
453/*
454 * search the supplied keyring tree for a key that matches the criterion
455 * - perform a breadth-then-depth search up to the prescribed limit
456 * - we only find keys on which we have search permission
457 * - we readlock the keyrings as we search down the tree
458 * - we return -EAGAIN if we didn't find any matching key
459 * - we return -ENOKEY if we only found negative matching keys
460 */
664cceb0
DH
461key_ref_t keyring_search(key_ref_t keyring,
462 struct key_type *type,
463 const char *description)
1da177e4 464{
3e30148c
DH
465 if (!type->match)
466 return ERR_PTR(-ENOKEY);
467
d84f4f99 468 return keyring_search_aux(keyring, current->cred,
3e30148c 469 type, description, type->match);
1da177e4
LT
470
471} /* end keyring_search() */
472
473EXPORT_SYMBOL(keyring_search);
474
475/*****************************************************************************/
476/*
477 * search the given keyring only (no recursion)
478 * - keyring must be locked by caller
c3a9d654 479 * - caller must guarantee that the keyring is a keyring
1da177e4 480 */
664cceb0
DH
481key_ref_t __keyring_search_one(key_ref_t keyring_ref,
482 const struct key_type *ktype,
483 const char *description,
484 key_perm_t perm)
1da177e4
LT
485{
486 struct keyring_list *klist;
664cceb0
DH
487 unsigned long possessed;
488 struct key *keyring, *key;
1da177e4
LT
489 int loop;
490
664cceb0
DH
491 keyring = key_ref_to_ptr(keyring_ref);
492 possessed = is_key_possessed(keyring_ref);
493
76d8aeab
DH
494 rcu_read_lock();
495
496 klist = rcu_dereference(keyring->payload.subscriptions);
1da177e4
LT
497 if (klist) {
498 for (loop = 0; loop < klist->nkeys; loop++) {
499 key = klist->keys[loop];
500
501 if (key->type == ktype &&
3e30148c
DH
502 (!key->type->match ||
503 key->type->match(key, description)) &&
664cceb0 504 key_permission(make_key_ref(key, possessed),
db1d1d57 505 perm) == 0 &&
76d8aeab 506 !test_bit(KEY_FLAG_REVOKED, &key->flags)
1da177e4
LT
507 )
508 goto found;
509 }
510 }
511
664cceb0
DH
512 rcu_read_unlock();
513 return ERR_PTR(-ENOKEY);
1da177e4
LT
514
515 found:
516 atomic_inc(&key->usage);
76d8aeab 517 rcu_read_unlock();
664cceb0 518 return make_key_ref(key, possessed);
1da177e4
LT
519
520} /* end __keyring_search_one() */
521
522/*****************************************************************************/
523/*
524 * find a keyring with the specified name
525 * - all named keyrings are searched
69664cf1 526 * - normally only finds keyrings with search permission for the current process
1da177e4 527 */
69664cf1 528struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
1da177e4
LT
529{
530 struct key *keyring;
531 int bucket;
532
1da177e4 533 if (!name)
cea7daa3 534 return ERR_PTR(-EINVAL);
1da177e4
LT
535
536 bucket = keyring_hash(name);
537
538 read_lock(&keyring_name_lock);
539
540 if (keyring_name_hash[bucket].next) {
541 /* search this hash bucket for a keyring with a matching name
542 * that's readable and that hasn't been revoked */
543 list_for_each_entry(keyring,
544 &keyring_name_hash[bucket],
545 type_data.link
546 ) {
2ea190d0
SH
547 if (keyring->user->user_ns != current_user_ns())
548 continue;
549
76d8aeab 550 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1da177e4
LT
551 continue;
552
553 if (strcmp(keyring->description, name) != 0)
554 continue;
555
69664cf1
DH
556 if (!skip_perm_check &&
557 key_permission(make_key_ref(keyring, 0),
0f6ed7c2 558 KEY_SEARCH) < 0)
1da177e4
LT
559 continue;
560
cea7daa3
TO
561 /* we've got a match but we might end up racing with
562 * key_cleanup() if the keyring is currently 'dead'
563 * (ie. it has a zero usage count) */
564 if (!atomic_inc_not_zero(&keyring->usage))
565 continue;
566 goto out;
1da177e4
LT
567 }
568 }
569
1da177e4 570 keyring = ERR_PTR(-ENOKEY);
cea7daa3
TO
571out:
572 read_unlock(&keyring_name_lock);
1da177e4
LT
573 return keyring;
574
575} /* end find_keyring_by_name() */
576
577/*****************************************************************************/
578/*
579 * see if a cycle will will be created by inserting acyclic tree B in acyclic
580 * tree A at the topmost level (ie: as a direct child of A)
581 * - since we are adding B to A at the top level, checking for cycles should
582 * just be a matter of seeing if node A is somewhere in tree B
583 */
584static int keyring_detect_cycle(struct key *A, struct key *B)
585{
586 struct {
76d8aeab 587 struct keyring_list *keylist;
1da177e4
LT
588 int kix;
589 } stack[KEYRING_SEARCH_MAX_DEPTH];
590
591 struct keyring_list *keylist;
592 struct key *subtree, *key;
593 int sp, kix, ret;
594
76d8aeab
DH
595 rcu_read_lock();
596
1da177e4
LT
597 ret = -EDEADLK;
598 if (A == B)
76d8aeab 599 goto cycle_detected;
1da177e4
LT
600
601 subtree = B;
602 sp = 0;
603
604 /* start processing a new keyring */
605 descend:
76d8aeab 606 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
1da177e4
LT
607 goto not_this_keyring;
608
76d8aeab 609 keylist = rcu_dereference(subtree->payload.subscriptions);
1da177e4
LT
610 if (!keylist)
611 goto not_this_keyring;
612 kix = 0;
613
614 ascend:
615 /* iterate through the remaining keys in this keyring */
616 for (; kix < keylist->nkeys; kix++) {
617 key = keylist->keys[kix];
618
619 if (key == A)
620 goto cycle_detected;
621
622 /* recursively check nested keyrings */
623 if (key->type == &key_type_keyring) {
624 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
625 goto too_deep;
626
627 /* stack the current position */
76d8aeab 628 stack[sp].keylist = keylist;
1da177e4
LT
629 stack[sp].kix = kix;
630 sp++;
631
632 /* begin again with the new keyring */
633 subtree = key;
634 goto descend;
635 }
636 }
637
638 /* the keyring we're looking at was disqualified or didn't contain a
639 * matching key */
640 not_this_keyring:
1da177e4
LT
641 if (sp > 0) {
642 /* resume the checking of a keyring higher up in the tree */
643 sp--;
76d8aeab 644 keylist = stack[sp].keylist;
1da177e4
LT
645 kix = stack[sp].kix + 1;
646 goto ascend;
647 }
648
649 ret = 0; /* no cycles detected */
650
651 error:
76d8aeab 652 rcu_read_unlock();
1da177e4
LT
653 return ret;
654
655 too_deep:
656 ret = -ELOOP;
76d8aeab
DH
657 goto error;
658
1da177e4
LT
659 cycle_detected:
660 ret = -EDEADLK;
1da177e4
LT
661 goto error;
662
663} /* end keyring_detect_cycle() */
664
76d8aeab
DH
665/*****************************************************************************/
666/*
667 * dispose of a keyring list after the RCU grace period
668 */
669static void keyring_link_rcu_disposal(struct rcu_head *rcu)
670{
671 struct keyring_list *klist =
672 container_of(rcu, struct keyring_list, rcu);
673
674 kfree(klist);
675
676} /* end keyring_link_rcu_disposal() */
677
cab8eb59
DH
678/*****************************************************************************/
679/*
680 * dispose of a keyring list after the RCU grace period, freeing the unlinked
681 * key
682 */
683static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
684{
685 struct keyring_list *klist =
686 container_of(rcu, struct keyring_list, rcu);
687
688 key_put(klist->keys[klist->delkey]);
689 kfree(klist);
690
691} /* end keyring_unlink_rcu_disposal() */
692
1da177e4
LT
693/*****************************************************************************/
694/*
695 * link a key into to a keyring
76d8aeab 696 * - must be called with the keyring's semaphore write-locked
cab8eb59 697 * - discard already extant link to matching key if there is one
1da177e4
LT
698 */
699int __key_link(struct key *keyring, struct key *key)
700{
701 struct keyring_list *klist, *nklist;
702 unsigned max;
703 size_t size;
cab8eb59 704 int loop, ret;
1da177e4
LT
705
706 ret = -EKEYREVOKED;
76d8aeab 707 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1da177e4
LT
708 goto error;
709
710 ret = -ENOTDIR;
711 if (keyring->type != &key_type_keyring)
712 goto error;
713
714 /* serialise link/link calls to prevent parallel calls causing a
715 * cycle when applied to two keyring in opposite orders */
716 down_write(&keyring_serialise_link_sem);
717
718 /* check that we aren't going to create a cycle adding one keyring to
719 * another */
720 if (key->type == &key_type_keyring) {
721 ret = keyring_detect_cycle(keyring, key);
722 if (ret < 0)
723 goto error2;
724 }
725
cab8eb59 726 /* see if there's a matching key we can displace */
f0641cba 727 klist = rcu_dereference_locked_keyring(keyring);
cab8eb59
DH
728 if (klist && klist->nkeys > 0) {
729 struct key_type *type = key->type;
730
731 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
732 if (klist->keys[loop]->type == type &&
733 strcmp(klist->keys[loop]->description,
734 key->description) == 0
735 ) {
736 /* found a match - replace with new key */
737 size = sizeof(struct key *) * klist->maxkeys;
738 size += sizeof(*klist);
739 BUG_ON(size > PAGE_SIZE);
740
741 ret = -ENOMEM;
48ad504e 742 nklist = kmemdup(klist, size, GFP_KERNEL);
cab8eb59
DH
743 if (!nklist)
744 goto error2;
745
cab8eb59
DH
746 /* replace matched key */
747 atomic_inc(&key->usage);
748 nklist->keys[loop] = key;
749
750 rcu_assign_pointer(
751 keyring->payload.subscriptions,
752 nklist);
753
754 /* dispose of the old keyring list and the
755 * displaced key */
756 klist->delkey = loop;
757 call_rcu(&klist->rcu,
758 keyring_unlink_rcu_disposal);
759
760 goto done;
761 }
762 }
763 }
764
1da177e4
LT
765 /* check that we aren't going to overrun the user's quota */
766 ret = key_payload_reserve(keyring,
767 keyring->datalen + KEYQUOTA_LINK_BYTES);
768 if (ret < 0)
769 goto error2;
770
1da177e4
LT
771 if (klist && klist->nkeys < klist->maxkeys) {
772 /* there's sufficient slack space to add directly */
773 atomic_inc(&key->usage);
774
76d8aeab
DH
775 klist->keys[klist->nkeys] = key;
776 smp_wmb();
777 klist->nkeys++;
778 smp_wmb();
1da177e4
LT
779 }
780 else {
781 /* grow the key list */
782 max = 4;
783 if (klist)
784 max += klist->maxkeys;
785
786 ret = -ENFILE;
76d8aeab
DH
787 if (max > 65535)
788 goto error3;
a4014d8f 789 size = sizeof(*klist) + sizeof(struct key *) * max;
1da177e4
LT
790 if (size > PAGE_SIZE)
791 goto error3;
792
793 ret = -ENOMEM;
794 nklist = kmalloc(size, GFP_KERNEL);
795 if (!nklist)
796 goto error3;
797 nklist->maxkeys = max;
798 nklist->nkeys = 0;
799
800 if (klist) {
801 nklist->nkeys = klist->nkeys;
802 memcpy(nklist->keys,
803 klist->keys,
804 sizeof(struct key *) * klist->nkeys);
805 }
806
807 /* add the key into the new space */
808 atomic_inc(&key->usage);
1da177e4 809 nklist->keys[nklist->nkeys++] = key;
76d8aeab
DH
810
811 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
1da177e4
LT
812
813 /* dispose of the old keyring list */
76d8aeab
DH
814 if (klist)
815 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
1da177e4
LT
816 }
817
cab8eb59
DH
818done:
819 ret = 0;
820error2:
1da177e4 821 up_write(&keyring_serialise_link_sem);
cab8eb59 822error:
1da177e4
LT
823 return ret;
824
cab8eb59 825error3:
1da177e4
LT
826 /* undo the quota changes */
827 key_payload_reserve(keyring,
828 keyring->datalen - KEYQUOTA_LINK_BYTES);
829 goto error2;
830
831} /* end __key_link() */
832
833/*****************************************************************************/
834/*
835 * link a key to a keyring
836 */
837int key_link(struct key *keyring, struct key *key)
838{
839 int ret;
840
841 key_check(keyring);
842 key_check(key);
843
844 down_write(&keyring->sem);
845 ret = __key_link(keyring, key);
846 up_write(&keyring->sem);
847
848 return ret;
849
850} /* end key_link() */
851
852EXPORT_SYMBOL(key_link);
853
854/*****************************************************************************/
855/*
856 * unlink the first link to a key from a keyring
857 */
858int key_unlink(struct key *keyring, struct key *key)
859{
76d8aeab 860 struct keyring_list *klist, *nklist;
1da177e4
LT
861 int loop, ret;
862
863 key_check(keyring);
864 key_check(key);
865
866 ret = -ENOTDIR;
867 if (keyring->type != &key_type_keyring)
868 goto error;
869
870 down_write(&keyring->sem);
871
f0641cba 872 klist = rcu_dereference_locked_keyring(keyring);
1da177e4
LT
873 if (klist) {
874 /* search the keyring for the key */
875 for (loop = 0; loop < klist->nkeys; loop++)
876 if (klist->keys[loop] == key)
877 goto key_is_present;
878 }
879
880 up_write(&keyring->sem);
881 ret = -ENOENT;
882 goto error;
883
76d8aeab
DH
884key_is_present:
885 /* we need to copy the key list for RCU purposes */
a4014d8f
DH
886 nklist = kmalloc(sizeof(*klist) +
887 sizeof(struct key *) * klist->maxkeys,
76d8aeab
DH
888 GFP_KERNEL);
889 if (!nklist)
890 goto nomem;
891 nklist->maxkeys = klist->maxkeys;
892 nklist->nkeys = klist->nkeys - 1;
893
894 if (loop > 0)
895 memcpy(&nklist->keys[0],
896 &klist->keys[0],
a4014d8f 897 loop * sizeof(struct key *));
76d8aeab
DH
898
899 if (loop < nklist->nkeys)
900 memcpy(&nklist->keys[loop],
901 &klist->keys[loop + 1],
a4014d8f 902 (nklist->nkeys - loop) * sizeof(struct key *));
76d8aeab 903
1da177e4
LT
904 /* adjust the user's quota */
905 key_payload_reserve(keyring,
906 keyring->datalen - KEYQUOTA_LINK_BYTES);
907
76d8aeab 908 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
1da177e4 909
76d8aeab 910 up_write(&keyring->sem);
1da177e4 911
76d8aeab
DH
912 /* schedule for later cleanup */
913 klist->delkey = loop;
914 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
1da177e4 915
1da177e4
LT
916 ret = 0;
917
76d8aeab 918error:
1da177e4 919 return ret;
76d8aeab
DH
920nomem:
921 ret = -ENOMEM;
922 up_write(&keyring->sem);
923 goto error;
1da177e4
LT
924
925} /* end key_unlink() */
926
927EXPORT_SYMBOL(key_unlink);
928
76d8aeab
DH
929/*****************************************************************************/
930/*
931 * dispose of a keyring list after the RCU grace period, releasing the keys it
932 * links to
933 */
934static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
935{
936 struct keyring_list *klist;
937 int loop;
938
939 klist = container_of(rcu, struct keyring_list, rcu);
940
941 for (loop = klist->nkeys - 1; loop >= 0; loop--)
942 key_put(klist->keys[loop]);
943
944 kfree(klist);
945
946} /* end keyring_clear_rcu_disposal() */
947
1da177e4
LT
948/*****************************************************************************/
949/*
950 * clear the specified process keyring
951 * - implements keyctl(KEYCTL_CLEAR)
952 */
953int keyring_clear(struct key *keyring)
954{
955 struct keyring_list *klist;
76d8aeab 956 int ret;
1da177e4
LT
957
958 ret = -ENOTDIR;
959 if (keyring->type == &key_type_keyring) {
960 /* detach the pointer block with the locks held */
961 down_write(&keyring->sem);
962
f0641cba 963 klist = rcu_dereference_locked_keyring(keyring);
1da177e4
LT
964 if (klist) {
965 /* adjust the quota */
966 key_payload_reserve(keyring,
967 sizeof(struct keyring_list));
968
76d8aeab
DH
969 rcu_assign_pointer(keyring->payload.subscriptions,
970 NULL);
1da177e4
LT
971 }
972
973 up_write(&keyring->sem);
974
975 /* free the keys after the locks have been dropped */
76d8aeab
DH
976 if (klist)
977 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1da177e4
LT
978
979 ret = 0;
980 }
981
982 return ret;
983
984} /* end keyring_clear() */
985
986EXPORT_SYMBOL(keyring_clear);
31204ed9
DH
987
988/*****************************************************************************/
989/*
990 * dispose of the links from a revoked keyring
991 * - called with the key sem write-locked
992 */
993static void keyring_revoke(struct key *keyring)
994{
f0641cba
DH
995 struct keyring_list *klist;
996
997 klist = rcu_dereference_locked_keyring(keyring);
31204ed9
DH
998
999 /* adjust the quota */
1000 key_payload_reserve(keyring, 0);
1001
1002 if (klist) {
1003 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1004 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1005 }
1006
1007} /* end keyring_revoke() */
5d135440
DH
1008
1009/*
1010 * Determine whether a key is dead
1011 */
1012static bool key_is_dead(struct key *key, time_t limit)
1013{
1014 return test_bit(KEY_FLAG_DEAD, &key->flags) ||
1015 (key->expiry > 0 && key->expiry <= limit);
1016}
1017
1018/*
1019 * Collect garbage from the contents of a keyring
1020 */
1021void keyring_gc(struct key *keyring, time_t limit)
1022{
1023 struct keyring_list *klist, *new;
1024 struct key *key;
1025 int loop, keep, max;
1026
c08ef808 1027 kenter("{%x,%s}", key_serial(keyring), keyring->description);
5d135440
DH
1028
1029 down_write(&keyring->sem);
1030
f0641cba 1031 klist = rcu_dereference_locked_keyring(keyring);
5d135440 1032 if (!klist)
c08ef808 1033 goto no_klist;
5d135440
DH
1034
1035 /* work out how many subscriptions we're keeping */
1036 keep = 0;
1037 for (loop = klist->nkeys - 1; loop >= 0; loop--)
c08ef808 1038 if (!key_is_dead(klist->keys[loop], limit))
5d135440
DH
1039 keep++;
1040
1041 if (keep == klist->nkeys)
1042 goto just_return;
1043
1044 /* allocate a new keyring payload */
1045 max = roundup(keep, 4);
1046 new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
1047 GFP_KERNEL);
1048 if (!new)
c08ef808 1049 goto nomem;
5d135440
DH
1050 new->maxkeys = max;
1051 new->nkeys = 0;
1052 new->delkey = 0;
1053
1054 /* install the live keys
1055 * - must take care as expired keys may be updated back to life
1056 */
1057 keep = 0;
1058 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
1059 key = klist->keys[loop];
1060 if (!key_is_dead(key, limit)) {
1061 if (keep >= max)
1062 goto discard_new;
1063 new->keys[keep++] = key_get(key);
1064 }
1065 }
1066 new->nkeys = keep;
1067
1068 /* adjust the quota */
1069 key_payload_reserve(keyring,
1070 sizeof(struct keyring_list) +
1071 KEYQUOTA_LINK_BYTES * keep);
1072
1073 if (keep == 0) {
1074 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1075 kfree(new);
1076 } else {
1077 rcu_assign_pointer(keyring->payload.subscriptions, new);
1078 }
1079
1080 up_write(&keyring->sem);
1081
1082 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1083 kleave(" [yes]");
1084 return;
1085
1086discard_new:
1087 new->nkeys = keep;
1088 keyring_clear_rcu_disposal(&new->rcu);
c08ef808
DH
1089 up_write(&keyring->sem);
1090 kleave(" [discard]");
1091 return;
1092
5d135440
DH
1093just_return:
1094 up_write(&keyring->sem);
c08ef808
DH
1095 kleave(" [no dead]");
1096 return;
1097
1098no_klist:
1099 up_write(&keyring->sem);
1100 kleave(" [no_klist]");
1101 return;
1102
1103nomem:
1104 up_write(&keyring->sem);
1105 kleave(" [oom]");
5d135440 1106}