• Home
  • Raw
  • Download

Lines Matching full:key

1 /* Basic authentication token and access key management
30 unsigned int key_quota_root_maxkeys = 1000000; /* root's key count quota */
31 unsigned int key_quota_root_maxbytes = 25000000; /* root's key space quota */
32 unsigned int key_quota_maxkeys = 200; /* general key count quota */
33 unsigned int key_quota_maxbytes = 20000; /* general key space quota */
38 /* We serialise key instantiation and link */
42 void __key_check(const struct key *key) in __key_check() argument
44 printk("__key_check: key %p {%08x} should be {%08x}\n", in __key_check()
45 key, key->magic, KEY_DEBUG_MAGIC); in __key_check()
51 * Get the key quota record for a user, allocating a new record if one doesn't
134 * Allocate a serial number for a key. These are assigned randomly to avoid
137 static inline void key_alloc_serial(struct key *key) in key_alloc_serial() argument
140 struct key *xkey; in key_alloc_serial()
145 get_random_bytes(&key->serial, sizeof(key->serial)); in key_alloc_serial()
147 key->serial >>= 1; /* negative numbers are not permitted */ in key_alloc_serial()
148 } while (key->serial < 3); in key_alloc_serial()
158 xkey = rb_entry(parent, struct key, serial_node); in key_alloc_serial()
160 if (key->serial < xkey->serial) in key_alloc_serial()
162 else if (key->serial > xkey->serial) in key_alloc_serial()
168 /* we've found a suitable hole - arrange for this key to occupy it */ in key_alloc_serial()
169 rb_link_node(&key->serial_node, parent, p); in key_alloc_serial()
170 rb_insert_color(&key->serial_node, &key_serial_tree); in key_alloc_serial()
175 /* we found a key with the proposed serial number - walk the tree from in key_alloc_serial()
179 key->serial++; in key_alloc_serial()
180 if (key->serial < 3) { in key_alloc_serial()
181 key->serial = 3; in key_alloc_serial()
189 xkey = rb_entry(parent, struct key, serial_node); in key_alloc_serial()
190 if (key->serial < xkey->serial) in key_alloc_serial()
196 * key_alloc - Allocate a key of the specified type.
197 * @type: The type of key to allocate.
198 * @desc: The key description to allow the key to be searched out.
199 * @uid: The owner of the new key.
200 * @gid: The group ID for the new key's group permissions.
202 * @perm: The permissions mask of the new key.
206 * Allocate a key of the specified type with the attributes given. The key is
208 * key before returning.
213 * The user's key count quota is updated to reflect the creation of the key and
214 * the user's key data quota has the default for the key type reserved. The
218 * The LSM security modules can prevent a key being created, in which case
221 * Returns a pointer to the new key if successful and an error code otherwise.
223 * Note that the caller needs to ensure the key type isn't uninstantiated.
225 * be done by either never unregistering the key type, or making sure
228 struct key *key_alloc(struct key_type *type, const char *desc, in key_alloc()
234 struct key *key; in key_alloc() local
238 key = ERR_PTR(-EINVAL); in key_alloc()
245 key = ERR_PTR(ret); in key_alloc()
253 /* get hold of the key tracking for this user */ in key_alloc()
258 /* check that the user's quota permits allocation of another key and in key_alloc()
279 /* allocate and initialise the key and its description */ in key_alloc()
280 key = kmem_cache_zalloc(key_jar, GFP_KERNEL); in key_alloc()
281 if (!key) in key_alloc()
284 key->index_key.desc_len = desclen; in key_alloc()
285 key->index_key.description = kmemdup(desc, desclen + 1, GFP_KERNEL); in key_alloc()
286 if (!key->index_key.description) in key_alloc()
289 refcount_set(&key->usage, 1); in key_alloc()
290 init_rwsem(&key->sem); in key_alloc()
291 lockdep_set_class(&key->sem, &type->lock_class); in key_alloc()
292 key->index_key.type = type; in key_alloc()
293 key->user = user; in key_alloc()
294 key->quotalen = quotalen; in key_alloc()
295 key->datalen = type->def_datalen; in key_alloc()
296 key->uid = uid; in key_alloc()
297 key->gid = gid; in key_alloc()
298 key->perm = perm; in key_alloc()
299 key->restrict_link = restrict_link; in key_alloc()
300 key->last_used_at = ktime_get_real_seconds(); in key_alloc()
303 key->flags |= 1 << KEY_FLAG_IN_QUOTA; in key_alloc()
305 key->flags |= 1 << KEY_FLAG_BUILTIN; in key_alloc()
307 key->flags |= 1 << KEY_FLAG_UID_KEYRING; in key_alloc()
310 key->magic = KEY_DEBUG_MAGIC; in key_alloc()
313 /* let the security module know about the key */ in key_alloc()
314 ret = security_key_alloc(key, cred, flags); in key_alloc()
318 /* publish the key by giving it a serial number */ in key_alloc()
320 key_alloc_serial(key); in key_alloc()
323 return key; in key_alloc()
326 kfree(key->description); in key_alloc()
327 kmem_cache_free(key_jar, key); in key_alloc()
335 key = ERR_PTR(ret); in key_alloc()
339 kmem_cache_free(key_jar, key); in key_alloc()
349 key = ERR_PTR(-ENOMEM); in key_alloc()
355 key = ERR_PTR(-EDQUOT); in key_alloc()
361 * key_payload_reserve - Adjust data quota reservation for the key's payload
362 * @key: The key to make the reservation for.
365 * Adjust the amount of the owning user's key data quota that a key reserves.
371 int key_payload_reserve(struct key *key, size_t datalen) in key_payload_reserve() argument
373 int delta = (int)datalen - key->datalen; in key_payload_reserve()
376 key_check(key); in key_payload_reserve()
379 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { in key_payload_reserve()
380 unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ? in key_payload_reserve()
383 spin_lock(&key->user->lock); in key_payload_reserve()
386 (key->user->qnbytes + delta > maxbytes || in key_payload_reserve()
387 key->user->qnbytes + delta < key->user->qnbytes)) { in key_payload_reserve()
391 key->user->qnbytes += delta; in key_payload_reserve()
392 key->quotalen += delta; in key_payload_reserve()
394 spin_unlock(&key->user->lock); in key_payload_reserve()
399 key->datalen = datalen; in key_payload_reserve()
406 * Change the key state to being instantiated.
408 static void mark_key_instantiated(struct key *key, int reject_error) in mark_key_instantiated() argument
413 smp_store_release(&key->state, in mark_key_instantiated()
418 * Instantiate a key and link it into the target keyring atomically. Must be
419 * called with the target keyring's semaphore writelocked. The target key's
423 static int __key_instantiate_and_link(struct key *key, in __key_instantiate_and_link() argument
425 struct key *keyring, in __key_instantiate_and_link()
426 struct key *authkey, in __key_instantiate_and_link()
431 key_check(key); in __key_instantiate_and_link()
440 if (key->state == KEY_IS_UNINSTANTIATED) { in __key_instantiate_and_link()
441 /* instantiate the key */ in __key_instantiate_and_link()
442 ret = key->type->instantiate(key, prep); in __key_instantiate_and_link()
445 /* mark the key as being instantiated */ in __key_instantiate_and_link()
446 atomic_inc(&key->user->nikeys); in __key_instantiate_and_link()
447 mark_key_instantiated(key, 0); in __key_instantiate_and_link()
449 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in __key_instantiate_and_link()
455 set_bit(KEY_FLAG_KEEP, &key->flags); in __key_instantiate_and_link()
457 __key_link(key, _edit); in __key_instantiate_and_link()
460 /* disable the authorisation key */ in __key_instantiate_and_link()
465 key->expiry = prep->expiry; in __key_instantiate_and_link()
473 /* wake up anyone waiting for a key to be constructed */ in __key_instantiate_and_link()
475 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in __key_instantiate_and_link()
481 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
482 * @key: The key to instantiate.
488 * Instantiate a key that's in the uninstantiated state using the provided data
493 * waiting for the key is woken up. If the key was already instantiated,
496 int key_instantiate_and_link(struct key *key, in key_instantiate_and_link() argument
499 struct key *keyring, in key_instantiate_and_link()
500 struct key *authkey) in key_instantiate_and_link()
509 prep.quotalen = key->type->def_datalen; in key_instantiate_and_link()
511 if (key->type->preparse) { in key_instantiate_and_link()
512 ret = key->type->preparse(&prep); in key_instantiate_and_link()
518 ret = __key_link_begin(keyring, &key->index_key, &edit); in key_instantiate_and_link()
525 ret = keyres->check(keyring, key->type, &prep.payload, in key_instantiate_and_link()
526 keyres->key); in key_instantiate_and_link()
532 ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit); in key_instantiate_and_link()
536 __key_link_end(keyring, &key->index_key, edit); in key_instantiate_and_link()
539 if (key->type->preparse) in key_instantiate_and_link()
540 key->type->free_preparse(&prep); in key_instantiate_and_link()
547 * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
548 * @key: The key to instantiate.
549 * @timeout: The timeout on the negative key.
550 * @error: The error to return when the key is hit.
554 * Negatively instantiate a key that's in the uninstantiated state and, if
556 * destination keyring if one is supplied. The key and any links to the key
561 * key expires.
564 * waiting for the key is woken up. If the key was already instantiated,
567 int key_reject_and_link(struct key *key, in key_reject_and_link() argument
570 struct key *keyring, in key_reject_and_link()
571 struct key *authkey) in key_reject_and_link()
576 key_check(key); in key_reject_and_link()
586 link_ret = __key_link_begin(keyring, &key->index_key, &edit); in key_reject_and_link()
592 if (key->state == KEY_IS_UNINSTANTIATED) { in key_reject_and_link()
593 /* mark the key as being negatively instantiated */ in key_reject_and_link()
594 atomic_inc(&key->user->nikeys); in key_reject_and_link()
595 mark_key_instantiated(key, -error); in key_reject_and_link()
596 key->expiry = ktime_get_real_seconds() + timeout; in key_reject_and_link()
597 key_schedule_gc(key->expiry + key_gc_delay); in key_reject_and_link()
599 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in key_reject_and_link()
606 __key_link(key, &edit); in key_reject_and_link()
608 /* disable the authorisation key */ in key_reject_and_link()
616 __key_link_end(keyring, &key->index_key, edit); in key_reject_and_link()
618 /* wake up anyone waiting for a key to be constructed */ in key_reject_and_link()
620 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in key_reject_and_link()
627 * key_put - Discard a reference to a key.
628 * @key: The key to discard a reference from.
630 * Discard a reference to a key, and when all the references are gone, we
634 void key_put(struct key *key) in key_put() argument
636 if (key) { in key_put()
637 key_check(key); in key_put()
639 if (refcount_dec_and_test(&key->usage)) in key_put()
646 * Find a key by its serial number.
648 struct key *key_lookup(key_serial_t id) in key_lookup()
651 struct key *key; in key_lookup() local
655 /* search the tree for the specified key */ in key_lookup()
658 key = rb_entry(n, struct key, serial_node); in key_lookup()
660 if (id < key->serial) in key_lookup()
662 else if (id > key->serial) in key_lookup()
669 key = ERR_PTR(-ENOKEY); in key_lookup()
673 /* A key is allowed to be looked up only if someone still owns a in key_lookup()
676 if (!refcount_inc_not_zero(&key->usage)) in key_lookup()
681 return key; in key_lookup()
685 * Find and lock the specified key type against removal.
696 /* look up the key type to see if it's one of the registered kernel in key_type_lookup()
710 void key_set_timeout(struct key *key, unsigned timeout) in key_set_timeout() argument
715 down_write(&key->sem); in key_set_timeout()
720 key->expiry = expiry; in key_set_timeout()
721 key_schedule_gc(key->expiry + key_gc_delay); in key_set_timeout()
723 up_write(&key->sem); in key_set_timeout()
728 * Unlock a key type locked by key_type_lookup().
736 * Attempt to update an existing key.
738 * The key is given to us with an incremented refcount that we need to discard
744 struct key *key = key_ref_to_ptr(key_ref); in __key_update() local
747 /* need write permission on the key to update it */ in __key_update()
753 if (!key->type->update) in __key_update()
756 down_write(&key->sem); in __key_update()
758 ret = key->type->update(key, prep); in __key_update()
760 /* Updating a negative key positively instantiates it */ in __key_update()
761 mark_key_instantiated(key, 0); in __key_update()
763 up_write(&key->sem); in __key_update()
771 key_put(key); in __key_update()
777 * key_create_or_update - Update or create and instantiate a key.
779 * @type: The type of key.
780 * @description: The searchable description for the key.
781 * @payload: The data to use to instantiate or update the key.
783 * @perm: The permissions mask for a new key.
784 * @flags: The quota flags for a new key.
786 * Search the destination keyring for a key of the same description and if one
790 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
793 * Returns a pointer to the new key if successful, -ENODEV if the key type
796 * creation of the key.
799 * the key ref before it is returned.
815 struct key *keyring, *key = NULL; in key_create_or_update() local
820 /* look up the key type to see if it's one of the registered kernel in key_create_or_update()
871 &prep.payload, restrict_link->key); in key_create_or_update()
878 /* if we're going to allocate a new key, we're going to have in key_create_or_update()
886 /* if it's possible to update this type of key, search for an existing in key_create_or_update()
887 * key of the same type and description in the destination keyring and in key_create_or_update()
909 /* allocate a new key */ in key_create_or_update()
910 key = key_alloc(index_key.type, index_key.description, in key_create_or_update()
912 if (IS_ERR(key)) { in key_create_or_update()
913 key_ref = ERR_CAST(key); in key_create_or_update()
918 ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit); in key_create_or_update()
920 key_put(key); in key_create_or_update()
925 key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); in key_create_or_update()
938 /* we found a matching key, so we're going to try to update it in key_create_or_update()
939 * - we can drop the locks first as we have the key pinned in key_create_or_update()
943 key = key_ref_to_ptr(key_ref); in key_create_or_update()
944 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) { in key_create_or_update()
945 ret = wait_for_key_construction(key, true); in key_create_or_update()
959 * key_update - Update a key's contents.
960 * @key_ref: The pointer (plus possession flag) to the key.
961 * @payload: The data to be used to update the key.
964 * Attempt to update the contents of a key with the given payload data. The
965 * caller must be granted Write permission on the key. Negative keys can be
968 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
969 * type does not support updating. The key type may return other errors.
974 struct key *key = key_ref_to_ptr(key_ref); in key_update() local
977 key_check(key); in key_update()
979 /* the key must be writable */ in key_update()
985 if (!key->type->update) in key_update()
991 prep.quotalen = key->type->def_datalen; in key_update()
993 if (key->type->preparse) { in key_update()
994 ret = key->type->preparse(&prep); in key_update()
999 down_write(&key->sem); in key_update()
1001 ret = key->type->update(key, &prep); in key_update()
1003 /* Updating a negative key positively instantiates it */ in key_update()
1004 mark_key_instantiated(key, 0); in key_update()
1006 up_write(&key->sem); in key_update()
1009 if (key->type->preparse) in key_update()
1010 key->type->free_preparse(&prep); in key_update()
1016 * key_revoke - Revoke a key.
1017 * @key: The key to be revoked.
1019 * Mark a key as being revoked and ask the type to free up its resources. The
1020 * revocation timeout is set and the key and all its links will be
1024 void key_revoke(struct key *key) in key_revoke() argument
1028 key_check(key); in key_revoke()
1030 /* make sure no one's trying to change or use the key when we mark it in key_revoke()
1032 * authorisation key whilst holding the sem on a key we've just in key_revoke()
1035 down_write_nested(&key->sem, 1); in key_revoke()
1036 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) && in key_revoke()
1037 key->type->revoke) in key_revoke()
1038 key->type->revoke(key); in key_revoke()
1042 if (key->revoked_at == 0 || key->revoked_at > time) { in key_revoke()
1043 key->revoked_at = time; in key_revoke()
1044 key_schedule_gc(key->revoked_at + key_gc_delay); in key_revoke()
1047 up_write(&key->sem); in key_revoke()
1052 * key_invalidate - Invalidate a key.
1053 * @key: The key to be invalidated.
1055 * Mark a key as being invalidated and have it cleaned up immediately. The key
1058 void key_invalidate(struct key *key) in key_invalidate() argument
1060 kenter("%d", key_serial(key)); in key_invalidate()
1062 key_check(key); in key_invalidate()
1064 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1065 down_write_nested(&key->sem, 1); in key_invalidate()
1066 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) in key_invalidate()
1068 up_write(&key->sem); in key_invalidate()
1074 * generic_key_instantiate - Simple instantiation of a key from preparsed data
1075 * @key: The key to be instantiated
1078 * Instantiate a key from preparsed data. We assume we can just copy the data
1081 * This can be pointed to directly by the key type instantiate op pointer.
1083 int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep) in generic_key_instantiate() argument
1089 ret = key_payload_reserve(key, prep->quotalen); in generic_key_instantiate()
1091 rcu_assign_keypointer(key, prep->payload.data[0]); in generic_key_instantiate()
1092 key->payload.data[1] = prep->payload.data[1]; in generic_key_instantiate()
1093 key->payload.data[2] = prep->payload.data[2]; in generic_key_instantiate()
1094 key->payload.data[3] = prep->payload.data[3]; in generic_key_instantiate()
1106 * register_key_type - Register a type of key.
1107 * @ktype: The new key type.
1109 * Register a new key type.
1123 /* disallow key types with the same name */ in register_key_type()
1132 pr_notice("Key type %s registered\n", ktype->name); in register_key_type()
1142 * unregister_key_type - Unregister a type of key.
1143 * @ktype: The key type.
1145 * Unregister a key type and mark all the extant keys of this type as dead.
1155 pr_notice("Key type %s unregistered\n", ktype->name); in unregister_key_type()
1161 * Initialise the key management state.
1166 key_jar = kmem_cache_create("key_jar", sizeof(struct key), in key_init()
1169 /* add the special key types */ in key_init()