hangcheck-timer: fix x86_32 bugs
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
512ea3bc 20#include <linux/uaccess.h>
1da177e4
LT
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++)
c5b60b5e 47 bucket += (unsigned char)*desc;
1da177e4
LT
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
c8563473 178 if (keyring->description)
1da177e4 179 seq_puts(m, keyring->description);
c8563473 180 else
1da177e4 181 seq_puts(m, "[anon]");
1da177e4 182
76d8aeab
DH
183 rcu_read_lock();
184 klist = rcu_dereference(keyring->payload.subscriptions);
1da177e4
LT
185 if (klist)
186 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
187 else
188 seq_puts(m, ": empty");
76d8aeab 189 rcu_read_unlock();
1da177e4
LT
190
191} /* end keyring_describe() */
192
193/*****************************************************************************/
194/*
195 * read a list of key IDs from the keyring's contents
76d8aeab 196 * - the keyring's semaphore is read-locked
1da177e4
LT
197 */
198static long keyring_read(const struct key *keyring,
199 char __user *buffer, size_t buflen)
200{
201 struct keyring_list *klist;
202 struct key *key;
203 size_t qty, tmp;
204 int loop, ret;
205
206 ret = 0;
f0641cba 207 klist = rcu_dereference_locked_keyring(keyring);
1da177e4
LT
208 if (klist) {
209 /* calculate how much data we could return */
210 qty = klist->nkeys * sizeof(key_serial_t);
211
212 if (buffer && buflen > 0) {
213 if (buflen > qty)
214 buflen = qty;
215
216 /* copy the IDs of the subscribed keys into the
217 * buffer */
218 ret = -EFAULT;
219
220 for (loop = 0; loop < klist->nkeys; loop++) {
221 key = klist->keys[loop];
222
223 tmp = sizeof(key_serial_t);
224 if (tmp > buflen)
225 tmp = buflen;
226
227 if (copy_to_user(buffer,
228 &key->serial,
229 tmp) != 0)
230 goto error;
231
232 buflen -= tmp;
233 if (buflen == 0)
234 break;
235 buffer += tmp;
236 }
237 }
238
239 ret = qty;
240 }
241
c5b60b5e 242error:
1da177e4
LT
243 return ret;
244
245} /* end keyring_read() */
246
247/*****************************************************************************/
248/*
249 * allocate a keyring and link into the destination keyring
250 */
251struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
d84f4f99 252 const struct cred *cred, unsigned long flags,
d720024e 253 struct key *dest)
1da177e4
LT
254{
255 struct key *keyring;
256 int ret;
257
258 keyring = key_alloc(&key_type_keyring, description,
d84f4f99 259 uid, gid, cred,
29db9190 260 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
7e047ef5 261 flags);
1da177e4
LT
262
263 if (!IS_ERR(keyring)) {
3e30148c 264 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
1da177e4
LT
265 if (ret < 0) {
266 key_put(keyring);
267 keyring = ERR_PTR(ret);
268 }
269 }
270
271 return keyring;
272
273} /* end keyring_alloc() */
274
275/*****************************************************************************/
276/*
277 * search the supplied keyring tree for a key that matches the criterion
278 * - perform a breadth-then-depth search up to the prescribed limit
279 * - we only find keys on which we have search permission
280 * - we use the supplied match function to see if the description (or other
281 * feature of interest) matches
3e30148c 282 * - we rely on RCU to prevent the keyring lists from disappearing on us
1da177e4
LT
283 * - we return -EAGAIN if we didn't find any matching key
284 * - we return -ENOKEY if we only found negative matching keys
664cceb0 285 * - we propagate the possession attribute from the keyring ref to the key ref
1da177e4 286 */
664cceb0 287key_ref_t keyring_search_aux(key_ref_t keyring_ref,
d84f4f99 288 const struct cred *cred,
664cceb0
DH
289 struct key_type *type,
290 const void *description,
291 key_match_func_t match)
1da177e4
LT
292{
293 struct {
76d8aeab 294 struct keyring_list *keylist;
1da177e4
LT
295 int kix;
296 } stack[KEYRING_SEARCH_MAX_DEPTH];
297
298 struct keyring_list *keylist;
299 struct timespec now;
dceba994 300 unsigned long possessed, kflags;
664cceb0
DH
301 struct key *keyring, *key;
302 key_ref_t key_ref;
1da177e4 303 long err;
76d8aeab 304 int sp, kix;
1da177e4 305
664cceb0
DH
306 keyring = key_ref_to_ptr(keyring_ref);
307 possessed = is_key_possessed(keyring_ref);
1da177e4
LT
308 key_check(keyring);
309
310 /* top keyring must have search permission to begin the search */
512ea3bc 311 err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
29db9190
DH
312 if (err < 0) {
313 key_ref = ERR_PTR(err);
1da177e4 314 goto error;
29db9190 315 }
1da177e4 316
664cceb0 317 key_ref = ERR_PTR(-ENOTDIR);
1da177e4
LT
318 if (keyring->type != &key_type_keyring)
319 goto error;
320
664cceb0
DH
321 rcu_read_lock();
322
1da177e4
LT
323 now = current_kernel_time();
324 err = -EAGAIN;
325 sp = 0;
326
dceba994
KC
327 /* firstly we should check to see if this top-level keyring is what we
328 * are looking for */
329 key_ref = ERR_PTR(-EAGAIN);
330 kflags = keyring->flags;
331 if (keyring->type == type && match(keyring, description)) {
332 key = keyring;
333
334 /* check it isn't negative and hasn't expired or been
335 * revoked */
336 if (kflags & (1 << KEY_FLAG_REVOKED))
337 goto error_2;
338 if (key->expiry && now.tv_sec >= key->expiry)
339 goto error_2;
340 key_ref = ERR_PTR(-ENOKEY);
341 if (kflags & (1 << KEY_FLAG_NEGATIVE))
342 goto error_2;
343 goto found;
344 }
345
346 /* otherwise, the top keyring must not be revoked, expired, or
347 * negatively instantiated if we are to search it */
348 key_ref = ERR_PTR(-EAGAIN);
349 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
350 (keyring->expiry && now.tv_sec >= keyring->expiry))
351 goto error_2;
352
1da177e4 353 /* start processing a new keyring */
664cceb0 354descend:
76d8aeab 355 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1da177e4
LT
356 goto not_this_keyring;
357
76d8aeab 358 keylist = rcu_dereference(keyring->payload.subscriptions);
1da177e4
LT
359 if (!keylist)
360 goto not_this_keyring;
361
362 /* iterate through the keys in this keyring first */
363 for (kix = 0; kix < keylist->nkeys; kix++) {
364 key = keylist->keys[kix];
dceba994 365 kflags = key->flags;
1da177e4
LT
366
367 /* ignore keys not of this type */
368 if (key->type != type)
369 continue;
370
371 /* skip revoked keys and expired keys */
dceba994 372 if (kflags & (1 << KEY_FLAG_REVOKED))
1da177e4
LT
373 continue;
374
375 if (key->expiry && now.tv_sec >= key->expiry)
376 continue;
377
378 /* keys that don't match */
379 if (!match(key, description))
380 continue;
381
382 /* key must have search permissions */
29db9190 383 if (key_task_permission(make_key_ref(key, possessed),
d84f4f99 384 cred, KEY_SEARCH) < 0)
1da177e4
LT
385 continue;
386
dceba994
KC
387 /* we set a different error code if we pass a negative key */
388 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
1da177e4
LT
389 err = -ENOKEY;
390 continue;
391 }
392
393 goto found;
394 }
395
396 /* search through the keyrings nested in this one */
397 kix = 0;
664cceb0 398ascend:
76d8aeab 399 for (; kix < keylist->nkeys; kix++) {
1da177e4
LT
400 key = keylist->keys[kix];
401 if (key->type != &key_type_keyring)
76d8aeab 402 continue;
1da177e4
LT
403
404 /* recursively search nested keyrings
405 * - only search keyrings for which we have search permission
406 */
407 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
76d8aeab 408 continue;
1da177e4 409
0f6ed7c2 410 if (key_task_permission(make_key_ref(key, possessed),
d84f4f99 411 cred, KEY_SEARCH) < 0)
76d8aeab 412 continue;
1da177e4
LT
413
414 /* stack the current position */
76d8aeab 415 stack[sp].keylist = keylist;
1da177e4
LT
416 stack[sp].kix = kix;
417 sp++;
418
419 /* begin again with the new keyring */
420 keyring = key;
421 goto descend;
1da177e4
LT
422 }
423
424 /* the keyring we're looking at was disqualified or didn't contain a
425 * matching key */
664cceb0 426not_this_keyring:
1da177e4
LT
427 if (sp > 0) {
428 /* resume the processing of a keyring higher up in the tree */
429 sp--;
76d8aeab 430 keylist = stack[sp].keylist;
1da177e4
LT
431 kix = stack[sp].kix + 1;
432 goto ascend;
433 }
434
664cceb0
DH
435 key_ref = ERR_PTR(err);
436 goto error_2;
1da177e4
LT
437
438 /* we found a viable match */
664cceb0 439found:
1da177e4 440 atomic_inc(&key->usage);
1da177e4 441 key_check(key);
664cceb0
DH
442 key_ref = make_key_ref(key, possessed);
443error_2:
76d8aeab 444 rcu_read_unlock();
664cceb0
DH
445error:
446 return key_ref;
1da177e4
LT
447
448} /* end keyring_search_aux() */
449
450/*****************************************************************************/
451/*
452 * search the supplied keyring tree for a key that matches the criterion
453 * - perform a breadth-then-depth search up to the prescribed limit
454 * - we only find keys on which we have search permission
455 * - we readlock the keyrings as we search down the tree
456 * - we return -EAGAIN if we didn't find any matching key
457 * - we return -ENOKEY if we only found negative matching keys
458 */
664cceb0
DH
459key_ref_t keyring_search(key_ref_t keyring,
460 struct key_type *type,
461 const char *description)
1da177e4 462{
3e30148c
DH
463 if (!type->match)
464 return ERR_PTR(-ENOKEY);
465
d84f4f99 466 return keyring_search_aux(keyring, current->cred,
3e30148c 467 type, description, type->match);
1da177e4
LT
468
469} /* end keyring_search() */
470
471EXPORT_SYMBOL(keyring_search);
472
473/*****************************************************************************/
474/*
475 * search the given keyring only (no recursion)
476 * - keyring must be locked by caller
c3a9d654 477 * - caller must guarantee that the keyring is a keyring
1da177e4 478 */
664cceb0
DH
479key_ref_t __keyring_search_one(key_ref_t keyring_ref,
480 const struct key_type *ktype,
481 const char *description,
482 key_perm_t perm)
1da177e4
LT
483{
484 struct keyring_list *klist;
664cceb0
DH
485 unsigned long possessed;
486 struct key *keyring, *key;
1da177e4
LT
487 int loop;
488
664cceb0
DH
489 keyring = key_ref_to_ptr(keyring_ref);
490 possessed = is_key_possessed(keyring_ref);
491
76d8aeab
DH
492 rcu_read_lock();
493
494 klist = rcu_dereference(keyring->payload.subscriptions);
1da177e4
LT
495 if (klist) {
496 for (loop = 0; loop < klist->nkeys; loop++) {
497 key = klist->keys[loop];
498
499 if (key->type == ktype &&
3e30148c
DH
500 (!key->type->match ||
501 key->type->match(key, description)) &&
664cceb0 502 key_permission(make_key_ref(key, possessed),
db1d1d57 503 perm) == 0 &&
76d8aeab 504 !test_bit(KEY_FLAG_REVOKED, &key->flags)
1da177e4
LT
505 )
506 goto found;
507 }
508 }
509
664cceb0
DH
510 rcu_read_unlock();
511 return ERR_PTR(-ENOKEY);
1da177e4 512
c5b60b5e 513found:
1da177e4 514 atomic_inc(&key->usage);
76d8aeab 515 rcu_read_unlock();
664cceb0 516 return make_key_ref(key, possessed);
1da177e4
LT
517
518} /* end __keyring_search_one() */
519
520/*****************************************************************************/
521/*
522 * find a keyring with the specified name
523 * - all named keyrings are searched
69664cf1 524 * - normally only finds keyrings with search permission for the current process
1da177e4 525 */
69664cf1 526struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
1da177e4
LT
527{
528 struct key *keyring;
529 int bucket;
530
1da177e4 531 if (!name)
cea7daa3 532 return ERR_PTR(-EINVAL);
1da177e4
LT
533
534 bucket = keyring_hash(name);
535
536 read_lock(&keyring_name_lock);
537
538 if (keyring_name_hash[bucket].next) {
539 /* search this hash bucket for a keyring with a matching name
540 * that's readable and that hasn't been revoked */
541 list_for_each_entry(keyring,
542 &keyring_name_hash[bucket],
543 type_data.link
544 ) {
2ea190d0
SH
545 if (keyring->user->user_ns != current_user_ns())
546 continue;
547
76d8aeab 548 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1da177e4
LT
549 continue;
550
551 if (strcmp(keyring->description, name) != 0)
552 continue;
553
69664cf1
DH
554 if (!skip_perm_check &&
555 key_permission(make_key_ref(keyring, 0),
0f6ed7c2 556 KEY_SEARCH) < 0)
1da177e4
LT
557 continue;
558
cea7daa3
TO
559 /* we've got a match but we might end up racing with
560 * key_cleanup() if the keyring is currently 'dead'
561 * (ie. it has a zero usage count) */
562 if (!atomic_inc_not_zero(&keyring->usage))
563 continue;
564 goto out;
1da177e4
LT
565 }
566 }
567
1da177e4 568 keyring = ERR_PTR(-ENOKEY);
cea7daa3
TO
569out:
570 read_unlock(&keyring_name_lock);
1da177e4
LT
571 return keyring;
572
573} /* end find_keyring_by_name() */
574
575/*****************************************************************************/
576/*
577 * see if a cycle will will be created by inserting acyclic tree B in acyclic
578 * tree A at the topmost level (ie: as a direct child of A)
579 * - since we are adding B to A at the top level, checking for cycles should
580 * just be a matter of seeing if node A is somewhere in tree B
581 */
582static int keyring_detect_cycle(struct key *A, struct key *B)
583{
584 struct {
76d8aeab 585 struct keyring_list *keylist;
1da177e4
LT
586 int kix;
587 } stack[KEYRING_SEARCH_MAX_DEPTH];
588
589 struct keyring_list *keylist;
590 struct key *subtree, *key;
591 int sp, kix, ret;
592
76d8aeab
DH
593 rcu_read_lock();
594
1da177e4
LT
595 ret = -EDEADLK;
596 if (A == B)
76d8aeab 597 goto cycle_detected;
1da177e4
LT
598
599 subtree = B;
600 sp = 0;
601
602 /* start processing a new keyring */
c5b60b5e 603descend:
76d8aeab 604 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
1da177e4
LT
605 goto not_this_keyring;
606
76d8aeab 607 keylist = rcu_dereference(subtree->payload.subscriptions);
1da177e4
LT
608 if (!keylist)
609 goto not_this_keyring;
610 kix = 0;
611
c5b60b5e 612ascend:
1da177e4
LT
613 /* iterate through the remaining keys in this keyring */
614 for (; kix < keylist->nkeys; kix++) {
615 key = keylist->keys[kix];
616
617 if (key == A)
618 goto cycle_detected;
619
620 /* recursively check nested keyrings */
621 if (key->type == &key_type_keyring) {
622 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
623 goto too_deep;
624
625 /* stack the current position */
76d8aeab 626 stack[sp].keylist = keylist;
1da177e4
LT
627 stack[sp].kix = kix;
628 sp++;
629
630 /* begin again with the new keyring */
631 subtree = key;
632 goto descend;
633 }
634 }
635
636 /* the keyring we're looking at was disqualified or didn't contain a
637 * matching key */
c5b60b5e 638not_this_keyring:
1da177e4
LT
639 if (sp > 0) {
640 /* resume the checking of a keyring higher up in the tree */
641 sp--;
76d8aeab 642 keylist = stack[sp].keylist;
1da177e4
LT
643 kix = stack[sp].kix + 1;
644 goto ascend;
645 }
646
647 ret = 0; /* no cycles detected */
648
c5b60b5e 649error:
76d8aeab 650 rcu_read_unlock();
1da177e4
LT
651 return ret;
652
c5b60b5e 653too_deep:
1da177e4 654 ret = -ELOOP;
76d8aeab
DH
655 goto error;
656
c5b60b5e 657cycle_detected:
1da177e4 658 ret = -EDEADLK;
1da177e4
LT
659 goto error;
660
661} /* end keyring_detect_cycle() */
662
cab8eb59
DH
663/*
664 * dispose of a keyring list after the RCU grace period, freeing the unlinked
665 * key
666 */
667static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
668{
669 struct keyring_list *klist =
670 container_of(rcu, struct keyring_list, rcu);
671
f70e2e06
DH
672 if (klist->delkey != USHORT_MAX)
673 key_put(klist->keys[klist->delkey]);
cab8eb59 674 kfree(klist);
f70e2e06 675}
cab8eb59 676
1da177e4 677/*
f70e2e06 678 * preallocate memory so that a key can be linked into to a keyring
1da177e4 679 */
f70e2e06
DH
680int __key_link_begin(struct key *keyring, const struct key_type *type,
681 const char *description,
682 struct keyring_list **_prealloc)
683 __acquires(&keyring->sem)
1da177e4
LT
684{
685 struct keyring_list *klist, *nklist;
686 unsigned max;
687 size_t size;
cab8eb59 688 int loop, ret;
1da177e4 689
f70e2e06 690 kenter("%d,%s,%s,", key_serial(keyring), type->name, description);
1da177e4 691
1da177e4 692 if (keyring->type != &key_type_keyring)
f70e2e06
DH
693 return -ENOTDIR;
694
695 down_write(&keyring->sem);
696
697 ret = -EKEYREVOKED;
698 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
699 goto error_krsem;
1da177e4 700
f70e2e06
DH
701 /* serialise link/link calls to prevent parallel calls causing a cycle
702 * when linking two keyring in opposite orders */
703 if (type == &key_type_keyring)
553d603c
DH
704 down_write(&keyring_serialise_link_sem);
705
f70e2e06 706 klist = rcu_dereference_locked_keyring(keyring);
1da177e4 707
cab8eb59 708 /* see if there's a matching key we can displace */
cab8eb59 709 if (klist && klist->nkeys > 0) {
cab8eb59
DH
710 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
711 if (klist->keys[loop]->type == type &&
712 strcmp(klist->keys[loop]->description,
f70e2e06 713 description) == 0
cab8eb59 714 ) {
f70e2e06
DH
715 /* found a match - we'll replace this one with
716 * the new key */
cab8eb59
DH
717 size = sizeof(struct key *) * klist->maxkeys;
718 size += sizeof(*klist);
719 BUG_ON(size > PAGE_SIZE);
720
721 ret = -ENOMEM;
48ad504e 722 nklist = kmemdup(klist, size, GFP_KERNEL);
cab8eb59 723 if (!nklist)
f70e2e06 724 goto error_sem;
cab8eb59 725
f70e2e06
DH
726 /* note replacement slot */
727 klist->delkey = nklist->delkey = loop;
cab8eb59
DH
728 goto done;
729 }
730 }
731 }
732
1da177e4
LT
733 /* check that we aren't going to overrun the user's quota */
734 ret = key_payload_reserve(keyring,
735 keyring->datalen + KEYQUOTA_LINK_BYTES);
736 if (ret < 0)
f70e2e06 737 goto error_sem;
1da177e4 738
1da177e4 739 if (klist && klist->nkeys < klist->maxkeys) {
f70e2e06
DH
740 /* there's sufficient slack space to append directly */
741 nklist = NULL;
512ea3bc 742 } else {
1da177e4
LT
743 /* grow the key list */
744 max = 4;
745 if (klist)
746 max += klist->maxkeys;
747
748 ret = -ENFILE;
f70e2e06
DH
749 if (max > USHORT_MAX - 1)
750 goto error_quota;
a4014d8f 751 size = sizeof(*klist) + sizeof(struct key *) * max;
1da177e4 752 if (size > PAGE_SIZE)
f70e2e06 753 goto error_quota;
1da177e4
LT
754
755 ret = -ENOMEM;
756 nklist = kmalloc(size, GFP_KERNEL);
757 if (!nklist)
f70e2e06 758 goto error_quota;
1da177e4 759
f70e2e06 760 nklist->maxkeys = max;
1da177e4 761 if (klist) {
f70e2e06 762 memcpy(nklist->keys, klist->keys,
1da177e4 763 sizeof(struct key *) * klist->nkeys);
f70e2e06
DH
764 nklist->delkey = klist->nkeys;
765 nklist->nkeys = klist->nkeys + 1;
766 klist->delkey = USHORT_MAX;
767 } else {
768 nklist->nkeys = 1;
769 nklist->delkey = 0;
1da177e4
LT
770 }
771
772 /* add the key into the new space */
f70e2e06 773 nklist->keys[nklist->delkey] = NULL;
1da177e4
LT
774 }
775
cab8eb59 776done:
f70e2e06
DH
777 *_prealloc = nklist;
778 kleave(" = 0");
779 return 0;
1da177e4 780
f70e2e06 781error_quota:
1da177e4
LT
782 /* undo the quota changes */
783 key_payload_reserve(keyring,
784 keyring->datalen - KEYQUOTA_LINK_BYTES);
f70e2e06
DH
785error_sem:
786 if (type == &key_type_keyring)
787 up_write(&keyring_serialise_link_sem);
788error_krsem:
789 up_write(&keyring->sem);
790 kleave(" = %d", ret);
791 return ret;
792}
1da177e4 793
f70e2e06
DH
794/*
795 * check already instantiated keys aren't going to be a problem
796 * - the caller must have called __key_link_begin()
797 * - don't need to call this for keys that were created since __key_link_begin()
798 * was called
799 */
800int __key_link_check_live_key(struct key *keyring, struct key *key)
801{
802 if (key->type == &key_type_keyring)
803 /* check that we aren't going to create a cycle by linking one
804 * keyring to another */
805 return keyring_detect_cycle(keyring, key);
806 return 0;
807}
808
809/*
810 * link a key into to a keyring
811 * - must be called with __key_link_begin() having being called
812 * - discard already extant link to matching key if there is one
813 */
814void __key_link(struct key *keyring, struct key *key,
815 struct keyring_list **_prealloc)
816{
817 struct keyring_list *klist, *nklist;
818
819 nklist = *_prealloc;
820 *_prealloc = NULL;
821
822 kenter("%d,%d,%p", keyring->serial, key->serial, nklist);
823
824 klist = rcu_dereference_protected(keyring->payload.subscriptions,
825 rwsem_is_locked(&keyring->sem));
826
827 atomic_inc(&key->usage);
828
829 /* there's a matching key we can displace or an empty slot in a newly
830 * allocated list we can fill */
831 if (nklist) {
832 kdebug("replace %hu/%hu/%hu",
833 nklist->delkey, nklist->nkeys, nklist->maxkeys);
834
835 nklist->keys[nklist->delkey] = key;
836
837 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
838
839 /* dispose of the old keyring list and, if there was one, the
840 * displaced key */
841 if (klist) {
842 kdebug("dispose %hu/%hu/%hu",
843 klist->delkey, klist->nkeys, klist->maxkeys);
844 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
845 }
846 } else {
847 /* there's sufficient slack space to append directly */
848 klist->keys[klist->nkeys] = key;
849 smp_wmb();
850 klist->nkeys++;
851 }
852}
853
854/*
855 * finish linking a key into to a keyring
856 * - must be called with __key_link_begin() having being called
857 */
858void __key_link_end(struct key *keyring, struct key_type *type,
859 struct keyring_list *prealloc)
860 __releases(&keyring->sem)
861{
862 BUG_ON(type == NULL);
863 BUG_ON(type->name == NULL);
864 kenter("%d,%s,%p", keyring->serial, type->name, prealloc);
865
866 if (type == &key_type_keyring)
867 up_write(&keyring_serialise_link_sem);
868
869 if (prealloc) {
870 kfree(prealloc);
871 key_payload_reserve(keyring,
872 keyring->datalen - KEYQUOTA_LINK_BYTES);
873 }
874 up_write(&keyring->sem);
875}
1da177e4 876
1da177e4
LT
877/*
878 * link a key to a keyring
879 */
880int key_link(struct key *keyring, struct key *key)
881{
f70e2e06 882 struct keyring_list *prealloc;
1da177e4
LT
883 int ret;
884
885 key_check(keyring);
886 key_check(key);
887
f70e2e06
DH
888 ret = __key_link_begin(keyring, key->type, key->description, &prealloc);
889 if (ret == 0) {
890 ret = __key_link_check_live_key(keyring, key);
891 if (ret == 0)
892 __key_link(keyring, key, &prealloc);
893 __key_link_end(keyring, key->type, prealloc);
894 }
1da177e4
LT
895
896 return ret;
f70e2e06 897}
1da177e4
LT
898
899EXPORT_SYMBOL(key_link);
900
901/*****************************************************************************/
902/*
903 * unlink the first link to a key from a keyring
904 */
905int key_unlink(struct key *keyring, struct key *key)
906{
76d8aeab 907 struct keyring_list *klist, *nklist;
1da177e4
LT
908 int loop, ret;
909
910 key_check(keyring);
911 key_check(key);
912
913 ret = -ENOTDIR;
914 if (keyring->type != &key_type_keyring)
915 goto error;
916
917 down_write(&keyring->sem);
918
f0641cba 919 klist = rcu_dereference_locked_keyring(keyring);
1da177e4
LT
920 if (klist) {
921 /* search the keyring for the key */
922 for (loop = 0; loop < klist->nkeys; loop++)
923 if (klist->keys[loop] == key)
924 goto key_is_present;
925 }
926
927 up_write(&keyring->sem);
928 ret = -ENOENT;
929 goto error;
930
76d8aeab
DH
931key_is_present:
932 /* we need to copy the key list for RCU purposes */
a4014d8f
DH
933 nklist = kmalloc(sizeof(*klist) +
934 sizeof(struct key *) * klist->maxkeys,
76d8aeab
DH
935 GFP_KERNEL);
936 if (!nklist)
937 goto nomem;
938 nklist->maxkeys = klist->maxkeys;
939 nklist->nkeys = klist->nkeys - 1;
940
941 if (loop > 0)
942 memcpy(&nklist->keys[0],
943 &klist->keys[0],
a4014d8f 944 loop * sizeof(struct key *));
76d8aeab
DH
945
946 if (loop < nklist->nkeys)
947 memcpy(&nklist->keys[loop],
948 &klist->keys[loop + 1],
a4014d8f 949 (nklist->nkeys - loop) * sizeof(struct key *));
76d8aeab 950
1da177e4
LT
951 /* adjust the user's quota */
952 key_payload_reserve(keyring,
953 keyring->datalen - KEYQUOTA_LINK_BYTES);
954
76d8aeab 955 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
1da177e4 956
76d8aeab 957 up_write(&keyring->sem);
1da177e4 958
76d8aeab
DH
959 /* schedule for later cleanup */
960 klist->delkey = loop;
961 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
1da177e4 962
1da177e4
LT
963 ret = 0;
964
76d8aeab 965error:
1da177e4 966 return ret;
76d8aeab
DH
967nomem:
968 ret = -ENOMEM;
969 up_write(&keyring->sem);
970 goto error;
1da177e4
LT
971
972} /* end key_unlink() */
973
974EXPORT_SYMBOL(key_unlink);
975
76d8aeab
DH
976/*****************************************************************************/
977/*
978 * dispose of a keyring list after the RCU grace period, releasing the keys it
979 * links to
980 */
981static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
982{
983 struct keyring_list *klist;
984 int loop;
985
986 klist = container_of(rcu, struct keyring_list, rcu);
987
988 for (loop = klist->nkeys - 1; loop >= 0; loop--)
989 key_put(klist->keys[loop]);
990
991 kfree(klist);
992
993} /* end keyring_clear_rcu_disposal() */
994
1da177e4
LT
995/*****************************************************************************/
996/*
997 * clear the specified process keyring
998 * - implements keyctl(KEYCTL_CLEAR)
999 */
1000int keyring_clear(struct key *keyring)
1001{
1002 struct keyring_list *klist;
76d8aeab 1003 int ret;
1da177e4
LT
1004
1005 ret = -ENOTDIR;
1006 if (keyring->type == &key_type_keyring) {
1007 /* detach the pointer block with the locks held */
1008 down_write(&keyring->sem);
1009
f0641cba 1010 klist = rcu_dereference_locked_keyring(keyring);
1da177e4
LT
1011 if (klist) {
1012 /* adjust the quota */
1013 key_payload_reserve(keyring,
1014 sizeof(struct keyring_list));
1015
76d8aeab
DH
1016 rcu_assign_pointer(keyring->payload.subscriptions,
1017 NULL);
1da177e4
LT
1018 }
1019
1020 up_write(&keyring->sem);
1021
1022 /* free the keys after the locks have been dropped */
76d8aeab
DH
1023 if (klist)
1024 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1da177e4
LT
1025
1026 ret = 0;
1027 }
1028
1029 return ret;
1030
1031} /* end keyring_clear() */
1032
1033EXPORT_SYMBOL(keyring_clear);
31204ed9
DH
1034
1035/*****************************************************************************/
1036/*
1037 * dispose of the links from a revoked keyring
1038 * - called with the key sem write-locked
1039 */
1040static void keyring_revoke(struct key *keyring)
1041{
f0641cba
DH
1042 struct keyring_list *klist;
1043
1044 klist = rcu_dereference_locked_keyring(keyring);
31204ed9
DH
1045
1046 /* adjust the quota */
1047 key_payload_reserve(keyring, 0);
1048
1049 if (klist) {
1050 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1051 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1052 }
1053
1054} /* end keyring_revoke() */
5d135440
DH
1055
1056/*
1057 * Determine whether a key is dead
1058 */
1059static bool key_is_dead(struct key *key, time_t limit)
1060{
1061 return test_bit(KEY_FLAG_DEAD, &key->flags) ||
1062 (key->expiry > 0 && key->expiry <= limit);
1063}
1064
1065/*
1066 * Collect garbage from the contents of a keyring
1067 */
1068void keyring_gc(struct key *keyring, time_t limit)
1069{
1070 struct keyring_list *klist, *new;
1071 struct key *key;
1072 int loop, keep, max;
1073
c08ef808 1074 kenter("{%x,%s}", key_serial(keyring), keyring->description);
5d135440
DH
1075
1076 down_write(&keyring->sem);
1077
f0641cba 1078 klist = rcu_dereference_locked_keyring(keyring);
5d135440 1079 if (!klist)
c08ef808 1080 goto no_klist;
5d135440
DH
1081
1082 /* work out how many subscriptions we're keeping */
1083 keep = 0;
1084 for (loop = klist->nkeys - 1; loop >= 0; loop--)
c08ef808 1085 if (!key_is_dead(klist->keys[loop], limit))
5d135440
DH
1086 keep++;
1087
1088 if (keep == klist->nkeys)
1089 goto just_return;
1090
1091 /* allocate a new keyring payload */
1092 max = roundup(keep, 4);
1093 new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
1094 GFP_KERNEL);
1095 if (!new)
c08ef808 1096 goto nomem;
5d135440
DH
1097 new->maxkeys = max;
1098 new->nkeys = 0;
1099 new->delkey = 0;
1100
1101 /* install the live keys
1102 * - must take care as expired keys may be updated back to life
1103 */
1104 keep = 0;
1105 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
1106 key = klist->keys[loop];
1107 if (!key_is_dead(key, limit)) {
1108 if (keep >= max)
1109 goto discard_new;
1110 new->keys[keep++] = key_get(key);
1111 }
1112 }
1113 new->nkeys = keep;
1114
1115 /* adjust the quota */
1116 key_payload_reserve(keyring,
1117 sizeof(struct keyring_list) +
1118 KEYQUOTA_LINK_BYTES * keep);
1119
1120 if (keep == 0) {
1121 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1122 kfree(new);
1123 } else {
1124 rcu_assign_pointer(keyring->payload.subscriptions, new);
1125 }
1126
1127 up_write(&keyring->sem);
1128
1129 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1130 kleave(" [yes]");
1131 return;
1132
1133discard_new:
1134 new->nkeys = keep;
1135 keyring_clear_rcu_disposal(&new->rcu);
c08ef808
DH
1136 up_write(&keyring->sem);
1137 kleave(" [discard]");
1138 return;
1139
5d135440
DH
1140just_return:
1141 up_write(&keyring->sem);
c08ef808
DH
1142 kleave(" [no dead]");
1143 return;
1144
1145no_klist:
1146 up_write(&keyring->sem);
1147 kleave(" [no_klist]");
1148 return;
1149
1150nomem:
1151 up_write(&keyring->sem);
1152 kleave(" [oom]");
5d135440 1153}