Message ID | 2874581.1742399866@warthog.procyon.org.uk (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | [v2] keys: Fix UAF in key_put() | expand |
On 03/19, David Howells wrote: > > --- a/security/keys/gc.c > +++ b/security/keys/gc.c > @@ -218,8 +218,10 @@ static void key_garbage_collector(struct work_struct *work) > key = rb_entry(cursor, struct key, serial_node); > cursor = rb_next(cursor); > > - if (refcount_read(&key->usage) == 0) > + if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) { > + smp_mb(); /* Clobber key->user after FINAL_PUT seen. */ > goto found_unreferenced_key; > + } > > if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) { > if (key->type == key_gc_dead_keytype) { > diff --git a/security/keys/key.c b/security/keys/key.c > index 3d7d185019d3..7198cd2ac3a3 100644 > --- a/security/keys/key.c > +++ b/security/keys/key.c > @@ -658,6 +658,8 @@ void key_put(struct key *key) > key->user->qnbytes -= key->quotalen; > spin_unlock_irqrestore(&key->user->lock, flags); > } > + smp_mb(); /* key->user before FINAL_PUT set. */ Can't resist, smp_mb__before_atomic() should equally work, but this doesn't really matter, please forget. > + set_bit(KEY_FLAG_FINAL_PUT, &key->flags); > schedule_work(&key_gc_work); I believe this patch is correct, Reviewed-by: Oleg Nesterov <oleg@redhat.com>
On Wed, 19 Mar 2025 at 09:31, Oleg Nesterov <oleg@redhat.com> wrote: > > Can't resist, smp_mb__before_atomic() should equally work, > but this doesn't really matter, please forget. We really should have "test_bit_acquire()" and "set_bit_release()". Well, we do have the test_bit_acquire(). We just don't have the set_bit side, because we only have the bit clearing version (and it's called "clear_bit_unlock()" for historical reasons). Annoying. Linus
Linus Torvalds <torvalds@linuxfoundation.org> wrote:
> We really should have "test_bit_acquire()" and "set_bit_release()".
I considered using test_bit_acquire() but, as you say, there's no
set_bit_release() as yet. I could switch things to initialise the flag to set
on key creation and clear the flag instead.
David
diff --git a/include/linux/key.h b/include/linux/key.h index 074dca3222b9..ba05de8579ec 100644 --- a/include/linux/key.h +++ b/include/linux/key.h @@ -236,6 +236,7 @@ struct key { #define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */ #define KEY_FLAG_KEEP 8 /* set if key should not be removed */ #define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */ +#define KEY_FLAG_FINAL_PUT 10 /* set if final put has happened on key */ /* the key type and key description string * - the desc is used to match a key against search criteria diff --git a/security/keys/gc.c b/security/keys/gc.c index 7d687b0962b1..f27223ea4578 100644 --- a/security/keys/gc.c +++ b/security/keys/gc.c @@ -218,8 +218,10 @@ static void key_garbage_collector(struct work_struct *work) key = rb_entry(cursor, struct key, serial_node); cursor = rb_next(cursor); - if (refcount_read(&key->usage) == 0) + if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) { + smp_mb(); /* Clobber key->user after FINAL_PUT seen. */ goto found_unreferenced_key; + } if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) { if (key->type == key_gc_dead_keytype) { diff --git a/security/keys/key.c b/security/keys/key.c index 3d7d185019d3..7198cd2ac3a3 100644 --- a/security/keys/key.c +++ b/security/keys/key.c @@ -658,6 +658,8 @@ void key_put(struct key *key) key->user->qnbytes -= key->quotalen; spin_unlock_irqrestore(&key->user->lock, flags); } + smp_mb(); /* key->user before FINAL_PUT set. */ + set_bit(KEY_FLAG_FINAL_PUT, &key->flags); schedule_work(&key_gc_work); } }