diff options
Diffstat (limited to 'security/keys/keyring.c')
-rw-r--r-- | security/keys/keyring.c | 326 |
1 files changed, 194 insertions, 132 deletions
diff --git a/security/keys/keyring.c b/security/keys/keyring.c index d37f713e73ce..5620f084dede 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c | |||
@@ -25,14 +25,16 @@ | |||
25 | (keyring)->payload.subscriptions, \ | 25 | (keyring)->payload.subscriptions, \ |
26 | rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem))) | 26 | rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem))) |
27 | 27 | ||
28 | #define KEY_LINK_FIXQUOTA 1UL | ||
29 | |||
28 | /* | 30 | /* |
29 | * when plumbing the depths of the key tree, this sets a hard limit set on how | 31 | * When plumbing the depths of the key tree, this sets a hard limit |
30 | * deep we're willing to go | 32 | * set on how deep we're willing to go. |
31 | */ | 33 | */ |
32 | #define KEYRING_SEARCH_MAX_DEPTH 6 | 34 | #define KEYRING_SEARCH_MAX_DEPTH 6 |
33 | 35 | ||
34 | /* | 36 | /* |
35 | * we keep all named keyrings in a hash to speed looking them up | 37 | * We keep all named keyrings in a hash to speed looking them up. |
36 | */ | 38 | */ |
37 | #define KEYRING_NAME_HASH_SIZE (1 << 5) | 39 | #define KEYRING_NAME_HASH_SIZE (1 << 5) |
38 | 40 | ||
@@ -50,7 +52,9 @@ static inline unsigned keyring_hash(const char *desc) | |||
50 | } | 52 | } |
51 | 53 | ||
52 | /* | 54 | /* |
53 | * the keyring type definition | 55 | * The keyring key type definition. Keyrings are simply keys of this type and |
56 | * can be treated as ordinary keys in addition to having their own special | ||
57 | * operations. | ||
54 | */ | 58 | */ |
55 | static int keyring_instantiate(struct key *keyring, | 59 | static int keyring_instantiate(struct key *keyring, |
56 | const void *data, size_t datalen); | 60 | const void *data, size_t datalen); |
@@ -71,19 +75,17 @@ struct key_type key_type_keyring = { | |||
71 | .describe = keyring_describe, | 75 | .describe = keyring_describe, |
72 | .read = keyring_read, | 76 | .read = keyring_read, |
73 | }; | 77 | }; |
74 | |||
75 | EXPORT_SYMBOL(key_type_keyring); | 78 | EXPORT_SYMBOL(key_type_keyring); |
76 | 79 | ||
77 | /* | 80 | /* |
78 | * semaphore to serialise link/link calls to prevent two link calls in parallel | 81 | * Semaphore to serialise link/link calls to prevent two link calls in parallel |
79 | * introducing a cycle | 82 | * introducing a cycle. |
80 | */ | 83 | */ |
81 | static DECLARE_RWSEM(keyring_serialise_link_sem); | 84 | static DECLARE_RWSEM(keyring_serialise_link_sem); |
82 | 85 | ||
83 | /*****************************************************************************/ | ||
84 | /* | 86 | /* |
85 | * publish the name of a keyring so that it can be found by name (if it has | 87 | * Publish the name of a keyring so that it can be found by name (if it has |
86 | * one) | 88 | * one). |
87 | */ | 89 | */ |
88 | static void keyring_publish_name(struct key *keyring) | 90 | static void keyring_publish_name(struct key *keyring) |
89 | { | 91 | { |
@@ -102,13 +104,12 @@ static void keyring_publish_name(struct key *keyring) | |||
102 | 104 | ||
103 | write_unlock(&keyring_name_lock); | 105 | write_unlock(&keyring_name_lock); |
104 | } | 106 | } |
107 | } | ||
105 | 108 | ||
106 | } /* end keyring_publish_name() */ | ||
107 | |||
108 | /*****************************************************************************/ | ||
109 | /* | 109 | /* |
110 | * initialise a keyring | 110 | * Initialise a keyring. |
111 | * - we object if we were given any data | 111 | * |
112 | * Returns 0 on success, -EINVAL if given any data. | ||
112 | */ | 113 | */ |
113 | static int keyring_instantiate(struct key *keyring, | 114 | static int keyring_instantiate(struct key *keyring, |
114 | const void *data, size_t datalen) | 115 | const void *data, size_t datalen) |
@@ -123,23 +124,20 @@ static int keyring_instantiate(struct key *keyring, | |||
123 | } | 124 | } |
124 | 125 | ||
125 | return ret; | 126 | return ret; |
127 | } | ||
126 | 128 | ||
127 | } /* end keyring_instantiate() */ | ||
128 | |||
129 | /*****************************************************************************/ | ||
130 | /* | 129 | /* |
131 | * match keyrings on their name | 130 | * Match keyrings on their name |
132 | */ | 131 | */ |
133 | static int keyring_match(const struct key *keyring, const void *description) | 132 | static int keyring_match(const struct key *keyring, const void *description) |
134 | { | 133 | { |
135 | return keyring->description && | 134 | return keyring->description && |
136 | strcmp(keyring->description, description) == 0; | 135 | strcmp(keyring->description, description) == 0; |
136 | } | ||
137 | 137 | ||
138 | } /* end keyring_match() */ | ||
139 | |||
140 | /*****************************************************************************/ | ||
141 | /* | 138 | /* |
142 | * dispose of the data dangling from the corpse of a keyring | 139 | * Clean up a keyring when it is destroyed. Unpublish its name if it had one |
140 | * and dispose of its data. | ||
143 | */ | 141 | */ |
144 | static void keyring_destroy(struct key *keyring) | 142 | static void keyring_destroy(struct key *keyring) |
145 | { | 143 | { |
@@ -164,12 +162,10 @@ static void keyring_destroy(struct key *keyring) | |||
164 | key_put(klist->keys[loop]); | 162 | key_put(klist->keys[loop]); |
165 | kfree(klist); | 163 | kfree(klist); |
166 | } | 164 | } |
165 | } | ||
167 | 166 | ||
168 | } /* end keyring_destroy() */ | ||
169 | |||
170 | /*****************************************************************************/ | ||
171 | /* | 167 | /* |
172 | * describe the keyring | 168 | * Describe a keyring for /proc. |
173 | */ | 169 | */ |
174 | static void keyring_describe(const struct key *keyring, struct seq_file *m) | 170 | static void keyring_describe(const struct key *keyring, struct seq_file *m) |
175 | { | 171 | { |
@@ -187,13 +183,12 @@ static void keyring_describe(const struct key *keyring, struct seq_file *m) | |||
187 | else | 183 | else |
188 | seq_puts(m, ": empty"); | 184 | seq_puts(m, ": empty"); |
189 | rcu_read_unlock(); | 185 | rcu_read_unlock(); |
186 | } | ||
190 | 187 | ||
191 | } /* end keyring_describe() */ | ||
192 | |||
193 | /*****************************************************************************/ | ||
194 | /* | 188 | /* |
195 | * read a list of key IDs from the keyring's contents | 189 | * Read a list of key IDs from the keyring's contents in binary form |
196 | * - the keyring's semaphore is read-locked | 190 | * |
191 | * The keyring's semaphore is read-locked by the caller. | ||
197 | */ | 192 | */ |
198 | static long keyring_read(const struct key *keyring, | 193 | static long keyring_read(const struct key *keyring, |
199 | char __user *buffer, size_t buflen) | 194 | char __user *buffer, size_t buflen) |
@@ -241,12 +236,10 @@ static long keyring_read(const struct key *keyring, | |||
241 | 236 | ||
242 | error: | 237 | error: |
243 | return ret; | 238 | return ret; |
239 | } | ||
244 | 240 | ||
245 | } /* end keyring_read() */ | ||
246 | |||
247 | /*****************************************************************************/ | ||
248 | /* | 241 | /* |
249 | * allocate a keyring and link into the destination keyring | 242 | * Allocate a keyring and link into the destination keyring. |
250 | */ | 243 | */ |
251 | struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, | 244 | struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, |
252 | const struct cred *cred, unsigned long flags, | 245 | const struct cred *cred, unsigned long flags, |
@@ -269,20 +262,42 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, | |||
269 | } | 262 | } |
270 | 263 | ||
271 | return keyring; | 264 | return keyring; |
265 | } | ||
272 | 266 | ||
273 | } /* end keyring_alloc() */ | 267 | /** |
274 | 268 | * keyring_search_aux - Search a keyring tree for a key matching some criteria | |
275 | /*****************************************************************************/ | 269 | * @keyring_ref: A pointer to the keyring with possession indicator. |
276 | /* | 270 | * @cred: The credentials to use for permissions checks. |
277 | * search the supplied keyring tree for a key that matches the criterion | 271 | * @type: The type of key to search for. |
278 | * - perform a breadth-then-depth search up to the prescribed limit | 272 | * @description: Parameter for @match. |
279 | * - we only find keys on which we have search permission | 273 | * @match: Function to rule on whether or not a key is the one required. |
280 | * - we use the supplied match function to see if the description (or other | 274 | * |
281 | * feature of interest) matches | 275 | * Search the supplied keyring tree for a key that matches the criteria given. |
282 | * - we rely on RCU to prevent the keyring lists from disappearing on us | 276 | * The root keyring and any linked keyrings must grant Search permission to the |
283 | * - we return -EAGAIN if we didn't find any matching key | 277 | * caller to be searchable and keys can only be found if they too grant Search |
284 | * - we return -ENOKEY if we only found negative matching keys | 278 | * to the caller. The possession flag on the root keyring pointer controls use |
285 | * - we propagate the possession attribute from the keyring ref to the key ref | 279 | * of the possessor bits in permissions checking of the entire tree. In |
280 | * addition, the LSM gets to forbid keyring searches and key matches. | ||
281 | * | ||
282 | * The search is performed as a breadth-then-depth search up to the prescribed | ||
283 | * limit (KEYRING_SEARCH_MAX_DEPTH). | ||
284 | * | ||
285 | * Keys are matched to the type provided and are then filtered by the match | ||
286 | * function, which is given the description to use in any way it sees fit. The | ||
287 | * match function may use any attributes of a key that it wishes to to | ||
288 | * determine the match. Normally the match function from the key type would be | ||
289 | * used. | ||
290 | * | ||
291 | * RCU is used to prevent the keyring key lists from disappearing without the | ||
292 | * need to take lots of locks. | ||
293 | * | ||
294 | * Returns a pointer to the found key and increments the key usage count if | ||
295 | * successful; -EAGAIN if no matching keys were found, or if expired or revoked | ||
296 | * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the | ||
297 | * specified keyring wasn't a keyring. | ||
298 | * | ||
299 | * In the case of a successful return, the possession attribute from | ||
300 | * @keyring_ref is propagated to the returned key reference. | ||
286 | */ | 301 | */ |
287 | key_ref_t keyring_search_aux(key_ref_t keyring_ref, | 302 | key_ref_t keyring_search_aux(key_ref_t keyring_ref, |
288 | const struct cred *cred, | 303 | const struct cred *cred, |
@@ -444,17 +459,16 @@ error_2: | |||
444 | rcu_read_unlock(); | 459 | rcu_read_unlock(); |
445 | error: | 460 | error: |
446 | return key_ref; | 461 | return key_ref; |
462 | } | ||
447 | 463 | ||
448 | } /* end keyring_search_aux() */ | 464 | /** |
449 | 465 | * keyring_search - Search the supplied keyring tree for a matching key | |
450 | /*****************************************************************************/ | 466 | * @keyring: The root of the keyring tree to be searched. |
451 | /* | 467 | * @type: The type of keyring we want to find. |
452 | * search the supplied keyring tree for a key that matches the criterion | 468 | * @description: The name of the keyring we want to find. |
453 | * - perform a breadth-then-depth search up to the prescribed limit | 469 | * |
454 | * - we only find keys on which we have search permission | 470 | * As keyring_search_aux() above, but using the current task's credentials and |
455 | * - we readlock the keyrings as we search down the tree | 471 | * type's default matching function. |
456 | * - we return -EAGAIN if we didn't find any matching key | ||
457 | * - we return -ENOKEY if we only found negative matching keys | ||
458 | */ | 472 | */ |
459 | key_ref_t keyring_search(key_ref_t keyring, | 473 | key_ref_t keyring_search(key_ref_t keyring, |
460 | struct key_type *type, | 474 | struct key_type *type, |
@@ -465,16 +479,23 @@ key_ref_t keyring_search(key_ref_t keyring, | |||
465 | 479 | ||
466 | return keyring_search_aux(keyring, current->cred, | 480 | return keyring_search_aux(keyring, current->cred, |
467 | type, description, type->match); | 481 | type, description, type->match); |
468 | 482 | } | |
469 | } /* end keyring_search() */ | ||
470 | |||
471 | EXPORT_SYMBOL(keyring_search); | 483 | EXPORT_SYMBOL(keyring_search); |
472 | 484 | ||
473 | /*****************************************************************************/ | ||
474 | /* | 485 | /* |
475 | * search the given keyring only (no recursion) | 486 | * Search the given keyring only (no recursion). |
476 | * - keyring must be locked by caller | 487 | * |
477 | * - caller must guarantee that the keyring is a keyring | 488 | * The caller must guarantee that the keyring is a keyring and that the |
489 | * permission is granted to search the keyring as no check is made here. | ||
490 | * | ||
491 | * RCU is used to make it unnecessary to lock the keyring key list here. | ||
492 | * | ||
493 | * Returns a pointer to the found key with usage count incremented if | ||
494 | * successful and returns -ENOKEY if not found. Revoked keys and keys not | ||
495 | * providing the requested permission are skipped over. | ||
496 | * | ||
497 | * If successful, the possession indicator is propagated from the keyring ref | ||
498 | * to the returned key reference. | ||
478 | */ | 499 | */ |
479 | key_ref_t __keyring_search_one(key_ref_t keyring_ref, | 500 | key_ref_t __keyring_search_one(key_ref_t keyring_ref, |
480 | const struct key_type *ktype, | 501 | const struct key_type *ktype, |
@@ -514,14 +535,18 @@ found: | |||
514 | atomic_inc(&key->usage); | 535 | atomic_inc(&key->usage); |
515 | rcu_read_unlock(); | 536 | rcu_read_unlock(); |
516 | return make_key_ref(key, possessed); | 537 | return make_key_ref(key, possessed); |
538 | } | ||
517 | 539 | ||
518 | } /* end __keyring_search_one() */ | ||
519 | |||
520 | /*****************************************************************************/ | ||
521 | /* | 540 | /* |
522 | * find a keyring with the specified name | 541 | * Find a keyring with the specified name. |
523 | * - all named keyrings are searched | 542 | * |
524 | * - normally only finds keyrings with search permission for the current process | 543 | * All named keyrings in the current user namespace are searched, provided they |
544 | * grant Search permission directly to the caller (unless this check is | ||
545 | * skipped). Keyrings whose usage points have reached zero or who have been | ||
546 | * revoked are skipped. | ||
547 | * | ||
548 | * Returns a pointer to the keyring with the keyring's refcount having being | ||
549 | * incremented on success. -ENOKEY is returned if a key could not be found. | ||
525 | */ | 550 | */ |
526 | struct key *find_keyring_by_name(const char *name, bool skip_perm_check) | 551 | struct key *find_keyring_by_name(const char *name, bool skip_perm_check) |
527 | { | 552 | { |
@@ -569,15 +594,14 @@ struct key *find_keyring_by_name(const char *name, bool skip_perm_check) | |||
569 | out: | 594 | out: |
570 | read_unlock(&keyring_name_lock); | 595 | read_unlock(&keyring_name_lock); |
571 | return keyring; | 596 | return keyring; |
597 | } | ||
572 | 598 | ||
573 | } /* end find_keyring_by_name() */ | ||
574 | |||
575 | /*****************************************************************************/ | ||
576 | /* | 599 | /* |
577 | * see if a cycle will will be created by inserting acyclic tree B in acyclic | 600 | * See if a cycle will will be created by inserting acyclic tree B in acyclic |
578 | * tree A at the topmost level (ie: as a direct child of A) | 601 | * tree A at the topmost level (ie: as a direct child of A). |
579 | * - since we are adding B to A at the top level, checking for cycles should | 602 | * |
580 | * just be a matter of seeing if node A is somewhere in tree B | 603 | * Since we are adding B to A at the top level, checking for cycles should just |
604 | * be a matter of seeing if node A is somewhere in tree B. | ||
581 | */ | 605 | */ |
582 | static int keyring_detect_cycle(struct key *A, struct key *B) | 606 | static int keyring_detect_cycle(struct key *A, struct key *B) |
583 | { | 607 | { |
@@ -657,11 +681,10 @@ too_deep: | |||
657 | cycle_detected: | 681 | cycle_detected: |
658 | ret = -EDEADLK; | 682 | ret = -EDEADLK; |
659 | goto error; | 683 | goto error; |
660 | 684 | } | |
661 | } /* end keyring_detect_cycle() */ | ||
662 | 685 | ||
663 | /* | 686 | /* |
664 | * dispose of a keyring list after the RCU grace period, freeing the unlinked | 687 | * Dispose of a keyring list after the RCU grace period, freeing the unlinked |
665 | * key | 688 | * key |
666 | */ | 689 | */ |
667 | static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) | 690 | static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) |
@@ -675,14 +698,14 @@ static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) | |||
675 | } | 698 | } |
676 | 699 | ||
677 | /* | 700 | /* |
678 | * preallocate memory so that a key can be linked into to a keyring | 701 | * Preallocate memory so that a key can be linked into to a keyring. |
679 | */ | 702 | */ |
680 | int __key_link_begin(struct key *keyring, const struct key_type *type, | 703 | int __key_link_begin(struct key *keyring, const struct key_type *type, |
681 | const char *description, | 704 | const char *description, unsigned long *_prealloc) |
682 | struct keyring_list **_prealloc) | ||
683 | __acquires(&keyring->sem) | 705 | __acquires(&keyring->sem) |
684 | { | 706 | { |
685 | struct keyring_list *klist, *nklist; | 707 | struct keyring_list *klist, *nklist; |
708 | unsigned long prealloc; | ||
686 | unsigned max; | 709 | unsigned max; |
687 | size_t size; | 710 | size_t size; |
688 | int loop, ret; | 711 | int loop, ret; |
@@ -725,6 +748,7 @@ int __key_link_begin(struct key *keyring, const struct key_type *type, | |||
725 | 748 | ||
726 | /* note replacement slot */ | 749 | /* note replacement slot */ |
727 | klist->delkey = nklist->delkey = loop; | 750 | klist->delkey = nklist->delkey = loop; |
751 | prealloc = (unsigned long)nklist; | ||
728 | goto done; | 752 | goto done; |
729 | } | 753 | } |
730 | } | 754 | } |
@@ -739,6 +763,7 @@ int __key_link_begin(struct key *keyring, const struct key_type *type, | |||
739 | if (klist && klist->nkeys < klist->maxkeys) { | 763 | if (klist && klist->nkeys < klist->maxkeys) { |
740 | /* there's sufficient slack space to append directly */ | 764 | /* there's sufficient slack space to append directly */ |
741 | nklist = NULL; | 765 | nklist = NULL; |
766 | prealloc = KEY_LINK_FIXQUOTA; | ||
742 | } else { | 767 | } else { |
743 | /* grow the key list */ | 768 | /* grow the key list */ |
744 | max = 4; | 769 | max = 4; |
@@ -773,8 +798,9 @@ int __key_link_begin(struct key *keyring, const struct key_type *type, | |||
773 | nklist->keys[nklist->delkey] = NULL; | 798 | nklist->keys[nklist->delkey] = NULL; |
774 | } | 799 | } |
775 | 800 | ||
801 | prealloc = (unsigned long)nklist | KEY_LINK_FIXQUOTA; | ||
776 | done: | 802 | done: |
777 | *_prealloc = nklist; | 803 | *_prealloc = prealloc; |
778 | kleave(" = 0"); | 804 | kleave(" = 0"); |
779 | return 0; | 805 | return 0; |
780 | 806 | ||
@@ -792,10 +818,10 @@ error_krsem: | |||
792 | } | 818 | } |
793 | 819 | ||
794 | /* | 820 | /* |
795 | * check already instantiated keys aren't going to be a problem | 821 | * Check already instantiated keys aren't going to be a problem. |
796 | * - the caller must have called __key_link_begin() | 822 | * |
797 | * - don't need to call this for keys that were created since __key_link_begin() | 823 | * The caller must have called __key_link_begin(). Don't need to call this for |
798 | * was called | 824 | * keys that were created since __key_link_begin() was called. |
799 | */ | 825 | */ |
800 | int __key_link_check_live_key(struct key *keyring, struct key *key) | 826 | int __key_link_check_live_key(struct key *keyring, struct key *key) |
801 | { | 827 | { |
@@ -807,17 +833,20 @@ int __key_link_check_live_key(struct key *keyring, struct key *key) | |||
807 | } | 833 | } |
808 | 834 | ||
809 | /* | 835 | /* |
810 | * link a key into to a keyring | 836 | * Link a key into to a keyring. |
811 | * - must be called with __key_link_begin() having being called | 837 | * |
812 | * - discard already extant link to matching key if there is one | 838 | * Must be called with __key_link_begin() having being called. Discards any |
839 | * already extant link to matching key if there is one, so that each keyring | ||
840 | * holds at most one link to any given key of a particular type+description | ||
841 | * combination. | ||
813 | */ | 842 | */ |
814 | void __key_link(struct key *keyring, struct key *key, | 843 | void __key_link(struct key *keyring, struct key *key, |
815 | struct keyring_list **_prealloc) | 844 | unsigned long *_prealloc) |
816 | { | 845 | { |
817 | struct keyring_list *klist, *nklist; | 846 | struct keyring_list *klist, *nklist; |
818 | 847 | ||
819 | nklist = *_prealloc; | 848 | nklist = (struct keyring_list *)(*_prealloc & ~KEY_LINK_FIXQUOTA); |
820 | *_prealloc = NULL; | 849 | *_prealloc = 0; |
821 | 850 | ||
822 | kenter("%d,%d,%p", keyring->serial, key->serial, nklist); | 851 | kenter("%d,%d,%p", keyring->serial, key->serial, nklist); |
823 | 852 | ||
@@ -852,34 +881,54 @@ void __key_link(struct key *keyring, struct key *key, | |||
852 | } | 881 | } |
853 | 882 | ||
854 | /* | 883 | /* |
855 | * finish linking a key into to a keyring | 884 | * Finish linking a key into to a keyring. |
856 | * - must be called with __key_link_begin() having being called | 885 | * |
886 | * Must be called with __key_link_begin() having being called. | ||
857 | */ | 887 | */ |
858 | void __key_link_end(struct key *keyring, struct key_type *type, | 888 | void __key_link_end(struct key *keyring, struct key_type *type, |
859 | struct keyring_list *prealloc) | 889 | unsigned long prealloc) |
860 | __releases(&keyring->sem) | 890 | __releases(&keyring->sem) |
861 | { | 891 | { |
862 | BUG_ON(type == NULL); | 892 | BUG_ON(type == NULL); |
863 | BUG_ON(type->name == NULL); | 893 | BUG_ON(type->name == NULL); |
864 | kenter("%d,%s,%p", keyring->serial, type->name, prealloc); | 894 | kenter("%d,%s,%lx", keyring->serial, type->name, prealloc); |
865 | 895 | ||
866 | if (type == &key_type_keyring) | 896 | if (type == &key_type_keyring) |
867 | up_write(&keyring_serialise_link_sem); | 897 | up_write(&keyring_serialise_link_sem); |
868 | 898 | ||
869 | if (prealloc) { | 899 | if (prealloc) { |
870 | kfree(prealloc); | 900 | if (prealloc & KEY_LINK_FIXQUOTA) |
871 | key_payload_reserve(keyring, | 901 | key_payload_reserve(keyring, |
872 | keyring->datalen - KEYQUOTA_LINK_BYTES); | 902 | keyring->datalen - |
903 | KEYQUOTA_LINK_BYTES); | ||
904 | kfree((struct keyring_list *)(prealloc & ~KEY_LINK_FIXQUOTA)); | ||
873 | } | 905 | } |
874 | up_write(&keyring->sem); | 906 | up_write(&keyring->sem); |
875 | } | 907 | } |
876 | 908 | ||
877 | /* | 909 | /** |
878 | * link a key to a keyring | 910 | * key_link - Link a key to a keyring |
911 | * @keyring: The keyring to make the link in. | ||
912 | * @key: The key to link to. | ||
913 | * | ||
914 | * Make a link in a keyring to a key, such that the keyring holds a reference | ||
915 | * on that key and the key can potentially be found by searching that keyring. | ||
916 | * | ||
917 | * This function will write-lock the keyring's semaphore and will consume some | ||
918 | * of the user's key data quota to hold the link. | ||
919 | * | ||
920 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, | ||
921 | * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is | ||
922 | * full, -EDQUOT if there is insufficient key data quota remaining to add | ||
923 | * another link or -ENOMEM if there's insufficient memory. | ||
924 | * | ||
925 | * It is assumed that the caller has checked that it is permitted for a link to | ||
926 | * be made (the keyring should have Write permission and the key Link | ||
927 | * permission). | ||
879 | */ | 928 | */ |
880 | int key_link(struct key *keyring, struct key *key) | 929 | int key_link(struct key *keyring, struct key *key) |
881 | { | 930 | { |
882 | struct keyring_list *prealloc; | 931 | unsigned long prealloc; |
883 | int ret; | 932 | int ret; |
884 | 933 | ||
885 | key_check(keyring); | 934 | key_check(keyring); |
@@ -895,12 +944,24 @@ int key_link(struct key *keyring, struct key *key) | |||
895 | 944 | ||
896 | return ret; | 945 | return ret; |
897 | } | 946 | } |
898 | |||
899 | EXPORT_SYMBOL(key_link); | 947 | EXPORT_SYMBOL(key_link); |
900 | 948 | ||
901 | /*****************************************************************************/ | 949 | /** |
902 | /* | 950 | * key_unlink - Unlink the first link to a key from a keyring. |
903 | * unlink the first link to a key from a keyring | 951 | * @keyring: The keyring to remove the link from. |
952 | * @key: The key the link is to. | ||
953 | * | ||
954 | * Remove a link from a keyring to a key. | ||
955 | * | ||
956 | * This function will write-lock the keyring's semaphore. | ||
957 | * | ||
958 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if | ||
959 | * the key isn't linked to by the keyring or -ENOMEM if there's insufficient | ||
960 | * memory. | ||
961 | * | ||
962 | * It is assumed that the caller has checked that it is permitted for a link to | ||
963 | * be removed (the keyring should have Write permission; no permissions are | ||
964 | * required on the key). | ||
904 | */ | 965 | */ |
905 | int key_unlink(struct key *keyring, struct key *key) | 966 | int key_unlink(struct key *keyring, struct key *key) |
906 | { | 967 | { |
@@ -968,15 +1029,12 @@ nomem: | |||
968 | ret = -ENOMEM; | 1029 | ret = -ENOMEM; |
969 | up_write(&keyring->sem); | 1030 | up_write(&keyring->sem); |
970 | goto error; | 1031 | goto error; |
971 | 1032 | } | |
972 | } /* end key_unlink() */ | ||
973 | |||
974 | EXPORT_SYMBOL(key_unlink); | 1033 | EXPORT_SYMBOL(key_unlink); |
975 | 1034 | ||
976 | /*****************************************************************************/ | ||
977 | /* | 1035 | /* |
978 | * dispose of a keyring list after the RCU grace period, releasing the keys it | 1036 | * Dispose of a keyring list after the RCU grace period, releasing the keys it |
979 | * links to | 1037 | * links to. |
980 | */ | 1038 | */ |
981 | static void keyring_clear_rcu_disposal(struct rcu_head *rcu) | 1039 | static void keyring_clear_rcu_disposal(struct rcu_head *rcu) |
982 | { | 1040 | { |
@@ -989,13 +1047,15 @@ static void keyring_clear_rcu_disposal(struct rcu_head *rcu) | |||
989 | key_put(klist->keys[loop]); | 1047 | key_put(klist->keys[loop]); |
990 | 1048 | ||
991 | kfree(klist); | 1049 | kfree(klist); |
1050 | } | ||
992 | 1051 | ||
993 | } /* end keyring_clear_rcu_disposal() */ | 1052 | /** |
994 | 1053 | * keyring_clear - Clear a keyring | |
995 | /*****************************************************************************/ | 1054 | * @keyring: The keyring to clear. |
996 | /* | 1055 | * |
997 | * clear the specified process keyring | 1056 | * Clear the contents of the specified keyring. |
998 | * - implements keyctl(KEYCTL_CLEAR) | 1057 | * |
1058 | * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring. | ||
999 | */ | 1059 | */ |
1000 | int keyring_clear(struct key *keyring) | 1060 | int keyring_clear(struct key *keyring) |
1001 | { | 1061 | { |
@@ -1027,15 +1087,13 @@ int keyring_clear(struct key *keyring) | |||
1027 | } | 1087 | } |
1028 | 1088 | ||
1029 | return ret; | 1089 | return ret; |
1030 | 1090 | } | |
1031 | } /* end keyring_clear() */ | ||
1032 | |||
1033 | EXPORT_SYMBOL(keyring_clear); | 1091 | EXPORT_SYMBOL(keyring_clear); |
1034 | 1092 | ||
1035 | /*****************************************************************************/ | ||
1036 | /* | 1093 | /* |
1037 | * dispose of the links from a revoked keyring | 1094 | * Dispose of the links from a revoked keyring. |
1038 | * - called with the key sem write-locked | 1095 | * |
1096 | * This is called with the key sem write-locked. | ||
1039 | */ | 1097 | */ |
1040 | static void keyring_revoke(struct key *keyring) | 1098 | static void keyring_revoke(struct key *keyring) |
1041 | { | 1099 | { |
@@ -1050,11 +1108,10 @@ static void keyring_revoke(struct key *keyring) | |||
1050 | rcu_assign_pointer(keyring->payload.subscriptions, NULL); | 1108 | rcu_assign_pointer(keyring->payload.subscriptions, NULL); |
1051 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); | 1109 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); |
1052 | } | 1110 | } |
1053 | 1111 | } | |
1054 | } /* end keyring_revoke() */ | ||
1055 | 1112 | ||
1056 | /* | 1113 | /* |
1057 | * Determine whether a key is dead | 1114 | * Determine whether a key is dead. |
1058 | */ | 1115 | */ |
1059 | static bool key_is_dead(struct key *key, time_t limit) | 1116 | static bool key_is_dead(struct key *key, time_t limit) |
1060 | { | 1117 | { |
@@ -1063,7 +1120,12 @@ static bool key_is_dead(struct key *key, time_t limit) | |||
1063 | } | 1120 | } |
1064 | 1121 | ||
1065 | /* | 1122 | /* |
1066 | * Collect garbage from the contents of a keyring | 1123 | * Collect garbage from the contents of a keyring, replacing the old list with |
1124 | * a new one with the pointers all shuffled down. | ||
1125 | * | ||
1126 | * Dead keys are classed as oned that are flagged as being dead or are revoked, | ||
1127 | * expired or negative keys that were revoked or expired before the specified | ||
1128 | * limit. | ||
1067 | */ | 1129 | */ |
1068 | void keyring_gc(struct key *keyring, time_t limit) | 1130 | void keyring_gc(struct key *keyring, time_t limit) |
1069 | { | 1131 | { |