aboutsummaryrefslogtreecommitdiffstats
path: root/security/keys/keyring.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/keys/keyring.c')
-rw-r--r--security/keys/keyring.c295
1 files changed, 175 insertions, 120 deletions
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index d37f713e73ce..92024ed12e0a 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -26,13 +26,13 @@
26 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem))) 26 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
27 27
28/* 28/*
29 * when plumbing the depths of the key tree, this sets a hard limit set on how 29 * When plumbing the depths of the key tree, this sets a hard limit
30 * deep we're willing to go 30 * set on how deep we're willing to go.
31 */ 31 */
32#define KEYRING_SEARCH_MAX_DEPTH 6 32#define KEYRING_SEARCH_MAX_DEPTH 6
33 33
34/* 34/*
35 * we keep all named keyrings in a hash to speed looking them up 35 * We keep all named keyrings in a hash to speed looking them up.
36 */ 36 */
37#define KEYRING_NAME_HASH_SIZE (1 << 5) 37#define KEYRING_NAME_HASH_SIZE (1 << 5)
38 38
@@ -50,7 +50,9 @@ static inline unsigned keyring_hash(const char *desc)
50} 50}
51 51
52/* 52/*
53 * the keyring type definition 53 * The keyring key type definition. Keyrings are simply keys of this type and
54 * can be treated as ordinary keys in addition to having their own special
55 * operations.
54 */ 56 */
55static int keyring_instantiate(struct key *keyring, 57static int keyring_instantiate(struct key *keyring,
56 const void *data, size_t datalen); 58 const void *data, size_t datalen);
@@ -71,19 +73,17 @@ struct key_type key_type_keyring = {
71 .describe = keyring_describe, 73 .describe = keyring_describe,
72 .read = keyring_read, 74 .read = keyring_read,
73}; 75};
74
75EXPORT_SYMBOL(key_type_keyring); 76EXPORT_SYMBOL(key_type_keyring);
76 77
77/* 78/*
78 * semaphore to serialise link/link calls to prevent two link calls in parallel 79 * Semaphore to serialise link/link calls to prevent two link calls in parallel
79 * introducing a cycle 80 * introducing a cycle.
80 */ 81 */
81static DECLARE_RWSEM(keyring_serialise_link_sem); 82static DECLARE_RWSEM(keyring_serialise_link_sem);
82 83
83/*****************************************************************************/
84/* 84/*
85 * publish the name of a keyring so that it can be found by name (if it has 85 * Publish the name of a keyring so that it can be found by name (if it has
86 * one) 86 * one).
87 */ 87 */
88static void keyring_publish_name(struct key *keyring) 88static void keyring_publish_name(struct key *keyring)
89{ 89{
@@ -102,13 +102,12 @@ static void keyring_publish_name(struct key *keyring)
102 102
103 write_unlock(&keyring_name_lock); 103 write_unlock(&keyring_name_lock);
104 } 104 }
105}
105 106
106} /* end keyring_publish_name() */
107
108/*****************************************************************************/
109/* 107/*
110 * initialise a keyring 108 * Initialise a keyring.
111 * - we object if we were given any data 109 *
110 * Returns 0 on success, -EINVAL if given any data.
112 */ 111 */
113static int keyring_instantiate(struct key *keyring, 112static int keyring_instantiate(struct key *keyring,
114 const void *data, size_t datalen) 113 const void *data, size_t datalen)
@@ -123,23 +122,20 @@ static int keyring_instantiate(struct key *keyring,
123 } 122 }
124 123
125 return ret; 124 return ret;
125}
126 126
127} /* end keyring_instantiate() */
128
129/*****************************************************************************/
130/* 127/*
131 * match keyrings on their name 128 * Match keyrings on their name
132 */ 129 */
133static int keyring_match(const struct key *keyring, const void *description) 130static int keyring_match(const struct key *keyring, const void *description)
134{ 131{
135 return keyring->description && 132 return keyring->description &&
136 strcmp(keyring->description, description) == 0; 133 strcmp(keyring->description, description) == 0;
134}
137 135
138} /* end keyring_match() */
139
140/*****************************************************************************/
141/* 136/*
142 * dispose of the data dangling from the corpse of a keyring 137 * Clean up a keyring when it is destroyed. Unpublish its name if it had one
138 * and dispose of its data.
143 */ 139 */
144static void keyring_destroy(struct key *keyring) 140static void keyring_destroy(struct key *keyring)
145{ 141{
@@ -164,12 +160,10 @@ static void keyring_destroy(struct key *keyring)
164 key_put(klist->keys[loop]); 160 key_put(klist->keys[loop]);
165 kfree(klist); 161 kfree(klist);
166 } 162 }
163}
167 164
168} /* end keyring_destroy() */
169
170/*****************************************************************************/
171/* 165/*
172 * describe the keyring 166 * Describe a keyring for /proc.
173 */ 167 */
174static void keyring_describe(const struct key *keyring, struct seq_file *m) 168static void keyring_describe(const struct key *keyring, struct seq_file *m)
175{ 169{
@@ -187,13 +181,12 @@ static void keyring_describe(const struct key *keyring, struct seq_file *m)
187 else 181 else
188 seq_puts(m, ": empty"); 182 seq_puts(m, ": empty");
189 rcu_read_unlock(); 183 rcu_read_unlock();
184}
190 185
191} /* end keyring_describe() */
192
193/*****************************************************************************/
194/* 186/*
195 * read a list of key IDs from the keyring's contents 187 * Read a list of key IDs from the keyring's contents in binary form
196 * - the keyring's semaphore is read-locked 188 *
189 * The keyring's semaphore is read-locked by the caller.
197 */ 190 */
198static long keyring_read(const struct key *keyring, 191static long keyring_read(const struct key *keyring,
199 char __user *buffer, size_t buflen) 192 char __user *buffer, size_t buflen)
@@ -241,12 +234,10 @@ static long keyring_read(const struct key *keyring,
241 234
242error: 235error:
243 return ret; 236 return ret;
237}
244 238
245} /* end keyring_read() */
246
247/*****************************************************************************/
248/* 239/*
249 * allocate a keyring and link into the destination keyring 240 * Allocate a keyring and link into the destination keyring.
250 */ 241 */
251struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, 242struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
252 const struct cred *cred, unsigned long flags, 243 const struct cred *cred, unsigned long flags,
@@ -269,20 +260,42 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
269 } 260 }
270 261
271 return keyring; 262 return keyring;
263}
272 264
273} /* end keyring_alloc() */ 265/**
274 266 * keyring_search_aux - Search a keyring tree for a key matching some criteria
275/*****************************************************************************/ 267 * @keyring_ref: A pointer to the keyring with possession indicator.
276/* 268 * @cred: The credentials to use for permissions checks.
277 * search the supplied keyring tree for a key that matches the criterion 269 * @type: The type of key to search for.
278 * - perform a breadth-then-depth search up to the prescribed limit 270 * @description: Parameter for @match.
279 * - we only find keys on which we have search permission 271 * @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 272 *
281 * feature of interest) matches 273 * 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 274 * 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 275 * 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 276 * 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 277 * of the possessor bits in permissions checking of the entire tree. In
278 * addition, the LSM gets to forbid keyring searches and key matches.
279 *
280 * The search is performed as a breadth-then-depth search up to the prescribed
281 * limit (KEYRING_SEARCH_MAX_DEPTH).
282 *
283 * Keys are matched to the type provided and are then filtered by the match
284 * function, which is given the description to use in any way it sees fit. The
285 * match function may use any attributes of a key that it wishes to to
286 * determine the match. Normally the match function from the key type would be
287 * used.
288 *
289 * RCU is used to prevent the keyring key lists from disappearing without the
290 * need to take lots of locks.
291 *
292 * Returns a pointer to the found key and increments the key usage count if
293 * successful; -EAGAIN if no matching keys were found, or if expired or revoked
294 * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
295 * specified keyring wasn't a keyring.
296 *
297 * In the case of a successful return, the possession attribute from
298 * @keyring_ref is propagated to the returned key reference.
286 */ 299 */
287key_ref_t keyring_search_aux(key_ref_t keyring_ref, 300key_ref_t keyring_search_aux(key_ref_t keyring_ref,
288 const struct cred *cred, 301 const struct cred *cred,
@@ -444,17 +457,16 @@ error_2:
444 rcu_read_unlock(); 457 rcu_read_unlock();
445error: 458error:
446 return key_ref; 459 return key_ref;
460}
447 461
448} /* end keyring_search_aux() */ 462/**
449 463 * keyring_search - Search the supplied keyring tree for a matching key
450/*****************************************************************************/ 464 * @keyring: The root of the keyring tree to be searched.
451/* 465 * @type: The type of keyring we want to find.
452 * search the supplied keyring tree for a key that matches the criterion 466 * @description: The name of the keyring we want to find.
453 * - perform a breadth-then-depth search up to the prescribed limit 467 *
454 * - we only find keys on which we have search permission 468 * As keyring_search_aux() above, but using the current task's credentials and
455 * - we readlock the keyrings as we search down the tree 469 * 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 */ 470 */
459key_ref_t keyring_search(key_ref_t keyring, 471key_ref_t keyring_search(key_ref_t keyring,
460 struct key_type *type, 472 struct key_type *type,
@@ -465,16 +477,23 @@ key_ref_t keyring_search(key_ref_t keyring,
465 477
466 return keyring_search_aux(keyring, current->cred, 478 return keyring_search_aux(keyring, current->cred,
467 type, description, type->match); 479 type, description, type->match);
468 480}
469} /* end keyring_search() */
470
471EXPORT_SYMBOL(keyring_search); 481EXPORT_SYMBOL(keyring_search);
472 482
473/*****************************************************************************/
474/* 483/*
475 * search the given keyring only (no recursion) 484 * Search the given keyring only (no recursion).
476 * - keyring must be locked by caller 485 *
477 * - caller must guarantee that the keyring is a keyring 486 * The caller must guarantee that the keyring is a keyring and that the
487 * permission is granted to search the keyring as no check is made here.
488 *
489 * RCU is used to make it unnecessary to lock the keyring key list here.
490 *
491 * Returns a pointer to the found key with usage count incremented if
492 * successful and returns -ENOKEY if not found. Revoked keys and keys not
493 * providing the requested permission are skipped over.
494 *
495 * If successful, the possession indicator is propagated from the keyring ref
496 * to the returned key reference.
478 */ 497 */
479key_ref_t __keyring_search_one(key_ref_t keyring_ref, 498key_ref_t __keyring_search_one(key_ref_t keyring_ref,
480 const struct key_type *ktype, 499 const struct key_type *ktype,
@@ -514,14 +533,18 @@ found:
514 atomic_inc(&key->usage); 533 atomic_inc(&key->usage);
515 rcu_read_unlock(); 534 rcu_read_unlock();
516 return make_key_ref(key, possessed); 535 return make_key_ref(key, possessed);
536}
517 537
518} /* end __keyring_search_one() */
519
520/*****************************************************************************/
521/* 538/*
522 * find a keyring with the specified name 539 * Find a keyring with the specified name.
523 * - all named keyrings are searched 540 *
524 * - normally only finds keyrings with search permission for the current process 541 * All named keyrings in the current user namespace are searched, provided they
542 * grant Search permission directly to the caller (unless this check is
543 * skipped). Keyrings whose usage points have reached zero or who have been
544 * revoked are skipped.
545 *
546 * Returns a pointer to the keyring with the keyring's refcount having being
547 * incremented on success. -ENOKEY is returned if a key could not be found.
525 */ 548 */
526struct key *find_keyring_by_name(const char *name, bool skip_perm_check) 549struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
527{ 550{
@@ -569,15 +592,14 @@ struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
569out: 592out:
570 read_unlock(&keyring_name_lock); 593 read_unlock(&keyring_name_lock);
571 return keyring; 594 return keyring;
595}
572 596
573} /* end find_keyring_by_name() */
574
575/*****************************************************************************/
576/* 597/*
577 * see if a cycle will will be created by inserting acyclic tree B in acyclic 598 * 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) 599 * 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 600 *
580 * just be a matter of seeing if node A is somewhere in tree B 601 * Since we are adding B to A at the top level, checking for cycles should just
602 * be a matter of seeing if node A is somewhere in tree B.
581 */ 603 */
582static int keyring_detect_cycle(struct key *A, struct key *B) 604static int keyring_detect_cycle(struct key *A, struct key *B)
583{ 605{
@@ -657,11 +679,10 @@ too_deep:
657cycle_detected: 679cycle_detected:
658 ret = -EDEADLK; 680 ret = -EDEADLK;
659 goto error; 681 goto error;
660 682}
661} /* end keyring_detect_cycle() */
662 683
663/* 684/*
664 * dispose of a keyring list after the RCU grace period, freeing the unlinked 685 * Dispose of a keyring list after the RCU grace period, freeing the unlinked
665 * key 686 * key
666 */ 687 */
667static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) 688static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
@@ -675,7 +696,7 @@ static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
675} 696}
676 697
677/* 698/*
678 * preallocate memory so that a key can be linked into to a keyring 699 * Preallocate memory so that a key can be linked into to a keyring.
679 */ 700 */
680int __key_link_begin(struct key *keyring, const struct key_type *type, 701int __key_link_begin(struct key *keyring, const struct key_type *type,
681 const char *description, 702 const char *description,
@@ -792,10 +813,10 @@ error_krsem:
792} 813}
793 814
794/* 815/*
795 * check already instantiated keys aren't going to be a problem 816 * Check already instantiated keys aren't going to be a problem.
796 * - the caller must have called __key_link_begin() 817 *
797 * - don't need to call this for keys that were created since __key_link_begin() 818 * The caller must have called __key_link_begin(). Don't need to call this for
798 * was called 819 * keys that were created since __key_link_begin() was called.
799 */ 820 */
800int __key_link_check_live_key(struct key *keyring, struct key *key) 821int __key_link_check_live_key(struct key *keyring, struct key *key)
801{ 822{
@@ -807,9 +828,12 @@ int __key_link_check_live_key(struct key *keyring, struct key *key)
807} 828}
808 829
809/* 830/*
810 * link a key into to a keyring 831 * Link a key into to a keyring.
811 * - must be called with __key_link_begin() having being called 832 *
812 * - discard already extant link to matching key if there is one 833 * Must be called with __key_link_begin() having being called. Discards any
834 * already extant link to matching key if there is one, so that each keyring
835 * holds at most one link to any given key of a particular type+description
836 * combination.
813 */ 837 */
814void __key_link(struct key *keyring, struct key *key, 838void __key_link(struct key *keyring, struct key *key,
815 struct keyring_list **_prealloc) 839 struct keyring_list **_prealloc)
@@ -852,8 +876,9 @@ void __key_link(struct key *keyring, struct key *key,
852} 876}
853 877
854/* 878/*
855 * finish linking a key into to a keyring 879 * Finish linking a key into to a keyring.
856 * - must be called with __key_link_begin() having being called 880 *
881 * Must be called with __key_link_begin() having being called.
857 */ 882 */
858void __key_link_end(struct key *keyring, struct key_type *type, 883void __key_link_end(struct key *keyring, struct key_type *type,
859 struct keyring_list *prealloc) 884 struct keyring_list *prealloc)
@@ -874,8 +899,25 @@ void __key_link_end(struct key *keyring, struct key_type *type,
874 up_write(&keyring->sem); 899 up_write(&keyring->sem);
875} 900}
876 901
877/* 902/**
878 * link a key to a keyring 903 * key_link - Link a key to a keyring
904 * @keyring: The keyring to make the link in.
905 * @key: The key to link to.
906 *
907 * Make a link in a keyring to a key, such that the keyring holds a reference
908 * on that key and the key can potentially be found by searching that keyring.
909 *
910 * This function will write-lock the keyring's semaphore and will consume some
911 * of the user's key data quota to hold the link.
912 *
913 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
914 * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
915 * full, -EDQUOT if there is insufficient key data quota remaining to add
916 * another link or -ENOMEM if there's insufficient memory.
917 *
918 * It is assumed that the caller has checked that it is permitted for a link to
919 * be made (the keyring should have Write permission and the key Link
920 * permission).
879 */ 921 */
880int key_link(struct key *keyring, struct key *key) 922int key_link(struct key *keyring, struct key *key)
881{ 923{
@@ -895,12 +937,24 @@ int key_link(struct key *keyring, struct key *key)
895 937
896 return ret; 938 return ret;
897} 939}
898
899EXPORT_SYMBOL(key_link); 940EXPORT_SYMBOL(key_link);
900 941
901/*****************************************************************************/ 942/**
902/* 943 * key_unlink - Unlink the first link to a key from a keyring.
903 * unlink the first link to a key from a keyring 944 * @keyring: The keyring to remove the link from.
945 * @key: The key the link is to.
946 *
947 * Remove a link from a keyring to a key.
948 *
949 * This function will write-lock the keyring's semaphore.
950 *
951 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
952 * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
953 * memory.
954 *
955 * It is assumed that the caller has checked that it is permitted for a link to
956 * be removed (the keyring should have Write permission; no permissions are
957 * required on the key).
904 */ 958 */
905int key_unlink(struct key *keyring, struct key *key) 959int key_unlink(struct key *keyring, struct key *key)
906{ 960{
@@ -968,15 +1022,12 @@ nomem:
968 ret = -ENOMEM; 1022 ret = -ENOMEM;
969 up_write(&keyring->sem); 1023 up_write(&keyring->sem);
970 goto error; 1024 goto error;
971 1025}
972} /* end key_unlink() */
973
974EXPORT_SYMBOL(key_unlink); 1026EXPORT_SYMBOL(key_unlink);
975 1027
976/*****************************************************************************/
977/* 1028/*
978 * dispose of a keyring list after the RCU grace period, releasing the keys it 1029 * Dispose of a keyring list after the RCU grace period, releasing the keys it
979 * links to 1030 * links to.
980 */ 1031 */
981static void keyring_clear_rcu_disposal(struct rcu_head *rcu) 1032static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
982{ 1033{
@@ -989,13 +1040,15 @@ static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
989 key_put(klist->keys[loop]); 1040 key_put(klist->keys[loop]);
990 1041
991 kfree(klist); 1042 kfree(klist);
1043}
992 1044
993} /* end keyring_clear_rcu_disposal() */ 1045/**
994 1046 * keyring_clear - Clear a keyring
995/*****************************************************************************/ 1047 * @keyring: The keyring to clear.
996/* 1048 *
997 * clear the specified process keyring 1049 * Clear the contents of the specified keyring.
998 * - implements keyctl(KEYCTL_CLEAR) 1050 *
1051 * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
999 */ 1052 */
1000int keyring_clear(struct key *keyring) 1053int keyring_clear(struct key *keyring)
1001{ 1054{
@@ -1027,15 +1080,13 @@ int keyring_clear(struct key *keyring)
1027 } 1080 }
1028 1081
1029 return ret; 1082 return ret;
1030 1083}
1031} /* end keyring_clear() */
1032
1033EXPORT_SYMBOL(keyring_clear); 1084EXPORT_SYMBOL(keyring_clear);
1034 1085
1035/*****************************************************************************/
1036/* 1086/*
1037 * dispose of the links from a revoked keyring 1087 * Dispose of the links from a revoked keyring.
1038 * - called with the key sem write-locked 1088 *
1089 * This is called with the key sem write-locked.
1039 */ 1090 */
1040static void keyring_revoke(struct key *keyring) 1091static void keyring_revoke(struct key *keyring)
1041{ 1092{
@@ -1050,11 +1101,10 @@ static void keyring_revoke(struct key *keyring)
1050 rcu_assign_pointer(keyring->payload.subscriptions, NULL); 1101 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1051 call_rcu(&klist->rcu, keyring_clear_rcu_disposal); 1102 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1052 } 1103 }
1053 1104}
1054} /* end keyring_revoke() */
1055 1105
1056/* 1106/*
1057 * Determine whether a key is dead 1107 * Determine whether a key is dead.
1058 */ 1108 */
1059static bool key_is_dead(struct key *key, time_t limit) 1109static bool key_is_dead(struct key *key, time_t limit)
1060{ 1110{
@@ -1063,7 +1113,12 @@ static bool key_is_dead(struct key *key, time_t limit)
1063} 1113}
1064 1114
1065/* 1115/*
1066 * Collect garbage from the contents of a keyring 1116 * Collect garbage from the contents of a keyring, replacing the old list with
1117 * a new one with the pointers all shuffled down.
1118 *
1119 * Dead keys are classed as oned that are flagged as being dead or are revoked,
1120 * expired or negative keys that were revoked or expired before the specified
1121 * limit.
1067 */ 1122 */
1068void keyring_gc(struct key *keyring, time_t limit) 1123void keyring_gc(struct key *keyring, time_t limit)
1069{ 1124{