Once a key's reference count has been reduced to 0, the garbage collector thread may destroy it at any time and so key_put() is not allowed to touch the key after that point. The most key_put() is normally allowed to do is to touch key_gc_work as that's a static global variable. However, in an effort to speed up the reclamation of quota, this is now done in key_put() once the key's usage is reduced to 0 - but now the code is looking at the key after the deadline, which is forbidden. Fix this by using a flag to indicate that a key can be gc'd now rather than looking at the key's refcount in the garbage collector.
Unified · Split
| @@ -236,6 +236,7 @@ struct key { · 235 unmodified lines | |||
| 236 | #define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */ | 236 | #define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */ |
| 237 | #define KEY_FLAG_KEEP 8 /* set if key should not be removed */ | 237 | #define KEY_FLAG_KEEP 8 /* set if key should not be removed */ |
| 238 | #define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */ | 238 | #define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */ |
| 239 | #define KEY_FLAG_FINAL_PUT 10 /* set if final put has happened on key */ | ||
| 239 | 240 | ||
| 240 | /* the key type and key description string | 241 | /* the key type and key description string |
| 241 | * - the desc is used to match a key against search criteria | 242 | * - the desc is used to match a key against search criteria |
| @@ -218,8 +218,10 @@ continue_scanning: · 217 unmodified lines | |||
| 218 | key = rb_entry(cursor, struct key, serial_node); | 218 | key = rb_entry(cursor, struct key, serial_node); |
| 219 | cursor = rb_next(cursor); | 219 | cursor = rb_next(cursor); |
| 220 | 220 | ||
| 221 | if (refcount_read(&key->usage) == 0) | 221 | if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) { |
| 222 | smp_mb(); /* Clobber key->user after FINAL_PUT seen. */ | ||
| 222 | goto found_unreferenced_key; | 223 | goto found_unreferenced_key; |
| 224 | } | ||
| 223 | 225 | ||
| 224 | if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) { | 226 | if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) { |
| 225 | if (key->type == key_gc_dead_keytype) { | 227 | if (key->type == key_gc_dead_keytype) { |
| @@ -658,6 +658,8 @@ void key_put(struct key *key) · 657 unmodified lines | |||
| 658 | key->user->qnbytes -= key->quotalen; | 658 | key->user->qnbytes -= key->quotalen; |
| 659 | spin_unlock_irqrestore(&key->user->lock, flags); | 659 | spin_unlock_irqrestore(&key->user->lock, flags); |
| 660 | } | 660 | } |
| 661 | smp_mb(); /* key->user before FINAL_PUT set. */ | ||
| 662 | set_bit(KEY_FLAG_FINAL_PUT, &key->flags); | ||
| 661 | schedule_work(&key_gc_work); | 663 | schedule_work(&key_gc_work); |
| 662 | } | 664 | } |
| 663 | } | 665 | } |