Lines Matching refs:key

39 void __key_check(const struct key *key)  in __key_check()  argument
42 key, key->magic, KEY_DEBUG_MAGIC); in __key_check()
134 static inline void key_alloc_serial(struct key *key) in key_alloc_serial() argument
137 struct key *xkey; in key_alloc_serial()
142 get_random_bytes(&key->serial, sizeof(key->serial)); in key_alloc_serial()
144 key->serial >>= 1; /* negative numbers are not permitted */ in key_alloc_serial()
145 } while (key->serial < 3); in key_alloc_serial()
155 xkey = rb_entry(parent, struct key, serial_node); in key_alloc_serial()
157 if (key->serial < xkey->serial) in key_alloc_serial()
159 else if (key->serial > xkey->serial) in key_alloc_serial()
166 rb_link_node(&key->serial_node, parent, p); in key_alloc_serial()
167 rb_insert_color(&key->serial_node, &key_serial_tree); in key_alloc_serial()
176 key->serial++; in key_alloc_serial()
177 if (key->serial < 3) { in key_alloc_serial()
178 key->serial = 3; in key_alloc_serial()
186 xkey = rb_entry(parent, struct key, serial_node); in key_alloc_serial()
187 if (key->serial < xkey->serial) in key_alloc_serial()
225 struct key *key_alloc(struct key_type *type, const char *desc, in key_alloc()
231 struct key *key; in key_alloc() local
235 key = ERR_PTR(-EINVAL); in key_alloc()
242 key = ERR_PTR(ret); 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->restrict_link = restrict_link; in key_alloc()
298 key->last_used_at = ktime_get_real_seconds(); in key_alloc()
301 key->flags |= 1 << KEY_FLAG_IN_QUOTA; in key_alloc()
303 key->flags |= 1 << KEY_FLAG_BUILTIN; in key_alloc()
305 key->flags |= 1 << KEY_FLAG_UID_KEYRING; in key_alloc()
307 key->flags |= 1 << KEY_FLAG_KEEP; in key_alloc()
310 key->magic = KEY_DEBUG_MAGIC; in key_alloc()
314 ret = security_key_alloc(key, cred, flags); in key_alloc()
319 refcount_inc(&key->domain_tag->usage); in key_alloc()
321 key_alloc_serial(key); in key_alloc()
324 return key; in key_alloc()
327 kfree(key->description); in key_alloc()
328 kmem_cache_free(key_jar, key); in key_alloc()
336 key = ERR_PTR(ret); in key_alloc()
340 kmem_cache_free(key_jar, key); in key_alloc()
350 key = ERR_PTR(-ENOMEM); in key_alloc()
356 key = ERR_PTR(-EDQUOT); in key_alloc()
372 int key_payload_reserve(struct key *key, size_t datalen) in key_payload_reserve() argument
374 int delta = (int)datalen - key->datalen; in key_payload_reserve()
377 key_check(key); in key_payload_reserve()
380 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { in key_payload_reserve()
381 unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ? in key_payload_reserve()
384 spin_lock(&key->user->lock); in key_payload_reserve()
387 (key->user->qnbytes + delta > maxbytes || in key_payload_reserve()
388 key->user->qnbytes + delta < key->user->qnbytes)) { in key_payload_reserve()
392 key->user->qnbytes += delta; in key_payload_reserve()
393 key->quotalen += delta; in key_payload_reserve()
395 spin_unlock(&key->user->lock); in key_payload_reserve()
400 key->datalen = datalen; in key_payload_reserve()
409 static void mark_key_instantiated(struct key *key, int reject_error) in mark_key_instantiated() argument
414 smp_store_release(&key->state, in mark_key_instantiated()
424 static int __key_instantiate_and_link(struct key *key, in __key_instantiate_and_link() argument
426 struct key *keyring, in __key_instantiate_and_link()
427 struct key *authkey, in __key_instantiate_and_link()
432 key_check(key); in __key_instantiate_and_link()
441 if (key->state == KEY_IS_UNINSTANTIATED) { in __key_instantiate_and_link()
443 ret = key->type->instantiate(key, prep); in __key_instantiate_and_link()
447 atomic_inc(&key->user->nikeys); in __key_instantiate_and_link()
448 mark_key_instantiated(key, 0); in __key_instantiate_and_link()
449 notify_key(key, NOTIFY_KEY_INSTANTIATED, 0); in __key_instantiate_and_link()
451 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in __key_instantiate_and_link()
457 set_bit(KEY_FLAG_KEEP, &key->flags); in __key_instantiate_and_link()
459 __key_link(keyring, key, _edit); in __key_instantiate_and_link()
467 key->expiry = prep->expiry; in __key_instantiate_and_link()
477 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in __key_instantiate_and_link()
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()
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()
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->expiry = ktime_get_real_seconds() + timeout; in key_reject_and_link()
610 key_schedule_gc(key->expiry + key_gc_delay); in key_reject_and_link()
612 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in key_reject_and_link()
619 __key_link(keyring, key, &edit); in key_reject_and_link()
629 __key_link_end(keyring, &key->index_key, edit); in key_reject_and_link()
633 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in key_reject_and_link()
647 void key_put(struct key *key) in key_put() argument
649 if (key) { in key_put()
650 key_check(key); in key_put()
652 if (refcount_dec_and_test(&key->usage)) in key_put()
661 struct key *key_lookup(key_serial_t id) in key_lookup()
664 struct key *key; in key_lookup() local
671 key = rb_entry(n, struct key, serial_node); in key_lookup()
673 if (id < key->serial) in key_lookup()
675 else if (id > key->serial) in key_lookup()
682 key = ERR_PTR(-ENOKEY); in key_lookup()
689 if (!refcount_inc_not_zero(&key->usage)) in key_lookup()
694 return key; in key_lookup()
723 void key_set_timeout(struct key *key, unsigned timeout) in key_set_timeout() argument
728 down_write(&key->sem); in key_set_timeout()
733 key->expiry = expiry; in key_set_timeout()
734 key_schedule_gc(key->expiry + key_gc_delay); in key_set_timeout()
736 up_write(&key->sem); in key_set_timeout()
757 struct key *key = key_ref_to_ptr(key_ref); in __key_update() local
766 if (!key->type->update) in __key_update()
769 down_write(&key->sem); in __key_update()
771 ret = key->type->update(key, prep); in __key_update()
774 mark_key_instantiated(key, 0); in __key_update()
775 notify_key(key, NOTIFY_KEY_UPDATED, 0); in __key_update()
778 up_write(&key->sem); in __key_update()
786 key_put(key); in __key_update()
830 struct key *keyring, *key = NULL; in key_create_or_update() local
894 &prep.payload, restrict_link->key); in key_create_or_update()
933 key = key_alloc(index_key.type, index_key.description, in key_create_or_update()
935 if (IS_ERR(key)) { in key_create_or_update()
936 key_ref = ERR_CAST(key); in key_create_or_update()
941 ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit); in key_create_or_update()
943 key_put(key); in key_create_or_update()
948 ima_post_key_create_or_update(keyring, key, payload, plen, in key_create_or_update()
951 key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); in key_create_or_update()
969 key = key_ref_to_ptr(key_ref); in key_create_or_update()
970 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) { in key_create_or_update()
971 ret = wait_for_key_construction(key, true); in key_create_or_update()
982 ima_post_key_create_or_update(keyring, key, in key_create_or_update()
1006 struct key *key = key_ref_to_ptr(key_ref); in key_update() local
1009 key_check(key); in key_update()
1017 if (!key->type->update) in key_update()
1023 prep.quotalen = key->type->def_datalen; in key_update()
1025 if (key->type->preparse) { in key_update()
1026 ret = key->type->preparse(&prep); in key_update()
1031 down_write(&key->sem); in key_update()
1033 ret = key->type->update(key, &prep); in key_update()
1036 mark_key_instantiated(key, 0); in key_update()
1037 notify_key(key, NOTIFY_KEY_UPDATED, 0); in key_update()
1040 up_write(&key->sem); in key_update()
1043 if (key->type->preparse) in key_update()
1044 key->type->free_preparse(&prep); in key_update()
1058 void key_revoke(struct key *key) in key_revoke() argument
1062 key_check(key); in key_revoke()
1069 down_write_nested(&key->sem, 1); in key_revoke()
1070 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) { in key_revoke()
1071 notify_key(key, NOTIFY_KEY_REVOKED, 0); in key_revoke()
1072 if (key->type->revoke) in key_revoke()
1073 key->type->revoke(key); in key_revoke()
1077 if (key->revoked_at == 0 || key->revoked_at > time) { in key_revoke()
1078 key->revoked_at = time; in key_revoke()
1079 key_schedule_gc(key->revoked_at + key_gc_delay); in key_revoke()
1083 up_write(&key->sem); in key_revoke()
1094 void key_invalidate(struct key *key) in key_invalidate() argument
1096 kenter("%d", key_serial(key)); in key_invalidate()
1098 key_check(key); in key_invalidate()
1100 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1101 down_write_nested(&key->sem, 1); in key_invalidate()
1102 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1103 notify_key(key, NOTIFY_KEY_INVALIDATED, 0); in key_invalidate()
1106 up_write(&key->sem); in key_invalidate()
1121 int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep) in generic_key_instantiate() argument
1127 ret = key_payload_reserve(key, prep->quotalen); in generic_key_instantiate()
1129 rcu_assign_keypointer(key, prep->payload.data[0]); in generic_key_instantiate()
1130 key->payload.data[1] = prep->payload.data[1]; in generic_key_instantiate()
1131 key->payload.data[2] = prep->payload.data[2]; in generic_key_instantiate()
1132 key->payload.data[3] = prep->payload.data[3]; in generic_key_instantiate()
1204 key_jar = kmem_cache_create("key_jar", sizeof(struct key), in key_init()