0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/slab.h>
0009 #include <linux/security.h>
0010 #include <keys/keyring-type.h>
0011 #include "internal.h"
0012
0013
0014
0015
0016 unsigned key_gc_delay = 5 * 60;
0017
0018
0019
0020
0021 static void key_garbage_collector(struct work_struct *work);
0022 DECLARE_WORK(key_gc_work, key_garbage_collector);
0023
0024
0025
0026
0027 static void key_gc_timer_func(struct timer_list *);
0028 static DEFINE_TIMER(key_gc_timer, key_gc_timer_func);
0029
0030 static time64_t key_gc_next_run = TIME64_MAX;
0031 static struct key_type *key_gc_dead_keytype;
0032
0033 static unsigned long key_gc_flags;
0034 #define KEY_GC_KEY_EXPIRED 0
0035 #define KEY_GC_REAP_KEYTYPE 1
0036 #define KEY_GC_REAPING_KEYTYPE 2
0037
0038
0039
0040
0041
0042
0043 struct key_type key_type_dead = {
0044 .name = ".dead",
0045 };
0046
0047
0048
0049
0050
0051 void key_schedule_gc(time64_t gc_at)
0052 {
0053 unsigned long expires;
0054 time64_t now = ktime_get_real_seconds();
0055
0056 kenter("%lld", gc_at - now);
0057
0058 if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
0059 kdebug("IMMEDIATE");
0060 schedule_work(&key_gc_work);
0061 } else if (gc_at < key_gc_next_run) {
0062 kdebug("DEFERRED");
0063 key_gc_next_run = gc_at;
0064 expires = jiffies + (gc_at - now) * HZ;
0065 mod_timer(&key_gc_timer, expires);
0066 }
0067 }
0068
0069
0070
0071
0072 void key_schedule_gc_links(void)
0073 {
0074 set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
0075 schedule_work(&key_gc_work);
0076 }
0077
0078
0079
0080
0081
0082 static void key_gc_timer_func(struct timer_list *unused)
0083 {
0084 kenter("");
0085 key_gc_next_run = TIME64_MAX;
0086 key_schedule_gc_links();
0087 }
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 void key_gc_keytype(struct key_type *ktype)
0100 {
0101 kenter("%s", ktype->name);
0102
0103 key_gc_dead_keytype = ktype;
0104 set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
0105 smp_mb();
0106 set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
0107
0108 kdebug("schedule");
0109 schedule_work(&key_gc_work);
0110
0111 kdebug("sleep");
0112 wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
0113 TASK_UNINTERRUPTIBLE);
0114
0115 key_gc_dead_keytype = NULL;
0116 kleave("");
0117 }
0118
0119
0120
0121
0122 static noinline void key_gc_unused_keys(struct list_head *keys)
0123 {
0124 while (!list_empty(keys)) {
0125 struct key *key =
0126 list_entry(keys->next, struct key, graveyard_link);
0127 short state = key->state;
0128
0129 list_del(&key->graveyard_link);
0130
0131 kdebug("- %u", key->serial);
0132 key_check(key);
0133
0134 #ifdef CONFIG_KEY_NOTIFICATIONS
0135 remove_watch_list(key->watchers, key->serial);
0136 key->watchers = NULL;
0137 #endif
0138
0139
0140 if (state == KEY_IS_POSITIVE && key->type->destroy)
0141 key->type->destroy(key);
0142
0143 security_key_free(key);
0144
0145
0146 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
0147 spin_lock(&key->user->lock);
0148 key->user->qnkeys--;
0149 key->user->qnbytes -= key->quotalen;
0150 spin_unlock(&key->user->lock);
0151 }
0152
0153 atomic_dec(&key->user->nkeys);
0154 if (state != KEY_IS_UNINSTANTIATED)
0155 atomic_dec(&key->user->nikeys);
0156
0157 key_user_put(key->user);
0158 key_put_tag(key->domain_tag);
0159 kfree(key->description);
0160
0161 memzero_explicit(key, sizeof(*key));
0162 kmem_cache_free(key_jar, key);
0163 }
0164 }
0165
0166
0167
0168
0169
0170
0171
0172
0173 static void key_garbage_collector(struct work_struct *work)
0174 {
0175 static LIST_HEAD(graveyard);
0176 static u8 gc_state;
0177 #define KEY_GC_REAP_AGAIN 0x01
0178 #define KEY_GC_REAPING_LINKS 0x02
0179 #define KEY_GC_SET_TIMER 0x04
0180 #define KEY_GC_REAPING_DEAD_1 0x10
0181 #define KEY_GC_REAPING_DEAD_2 0x20
0182 #define KEY_GC_REAPING_DEAD_3 0x40
0183 #define KEY_GC_FOUND_DEAD_KEY 0x80
0184
0185 struct rb_node *cursor;
0186 struct key *key;
0187 time64_t new_timer, limit;
0188
0189 kenter("[%lx,%x]", key_gc_flags, gc_state);
0190
0191 limit = ktime_get_real_seconds();
0192 if (limit > key_gc_delay)
0193 limit -= key_gc_delay;
0194 else
0195 limit = key_gc_delay;
0196
0197
0198 gc_state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
0199 gc_state <<= 1;
0200 if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
0201 gc_state |= KEY_GC_REAPING_LINKS | KEY_GC_SET_TIMER;
0202
0203 if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
0204 gc_state |= KEY_GC_REAPING_DEAD_1;
0205 kdebug("new pass %x", gc_state);
0206
0207 new_timer = TIME64_MAX;
0208
0209
0210
0211
0212
0213 spin_lock(&key_serial_lock);
0214 cursor = rb_first(&key_serial_tree);
0215
0216 continue_scanning:
0217 while (cursor) {
0218 key = rb_entry(cursor, struct key, serial_node);
0219 cursor = rb_next(cursor);
0220
0221 if (refcount_read(&key->usage) == 0)
0222 goto found_unreferenced_key;
0223
0224 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
0225 if (key->type == key_gc_dead_keytype) {
0226 gc_state |= KEY_GC_FOUND_DEAD_KEY;
0227 set_bit(KEY_FLAG_DEAD, &key->flags);
0228 key->perm = 0;
0229 goto skip_dead_key;
0230 } else if (key->type == &key_type_keyring &&
0231 key->restrict_link) {
0232 goto found_restricted_keyring;
0233 }
0234 }
0235
0236 if (gc_state & KEY_GC_SET_TIMER) {
0237 if (key->expiry > limit && key->expiry < new_timer) {
0238 kdebug("will expire %x in %lld",
0239 key_serial(key), key->expiry - limit);
0240 new_timer = key->expiry;
0241 }
0242 }
0243
0244 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
0245 if (key->type == key_gc_dead_keytype)
0246 gc_state |= KEY_GC_FOUND_DEAD_KEY;
0247
0248 if ((gc_state & KEY_GC_REAPING_LINKS) ||
0249 unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
0250 if (key->type == &key_type_keyring)
0251 goto found_keyring;
0252 }
0253
0254 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3))
0255 if (key->type == key_gc_dead_keytype)
0256 goto destroy_dead_key;
0257
0258 skip_dead_key:
0259 if (spin_is_contended(&key_serial_lock) || need_resched())
0260 goto contended;
0261 }
0262
0263 contended:
0264 spin_unlock(&key_serial_lock);
0265
0266 maybe_resched:
0267 if (cursor) {
0268 cond_resched();
0269 spin_lock(&key_serial_lock);
0270 goto continue_scanning;
0271 }
0272
0273
0274
0275
0276
0277 kdebug("pass complete");
0278
0279 if (gc_state & KEY_GC_SET_TIMER && new_timer != (time64_t)TIME64_MAX) {
0280 new_timer += key_gc_delay;
0281 key_schedule_gc(new_timer);
0282 }
0283
0284 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
0285 !list_empty(&graveyard)) {
0286
0287
0288
0289
0290
0291 kdebug("gc sync");
0292 synchronize_rcu();
0293 }
0294
0295 if (!list_empty(&graveyard)) {
0296 kdebug("gc keys");
0297 key_gc_unused_keys(&graveyard);
0298 }
0299
0300 if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
0301 KEY_GC_REAPING_DEAD_2))) {
0302 if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
0303
0304
0305
0306 kdebug("dead short");
0307 gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
0308 gc_state |= KEY_GC_REAPING_DEAD_3;
0309 } else {
0310 gc_state |= KEY_GC_REAP_AGAIN;
0311 }
0312 }
0313
0314 if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
0315 kdebug("dead wake");
0316 smp_mb();
0317 clear_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
0318 wake_up_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE);
0319 }
0320
0321 if (gc_state & KEY_GC_REAP_AGAIN)
0322 schedule_work(&key_gc_work);
0323 kleave(" [end %x]", gc_state);
0324 return;
0325
0326
0327
0328
0329 found_unreferenced_key:
0330 kdebug("unrefd key %d", key->serial);
0331 rb_erase(&key->serial_node, &key_serial_tree);
0332 spin_unlock(&key_serial_lock);
0333
0334 list_add_tail(&key->graveyard_link, &graveyard);
0335 gc_state |= KEY_GC_REAP_AGAIN;
0336 goto maybe_resched;
0337
0338
0339
0340
0341 found_restricted_keyring:
0342 spin_unlock(&key_serial_lock);
0343 keyring_restriction_gc(key, key_gc_dead_keytype);
0344 goto maybe_resched;
0345
0346
0347
0348
0349
0350
0351 found_keyring:
0352 spin_unlock(&key_serial_lock);
0353 keyring_gc(key, limit);
0354 goto maybe_resched;
0355
0356
0357
0358
0359 destroy_dead_key:
0360 spin_unlock(&key_serial_lock);
0361 kdebug("destroy key %d", key->serial);
0362 down_write(&key->sem);
0363 key->type = &key_type_dead;
0364 if (key_gc_dead_keytype->destroy)
0365 key_gc_dead_keytype->destroy(key);
0366 memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
0367 up_write(&key->sem);
0368 goto maybe_resched;
0369 }