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 bool key_gc_again;
30 static unsigned long key_gc_executing;
31 static time_t key_gc_next_run = LONG_MAX;
32 static time_t key_gc_new_timer;
35 * Schedule a garbage collection run
36 * - precision isn't particularly important
38 void key_schedule_gc(time_t gc_at)
40 unsigned long expires;
41 time_t now = current_kernel_time().tv_sec;
43 kenter("%ld", gc_at - now);
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)
68 __releases(key_serial_lock)
70 struct keyring_list *klist;
74 kenter("%x", key_serial(keyring));
76 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
79 /* scan the keyring looking for dead keys */
80 klist = rcu_dereference_check(keyring->payload.subscriptions,
81 lockdep_is_held(&key_serial_lock));
85 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
86 key = klist->keys[loop];
87 if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
88 (key->expiry > 0 && key->expiry <= limit))
97 key_gc_cursor = keyring->serial;
99 spin_unlock(&key_serial_lock);
100 keyring_gc(keyring, limit);
107 * Garbage collector for keys
108 * - this involves scanning the keyrings for dead, expired and revoked keys
109 * that have overstayed their welcome
111 static void key_garbage_collector(struct work_struct *work)
115 struct key *key, *xkey;
116 time_t new_timer = LONG_MAX, limit, now;
118 now = current_kernel_time().tv_sec;
119 kenter("[%x,%ld]", key_gc_cursor, key_gc_new_timer - now);
121 if (test_and_set_bit(0, &key_gc_executing)) {
122 key_schedule_gc(current_kernel_time().tv_sec + 1);
123 kleave(" [busy; deferring]");
128 if (limit > key_gc_delay)
129 limit -= key_gc_delay;
131 limit = key_gc_delay;
133 spin_lock(&key_serial_lock);
135 if (unlikely(RB_EMPTY_ROOT(&key_serial_tree))) {
136 spin_unlock(&key_serial_lock);
137 clear_bit(0, &key_gc_executing);
141 cursor = key_gc_cursor;
145 new_timer = key_gc_new_timer;
147 key_gc_again = false;
149 /* find the first key above the cursor */
151 rb = key_serial_tree.rb_node;
153 xkey = rb_entry(rb, struct key, serial_node);
154 if (cursor < xkey->serial) {
157 } else if (cursor > xkey->serial) {
162 goto reached_the_end;
163 key = rb_entry(rb, struct key, serial_node);
169 goto reached_the_end;
171 /* trawl through the keys looking for keyrings */
173 if (key->expiry > limit && key->expiry < new_timer) {
174 kdebug("will expire %x in %ld",
175 key_serial(key), key->expiry - limit);
176 new_timer = key->expiry;
179 if (key->type == &key_type_keyring &&
180 key_gc_keyring(key, limit))
181 /* the gc had to release our lock so that the keyring
182 * could be modified, so we have to get it again */
183 goto gc_released_our_lock;
185 rb = rb_next(&key->serial_node);
187 goto reached_the_end;
188 key = rb_entry(rb, struct key, serial_node);
191 gc_released_our_lock:
192 kdebug("gc_released_our_lock");
193 key_gc_new_timer = new_timer;
195 clear_bit(0, &key_gc_executing);
196 schedule_work(&key_gc_work);
197 kleave(" [continue]");
200 /* when we reach the end of the run, we set the timer for the next one */
202 kdebug("reached_the_end");
203 spin_unlock(&key_serial_lock);
204 key_gc_new_timer = new_timer;
206 clear_bit(0, &key_gc_executing);
209 /* there may have been a key that expired whilst we were
210 * scanning, so if we discarded any links we should do another
213 key_schedule_gc(new_timer);
214 } else if (new_timer < LONG_MAX) {
215 new_timer += key_gc_delay;
216 key_schedule_gc(new_timer);