gc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. {
  60. struct keyring_list *klist;
  61. struct key *key;
  62. int loop;
  63. kenter("%x", key_serial(keyring));
  64. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  65. goto dont_gc;
  66. /* scan the keyring looking for dead keys */
  67. klist = rcu_dereference(keyring->payload.subscriptions);
  68. if (!klist)
  69. goto dont_gc;
  70. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  71. key = klist->keys[loop];
  72. if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
  73. (key->expiry > 0 && key->expiry <= limit))
  74. goto do_gc;
  75. }
  76. dont_gc:
  77. kleave(" = false");
  78. return false;
  79. do_gc:
  80. key_gc_cursor = keyring->serial;
  81. key_get(keyring);
  82. spin_unlock(&key_serial_lock);
  83. keyring_gc(keyring, limit);
  84. key_put(keyring);
  85. kleave(" = true");
  86. return true;
  87. }
  88. /*
  89. * Garbage collector for keys
  90. * - this involves scanning the keyrings for dead, expired and revoked keys
  91. * that have overstayed their welcome
  92. */
  93. static void key_garbage_collector(struct work_struct *work)
  94. {
  95. struct rb_node *rb;
  96. key_serial_t cursor;
  97. struct key *key, *xkey;
  98. time_t new_timer = LONG_MAX, limit;
  99. kenter("");
  100. if (test_and_set_bit(0, &key_gc_executing)) {
  101. key_schedule_gc(current_kernel_time().tv_sec);
  102. return;
  103. }
  104. limit = current_kernel_time().tv_sec;
  105. if (limit > key_gc_delay)
  106. limit -= key_gc_delay;
  107. else
  108. limit = key_gc_delay;
  109. spin_lock(&key_serial_lock);
  110. if (RB_EMPTY_ROOT(&key_serial_tree))
  111. goto reached_the_end;
  112. cursor = key_gc_cursor;
  113. if (cursor < 0)
  114. cursor = 0;
  115. /* find the first key above the cursor */
  116. key = NULL;
  117. rb = key_serial_tree.rb_node;
  118. while (rb) {
  119. xkey = rb_entry(rb, struct key, serial_node);
  120. if (cursor < xkey->serial) {
  121. key = xkey;
  122. rb = rb->rb_left;
  123. } else if (cursor > xkey->serial) {
  124. rb = rb->rb_right;
  125. } else {
  126. rb = rb_next(rb);
  127. if (!rb)
  128. goto reached_the_end;
  129. key = rb_entry(rb, struct key, serial_node);
  130. break;
  131. }
  132. }
  133. if (!key)
  134. goto reached_the_end;
  135. /* trawl through the keys looking for keyrings */
  136. for (;;) {
  137. if (key->expiry > 0 && key->expiry < new_timer)
  138. new_timer = key->expiry;
  139. if (key->type == &key_type_keyring &&
  140. key_gc_keyring(key, limit)) {
  141. /* the gc ate our lock */
  142. schedule_work(&key_gc_work);
  143. goto no_unlock;
  144. }
  145. rb = rb_next(&key->serial_node);
  146. if (!rb) {
  147. key_gc_cursor = 0;
  148. break;
  149. }
  150. key = rb_entry(rb, struct key, serial_node);
  151. }
  152. out:
  153. spin_unlock(&key_serial_lock);
  154. no_unlock:
  155. clear_bit(0, &key_gc_executing);
  156. if (new_timer < LONG_MAX)
  157. key_schedule_gc(new_timer);
  158. kleave("");
  159. return;
  160. reached_the_end:
  161. key_gc_cursor = 0;
  162. goto out;
  163. }