Lines Matching +full:serial +full:- +full:state

1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
20 struct rb_root key_serial_tree; /* tree of keys indexed by serial */
41 key, key->magic, KEY_DEBUG_MAGIC); in __key_check()
65 if (uid_lt(uid, user->uid)) in key_user_lookup()
66 p = &(*p)->rb_left; in key_user_lookup()
67 else if (uid_gt(uid, user->uid)) in key_user_lookup()
68 p = &(*p)->rb_right; in key_user_lookup()
91 * second pass - so we use the candidate record */ in key_user_lookup()
92 refcount_set(&candidate->usage, 1); in key_user_lookup()
93 atomic_set(&candidate->nkeys, 0); in key_user_lookup()
94 atomic_set(&candidate->nikeys, 0); in key_user_lookup()
95 candidate->uid = uid; in key_user_lookup()
96 candidate->qnkeys = 0; in key_user_lookup()
97 candidate->qnbytes = 0; in key_user_lookup()
98 spin_lock_init(&candidate->lock); in key_user_lookup()
99 mutex_init(&candidate->cons_lock); in key_user_lookup()
101 rb_link_node(&candidate->node, parent, p); in key_user_lookup()
102 rb_insert_color(&candidate->node, &key_user_tree); in key_user_lookup()
107 /* okay - we found a user record for this UID */ in key_user_lookup()
109 refcount_inc(&user->usage); in key_user_lookup()
121 if (refcount_dec_and_lock(&user->usage, &key_user_lock)) { in key_user_put()
122 rb_erase(&user->node, &key_user_tree); in key_user_put()
130 * Allocate a serial number for a key. These are assigned randomly to avoid
138 /* propose a random serial number and look for a hole for it in the in key_alloc_serial()
139 * serial number tree */ 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()
156 if (key->serial < xkey->serial) in key_alloc_serial()
157 p = &(*p)->rb_left; in key_alloc_serial()
158 else if (key->serial > xkey->serial) in key_alloc_serial()
159 p = &(*p)->rb_right; 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()
172 * that point looking for the next unused serial number */ 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()
186 if (key->serial < xkey->serial) in key_alloc_serial()
192 * key_alloc - Allocate a key of the specified type.
203 * returned in an uninstantiated state and the caller needs to instantiate the
212 * quota is available, -EDQUOT will be returned.
215 * -EACCES will be returned.
235 key = ERR_PTR(-EINVAL); in key_alloc()
239 if (type->vet_description) { in key_alloc()
240 ret = type->vet_description(desc); in key_alloc()
248 quotalen = desclen + 1 + type->def_datalen; in key_alloc()
263 spin_lock_irqsave(&user->lock, irqflags); in key_alloc()
265 if (user->qnkeys + 1 > maxkeys || in key_alloc()
266 user->qnbytes + quotalen > maxbytes || in key_alloc()
267 user->qnbytes + quotalen < user->qnbytes) in key_alloc()
271 user->qnkeys++; in key_alloc()
272 user->qnbytes += quotalen; in key_alloc()
273 spin_unlock_irqrestore(&user->lock, irqflags); 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()
319 /* publish the key by giving it a serial number */ in key_alloc()
320 refcount_inc(&key->domain_tag->usage); in key_alloc()
321 atomic_inc(&user->nkeys); in key_alloc()
328 kfree(key->description); in key_alloc()
331 spin_lock_irqsave(&user->lock, irqflags); in key_alloc()
332 user->qnkeys--; in key_alloc()
333 user->qnbytes -= quotalen; in key_alloc()
334 spin_unlock_irqrestore(&user->lock, irqflags); in key_alloc()
344 spin_lock_irqsave(&user->lock, irqflags); in key_alloc()
345 user->qnkeys--; in key_alloc()
346 user->qnbytes -= quotalen; in key_alloc()
347 spin_unlock_irqrestore(&user->lock, irqflags); in key_alloc()
351 key = ERR_PTR(-ENOMEM); in key_alloc()
355 spin_unlock_irqrestore(&user->lock, irqflags); in key_alloc()
357 key = ERR_PTR(-EDQUOT); in key_alloc()
363 * key_payload_reserve - Adjust data quota reservation for the key's payload
368 * If the amount is increased, then -EDQUOT may be returned if there isn't
375 int delta = (int)datalen - key->datalen; 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()
391 ret = -EDQUOT; 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.
413 /* Commit the payload before setting the state; barrier versus in mark_key_instantiated()
416 smp_store_release(&key->state, in mark_key_instantiated()
438 ret = -EBUSY; in __key_instantiate_and_link()
443 if (key->state == KEY_IS_UNINSTANTIATED) { in __key_instantiate_and_link()
445 ret = key->type->instantiate(key, prep); in __key_instantiate_and_link()
449 atomic_inc(&key->user->nikeys); in __key_instantiate_and_link()
453 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in __key_instantiate_and_link()
458 if (test_bit(KEY_FLAG_KEEP, &keyring->flags)) in __key_instantiate_and_link()
459 set_bit(KEY_FLAG_KEEP, &key->flags); in __key_instantiate_and_link()
468 if (prep->expiry != TIME64_MAX) in __key_instantiate_and_link()
469 key_set_expiry(key, prep->expiry); 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.
490 * Instantiate a key that's in the uninstantiated state using the provided data
496 * -EBUSY will be returned.
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()
529 if (keyring->restrict_link && keyring->restrict_link->check) { in key_instantiate_and_link()
530 struct key_restriction *keyres = keyring->restrict_link; 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()
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.
561 * Negatively instantiate a key that's in the uninstantiated state and, if
572 * -EBUSY will be returned.
587 ret = -EBUSY; in key_reject_and_link()
590 if (keyring->restrict_link) in key_reject_and_link()
591 return -EPERM; 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()
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()
611 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in key_reject_and_link()
628 __key_link_end(keyring, &key->index_key, edit); 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.
651 if (refcount_dec_and_test(&key->usage)) { 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()
668 * Find a key by its serial number.
682 if (id < key->serial) in key_lookup()
683 n = n->rb_left; in key_lookup()
684 else if (id > key->serial) in key_lookup()
685 n = n->rb_right; in key_lookup()
691 key = ERR_PTR(-ENOKEY); in key_lookup()
696 * reference to it - otherwise it's awaiting the gc. in key_lookup()
698 if (!refcount_inc_not_zero(&key->usage)) in key_lookup()
710 * We return with the sem read-locked if successful. If the type wasn't
711 * available -ENOKEY is returned instead.
722 if (strcmp(ktype->name, type) == 0) in key_type_lookup()
727 ktype = ERR_PTR(-ENOKEY); in key_type_lookup()
738 down_write(&key->sem); in key_set_timeout()
744 up_write(&key->sem); in key_set_timeout()
773 ret = -EEXIST; in __key_update()
774 if (!key->type->update) in __key_update()
777 down_write(&key->sem); in __key_update()
779 ret = key->type->update(key, prep); in __key_update()
786 up_write(&key->sem); in __key_update()
827 key_ref = ERR_PTR(-ENODEV); in __key_create_or_update()
831 key_ref = ERR_PTR(-EINVAL); in __key_create_or_update()
832 if (!index_key.type->instantiate || in __key_create_or_update()
833 (!index_key.description && !index_key.type->preparse)) in __key_create_or_update()
841 restrict_link = keyring->restrict_link; in __key_create_or_update()
843 key_ref = ERR_PTR(-ENOTDIR); in __key_create_or_update()
844 if (keyring->type != &key_type_keyring) in __key_create_or_update()
851 prep.quotalen = index_key.type->def_datalen; in __key_create_or_update()
853 if (index_key.type->preparse) { in __key_create_or_update()
854 ret = index_key.type->preparse(&prep); in __key_create_or_update()
861 key_ref = ERR_PTR(-EINVAL); in __key_create_or_update()
880 if (restrict_link && restrict_link->check) { in __key_create_or_update()
881 ret = restrict_link->check(keyring, index_key.type, in __key_create_or_update()
882 &prep.payload, restrict_link->key); in __key_create_or_update()
902 if (index_key.type->update) { in __key_create_or_update()
911 key_ref = ERR_PTR(-EEXIST); in __key_create_or_update()
921 if (index_key.type->read) in __key_create_or_update()
925 index_key.type->update) in __key_create_or_update()
931 cred->fsuid, cred->fsgid, cred, perm, flags, NULL); in __key_create_or_update()
953 if (index_key.type->preparse) in __key_create_or_update()
954 index_key.type->free_preparse(&prep); in __key_create_or_update()
962 * - we can drop the locks first as we have the key pinned in __key_create_or_update()
967 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) { in __key_create_or_update()
986 * key_create_or_update - Update or create and instantiate a key.
1002 * Returns a pointer to the new key if successful, -ENODEV if the key type
1003 * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
1024 * key_create - Create and instantiate a key.
1038 * Returns a pointer to the new key if successful, -EEXIST if a key with the
1039 * same description already exists, -ENODEV if the key type wasn't available,
1040 * -ENOTDIR if the keyring wasn't a keyring, -EACCES if the caller isn't
1061 * key_update - Update a key's contents.
1070 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
1087 if (!key->type->update) in key_update()
1088 return -EOPNOTSUPP; in key_update()
1093 prep.quotalen = key->type->def_datalen; in key_update()
1095 if (key->type->preparse) { in key_update()
1096 ret = key->type->preparse(&prep); in key_update()
1101 down_write(&key->sem); in key_update()
1103 ret = key->type->update(key, &prep); in key_update()
1110 up_write(&key->sem); in key_update()
1113 if (key->type->preparse) in key_update()
1114 key->type->free_preparse(&prep); in key_update()
1120 * key_revoke - Revoke a key.
1135 * - we tell lockdep that we might nest because we might be revoking an in key_revoke()
1139 down_write_nested(&key->sem, 1); in key_revoke()
1140 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) { in key_revoke()
1142 if (key->type->revoke) in key_revoke()
1143 key->type->revoke(key); in key_revoke()
1147 if (key->revoked_at == 0 || key->revoked_at > time) { in key_revoke()
1148 key->revoked_at = time; in key_revoke()
1149 key_schedule_gc(key->revoked_at + key_gc_delay); in key_revoke()
1153 up_write(&key->sem); in key_revoke()
1158 * key_invalidate - Invalidate a key.
1170 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1171 down_write_nested(&key->sem, 1); in key_invalidate()
1172 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1176 up_write(&key->sem); in key_invalidate()
1182 * generic_key_instantiate - Simple instantiation of a key from preparsed data
1197 ret = key_payload_reserve(key, prep->quotalen); in generic_key_instantiate()
1199 rcu_assign_keypointer(key, prep->payload.data[0]); in generic_key_instantiate()
1200 key->payload.data[1] = prep->payload.data[1]; in generic_key_instantiate()
1201 key->payload.data[2] = prep->payload.data[2]; in generic_key_instantiate()
1202 key->payload.data[3] = prep->payload.data[3]; in generic_key_instantiate()
1203 prep->payload.data[0] = NULL; in generic_key_instantiate()
1204 prep->payload.data[1] = NULL; in generic_key_instantiate()
1205 prep->payload.data[2] = NULL; in generic_key_instantiate()
1206 prep->payload.data[3] = NULL; in generic_key_instantiate()
1214 * register_key_type - Register a type of key.
1219 * Returns 0 on success or -EEXIST if a type of this name already exists.
1226 memset(&ktype->lock_class, 0, sizeof(ktype->lock_class)); in register_key_type()
1228 ret = -EEXIST; in register_key_type()
1233 if (strcmp(p->name, ktype->name) == 0) in register_key_type()
1238 list_add(&ktype->link, &key_types_list); in register_key_type()
1240 pr_notice("Key type %s registered\n", ktype->name); in register_key_type()
1250 * unregister_key_type - Unregister a type of key.
1260 list_del_init(&ktype->link); in unregister_key_type()
1263 pr_notice("Key type %s unregistered\n", ktype->name); in unregister_key_type()
1269 * Initialise the key management state.