KEYS: Do LRU discard in full keyrings
[deliverable/linux.git] / security / keys / gc.c
CommitLineData
5d135440
DH
1/* Key garbage collector
2 *
0c061b57 3 * Copyright (C) 2009-2011 Red Hat, Inc. All Rights Reserved.
5d135440
DH
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/module.h>
8bc16dea
DH
13#include <linux/slab.h>
14#include <linux/security.h>
5d135440
DH
15#include <keys/keyring-type.h>
16#include "internal.h"
17
18/*
19 * Delay between key revocation/expiry in seconds
20 */
21unsigned key_gc_delay = 5 * 60;
22
23/*
8bc16dea
DH
24 * Reaper for unused keys.
25 */
0c061b57
DH
26static void key_garbage_collector(struct work_struct *work);
27DECLARE_WORK(key_gc_work, key_garbage_collector);
8bc16dea
DH
28
29/*
30 * Reaper for links from keyrings to dead keys.
5d135440
DH
31 */
32static void key_gc_timer_func(unsigned long);
5d135440 33static DEFINE_TIMER(key_gc_timer, key_gc_timer_func, 0, 0);
0c061b57 34
5d135440 35static time_t key_gc_next_run = LONG_MAX;
0c061b57
DH
36static struct key_type *key_gc_dead_keytype;
37
38static unsigned long key_gc_flags;
39#define KEY_GC_KEY_EXPIRED 0 /* A key expired and needs unlinking */
40#define KEY_GC_REAP_KEYTYPE 1 /* A keytype is being unregistered */
41#define KEY_GC_REAPING_KEYTYPE 2 /* Cleared when keytype reaped */
42
43
44/*
45 * Any key whose type gets unregistered will be re-typed to this if it can't be
46 * immediately unlinked.
47 */
48struct key_type key_type_dead = {
49 .name = "dead",
50};
5d135440
DH
51
52/*
973c9f4f
DH
53 * Schedule a garbage collection run.
54 * - time precision isn't particularly important
5d135440
DH
55 */
56void key_schedule_gc(time_t gc_at)
57{
58 unsigned long expires;
59 time_t now = current_kernel_time().tv_sec;
60
61 kenter("%ld", gc_at - now);
62
0c061b57
DH
63 if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
64 kdebug("IMMEDIATE");
d199798b 65 queue_work(system_nrt_wq, &key_gc_work);
5d135440 66 } else if (gc_at < key_gc_next_run) {
0c061b57
DH
67 kdebug("DEFERRED");
68 key_gc_next_run = gc_at;
5d135440
DH
69 expires = jiffies + (gc_at - now) * HZ;
70 mod_timer(&key_gc_timer, expires);
71 }
72}
73
74/*
0c061b57
DH
75 * Some key's cleanup time was met after it expired, so we need to get the
76 * reaper to go through a cycle finding expired keys.
5d135440
DH
77 */
78static void key_gc_timer_func(unsigned long data)
79{
80 kenter("");
81 key_gc_next_run = LONG_MAX;
0c061b57 82 set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
d199798b 83 queue_work(system_nrt_wq, &key_gc_work);
5d135440
DH
84}
85
0c061b57
DH
86/*
87 * wait_on_bit() sleep function for uninterruptible waiting
88 */
89static int key_gc_wait_bit(void *flags)
90{
91 schedule();
92 return 0;
93}
94
95/*
96 * Reap keys of dead type.
97 *
98 * We use three flags to make sure we see three complete cycles of the garbage
99 * collector: the first to mark keys of that type as being dead, the second to
100 * collect dead links and the third to clean up the dead keys. We have to be
101 * careful as there may already be a cycle in progress.
102 *
103 * The caller must be holding key_types_sem.
104 */
105void key_gc_keytype(struct key_type *ktype)
106{
107 kenter("%s", ktype->name);
108
109 key_gc_dead_keytype = ktype;
110 set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
111 smp_mb();
112 set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
113
114 kdebug("schedule");
115 queue_work(system_nrt_wq, &key_gc_work);
116
117 kdebug("sleep");
118 wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE, key_gc_wait_bit,
119 TASK_UNINTERRUPTIBLE);
120
121 key_gc_dead_keytype = NULL;
122 kleave("");
123}
124
5d135440 125/*
973c9f4f
DH
126 * Garbage collect pointers from a keyring.
127 *
0c061b57
DH
128 * Not called with any locks held. The keyring's key struct will not be
129 * deallocated under us as only our caller may deallocate it.
5d135440 130 */
0c061b57 131static void key_gc_keyring(struct key *keyring, time_t limit)
5d135440
DH
132{
133 struct keyring_list *klist;
134 struct key *key;
135 int loop;
136
137 kenter("%x", key_serial(keyring));
138
139 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
140 goto dont_gc;
141
142 /* scan the keyring looking for dead keys */
cf8304e8
DH
143 rcu_read_lock();
144 klist = rcu_dereference(keyring->payload.subscriptions);
5d135440 145 if (!klist)
cf8304e8 146 goto unlock_dont_gc;
5d135440 147
efde8b6e
DH
148 loop = klist->nkeys;
149 smp_rmb();
150 for (loop--; loop >= 0; loop--) {
233e4735 151 key = rcu_dereference(klist->keys[loop]);
5d135440
DH
152 if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
153 (key->expiry > 0 && key->expiry <= limit))
154 goto do_gc;
155 }
156
cf8304e8
DH
157unlock_dont_gc:
158 rcu_read_unlock();
5d135440 159dont_gc:
0c061b57
DH
160 kleave(" [no gc]");
161 return;
5d135440
DH
162
163do_gc:
cf8304e8 164 rcu_read_unlock();
0c061b57 165
5d135440 166 keyring_gc(keyring, limit);
0c061b57 167 kleave(" [gc]");
5d135440
DH
168}
169
170/*
65d87fe6 171 * Garbage collect a list of unreferenced, detached keys
5d135440 172 */
65d87fe6 173static noinline void key_gc_unused_keys(struct list_head *keys)
5d135440 174{
65d87fe6
DH
175 while (!list_empty(keys)) {
176 struct key *key =
177 list_entry(keys->next, struct key, graveyard_link);
178 list_del(&key->graveyard_link);
179
180 kdebug("- %u", key->serial);
181 key_check(key);
182
183 security_key_free(key);
184
185 /* deal with the user's key tracking and quota */
186 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
187 spin_lock(&key->user->lock);
188 key->user->qnkeys--;
189 key->user->qnbytes -= key->quotalen;
190 spin_unlock(&key->user->lock);
191 }
5d135440 192
65d87fe6
DH
193 atomic_dec(&key->user->nkeys);
194 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
195 atomic_dec(&key->user->nikeys);
5d135440 196
65d87fe6 197 key_user_put(key->user);
5d135440 198
65d87fe6
DH
199 /* now throw away the key memory */
200 if (key->type->destroy)
201 key->type->destroy(key);
5d135440 202
65d87fe6 203 kfree(key->description);
5d135440 204
0c061b57 205#ifdef KEY_DEBUGGING
65d87fe6 206 key->magic = KEY_DEBUG_MAGIC_X;
0c061b57 207#endif
65d87fe6
DH
208 kmem_cache_free(key_jar, key);
209 }
5d135440 210}
8bc16dea
DH
211
212/*
213 * Garbage collector for unused keys.
214 *
215 * This is done in process context so that we don't have to disable interrupts
216 * all over the place. key_put() schedules this rather than trying to do the
217 * cleanup itself, which means key_put() doesn't have to sleep.
218 */
0c061b57 219static void key_garbage_collector(struct work_struct *work)
8bc16dea 220{
65d87fe6 221 static LIST_HEAD(graveyard);
0c061b57
DH
222 static u8 gc_state; /* Internal persistent state */
223#define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
224#define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */
225#define KEY_GC_SET_TIMER 0x04 /* - We need to restart the timer */
226#define KEY_GC_REAPING_DEAD_1 0x10 /* - We need to mark dead keys */
227#define KEY_GC_REAPING_DEAD_2 0x20 /* - We need to reap dead key links */
228#define KEY_GC_REAPING_DEAD_3 0x40 /* - We need to reap dead keys */
229#define KEY_GC_FOUND_DEAD_KEY 0x80 /* - We found at least one dead key */
230
231 struct rb_node *cursor;
8bc16dea 232 struct key *key;
0c061b57
DH
233 time_t new_timer, limit;
234
235 kenter("[%lx,%x]", key_gc_flags, gc_state);
236
237 limit = current_kernel_time().tv_sec;
238 if (limit > key_gc_delay)
239 limit -= key_gc_delay;
240 else
241 limit = key_gc_delay;
242
243 /* Work out what we're going to be doing in this pass */
244 gc_state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
245 gc_state <<= 1;
246 if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
247 gc_state |= KEY_GC_REAPING_LINKS | KEY_GC_SET_TIMER;
248
249 if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
250 gc_state |= KEY_GC_REAPING_DEAD_1;
251 kdebug("new pass %x", gc_state);
252
253 new_timer = LONG_MAX;
8bc16dea 254
0c061b57
DH
255 /* As only this function is permitted to remove things from the key
256 * serial tree, if cursor is non-NULL then it will always point to a
257 * valid node in the tree - even if lock got dropped.
258 */
8bc16dea 259 spin_lock(&key_serial_lock);
0c061b57 260 cursor = rb_first(&key_serial_tree);
8bc16dea 261
0c061b57
DH
262continue_scanning:
263 while (cursor) {
264 key = rb_entry(cursor, struct key, serial_node);
265 cursor = rb_next(cursor);
8bc16dea
DH
266
267 if (atomic_read(&key->usage) == 0)
0c061b57
DH
268 goto found_unreferenced_key;
269
270 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
271 if (key->type == key_gc_dead_keytype) {
272 gc_state |= KEY_GC_FOUND_DEAD_KEY;
273 set_bit(KEY_FLAG_DEAD, &key->flags);
274 key->perm = 0;
275 goto skip_dead_key;
276 }
277 }
278
279 if (gc_state & KEY_GC_SET_TIMER) {
280 if (key->expiry > limit && key->expiry < new_timer) {
281 kdebug("will expire %x in %ld",
282 key_serial(key), key->expiry - limit);
283 new_timer = key->expiry;
284 }
285 }
286
287 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
288 if (key->type == key_gc_dead_keytype)
289 gc_state |= KEY_GC_FOUND_DEAD_KEY;
290
291 if ((gc_state & KEY_GC_REAPING_LINKS) ||
292 unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
293 if (key->type == &key_type_keyring)
294 goto found_keyring;
295 }
296
297 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3))
298 if (key->type == key_gc_dead_keytype)
299 goto destroy_dead_key;
300
301 skip_dead_key:
302 if (spin_is_contended(&key_serial_lock) || need_resched())
303 goto contended;
8bc16dea
DH
304 }
305
0c061b57 306contended:
8bc16dea 307 spin_unlock(&key_serial_lock);
8bc16dea 308
0c061b57
DH
309maybe_resched:
310 if (cursor) {
311 cond_resched();
312 spin_lock(&key_serial_lock);
313 goto continue_scanning;
314 }
8bc16dea 315
0c061b57
DH
316 /* We've completed the pass. Set the timer if we need to and queue a
317 * new cycle if necessary. We keep executing cycles until we find one
318 * where we didn't reap any keys.
319 */
320 kdebug("pass complete");
8bc16dea 321
0c061b57
DH
322 if (gc_state & KEY_GC_SET_TIMER && new_timer != (time_t)LONG_MAX) {
323 new_timer += key_gc_delay;
324 key_schedule_gc(new_timer);
325 }
8bc16dea 326
65d87fe6
DH
327 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
328 !list_empty(&graveyard)) {
329 /* Make sure that all pending keyring payload destructions are
330 * fulfilled and that people aren't now looking at dead or
331 * dying keys that they don't have a reference upon or a link
332 * to.
0c061b57 333 */
65d87fe6 334 kdebug("gc sync");
0c061b57 335 synchronize_rcu();
8bc16dea
DH
336 }
337
65d87fe6
DH
338 if (!list_empty(&graveyard)) {
339 kdebug("gc keys");
340 key_gc_unused_keys(&graveyard);
341 }
342
0c061b57
DH
343 if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
344 KEY_GC_REAPING_DEAD_2))) {
345 if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
346 /* No remaining dead keys: short circuit the remaining
347 * keytype reap cycles.
348 */
349 kdebug("dead short");
350 gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
351 gc_state |= KEY_GC_REAPING_DEAD_3;
352 } else {
353 gc_state |= KEY_GC_REAP_AGAIN;
354 }
355 }
8bc16dea 356
0c061b57
DH
357 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
358 kdebug("dead wake");
359 smp_mb();
360 clear_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
361 wake_up_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE);
362 }
8bc16dea 363
0c061b57
DH
364 if (gc_state & KEY_GC_REAP_AGAIN)
365 queue_work(system_nrt_wq, &key_gc_work);
366 kleave(" [end %x]", gc_state);
367 return;
8bc16dea 368
0c061b57
DH
369 /* We found an unreferenced key - once we've removed it from the tree,
370 * we can safely drop the lock.
371 */
372found_unreferenced_key:
373 kdebug("unrefd key %d", key->serial);
374 rb_erase(&key->serial_node, &key_serial_tree);
375 spin_unlock(&key_serial_lock);
8bc16dea 376
65d87fe6 377 list_add_tail(&key->graveyard_link, &graveyard);
0c061b57
DH
378 gc_state |= KEY_GC_REAP_AGAIN;
379 goto maybe_resched;
8bc16dea 380
0c061b57
DH
381 /* We found a keyring and we need to check the payload for links to
382 * dead or expired keys. We don't flag another reap immediately as we
383 * have to wait for the old payload to be destroyed by RCU before we
384 * can reap the keys to which it refers.
385 */
386found_keyring:
387 spin_unlock(&key_serial_lock);
388 kdebug("scan keyring %d", key->serial);
389 key_gc_keyring(key, limit);
390 goto maybe_resched;
391
392 /* We found a dead key that is still referenced. Reset its type and
393 * destroy its payload with its semaphore held.
394 */
395destroy_dead_key:
396 spin_unlock(&key_serial_lock);
397 kdebug("destroy key %d", key->serial);
398 down_write(&key->sem);
399 key->type = &key_type_dead;
400 if (key_gc_dead_keytype->destroy)
401 key_gc_dead_keytype->destroy(key);
402 memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
403 up_write(&key->sem);
404 goto maybe_resched;
8bc16dea 405}
This page took 0.239739 seconds and 5 git commands to generate.