gc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. schedule_work(&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. * Schedule a dead links collection run.
  64. */
  65. void key_schedule_gc_links(void)
  66. {
  67. set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
  68. schedule_work(&key_gc_work);
  69. }
  70. /*
  71. * Some key's cleanup time was met after it expired, so we need to get the
  72. * reaper to go through a cycle finding expired keys.
  73. */
  74. static void key_gc_timer_func(unsigned long data)
  75. {
  76. kenter("");
  77. key_gc_next_run = LONG_MAX;
  78. key_schedule_gc_links();
  79. }
  80. /*
  81. * Reap keys of dead type.
  82. *
  83. * We use three flags to make sure we see three complete cycles of the garbage
  84. * collector: the first to mark keys of that type as being dead, the second to
  85. * collect dead links and the third to clean up the dead keys. We have to be
  86. * careful as there may already be a cycle in progress.
  87. *
  88. * The caller must be holding key_types_sem.
  89. */
  90. void key_gc_keytype(struct key_type *ktype)
  91. {
  92. kenter("%s", ktype->name);
  93. key_gc_dead_keytype = ktype;
  94. set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
  95. smp_mb();
  96. set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
  97. kdebug("schedule");
  98. schedule_work(&key_gc_work);
  99. kdebug("sleep");
  100. wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
  101. TASK_UNINTERRUPTIBLE);
  102. key_gc_dead_keytype = NULL;
  103. kleave("");
  104. }
  105. /*
  106. * Garbage collect a list of unreferenced, detached keys
  107. */
  108. static noinline void key_gc_unused_keys(struct list_head *keys)
  109. {
  110. while (!list_empty(keys)) {
  111. struct key *key =
  112. list_entry(keys->next, struct key, graveyard_link);
  113. list_del(&key->graveyard_link);
  114. kdebug("- %u", key->serial);
  115. key_check(key);
  116. /* Throw away the key data if the key is instantiated */
  117. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags) &&
  118. !test_bit(KEY_FLAG_NEGATIVE, &key->flags) &&
  119. key->type->destroy)
  120. key->type->destroy(key);
  121. security_key_free(key);
  122. /* deal with the user's key tracking and quota */
  123. if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  124. spin_lock(&key->user->lock);
  125. key->user->qnkeys--;
  126. key->user->qnbytes -= key->quotalen;
  127. spin_unlock(&key->user->lock);
  128. }
  129. atomic_dec(&key->user->nkeys);
  130. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  131. atomic_dec(&key->user->nikeys);
  132. key_user_put(key->user);
  133. kfree(key->description);
  134. #ifdef KEY_DEBUGGING
  135. key->magic = KEY_DEBUG_MAGIC_X;
  136. #endif
  137. kmem_cache_free(key_jar, key);
  138. }
  139. }
  140. /*
  141. * Garbage collector for unused keys.
  142. *
  143. * This is done in process context so that we don't have to disable interrupts
  144. * all over the place. key_put() schedules this rather than trying to do the
  145. * cleanup itself, which means key_put() doesn't have to sleep.
  146. */
  147. static void key_garbage_collector(struct work_struct *work)
  148. {
  149. static LIST_HEAD(graveyard);
  150. static u8 gc_state; /* Internal persistent state */
  151. #define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
  152. #define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */
  153. #define KEY_GC_SET_TIMER 0x04 /* - We need to restart the timer */
  154. #define KEY_GC_REAPING_DEAD_1 0x10 /* - We need to mark dead keys */
  155. #define KEY_GC_REAPING_DEAD_2 0x20 /* - We need to reap dead key links */
  156. #define KEY_GC_REAPING_DEAD_3 0x40 /* - We need to reap dead keys */
  157. #define KEY_GC_FOUND_DEAD_KEY 0x80 /* - We found at least one dead key */
  158. struct rb_node *cursor;
  159. struct key *key;
  160. time_t new_timer, limit;
  161. kenter("[%lx,%x]", key_gc_flags, gc_state);
  162. limit = current_kernel_time().tv_sec;
  163. if (limit > key_gc_delay)
  164. limit -= key_gc_delay;
  165. else
  166. limit = key_gc_delay;
  167. /* Work out what we're going to be doing in this pass */
  168. gc_state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
  169. gc_state <<= 1;
  170. if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
  171. gc_state |= KEY_GC_REAPING_LINKS | KEY_GC_SET_TIMER;
  172. if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
  173. gc_state |= KEY_GC_REAPING_DEAD_1;
  174. kdebug("new pass %x", gc_state);
  175. new_timer = LONG_MAX;
  176. /* As only this function is permitted to remove things from the key
  177. * serial tree, if cursor is non-NULL then it will always point to a
  178. * valid node in the tree - even if lock got dropped.
  179. */
  180. spin_lock(&key_serial_lock);
  181. cursor = rb_first(&key_serial_tree);
  182. continue_scanning:
  183. while (cursor) {
  184. key = rb_entry(cursor, struct key, serial_node);
  185. cursor = rb_next(cursor);
  186. if (refcount_read(&key->usage) == 0)
  187. goto found_unreferenced_key;
  188. if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
  189. if (key->type == key_gc_dead_keytype) {
  190. gc_state |= KEY_GC_FOUND_DEAD_KEY;
  191. set_bit(KEY_FLAG_DEAD, &key->flags);
  192. key->perm = 0;
  193. goto skip_dead_key;
  194. } else if (key->type == &key_type_keyring &&
  195. key->restrict_link) {
  196. goto found_restricted_keyring;
  197. }
  198. }
  199. if (gc_state & KEY_GC_SET_TIMER) {
  200. if (key->expiry > limit && key->expiry < new_timer) {
  201. kdebug("will expire %x in %ld",
  202. key_serial(key), key->expiry - limit);
  203. new_timer = key->expiry;
  204. }
  205. }
  206. if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
  207. if (key->type == key_gc_dead_keytype)
  208. gc_state |= KEY_GC_FOUND_DEAD_KEY;
  209. if ((gc_state & KEY_GC_REAPING_LINKS) ||
  210. unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
  211. if (key->type == &key_type_keyring)
  212. goto found_keyring;
  213. }
  214. if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3))
  215. if (key->type == key_gc_dead_keytype)
  216. goto destroy_dead_key;
  217. skip_dead_key:
  218. if (spin_is_contended(&key_serial_lock) || need_resched())
  219. goto contended;
  220. }
  221. contended:
  222. spin_unlock(&key_serial_lock);
  223. maybe_resched:
  224. if (cursor) {
  225. cond_resched();
  226. spin_lock(&key_serial_lock);
  227. goto continue_scanning;
  228. }
  229. /* We've completed the pass. Set the timer if we need to and queue a
  230. * new cycle if necessary. We keep executing cycles until we find one
  231. * where we didn't reap any keys.
  232. */
  233. kdebug("pass complete");
  234. if (gc_state & KEY_GC_SET_TIMER && new_timer != (time_t)LONG_MAX) {
  235. new_timer += key_gc_delay;
  236. key_schedule_gc(new_timer);
  237. }
  238. if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
  239. !list_empty(&graveyard)) {
  240. /* Make sure that all pending keyring payload destructions are
  241. * fulfilled and that people aren't now looking at dead or
  242. * dying keys that they don't have a reference upon or a link
  243. * to.
  244. */
  245. kdebug("gc sync");
  246. synchronize_rcu();
  247. }
  248. if (!list_empty(&graveyard)) {
  249. kdebug("gc keys");
  250. key_gc_unused_keys(&graveyard);
  251. }
  252. if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
  253. KEY_GC_REAPING_DEAD_2))) {
  254. if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
  255. /* No remaining dead keys: short circuit the remaining
  256. * keytype reap cycles.
  257. */
  258. kdebug("dead short");
  259. gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
  260. gc_state |= KEY_GC_REAPING_DEAD_3;
  261. } else {
  262. gc_state |= KEY_GC_REAP_AGAIN;
  263. }
  264. }
  265. if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
  266. kdebug("dead wake");
  267. smp_mb();
  268. clear_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
  269. wake_up_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE);
  270. }
  271. if (gc_state & KEY_GC_REAP_AGAIN)
  272. schedule_work(&key_gc_work);
  273. kleave(" [end %x]", gc_state);
  274. return;
  275. /* We found an unreferenced key - once we've removed it from the tree,
  276. * we can safely drop the lock.
  277. */
  278. found_unreferenced_key:
  279. kdebug("unrefd key %d", key->serial);
  280. rb_erase(&key->serial_node, &key_serial_tree);
  281. spin_unlock(&key_serial_lock);
  282. list_add_tail(&key->graveyard_link, &graveyard);
  283. gc_state |= KEY_GC_REAP_AGAIN;
  284. goto maybe_resched;
  285. /* We found a restricted keyring and need to update the restriction if
  286. * it is associated with the dead key type.
  287. */
  288. found_restricted_keyring:
  289. spin_unlock(&key_serial_lock);
  290. keyring_restriction_gc(key, key_gc_dead_keytype);
  291. goto maybe_resched;
  292. /* We found a keyring and we need to check the payload for links to
  293. * dead or expired keys. We don't flag another reap immediately as we
  294. * have to wait for the old payload to be destroyed by RCU before we
  295. * can reap the keys to which it refers.
  296. */
  297. found_keyring:
  298. spin_unlock(&key_serial_lock);
  299. keyring_gc(key, limit);
  300. goto maybe_resched;
  301. /* We found a dead key that is still referenced. Reset its type and
  302. * destroy its payload with its semaphore held.
  303. */
  304. destroy_dead_key:
  305. spin_unlock(&key_serial_lock);
  306. kdebug("destroy key %d", key->serial);
  307. down_write(&key->sem);
  308. key->type = &key_type_dead;
  309. if (key_gc_dead_keytype->destroy)
  310. key_gc_dead_keytype->destroy(key);
  311. memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
  312. up_write(&key->sem);
  313. goto maybe_resched;
  314. }