gc.c 5.1 KB

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