[PATCH] KEYS: protect construct_key type dereference with key semaphore

From: Tristan Madani

Date: Sat Nov 21 2026 - 06:29:31 EST


From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>

construct_key() reads key->type->request_key without holding key->sem.
A key reference keeps the key alive but does not pin the module that
provides its type. If the module is unloaded concurrently, the key GC
retypes the key to key_type_dead under key->sem, and the module text is
freed. Without the lock, construct_key can read a stale type pointer
after the method has been unmapped.

This is the same class of issue fixed for other key type methods in
commits 925c84e3cc21 ("KEYS: Protect the read method check against type
removal"), 51bcd09b9ffca ("KEYS: Protect the update method check against
type removal"), and bab48a1044475 ("KEYS: Protect the pkey method checks
against type removal"). construct_key was missed because request_key is
less commonly used than read/update/asym operations.

Take key->sem for reading around the key->type->request_key access to
prevent the type from changing while the method pointer is read.

Fixes: b5f545c880a2 ("[PATCH] keys: Permit running process to instantiate keys")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
---
security/keys/request_key.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/security/keys/request_key.c b/security/keys/request_key.c
index 22cd66a44da56..eff67c5fb9015 100644
--- a/security/keys/request_key.c
+++ b/security/keys/request_key.c
@@ -241,8 +241,10 @@ static int construct_key(struct key *key, const void *callout_info,

/* Make the call */
actor = call_sbin_request_key;
+ down_read(&key->sem);
if (key->type->request_key)
actor = key->type->request_key;
+ up_read(&key->sem);

ret = actor(authkey, aux);

--
2.47.3