gc.c 10 KB

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