gc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * - 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. * - return true if we altered the keyring
  58. */
  59. static bool key_gc_keyring(struct key *keyring, time_t limit)
  60. __releases(key_serial_lock)
  61. {
  62. struct keyring_list *klist;
  63. struct key *key;
  64. int loop;
  65. kenter("%x", key_serial(keyring));
  66. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  67. goto dont_gc;
  68. /* scan the keyring looking for dead keys */
  69. klist = rcu_dereference_check(keyring->payload.subscriptions,
  70. lockdep_is_held(&key_serial_lock));
  71. if (!klist)
  72. goto dont_gc;
  73. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  74. key = klist->keys[loop];
  75. if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
  76. (key->expiry > 0 && key->expiry <= limit))
  77. goto do_gc;
  78. }
  79. dont_gc:
  80. kleave(" = false");
  81. return false;
  82. do_gc:
  83. key_gc_cursor = keyring->serial;
  84. key_get(keyring);
  85. spin_unlock(&key_serial_lock);
  86. keyring_gc(keyring, limit);
  87. key_put(keyring);
  88. kleave(" = true");
  89. return true;
  90. }
  91. /*
  92. * Garbage collector for keys
  93. * - this involves scanning the keyrings for dead, expired and revoked keys
  94. * that have overstayed their welcome
  95. */
  96. static void key_garbage_collector(struct work_struct *work)
  97. {
  98. struct rb_node *rb;
  99. key_serial_t cursor;
  100. struct key *key, *xkey;
  101. time_t new_timer = LONG_MAX, limit, now;
  102. now = current_kernel_time().tv_sec;
  103. kenter("[%x,%ld]", key_gc_cursor, key_gc_new_timer - now);
  104. if (test_and_set_bit(0, &key_gc_executing)) {
  105. key_schedule_gc(current_kernel_time().tv_sec + 1);
  106. kleave(" [busy; deferring]");
  107. return;
  108. }
  109. limit = now;
  110. if (limit > key_gc_delay)
  111. limit -= key_gc_delay;
  112. else
  113. limit = key_gc_delay;
  114. spin_lock(&key_serial_lock);
  115. if (unlikely(RB_EMPTY_ROOT(&key_serial_tree))) {
  116. spin_unlock(&key_serial_lock);
  117. clear_bit(0, &key_gc_executing);
  118. return;
  119. }
  120. cursor = key_gc_cursor;
  121. if (cursor < 0)
  122. cursor = 0;
  123. if (cursor > 0)
  124. new_timer = key_gc_new_timer;
  125. else
  126. key_gc_again = false;
  127. /* find the first key above the cursor */
  128. key = NULL;
  129. rb = key_serial_tree.rb_node;
  130. while (rb) {
  131. xkey = rb_entry(rb, struct key, serial_node);
  132. if (cursor < xkey->serial) {
  133. key = xkey;
  134. rb = rb->rb_left;
  135. } else if (cursor > xkey->serial) {
  136. rb = rb->rb_right;
  137. } else {
  138. rb = rb_next(rb);
  139. if (!rb)
  140. goto reached_the_end;
  141. key = rb_entry(rb, struct key, serial_node);
  142. break;
  143. }
  144. }
  145. if (!key)
  146. goto reached_the_end;
  147. /* trawl through the keys looking for keyrings */
  148. for (;;) {
  149. if (key->expiry > limit && key->expiry < new_timer) {
  150. kdebug("will expire %x in %ld",
  151. key_serial(key), key->expiry - limit);
  152. new_timer = key->expiry;
  153. }
  154. if (key->type == &key_type_keyring &&
  155. key_gc_keyring(key, limit))
  156. /* the gc had to release our lock so that the keyring
  157. * could be modified, so we have to get it again */
  158. goto gc_released_our_lock;
  159. rb = rb_next(&key->serial_node);
  160. if (!rb)
  161. goto reached_the_end;
  162. key = rb_entry(rb, struct key, serial_node);
  163. }
  164. gc_released_our_lock:
  165. kdebug("gc_released_our_lock");
  166. key_gc_new_timer = new_timer;
  167. key_gc_again = true;
  168. clear_bit(0, &key_gc_executing);
  169. schedule_work(&key_gc_work);
  170. kleave(" [continue]");
  171. return;
  172. /* when we reach the end of the run, we set the timer for the next one */
  173. reached_the_end:
  174. kdebug("reached_the_end");
  175. spin_unlock(&key_serial_lock);
  176. key_gc_new_timer = new_timer;
  177. key_gc_cursor = 0;
  178. clear_bit(0, &key_gc_executing);
  179. if (key_gc_again) {
  180. /* there may have been a key that expired whilst we were
  181. * scanning, so if we discarded any links we should do another
  182. * scan */
  183. new_timer = now + 1;
  184. key_schedule_gc(new_timer);
  185. } else if (new_timer < LONG_MAX) {
  186. new_timer += key_gc_delay;
  187. key_schedule_gc(new_timer);
  188. }
  189. kleave(" [end]");
  190. }