Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH v3] KEYS: protect construct_key type dereference with key semaphore
@ 2026-11-24 21:41 Tristan Madani
  2026-11-24 21:49 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tristan Madani @ 2026-11-24 21:41 UTC (permalink / raw)
  To: David Howells, Jarkko Sakkinen
  Cc: Paul Moore, James Morris, Serge E. Hallyn, keyrings,
	linux-security-module, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

construct_key() reads key->type->request_key without holding key->sem,
then calls the resulting actor function pointer.  The key garbage
collector retypes keys to key_type_dead under key->sem write lock when
their type is unregistered.  Without the read lock, construct_key() can
observe a stale type pointer if the garbage collector retypes the key
between the pointer read and the actor invocation.

Take key->sem for reading around the key->type->request_key lookup to
prevent concurrent retyping by the garbage collector, then release the
semaphore before invoking the actor.  The semaphore cannot be held
across the actor call because call_sbin_request_key() blocks in
call_usermodehelper_keys() with UMH_WAIT_PROC until the userspace
helper exits, and standard helpers such as key.dns_resolver and
nfsidmap take key->sem for writing via keyctl_set_timeout() before
they return.

This follows the same defensive pattern as commit 925c84e3cc21 ("keys:
Protect the read method check against type removal") but narrows the
lock scope to the type pointer read to avoid the deadlock.

Fixes: b5f545c880a2 ("[PATCH] keys: Permit running process to instantiate keys")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
v3: narrow the lock scope to the type pointer read only, releasing
key->sem before the actor() call. Holding the semaphore across actor()
deadlocks when call_sbin_request_key blocks in UMH_WAIT_PROC and the
userspace helper takes key->sem write via keyctl_set_timeout() (Sashiko)

v2: hold key->sem across the actor() call, not just the type pointer
read, matching the established pattern from three prior fixes (Sashiko)

Build tested: x86_64 defconfig + CONFIG_KEYS=y, gcc 14.2, W=1,
0 warnings.

 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 a7673ad86d18d..ccc4e94197423 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 21:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 21:41 [PATCH v3] KEYS: protect construct_key type dereference with key semaphore Tristan Madani
2026-11-24 21:49 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox