gc.c 9.9 KB

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