projects/linux·commit fcea5418

Merge tag 'keys-next-6.14-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd

fcea5418 · Linus Torvalds <[email protected]> · 2025-03-22 14:10:07 -07:00

Pull keys fix from Jarkko Sakkinen:
 "Fix potential use-after-free in key_put()"

* tag 'keys-next-6.14-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd:
  keys: Fix UAF in key_put()
Commit
fcea541800539899ba0073259cd35d615488a415
Parents
bb18645a · 75845c6c

Merge commit. Changes below are compared with the first parent: bb18645a.

Changes

3 files changed · +6 −1

FileStatusAddDelContent
include/linux/key.hmodified+1−0View file
security/keys/gc.cmodified+3−1View file
security/keys/key.cmodified+2−0View file

include/linux/key.h

View file
diff --git a/include/linux/key.h b/include/linux/key.h
index 074dca3..ba05de8 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

security/keys/gc.c

View file
diff --git a/security/keys/gc.c b/security/keys/gc.c
index 7d687b0..f27223e 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -218,8 +218,10 @@ continue_scanning:
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) {

security/keys/key.c

View file
diff --git a/security/keys/key.c b/security/keys/key.c
index 3d7d185..7198cd2 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);
}
}