1 /* Key garbage collector
3 * Copyright (C) 2009 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <keys/keyring-type.h>
17 * Delay between key revocation/expiry in seconds
19 unsigned key_gc_delay = 5 * 60;
24 static void key_gc_timer_func(unsigned long);
25 static void key_garbage_collector(struct work_struct *);
26 static DEFINE_TIMER(key_gc_timer, key_gc_timer_func, 0, 0);
27 static DECLARE_WORK(key_gc_work, key_garbage_collector);
28 static key_serial_t key_gc_cursor; /* the last key the gc considered */
29 static unsigned long key_gc_executing;
30 static time_t key_gc_next_run = LONG_MAX;
33 * Schedule a garbage collection run
34 * - precision isn't particularly important
36 void key_schedule_gc(time_t gc_at)
38 unsigned long expires;
39 time_t now = current_kernel_time().tv_sec;
41 kenter("%ld", gc_at - now);
43 gc_at += key_gc_delay;
46 schedule_work(&key_gc_work);
47 } else if (gc_at < key_gc_next_run) {
48 expires = jiffies + (gc_at - now) * HZ;
49 mod_timer(&key_gc_timer, expires);
54 * The garbage collector timer kicked off
56 static void key_gc_timer_func(unsigned long data)
59 key_gc_next_run = LONG_MAX;
60 schedule_work(&key_gc_work);
64 * Garbage collect pointers from a keyring
65 * - return true if we altered the keyring
67 static bool key_gc_keyring(struct key *keyring, time_t limit)
69 struct keyring_list *klist;
73 kenter("%x", key_serial(keyring));
75 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
78 /* scan the keyring looking for dead keys */
79 klist = rcu_dereference(keyring->payload.subscriptions);
83 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
84 key = klist->keys[loop];
85 if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
86 (key->expiry > 0 && key->expiry <= limit))
95 key_gc_cursor = keyring->serial;
97 spin_unlock(&key_serial_lock);
98 keyring_gc(keyring, limit);
105 * Garbage collector for keys
106 * - this involves scanning the keyrings for dead, expired and revoked keys
107 * that have overstayed their welcome
109 static void key_garbage_collector(struct work_struct *work)
113 struct key *key, *xkey;
114 time_t new_timer = LONG_MAX, limit;
118 if (test_and_set_bit(0, &key_gc_executing)) {
119 key_schedule_gc(current_kernel_time().tv_sec);
123 limit = current_kernel_time().tv_sec;
124 if (limit > key_gc_delay)
125 limit -= key_gc_delay;
127 limit = key_gc_delay;
129 spin_lock(&key_serial_lock);
131 if (RB_EMPTY_ROOT(&key_serial_tree))
132 goto reached_the_end;
134 cursor = key_gc_cursor;
138 /* find the first key above the cursor */
140 rb = key_serial_tree.rb_node;
142 xkey = rb_entry(rb, struct key, serial_node);
143 if (cursor < xkey->serial) {
146 } else if (cursor > xkey->serial) {
151 goto reached_the_end;
152 key = rb_entry(rb, struct key, serial_node);
158 goto reached_the_end;
160 /* trawl through the keys looking for keyrings */
162 if (key->expiry > 0 && key->expiry < new_timer)
163 new_timer = key->expiry;
165 if (key->type == &key_type_keyring &&
166 key_gc_keyring(key, limit)) {
167 /* the gc ate our lock */
168 schedule_work(&key_gc_work);
172 rb = rb_next(&key->serial_node);
177 key = rb_entry(rb, struct key, serial_node);
181 spin_unlock(&key_serial_lock);
183 clear_bit(0, &key_gc_executing);
184 if (new_timer < LONG_MAX)
185 key_schedule_gc(new_timer);