Lines Matching refs:key
2 /* Basic authentication token and access key management
27 unsigned int key_quota_root_maxkeys = 1000000; /* root's key count quota */
28 unsigned int key_quota_root_maxbytes = 25000000; /* root's key space quota */
29 unsigned int key_quota_maxkeys = 200; /* general key count quota */
30 unsigned int key_quota_maxbytes = 20000; /* general key space quota */
35 /* We serialise key instantiation and link */
39 void __key_check(const struct key *key)
41 printk("__key_check: key %p {%08x} should be {%08x}\n",
42 key, key->magic, KEY_DEBUG_MAGIC);
48 * Get the key quota record for a user, allocating a new record if one doesn't
131 * Allocate a serial number for a key. These are assigned randomly to avoid
134 static inline void key_alloc_serial(struct key *key)
137 struct key *xkey;
142 get_random_bytes(&key->serial, sizeof(key->serial));
144 key->serial >>= 1; /* negative numbers are not permitted */
145 } while (key->serial < 3);
155 xkey = rb_entry(parent, struct key, serial_node);
157 if (key->serial < xkey->serial)
159 else if (key->serial > xkey->serial)
165 /* we've found a suitable hole - arrange for this key to occupy it */
166 rb_link_node(&key->serial_node, parent, p);
167 rb_insert_color(&key->serial_node, &key_serial_tree);
172 /* we found a key with the proposed serial number - walk the tree from
176 key->serial++;
177 if (key->serial < 3) {
178 key->serial = 3;
186 xkey = rb_entry(parent, struct key, serial_node);
187 if (key->serial < xkey->serial)
193 * key_alloc - Allocate a key of the specified type.
194 * @type: The type of key to allocate.
195 * @desc: The key description to allow the key to be searched out.
196 * @uid: The owner of the new key.
197 * @gid: The group ID for the new key's group permissions.
199 * @perm: The permissions mask of the new key.
203 * Allocate a key of the specified type with the attributes given. The key is
205 * key before returning.
210 * The user's key count quota is updated to reflect the creation of the key and
211 * the user's key data quota has the default for the key type reserved. The
215 * The LSM security modules can prevent a key being created, in which case
218 * Returns a pointer to the new key if successful and an error code otherwise.
220 * Note that the caller needs to ensure the key type isn't uninstantiated.
222 * be done by either never unregistering the key type, or making sure
225 struct key *key_alloc(struct key_type *type, const char *desc,
231 struct key *key;
235 key = ERR_PTR(-EINVAL);
242 key = ERR_PTR(ret);
250 /* get hold of the key tracking for this user */
255 /* check that the user's quota permits allocation of another key and
276 /* allocate and initialise the key and its description */
277 key = kmem_cache_zalloc(key_jar, GFP_KERNEL);
278 if (!key)
281 key->index_key.desc_len = desclen;
282 key->index_key.description = kmemdup(desc, desclen + 1, GFP_KERNEL);
283 if (!key->index_key.description)
285 key->index_key.type = type;
286 key_set_index_key(&key->index_key);
288 refcount_set(&key->usage, 1);
289 init_rwsem(&key->sem);
290 lockdep_set_class(&key->sem, &type->lock_class);
291 key->user = user;
292 key->quotalen = quotalen;
293 key->datalen = type->def_datalen;
294 key->uid = uid;
295 key->gid = gid;
296 key->perm = perm;
297 key->expiry = TIME64_MAX;
298 key->restrict_link = restrict_link;
299 key->last_used_at = ktime_get_real_seconds();
302 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
304 key->flags |= 1 << KEY_FLAG_BUILTIN;
306 key->flags |= 1 << KEY_FLAG_UID_KEYRING;
308 key->flags |= 1 << KEY_FLAG_KEEP;
311 key->magic = KEY_DEBUG_MAGIC;
314 /* let the security module know about the key */
315 ret = security_key_alloc(key, cred, flags);
319 /* publish the key by giving it a serial number */
320 refcount_inc(&key->domain_tag->usage);
322 key_alloc_serial(key);
325 return key;
328 kfree(key->description);
329 kmem_cache_free(key_jar, key);
337 key = ERR_PTR(ret);
341 kmem_cache_free(key_jar, key);
351 key = ERR_PTR(-ENOMEM);
357 key = ERR_PTR(-EDQUOT);
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)
375 int delta = (int)datalen - key->datalen;
378 key_check(key);
381 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
382 unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ?
385 spin_lock(&key->user->lock);
388 (key->user->qnbytes + delta > maxbytes ||
389 key->user->qnbytes + delta < key->user->qnbytes)) {
393 key->user->qnbytes += delta;
394 key->quotalen += delta;
396 spin_unlock(&key->user->lock);
401 key->datalen = datalen;
408 * Change the key state to being instantiated.
410 static void mark_key_instantiated(struct key *key, int reject_error)
415 smp_store_release(&key->state,
420 * Instantiate a key and link it into the target keyring atomically. Must be
421 * called with the target keyring's semaphore writelocked. The target key's
425 static int __key_instantiate_and_link(struct key *key,
427 struct key *keyring,
428 struct key *authkey,
433 key_check(key);
442 if (key->state == KEY_IS_UNINSTANTIATED) {
443 /* instantiate the key */
444 ret = key->type->instantiate(key, prep);
447 /* mark the key as being instantiated */
448 atomic_inc(&key->user->nikeys);
449 mark_key_instantiated(key, 0);
450 notify_key(key, NOTIFY_KEY_INSTANTIATED, 0);
452 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
458 set_bit(KEY_FLAG_KEEP, &key->flags);
460 __key_link(keyring, key, _edit);
463 /* disable the authorisation key */
468 key_set_expiry(key, prep->expiry);
474 /* wake up anyone waiting for a key to be constructed */
476 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
482 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
483 * @key: The key to instantiate.
489 * Instantiate a key that's in the uninstantiated state using the provided data
494 * waiting for the key is woken up. If the key was already instantiated,
497 int key_instantiate_and_link(struct key *key,
500 struct key *keyring,
501 struct key *authkey)
510 prep.quotalen = key->type->def_datalen;
512 if (key->type->preparse) {
513 ret = key->type->preparse(&prep);
519 ret = __key_link_lock(keyring, &key->index_key);
523 ret = __key_link_begin(keyring, &key->index_key, &edit);
530 ret = keyres->check(keyring, key->type, &prep.payload,
531 keyres->key);
537 ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
541 __key_link_end(keyring, &key->index_key, edit);
544 if (key->type->preparse)
545 key->type->free_preparse(&prep);
552 * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
553 * @key: The key to instantiate.
554 * @timeout: The timeout on the negative key.
555 * @error: The error to return when the key is hit.
559 * Negatively instantiate a key that's in the uninstantiated state and, if
561 * destination keyring if one is supplied. The key and any links to the key
566 * key expires.
569 * waiting for the key is woken up. If the key was already instantiated,
572 int key_reject_and_link(struct key *key,
575 struct key *keyring,
576 struct key *authkey)
581 key_check(key);
591 link_ret = __key_link_lock(keyring, &key->index_key);
593 link_ret = __key_link_begin(keyring, &key->index_key, &edit);
595 __key_link_end(keyring, &key->index_key, edit);
602 if (key->state == KEY_IS_UNINSTANTIATED) {
603 /* mark the key as being negatively instantiated */
604 atomic_inc(&key->user->nikeys);
605 mark_key_instantiated(key, -error);
606 notify_key(key, NOTIFY_KEY_INSTANTIATED, -error);
607 key_set_expiry(key, ktime_get_real_seconds() + timeout);
609 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
616 __key_link(keyring, key, &edit);
618 /* disable the authorisation key */
626 __key_link_end(keyring, &key->index_key, edit);
628 /* wake up anyone waiting for a key to be constructed */
630 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
637 * key_put - Discard a reference to a key.
638 * @key: The key to discard a reference from.
640 * Discard a reference to a key, and when all the references are gone, we
644 void key_put(struct key *key)
646 if (key) {
647 key_check(key);
649 if (refcount_dec_and_test(&key->usage))
656 * Find a key by its serial number.
658 struct key *key_lookup(key_serial_t id)
661 struct key *key;
665 /* search the tree for the specified key */
668 key = rb_entry(n, struct key, serial_node);
670 if (id < key->serial)
672 else if (id > key->serial)
679 key = ERR_PTR(-ENOKEY);
683 /* A key is allowed to be looked up only if someone still owns a
686 if (!refcount_inc_not_zero(&key->usage))
691 return key;
695 * Find and lock the specified key type against removal.
706 /* look up the key type to see if it's one of the registered kernel
720 void key_set_timeout(struct key *key, unsigned timeout)
725 down_write(&key->sem);
729 key_set_expiry(key, expiry);
731 up_write(&key->sem);
736 * Unlock a key type locked by key_type_lookup().
744 * Attempt to update an existing key.
746 * The key is given to us with an incremented refcount that we need to discard
752 struct key *key = key_ref_to_ptr(key_ref);
755 /* need write permission on the key to update it */
761 if (!key->type->update)
764 down_write(&key->sem);
766 ret = key->type->update(key, prep);
768 /* Updating a negative key positively instantiates it */
769 mark_key_instantiated(key, 0);
770 notify_key(key, NOTIFY_KEY_UPDATED, 0);
773 up_write(&key->sem);
781 key_put(key);
787 * key_create_or_update - Update or create and instantiate a key.
789 * @type: The type of key.
790 * @description: The searchable description for the key.
791 * @payload: The data to use to instantiate or update the key.
793 * @perm: The permissions mask for a new key.
794 * @flags: The quota flags for a new key.
796 * Search the destination keyring for a key of the same description and if one
800 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
803 * Returns a pointer to the new key if successful, -ENODEV if the key type
806 * creation of the key.
809 * the key ref before it is returned.
825 struct key *keyring, *key = NULL;
830 /* look up the key type to see if it's one of the registered kernel
888 &prep.payload, restrict_link->key);
895 /* if we're going to allocate a new key, we're going to have
903 /* if it's possible to update this type of key, search for an existing
904 * key of the same type and description in the destination keyring and
926 /* allocate a new key */
927 key = key_alloc(index_key.type, index_key.description,
929 if (IS_ERR(key)) {
930 key_ref = ERR_CAST(key);
935 ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit);
937 key_put(key);
942 ima_post_key_create_or_update(keyring, key, payload, plen,
945 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
958 /* we found a matching key, so we're going to try to update it
959 * - we can drop the locks first as we have the key pinned
963 key = key_ref_to_ptr(key_ref);
964 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) {
965 ret = wait_for_key_construction(key, true);
976 ima_post_key_create_or_update(keyring, key,
985 * key_update - Update a key's contents.
986 * @key_ref: The pointer (plus possession flag) to the key.
987 * @payload: The data to be used to update the key.
990 * Attempt to update the contents of a key with the given payload data. The
991 * caller must be granted Write permission on the key. Negative keys can be
994 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
995 * type does not support updating. The key type may return other errors.
1000 struct key *key = key_ref_to_ptr(key_ref);
1003 key_check(key);
1005 /* the key must be writable */
1011 if (!key->type->update)
1017 prep.quotalen = key->type->def_datalen;
1019 if (key->type->preparse) {
1020 ret = key->type->preparse(&prep);
1025 down_write(&key->sem);
1027 ret = key->type->update(key, &prep);
1029 /* Updating a negative key positively instantiates it */
1030 mark_key_instantiated(key, 0);
1031 notify_key(key, NOTIFY_KEY_UPDATED, 0);
1034 up_write(&key->sem);
1037 if (key->type->preparse)
1038 key->type->free_preparse(&prep);
1044 * key_revoke - Revoke a key.
1045 * @key: The key to be revoked.
1047 * Mark a key as being revoked and ask the type to free up its resources. The
1048 * revocation timeout is set and the key and all its links will be
1052 void key_revoke(struct key *key)
1056 key_check(key);
1058 /* make sure no one's trying to change or use the key when we mark it
1060 * authorisation key whilst holding the sem on a key we've just
1063 down_write_nested(&key->sem, 1);
1064 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) {
1065 notify_key(key, NOTIFY_KEY_REVOKED, 0);
1066 if (key->type->revoke)
1067 key->type->revoke(key);
1071 if (key->revoked_at == 0 || key->revoked_at > time) {
1072 key->revoked_at = time;
1073 key_schedule_gc(key->revoked_at + key_gc_delay);
1077 up_write(&key->sem);
1082 * key_invalidate - Invalidate a key.
1083 * @key: The key to be invalidated.
1085 * Mark a key as being invalidated and have it cleaned up immediately. The key
1088 void key_invalidate(struct key *key)
1090 kenter("%d", key_serial(key));
1092 key_check(key);
1094 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
1095 down_write_nested(&key->sem, 1);
1096 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
1097 notify_key(key, NOTIFY_KEY_INVALIDATED, 0);
1100 up_write(&key->sem);
1106 * generic_key_instantiate - Simple instantiation of a key from preparsed data
1107 * @key: The key to be instantiated
1110 * Instantiate a key from preparsed data. We assume we can just copy the data
1113 * This can be pointed to directly by the key type instantiate op pointer.
1115 int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
1121 ret = key_payload_reserve(key, prep->quotalen);
1123 rcu_assign_keypointer(key, prep->payload.data[0]);
1124 key->payload.data[1] = prep->payload.data[1];
1125 key->payload.data[2] = prep->payload.data[2];
1126 key->payload.data[3] = prep->payload.data[3];
1138 * register_key_type - Register a type of key.
1139 * @ktype: The new key type.
1141 * Register a new key type.
1155 /* disallow key types with the same name */
1174 * unregister_key_type - Unregister a type of key.
1175 * @ktype: The key type.
1177 * Unregister a key type and mark all the extant keys of this type as dead.
1193 * Initialise the key management state.
1198 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1201 /* add the special key types */