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)
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(keyring->payload.subscriptions);
84 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
85 key = klist->keys[loop];
86 if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
87 (key->expiry > 0 && key->expiry <= limit))
96 key_gc_cursor = keyring->serial;
98 spin_unlock(&key_serial_lock);
99 keyring_gc(keyring, limit);
106 * Garbage collector for keys
107 * - this involves scanning the keyrings for dead, expired and revoked keys
108 * that have overstayed their welcome
110 static void key_garbage_collector(struct work_struct *work)
114 struct key *key, *xkey;
115 time_t new_timer = LONG_MAX, limit;
119 if (test_and_set_bit(0, &key_gc_executing)) {
120 key_schedule_gc(current_kernel_time().tv_sec);
124 limit = current_kernel_time().tv_sec;
125 if (limit > key_gc_delay)
126 limit -= key_gc_delay;
128 limit = key_gc_delay;
130 spin_lock(&key_serial_lock);
132 if (RB_EMPTY_ROOT(&key_serial_tree))
133 goto reached_the_end;
135 cursor = key_gc_cursor;
139 /* find the first key above the cursor */
141 rb = key_serial_tree.rb_node;
143 xkey = rb_entry(rb, struct key, serial_node);
144 if (cursor < xkey->serial) {
147 } else if (cursor > xkey->serial) {
152 goto reached_the_end;
153 key = rb_entry(rb, struct key, serial_node);
159 goto reached_the_end;
161 /* trawl through the keys looking for keyrings */
163 if (key->expiry > 0 && key->expiry < new_timer)
164 new_timer = key->expiry;
166 if (key->type == &key_type_keyring &&
167 key_gc_keyring(key, limit)) {
168 /* the gc ate our lock */
169 schedule_work(&key_gc_work);
173 rb = rb_next(&key->serial_node);
178 key = rb_entry(rb, struct key, serial_node);
182 spin_unlock(&key_serial_lock);
184 clear_bit(0, &key_gc_executing);
185 if (new_timer < LONG_MAX)
186 key_schedule_gc(new_timer);