]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* keyring.c: keyring handling |
2 | * | |
3 | * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. | |
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> | |
16 | #include <linux/seq_file.h> | |
17 | #include <linux/err.h> | |
18 | #include <asm/uaccess.h> | |
19 | #include "internal.h" | |
20 | ||
21 | /* | |
22 | * when plumbing the depths of the key tree, this sets a hard limit set on how | |
23 | * deep we're willing to go | |
24 | */ | |
25 | #define KEYRING_SEARCH_MAX_DEPTH 6 | |
26 | ||
27 | /* | |
28 | * we keep all named keyrings in a hash to speed looking them up | |
29 | */ | |
30 | #define KEYRING_NAME_HASH_SIZE (1 << 5) | |
31 | ||
32 | static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE]; | |
33 | static DEFINE_RWLOCK(keyring_name_lock); | |
34 | ||
35 | static inline unsigned keyring_hash(const char *desc) | |
36 | { | |
37 | unsigned bucket = 0; | |
38 | ||
39 | for (; *desc; desc++) | |
40 | bucket += (unsigned char) *desc; | |
41 | ||
42 | return bucket & (KEYRING_NAME_HASH_SIZE - 1); | |
43 | } | |
44 | ||
45 | /* | |
46 | * the keyring type definition | |
47 | */ | |
48 | static int keyring_instantiate(struct key *keyring, | |
49 | const void *data, size_t datalen); | |
50 | static int keyring_duplicate(struct key *keyring, const struct key *source); | |
51 | static int keyring_match(const struct key *keyring, const void *criterion); | |
52 | static void keyring_destroy(struct key *keyring); | |
53 | static void keyring_describe(const struct key *keyring, struct seq_file *m); | |
54 | static long keyring_read(const struct key *keyring, | |
55 | char __user *buffer, size_t buflen); | |
56 | ||
57 | struct key_type key_type_keyring = { | |
58 | .name = "keyring", | |
59 | .def_datalen = sizeof(struct keyring_list), | |
60 | .instantiate = keyring_instantiate, | |
61 | .duplicate = keyring_duplicate, | |
62 | .match = keyring_match, | |
63 | .destroy = keyring_destroy, | |
64 | .describe = keyring_describe, | |
65 | .read = keyring_read, | |
66 | }; | |
67 | ||
68 | /* | |
69 | * semaphore to serialise link/link calls to prevent two link calls in parallel | |
70 | * introducing a cycle | |
71 | */ | |
72 | DECLARE_RWSEM(keyring_serialise_link_sem); | |
73 | ||
74 | /*****************************************************************************/ | |
75 | /* | |
76 | * publish the name of a keyring so that it can be found by name (if it has | |
77 | * one) | |
78 | */ | |
79 | void keyring_publish_name(struct key *keyring) | |
80 | { | |
81 | int bucket; | |
82 | ||
83 | if (keyring->description) { | |
84 | bucket = keyring_hash(keyring->description); | |
85 | ||
86 | write_lock(&keyring_name_lock); | |
87 | ||
88 | if (!keyring_name_hash[bucket].next) | |
89 | INIT_LIST_HEAD(&keyring_name_hash[bucket]); | |
90 | ||
91 | list_add_tail(&keyring->type_data.link, | |
92 | &keyring_name_hash[bucket]); | |
93 | ||
94 | write_unlock(&keyring_name_lock); | |
95 | } | |
96 | ||
97 | } /* end keyring_publish_name() */ | |
98 | ||
99 | /*****************************************************************************/ | |
100 | /* | |
101 | * initialise a keyring | |
102 | * - we object if we were given any data | |
103 | */ | |
104 | static int keyring_instantiate(struct key *keyring, | |
105 | const void *data, size_t datalen) | |
106 | { | |
107 | int ret; | |
108 | ||
109 | ret = -EINVAL; | |
110 | if (datalen == 0) { | |
111 | /* make the keyring available by name if it has one */ | |
112 | keyring_publish_name(keyring); | |
113 | ret = 0; | |
114 | } | |
115 | ||
116 | return ret; | |
117 | ||
118 | } /* end keyring_instantiate() */ | |
119 | ||
120 | /*****************************************************************************/ | |
121 | /* | |
122 | * duplicate the list of subscribed keys from a source keyring into this one | |
123 | */ | |
124 | static int keyring_duplicate(struct key *keyring, const struct key *source) | |
125 | { | |
126 | struct keyring_list *sklist, *klist; | |
127 | unsigned max; | |
128 | size_t size; | |
129 | int loop, ret; | |
130 | ||
131 | const unsigned limit = | |
132 | (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key); | |
133 | ||
134 | ret = 0; | |
1da177e4 | 135 | |
76d8aeab DH |
136 | /* find out how many keys are currently linked */ |
137 | rcu_read_lock(); | |
138 | sklist = rcu_dereference(source->payload.subscriptions); | |
139 | max = 0; | |
140 | if (sklist) | |
1da177e4 | 141 | max = sklist->nkeys; |
76d8aeab DH |
142 | rcu_read_unlock(); |
143 | ||
144 | /* allocate a new payload and stuff load with key links */ | |
145 | if (max > 0) { | |
1da177e4 LT |
146 | BUG_ON(max > limit); |
147 | ||
148 | max = (max + 3) & ~3; | |
149 | if (max > limit) | |
150 | max = limit; | |
151 | ||
152 | ret = -ENOMEM; | |
153 | size = sizeof(*klist) + sizeof(struct key) * max; | |
154 | klist = kmalloc(size, GFP_KERNEL); | |
155 | if (!klist) | |
156 | goto error; | |
157 | ||
76d8aeab DH |
158 | /* set links */ |
159 | rcu_read_lock(); | |
160 | sklist = rcu_dereference(source->payload.subscriptions); | |
161 | ||
1da177e4 LT |
162 | klist->maxkeys = max; |
163 | klist->nkeys = sklist->nkeys; | |
164 | memcpy(klist->keys, | |
165 | sklist->keys, | |
166 | sklist->nkeys * sizeof(struct key)); | |
167 | ||
168 | for (loop = klist->nkeys - 1; loop >= 0; loop--) | |
169 | atomic_inc(&klist->keys[loop]->usage); | |
170 | ||
76d8aeab DH |
171 | rcu_read_unlock(); |
172 | ||
173 | rcu_assign_pointer(keyring->payload.subscriptions, klist); | |
1da177e4 LT |
174 | ret = 0; |
175 | } | |
176 | ||
177 | error: | |
178 | return ret; | |
179 | ||
180 | } /* end keyring_duplicate() */ | |
181 | ||
182 | /*****************************************************************************/ | |
183 | /* | |
184 | * match keyrings on their name | |
185 | */ | |
186 | static int keyring_match(const struct key *keyring, const void *description) | |
187 | { | |
188 | return keyring->description && | |
189 | strcmp(keyring->description, description) == 0; | |
190 | ||
191 | } /* end keyring_match() */ | |
192 | ||
193 | /*****************************************************************************/ | |
194 | /* | |
195 | * dispose of the data dangling from the corpse of a keyring | |
196 | */ | |
197 | static void keyring_destroy(struct key *keyring) | |
198 | { | |
199 | struct keyring_list *klist; | |
200 | int loop; | |
201 | ||
202 | if (keyring->description) { | |
203 | write_lock(&keyring_name_lock); | |
204 | list_del(&keyring->type_data.link); | |
205 | write_unlock(&keyring_name_lock); | |
206 | } | |
207 | ||
76d8aeab | 208 | klist = rcu_dereference(keyring->payload.subscriptions); |
1da177e4 LT |
209 | if (klist) { |
210 | for (loop = klist->nkeys - 1; loop >= 0; loop--) | |
211 | key_put(klist->keys[loop]); | |
212 | kfree(klist); | |
213 | } | |
214 | ||
215 | } /* end keyring_destroy() */ | |
216 | ||
217 | /*****************************************************************************/ | |
218 | /* | |
219 | * describe the keyring | |
220 | */ | |
221 | static void keyring_describe(const struct key *keyring, struct seq_file *m) | |
222 | { | |
223 | struct keyring_list *klist; | |
224 | ||
225 | if (keyring->description) { | |
226 | seq_puts(m, keyring->description); | |
227 | } | |
228 | else { | |
229 | seq_puts(m, "[anon]"); | |
230 | } | |
231 | ||
76d8aeab DH |
232 | rcu_read_lock(); |
233 | klist = rcu_dereference(keyring->payload.subscriptions); | |
1da177e4 LT |
234 | if (klist) |
235 | seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys); | |
236 | else | |
237 | seq_puts(m, ": empty"); | |
76d8aeab | 238 | rcu_read_unlock(); |
1da177e4 LT |
239 | |
240 | } /* end keyring_describe() */ | |
241 | ||
242 | /*****************************************************************************/ | |
243 | /* | |
244 | * read a list of key IDs from the keyring's contents | |
76d8aeab | 245 | * - the keyring's semaphore is read-locked |
1da177e4 LT |
246 | */ |
247 | static long keyring_read(const struct key *keyring, | |
248 | char __user *buffer, size_t buflen) | |
249 | { | |
250 | struct keyring_list *klist; | |
251 | struct key *key; | |
252 | size_t qty, tmp; | |
253 | int loop, ret; | |
254 | ||
255 | ret = 0; | |
76d8aeab | 256 | klist = rcu_dereference(keyring->payload.subscriptions); |
1da177e4 LT |
257 | |
258 | if (klist) { | |
259 | /* calculate how much data we could return */ | |
260 | qty = klist->nkeys * sizeof(key_serial_t); | |
261 | ||
262 | if (buffer && buflen > 0) { | |
263 | if (buflen > qty) | |
264 | buflen = qty; | |
265 | ||
266 | /* copy the IDs of the subscribed keys into the | |
267 | * buffer */ | |
268 | ret = -EFAULT; | |
269 | ||
270 | for (loop = 0; loop < klist->nkeys; loop++) { | |
271 | key = klist->keys[loop]; | |
272 | ||
273 | tmp = sizeof(key_serial_t); | |
274 | if (tmp > buflen) | |
275 | tmp = buflen; | |
276 | ||
277 | if (copy_to_user(buffer, | |
278 | &key->serial, | |
279 | tmp) != 0) | |
280 | goto error; | |
281 | ||
282 | buflen -= tmp; | |
283 | if (buflen == 0) | |
284 | break; | |
285 | buffer += tmp; | |
286 | } | |
287 | } | |
288 | ||
289 | ret = qty; | |
290 | } | |
291 | ||
292 | error: | |
293 | return ret; | |
294 | ||
295 | } /* end keyring_read() */ | |
296 | ||
297 | /*****************************************************************************/ | |
298 | /* | |
299 | * allocate a keyring and link into the destination keyring | |
300 | */ | |
301 | struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, | |
302 | int not_in_quota, struct key *dest) | |
303 | { | |
304 | struct key *keyring; | |
305 | int ret; | |
306 | ||
307 | keyring = key_alloc(&key_type_keyring, description, | |
308 | uid, gid, KEY_USR_ALL, not_in_quota); | |
309 | ||
310 | if (!IS_ERR(keyring)) { | |
311 | ret = key_instantiate_and_link(keyring, NULL, 0, dest); | |
312 | if (ret < 0) { | |
313 | key_put(keyring); | |
314 | keyring = ERR_PTR(ret); | |
315 | } | |
316 | } | |
317 | ||
318 | return keyring; | |
319 | ||
320 | } /* end keyring_alloc() */ | |
321 | ||
322 | /*****************************************************************************/ | |
323 | /* | |
324 | * search the supplied keyring tree for a key that matches the criterion | |
325 | * - perform a breadth-then-depth search up to the prescribed limit | |
326 | * - we only find keys on which we have search permission | |
327 | * - we use the supplied match function to see if the description (or other | |
328 | * feature of interest) matches | |
329 | * - we readlock the keyrings as we search down the tree | |
330 | * - we return -EAGAIN if we didn't find any matching key | |
331 | * - we return -ENOKEY if we only found negative matching keys | |
332 | */ | |
333 | struct key *keyring_search_aux(struct key *keyring, | |
334 | struct key_type *type, | |
335 | const void *description, | |
336 | key_match_func_t match) | |
337 | { | |
338 | struct { | |
76d8aeab | 339 | struct keyring_list *keylist; |
1da177e4 LT |
340 | int kix; |
341 | } stack[KEYRING_SEARCH_MAX_DEPTH]; | |
342 | ||
343 | struct keyring_list *keylist; | |
344 | struct timespec now; | |
345 | struct key *key; | |
346 | long err; | |
76d8aeab | 347 | int sp, kix; |
1da177e4 LT |
348 | |
349 | key_check(keyring); | |
350 | ||
76d8aeab DH |
351 | rcu_read_lock(); |
352 | ||
1da177e4 LT |
353 | /* top keyring must have search permission to begin the search */ |
354 | key = ERR_PTR(-EACCES); | |
355 | if (!key_permission(keyring, KEY_SEARCH)) | |
356 | goto error; | |
357 | ||
358 | key = ERR_PTR(-ENOTDIR); | |
359 | if (keyring->type != &key_type_keyring) | |
360 | goto error; | |
361 | ||
362 | now = current_kernel_time(); | |
363 | err = -EAGAIN; | |
364 | sp = 0; | |
365 | ||
366 | /* start processing a new keyring */ | |
367 | descend: | |
76d8aeab | 368 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
1da177e4 LT |
369 | goto not_this_keyring; |
370 | ||
76d8aeab | 371 | keylist = rcu_dereference(keyring->payload.subscriptions); |
1da177e4 LT |
372 | if (!keylist) |
373 | goto not_this_keyring; | |
374 | ||
375 | /* iterate through the keys in this keyring first */ | |
376 | for (kix = 0; kix < keylist->nkeys; kix++) { | |
377 | key = keylist->keys[kix]; | |
378 | ||
379 | /* ignore keys not of this type */ | |
380 | if (key->type != type) | |
381 | continue; | |
382 | ||
383 | /* skip revoked keys and expired keys */ | |
76d8aeab | 384 | if (test_bit(KEY_FLAG_REVOKED, &key->flags)) |
1da177e4 LT |
385 | continue; |
386 | ||
387 | if (key->expiry && now.tv_sec >= key->expiry) | |
388 | continue; | |
389 | ||
390 | /* keys that don't match */ | |
391 | if (!match(key, description)) | |
392 | continue; | |
393 | ||
394 | /* key must have search permissions */ | |
395 | if (!key_permission(key, KEY_SEARCH)) | |
396 | continue; | |
397 | ||
398 | /* we set a different error code if we find a negative key */ | |
76d8aeab | 399 | if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) { |
1da177e4 LT |
400 | err = -ENOKEY; |
401 | continue; | |
402 | } | |
403 | ||
404 | goto found; | |
405 | } | |
406 | ||
407 | /* search through the keyrings nested in this one */ | |
408 | kix = 0; | |
409 | ascend: | |
76d8aeab | 410 | for (; kix < keylist->nkeys; kix++) { |
1da177e4 LT |
411 | key = keylist->keys[kix]; |
412 | if (key->type != &key_type_keyring) | |
76d8aeab | 413 | continue; |
1da177e4 LT |
414 | |
415 | /* recursively search nested keyrings | |
416 | * - only search keyrings for which we have search permission | |
417 | */ | |
418 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) | |
76d8aeab | 419 | continue; |
1da177e4 LT |
420 | |
421 | if (!key_permission(key, KEY_SEARCH)) | |
76d8aeab | 422 | continue; |
1da177e4 LT |
423 | |
424 | /* stack the current position */ | |
76d8aeab | 425 | stack[sp].keylist = keylist; |
1da177e4 LT |
426 | stack[sp].kix = kix; |
427 | sp++; | |
428 | ||
429 | /* begin again with the new keyring */ | |
430 | keyring = key; | |
431 | goto descend; | |
1da177e4 LT |
432 | } |
433 | ||
434 | /* the keyring we're looking at was disqualified or didn't contain a | |
435 | * matching key */ | |
436 | not_this_keyring: | |
1da177e4 LT |
437 | if (sp > 0) { |
438 | /* resume the processing of a keyring higher up in the tree */ | |
439 | sp--; | |
76d8aeab | 440 | keylist = stack[sp].keylist; |
1da177e4 LT |
441 | kix = stack[sp].kix + 1; |
442 | goto ascend; | |
443 | } | |
444 | ||
445 | key = ERR_PTR(err); | |
446 | goto error; | |
447 | ||
448 | /* we found a viable match */ | |
449 | found: | |
450 | atomic_inc(&key->usage); | |
1da177e4 LT |
451 | key_check(key); |
452 | error: | |
76d8aeab | 453 | rcu_read_unlock(); |
1da177e4 LT |
454 | return key; |
455 | ||
456 | } /* end keyring_search_aux() */ | |
457 | ||
458 | /*****************************************************************************/ | |
459 | /* | |
460 | * search the supplied keyring tree for a key that matches the criterion | |
461 | * - perform a breadth-then-depth search up to the prescribed limit | |
462 | * - we only find keys on which we have search permission | |
463 | * - we readlock the keyrings as we search down the tree | |
464 | * - we return -EAGAIN if we didn't find any matching key | |
465 | * - we return -ENOKEY if we only found negative matching keys | |
466 | */ | |
467 | struct key *keyring_search(struct key *keyring, | |
468 | struct key_type *type, | |
469 | const char *description) | |
470 | { | |
471 | return keyring_search_aux(keyring, type, description, type->match); | |
472 | ||
473 | } /* end keyring_search() */ | |
474 | ||
475 | EXPORT_SYMBOL(keyring_search); | |
476 | ||
477 | /*****************************************************************************/ | |
478 | /* | |
479 | * search the given keyring only (no recursion) | |
480 | * - keyring must be locked by caller | |
481 | */ | |
482 | struct key *__keyring_search_one(struct key *keyring, | |
483 | const struct key_type *ktype, | |
484 | const char *description, | |
485 | key_perm_t perm) | |
486 | { | |
487 | struct keyring_list *klist; | |
488 | struct key *key; | |
489 | int loop; | |
490 | ||
76d8aeab DH |
491 | rcu_read_lock(); |
492 | ||
493 | klist = rcu_dereference(keyring->payload.subscriptions); | |
1da177e4 LT |
494 | if (klist) { |
495 | for (loop = 0; loop < klist->nkeys; loop++) { | |
496 | key = klist->keys[loop]; | |
497 | ||
498 | if (key->type == ktype && | |
499 | key->type->match(key, description) && | |
500 | key_permission(key, perm) && | |
76d8aeab | 501 | !test_bit(KEY_FLAG_REVOKED, &key->flags) |
1da177e4 LT |
502 | ) |
503 | goto found; | |
504 | } | |
505 | } | |
506 | ||
507 | key = ERR_PTR(-ENOKEY); | |
508 | goto error; | |
509 | ||
510 | found: | |
511 | atomic_inc(&key->usage); | |
512 | error: | |
76d8aeab | 513 | rcu_read_unlock(); |
1da177e4 LT |
514 | return key; |
515 | ||
516 | } /* end __keyring_search_one() */ | |
517 | ||
518 | /*****************************************************************************/ | |
519 | /* | |
520 | * find a keyring with the specified name | |
521 | * - all named keyrings are searched | |
522 | * - only find keyrings with search permission for the process | |
523 | * - only find keyrings with a serial number greater than the one specified | |
524 | */ | |
525 | struct key *find_keyring_by_name(const char *name, key_serial_t bound) | |
526 | { | |
527 | struct key *keyring; | |
528 | int bucket; | |
529 | ||
530 | keyring = ERR_PTR(-EINVAL); | |
531 | if (!name) | |
532 | goto error; | |
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 | ) { | |
76d8aeab | 545 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
1da177e4 LT |
546 | continue; |
547 | ||
548 | if (strcmp(keyring->description, name) != 0) | |
549 | continue; | |
550 | ||
551 | if (!key_permission(keyring, KEY_SEARCH)) | |
552 | continue; | |
553 | ||
554 | /* found a potential candidate, but we still need to | |
555 | * check the serial number */ | |
556 | if (keyring->serial <= bound) | |
557 | continue; | |
558 | ||
559 | /* we've got a match */ | |
560 | atomic_inc(&keyring->usage); | |
561 | read_unlock(&keyring_name_lock); | |
562 | goto error; | |
563 | } | |
564 | } | |
565 | ||
566 | read_unlock(&keyring_name_lock); | |
567 | keyring = ERR_PTR(-ENOKEY); | |
568 | ||
569 | error: | |
570 | return keyring; | |
571 | ||
572 | } /* end find_keyring_by_name() */ | |
573 | ||
574 | /*****************************************************************************/ | |
575 | /* | |
576 | * see if a cycle will will be created by inserting acyclic tree B in acyclic | |
577 | * tree A at the topmost level (ie: as a direct child of A) | |
578 | * - since we are adding B to A at the top level, checking for cycles should | |
579 | * just be a matter of seeing if node A is somewhere in tree B | |
580 | */ | |
581 | static int keyring_detect_cycle(struct key *A, struct key *B) | |
582 | { | |
583 | struct { | |
76d8aeab | 584 | struct keyring_list *keylist; |
1da177e4 LT |
585 | int kix; |
586 | } stack[KEYRING_SEARCH_MAX_DEPTH]; | |
587 | ||
588 | struct keyring_list *keylist; | |
589 | struct key *subtree, *key; | |
590 | int sp, kix, ret; | |
591 | ||
76d8aeab DH |
592 | rcu_read_lock(); |
593 | ||
1da177e4 LT |
594 | ret = -EDEADLK; |
595 | if (A == B) | |
76d8aeab | 596 | goto cycle_detected; |
1da177e4 LT |
597 | |
598 | subtree = B; | |
599 | sp = 0; | |
600 | ||
601 | /* start processing a new keyring */ | |
602 | descend: | |
76d8aeab | 603 | if (test_bit(KEY_FLAG_REVOKED, &subtree->flags)) |
1da177e4 LT |
604 | goto not_this_keyring; |
605 | ||
76d8aeab | 606 | keylist = rcu_dereference(subtree->payload.subscriptions); |
1da177e4 LT |
607 | if (!keylist) |
608 | goto not_this_keyring; | |
609 | kix = 0; | |
610 | ||
611 | ascend: | |
612 | /* iterate through the remaining keys in this keyring */ | |
613 | for (; kix < keylist->nkeys; kix++) { | |
614 | key = keylist->keys[kix]; | |
615 | ||
616 | if (key == A) | |
617 | goto cycle_detected; | |
618 | ||
619 | /* recursively check nested keyrings */ | |
620 | if (key->type == &key_type_keyring) { | |
621 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) | |
622 | goto too_deep; | |
623 | ||
624 | /* stack the current position */ | |
76d8aeab | 625 | stack[sp].keylist = keylist; |
1da177e4 LT |
626 | stack[sp].kix = kix; |
627 | sp++; | |
628 | ||
629 | /* begin again with the new keyring */ | |
630 | subtree = key; | |
631 | goto descend; | |
632 | } | |
633 | } | |
634 | ||
635 | /* the keyring we're looking at was disqualified or didn't contain a | |
636 | * matching key */ | |
637 | not_this_keyring: | |
1da177e4 LT |
638 | if (sp > 0) { |
639 | /* resume the checking of a keyring higher up in the tree */ | |
640 | sp--; | |
76d8aeab | 641 | keylist = stack[sp].keylist; |
1da177e4 LT |
642 | kix = stack[sp].kix + 1; |
643 | goto ascend; | |
644 | } | |
645 | ||
646 | ret = 0; /* no cycles detected */ | |
647 | ||
648 | error: | |
76d8aeab | 649 | rcu_read_unlock(); |
1da177e4 LT |
650 | return ret; |
651 | ||
652 | too_deep: | |
653 | ret = -ELOOP; | |
76d8aeab DH |
654 | goto error; |
655 | ||
1da177e4 LT |
656 | cycle_detected: |
657 | ret = -EDEADLK; | |
1da177e4 LT |
658 | goto error; |
659 | ||
660 | } /* end keyring_detect_cycle() */ | |
661 | ||
76d8aeab DH |
662 | /*****************************************************************************/ |
663 | /* | |
664 | * dispose of a keyring list after the RCU grace period | |
665 | */ | |
666 | static void keyring_link_rcu_disposal(struct rcu_head *rcu) | |
667 | { | |
668 | struct keyring_list *klist = | |
669 | container_of(rcu, struct keyring_list, rcu); | |
670 | ||
671 | kfree(klist); | |
672 | ||
673 | } /* end keyring_link_rcu_disposal() */ | |
674 | ||
1da177e4 LT |
675 | /*****************************************************************************/ |
676 | /* | |
677 | * link a key into to a keyring | |
76d8aeab | 678 | * - must be called with the keyring's semaphore write-locked |
1da177e4 LT |
679 | */ |
680 | int __key_link(struct key *keyring, struct key *key) | |
681 | { | |
682 | struct keyring_list *klist, *nklist; | |
683 | unsigned max; | |
684 | size_t size; | |
685 | int ret; | |
686 | ||
687 | ret = -EKEYREVOKED; | |
76d8aeab | 688 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
1da177e4 LT |
689 | goto error; |
690 | ||
691 | ret = -ENOTDIR; | |
692 | if (keyring->type != &key_type_keyring) | |
693 | goto error; | |
694 | ||
695 | /* serialise link/link calls to prevent parallel calls causing a | |
696 | * cycle when applied to two keyring in opposite orders */ | |
697 | down_write(&keyring_serialise_link_sem); | |
698 | ||
699 | /* check that we aren't going to create a cycle adding one keyring to | |
700 | * another */ | |
701 | if (key->type == &key_type_keyring) { | |
702 | ret = keyring_detect_cycle(keyring, key); | |
703 | if (ret < 0) | |
704 | goto error2; | |
705 | } | |
706 | ||
707 | /* check that we aren't going to overrun the user's quota */ | |
708 | ret = key_payload_reserve(keyring, | |
709 | keyring->datalen + KEYQUOTA_LINK_BYTES); | |
710 | if (ret < 0) | |
711 | goto error2; | |
712 | ||
713 | klist = keyring->payload.subscriptions; | |
714 | ||
715 | if (klist && klist->nkeys < klist->maxkeys) { | |
716 | /* there's sufficient slack space to add directly */ | |
717 | atomic_inc(&key->usage); | |
718 | ||
76d8aeab DH |
719 | klist->keys[klist->nkeys] = key; |
720 | smp_wmb(); | |
721 | klist->nkeys++; | |
722 | smp_wmb(); | |
1da177e4 LT |
723 | |
724 | ret = 0; | |
725 | } | |
726 | else { | |
727 | /* grow the key list */ | |
728 | max = 4; | |
729 | if (klist) | |
730 | max += klist->maxkeys; | |
731 | ||
732 | ret = -ENFILE; | |
76d8aeab DH |
733 | if (max > 65535) |
734 | goto error3; | |
1da177e4 LT |
735 | size = sizeof(*klist) + sizeof(*key) * max; |
736 | if (size > PAGE_SIZE) | |
737 | goto error3; | |
738 | ||
739 | ret = -ENOMEM; | |
740 | nklist = kmalloc(size, GFP_KERNEL); | |
741 | if (!nklist) | |
742 | goto error3; | |
743 | nklist->maxkeys = max; | |
744 | nklist->nkeys = 0; | |
745 | ||
746 | if (klist) { | |
747 | nklist->nkeys = klist->nkeys; | |
748 | memcpy(nklist->keys, | |
749 | klist->keys, | |
750 | sizeof(struct key *) * klist->nkeys); | |
751 | } | |
752 | ||
753 | /* add the key into the new space */ | |
754 | atomic_inc(&key->usage); | |
1da177e4 | 755 | nklist->keys[nklist->nkeys++] = key; |
76d8aeab DH |
756 | |
757 | rcu_assign_pointer(keyring->payload.subscriptions, nklist); | |
1da177e4 LT |
758 | |
759 | /* dispose of the old keyring list */ | |
76d8aeab DH |
760 | if (klist) |
761 | call_rcu(&klist->rcu, keyring_link_rcu_disposal); | |
1da177e4 LT |
762 | |
763 | ret = 0; | |
764 | } | |
765 | ||
766 | error2: | |
767 | up_write(&keyring_serialise_link_sem); | |
768 | error: | |
769 | return ret; | |
770 | ||
771 | error3: | |
772 | /* undo the quota changes */ | |
773 | key_payload_reserve(keyring, | |
774 | keyring->datalen - KEYQUOTA_LINK_BYTES); | |
775 | goto error2; | |
776 | ||
777 | } /* end __key_link() */ | |
778 | ||
779 | /*****************************************************************************/ | |
780 | /* | |
781 | * link a key to a keyring | |
782 | */ | |
783 | int key_link(struct key *keyring, struct key *key) | |
784 | { | |
785 | int ret; | |
786 | ||
787 | key_check(keyring); | |
788 | key_check(key); | |
789 | ||
790 | down_write(&keyring->sem); | |
791 | ret = __key_link(keyring, key); | |
792 | up_write(&keyring->sem); | |
793 | ||
794 | return ret; | |
795 | ||
796 | } /* end key_link() */ | |
797 | ||
798 | EXPORT_SYMBOL(key_link); | |
799 | ||
76d8aeab DH |
800 | /*****************************************************************************/ |
801 | /* | |
802 | * dispose of a keyring list after the RCU grace period, freeing the unlinked | |
803 | * key | |
804 | */ | |
805 | static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) | |
806 | { | |
807 | struct keyring_list *klist = | |
808 | container_of(rcu, struct keyring_list, rcu); | |
809 | ||
810 | key_put(klist->keys[klist->delkey]); | |
811 | kfree(klist); | |
812 | ||
813 | } /* end keyring_unlink_rcu_disposal() */ | |
814 | ||
1da177e4 LT |
815 | /*****************************************************************************/ |
816 | /* | |
817 | * unlink the first link to a key from a keyring | |
818 | */ | |
819 | int key_unlink(struct key *keyring, struct key *key) | |
820 | { | |
76d8aeab | 821 | struct keyring_list *klist, *nklist; |
1da177e4 LT |
822 | int loop, ret; |
823 | ||
824 | key_check(keyring); | |
825 | key_check(key); | |
826 | ||
827 | ret = -ENOTDIR; | |
828 | if (keyring->type != &key_type_keyring) | |
829 | goto error; | |
830 | ||
831 | down_write(&keyring->sem); | |
832 | ||
833 | klist = keyring->payload.subscriptions; | |
834 | if (klist) { | |
835 | /* search the keyring for the key */ | |
836 | for (loop = 0; loop < klist->nkeys; loop++) | |
837 | if (klist->keys[loop] == key) | |
838 | goto key_is_present; | |
839 | } | |
840 | ||
841 | up_write(&keyring->sem); | |
842 | ret = -ENOENT; | |
843 | goto error; | |
844 | ||
76d8aeab DH |
845 | key_is_present: |
846 | /* we need to copy the key list for RCU purposes */ | |
847 | nklist = kmalloc(sizeof(*klist) + sizeof(*key) * klist->maxkeys, | |
848 | GFP_KERNEL); | |
849 | if (!nklist) | |
850 | goto nomem; | |
851 | nklist->maxkeys = klist->maxkeys; | |
852 | nklist->nkeys = klist->nkeys - 1; | |
853 | ||
854 | if (loop > 0) | |
855 | memcpy(&nklist->keys[0], | |
856 | &klist->keys[0], | |
857 | loop * sizeof(klist->keys[0])); | |
858 | ||
859 | if (loop < nklist->nkeys) | |
860 | memcpy(&nklist->keys[loop], | |
861 | &klist->keys[loop + 1], | |
862 | (nklist->nkeys - loop) * sizeof(klist->keys[0])); | |
863 | ||
1da177e4 LT |
864 | /* adjust the user's quota */ |
865 | key_payload_reserve(keyring, | |
866 | keyring->datalen - KEYQUOTA_LINK_BYTES); | |
867 | ||
76d8aeab | 868 | rcu_assign_pointer(keyring->payload.subscriptions, nklist); |
1da177e4 | 869 | |
76d8aeab | 870 | up_write(&keyring->sem); |
1da177e4 | 871 | |
76d8aeab DH |
872 | /* schedule for later cleanup */ |
873 | klist->delkey = loop; | |
874 | call_rcu(&klist->rcu, keyring_unlink_rcu_disposal); | |
1da177e4 | 875 | |
1da177e4 LT |
876 | ret = 0; |
877 | ||
76d8aeab | 878 | error: |
1da177e4 | 879 | return ret; |
76d8aeab DH |
880 | nomem: |
881 | ret = -ENOMEM; | |
882 | up_write(&keyring->sem); | |
883 | goto error; | |
1da177e4 LT |
884 | |
885 | } /* end key_unlink() */ | |
886 | ||
887 | EXPORT_SYMBOL(key_unlink); | |
888 | ||
76d8aeab DH |
889 | /*****************************************************************************/ |
890 | /* | |
891 | * dispose of a keyring list after the RCU grace period, releasing the keys it | |
892 | * links to | |
893 | */ | |
894 | static void keyring_clear_rcu_disposal(struct rcu_head *rcu) | |
895 | { | |
896 | struct keyring_list *klist; | |
897 | int loop; | |
898 | ||
899 | klist = container_of(rcu, struct keyring_list, rcu); | |
900 | ||
901 | for (loop = klist->nkeys - 1; loop >= 0; loop--) | |
902 | key_put(klist->keys[loop]); | |
903 | ||
904 | kfree(klist); | |
905 | ||
906 | } /* end keyring_clear_rcu_disposal() */ | |
907 | ||
1da177e4 LT |
908 | /*****************************************************************************/ |
909 | /* | |
910 | * clear the specified process keyring | |
911 | * - implements keyctl(KEYCTL_CLEAR) | |
912 | */ | |
913 | int keyring_clear(struct key *keyring) | |
914 | { | |
915 | struct keyring_list *klist; | |
76d8aeab | 916 | int ret; |
1da177e4 LT |
917 | |
918 | ret = -ENOTDIR; | |
919 | if (keyring->type == &key_type_keyring) { | |
920 | /* detach the pointer block with the locks held */ | |
921 | down_write(&keyring->sem); | |
922 | ||
923 | klist = keyring->payload.subscriptions; | |
924 | if (klist) { | |
925 | /* adjust the quota */ | |
926 | key_payload_reserve(keyring, | |
927 | sizeof(struct keyring_list)); | |
928 | ||
76d8aeab DH |
929 | rcu_assign_pointer(keyring->payload.subscriptions, |
930 | NULL); | |
1da177e4 LT |
931 | } |
932 | ||
933 | up_write(&keyring->sem); | |
934 | ||
935 | /* free the keys after the locks have been dropped */ | |
76d8aeab DH |
936 | if (klist) |
937 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); | |
1da177e4 LT |
938 | |
939 | ret = 0; | |
940 | } | |
941 | ||
942 | return ret; | |
943 | ||
944 | } /* end keyring_clear() */ | |
945 | ||
946 | EXPORT_SYMBOL(keyring_clear); |