Lines Matching refs:key

42 void __key_check(const struct key *key)  in __key_check()  argument
45 key, key->magic, KEY_DEBUG_MAGIC); in __key_check()
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()
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()
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()
224 struct key *key_alloc(struct key_type *type, const char *desc, in key_alloc()
229 struct key *key; in key_alloc() local
233 key = ERR_PTR(-EINVAL); in key_alloc()
240 key = ERR_PTR(ret); in key_alloc()
275 key = kmem_cache_zalloc(key_jar, GFP_KERNEL); in key_alloc()
276 if (!key) in key_alloc()
279 key->index_key.desc_len = desclen; in key_alloc()
280 key->index_key.description = kmemdup(desc, desclen + 1, GFP_KERNEL); in key_alloc()
281 if (!key->index_key.description) in key_alloc()
284 atomic_set(&key->usage, 1); in key_alloc()
285 init_rwsem(&key->sem); in key_alloc()
286 lockdep_set_class(&key->sem, &type->lock_class); in key_alloc()
287 key->index_key.type = type; in key_alloc()
288 key->user = user; in key_alloc()
289 key->quotalen = quotalen; in key_alloc()
290 key->datalen = type->def_datalen; in key_alloc()
291 key->uid = uid; in key_alloc()
292 key->gid = gid; in key_alloc()
293 key->perm = perm; in key_alloc()
296 key->flags |= 1 << KEY_FLAG_IN_QUOTA; in key_alloc()
298 key->flags |= 1 << KEY_FLAG_TRUSTED; in key_alloc()
301 key->magic = KEY_DEBUG_MAGIC; in key_alloc()
305 ret = security_key_alloc(key, cred, flags); in key_alloc()
311 key_alloc_serial(key); in key_alloc()
314 return key; in key_alloc()
317 kfree(key->description); in key_alloc()
318 kmem_cache_free(key_jar, key); in key_alloc()
326 key = ERR_PTR(ret); in key_alloc()
330 kmem_cache_free(key_jar, key); in key_alloc()
340 key = ERR_PTR(-ENOMEM); in key_alloc()
346 key = ERR_PTR(-EDQUOT); in key_alloc()
362 int key_payload_reserve(struct key *key, size_t datalen) in key_payload_reserve() argument
364 int delta = (int)datalen - key->datalen; in key_payload_reserve()
367 key_check(key); in key_payload_reserve()
370 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { in key_payload_reserve()
371 unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ? in key_payload_reserve()
374 spin_lock(&key->user->lock); in key_payload_reserve()
377 (key->user->qnbytes + delta >= maxbytes || in key_payload_reserve()
378 key->user->qnbytes + delta < key->user->qnbytes)) { in key_payload_reserve()
382 key->user->qnbytes += delta; in key_payload_reserve()
383 key->quotalen += delta; in key_payload_reserve()
385 spin_unlock(&key->user->lock); in key_payload_reserve()
390 key->datalen = datalen; in key_payload_reserve()
402 static int __key_instantiate_and_link(struct key *key, in __key_instantiate_and_link() argument
404 struct key *keyring, in __key_instantiate_and_link()
405 struct key *authkey, in __key_instantiate_and_link()
410 key_check(key); in __key_instantiate_and_link()
419 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { in __key_instantiate_and_link()
421 ret = key->type->instantiate(key, prep); in __key_instantiate_and_link()
425 atomic_inc(&key->user->nikeys); in __key_instantiate_and_link()
426 set_bit(KEY_FLAG_INSTANTIATED, &key->flags); in __key_instantiate_and_link()
428 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in __key_instantiate_and_link()
433 __key_link(key, _edit); in __key_instantiate_and_link()
440 key->expiry = prep->expiry; in __key_instantiate_and_link()
450 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in __key_instantiate_and_link()
471 int key_instantiate_and_link(struct key *key, in key_instantiate_and_link() argument
474 struct key *keyring, in key_instantiate_and_link()
475 struct key *authkey) in key_instantiate_and_link()
484 prep.quotalen = key->type->def_datalen; in key_instantiate_and_link()
486 if (key->type->preparse) { in key_instantiate_and_link()
487 ret = key->type->preparse(&prep); in key_instantiate_and_link()
493 ret = __key_link_begin(keyring, &key->index_key, &edit); in key_instantiate_and_link()
498 ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit); in key_instantiate_and_link()
501 __key_link_end(keyring, &key->index_key, edit); in key_instantiate_and_link()
504 if (key->type->preparse) in key_instantiate_and_link()
505 key->type->free_preparse(&prep); in key_instantiate_and_link()
532 int key_reject_and_link(struct key *key, in key_reject_and_link() argument
535 struct key *keyring, in key_reject_and_link()
536 struct key *authkey) in key_reject_and_link()
542 key_check(key); in key_reject_and_link()
549 link_ret = __key_link_begin(keyring, &key->index_key, &edit); in key_reject_and_link()
554 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { in key_reject_and_link()
556 atomic_inc(&key->user->nikeys); in key_reject_and_link()
557 key->reject_error = -error; in key_reject_and_link()
559 set_bit(KEY_FLAG_NEGATIVE, &key->flags); in key_reject_and_link()
560 set_bit(KEY_FLAG_INSTANTIATED, &key->flags); in key_reject_and_link()
562 key->expiry = now.tv_sec + timeout; in key_reject_and_link()
563 key_schedule_gc(key->expiry + key_gc_delay); in key_reject_and_link()
565 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) in key_reject_and_link()
572 __key_link(key, &edit); in key_reject_and_link()
582 __key_link_end(keyring, &key->index_key, edit); in key_reject_and_link()
586 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); in key_reject_and_link()
600 void key_put(struct key *key) in key_put() argument
602 if (key) { in key_put()
603 key_check(key); in key_put()
605 if (atomic_dec_and_test(&key->usage)) in key_put()
614 struct key *key_lookup(key_serial_t id) in key_lookup()
617 struct key *key; in key_lookup() local
624 key = rb_entry(n, struct key, serial_node); in key_lookup()
626 if (id < key->serial) in key_lookup()
628 else if (id > key->serial) in key_lookup()
635 key = ERR_PTR(-ENOKEY); in key_lookup()
640 if (atomic_read(&key->usage) == 0) in key_lookup()
646 __key_get(key); in key_lookup()
650 return key; in key_lookup()
679 void key_set_timeout(struct key *key, unsigned timeout) in key_set_timeout() argument
685 down_write(&key->sem); in key_set_timeout()
692 key->expiry = expiry; in key_set_timeout()
693 key_schedule_gc(key->expiry + key_gc_delay); in key_set_timeout()
695 up_write(&key->sem); in key_set_timeout()
716 struct key *key = key_ref_to_ptr(key_ref); in __key_update() local
725 if (!key->type->update) in __key_update()
728 down_write(&key->sem); in __key_update()
730 ret = key->type->update(key, prep); in __key_update()
733 clear_bit(KEY_FLAG_NEGATIVE, &key->flags); in __key_update()
735 up_write(&key->sem); in __key_update()
743 key_put(key); in __key_update()
787 struct key *keyring, *key = NULL; in key_create_or_update() local
875 key = key_alloc(index_key.type, index_key.description, in key_create_or_update()
877 if (IS_ERR(key)) { in key_create_or_update()
878 key_ref = ERR_CAST(key); in key_create_or_update()
883 ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit); in key_create_or_update()
885 key_put(key); in key_create_or_update()
890 key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); in key_create_or_update()
929 struct key *key = key_ref_to_ptr(key_ref); in key_update() local
932 key_check(key); in key_update()
941 if (!key->type->update) in key_update()
947 prep.quotalen = key->type->def_datalen; in key_update()
949 if (key->type->preparse) { in key_update()
950 ret = key->type->preparse(&prep); in key_update()
955 down_write(&key->sem); in key_update()
957 ret = key->type->update(key, &prep); in key_update()
960 clear_bit(KEY_FLAG_NEGATIVE, &key->flags); in key_update()
962 up_write(&key->sem); in key_update()
965 if (key->type->preparse) in key_update()
966 key->type->free_preparse(&prep); in key_update()
980 void key_revoke(struct key *key) in key_revoke() argument
985 key_check(key); in key_revoke()
992 down_write_nested(&key->sem, 1); in key_revoke()
993 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) && in key_revoke()
994 key->type->revoke) in key_revoke()
995 key->type->revoke(key); in key_revoke()
1000 if (key->revoked_at == 0 || key->revoked_at > time) { in key_revoke()
1001 key->revoked_at = time; in key_revoke()
1002 key_schedule_gc(key->revoked_at + key_gc_delay); in key_revoke()
1005 up_write(&key->sem); in key_revoke()
1016 void key_invalidate(struct key *key) in key_invalidate() argument
1018 kenter("%d", key_serial(key)); in key_invalidate()
1020 key_check(key); in key_invalidate()
1022 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { in key_invalidate()
1023 down_write_nested(&key->sem, 1); in key_invalidate()
1024 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) in key_invalidate()
1026 up_write(&key->sem); in key_invalidate()
1041 int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep) in generic_key_instantiate() argument
1047 ret = key_payload_reserve(key, prep->quotalen); in generic_key_instantiate()
1049 rcu_assign_keypointer(key, prep->payload.data[0]); in generic_key_instantiate()
1050 key->payload.data[1] = prep->payload.data[1]; in generic_key_instantiate()
1051 key->payload.data[2] = prep->payload.data[2]; in generic_key_instantiate()
1052 key->payload.data[3] = prep->payload.data[3]; in generic_key_instantiate()
1124 key_jar = kmem_cache_create("key_jar", sizeof(struct key), in key_init()