From: Tristan Madani <tristmd@gmail.com>
To: David Howells <dhowells@redhat.com>, Jarkko Sakkinen <jarkko@kernel.org>
Cc: Paul Moore <paul@paul-moore.com>,
James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
keyrings@vger.kernel.org, linux-security-module@vger.kernel.org,
stable@vger.kernel.org,
Tristan Madani <tristan@talencesecurity.com>
Subject: [PATCH v3] KEYS: protect construct_key type dereference with key semaphore
Date: Tue, 24 Nov 2026 21:41:25 +0000 [thread overview]
Message-ID: <20261124214125.3198541-1-tristmd@gmail.com> (raw)
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
next reply other threads:[~2026-11-24 21:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 21:41 Tristan Madani [this message]
2026-11-24 21:49 ` [PATCH v3] KEYS: protect construct_key type dereference with key semaphore sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124214125.3198541-1-tristmd@gmail.com \
--to=tristmd@gmail.com \
--cc=dhowells@redhat.com \
--cc=jarkko@kernel.org \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.com \
--cc=stable@vger.kernel.org \
--cc=tristan@talencesecurity.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox