1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25d135440SDavid Howells /* Key garbage collector
35d135440SDavid Howells *
40c061b57SDavid Howells * Copyright (C) 2009-2011 Red Hat, Inc. All Rights Reserved.
55d135440SDavid Howells * Written by David Howells (dhowells@redhat.com)
65d135440SDavid Howells */
75d135440SDavid Howells
88bc16deaSDavid Howells #include <linux/slab.h>
98bc16deaSDavid Howells #include <linux/security.h>
105d135440SDavid Howells #include <keys/keyring-type.h>
115d135440SDavid Howells #include "internal.h"
125d135440SDavid Howells
135d135440SDavid Howells /*
145d135440SDavid Howells * Delay between key revocation/expiry in seconds
155d135440SDavid Howells */
165d135440SDavid Howells unsigned key_gc_delay = 5 * 60;
175d135440SDavid Howells
185d135440SDavid Howells /*
198bc16deaSDavid Howells * Reaper for unused keys.
208bc16deaSDavid Howells */
210c061b57SDavid Howells static void key_garbage_collector(struct work_struct *work);
220c061b57SDavid Howells DECLARE_WORK(key_gc_work, key_garbage_collector);
238bc16deaSDavid Howells
248bc16deaSDavid Howells /*
258bc16deaSDavid Howells * Reaper for links from keyrings to dead keys.
265d135440SDavid Howells */
2724ed960aSKees Cook static void key_gc_timer_func(struct timer_list *);
281d27e3e2SKees Cook static DEFINE_TIMER(key_gc_timer, key_gc_timer_func);
290c061b57SDavid Howells
30074d5898SBaolin Wang static time64_t key_gc_next_run = TIME64_MAX;
310c061b57SDavid Howells static struct key_type *key_gc_dead_keytype;
320c061b57SDavid Howells
330c061b57SDavid Howells static unsigned long key_gc_flags;
340c061b57SDavid Howells #define KEY_GC_KEY_EXPIRED 0 /* A key expired and needs unlinking */
350c061b57SDavid Howells #define KEY_GC_REAP_KEYTYPE 1 /* A keytype is being unregistered */
360c061b57SDavid Howells #define KEY_GC_REAPING_KEYTYPE 2 /* Cleared when keytype reaped */
370c061b57SDavid Howells
380c061b57SDavid Howells
390c061b57SDavid Howells /*
400c061b57SDavid Howells * Any key whose type gets unregistered will be re-typed to this if it can't be
410c061b57SDavid Howells * immediately unlinked.
420c061b57SDavid Howells */
430c061b57SDavid Howells struct key_type key_type_dead = {
44c1644fe0SDavid Howells .name = ".dead",
450c061b57SDavid Howells };
465d135440SDavid Howells
475d135440SDavid Howells /*
48973c9f4fSDavid Howells * Schedule a garbage collection run.
49973c9f4fSDavid Howells * - time precision isn't particularly important
505d135440SDavid Howells */
key_schedule_gc(time64_t gc_at)51074d5898SBaolin Wang void key_schedule_gc(time64_t gc_at)
525d135440SDavid Howells {
535d135440SDavid Howells unsigned long expires;
54074d5898SBaolin Wang time64_t now = ktime_get_real_seconds();
555d135440SDavid Howells
56074d5898SBaolin Wang kenter("%lld", gc_at - now);
575d135440SDavid Howells
580c061b57SDavid Howells if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
590c061b57SDavid Howells kdebug("IMMEDIATE");
603b07e9caSTejun Heo schedule_work(&key_gc_work);
615d135440SDavid Howells } else if (gc_at < key_gc_next_run) {
620c061b57SDavid Howells kdebug("DEFERRED");
630c061b57SDavid Howells key_gc_next_run = gc_at;
645d135440SDavid Howells expires = jiffies + (gc_at - now) * HZ;
655d135440SDavid Howells mod_timer(&key_gc_timer, expires);
665d135440SDavid Howells }
675d135440SDavid Howells }
685d135440SDavid Howells
695d135440SDavid Howells /*
70*afc360e8SDavid Howells * Set the expiration time on a key.
71*afc360e8SDavid Howells */
key_set_expiry(struct key * key,time64_t expiry)72*afc360e8SDavid Howells void key_set_expiry(struct key *key, time64_t expiry)
73*afc360e8SDavid Howells {
74*afc360e8SDavid Howells key->expiry = expiry;
75*afc360e8SDavid Howells if (expiry != TIME64_MAX) {
76*afc360e8SDavid Howells if (!(key->type->flags & KEY_TYPE_INSTANT_REAP))
77*afc360e8SDavid Howells expiry += key_gc_delay;
78*afc360e8SDavid Howells key_schedule_gc(expiry);
79*afc360e8SDavid Howells }
80*afc360e8SDavid Howells }
81*afc360e8SDavid Howells
82*afc360e8SDavid Howells /*
83fd75815fSDavid Howells * Schedule a dead links collection run.
84fd75815fSDavid Howells */
key_schedule_gc_links(void)85fd75815fSDavid Howells void key_schedule_gc_links(void)
86fd75815fSDavid Howells {
87fd75815fSDavid Howells set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
883b07e9caSTejun Heo schedule_work(&key_gc_work);
89fd75815fSDavid Howells }
90fd75815fSDavid Howells
91fd75815fSDavid Howells /*
920c061b57SDavid Howells * Some key's cleanup time was met after it expired, so we need to get the
930c061b57SDavid Howells * reaper to go through a cycle finding expired keys.
945d135440SDavid Howells */
key_gc_timer_func(struct timer_list * unused)9524ed960aSKees Cook static void key_gc_timer_func(struct timer_list *unused)
965d135440SDavid Howells {
975d135440SDavid Howells kenter("");
98074d5898SBaolin Wang key_gc_next_run = TIME64_MAX;
99fd75815fSDavid Howells key_schedule_gc_links();
1005d135440SDavid Howells }
1015d135440SDavid Howells
1025d135440SDavid Howells /*
1030c061b57SDavid Howells * Reap keys of dead type.
1040c061b57SDavid Howells *
1050c061b57SDavid Howells * We use three flags to make sure we see three complete cycles of the garbage
1060c061b57SDavid Howells * collector: the first to mark keys of that type as being dead, the second to
1070c061b57SDavid Howells * collect dead links and the third to clean up the dead keys. We have to be
1080c061b57SDavid Howells * careful as there may already be a cycle in progress.
1090c061b57SDavid Howells *
1100c061b57SDavid Howells * The caller must be holding key_types_sem.
1110c061b57SDavid Howells */
key_gc_keytype(struct key_type * ktype)1120c061b57SDavid Howells void key_gc_keytype(struct key_type *ktype)
1130c061b57SDavid Howells {
1140c061b57SDavid Howells kenter("%s", ktype->name);
1150c061b57SDavid Howells
1160c061b57SDavid Howells key_gc_dead_keytype = ktype;
1170c061b57SDavid Howells set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
1180c061b57SDavid Howells smp_mb();
1190c061b57SDavid Howells set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
1200c061b57SDavid Howells
1210c061b57SDavid Howells kdebug("schedule");
1223b07e9caSTejun Heo schedule_work(&key_gc_work);
1230c061b57SDavid Howells
1240c061b57SDavid Howells kdebug("sleep");
12574316201SNeilBrown wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
1260c061b57SDavid Howells TASK_UNINTERRUPTIBLE);
1270c061b57SDavid Howells
1280c061b57SDavid Howells key_gc_dead_keytype = NULL;
1290c061b57SDavid Howells kleave("");
1300c061b57SDavid Howells }
1310c061b57SDavid Howells
1325d135440SDavid Howells /*
13365d87fe6SDavid Howells * Garbage collect a list of unreferenced, detached keys
1345d135440SDavid Howells */
key_gc_unused_keys(struct list_head * keys)13565d87fe6SDavid Howells static noinline void key_gc_unused_keys(struct list_head *keys)
1365d135440SDavid Howells {
13765d87fe6SDavid Howells while (!list_empty(keys)) {
13865d87fe6SDavid Howells struct key *key =
13965d87fe6SDavid Howells list_entry(keys->next, struct key, graveyard_link);
140363b02daSDavid Howells short state = key->state;
141363b02daSDavid Howells
14265d87fe6SDavid Howells list_del(&key->graveyard_link);
14365d87fe6SDavid Howells
14465d87fe6SDavid Howells kdebug("- %u", key->serial);
1458bc16deaSDavid Howells key_check(key);
1468bc16deaSDavid Howells
147f7e47677SDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
148f7e47677SDavid Howells remove_watch_list(key->watchers, key->serial);
149f7e47677SDavid Howells key->watchers = NULL;
150f7e47677SDavid Howells #endif
151f7e47677SDavid Howells
152f05819dfSDavid Howells /* Throw away the key data if the key is instantiated */
153363b02daSDavid Howells if (state == KEY_IS_POSITIVE && key->type->destroy)
15494c4554bSDavid Howells key->type->destroy(key);
15594c4554bSDavid Howells
1568bc16deaSDavid Howells security_key_free(key);
1578bc16deaSDavid Howells
1588bc16deaSDavid Howells /* deal with the user's key tracking and quota */
1598bc16deaSDavid Howells if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
1608bc16deaSDavid Howells spin_lock(&key->user->lock);
1618bc16deaSDavid Howells key->user->qnkeys--;
1628bc16deaSDavid Howells key->user->qnbytes -= key->quotalen;
1638bc16deaSDavid Howells spin_unlock(&key->user->lock);
1648bc16deaSDavid Howells }
1658bc16deaSDavid Howells
1668bc16deaSDavid Howells atomic_dec(&key->user->nkeys);
167363b02daSDavid Howells if (state != KEY_IS_UNINSTANTIATED)
1688bc16deaSDavid Howells atomic_dec(&key->user->nikeys);
1698bc16deaSDavid Howells
170a3a87844SSasha Levin key_user_put(key->user);
1713b6e4de0SDavid Howells key_put_tag(key->domain_tag);
1728bc16deaSDavid Howells kfree(key->description);
1738bc16deaSDavid Howells
1740620fddbSEric Biggers memzero_explicit(key, sizeof(*key));
1758bc16deaSDavid Howells kmem_cache_free(key_jar, key);
1760c061b57SDavid Howells }
17765d87fe6SDavid Howells }
1788bc16deaSDavid Howells
1790c061b57SDavid Howells /*
1800c061b57SDavid Howells * Garbage collector for unused keys.
1810c061b57SDavid Howells *
1820c061b57SDavid Howells * This is done in process context so that we don't have to disable interrupts
1830c061b57SDavid Howells * all over the place. key_put() schedules this rather than trying to do the
1840c061b57SDavid Howells * cleanup itself, which means key_put() doesn't have to sleep.
1850c061b57SDavid Howells */
key_garbage_collector(struct work_struct * work)1860c061b57SDavid Howells static void key_garbage_collector(struct work_struct *work)
1870c061b57SDavid Howells {
18865d87fe6SDavid Howells static LIST_HEAD(graveyard);
1890c061b57SDavid Howells static u8 gc_state; /* Internal persistent state */
1900c061b57SDavid Howells #define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
1910c061b57SDavid Howells #define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */
1920c061b57SDavid Howells #define KEY_GC_REAPING_DEAD_1 0x10 /* - We need to mark dead keys */
1930c061b57SDavid Howells #define KEY_GC_REAPING_DEAD_2 0x20 /* - We need to reap dead key links */
1940c061b57SDavid Howells #define KEY_GC_REAPING_DEAD_3 0x40 /* - We need to reap dead keys */
1950c061b57SDavid Howells #define KEY_GC_FOUND_DEAD_KEY 0x80 /* - We found at least one dead key */
1960c061b57SDavid Howells
1970c061b57SDavid Howells struct rb_node *cursor;
1980c061b57SDavid Howells struct key *key;
199*afc360e8SDavid Howells time64_t new_timer, limit, expiry;
2000c061b57SDavid Howells
2010c061b57SDavid Howells kenter("[%lx,%x]", key_gc_flags, gc_state);
2020c061b57SDavid Howells
203074d5898SBaolin Wang limit = ktime_get_real_seconds();
2040c061b57SDavid Howells
2050c061b57SDavid Howells /* Work out what we're going to be doing in this pass */
2060c061b57SDavid Howells gc_state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
2070c061b57SDavid Howells gc_state <<= 1;
2080c061b57SDavid Howells if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
209*afc360e8SDavid Howells gc_state |= KEY_GC_REAPING_LINKS;
2100c061b57SDavid Howells
2110c061b57SDavid Howells if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
2120c061b57SDavid Howells gc_state |= KEY_GC_REAPING_DEAD_1;
2130c061b57SDavid Howells kdebug("new pass %x", gc_state);
2140c061b57SDavid Howells
215074d5898SBaolin Wang new_timer = TIME64_MAX;
2160c061b57SDavid Howells
2170c061b57SDavid Howells /* As only this function is permitted to remove things from the key
2180c061b57SDavid Howells * serial tree, if cursor is non-NULL then it will always point to a
2190c061b57SDavid Howells * valid node in the tree - even if lock got dropped.
2200c061b57SDavid Howells */
2210c061b57SDavid Howells spin_lock(&key_serial_lock);
2220c061b57SDavid Howells cursor = rb_first(&key_serial_tree);
2230c061b57SDavid Howells
2240c061b57SDavid Howells continue_scanning:
2250c061b57SDavid Howells while (cursor) {
2260c061b57SDavid Howells key = rb_entry(cursor, struct key, serial_node);
2270c061b57SDavid Howells cursor = rb_next(cursor);
2280c061b57SDavid Howells
229fff29291SElena Reshetova if (refcount_read(&key->usage) == 0)
2300c061b57SDavid Howells goto found_unreferenced_key;
2310c061b57SDavid Howells
2320c061b57SDavid Howells if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
2330c061b57SDavid Howells if (key->type == key_gc_dead_keytype) {
2340c061b57SDavid Howells gc_state |= KEY_GC_FOUND_DEAD_KEY;
2350c061b57SDavid Howells set_bit(KEY_FLAG_DEAD, &key->flags);
236028db3e2SLinus Torvalds key->perm = 0;
2370c061b57SDavid Howells goto skip_dead_key;
2382b6aa412SMat Martineau } else if (key->type == &key_type_keyring &&
2392b6aa412SMat Martineau key->restrict_link) {
2402b6aa412SMat Martineau goto found_restricted_keyring;
2410c061b57SDavid Howells }
2420c061b57SDavid Howells }
2430c061b57SDavid Howells
244*afc360e8SDavid Howells expiry = key->expiry;
245*afc360e8SDavid Howells if (expiry != TIME64_MAX) {
246*afc360e8SDavid Howells if (!(key->type->flags & KEY_TYPE_INSTANT_REAP))
247*afc360e8SDavid Howells expiry += key_gc_delay;
248*afc360e8SDavid Howells if (expiry > limit && expiry < new_timer) {
249074d5898SBaolin Wang kdebug("will expire %x in %lld",
2500c061b57SDavid Howells key_serial(key), key->expiry - limit);
2510c061b57SDavid Howells new_timer = key->expiry;
2520c061b57SDavid Howells }
2530c061b57SDavid Howells }
2540c061b57SDavid Howells
2550c061b57SDavid Howells if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
2560c061b57SDavid Howells if (key->type == key_gc_dead_keytype)
2570c061b57SDavid Howells gc_state |= KEY_GC_FOUND_DEAD_KEY;
2580c061b57SDavid Howells
2590c061b57SDavid Howells if ((gc_state & KEY_GC_REAPING_LINKS) ||
2600c061b57SDavid Howells unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
2610c061b57SDavid Howells if (key->type == &key_type_keyring)
2620c061b57SDavid Howells goto found_keyring;
2630c061b57SDavid Howells }
2640c061b57SDavid Howells
2650c061b57SDavid Howells if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3))
2660c061b57SDavid Howells if (key->type == key_gc_dead_keytype)
2670c061b57SDavid Howells goto destroy_dead_key;
2680c061b57SDavid Howells
2690c061b57SDavid Howells skip_dead_key:
2700c061b57SDavid Howells if (spin_is_contended(&key_serial_lock) || need_resched())
2710c061b57SDavid Howells goto contended;
2720c061b57SDavid Howells }
2730c061b57SDavid Howells
2740c061b57SDavid Howells contended:
2750c061b57SDavid Howells spin_unlock(&key_serial_lock);
2760c061b57SDavid Howells
2770c061b57SDavid Howells maybe_resched:
2780c061b57SDavid Howells if (cursor) {
2790c061b57SDavid Howells cond_resched();
2800c061b57SDavid Howells spin_lock(&key_serial_lock);
2810c061b57SDavid Howells goto continue_scanning;
2820c061b57SDavid Howells }
2830c061b57SDavid Howells
2840c061b57SDavid Howells /* We've completed the pass. Set the timer if we need to and queue a
2850c061b57SDavid Howells * new cycle if necessary. We keep executing cycles until we find one
2860c061b57SDavid Howells * where we didn't reap any keys.
2870c061b57SDavid Howells */
2880c061b57SDavid Howells kdebug("pass complete");
2890c061b57SDavid Howells
290*afc360e8SDavid Howells if (new_timer != TIME64_MAX) {
2910c061b57SDavid Howells new_timer += key_gc_delay;
2920c061b57SDavid Howells key_schedule_gc(new_timer);
2930c061b57SDavid Howells }
2940c061b57SDavid Howells
29565d87fe6SDavid Howells if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
29665d87fe6SDavid Howells !list_empty(&graveyard)) {
29765d87fe6SDavid Howells /* Make sure that all pending keyring payload destructions are
29865d87fe6SDavid Howells * fulfilled and that people aren't now looking at dead or
29965d87fe6SDavid Howells * dying keys that they don't have a reference upon or a link
30065d87fe6SDavid Howells * to.
3010c061b57SDavid Howells */
30265d87fe6SDavid Howells kdebug("gc sync");
3030c061b57SDavid Howells synchronize_rcu();
3040c061b57SDavid Howells }
3050c061b57SDavid Howells
30665d87fe6SDavid Howells if (!list_empty(&graveyard)) {
30765d87fe6SDavid Howells kdebug("gc keys");
30865d87fe6SDavid Howells key_gc_unused_keys(&graveyard);
30965d87fe6SDavid Howells }
31065d87fe6SDavid Howells
3110c061b57SDavid Howells if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
3120c061b57SDavid Howells KEY_GC_REAPING_DEAD_2))) {
3130c061b57SDavid Howells if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
3140c061b57SDavid Howells /* No remaining dead keys: short circuit the remaining
3150c061b57SDavid Howells * keytype reap cycles.
3160c061b57SDavid Howells */
3170c061b57SDavid Howells kdebug("dead short");
3180c061b57SDavid Howells gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
3190c061b57SDavid Howells gc_state |= KEY_GC_REAPING_DEAD_3;
3200c061b57SDavid Howells } else {
3210c061b57SDavid Howells gc_state |= KEY_GC_REAP_AGAIN;
3220c061b57SDavid Howells }
3230c061b57SDavid Howells }
3240c061b57SDavid Howells
3250c061b57SDavid Howells if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
3260c061b57SDavid Howells kdebug("dead wake");
3270c061b57SDavid Howells smp_mb();
3280c061b57SDavid Howells clear_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
3290c061b57SDavid Howells wake_up_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE);
3300c061b57SDavid Howells }
3310c061b57SDavid Howells
3320c061b57SDavid Howells if (gc_state & KEY_GC_REAP_AGAIN)
3333b07e9caSTejun Heo schedule_work(&key_gc_work);
3340c061b57SDavid Howells kleave(" [end %x]", gc_state);
3350c061b57SDavid Howells return;
3360c061b57SDavid Howells
3370c061b57SDavid Howells /* We found an unreferenced key - once we've removed it from the tree,
3380c061b57SDavid Howells * we can safely drop the lock.
3390c061b57SDavid Howells */
3400c061b57SDavid Howells found_unreferenced_key:
3410c061b57SDavid Howells kdebug("unrefd key %d", key->serial);
3420c061b57SDavid Howells rb_erase(&key->serial_node, &key_serial_tree);
3430c061b57SDavid Howells spin_unlock(&key_serial_lock);
3440c061b57SDavid Howells
34565d87fe6SDavid Howells list_add_tail(&key->graveyard_link, &graveyard);
3460c061b57SDavid Howells gc_state |= KEY_GC_REAP_AGAIN;
3470c061b57SDavid Howells goto maybe_resched;
3480c061b57SDavid Howells
3492b6aa412SMat Martineau /* We found a restricted keyring and need to update the restriction if
3502b6aa412SMat Martineau * it is associated with the dead key type.
3512b6aa412SMat Martineau */
3522b6aa412SMat Martineau found_restricted_keyring:
3532b6aa412SMat Martineau spin_unlock(&key_serial_lock);
3542b6aa412SMat Martineau keyring_restriction_gc(key, key_gc_dead_keytype);
3552b6aa412SMat Martineau goto maybe_resched;
3562b6aa412SMat Martineau
3570c061b57SDavid Howells /* We found a keyring and we need to check the payload for links to
3580c061b57SDavid Howells * dead or expired keys. We don't flag another reap immediately as we
3590c061b57SDavid Howells * have to wait for the old payload to be destroyed by RCU before we
3600c061b57SDavid Howells * can reap the keys to which it refers.
3610c061b57SDavid Howells */
3620c061b57SDavid Howells found_keyring:
3630c061b57SDavid Howells spin_unlock(&key_serial_lock);
36462fe3182SDavid Howells keyring_gc(key, limit);
3650c061b57SDavid Howells goto maybe_resched;
3660c061b57SDavid Howells
3670c061b57SDavid Howells /* We found a dead key that is still referenced. Reset its type and
3680c061b57SDavid Howells * destroy its payload with its semaphore held.
3690c061b57SDavid Howells */
3700c061b57SDavid Howells destroy_dead_key:
3710c061b57SDavid Howells spin_unlock(&key_serial_lock);
3720c061b57SDavid Howells kdebug("destroy key %d", key->serial);
3730c061b57SDavid Howells down_write(&key->sem);
3740c061b57SDavid Howells key->type = &key_type_dead;
3750c061b57SDavid Howells if (key_gc_dead_keytype->destroy)
3760c061b57SDavid Howells key_gc_dead_keytype->destroy(key);
3770c061b57SDavid Howells memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
3780c061b57SDavid Howells up_write(&key->sem);
3790c061b57SDavid Howells goto maybe_resched;
3808bc16deaSDavid Howells }
381