blob: 09d92d52ef752e94bbde64c62bb5f298256bd806 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* keyring.c: keyring handling
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>
David Howells29db9192005-10-30 15:02:44 -080016#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/seq_file.h>
18#include <linux/err.h>
19#include <asm/uaccess.h>
20#include "internal.h"
21
22/*
23 * when plumbing the depths of the key tree, this sets a hard limit set on how
24 * deep we're willing to go
25 */
26#define KEYRING_SEARCH_MAX_DEPTH 6
27
28/*
29 * we keep all named keyrings in a hash to speed looking them up
30 */
31#define KEYRING_NAME_HASH_SIZE (1 << 5)
32
33static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
34static DEFINE_RWLOCK(keyring_name_lock);
35
36static inline unsigned keyring_hash(const char *desc)
37{
38 unsigned bucket = 0;
39
40 for (; *desc; desc++)
41 bucket += (unsigned char) *desc;
42
43 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
44}
45
46/*
47 * the keyring type definition
48 */
49static int keyring_instantiate(struct key *keyring,
50 const void *data, size_t datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int keyring_match(const struct key *keyring, const void *criterion);
52static void keyring_destroy(struct key *keyring);
53static void keyring_describe(const struct key *keyring, struct seq_file *m);
54static long keyring_read(const struct key *keyring,
55 char __user *buffer, size_t buflen);
56
57struct key_type key_type_keyring = {
58 .name = "keyring",
59 .def_datalen = sizeof(struct keyring_list),
60 .instantiate = keyring_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .match = keyring_match,
62 .destroy = keyring_destroy,
63 .describe = keyring_describe,
64 .read = keyring_read,
65};
66
67/*
68 * semaphore to serialise link/link calls to prevent two link calls in parallel
69 * introducing a cycle
70 */
Adrian Bunk1ae8f402006-01-06 00:11:25 -080071static DECLARE_RWSEM(keyring_serialise_link_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*****************************************************************************/
74/*
75 * publish the name of a keyring so that it can be found by name (if it has
76 * one)
77 */
78void keyring_publish_name(struct key *keyring)
79{
80 int bucket;
81
82 if (keyring->description) {
83 bucket = keyring_hash(keyring->description);
84
85 write_lock(&keyring_name_lock);
86
87 if (!keyring_name_hash[bucket].next)
88 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
89
90 list_add_tail(&keyring->type_data.link,
91 &keyring_name_hash[bucket]);
92
93 write_unlock(&keyring_name_lock);
94 }
95
96} /* end keyring_publish_name() */
97
98/*****************************************************************************/
99/*
100 * initialise a keyring
101 * - we object if we were given any data
102 */
103static int keyring_instantiate(struct key *keyring,
104 const void *data, size_t datalen)
105{
106 int ret;
107
108 ret = -EINVAL;
109 if (datalen == 0) {
110 /* make the keyring available by name if it has one */
111 keyring_publish_name(keyring);
112 ret = 0;
113 }
114
115 return ret;
116
117} /* end keyring_instantiate() */
118
119/*****************************************************************************/
120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * match keyrings on their name
122 */
123static int keyring_match(const struct key *keyring, const void *description)
124{
125 return keyring->description &&
126 strcmp(keyring->description, description) == 0;
127
128} /* end keyring_match() */
129
130/*****************************************************************************/
131/*
132 * dispose of the data dangling from the corpse of a keyring
133 */
134static void keyring_destroy(struct key *keyring)
135{
136 struct keyring_list *klist;
137 int loop;
138
139 if (keyring->description) {
140 write_lock(&keyring_name_lock);
David Howells94efe722005-08-04 13:07:07 -0700141
142 if (keyring->type_data.link.next != NULL &&
143 !list_empty(&keyring->type_data.link))
144 list_del(&keyring->type_data.link);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 write_unlock(&keyring_name_lock);
147 }
148
David Howells76d8aea2005-06-23 22:00:49 -0700149 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (klist) {
151 for (loop = klist->nkeys - 1; loop >= 0; loop--)
152 key_put(klist->keys[loop]);
153 kfree(klist);
154 }
155
156} /* end keyring_destroy() */
157
158/*****************************************************************************/
159/*
160 * describe the keyring
161 */
162static void keyring_describe(const struct key *keyring, struct seq_file *m)
163{
164 struct keyring_list *klist;
165
166 if (keyring->description) {
167 seq_puts(m, keyring->description);
168 }
169 else {
170 seq_puts(m, "[anon]");
171 }
172
David Howells76d8aea2005-06-23 22:00:49 -0700173 rcu_read_lock();
174 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (klist)
176 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
177 else
178 seq_puts(m, ": empty");
David Howells76d8aea2005-06-23 22:00:49 -0700179 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181} /* end keyring_describe() */
182
183/*****************************************************************************/
184/*
185 * read a list of key IDs from the keyring's contents
David Howells76d8aea2005-06-23 22:00:49 -0700186 * - the keyring's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188static long keyring_read(const struct key *keyring,
189 char __user *buffer, size_t buflen)
190{
191 struct keyring_list *klist;
192 struct key *key;
193 size_t qty, tmp;
194 int loop, ret;
195
196 ret = 0;
David Howells76d8aea2005-06-23 22:00:49 -0700197 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (klist) {
200 /* calculate how much data we could return */
201 qty = klist->nkeys * sizeof(key_serial_t);
202
203 if (buffer && buflen > 0) {
204 if (buflen > qty)
205 buflen = qty;
206
207 /* copy the IDs of the subscribed keys into the
208 * buffer */
209 ret = -EFAULT;
210
211 for (loop = 0; loop < klist->nkeys; loop++) {
212 key = klist->keys[loop];
213
214 tmp = sizeof(key_serial_t);
215 if (tmp > buflen)
216 tmp = buflen;
217
218 if (copy_to_user(buffer,
219 &key->serial,
220 tmp) != 0)
221 goto error;
222
223 buflen -= tmp;
224 if (buflen == 0)
225 break;
226 buffer += tmp;
227 }
228 }
229
230 ret = qty;
231 }
232
233 error:
234 return ret;
235
236} /* end keyring_read() */
237
238/*****************************************************************************/
239/*
240 * allocate a keyring and link into the destination keyring
241 */
242struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
243 int not_in_quota, struct key *dest)
244{
245 struct key *keyring;
246 int ret;
247
248 keyring = key_alloc(&key_type_keyring, description,
David Howells29db9192005-10-30 15:02:44 -0800249 uid, gid,
250 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
251 not_in_quota);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (!IS_ERR(keyring)) {
David Howells3e301482005-06-23 22:00:56 -0700254 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ret < 0) {
256 key_put(keyring);
257 keyring = ERR_PTR(ret);
258 }
259 }
260
261 return keyring;
262
263} /* end keyring_alloc() */
264
265/*****************************************************************************/
266/*
267 * search the supplied keyring tree for a key that matches the criterion
268 * - perform a breadth-then-depth search up to the prescribed limit
269 * - we only find keys on which we have search permission
270 * - we use the supplied match function to see if the description (or other
271 * feature of interest) matches
David Howells3e301482005-06-23 22:00:56 -0700272 * - we rely on RCU to prevent the keyring lists from disappearing on us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * - we return -EAGAIN if we didn't find any matching key
274 * - we return -ENOKEY if we only found negative matching keys
David Howells664cceb2005-09-28 17:03:15 +0100275 * - we propagate the possession attribute from the keyring ref to the key ref
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
David Howells664cceb2005-09-28 17:03:15 +0100277key_ref_t keyring_search_aux(key_ref_t keyring_ref,
278 struct task_struct *context,
279 struct key_type *type,
280 const void *description,
281 key_match_func_t match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700284 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int kix;
286 } stack[KEYRING_SEARCH_MAX_DEPTH];
287
288 struct keyring_list *keylist;
289 struct timespec now;
David Howells664cceb2005-09-28 17:03:15 +0100290 unsigned long possessed;
291 struct key *keyring, *key;
292 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 long err;
David Howells76d8aea2005-06-23 22:00:49 -0700294 int sp, kix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
David Howells664cceb2005-09-28 17:03:15 +0100296 keyring = key_ref_to_ptr(keyring_ref);
297 possessed = is_key_possessed(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 key_check(keyring);
299
300 /* top keyring must have search permission to begin the search */
David Howells29db9192005-10-30 15:02:44 -0800301 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
302 if (err < 0) {
303 key_ref = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto error;
David Howells29db9192005-10-30 15:02:44 -0800305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Howells664cceb2005-09-28 17:03:15 +0100307 key_ref = ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (keyring->type != &key_type_keyring)
309 goto error;
310
David Howells664cceb2005-09-28 17:03:15 +0100311 rcu_read_lock();
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 now = current_kernel_time();
314 err = -EAGAIN;
315 sp = 0;
316
317 /* start processing a new keyring */
David Howells664cceb2005-09-28 17:03:15 +0100318descend:
David Howells76d8aea2005-06-23 22:00:49 -0700319 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto not_this_keyring;
321
David Howells76d8aea2005-06-23 22:00:49 -0700322 keylist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!keylist)
324 goto not_this_keyring;
325
326 /* iterate through the keys in this keyring first */
327 for (kix = 0; kix < keylist->nkeys; kix++) {
328 key = keylist->keys[kix];
329
330 /* ignore keys not of this type */
331 if (key->type != type)
332 continue;
333
334 /* skip revoked keys and expired keys */
David Howells76d8aea2005-06-23 22:00:49 -0700335 if (test_bit(KEY_FLAG_REVOKED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 continue;
337
338 if (key->expiry && now.tv_sec >= key->expiry)
339 continue;
340
341 /* keys that don't match */
342 if (!match(key, description))
343 continue;
344
345 /* key must have search permissions */
David Howells29db9192005-10-30 15:02:44 -0800346 if (key_task_permission(make_key_ref(key, possessed),
347 context, KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 continue;
349
350 /* we set a different error code if we find a negative key */
David Howells76d8aea2005-06-23 22:00:49 -0700351 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 err = -ENOKEY;
353 continue;
354 }
355
356 goto found;
357 }
358
359 /* search through the keyrings nested in this one */
360 kix = 0;
David Howells664cceb2005-09-28 17:03:15 +0100361ascend:
David Howells76d8aea2005-06-23 22:00:49 -0700362 for (; kix < keylist->nkeys; kix++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 key = keylist->keys[kix];
364 if (key->type != &key_type_keyring)
David Howells76d8aea2005-06-23 22:00:49 -0700365 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* recursively search nested keyrings
368 * - only search keyrings for which we have search permission
369 */
370 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
David Howells76d8aea2005-06-23 22:00:49 -0700371 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Howells0f6ed7c2005-11-07 00:59:30 -0800373 if (key_task_permission(make_key_ref(key, possessed),
374 context, KEY_SEARCH) < 0)
David Howells76d8aea2005-06-23 22:00:49 -0700375 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700378 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 stack[sp].kix = kix;
380 sp++;
381
382 /* begin again with the new keyring */
383 keyring = key;
384 goto descend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
387 /* the keyring we're looking at was disqualified or didn't contain a
388 * matching key */
David Howells664cceb2005-09-28 17:03:15 +0100389not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (sp > 0) {
391 /* resume the processing of a keyring higher up in the tree */
392 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700393 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 kix = stack[sp].kix + 1;
395 goto ascend;
396 }
397
David Howells664cceb2005-09-28 17:03:15 +0100398 key_ref = ERR_PTR(err);
399 goto error_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* we found a viable match */
David Howells664cceb2005-09-28 17:03:15 +0100402found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 key_check(key);
David Howells664cceb2005-09-28 17:03:15 +0100405 key_ref = make_key_ref(key, possessed);
406error_2:
David Howells76d8aea2005-06-23 22:00:49 -0700407 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100408error:
409 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411} /* end keyring_search_aux() */
412
413/*****************************************************************************/
414/*
415 * search the supplied keyring tree for a key that matches the criterion
416 * - perform a breadth-then-depth search up to the prescribed limit
417 * - we only find keys on which we have search permission
418 * - we readlock the keyrings as we search down the tree
419 * - we return -EAGAIN if we didn't find any matching key
420 * - we return -ENOKEY if we only found negative matching keys
421 */
David Howells664cceb2005-09-28 17:03:15 +0100422key_ref_t keyring_search(key_ref_t keyring,
423 struct key_type *type,
424 const char *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
David Howells3e301482005-06-23 22:00:56 -0700426 if (!type->match)
427 return ERR_PTR(-ENOKEY);
428
429 return keyring_search_aux(keyring, current,
430 type, description, type->match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432} /* end keyring_search() */
433
434EXPORT_SYMBOL(keyring_search);
435
436/*****************************************************************************/
437/*
438 * search the given keyring only (no recursion)
439 * - keyring must be locked by caller
440 */
David Howells664cceb2005-09-28 17:03:15 +0100441key_ref_t __keyring_search_one(key_ref_t keyring_ref,
442 const struct key_type *ktype,
443 const char *description,
444 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct keyring_list *klist;
David Howells664cceb2005-09-28 17:03:15 +0100447 unsigned long possessed;
448 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int loop;
450
David Howells664cceb2005-09-28 17:03:15 +0100451 keyring = key_ref_to_ptr(keyring_ref);
452 possessed = is_key_possessed(keyring_ref);
453
David Howells76d8aea2005-06-23 22:00:49 -0700454 rcu_read_lock();
455
456 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (klist) {
458 for (loop = 0; loop < klist->nkeys; loop++) {
459 key = klist->keys[loop];
460
461 if (key->type == ktype &&
David Howells3e301482005-06-23 22:00:56 -0700462 (!key->type->match ||
463 key->type->match(key, description)) &&
David Howells664cceb2005-09-28 17:03:15 +0100464 key_permission(make_key_ref(key, possessed),
David Howellsdb1d1d52005-12-01 00:51:18 -0800465 perm) == 0 &&
David Howells76d8aea2005-06-23 22:00:49 -0700466 !test_bit(KEY_FLAG_REVOKED, &key->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 )
468 goto found;
469 }
470 }
471
David Howells664cceb2005-09-28 17:03:15 +0100472 rcu_read_unlock();
473 return ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 found:
476 atomic_inc(&key->usage);
David Howells76d8aea2005-06-23 22:00:49 -0700477 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100478 return make_key_ref(key, possessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480} /* end __keyring_search_one() */
481
482/*****************************************************************************/
483/*
David Howells3e301482005-06-23 22:00:56 -0700484 * search for an instantiation authorisation key matching a target key
485 * - the RCU read lock must be held by the caller
486 * - a target_id of zero specifies any valid token
487 */
488struct key *keyring_search_instkey(struct key *keyring,
489 key_serial_t target_id)
490{
491 struct request_key_auth *rka;
492 struct keyring_list *klist;
493 struct key *instkey;
494 int loop;
495
496 klist = rcu_dereference(keyring->payload.subscriptions);
497 if (klist) {
498 for (loop = 0; loop < klist->nkeys; loop++) {
499 instkey = klist->keys[loop];
500
501 if (instkey->type != &key_type_request_key_auth)
502 continue;
503
504 rka = instkey->payload.data;
505 if (target_id && rka->target_key->serial != target_id)
506 continue;
507
508 /* the auth key is revoked during instantiation */
509 if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
510 goto found;
511
512 instkey = ERR_PTR(-EKEYREVOKED);
513 goto error;
514 }
515 }
516
517 instkey = ERR_PTR(-EACCES);
518 goto error;
519
520found:
521 atomic_inc(&instkey->usage);
522error:
523 return instkey;
524
525} /* end keyring_search_instkey() */
526
527/*****************************************************************************/
528/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * find a keyring with the specified name
530 * - all named keyrings are searched
531 * - only find keyrings with search permission for the process
532 * - only find keyrings with a serial number greater than the one specified
533 */
534struct key *find_keyring_by_name(const char *name, key_serial_t bound)
535{
536 struct key *keyring;
537 int bucket;
538
539 keyring = ERR_PTR(-EINVAL);
540 if (!name)
541 goto error;
542
543 bucket = keyring_hash(name);
544
545 read_lock(&keyring_name_lock);
546
547 if (keyring_name_hash[bucket].next) {
548 /* search this hash bucket for a keyring with a matching name
549 * that's readable and that hasn't been revoked */
550 list_for_each_entry(keyring,
551 &keyring_name_hash[bucket],
552 type_data.link
553 ) {
David Howells76d8aea2005-06-23 22:00:49 -0700554 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 continue;
556
557 if (strcmp(keyring->description, name) != 0)
558 continue;
559
David Howells0f6ed7c2005-11-07 00:59:30 -0800560 if (key_permission(make_key_ref(keyring, 0),
561 KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 continue;
563
564 /* found a potential candidate, but we still need to
565 * check the serial number */
566 if (keyring->serial <= bound)
567 continue;
568
569 /* we've got a match */
570 atomic_inc(&keyring->usage);
571 read_unlock(&keyring_name_lock);
572 goto error;
573 }
574 }
575
576 read_unlock(&keyring_name_lock);
577 keyring = ERR_PTR(-ENOKEY);
578
579 error:
580 return keyring;
581
582} /* end find_keyring_by_name() */
583
584/*****************************************************************************/
585/*
586 * see if a cycle will will be created by inserting acyclic tree B in acyclic
587 * tree A at the topmost level (ie: as a direct child of A)
588 * - since we are adding B to A at the top level, checking for cycles should
589 * just be a matter of seeing if node A is somewhere in tree B
590 */
591static int keyring_detect_cycle(struct key *A, struct key *B)
592{
593 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700594 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int kix;
596 } stack[KEYRING_SEARCH_MAX_DEPTH];
597
598 struct keyring_list *keylist;
599 struct key *subtree, *key;
600 int sp, kix, ret;
601
David Howells76d8aea2005-06-23 22:00:49 -0700602 rcu_read_lock();
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ret = -EDEADLK;
605 if (A == B)
David Howells76d8aea2005-06-23 22:00:49 -0700606 goto cycle_detected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 subtree = B;
609 sp = 0;
610
611 /* start processing a new keyring */
612 descend:
David Howells76d8aea2005-06-23 22:00:49 -0700613 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto not_this_keyring;
615
David Howells76d8aea2005-06-23 22:00:49 -0700616 keylist = rcu_dereference(subtree->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!keylist)
618 goto not_this_keyring;
619 kix = 0;
620
621 ascend:
622 /* iterate through the remaining keys in this keyring */
623 for (; kix < keylist->nkeys; kix++) {
624 key = keylist->keys[kix];
625
626 if (key == A)
627 goto cycle_detected;
628
629 /* recursively check nested keyrings */
630 if (key->type == &key_type_keyring) {
631 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
632 goto too_deep;
633
634 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700635 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 stack[sp].kix = kix;
637 sp++;
638
639 /* begin again with the new keyring */
640 subtree = key;
641 goto descend;
642 }
643 }
644
645 /* the keyring we're looking at was disqualified or didn't contain a
646 * matching key */
647 not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (sp > 0) {
649 /* resume the checking of a keyring higher up in the tree */
650 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700651 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 kix = stack[sp].kix + 1;
653 goto ascend;
654 }
655
656 ret = 0; /* no cycles detected */
657
658 error:
David Howells76d8aea2005-06-23 22:00:49 -0700659 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661
662 too_deep:
663 ret = -ELOOP;
David Howells76d8aea2005-06-23 22:00:49 -0700664 goto error;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 cycle_detected:
667 ret = -EDEADLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto error;
669
670} /* end keyring_detect_cycle() */
671
672/*****************************************************************************/
673/*
David Howells76d8aea2005-06-23 22:00:49 -0700674 * dispose of a keyring list after the RCU grace period
675 */
676static void keyring_link_rcu_disposal(struct rcu_head *rcu)
677{
678 struct keyring_list *klist =
679 container_of(rcu, struct keyring_list, rcu);
680
681 kfree(klist);
682
683} /* end keyring_link_rcu_disposal() */
684
685/*****************************************************************************/
686/*
David Howellscab8eb52006-01-08 01:02:45 -0800687 * dispose of a keyring list after the RCU grace period, freeing the unlinked
688 * key
689 */
690static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
691{
692 struct keyring_list *klist =
693 container_of(rcu, struct keyring_list, rcu);
694
695 key_put(klist->keys[klist->delkey]);
696 kfree(klist);
697
698} /* end keyring_unlink_rcu_disposal() */
699
700/*****************************************************************************/
701/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * link a key into to a keyring
David Howells76d8aea2005-06-23 22:00:49 -0700703 * - must be called with the keyring's semaphore write-locked
David Howellscab8eb52006-01-08 01:02:45 -0800704 * - discard already extant link to matching key if there is one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
706int __key_link(struct key *keyring, struct key *key)
707{
708 struct keyring_list *klist, *nklist;
709 unsigned max;
710 size_t size;
David Howellscab8eb52006-01-08 01:02:45 -0800711 int loop, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 ret = -EKEYREVOKED;
David Howells76d8aea2005-06-23 22:00:49 -0700714 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto error;
716
717 ret = -ENOTDIR;
718 if (keyring->type != &key_type_keyring)
719 goto error;
720
721 /* serialise link/link calls to prevent parallel calls causing a
722 * cycle when applied to two keyring in opposite orders */
723 down_write(&keyring_serialise_link_sem);
724
725 /* check that we aren't going to create a cycle adding one keyring to
726 * another */
727 if (key->type == &key_type_keyring) {
728 ret = keyring_detect_cycle(keyring, key);
729 if (ret < 0)
730 goto error2;
731 }
732
David Howellscab8eb52006-01-08 01:02:45 -0800733 /* see if there's a matching key we can displace */
734 klist = keyring->payload.subscriptions;
735
736 if (klist && klist->nkeys > 0) {
737 struct key_type *type = key->type;
738
739 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
740 if (klist->keys[loop]->type == type &&
741 strcmp(klist->keys[loop]->description,
742 key->description) == 0
743 ) {
744 /* found a match - replace with new key */
745 size = sizeof(struct key *) * klist->maxkeys;
746 size += sizeof(*klist);
747 BUG_ON(size > PAGE_SIZE);
748
749 ret = -ENOMEM;
750 nklist = kmalloc(size, GFP_KERNEL);
751 if (!nklist)
752 goto error2;
753
754 memcpy(nklist, klist, size);
755
756 /* replace matched key */
757 atomic_inc(&key->usage);
758 nklist->keys[loop] = key;
759
760 rcu_assign_pointer(
761 keyring->payload.subscriptions,
762 nklist);
763
764 /* dispose of the old keyring list and the
765 * displaced key */
766 klist->delkey = loop;
767 call_rcu(&klist->rcu,
768 keyring_unlink_rcu_disposal);
769
770 goto done;
771 }
772 }
773 }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* check that we aren't going to overrun the user's quota */
776 ret = key_payload_reserve(keyring,
777 keyring->datalen + KEYQUOTA_LINK_BYTES);
778 if (ret < 0)
779 goto error2;
780
781 klist = keyring->payload.subscriptions;
782
783 if (klist && klist->nkeys < klist->maxkeys) {
784 /* there's sufficient slack space to add directly */
785 atomic_inc(&key->usage);
786
David Howells76d8aea2005-06-23 22:00:49 -0700787 klist->keys[klist->nkeys] = key;
788 smp_wmb();
789 klist->nkeys++;
790 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 else {
793 /* grow the key list */
794 max = 4;
795 if (klist)
796 max += klist->maxkeys;
797
798 ret = -ENFILE;
David Howells76d8aea2005-06-23 22:00:49 -0700799 if (max > 65535)
800 goto error3;
David Howellsa4014d82005-07-07 17:57:03 -0700801 size = sizeof(*klist) + sizeof(struct key *) * max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (size > PAGE_SIZE)
803 goto error3;
804
805 ret = -ENOMEM;
806 nklist = kmalloc(size, GFP_KERNEL);
807 if (!nklist)
808 goto error3;
809 nklist->maxkeys = max;
810 nklist->nkeys = 0;
811
812 if (klist) {
813 nklist->nkeys = klist->nkeys;
814 memcpy(nklist->keys,
815 klist->keys,
816 sizeof(struct key *) * klist->nkeys);
817 }
818
819 /* add the key into the new space */
820 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 nklist->keys[nklist->nkeys++] = key;
David Howells76d8aea2005-06-23 22:00:49 -0700822
823 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* dispose of the old keyring list */
David Howells76d8aea2005-06-23 22:00:49 -0700826 if (klist)
827 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
David Howellscab8eb52006-01-08 01:02:45 -0800830done:
831 ret = 0;
832error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 up_write(&keyring_serialise_link_sem);
David Howellscab8eb52006-01-08 01:02:45 -0800834error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
836
David Howellscab8eb52006-01-08 01:02:45 -0800837error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* undo the quota changes */
839 key_payload_reserve(keyring,
840 keyring->datalen - KEYQUOTA_LINK_BYTES);
841 goto error2;
842
843} /* end __key_link() */
844
845/*****************************************************************************/
846/*
847 * link a key to a keyring
848 */
849int key_link(struct key *keyring, struct key *key)
850{
851 int ret;
852
853 key_check(keyring);
854 key_check(key);
855
856 down_write(&keyring->sem);
857 ret = __key_link(keyring, key);
858 up_write(&keyring->sem);
859
860 return ret;
861
862} /* end key_link() */
863
864EXPORT_SYMBOL(key_link);
865
866/*****************************************************************************/
867/*
868 * unlink the first link to a key from a keyring
869 */
870int key_unlink(struct key *keyring, struct key *key)
871{
David Howells76d8aea2005-06-23 22:00:49 -0700872 struct keyring_list *klist, *nklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int loop, ret;
874
875 key_check(keyring);
876 key_check(key);
877
878 ret = -ENOTDIR;
879 if (keyring->type != &key_type_keyring)
880 goto error;
881
882 down_write(&keyring->sem);
883
884 klist = keyring->payload.subscriptions;
885 if (klist) {
886 /* search the keyring for the key */
887 for (loop = 0; loop < klist->nkeys; loop++)
888 if (klist->keys[loop] == key)
889 goto key_is_present;
890 }
891
892 up_write(&keyring->sem);
893 ret = -ENOENT;
894 goto error;
895
David Howells76d8aea2005-06-23 22:00:49 -0700896key_is_present:
897 /* we need to copy the key list for RCU purposes */
David Howellsa4014d82005-07-07 17:57:03 -0700898 nklist = kmalloc(sizeof(*klist) +
899 sizeof(struct key *) * klist->maxkeys,
David Howells76d8aea2005-06-23 22:00:49 -0700900 GFP_KERNEL);
901 if (!nklist)
902 goto nomem;
903 nklist->maxkeys = klist->maxkeys;
904 nklist->nkeys = klist->nkeys - 1;
905
906 if (loop > 0)
907 memcpy(&nklist->keys[0],
908 &klist->keys[0],
David Howellsa4014d82005-07-07 17:57:03 -0700909 loop * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700910
911 if (loop < nklist->nkeys)
912 memcpy(&nklist->keys[loop],
913 &klist->keys[loop + 1],
David Howellsa4014d82005-07-07 17:57:03 -0700914 (nklist->nkeys - loop) * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* adjust the user's quota */
917 key_payload_reserve(keyring,
918 keyring->datalen - KEYQUOTA_LINK_BYTES);
919
David Howells76d8aea2005-06-23 22:00:49 -0700920 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 up_write(&keyring->sem);
David Howells76d8aea2005-06-23 22:00:49 -0700923
924 /* schedule for later cleanup */
925 klist->delkey = loop;
926 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 ret = 0;
929
David Howells76d8aea2005-06-23 22:00:49 -0700930error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return ret;
David Howells76d8aea2005-06-23 22:00:49 -0700932nomem:
933 ret = -ENOMEM;
934 up_write(&keyring->sem);
935 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937} /* end key_unlink() */
938
939EXPORT_SYMBOL(key_unlink);
940
941/*****************************************************************************/
942/*
David Howells76d8aea2005-06-23 22:00:49 -0700943 * dispose of a keyring list after the RCU grace period, releasing the keys it
944 * links to
945 */
946static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
947{
948 struct keyring_list *klist;
949 int loop;
950
951 klist = container_of(rcu, struct keyring_list, rcu);
952
953 for (loop = klist->nkeys - 1; loop >= 0; loop--)
954 key_put(klist->keys[loop]);
955
956 kfree(klist);
957
958} /* end keyring_clear_rcu_disposal() */
959
960/*****************************************************************************/
961/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * clear the specified process keyring
963 * - implements keyctl(KEYCTL_CLEAR)
964 */
965int keyring_clear(struct key *keyring)
966{
967 struct keyring_list *klist;
David Howells76d8aea2005-06-23 22:00:49 -0700968 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 ret = -ENOTDIR;
971 if (keyring->type == &key_type_keyring) {
972 /* detach the pointer block with the locks held */
973 down_write(&keyring->sem);
974
975 klist = keyring->payload.subscriptions;
976 if (klist) {
977 /* adjust the quota */
978 key_payload_reserve(keyring,
979 sizeof(struct keyring_list));
980
David Howells76d8aea2005-06-23 22:00:49 -0700981 rcu_assign_pointer(keyring->payload.subscriptions,
982 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
985 up_write(&keyring->sem);
986
987 /* free the keys after the locks have been dropped */
David Howells76d8aea2005-06-23 22:00:49 -0700988 if (klist)
989 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 ret = 0;
992 }
993
994 return ret;
995
996} /* end keyring_clear() */
997
998EXPORT_SYMBOL(keyring_clear);