Lines Matching full:key

2 /* Basic authentication token and access key management
26 unsigned int key_quota_root_maxkeys = 1000000; /* root's key count quota */
27 unsigned int key_quota_root_maxbytes = 25000000; /* root's key space quota */
28 unsigned int key_quota_maxkeys = 200; /* general key count quota */
29 unsigned int key_quota_maxbytes = 20000; /* general key space quota */
34 /* We serialise key instantiation and link */
38 void __key_check(const struct key *key) in __key_check() argument
40 printk("__key_check: key %p {%08x} should be {%08x}\n", in __key_check()
41 key, key->magic, KEY_DEBUG_MAGIC); in __key_check()
47 * Get the key quota record for a user, allocating a new record if one doesn't
130 * Allocate a serial number for a key. These are assigned randomly to avoid
133 static inline void key_alloc_serial(struct key *key) in key_alloc_serial() argument
136 struct key *xkey; in key_alloc_serial()
141 get_random_bytes(&key->serial, sizeof(key->serial)); in key_alloc_serial()
143 key->serial >>= 1; /* negative numbers are not permitted */ in key_alloc_serial()
144 } while (key->serial < 3); in key_alloc_serial()
154 xkey = rb_entry(parent, struct key, serial_node); in key_alloc_serial()
156 if (key->serial < xkey->serial) in key_alloc_serial()
158 else if (key->serial > xkey->serial) in key_alloc_serial()
164 /* we've found a suitable hole - arrange for this key to occupy it */ in key_alloc_serial()
165 rb_link_node(&key->serial_node, parent, p); in key_alloc_serial()
166 rb_insert_color(&key->serial_node, &key_serial_tree); in key_alloc_serial()
171 /* we found a key with the proposed serial number - walk the tree from in key_alloc_serial()
175 key->serial++; in key_alloc_serial()
176 if (key->serial < 3) { in key_alloc_serial()
177 key->serial = 3; in key_alloc_serial()
185 xkey = rb_entry(parent, struct key, serial_node); in key_alloc_serial()
186 if (key->serial < xkey->serial) in key_alloc_serial()
192 * key_alloc - Allocate a key of the specified type.
193 * @type: The type of key to allocate.
194 * @desc: The key description to allow the key to be searched out.
195 * @uid: The owner of the new key.
196 * @gid: The group ID for the new key's group permissions.
198 * @perm: The permissions mask of the new key.
202 * Allocate a key of the specified type with the attributes given. The key is
204 * key before returning.
209 * The user's key count quota is updated to reflect the creation of the key and
210 * the user's key data quota has the default for the key type reserved. The
214 * The LSM security modules can prevent a key being created, in which case
217 * Returns a pointer to the new key if successful and an error code otherwise.
219 * Note that the caller needs to ensure the key type isn't uninstantiated.
221 * be done by either never unregistering the key type, or making sure
224 struct key *key_alloc(struct key_type *type, const char *desc, in key_alloc()
230 struct key *key; in key_alloc() local
235 key = ERR_PTR(-EINVAL); in key_alloc()
242 key = ERR_PTR(ret); in key_alloc()
250 /* get hold of the key tracking for this user */ in key_alloc()
255 /* check that the user's quota permits allocation of another key and in key_alloc()
276 /* allocate and initialise the key and its description */ in key_alloc()
277 key = kmem_cache_zalloc(key_jar, GFP_KERNEL); in key_alloc()
278 if (!key) in key_alloc()
281 key->index_key.desc_len = desclen; in key_alloc()
282 key->index_key.description = kmemdup(desc, desclen + 1, GFP_KERNEL); in key_alloc()
283 if (!key->index_key.description) in key_alloc()
285 key->index_key.type = type; in key_alloc()
286 key_set_index_key(&key->index_key); in key_alloc()
288 refcount_set(&key->usage, 1); in key_alloc()
289 init_rwsem(&key->sem); in key_alloc()
290 lockdep_set_class(&key->sem, &type->lock_class); in key_alloc()
291 key->user = user; in key_alloc()
292 key->quotalen = quotalen; in key_alloc()
293 key->datalen = type->def_datalen; in key_alloc()
294 key->uid = uid; in key_alloc()
295 key->gid = gid; in key_alloc()
296 key->perm = perm; in key_alloc()
297 key->expiry = TIME64_MAX; in key_alloc()
298 key->restrict_link = restrict_link; in key_alloc()
299 key->last_used_at = ktime_get_real_seconds(); in key_alloc()
302 key->flags |= 1 << KEY_FLAG_IN_QUOTA; in key_alloc()
304 key->flags |= 1 << KEY_FLAG_BUILTIN; in key_alloc()
306 key->flags |= 1 << KEY_FLAG_UID_KEYRING; in key_alloc()
308 key->flags |= 1 << KEY_FLAG_KEEP; in key_alloc()
311 key->magic = KEY_DEBUG_MAGIC; in key_alloc()
314 /* let the security module know about the key */ in key_alloc()
315 ret = security_key_alloc(key, cred, flags); in key_alloc()
319 /* publish the key by giving it a serial number */ in key_alloc()
320 refcount_inc(&key->domain_tag->usage); in key_alloc()
322 key_alloc_serial(key); in key_alloc()
325 return key; in key_alloc()
328 kfree(key->description); in key_alloc()
329 kmem_cache_free(key_jar, key); in key_alloc()
337 key = ERR_PTR(ret); in key_alloc()
341 kmem_cache_free(key_jar, key); in key_alloc()
351 key = ERR_PTR(-ENOMEM); in key_alloc()
357 key = ERR_PTR(-EDQUOT); in key_alloc()
363 * key_payload_reserve - Adjust data quota reservation for the key's payload
364 * @key: The key to make the reservation for.
367 * Adjust the amount of the owning user's key data quota that a key reserves.
373 int key_payload_reserve(struct key *key, size_t datalen) in key_payload_reserve() argument
375 int delta = (int)datalen - key->datalen; in key_payload_reserve()
378 key_check(key); in key_payload_reserve()
381 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { in key_payload_reserve()
382 unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ? in key_payload_reserve()
386 spin_lock_irqsave(&key->user->lock, flags); in key_payload_reserve()
389 (key->user->qnbytes + delta > maxbytes || in key_payload_reserve()
390 key->user->qnbytes + delta < key->user->qnbytes)) { in key_payload_reserve()
394 key->user->qnbytes += delta; in key_payload_reserve()
395 key->quotalen += delta; in key_payload_reserve()
397 spin_unlock_irqrestore(&key->user->lock, flags); in key_payload_reserve()
402 key->datalen = datalen; in key_payload_reserve()
409 * Change the key state to being instantiated.
411 static void mark_key_instantiated(struct key *key, int reject_error) in mark_key_instantiated() argument
416 smp_store_release(&key->state, in mark_key_instantiated()
421 * Instantiate a key and link it into the target keyring atomically. Must be
422 * called with the target keyring's semaphore writelocked. The target key's
426 static int __key_instantiate_and_link(struct key *key, in __key_instantiate_and_link() argument
428 struct key *keyring, in __key_instantiate_and_link()
429 struct key *authkey, in __key_instantiate_and_link()
434 key_check(key); in __key_instantiate_and_link()
443 if (key->state == KEY_IS_UNINSTANTIATED) { in __key_instantiate_and_link()
444 /* instantiate the key */ in __key_instantiate_and_link()
445 ret = key->type->instantiate(key, prep); in __key_instantiate_and_link()
448 /* mark the key as being instantiated */ in __key_instantiate_and_link()
449 atomic_inc(&key->user->nikeys); in __key_instantiate_and_link()
450 mark_key_instantiated(key, 0); in __key_instantiate_and_link()
451 notify_key(key, NOTIFY_KEY_INSTANTIATED, 0); in __key_instantiate_and_link()
453 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in __key_instantiate_and_link()
459 set_bit(KEY_FLAG_KEEP, &key->flags); in __key_instantiate_and_link()
461 __key_link(keyring, key, _edit); in __key_instantiate_and_link()
464 /* disable the authorisation key */ in __key_instantiate_and_link()
469 key_set_expiry(key, prep->expiry); in __key_instantiate_and_link()
475 /* wake up anyone waiting for a key to be constructed */ in __key_instantiate_and_link()
477 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in __key_instantiate_and_link()
483 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
484 * @key: The key to instantiate.
490 * Instantiate a key that's in the uninstantiated state using the provided data
495 * waiting for the key is woken up. If the key was already instantiated,
498 int key_instantiate_and_link(struct key *key, in key_instantiate_and_link() argument
501 struct key *keyring, in key_instantiate_and_link()
502 struct key *authkey) in key_instantiate_and_link()
509 prep.orig_description = key->description; in key_instantiate_and_link()
512 prep.quotalen = key->type->def_datalen; in key_instantiate_and_link()
514 if (key->type->preparse) { in key_instantiate_and_link()
515 ret = key->type->preparse(&prep); in key_instantiate_and_link()
521 ret = __key_link_lock(keyring, &key->index_key); in key_instantiate_and_link()
525 ret = __key_link_begin(keyring, &key->index_key, &edit); in key_instantiate_and_link()
532 ret = keyres->check(keyring, key->type, &prep.payload, in key_instantiate_and_link()
533 keyres->key); in key_instantiate_and_link()
539 ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit); in key_instantiate_and_link()
543 __key_link_end(keyring, &key->index_key, edit); in key_instantiate_and_link()
546 if (key->type->preparse) in key_instantiate_and_link()
547 key->type->free_preparse(&prep); in key_instantiate_and_link()
554 * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
555 * @key: The key to instantiate.
556 * @timeout: The timeout on the negative key.
557 * @error: The error to return when the key is hit.
561 * Negatively instantiate a key that's in the uninstantiated state and, if
563 * destination keyring if one is supplied. The key and any links to the key
568 * key expires.
571 * waiting for the key is woken up. If the key was already instantiated,
574 int key_reject_and_link(struct key *key, in key_reject_and_link() argument
577 struct key *keyring, in key_reject_and_link()
578 struct key *authkey) in key_reject_and_link()
583 key_check(key); in key_reject_and_link()
593 link_ret = __key_link_lock(keyring, &key->index_key); in key_reject_and_link()
595 link_ret = __key_link_begin(keyring, &key->index_key, &edit); in key_reject_and_link()
597 __key_link_end(keyring, &key->index_key, edit); in key_reject_and_link()
604 if (key->state == KEY_IS_UNINSTANTIATED) { in key_reject_and_link()
605 /* mark the key as being negatively instantiated */ in key_reject_and_link()
606 atomic_inc(&key->user->nikeys); in key_reject_and_link()
607 mark_key_instantiated(key, -error); in key_reject_and_link()
608 notify_key(key, NOTIFY_KEY_INSTANTIATED, -error); in key_reject_and_link()
609 key_set_expiry(key, ktime_get_real_seconds() + timeout); in key_reject_and_link()
611 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in key_reject_and_link()
618 __key_link(keyring, key, &edit); in key_reject_and_link()
620 /* disable the authorisation key */ in key_reject_and_link()
628 __key_link_end(keyring, &key->index_key, edit); in key_reject_and_link()
630 /* wake up anyone waiting for a key to be constructed */ in key_reject_and_link()
632 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in key_reject_and_link()
639 * key_put - Discard a reference to a key.
640 * @key: The key to discard a reference from.
642 * Discard a reference to a key, and when all the references are gone, we
646 void key_put(struct key *key) in key_put() argument
648 if (key) { in key_put()
649 key_check(key); in key_put()
651 if (refcount_dec_and_test(&key->usage)) { in key_put()
654 /* deal with the user's key tracking and quota */ in key_put()
655 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { in key_put()
656 spin_lock_irqsave(&key->user->lock, flags); in key_put()
657 key->user->qnkeys--; in key_put()
658 key->user->qnbytes -= key->quotalen; in key_put()
659 spin_unlock_irqrestore(&key->user->lock, flags); in key_put()
661 smp_mb(); /* key->user before FINAL_PUT set. */ in key_put()
662 set_bit(KEY_FLAG_FINAL_PUT, &key->flags); in key_put()
670 * Find a key by its serial number.
672 struct key *key_lookup(key_serial_t id) in key_lookup()
675 struct key *key; in key_lookup() local
679 /* search the tree for the specified key */ in key_lookup()
682 key = rb_entry(n, struct key, serial_node); in key_lookup()
684 if (id < key->serial) in key_lookup()
686 else if (id > key->serial) in key_lookup()
693 key = ERR_PTR(-ENOKEY); in key_lookup()
697 /* A key is allowed to be looked up only if someone still owns a in key_lookup()
700 if (!refcount_inc_not_zero(&key->usage)) in key_lookup()
705 return key; in key_lookup()
710 * Find and lock the specified key type against removal.
721 /* look up the key type to see if it's one of the registered kernel in key_type_lookup()
735 void key_set_timeout(struct key *key, unsigned timeout) in key_set_timeout() argument
740 down_write(&key->sem); in key_set_timeout()
744 key_set_expiry(key, expiry); in key_set_timeout()
746 up_write(&key->sem); in key_set_timeout()
751 * Unlock a key type locked by key_type_lookup().
759 * Attempt to update an existing key.
761 * The key is given to us with an incremented refcount that we need to discard
767 struct key *key = key_ref_to_ptr(key_ref); in __key_update() local
770 /* need write permission on the key to update it */ in __key_update()
776 if (!key->type->update) in __key_update()
779 down_write(&key->sem); in __key_update()
781 ret = key->type->update(key, prep); in __key_update()
783 /* Updating a negative key positively instantiates it */ in __key_update()
784 mark_key_instantiated(key, 0); in __key_update()
785 notify_key(key, NOTIFY_KEY_UPDATED, 0); in __key_update()
788 up_write(&key->sem); in __key_update()
796 key_put(key); in __key_update()
802 * Create or potentially update a key. The combined logic behind
820 struct key *keyring, *key = NULL; in __key_create_or_update() local
825 /* look up the key type to see if it's one of the registered kernel in __key_create_or_update()
884 &prep.payload, restrict_link->key); in __key_create_or_update()
891 /* if we're going to allocate a new key, we're going to have in __key_create_or_update()
899 /* if it's requested and possible to update this type of key, search in __key_create_or_update()
900 * for an existing key of the same type and description in the in __key_create_or_update()
931 /* allocate a new key */ in __key_create_or_update()
932 key = key_alloc(index_key.type, index_key.description, in __key_create_or_update()
934 if (IS_ERR(key)) { in __key_create_or_update()
935 key_ref = ERR_CAST(key); in __key_create_or_update()
940 ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit); in __key_create_or_update()
942 key_put(key); in __key_create_or_update()
947 security_key_post_create_or_update(keyring, key, payload, plen, flags, in __key_create_or_update()
950 key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); in __key_create_or_update()
963 /* we found a matching key, so we're going to try to update it in __key_create_or_update()
964 * - we can drop the locks first as we have the key pinned in __key_create_or_update()
968 key = key_ref_to_ptr(key_ref); in __key_create_or_update()
969 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) { in __key_create_or_update()
970 ret = wait_for_key_construction(key, true); in __key_create_or_update()
981 security_key_post_create_or_update(keyring, key, payload, plen, in __key_create_or_update()
988 * key_create_or_update - Update or create and instantiate a key.
990 * @type: The type of key.
991 * @description: The searchable description for the key.
992 * @payload: The data to use to instantiate or update the key.
994 * @perm: The permissions mask for a new key.
995 * @flags: The quota flags for a new key.
997 * Search the destination keyring for a key of the same description and if one
1001 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
1004 * Returns a pointer to the new key if successful, -ENODEV if the key type
1007 * creation of the key.
1010 * the key ref before it is returned.
1026 * key_create - Create and instantiate a key.
1028 * @type: The type of key.
1029 * @description: The searchable description for the key.
1030 * @payload: The data to use to instantiate or update the key.
1032 * @perm: The permissions mask for a new key.
1033 * @flags: The quota flags for a new key.
1035 * Create and instantiate a new key and link to it from the destination keyring.
1037 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
1040 * Returns a pointer to the new key if successful, -EEXIST if a key with the
1041 * same description already exists, -ENODEV if the key type wasn't available,
1044 * key.
1047 * the key ref before it is returned.
1063 * key_update - Update a key's contents.
1064 * @key_ref: The pointer (plus possession flag) to the key.
1065 * @payload: The data to be used to update the key.
1068 * Attempt to update the contents of a key with the given payload data. The
1069 * caller must be granted Write permission on the key. Negative keys can be
1072 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
1073 * type does not support updating. The key type may return other errors.
1078 struct key *key = key_ref_to_ptr(key_ref); in key_update() local
1081 key_check(key); in key_update()
1083 /* the key must be writable */ in key_update()
1089 if (!key->type->update) in key_update()
1095 prep.quotalen = key->type->def_datalen; in key_update()
1097 if (key->type->preparse) { in key_update()
1098 ret = key->type->preparse(&prep); in key_update()
1103 down_write(&key->sem); in key_update()
1105 ret = key->type->update(key, &prep); in key_update()
1107 /* Updating a negative key positively instantiates it */ in key_update()
1108 mark_key_instantiated(key, 0); in key_update()
1109 notify_key(key, NOTIFY_KEY_UPDATED, 0); in key_update()
1112 up_write(&key->sem); in key_update()
1115 if (key->type->preparse) in key_update()
1116 key->type->free_preparse(&prep); in key_update()
1122 * key_revoke - Revoke a key.
1123 * @key: The key to be revoked.
1125 * Mark a key as being revoked and ask the type to free up its resources. The
1126 * revocation timeout is set and the key and all its links will be
1130 void key_revoke(struct key *key) in key_revoke() argument
1134 key_check(key); in key_revoke()
1136 /* make sure no one's trying to change or use the key when we mark it in key_revoke()
1138 * authorisation key whilst holding the sem on a key we've just in key_revoke()
1141 down_write_nested(&key->sem, 1); in key_revoke()
1142 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) { in key_revoke()
1143 notify_key(key, NOTIFY_KEY_REVOKED, 0); in key_revoke()
1144 if (key->type->revoke) in key_revoke()
1145 key->type->revoke(key); in key_revoke()
1149 if (key->revoked_at == 0 || key->revoked_at > time) { in key_revoke()
1150 key->revoked_at = time; in key_revoke()
1151 key_schedule_gc(key->revoked_at + key_gc_delay); in key_revoke()
1155 up_write(&key->sem); in key_revoke()
1160 * key_invalidate - Invalidate a key.
1161 * @key: The key to be invalidated.
1163 * Mark a key as being invalidated and have it cleaned up immediately. The key
1166 void key_invalidate(struct key *key) in key_invalidate() argument
1168 kenter("%d", key_serial(key)); in key_invalidate()
1170 key_check(key); in key_invalidate()
1172 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1173 down_write_nested(&key->sem, 1); in key_invalidate()
1174 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1175 notify_key(key, NOTIFY_KEY_INVALIDATED, 0); in key_invalidate()
1178 up_write(&key->sem); in key_invalidate()
1184 * generic_key_instantiate - Simple instantiation of a key from preparsed data
1185 * @key: The key to be instantiated
1188 * Instantiate a key from preparsed data. We assume we can just copy the data
1191 * This can be pointed to directly by the key type instantiate op pointer.
1193 int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep) in generic_key_instantiate() argument
1199 ret = key_payload_reserve(key, prep->quotalen); in generic_key_instantiate()
1201 rcu_assign_keypointer(key, prep->payload.data[0]); in generic_key_instantiate()
1202 key->payload.data[1] = prep->payload.data[1]; in generic_key_instantiate()
1203 key->payload.data[2] = prep->payload.data[2]; in generic_key_instantiate()
1204 key->payload.data[3] = prep->payload.data[3]; in generic_key_instantiate()
1216 * register_key_type - Register a type of key.
1217 * @ktype: The new key type.
1219 * Register a new key type.
1233 /* disallow key types with the same name */ in register_key_type()
1242 pr_notice("Key type %s registered\n", ktype->name); in register_key_type()
1252 * unregister_key_type - Unregister a type of key.
1253 * @ktype: The key type.
1255 * Unregister a key type and mark all the extant keys of this type as dead.
1265 pr_notice("Key type %s unregistered\n", ktype->name); in unregister_key_type()
1271 * Initialise the key management state.
1276 key_jar = kmem_cache_create("key_jar", sizeof(struct key), in key_init()
1279 /* add the special key types */ in key_init()