gc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 unsigned long key_gc_executing;
  27. static time_t key_gc_next_run = LONG_MAX;
  28. /*
  29. * Schedule a garbage collection run
  30. * - precision isn't particularly important
  31. */
  32. void key_schedule_gc(time_t gc_at)
  33. {
  34. unsigned long expires;
  35. time_t now = current_kernel_time().tv_sec;
  36. kenter("%ld", gc_at - now);
  37. gc_at += key_gc_delay;
  38. if (now >= gc_at) {
  39. schedule_work(&key_gc_work);
  40. } else if (gc_at < key_gc_next_run) {
  41. expires = jiffies + (gc_at - now) * HZ;
  42. mod_timer(&key_gc_timer, expires);
  43. }
  44. }
  45. /*
  46. * The garbage collector timer kicked off
  47. */
  48. static void key_gc_timer_func(unsigned long data)
  49. {
  50. kenter("");
  51. key_gc_next_run = LONG_MAX;
  52. schedule_work(&key_gc_work);
  53. }
  54. /*
  55. * Garbage collect pointers from a keyring
  56. * - return true if we altered the keyring
  57. */
  58. static bool key_gc_keyring(struct key *keyring, time_t limit)
  59. __releases(key_serial_lock)
  60. {
  61. struct keyring_list *klist;
  62. struct key *key;
  63. int loop;
  64. kenter("%x", key_serial(keyring));
  65. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  66. goto dont_gc;
  67. /* scan the keyring looking for dead keys */
  68. klist = rcu_dereference(keyring->payload.subscriptions);
  69. if (!klist)
  70. goto dont_gc;
  71. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  72. key = klist->keys[loop];
  73. if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
  74. (key->expiry > 0 && key->expiry <= limit))
  75. goto do_gc;
  76. }
  77. dont_gc:
  78. kleave(" = false");
  79. return false;
  80. do_gc:
  81. key_gc_cursor = keyring->serial;
  82. key_get(keyring);
  83. spin_unlock(&key_serial_lock);
  84. keyring_gc(keyring, limit);
  85. key_put(keyring);
  86. kleave(" = true");
  87. return true;
  88. }
  89. /*
  90. * Garbage collector for keys
  91. * - this involves scanning the keyrings for dead, expired and revoked keys
  92. * that have overstayed their welcome
  93. */
  94. static void key_garbage_collector(struct work_struct *work)
  95. {
  96. struct rb_node *rb;
  97. key_serial_t cursor;
  98. struct key *key, *xkey;
  99. time_t new_timer = LONG_MAX, limit;
  100. kenter("");
  101. if (test_and_set_bit(0, &key_gc_executing)) {
  102. key_schedule_gc(current_kernel_time().tv_sec);
  103. return;
  104. }
  105. limit = current_kernel_time().tv_sec;
  106. if (limit > key_gc_delay)
  107. limit -= key_gc_delay;
  108. else
  109. limit = key_gc_delay;
  110. spin_lock(&key_serial_lock);
  111. if (RB_EMPTY_ROOT(&key_serial_tree))
  112. goto reached_the_end;
  113. cursor = key_gc_cursor;
  114. if (cursor < 0)
  115. cursor = 0;
  116. /* find the first key above the cursor */
  117. key = NULL;
  118. rb = key_serial_tree.rb_node;
  119. while (rb) {
  120. xkey = rb_entry(rb, struct key, serial_node);
  121. if (cursor < xkey->serial) {
  122. key = xkey;
  123. rb = rb->rb_left;
  124. } else if (cursor > xkey->serial) {
  125. rb = rb->rb_right;
  126. } else {
  127. rb = rb_next(rb);
  128. if (!rb)
  129. goto reached_the_end;
  130. key = rb_entry(rb, struct key, serial_node);
  131. break;
  132. }
  133. }
  134. if (!key)
  135. goto reached_the_end;
  136. /* trawl through the keys looking for keyrings */
  137. for (;;) {
  138. if (key->expiry > 0 && key->expiry < new_timer)
  139. new_timer = key->expiry;
  140. if (key->type == &key_type_keyring &&
  141. key_gc_keyring(key, limit)) {
  142. /* the gc ate our lock */
  143. schedule_work(&key_gc_work);
  144. goto no_unlock;
  145. }
  146. rb = rb_next(&key->serial_node);
  147. if (!rb) {
  148. key_gc_cursor = 0;
  149. break;
  150. }
  151. key = rb_entry(rb, struct key, serial_node);
  152. }
  153. out:
  154. spin_unlock(&key_serial_lock);
  155. no_unlock:
  156. clear_bit(0, &key_gc_executing);
  157. if (new_timer < LONG_MAX)
  158. key_schedule_gc(new_timer);
  159. kleave("");
  160. return;
  161. reached_the_end:
  162. key_gc_cursor = 0;
  163. goto out;
  164. }