gc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Key garbage collector
  2. *
  3. * Copyright (C) 2009 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 <keys/keyring-type.h>
  13. #include "internal.h"
  14. /*
  15. * Delay between key revocation/expiry in seconds
  16. */
  17. unsigned key_gc_delay = 5 * 60;
  18. /*
  19. * Reaper
  20. */
  21. static void key_gc_timer_func(unsigned long);
  22. static void key_garbage_collector(struct work_struct *);
  23. static DEFINE_TIMER(key_gc_timer, key_gc_timer_func, 0, 0);
  24. static DECLARE_WORK(key_gc_work, key_garbage_collector);
  25. static key_serial_t key_gc_cursor; /* the last key the gc considered */
  26. static bool key_gc_again;
  27. static unsigned long key_gc_executing;
  28. static time_t key_gc_next_run = LONG_MAX;
  29. static time_t key_gc_new_timer;
  30. /*
  31. * Schedule a garbage collection run.
  32. * - time precision isn't particularly important
  33. */
  34. void key_schedule_gc(time_t gc_at)
  35. {
  36. unsigned long expires;
  37. time_t now = current_kernel_time().tv_sec;
  38. kenter("%ld", gc_at - now);
  39. if (gc_at <= now) {
  40. schedule_work(&key_gc_work);
  41. } else if (gc_at < key_gc_next_run) {
  42. expires = jiffies + (gc_at - now) * HZ;
  43. mod_timer(&key_gc_timer, expires);
  44. }
  45. }
  46. /*
  47. * The garbage collector timer kicked off
  48. */
  49. static void key_gc_timer_func(unsigned long data)
  50. {
  51. kenter("");
  52. key_gc_next_run = LONG_MAX;
  53. schedule_work(&key_gc_work);
  54. }
  55. /*
  56. * Garbage collect pointers from a keyring.
  57. *
  58. * Return true if we altered the keyring.
  59. */
  60. static bool key_gc_keyring(struct key *keyring, time_t limit)
  61. __releases(key_serial_lock)
  62. {
  63. struct keyring_list *klist;
  64. struct key *key;
  65. int loop;
  66. kenter("%x", key_serial(keyring));
  67. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  68. goto dont_gc;
  69. /* scan the keyring looking for dead keys */
  70. rcu_read_lock();
  71. klist = rcu_dereference(keyring->payload.subscriptions);
  72. if (!klist)
  73. goto unlock_dont_gc;
  74. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  75. key = klist->keys[loop];
  76. if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
  77. (key->expiry > 0 && key->expiry <= limit))
  78. goto do_gc;
  79. }
  80. unlock_dont_gc:
  81. rcu_read_unlock();
  82. dont_gc:
  83. kleave(" = false");
  84. return false;
  85. do_gc:
  86. rcu_read_unlock();
  87. key_gc_cursor = keyring->serial;
  88. key_get(keyring);
  89. spin_unlock(&key_serial_lock);
  90. keyring_gc(keyring, limit);
  91. key_put(keyring);
  92. kleave(" = true");
  93. return true;
  94. }
  95. /*
  96. * Garbage collector for keys. This involves scanning the keyrings for dead,
  97. * expired and revoked keys that have overstayed their welcome
  98. */
  99. static void key_garbage_collector(struct work_struct *work)
  100. {
  101. struct rb_node *rb;
  102. key_serial_t cursor;
  103. struct key *key, *xkey;
  104. time_t new_timer = LONG_MAX, limit, now;
  105. now = current_kernel_time().tv_sec;
  106. kenter("[%x,%ld]", key_gc_cursor, key_gc_new_timer - now);
  107. if (test_and_set_bit(0, &key_gc_executing)) {
  108. key_schedule_gc(current_kernel_time().tv_sec + 1);
  109. kleave(" [busy; deferring]");
  110. return;
  111. }
  112. limit = now;
  113. if (limit > key_gc_delay)
  114. limit -= key_gc_delay;
  115. else
  116. limit = key_gc_delay;
  117. spin_lock(&key_serial_lock);
  118. if (unlikely(RB_EMPTY_ROOT(&key_serial_tree))) {
  119. spin_unlock(&key_serial_lock);
  120. clear_bit(0, &key_gc_executing);
  121. return;
  122. }
  123. cursor = key_gc_cursor;
  124. if (cursor < 0)
  125. cursor = 0;
  126. if (cursor > 0)
  127. new_timer = key_gc_new_timer;
  128. else
  129. key_gc_again = false;
  130. /* find the first key above the cursor */
  131. key = NULL;
  132. rb = key_serial_tree.rb_node;
  133. while (rb) {
  134. xkey = rb_entry(rb, struct key, serial_node);
  135. if (cursor < xkey->serial) {
  136. key = xkey;
  137. rb = rb->rb_left;
  138. } else if (cursor > xkey->serial) {
  139. rb = rb->rb_right;
  140. } else {
  141. rb = rb_next(rb);
  142. if (!rb)
  143. goto reached_the_end;
  144. key = rb_entry(rb, struct key, serial_node);
  145. break;
  146. }
  147. }
  148. if (!key)
  149. goto reached_the_end;
  150. /* trawl through the keys looking for keyrings */
  151. for (;;) {
  152. if (key->expiry > limit && key->expiry < new_timer) {
  153. kdebug("will expire %x in %ld",
  154. key_serial(key), key->expiry - limit);
  155. new_timer = key->expiry;
  156. }
  157. if (key->type == &key_type_keyring &&
  158. key_gc_keyring(key, limit))
  159. /* the gc had to release our lock so that the keyring
  160. * could be modified, so we have to get it again */
  161. goto gc_released_our_lock;
  162. rb = rb_next(&key->serial_node);
  163. if (!rb)
  164. goto reached_the_end;
  165. key = rb_entry(rb, struct key, serial_node);
  166. }
  167. gc_released_our_lock:
  168. kdebug("gc_released_our_lock");
  169. key_gc_new_timer = new_timer;
  170. key_gc_again = true;
  171. clear_bit(0, &key_gc_executing);
  172. schedule_work(&key_gc_work);
  173. kleave(" [continue]");
  174. return;
  175. /* when we reach the end of the run, we set the timer for the next one */
  176. reached_the_end:
  177. kdebug("reached_the_end");
  178. spin_unlock(&key_serial_lock);
  179. key_gc_new_timer = new_timer;
  180. key_gc_cursor = 0;
  181. clear_bit(0, &key_gc_executing);
  182. if (key_gc_again) {
  183. /* there may have been a key that expired whilst we were
  184. * scanning, so if we discarded any links we should do another
  185. * scan */
  186. new_timer = now + 1;
  187. key_schedule_gc(new_timer);
  188. } else if (new_timer < LONG_MAX) {
  189. new_timer += key_gc_delay;
  190. key_schedule_gc(new_timer);
  191. }
  192. kleave(" [end]");
  193. }