blob: 4e9fa8be44b8e7071c75cb9f03ea5c2cb6d819bd [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);
51static int keyring_duplicate(struct key *keyring, const struct key *source);
52static int keyring_match(const struct key *keyring, const void *criterion);
53static void keyring_destroy(struct key *keyring);
54static void keyring_describe(const struct key *keyring, struct seq_file *m);
55static long keyring_read(const struct key *keyring,
56 char __user *buffer, size_t buflen);
57
58struct key_type key_type_keyring = {
59 .name = "keyring",
60 .def_datalen = sizeof(struct keyring_list),
61 .instantiate = keyring_instantiate,
62 .duplicate = keyring_duplicate,
63 .match = keyring_match,
64 .destroy = keyring_destroy,
65 .describe = keyring_describe,
66 .read = keyring_read,
67};
68
69/*
70 * semaphore to serialise link/link calls to prevent two link calls in parallel
71 * introducing a cycle
72 */
73DECLARE_RWSEM(keyring_serialise_link_sem);
74
75/*****************************************************************************/
76/*
77 * publish the name of a keyring so that it can be found by name (if it has
78 * one)
79 */
80void keyring_publish_name(struct key *keyring)
81{
82 int bucket;
83
84 if (keyring->description) {
85 bucket = keyring_hash(keyring->description);
86
87 write_lock(&keyring_name_lock);
88
89 if (!keyring_name_hash[bucket].next)
90 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
91
92 list_add_tail(&keyring->type_data.link,
93 &keyring_name_hash[bucket]);
94
95 write_unlock(&keyring_name_lock);
96 }
97
98} /* end keyring_publish_name() */
99
100/*****************************************************************************/
101/*
102 * initialise a keyring
103 * - we object if we were given any data
104 */
105static int keyring_instantiate(struct key *keyring,
106 const void *data, size_t datalen)
107{
108 int ret;
109
110 ret = -EINVAL;
111 if (datalen == 0) {
112 /* make the keyring available by name if it has one */
113 keyring_publish_name(keyring);
114 ret = 0;
115 }
116
117 return ret;
118
119} /* end keyring_instantiate() */
120
121/*****************************************************************************/
122/*
123 * duplicate the list of subscribed keys from a source keyring into this one
124 */
125static int keyring_duplicate(struct key *keyring, const struct key *source)
126{
127 struct keyring_list *sklist, *klist;
128 unsigned max;
129 size_t size;
130 int loop, ret;
131
132 const unsigned limit =
David Howellsa4014d82005-07-07 17:57:03 -0700133 (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David Howells76d8aea2005-06-23 22:00:49 -0700137 /* find out how many keys are currently linked */
138 rcu_read_lock();
139 sklist = rcu_dereference(source->payload.subscriptions);
140 max = 0;
141 if (sklist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 max = sklist->nkeys;
David Howells76d8aea2005-06-23 22:00:49 -0700143 rcu_read_unlock();
144
145 /* allocate a new payload and stuff load with key links */
146 if (max > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 BUG_ON(max > limit);
148
149 max = (max + 3) & ~3;
150 if (max > limit)
151 max = limit;
152
153 ret = -ENOMEM;
David Howellsa4014d82005-07-07 17:57:03 -0700154 size = sizeof(*klist) + sizeof(struct key *) * max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 klist = kmalloc(size, GFP_KERNEL);
156 if (!klist)
157 goto error;
158
David Howells76d8aea2005-06-23 22:00:49 -0700159 /* set links */
160 rcu_read_lock();
161 sklist = rcu_dereference(source->payload.subscriptions);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 klist->maxkeys = max;
164 klist->nkeys = sklist->nkeys;
165 memcpy(klist->keys,
166 sklist->keys,
David Howellsa4014d82005-07-07 17:57:03 -0700167 sklist->nkeys * sizeof(struct key *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 for (loop = klist->nkeys - 1; loop >= 0; loop--)
170 atomic_inc(&klist->keys[loop]->usage);
171
David Howells76d8aea2005-06-23 22:00:49 -0700172 rcu_read_unlock();
173
174 rcu_assign_pointer(keyring->payload.subscriptions, klist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ret = 0;
176 }
177
178 error:
179 return ret;
180
181} /* end keyring_duplicate() */
182
183/*****************************************************************************/
184/*
185 * match keyrings on their name
186 */
187static int keyring_match(const struct key *keyring, const void *description)
188{
189 return keyring->description &&
190 strcmp(keyring->description, description) == 0;
191
192} /* end keyring_match() */
193
194/*****************************************************************************/
195/*
196 * dispose of the data dangling from the corpse of a keyring
197 */
198static void keyring_destroy(struct key *keyring)
199{
200 struct keyring_list *klist;
201 int loop;
202
203 if (keyring->description) {
204 write_lock(&keyring_name_lock);
David Howells94efe722005-08-04 13:07:07 -0700205
206 if (keyring->type_data.link.next != NULL &&
207 !list_empty(&keyring->type_data.link))
208 list_del(&keyring->type_data.link);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 write_unlock(&keyring_name_lock);
211 }
212
David Howells76d8aea2005-06-23 22:00:49 -0700213 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (klist) {
215 for (loop = klist->nkeys - 1; loop >= 0; loop--)
216 key_put(klist->keys[loop]);
217 kfree(klist);
218 }
219
220} /* end keyring_destroy() */
221
222/*****************************************************************************/
223/*
224 * describe the keyring
225 */
226static void keyring_describe(const struct key *keyring, struct seq_file *m)
227{
228 struct keyring_list *klist;
229
230 if (keyring->description) {
231 seq_puts(m, keyring->description);
232 }
233 else {
234 seq_puts(m, "[anon]");
235 }
236
David Howells76d8aea2005-06-23 22:00:49 -0700237 rcu_read_lock();
238 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (klist)
240 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
241 else
242 seq_puts(m, ": empty");
David Howells76d8aea2005-06-23 22:00:49 -0700243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245} /* end keyring_describe() */
246
247/*****************************************************************************/
248/*
249 * read a list of key IDs from the keyring's contents
David Howells76d8aea2005-06-23 22:00:49 -0700250 * - the keyring's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252static long keyring_read(const struct key *keyring,
253 char __user *buffer, size_t buflen)
254{
255 struct keyring_list *klist;
256 struct key *key;
257 size_t qty, tmp;
258 int loop, ret;
259
260 ret = 0;
David Howells76d8aea2005-06-23 22:00:49 -0700261 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (klist) {
264 /* calculate how much data we could return */
265 qty = klist->nkeys * sizeof(key_serial_t);
266
267 if (buffer && buflen > 0) {
268 if (buflen > qty)
269 buflen = qty;
270
271 /* copy the IDs of the subscribed keys into the
272 * buffer */
273 ret = -EFAULT;
274
275 for (loop = 0; loop < klist->nkeys; loop++) {
276 key = klist->keys[loop];
277
278 tmp = sizeof(key_serial_t);
279 if (tmp > buflen)
280 tmp = buflen;
281
282 if (copy_to_user(buffer,
283 &key->serial,
284 tmp) != 0)
285 goto error;
286
287 buflen -= tmp;
288 if (buflen == 0)
289 break;
290 buffer += tmp;
291 }
292 }
293
294 ret = qty;
295 }
296
297 error:
298 return ret;
299
300} /* end keyring_read() */
301
302/*****************************************************************************/
303/*
304 * allocate a keyring and link into the destination keyring
305 */
306struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
307 int not_in_quota, struct key *dest)
308{
309 struct key *keyring;
310 int ret;
311
312 keyring = key_alloc(&key_type_keyring, description,
David Howells29db9192005-10-30 15:02:44 -0800313 uid, gid,
314 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
315 not_in_quota);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if (!IS_ERR(keyring)) {
David Howells3e301482005-06-23 22:00:56 -0700318 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (ret < 0) {
320 key_put(keyring);
321 keyring = ERR_PTR(ret);
322 }
323 }
324
325 return keyring;
326
327} /* end keyring_alloc() */
328
329/*****************************************************************************/
330/*
331 * search the supplied keyring tree for a key that matches the criterion
332 * - perform a breadth-then-depth search up to the prescribed limit
333 * - we only find keys on which we have search permission
334 * - we use the supplied match function to see if the description (or other
335 * feature of interest) matches
David Howells3e301482005-06-23 22:00:56 -0700336 * - we rely on RCU to prevent the keyring lists from disappearing on us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * - we return -EAGAIN if we didn't find any matching key
338 * - we return -ENOKEY if we only found negative matching keys
David Howells664cceb2005-09-28 17:03:15 +0100339 * - we propagate the possession attribute from the keyring ref to the key ref
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
David Howells664cceb2005-09-28 17:03:15 +0100341key_ref_t keyring_search_aux(key_ref_t keyring_ref,
342 struct task_struct *context,
343 struct key_type *type,
344 const void *description,
345 key_match_func_t match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700348 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int kix;
350 } stack[KEYRING_SEARCH_MAX_DEPTH];
351
352 struct keyring_list *keylist;
353 struct timespec now;
David Howells664cceb2005-09-28 17:03:15 +0100354 unsigned long possessed;
355 struct key *keyring, *key;
356 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 long err;
David Howells76d8aea2005-06-23 22:00:49 -0700358 int sp, kix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Howells664cceb2005-09-28 17:03:15 +0100360 keyring = key_ref_to_ptr(keyring_ref);
361 possessed = is_key_possessed(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 key_check(keyring);
363
364 /* top keyring must have search permission to begin the search */
David Howells29db9192005-10-30 15:02:44 -0800365 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
366 if (err < 0) {
367 key_ref = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto error;
David Howells29db9192005-10-30 15:02:44 -0800369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Howells664cceb2005-09-28 17:03:15 +0100371 key_ref = ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (keyring->type != &key_type_keyring)
373 goto error;
374
David Howells664cceb2005-09-28 17:03:15 +0100375 rcu_read_lock();
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 now = current_kernel_time();
378 err = -EAGAIN;
379 sp = 0;
380
381 /* start processing a new keyring */
David Howells664cceb2005-09-28 17:03:15 +0100382descend:
David Howells76d8aea2005-06-23 22:00:49 -0700383 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto not_this_keyring;
385
David Howells76d8aea2005-06-23 22:00:49 -0700386 keylist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!keylist)
388 goto not_this_keyring;
389
390 /* iterate through the keys in this keyring first */
391 for (kix = 0; kix < keylist->nkeys; kix++) {
392 key = keylist->keys[kix];
393
394 /* ignore keys not of this type */
395 if (key->type != type)
396 continue;
397
398 /* skip revoked keys and expired keys */
David Howells76d8aea2005-06-23 22:00:49 -0700399 if (test_bit(KEY_FLAG_REVOKED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 continue;
401
402 if (key->expiry && now.tv_sec >= key->expiry)
403 continue;
404
405 /* keys that don't match */
406 if (!match(key, description))
407 continue;
408
409 /* key must have search permissions */
David Howells29db9192005-10-30 15:02:44 -0800410 if (key_task_permission(make_key_ref(key, possessed),
411 context, KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 continue;
413
414 /* we set a different error code if we find a negative key */
David Howells76d8aea2005-06-23 22:00:49 -0700415 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 err = -ENOKEY;
417 continue;
418 }
419
420 goto found;
421 }
422
423 /* search through the keyrings nested in this one */
424 kix = 0;
David Howells664cceb2005-09-28 17:03:15 +0100425ascend:
David Howells76d8aea2005-06-23 22:00:49 -0700426 for (; kix < keylist->nkeys; kix++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 key = keylist->keys[kix];
428 if (key->type != &key_type_keyring)
David Howells76d8aea2005-06-23 22:00:49 -0700429 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* recursively search nested keyrings
432 * - only search keyrings for which we have search permission
433 */
434 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
David Howells76d8aea2005-06-23 22:00:49 -0700435 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Howells0f6ed7c2005-11-07 00:59:30 -0800437 if (key_task_permission(make_key_ref(key, possessed),
438 context, KEY_SEARCH) < 0)
David Howells76d8aea2005-06-23 22:00:49 -0700439 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700442 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 stack[sp].kix = kix;
444 sp++;
445
446 /* begin again with the new keyring */
447 keyring = key;
448 goto descend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 /* the keyring we're looking at was disqualified or didn't contain a
452 * matching key */
David Howells664cceb2005-09-28 17:03:15 +0100453not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (sp > 0) {
455 /* resume the processing of a keyring higher up in the tree */
456 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700457 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 kix = stack[sp].kix + 1;
459 goto ascend;
460 }
461
David Howells664cceb2005-09-28 17:03:15 +0100462 key_ref = ERR_PTR(err);
463 goto error_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /* we found a viable match */
David Howells664cceb2005-09-28 17:03:15 +0100466found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 key_check(key);
David Howells664cceb2005-09-28 17:03:15 +0100469 key_ref = make_key_ref(key, possessed);
470error_2:
David Howells76d8aea2005-06-23 22:00:49 -0700471 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100472error:
473 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475} /* end keyring_search_aux() */
476
477/*****************************************************************************/
478/*
479 * search the supplied keyring tree for a key that matches the criterion
480 * - perform a breadth-then-depth search up to the prescribed limit
481 * - we only find keys on which we have search permission
482 * - we readlock the keyrings as we search down the tree
483 * - we return -EAGAIN if we didn't find any matching key
484 * - we return -ENOKEY if we only found negative matching keys
485 */
David Howells664cceb2005-09-28 17:03:15 +0100486key_ref_t keyring_search(key_ref_t keyring,
487 struct key_type *type,
488 const char *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
David Howells3e301482005-06-23 22:00:56 -0700490 if (!type->match)
491 return ERR_PTR(-ENOKEY);
492
493 return keyring_search_aux(keyring, current,
494 type, description, type->match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496} /* end keyring_search() */
497
498EXPORT_SYMBOL(keyring_search);
499
500/*****************************************************************************/
501/*
502 * search the given keyring only (no recursion)
503 * - keyring must be locked by caller
504 */
David Howells664cceb2005-09-28 17:03:15 +0100505key_ref_t __keyring_search_one(key_ref_t keyring_ref,
506 const struct key_type *ktype,
507 const char *description,
508 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct keyring_list *klist;
David Howells664cceb2005-09-28 17:03:15 +0100511 unsigned long possessed;
512 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int loop;
514
David Howells664cceb2005-09-28 17:03:15 +0100515 keyring = key_ref_to_ptr(keyring_ref);
516 possessed = is_key_possessed(keyring_ref);
517
David Howells76d8aea2005-06-23 22:00:49 -0700518 rcu_read_lock();
519
520 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (klist) {
522 for (loop = 0; loop < klist->nkeys; loop++) {
523 key = klist->keys[loop];
524
525 if (key->type == ktype &&
David Howells3e301482005-06-23 22:00:56 -0700526 (!key->type->match ||
527 key->type->match(key, description)) &&
David Howells664cceb2005-09-28 17:03:15 +0100528 key_permission(make_key_ref(key, possessed),
David Howellsdb1d1d52005-12-01 00:51:18 -0800529 perm) == 0 &&
David Howells76d8aea2005-06-23 22:00:49 -0700530 !test_bit(KEY_FLAG_REVOKED, &key->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 )
532 goto found;
533 }
534 }
535
David Howells664cceb2005-09-28 17:03:15 +0100536 rcu_read_unlock();
537 return ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 found:
540 atomic_inc(&key->usage);
David Howells76d8aea2005-06-23 22:00:49 -0700541 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100542 return make_key_ref(key, possessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544} /* end __keyring_search_one() */
545
546/*****************************************************************************/
547/*
David Howells3e301482005-06-23 22:00:56 -0700548 * search for an instantiation authorisation key matching a target key
549 * - the RCU read lock must be held by the caller
550 * - a target_id of zero specifies any valid token
551 */
552struct key *keyring_search_instkey(struct key *keyring,
553 key_serial_t target_id)
554{
555 struct request_key_auth *rka;
556 struct keyring_list *klist;
557 struct key *instkey;
558 int loop;
559
560 klist = rcu_dereference(keyring->payload.subscriptions);
561 if (klist) {
562 for (loop = 0; loop < klist->nkeys; loop++) {
563 instkey = klist->keys[loop];
564
565 if (instkey->type != &key_type_request_key_auth)
566 continue;
567
568 rka = instkey->payload.data;
569 if (target_id && rka->target_key->serial != target_id)
570 continue;
571
572 /* the auth key is revoked during instantiation */
573 if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
574 goto found;
575
576 instkey = ERR_PTR(-EKEYREVOKED);
577 goto error;
578 }
579 }
580
581 instkey = ERR_PTR(-EACCES);
582 goto error;
583
584found:
585 atomic_inc(&instkey->usage);
586error:
587 return instkey;
588
589} /* end keyring_search_instkey() */
590
591/*****************************************************************************/
592/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * find a keyring with the specified name
594 * - all named keyrings are searched
595 * - only find keyrings with search permission for the process
596 * - only find keyrings with a serial number greater than the one specified
597 */
598struct key *find_keyring_by_name(const char *name, key_serial_t bound)
599{
600 struct key *keyring;
601 int bucket;
602
603 keyring = ERR_PTR(-EINVAL);
604 if (!name)
605 goto error;
606
607 bucket = keyring_hash(name);
608
609 read_lock(&keyring_name_lock);
610
611 if (keyring_name_hash[bucket].next) {
612 /* search this hash bucket for a keyring with a matching name
613 * that's readable and that hasn't been revoked */
614 list_for_each_entry(keyring,
615 &keyring_name_hash[bucket],
616 type_data.link
617 ) {
David Howells76d8aea2005-06-23 22:00:49 -0700618 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 continue;
620
621 if (strcmp(keyring->description, name) != 0)
622 continue;
623
David Howells0f6ed7c2005-11-07 00:59:30 -0800624 if (key_permission(make_key_ref(keyring, 0),
625 KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 continue;
627
628 /* found a potential candidate, but we still need to
629 * check the serial number */
630 if (keyring->serial <= bound)
631 continue;
632
633 /* we've got a match */
634 atomic_inc(&keyring->usage);
635 read_unlock(&keyring_name_lock);
636 goto error;
637 }
638 }
639
640 read_unlock(&keyring_name_lock);
641 keyring = ERR_PTR(-ENOKEY);
642
643 error:
644 return keyring;
645
646} /* end find_keyring_by_name() */
647
648/*****************************************************************************/
649/*
650 * see if a cycle will will be created by inserting acyclic tree B in acyclic
651 * tree A at the topmost level (ie: as a direct child of A)
652 * - since we are adding B to A at the top level, checking for cycles should
653 * just be a matter of seeing if node A is somewhere in tree B
654 */
655static int keyring_detect_cycle(struct key *A, struct key *B)
656{
657 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700658 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int kix;
660 } stack[KEYRING_SEARCH_MAX_DEPTH];
661
662 struct keyring_list *keylist;
663 struct key *subtree, *key;
664 int sp, kix, ret;
665
David Howells76d8aea2005-06-23 22:00:49 -0700666 rcu_read_lock();
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ret = -EDEADLK;
669 if (A == B)
David Howells76d8aea2005-06-23 22:00:49 -0700670 goto cycle_detected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 subtree = B;
673 sp = 0;
674
675 /* start processing a new keyring */
676 descend:
David Howells76d8aea2005-06-23 22:00:49 -0700677 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto not_this_keyring;
679
David Howells76d8aea2005-06-23 22:00:49 -0700680 keylist = rcu_dereference(subtree->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (!keylist)
682 goto not_this_keyring;
683 kix = 0;
684
685 ascend:
686 /* iterate through the remaining keys in this keyring */
687 for (; kix < keylist->nkeys; kix++) {
688 key = keylist->keys[kix];
689
690 if (key == A)
691 goto cycle_detected;
692
693 /* recursively check nested keyrings */
694 if (key->type == &key_type_keyring) {
695 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
696 goto too_deep;
697
698 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700699 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 stack[sp].kix = kix;
701 sp++;
702
703 /* begin again with the new keyring */
704 subtree = key;
705 goto descend;
706 }
707 }
708
709 /* the keyring we're looking at was disqualified or didn't contain a
710 * matching key */
711 not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (sp > 0) {
713 /* resume the checking of a keyring higher up in the tree */
714 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700715 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 kix = stack[sp].kix + 1;
717 goto ascend;
718 }
719
720 ret = 0; /* no cycles detected */
721
722 error:
David Howells76d8aea2005-06-23 22:00:49 -0700723 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return ret;
725
726 too_deep:
727 ret = -ELOOP;
David Howells76d8aea2005-06-23 22:00:49 -0700728 goto error;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 cycle_detected:
731 ret = -EDEADLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto error;
733
734} /* end keyring_detect_cycle() */
735
736/*****************************************************************************/
737/*
David Howells76d8aea2005-06-23 22:00:49 -0700738 * dispose of a keyring list after the RCU grace period
739 */
740static void keyring_link_rcu_disposal(struct rcu_head *rcu)
741{
742 struct keyring_list *klist =
743 container_of(rcu, struct keyring_list, rcu);
744
745 kfree(klist);
746
747} /* end keyring_link_rcu_disposal() */
748
749/*****************************************************************************/
750/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * link a key into to a keyring
David Howells76d8aea2005-06-23 22:00:49 -0700752 * - must be called with the keyring's semaphore write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754int __key_link(struct key *keyring, struct key *key)
755{
756 struct keyring_list *klist, *nklist;
757 unsigned max;
758 size_t size;
759 int ret;
760
761 ret = -EKEYREVOKED;
David Howells76d8aea2005-06-23 22:00:49 -0700762 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto error;
764
765 ret = -ENOTDIR;
766 if (keyring->type != &key_type_keyring)
767 goto error;
768
769 /* serialise link/link calls to prevent parallel calls causing a
770 * cycle when applied to two keyring in opposite orders */
771 down_write(&keyring_serialise_link_sem);
772
773 /* check that we aren't going to create a cycle adding one keyring to
774 * another */
775 if (key->type == &key_type_keyring) {
776 ret = keyring_detect_cycle(keyring, key);
777 if (ret < 0)
778 goto error2;
779 }
780
781 /* check that we aren't going to overrun the user's quota */
782 ret = key_payload_reserve(keyring,
783 keyring->datalen + KEYQUOTA_LINK_BYTES);
784 if (ret < 0)
785 goto error2;
786
787 klist = keyring->payload.subscriptions;
788
789 if (klist && klist->nkeys < klist->maxkeys) {
790 /* there's sufficient slack space to add directly */
791 atomic_inc(&key->usage);
792
David Howells76d8aea2005-06-23 22:00:49 -0700793 klist->keys[klist->nkeys] = key;
794 smp_wmb();
795 klist->nkeys++;
796 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 ret = 0;
799 }
800 else {
801 /* grow the key list */
802 max = 4;
803 if (klist)
804 max += klist->maxkeys;
805
806 ret = -ENFILE;
David Howells76d8aea2005-06-23 22:00:49 -0700807 if (max > 65535)
808 goto error3;
David Howellsa4014d82005-07-07 17:57:03 -0700809 size = sizeof(*klist) + sizeof(struct key *) * max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (size > PAGE_SIZE)
811 goto error3;
812
813 ret = -ENOMEM;
814 nklist = kmalloc(size, GFP_KERNEL);
815 if (!nklist)
816 goto error3;
817 nklist->maxkeys = max;
818 nklist->nkeys = 0;
819
820 if (klist) {
821 nklist->nkeys = klist->nkeys;
822 memcpy(nklist->keys,
823 klist->keys,
824 sizeof(struct key *) * klist->nkeys);
825 }
826
827 /* add the key into the new space */
828 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 nklist->keys[nklist->nkeys++] = key;
David Howells76d8aea2005-06-23 22:00:49 -0700830
831 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* dispose of the old keyring list */
David Howells76d8aea2005-06-23 22:00:49 -0700834 if (klist)
835 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 ret = 0;
838 }
839
840 error2:
841 up_write(&keyring_serialise_link_sem);
842 error:
843 return ret;
844
845 error3:
846 /* undo the quota changes */
847 key_payload_reserve(keyring,
848 keyring->datalen - KEYQUOTA_LINK_BYTES);
849 goto error2;
850
851} /* end __key_link() */
852
853/*****************************************************************************/
854/*
855 * link a key to a keyring
856 */
857int key_link(struct key *keyring, struct key *key)
858{
859 int ret;
860
861 key_check(keyring);
862 key_check(key);
863
864 down_write(&keyring->sem);
865 ret = __key_link(keyring, key);
866 up_write(&keyring->sem);
867
868 return ret;
869
870} /* end key_link() */
871
872EXPORT_SYMBOL(key_link);
873
874/*****************************************************************************/
875/*
David Howells76d8aea2005-06-23 22:00:49 -0700876 * dispose of a keyring list after the RCU grace period, freeing the unlinked
877 * key
878 */
879static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
880{
881 struct keyring_list *klist =
882 container_of(rcu, struct keyring_list, rcu);
883
884 key_put(klist->keys[klist->delkey]);
885 kfree(klist);
886
887} /* end keyring_unlink_rcu_disposal() */
888
889/*****************************************************************************/
890/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * unlink the first link to a key from a keyring
892 */
893int key_unlink(struct key *keyring, struct key *key)
894{
David Howells76d8aea2005-06-23 22:00:49 -0700895 struct keyring_list *klist, *nklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int loop, ret;
897
898 key_check(keyring);
899 key_check(key);
900
901 ret = -ENOTDIR;
902 if (keyring->type != &key_type_keyring)
903 goto error;
904
905 down_write(&keyring->sem);
906
907 klist = keyring->payload.subscriptions;
908 if (klist) {
909 /* search the keyring for the key */
910 for (loop = 0; loop < klist->nkeys; loop++)
911 if (klist->keys[loop] == key)
912 goto key_is_present;
913 }
914
915 up_write(&keyring->sem);
916 ret = -ENOENT;
917 goto error;
918
David Howells76d8aea2005-06-23 22:00:49 -0700919key_is_present:
920 /* we need to copy the key list for RCU purposes */
David Howellsa4014d82005-07-07 17:57:03 -0700921 nklist = kmalloc(sizeof(*klist) +
922 sizeof(struct key *) * klist->maxkeys,
David Howells76d8aea2005-06-23 22:00:49 -0700923 GFP_KERNEL);
924 if (!nklist)
925 goto nomem;
926 nklist->maxkeys = klist->maxkeys;
927 nklist->nkeys = klist->nkeys - 1;
928
929 if (loop > 0)
930 memcpy(&nklist->keys[0],
931 &klist->keys[0],
David Howellsa4014d82005-07-07 17:57:03 -0700932 loop * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700933
934 if (loop < nklist->nkeys)
935 memcpy(&nklist->keys[loop],
936 &klist->keys[loop + 1],
David Howellsa4014d82005-07-07 17:57:03 -0700937 (nklist->nkeys - loop) * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* adjust the user's quota */
940 key_payload_reserve(keyring,
941 keyring->datalen - KEYQUOTA_LINK_BYTES);
942
David Howells76d8aea2005-06-23 22:00:49 -0700943 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 up_write(&keyring->sem);
David Howells76d8aea2005-06-23 22:00:49 -0700946
947 /* schedule for later cleanup */
948 klist->delkey = loop;
949 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 ret = 0;
952
David Howells76d8aea2005-06-23 22:00:49 -0700953error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return ret;
David Howells76d8aea2005-06-23 22:00:49 -0700955nomem:
956 ret = -ENOMEM;
957 up_write(&keyring->sem);
958 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960} /* end key_unlink() */
961
962EXPORT_SYMBOL(key_unlink);
963
964/*****************************************************************************/
965/*
David Howells76d8aea2005-06-23 22:00:49 -0700966 * dispose of a keyring list after the RCU grace period, releasing the keys it
967 * links to
968 */
969static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
970{
971 struct keyring_list *klist;
972 int loop;
973
974 klist = container_of(rcu, struct keyring_list, rcu);
975
976 for (loop = klist->nkeys - 1; loop >= 0; loop--)
977 key_put(klist->keys[loop]);
978
979 kfree(klist);
980
981} /* end keyring_clear_rcu_disposal() */
982
983/*****************************************************************************/
984/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * clear the specified process keyring
986 * - implements keyctl(KEYCTL_CLEAR)
987 */
988int keyring_clear(struct key *keyring)
989{
990 struct keyring_list *klist;
David Howells76d8aea2005-06-23 22:00:49 -0700991 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 ret = -ENOTDIR;
994 if (keyring->type == &key_type_keyring) {
995 /* detach the pointer block with the locks held */
996 down_write(&keyring->sem);
997
998 klist = keyring->payload.subscriptions;
999 if (klist) {
1000 /* adjust the quota */
1001 key_payload_reserve(keyring,
1002 sizeof(struct keyring_list));
1003
David Howells76d8aea2005-06-23 22:00:49 -07001004 rcu_assign_pointer(keyring->payload.subscriptions,
1005 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
1008 up_write(&keyring->sem);
1009
1010 /* free the keys after the locks have been dropped */
David Howells76d8aea2005-06-23 22:00:49 -07001011 if (klist)
1012 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 ret = 0;
1015 }
1016
1017 return ret;
1018
1019} /* end keyring_clear() */
1020
1021EXPORT_SYMBOL(keyring_clear);