3 * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/security.h>
17 #include <linux/seq_file.h>
18 #include <linux/err.h>
19 #include <linux/user_namespace.h>
20 #include <linux/nsproxy.h>
21 #include <keys/keyring-type.h>
22 #include <keys/user-type.h>
23 #include <linux/assoc_array_priv.h>
24 #include <linux/uaccess.h>
25 #include <net/net_namespace.h>
29 * When plumbing the depths of the key tree, this sets a hard limit
30 * set on how deep we're willing to go.
32 #define KEYRING_SEARCH_MAX_DEPTH 6
35 * We mark pointers we pass to the associative array with bit 1 set if
36 * they're keyrings and clear otherwise.
38 #define KEYRING_PTR_SUBTYPE 0x2UL
40 static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
42 return (unsigned long)x & KEYRING_PTR_SUBTYPE;
44 static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
46 void *object = assoc_array_ptr_to_leaf(x);
47 return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
49 static inline void *keyring_key_to_ptr(struct key *key)
51 if (key->type == &key_type_keyring)
52 return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
56 static DEFINE_RWLOCK(keyring_name_lock);
59 * Clean up the bits of user_namespace that belong to us.
61 void key_free_user_ns(struct user_namespace *ns)
63 write_lock(&keyring_name_lock);
64 list_del_init(&ns->keyring_name_list);
65 write_unlock(&keyring_name_lock);
67 key_put(ns->user_keyring_register);
68 #ifdef CONFIG_PERSISTENT_KEYRINGS
69 key_put(ns->persistent_keyring_register);
74 * The keyring key type definition. Keyrings are simply keys of this type and
75 * can be treated as ordinary keys in addition to having their own special
78 static int keyring_preparse(struct key_preparsed_payload *prep);
79 static void keyring_free_preparse(struct key_preparsed_payload *prep);
80 static int keyring_instantiate(struct key *keyring,
81 struct key_preparsed_payload *prep);
82 static void keyring_revoke(struct key *keyring);
83 static void keyring_destroy(struct key *keyring);
84 static void keyring_describe(const struct key *keyring, struct seq_file *m);
85 static long keyring_read(const struct key *keyring,
86 char __user *buffer, size_t buflen);
88 struct key_type key_type_keyring = {
91 .preparse = keyring_preparse,
92 .free_preparse = keyring_free_preparse,
93 .instantiate = keyring_instantiate,
94 .revoke = keyring_revoke,
95 .destroy = keyring_destroy,
96 .describe = keyring_describe,
99 EXPORT_SYMBOL(key_type_keyring);
102 * Semaphore to serialise link/link calls to prevent two link calls in parallel
103 * introducing a cycle.
105 static DEFINE_MUTEX(keyring_serialise_link_lock);
108 * Publish the name of a keyring so that it can be found by name (if it has
109 * one and it doesn't begin with a dot).
111 static void keyring_publish_name(struct key *keyring)
113 struct user_namespace *ns = current_user_ns();
115 if (keyring->description &&
116 keyring->description[0] &&
117 keyring->description[0] != '.') {
118 write_lock(&keyring_name_lock);
119 list_add_tail(&keyring->name_link, &ns->keyring_name_list);
120 write_unlock(&keyring_name_lock);
125 * Preparse a keyring payload
127 static int keyring_preparse(struct key_preparsed_payload *prep)
129 return prep->datalen != 0 ? -EINVAL : 0;
133 * Free a preparse of a user defined key payload
135 static void keyring_free_preparse(struct key_preparsed_payload *prep)
140 * Initialise a keyring.
142 * Returns 0 on success, -EINVAL if given any data.
144 static int keyring_instantiate(struct key *keyring,
145 struct key_preparsed_payload *prep)
147 assoc_array_init(&keyring->keys);
148 /* make the keyring available by name if it has one */
149 keyring_publish_name(keyring);
154 * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit. Ideally we'd
155 * fold the carry back too, but that requires inline asm.
157 static u64 mult_64x32_and_fold(u64 x, u32 y)
159 u64 hi = (u64)(u32)(x >> 32) * y;
160 u64 lo = (u64)(u32)(x) * y;
161 return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
165 * Hash a key type and description.
167 static void hash_key_type_and_desc(struct keyring_index_key *index_key)
169 const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
170 const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
171 const char *description = index_key->description;
172 unsigned long hash, type;
175 int n, desc_len = index_key->desc_len;
177 type = (unsigned long)index_key->type;
178 acc = mult_64x32_and_fold(type, desc_len + 13);
179 acc = mult_64x32_and_fold(acc, 9207);
180 piece = (unsigned long)index_key->domain_tag;
181 acc = mult_64x32_and_fold(acc, piece);
182 acc = mult_64x32_and_fold(acc, 9207);
191 memcpy(&piece, description, n);
194 acc = mult_64x32_and_fold(acc, piece);
195 acc = mult_64x32_and_fold(acc, 9207);
198 /* Fold the hash down to 32 bits if need be. */
200 if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
203 /* Squidge all the keyrings into a separate part of the tree to
204 * ordinary keys by making sure the lowest level segment in the hash is
205 * zero for keyrings and non-zero otherwise.
207 if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
208 hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
209 else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
210 hash = (hash + (hash << level_shift)) & ~fan_mask;
211 index_key->hash = hash;
215 * Finalise an index key to include a part of the description actually in the
216 * index key, to set the domain tag and to calculate the hash.
218 void key_set_index_key(struct keyring_index_key *index_key)
220 static struct key_tag default_domain_tag = { .usage = REFCOUNT_INIT(1), };
221 size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc));
223 memcpy(index_key->desc, index_key->description, n);
225 if (!index_key->domain_tag) {
226 if (index_key->type->flags & KEY_TYPE_NET_DOMAIN)
227 index_key->domain_tag = current->nsproxy->net_ns->key_domain;
229 index_key->domain_tag = &default_domain_tag;
232 hash_key_type_and_desc(index_key);
236 * key_put_tag - Release a ref on a tag.
237 * @tag: The tag to release.
239 * This releases a reference the given tag and returns true if that ref was the
242 bool key_put_tag(struct key_tag *tag)
244 if (refcount_dec_and_test(&tag->usage)) {
253 * key_remove_domain - Kill off a key domain and gc its keys
254 * @domain_tag: The domain tag to release.
256 * This marks a domain tag as being dead and releases a ref on it. If that
257 * wasn't the last reference, the garbage collector is poked to try and delete
258 * all keys that were in the domain.
260 void key_remove_domain(struct key_tag *domain_tag)
262 domain_tag->removed = true;
263 if (!key_put_tag(domain_tag))
264 key_schedule_gc_links();
268 * Build the next index key chunk.
270 * We return it one word-sized chunk at a time.
272 static unsigned long keyring_get_key_chunk(const void *data, int level)
274 const struct keyring_index_key *index_key = data;
275 unsigned long chunk = 0;
277 int desc_len = index_key->desc_len, n = sizeof(chunk);
279 level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
282 return index_key->hash;
286 return (unsigned long)index_key->type;
288 return (unsigned long)index_key->domain_tag;
291 if (desc_len <= sizeof(index_key->desc))
294 d = index_key->description + sizeof(index_key->desc);
295 d += level * sizeof(long);
296 desc_len -= sizeof(index_key->desc);
302 } while (--desc_len > 0);
307 static unsigned long keyring_get_object_key_chunk(const void *object, int level)
309 const struct key *key = keyring_ptr_to_key(object);
310 return keyring_get_key_chunk(&key->index_key, level);
313 static bool keyring_compare_object(const void *object, const void *data)
315 const struct keyring_index_key *index_key = data;
316 const struct key *key = keyring_ptr_to_key(object);
318 return key->index_key.type == index_key->type &&
319 key->index_key.domain_tag == index_key->domain_tag &&
320 key->index_key.desc_len == index_key->desc_len &&
321 memcmp(key->index_key.description, index_key->description,
322 index_key->desc_len) == 0;
326 * Compare the index keys of a pair of objects and determine the bit position
327 * at which they differ - if they differ.
329 static int keyring_diff_objects(const void *object, const void *data)
331 const struct key *key_a = keyring_ptr_to_key(object);
332 const struct keyring_index_key *a = &key_a->index_key;
333 const struct keyring_index_key *b = data;
334 unsigned long seg_a, seg_b;
340 if ((seg_a ^ seg_b) != 0)
342 level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
344 /* The number of bits contributed by the hash is controlled by a
345 * constant in the assoc_array headers. Everything else thereafter we
346 * can deal with as being machine word-size dependent.
350 if ((seg_a ^ seg_b) != 0)
352 level += sizeof(unsigned long);
354 /* The next bit may not work on big endian */
355 seg_a = (unsigned long)a->type;
356 seg_b = (unsigned long)b->type;
357 if ((seg_a ^ seg_b) != 0)
359 level += sizeof(unsigned long);
361 seg_a = (unsigned long)a->domain_tag;
362 seg_b = (unsigned long)b->domain_tag;
363 if ((seg_a ^ seg_b) != 0)
365 level += sizeof(unsigned long);
368 if (a->desc_len <= i)
371 for (; i < a->desc_len; i++) {
372 seg_a = *(unsigned char *)(a->description + i);
373 seg_b = *(unsigned char *)(b->description + i);
374 if ((seg_a ^ seg_b) != 0)
384 i = level * 8 + __ffs(seg_a ^ seg_b);
389 * Free an object after stripping the keyring flag off of the pointer.
391 static void keyring_free_object(void *object)
393 key_put(keyring_ptr_to_key(object));
397 * Operations for keyring management by the index-tree routines.
399 static const struct assoc_array_ops keyring_assoc_array_ops = {
400 .get_key_chunk = keyring_get_key_chunk,
401 .get_object_key_chunk = keyring_get_object_key_chunk,
402 .compare_object = keyring_compare_object,
403 .diff_objects = keyring_diff_objects,
404 .free_object = keyring_free_object,
408 * Clean up a keyring when it is destroyed. Unpublish its name if it had one
409 * and dispose of its data.
411 * The garbage collector detects the final key_put(), removes the keyring from
412 * the serial number tree and then does RCU synchronisation before coming here,
413 * so we shouldn't need to worry about code poking around here with the RCU
414 * readlock held by this time.
416 static void keyring_destroy(struct key *keyring)
418 if (keyring->description) {
419 write_lock(&keyring_name_lock);
421 if (keyring->name_link.next != NULL &&
422 !list_empty(&keyring->name_link))
423 list_del(&keyring->name_link);
425 write_unlock(&keyring_name_lock);
428 if (keyring->restrict_link) {
429 struct key_restriction *keyres = keyring->restrict_link;
431 key_put(keyres->key);
435 assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
439 * Describe a keyring for /proc.
441 static void keyring_describe(const struct key *keyring, struct seq_file *m)
443 if (keyring->description)
444 seq_puts(m, keyring->description);
446 seq_puts(m, "[anon]");
448 if (key_is_positive(keyring)) {
449 if (keyring->keys.nr_leaves_on_tree != 0)
450 seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
452 seq_puts(m, ": empty");
456 struct keyring_read_iterator_context {
459 key_serial_t __user *buffer;
462 static int keyring_read_iterator(const void *object, void *data)
464 struct keyring_read_iterator_context *ctx = data;
465 const struct key *key = keyring_ptr_to_key(object);
468 kenter("{%s,%d},,{%zu/%zu}",
469 key->type->name, key->serial, ctx->count, ctx->buflen);
471 if (ctx->count >= ctx->buflen)
474 ret = put_user(key->serial, ctx->buffer);
478 ctx->count += sizeof(key->serial);
483 * Read a list of key IDs from the keyring's contents in binary form
485 * The keyring's semaphore is read-locked by the caller. This prevents someone
486 * from modifying it under us - which could cause us to read key IDs multiple
489 static long keyring_read(const struct key *keyring,
490 char __user *buffer, size_t buflen)
492 struct keyring_read_iterator_context ctx;
495 kenter("{%d},,%zu", key_serial(keyring), buflen);
497 if (buflen & (sizeof(key_serial_t) - 1))
500 /* Copy as many key IDs as fit into the buffer */
501 if (buffer && buflen) {
502 ctx.buffer = (key_serial_t __user *)buffer;
505 ret = assoc_array_iterate(&keyring->keys,
506 keyring_read_iterator, &ctx);
508 kleave(" = %ld [iterate]", ret);
513 /* Return the size of the buffer needed */
514 ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
516 kleave("= %ld [ok]", ret);
518 kleave("= %ld [buffer too small]", ret);
523 * Allocate a keyring and link into the destination keyring.
525 struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
526 const struct cred *cred, key_perm_t perm,
528 struct key_restriction *restrict_link,
534 keyring = key_alloc(&key_type_keyring, description,
535 uid, gid, cred, perm, flags, restrict_link);
536 if (!IS_ERR(keyring)) {
537 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
540 keyring = ERR_PTR(ret);
546 EXPORT_SYMBOL(keyring_alloc);
549 * restrict_link_reject - Give -EPERM to restrict link
550 * @keyring: The keyring being added to.
551 * @type: The type of key being added.
552 * @payload: The payload of the key intended to be added.
553 * @restriction_key: Keys providing additional data for evaluating restriction.
555 * Reject the addition of any links to a keyring. It can be overridden by
556 * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
557 * adding a key to a keyring.
559 * This is meant to be stored in a key_restriction structure which is passed
560 * in the restrict_link parameter to keyring_alloc().
562 int restrict_link_reject(struct key *keyring,
563 const struct key_type *type,
564 const union key_payload *payload,
565 struct key *restriction_key)
571 * By default, we keys found by getting an exact match on their descriptions.
573 bool key_default_cmp(const struct key *key,
574 const struct key_match_data *match_data)
576 return strcmp(key->description, match_data->raw_data) == 0;
580 * Iteration function to consider each key found.
582 static int keyring_search_iterator(const void *object, void *iterator_data)
584 struct keyring_search_context *ctx = iterator_data;
585 const struct key *key = keyring_ptr_to_key(object);
586 unsigned long kflags = READ_ONCE(key->flags);
587 short state = READ_ONCE(key->state);
589 kenter("{%d}", key->serial);
591 /* ignore keys not of this type */
592 if (key->type != ctx->index_key.type) {
593 kleave(" = 0 [!type]");
597 /* skip invalidated, revoked and expired keys */
598 if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
599 time64_t expiry = READ_ONCE(key->expiry);
601 if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
602 (1 << KEY_FLAG_REVOKED))) {
603 ctx->result = ERR_PTR(-EKEYREVOKED);
604 kleave(" = %d [invrev]", ctx->skipped_ret);
608 if (expiry && ctx->now >= expiry) {
609 if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
610 ctx->result = ERR_PTR(-EKEYEXPIRED);
611 kleave(" = %d [expire]", ctx->skipped_ret);
616 /* keys that don't match */
617 if (!ctx->match_data.cmp(key, &ctx->match_data)) {
618 kleave(" = 0 [!match]");
622 /* key must have search permissions */
623 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
624 key_task_permission(make_key_ref(key, ctx->possessed),
625 ctx->cred, KEY_NEED_SEARCH) < 0) {
626 ctx->result = ERR_PTR(-EACCES);
627 kleave(" = %d [!perm]", ctx->skipped_ret);
631 if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
632 /* we set a different error code if we pass a negative key */
634 ctx->result = ERR_PTR(state);
635 kleave(" = %d [neg]", ctx->skipped_ret);
641 ctx->result = make_key_ref(key, ctx->possessed);
642 kleave(" = 1 [found]");
646 return ctx->skipped_ret;
650 * Search inside a keyring for a key. We can search by walking to it
651 * directly based on its index-key or we can iterate over the entire
652 * tree looking for it, based on the match function.
654 static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
656 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
659 object = assoc_array_find(&keyring->keys,
660 &keyring_assoc_array_ops,
662 return object ? ctx->iterator(object, ctx) : 0;
664 return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
668 * Search a tree of keyrings that point to other keyrings up to the maximum
671 static bool search_nested_keyrings(struct key *keyring,
672 struct keyring_search_context *ctx)
676 struct assoc_array_node *node;
678 } stack[KEYRING_SEARCH_MAX_DEPTH];
680 struct assoc_array_shortcut *shortcut;
681 struct assoc_array_node *node;
682 struct assoc_array_ptr *ptr;
686 kenter("{%d},{%s,%s}",
688 ctx->index_key.type->name,
689 ctx->index_key.description);
691 #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
692 BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
693 (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
695 if (ctx->index_key.description)
696 key_set_index_key(&ctx->index_key);
698 /* Check to see if this top-level keyring is what we are looking for
699 * and whether it is valid or not.
701 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
702 keyring_compare_object(keyring, &ctx->index_key)) {
703 ctx->skipped_ret = 2;
704 switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
714 ctx->skipped_ret = 0;
716 /* Start processing a new keyring */
718 kdebug("descend to %d", keyring->serial);
719 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
720 (1 << KEY_FLAG_REVOKED)))
721 goto not_this_keyring;
723 /* Search through the keys in this keyring before its searching its
726 if (search_keyring(keyring, ctx))
729 /* Then manually iterate through the keyrings nested in this one.
731 * Start from the root node of the index tree. Because of the way the
732 * hash function has been set up, keyrings cluster on the leftmost
733 * branch of the root node (root slot 0) or in the root node itself.
734 * Non-keyrings avoid the leftmost branch of the root entirely (root
737 if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
738 goto not_this_keyring;
740 ptr = READ_ONCE(keyring->keys.root);
742 goto not_this_keyring;
744 if (assoc_array_ptr_is_shortcut(ptr)) {
745 /* If the root is a shortcut, either the keyring only contains
746 * keyring pointers (everything clusters behind root slot 0) or
747 * doesn't contain any keyring pointers.
749 shortcut = assoc_array_ptr_to_shortcut(ptr);
750 if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
751 goto not_this_keyring;
753 ptr = READ_ONCE(shortcut->next_node);
754 node = assoc_array_ptr_to_node(ptr);
758 node = assoc_array_ptr_to_node(ptr);
759 ptr = node->slots[0];
760 if (!assoc_array_ptr_is_meta(ptr))
764 /* Descend to a more distal node in this keyring's content tree and go
768 if (assoc_array_ptr_is_shortcut(ptr)) {
769 shortcut = assoc_array_ptr_to_shortcut(ptr);
770 ptr = READ_ONCE(shortcut->next_node);
771 BUG_ON(!assoc_array_ptr_is_node(ptr));
773 node = assoc_array_ptr_to_node(ptr);
776 kdebug("begin_node");
779 /* Go through the slots in a node */
780 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
781 ptr = READ_ONCE(node->slots[slot]);
783 if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
784 goto descend_to_node;
786 if (!keyring_ptr_is_keyring(ptr))
789 key = keyring_ptr_to_key(ptr);
791 if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
792 if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
793 ctx->result = ERR_PTR(-ELOOP);
796 goto not_this_keyring;
799 /* Search a nested keyring */
800 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
801 key_task_permission(make_key_ref(key, ctx->possessed),
802 ctx->cred, KEY_NEED_SEARCH) < 0)
805 /* stack the current position */
806 stack[sp].keyring = keyring;
807 stack[sp].node = node;
808 stack[sp].slot = slot;
811 /* begin again with the new keyring */
813 goto descend_to_keyring;
816 /* We've dealt with all the slots in the current node, so now we need
817 * to ascend to the parent and continue processing there.
819 ptr = READ_ONCE(node->back_pointer);
820 slot = node->parent_slot;
822 if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
823 shortcut = assoc_array_ptr_to_shortcut(ptr);
824 ptr = READ_ONCE(shortcut->back_pointer);
825 slot = shortcut->parent_slot;
828 goto not_this_keyring;
829 node = assoc_array_ptr_to_node(ptr);
832 /* If we've ascended to the root (zero backpointer), we must have just
833 * finished processing the leftmost branch rather than the root slots -
834 * so there can't be any more keyrings for us to find.
836 if (node->back_pointer) {
837 kdebug("ascend %d", slot);
841 /* The keyring we're looking at was disqualified or didn't contain a
845 kdebug("not_this_keyring %d", sp);
851 /* Resume the processing of a keyring higher up in the tree */
853 keyring = stack[sp].keyring;
854 node = stack[sp].node;
855 slot = stack[sp].slot + 1;
856 kdebug("ascend to %d [%d]", keyring->serial, slot);
859 /* We found a viable match */
861 key = key_ref_to_ptr(ctx->result);
863 if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
864 key->last_used_at = ctx->now;
865 keyring->last_used_at = ctx->now;
867 stack[--sp].keyring->last_used_at = ctx->now;
874 * keyring_search_rcu - Search a keyring tree for a matching key under RCU
875 * @keyring_ref: A pointer to the keyring with possession indicator.
876 * @ctx: The keyring search context.
878 * Search the supplied keyring tree for a key that matches the criteria given.
879 * The root keyring and any linked keyrings must grant Search permission to the
880 * caller to be searchable and keys can only be found if they too grant Search
881 * to the caller. The possession flag on the root keyring pointer controls use
882 * of the possessor bits in permissions checking of the entire tree. In
883 * addition, the LSM gets to forbid keyring searches and key matches.
885 * The search is performed as a breadth-then-depth search up to the prescribed
886 * limit (KEYRING_SEARCH_MAX_DEPTH). The caller must hold the RCU read lock to
887 * prevent keyrings from being destroyed or rearranged whilst they are being
890 * Keys are matched to the type provided and are then filtered by the match
891 * function, which is given the description to use in any way it sees fit. The
892 * match function may use any attributes of a key that it wishes to to
893 * determine the match. Normally the match function from the key type would be
896 * RCU can be used to prevent the keyring key lists from disappearing without
897 * the need to take lots of locks.
899 * Returns a pointer to the found key and increments the key usage count if
900 * successful; -EAGAIN if no matching keys were found, or if expired or revoked
901 * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
902 * specified keyring wasn't a keyring.
904 * In the case of a successful return, the possession attribute from
905 * @keyring_ref is propagated to the returned key reference.
907 key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
908 struct keyring_search_context *ctx)
913 ctx->iterator = keyring_search_iterator;
914 ctx->possessed = is_key_possessed(keyring_ref);
915 ctx->result = ERR_PTR(-EAGAIN);
917 keyring = key_ref_to_ptr(keyring_ref);
920 if (keyring->type != &key_type_keyring)
921 return ERR_PTR(-ENOTDIR);
923 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
924 err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
929 ctx->now = ktime_get_real_seconds();
930 if (search_nested_keyrings(keyring, ctx))
931 __key_get(key_ref_to_ptr(ctx->result));
936 * keyring_search - Search the supplied keyring tree for a matching key
937 * @keyring: The root of the keyring tree to be searched.
938 * @type: The type of keyring we want to find.
939 * @description: The name of the keyring we want to find.
940 * @recurse: True to search the children of @keyring also
942 * As keyring_search_rcu() above, but using the current task's credentials and
943 * type's default matching function and preferred search method.
945 key_ref_t keyring_search(key_ref_t keyring,
946 struct key_type *type,
947 const char *description,
950 struct keyring_search_context ctx = {
951 .index_key.type = type,
952 .index_key.description = description,
953 .index_key.desc_len = strlen(description),
954 .cred = current_cred(),
955 .match_data.cmp = key_default_cmp,
956 .match_data.raw_data = description,
957 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
958 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
964 ctx.flags |= KEYRING_SEARCH_RECURSE;
965 if (type->match_preparse) {
966 ret = type->match_preparse(&ctx.match_data);
972 key = keyring_search_rcu(keyring, &ctx);
975 if (type->match_free)
976 type->match_free(&ctx.match_data);
979 EXPORT_SYMBOL(keyring_search);
981 static struct key_restriction *keyring_restriction_alloc(
982 key_restrict_link_func_t check)
984 struct key_restriction *keyres =
985 kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
988 return ERR_PTR(-ENOMEM);
990 keyres->check = check;
996 * Semaphore to serialise restriction setup to prevent reference count
997 * cycles through restriction key pointers.
999 static DECLARE_RWSEM(keyring_serialise_restrict_sem);
1002 * Check for restriction cycles that would prevent keyring garbage collection.
1003 * keyring_serialise_restrict_sem must be held.
1005 static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
1006 struct key_restriction *keyres)
1008 while (keyres && keyres->key &&
1009 keyres->key->type == &key_type_keyring) {
1010 if (keyres->key == dest_keyring)
1013 keyres = keyres->key->restrict_link;
1020 * keyring_restrict - Look up and apply a restriction to a keyring
1021 * @keyring_ref: The keyring to be restricted
1022 * @type: The key type that will provide the restriction checker.
1023 * @restriction: The restriction options to apply to the keyring
1025 * Look up a keyring and apply a restriction to it. The restriction is managed
1026 * by the specific key type, but can be configured by the options specified in
1027 * the restriction string.
1029 int keyring_restrict(key_ref_t keyring_ref, const char *type,
1030 const char *restriction)
1032 struct key *keyring;
1033 struct key_type *restrict_type = NULL;
1034 struct key_restriction *restrict_link;
1037 keyring = key_ref_to_ptr(keyring_ref);
1040 if (keyring->type != &key_type_keyring)
1044 restrict_link = keyring_restriction_alloc(restrict_link_reject);
1046 restrict_type = key_type_lookup(type);
1048 if (IS_ERR(restrict_type))
1049 return PTR_ERR(restrict_type);
1051 if (!restrict_type->lookup_restriction) {
1056 restrict_link = restrict_type->lookup_restriction(restriction);
1059 if (IS_ERR(restrict_link)) {
1060 ret = PTR_ERR(restrict_link);
1064 down_write(&keyring->sem);
1065 down_write(&keyring_serialise_restrict_sem);
1067 if (keyring->restrict_link)
1069 else if (keyring_detect_restriction_cycle(keyring, restrict_link))
1072 keyring->restrict_link = restrict_link;
1074 up_write(&keyring_serialise_restrict_sem);
1075 up_write(&keyring->sem);
1078 key_put(restrict_link->key);
1079 kfree(restrict_link);
1084 key_type_put(restrict_type);
1088 EXPORT_SYMBOL(keyring_restrict);
1091 * Search the given keyring for a key that might be updated.
1093 * The caller must guarantee that the keyring is a keyring and that the
1094 * permission is granted to modify the keyring as no check is made here. The
1095 * caller must also hold a lock on the keyring semaphore.
1097 * Returns a pointer to the found key with usage count incremented if
1098 * successful and returns NULL if not found. Revoked and invalidated keys are
1101 * If successful, the possession indicator is propagated from the keyring ref
1102 * to the returned key reference.
1104 key_ref_t find_key_to_update(key_ref_t keyring_ref,
1105 const struct keyring_index_key *index_key)
1107 struct key *keyring, *key;
1110 keyring = key_ref_to_ptr(keyring_ref);
1112 kenter("{%d},{%s,%s}",
1113 keyring->serial, index_key->type->name, index_key->description);
1115 object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1125 key = keyring_ptr_to_key(object);
1126 if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1127 (1 << KEY_FLAG_REVOKED))) {
1128 kleave(" = NULL [x]");
1132 kleave(" = {%d}", key->serial);
1133 return make_key_ref(key, is_key_possessed(keyring_ref));
1137 * Find a keyring with the specified name.
1139 * Only keyrings that have nonzero refcount, are not revoked, and are owned by a
1140 * user in the current user namespace are considered. If @uid_keyring is %true,
1141 * the keyring additionally must have been allocated as a user or user session
1142 * keyring; otherwise, it must grant Search permission directly to the caller.
1144 * Returns a pointer to the keyring with the keyring's refcount having being
1145 * incremented on success. -ENOKEY is returned if a key could not be found.
1147 struct key *find_keyring_by_name(const char *name, bool uid_keyring)
1149 struct user_namespace *ns = current_user_ns();
1150 struct key *keyring;
1153 return ERR_PTR(-EINVAL);
1155 read_lock(&keyring_name_lock);
1157 /* Search this hash bucket for a keyring with a matching name that
1158 * grants Search permission and that hasn't been revoked
1160 list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
1161 if (!kuid_has_mapping(ns, keyring->user->uid))
1164 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1167 if (strcmp(keyring->description, name) != 0)
1171 if (!test_bit(KEY_FLAG_UID_KEYRING,
1175 if (key_permission(make_key_ref(keyring, 0),
1176 KEY_NEED_SEARCH) < 0)
1180 /* we've got a match but we might end up racing with
1181 * key_cleanup() if the keyring is currently 'dead'
1182 * (ie. it has a zero usage count) */
1183 if (!refcount_inc_not_zero(&keyring->usage))
1185 keyring->last_used_at = ktime_get_real_seconds();
1189 keyring = ERR_PTR(-ENOKEY);
1191 read_unlock(&keyring_name_lock);
1195 static int keyring_detect_cycle_iterator(const void *object,
1196 void *iterator_data)
1198 struct keyring_search_context *ctx = iterator_data;
1199 const struct key *key = keyring_ptr_to_key(object);
1201 kenter("{%d}", key->serial);
1203 /* We might get a keyring with matching index-key that is nonetheless a
1204 * different keyring. */
1205 if (key != ctx->match_data.raw_data)
1208 ctx->result = ERR_PTR(-EDEADLK);
1213 * See if a cycle will will be created by inserting acyclic tree B in acyclic
1214 * tree A at the topmost level (ie: as a direct child of A).
1216 * Since we are adding B to A at the top level, checking for cycles should just
1217 * be a matter of seeing if node A is somewhere in tree B.
1219 static int keyring_detect_cycle(struct key *A, struct key *B)
1221 struct keyring_search_context ctx = {
1222 .index_key = A->index_key,
1223 .match_data.raw_data = A,
1224 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1225 .iterator = keyring_detect_cycle_iterator,
1226 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
1227 KEYRING_SEARCH_NO_UPDATE_TIME |
1228 KEYRING_SEARCH_NO_CHECK_PERM |
1229 KEYRING_SEARCH_DETECT_TOO_DEEP |
1230 KEYRING_SEARCH_RECURSE),
1234 search_nested_keyrings(B, &ctx);
1236 return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1240 * Lock keyring for link.
1242 int __key_link_lock(struct key *keyring,
1243 const struct keyring_index_key *index_key)
1244 __acquires(&keyring->sem)
1245 __acquires(&keyring_serialise_link_lock)
1247 if (keyring->type != &key_type_keyring)
1250 down_write(&keyring->sem);
1252 /* Serialise link/link calls to prevent parallel calls causing a cycle
1253 * when linking two keyring in opposite orders.
1255 if (index_key->type == &key_type_keyring)
1256 mutex_lock(&keyring_serialise_link_lock);
1262 * Lock keyrings for move (link/unlink combination).
1264 int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1265 const struct keyring_index_key *index_key)
1266 __acquires(&l_keyring->sem)
1267 __acquires(&u_keyring->sem)
1268 __acquires(&keyring_serialise_link_lock)
1270 if (l_keyring->type != &key_type_keyring ||
1271 u_keyring->type != &key_type_keyring)
1274 /* We have to be very careful here to take the keyring locks in the
1275 * right order, lest we open ourselves to deadlocking against another
1278 if (l_keyring < u_keyring) {
1279 down_write(&l_keyring->sem);
1280 down_write_nested(&u_keyring->sem, 1);
1282 down_write(&u_keyring->sem);
1283 down_write_nested(&l_keyring->sem, 1);
1286 /* Serialise link/link calls to prevent parallel calls causing a cycle
1287 * when linking two keyring in opposite orders.
1289 if (index_key->type == &key_type_keyring)
1290 mutex_lock(&keyring_serialise_link_lock);
1296 * Preallocate memory so that a key can be linked into to a keyring.
1298 int __key_link_begin(struct key *keyring,
1299 const struct keyring_index_key *index_key,
1300 struct assoc_array_edit **_edit)
1302 struct assoc_array_edit *edit;
1306 keyring->serial, index_key->type->name, index_key->description);
1308 BUG_ON(index_key->desc_len == 0);
1309 BUG_ON(*_edit != NULL);
1314 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1317 /* Create an edit script that will insert/replace the key in the
1320 edit = assoc_array_insert(&keyring->keys,
1321 &keyring_assoc_array_ops,
1325 ret = PTR_ERR(edit);
1329 /* If we're not replacing a link in-place then we're going to need some
1332 if (!edit->dead_leaf) {
1333 ret = key_payload_reserve(keyring,
1334 keyring->datalen + KEYQUOTA_LINK_BYTES);
1344 assoc_array_cancel_edit(edit);
1346 kleave(" = %d", ret);
1351 * Check already instantiated keys aren't going to be a problem.
1353 * The caller must have called __key_link_begin(). Don't need to call this for
1354 * keys that were created since __key_link_begin() was called.
1356 int __key_link_check_live_key(struct key *keyring, struct key *key)
1358 if (key->type == &key_type_keyring)
1359 /* check that we aren't going to create a cycle by linking one
1360 * keyring to another */
1361 return keyring_detect_cycle(keyring, key);
1366 * Link a key into to a keyring.
1368 * Must be called with __key_link_begin() having being called. Discards any
1369 * already extant link to matching key if there is one, so that each keyring
1370 * holds at most one link to any given key of a particular type+description
1373 void __key_link(struct key *key, struct assoc_array_edit **_edit)
1376 assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1377 assoc_array_apply_edit(*_edit);
1382 * Finish linking a key into to a keyring.
1384 * Must be called with __key_link_begin() having being called.
1386 void __key_link_end(struct key *keyring,
1387 const struct keyring_index_key *index_key,
1388 struct assoc_array_edit *edit)
1389 __releases(&keyring->sem)
1390 __releases(&keyring_serialise_link_lock)
1392 BUG_ON(index_key->type == NULL);
1393 kenter("%d,%s,", keyring->serial, index_key->type->name);
1396 if (!edit->dead_leaf) {
1397 key_payload_reserve(keyring,
1398 keyring->datalen - KEYQUOTA_LINK_BYTES);
1400 assoc_array_cancel_edit(edit);
1402 up_write(&keyring->sem);
1404 if (index_key->type == &key_type_keyring)
1405 mutex_unlock(&keyring_serialise_link_lock);
1409 * Check addition of keys to restricted keyrings.
1411 static int __key_link_check_restriction(struct key *keyring, struct key *key)
1413 if (!keyring->restrict_link || !keyring->restrict_link->check)
1415 return keyring->restrict_link->check(keyring, key->type, &key->payload,
1416 keyring->restrict_link->key);
1420 * key_link - Link a key to a keyring
1421 * @keyring: The keyring to make the link in.
1422 * @key: The key to link to.
1424 * Make a link in a keyring to a key, such that the keyring holds a reference
1425 * on that key and the key can potentially be found by searching that keyring.
1427 * This function will write-lock the keyring's semaphore and will consume some
1428 * of the user's key data quota to hold the link.
1430 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1431 * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1432 * full, -EDQUOT if there is insufficient key data quota remaining to add
1433 * another link or -ENOMEM if there's insufficient memory.
1435 * It is assumed that the caller has checked that it is permitted for a link to
1436 * be made (the keyring should have Write permission and the key Link
1439 int key_link(struct key *keyring, struct key *key)
1441 struct assoc_array_edit *edit = NULL;
1444 kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1449 ret = __key_link_lock(keyring, &key->index_key);
1453 ret = __key_link_begin(keyring, &key->index_key, &edit);
1457 kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1458 ret = __key_link_check_restriction(keyring, key);
1460 ret = __key_link_check_live_key(keyring, key);
1462 __key_link(key, &edit);
1465 __key_link_end(keyring, &key->index_key, edit);
1467 kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
1470 EXPORT_SYMBOL(key_link);
1473 * Lock a keyring for unlink.
1475 static int __key_unlink_lock(struct key *keyring)
1476 __acquires(&keyring->sem)
1478 if (keyring->type != &key_type_keyring)
1481 down_write(&keyring->sem);
1486 * Begin the process of unlinking a key from a keyring.
1488 static int __key_unlink_begin(struct key *keyring, struct key *key,
1489 struct assoc_array_edit **_edit)
1491 struct assoc_array_edit *edit;
1493 BUG_ON(*_edit != NULL);
1495 edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1498 return PTR_ERR(edit);
1508 * Apply an unlink change.
1510 static void __key_unlink(struct key *keyring, struct key *key,
1511 struct assoc_array_edit **_edit)
1513 assoc_array_apply_edit(*_edit);
1515 key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1519 * Finish unlinking a key from to a keyring.
1521 static void __key_unlink_end(struct key *keyring,
1523 struct assoc_array_edit *edit)
1524 __releases(&keyring->sem)
1527 assoc_array_cancel_edit(edit);
1528 up_write(&keyring->sem);
1532 * key_unlink - Unlink the first link to a key from a keyring.
1533 * @keyring: The keyring to remove the link from.
1534 * @key: The key the link is to.
1536 * Remove a link from a keyring to a key.
1538 * This function will write-lock the keyring's semaphore.
1540 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1541 * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1544 * It is assumed that the caller has checked that it is permitted for a link to
1545 * be removed (the keyring should have Write permission; no permissions are
1546 * required on the key).
1548 int key_unlink(struct key *keyring, struct key *key)
1550 struct assoc_array_edit *edit = NULL;
1556 ret = __key_unlink_lock(keyring);
1560 ret = __key_unlink_begin(keyring, key, &edit);
1562 __key_unlink(keyring, key, &edit);
1563 __key_unlink_end(keyring, key, edit);
1566 EXPORT_SYMBOL(key_unlink);
1569 * key_move - Move a key from one keyring to another
1570 * @key: The key to move
1571 * @from_keyring: The keyring to remove the link from.
1572 * @to_keyring: The keyring to make the link in.
1573 * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1575 * Make a link in @to_keyring to a key, such that the keyring holds a reference
1576 * on that key and the key can potentially be found by searching that keyring
1577 * whilst simultaneously removing a link to the key from @from_keyring.
1579 * This function will write-lock both keyring's semaphores and will consume
1580 * some of the user's key data quota to hold the link on @to_keyring.
1582 * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1583 * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1584 * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1585 * to add another link or -ENOMEM if there's insufficient memory. If
1586 * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1587 * matching key in @to_keyring.
1589 * It is assumed that the caller has checked that it is permitted for a link to
1590 * be made (the keyring should have Write permission and the key Link
1593 int key_move(struct key *key,
1594 struct key *from_keyring,
1595 struct key *to_keyring,
1598 struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1601 kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1603 if (from_keyring == to_keyring)
1607 key_check(from_keyring);
1608 key_check(to_keyring);
1610 ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1613 ret = __key_unlink_begin(from_keyring, key, &from_edit);
1616 ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1621 if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1624 ret = __key_link_check_restriction(to_keyring, key);
1627 ret = __key_link_check_live_key(to_keyring, key);
1631 __key_unlink(from_keyring, key, &from_edit);
1632 __key_link(key, &to_edit);
1634 __key_link_end(to_keyring, &key->index_key, to_edit);
1635 __key_unlink_end(from_keyring, key, from_edit);
1637 kleave(" = %d", ret);
1640 EXPORT_SYMBOL(key_move);
1643 * keyring_clear - Clear a keyring
1644 * @keyring: The keyring to clear.
1646 * Clear the contents of the specified keyring.
1648 * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
1650 int keyring_clear(struct key *keyring)
1652 struct assoc_array_edit *edit;
1655 if (keyring->type != &key_type_keyring)
1658 down_write(&keyring->sem);
1660 edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1662 ret = PTR_ERR(edit);
1665 assoc_array_apply_edit(edit);
1666 key_payload_reserve(keyring, 0);
1670 up_write(&keyring->sem);
1673 EXPORT_SYMBOL(keyring_clear);
1676 * Dispose of the links from a revoked keyring.
1678 * This is called with the key sem write-locked.
1680 static void keyring_revoke(struct key *keyring)
1682 struct assoc_array_edit *edit;
1684 edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1685 if (!IS_ERR(edit)) {
1687 assoc_array_apply_edit(edit);
1688 key_payload_reserve(keyring, 0);
1692 static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1694 struct key *key = keyring_ptr_to_key(object);
1695 time64_t *limit = iterator_data;
1697 if (key_is_dead(key, *limit))
1703 static int keyring_gc_check_iterator(const void *object, void *iterator_data)
1705 const struct key *key = keyring_ptr_to_key(object);
1706 time64_t *limit = iterator_data;
1709 return key_is_dead(key, *limit);
1713 * Garbage collect pointers from a keyring.
1715 * Not called with any locks held. The keyring's key struct will not be
1716 * deallocated under us as only our caller may deallocate it.
1718 void keyring_gc(struct key *keyring, time64_t limit)
1722 kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1724 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
1725 (1 << KEY_FLAG_REVOKED)))
1728 /* scan the keyring looking for dead keys */
1730 result = assoc_array_iterate(&keyring->keys,
1731 keyring_gc_check_iterator, &limit);
1741 down_write(&keyring->sem);
1742 assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
1743 keyring_gc_select_iterator, &limit);
1744 up_write(&keyring->sem);
1749 * Garbage collect restriction pointers from a keyring.
1751 * Keyring restrictions are associated with a key type, and must be cleaned
1752 * up if the key type is unregistered. The restriction is altered to always
1753 * reject additional keys so a keyring cannot be opened up by unregistering
1756 * Not called with any keyring locks held. The keyring's key struct will not
1757 * be deallocated under us as only our caller may deallocate it.
1759 * The caller is required to hold key_types_sem and dead_type->sem. This is
1760 * fulfilled by key_gc_keytype() holding the locks on behalf of
1761 * key_garbage_collector(), which it invokes on a workqueue.
1763 void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
1765 struct key_restriction *keyres;
1767 kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1770 * keyring->restrict_link is only assigned at key allocation time
1771 * or with the key type locked, so the only values that could be
1772 * concurrently assigned to keyring->restrict_link are for key
1773 * types other than dead_type. Given this, it's ok to check
1774 * the key type before acquiring keyring->sem.
1776 if (!dead_type || !keyring->restrict_link ||
1777 keyring->restrict_link->keytype != dead_type) {
1778 kleave(" [no restriction gc]");
1782 /* Lock the keyring to ensure that a link is not in progress */
1783 down_write(&keyring->sem);
1785 keyres = keyring->restrict_link;
1787 keyres->check = restrict_link_reject;
1789 key_put(keyres->key);
1791 keyres->keytype = NULL;
1793 up_write(&keyring->sem);
1795 kleave(" [restriction gc]");