KEYS: protect construct_key type dereference with key semaphore
Subsystems (3)
PATCH v3 1/1 KEYS: protect construct_key type dereference with key semaphore
From: Tristan Madani <[email protected]>
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.
---
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(+)
security/keys/request_key.c +2 -0
@@ -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);
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] keys: releasing key->sem before actor() in construct_key() defeats protection --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] keys: releasing key->sem before actor() in construct_key() defeats protection
--
commit 9d2b5e770f320f3d569ad3450cb1b85673517461
Author: Tristan Madani <[email protected]>
KEYS: protect construct_key type dereference with key semaphore
This commit takes key->sem for reading around the key->type->request_key lookup in construct_key() and releases it before invoking actor().
Quoted 5 lines
security/keys/request_key.c +0
-0
security/keys/request_key.c static int construct_key(struct key *key, const void *callout_info, · line 249
Quoted 9 lines
security/keys/request_key.c +2
-0@@ -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);
[Severity: Medium]
Does holding key->sem only across the key->type->request_key pointer load provide the protection against concurrent type unregistration that the commit message describes?
In commit 925c84e3cc21 ("keys: Protect the read method check against type removal"), __keyctl_read_key() holds key->sem across key_validate(key), the key->type->read check, and the key->type->read() call itself. Here, up_read(&key->sem) runs before actor(authkey, aux), so if key_garbage_collector() could race with construct_key(), it could acquire down_write(&key->sem), retype the key to key_type_dead, and let unregister_key_type() complete right after up_read(&key->sem) and before actor() runs. And if the key were retyped to key_type_dead before down_read(&key->sem), key_type_dead.request_key is NULL and KEY_FLAG_DEAD is not checked, so construct_key() would fall back to call_sbin_request_key on a dead key.
More broadly, construct_key() is only reached through request_key_and_link(), whose callers already pin the key_type for the entire operation (__do_sys_request_key() holds key_types_sem via key_type_lookup(), and request_key_and_link() itself dereferences type->match_preparse before and type->match_free after construct_key() without key->sem).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1