Skip to contentSkip to conversation

KEYS: protect construct_key type dereference with key semaphore

PATCH New Tristan Madani 2 participants 2 messages Nov 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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. While the sys_request_key() syscall path holds key_types_sem for reading (via key_type_lookup()), which prevents concurrent type removal, in-kernel request_key() callers pass struct key_type directly without holding key_types_sem. If unregister_key_type() races with such a caller, key_garbage_collector() can retype the key to key_type_dead and the module text backing the original request_key method can be freed before the actor call executes.

Take key->sem for reading around both the key->type->request_key lookup and the actor() call, consistent with the approach used by 925c84e3cc21 ("KEYS: Protect the read method check against type removal"), 51bcd09b9ffc ("KEYS: Protect the update method check against type removal") and bab48a104447 ("KEYS: Protect the pkey method checks against type removal") which protect analogous method dereferences in the keyctl paths.

Fixes b5f545c880a2 ("[PATCH] keys: Permit running process to instantiate keys")

---
Changes in v2:
- Hold key->sem across the actor() call, not just the type pointer read, matching the established pattern from the three prior fixes (Sashiko)
- Clarify that in-kernel request_key() callers do not hold key_types_sem so the race is reachable outside the syscall path (Sashiko)

 security/keys/request_key.c | 2 ++
 1 file changed, 2 insertions(+)
security/keys/request_key.c +2 -0
index 22cd66a44da56..60a8cb5924e11 100644
@@ -241,10 +241,12 @@ 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;
 
 	ret = actor(authkey, aux);
+	up_read(&key->sem);
 
 	/* check that the actor called complete_request_key() prior to
 	 * returning an error */
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KEYS: deadlock in construct_key() holding key->sem across actor() call --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KEYS: deadlock in construct_key() holding key->sem across actor() call
--

commit 89caf57fd1a6b94038c074beb5c384911f131253
Author: Tristan Madani <[email protected]>

KEYS: protect construct_key type dereference with key semaphore

This commit acquires key->sem for reading around the key->type->request_key lookup and actor() invocation in construct_key().

Quoted 7 lines
security/keys/request_key.c +0 -0
index fa2bb9f2f5381..afc9df61506b9 100644
@@ -241,10 +241,12 @@ static int construct_key(struct key *key, const void *callout_info,
 
 	/* Make the call */

security/keys/request_key.c static int construct_key(struct key *key, const void *callout_info, · line 252

Quoted 10 lines
security/keys/request_key.c +2 -0
 	actor = call_sbin_request_key;
+	down_read(&key->sem);
 	if (key->type->request_key)
 		actor = key->type->request_key;
 
 	ret = actor(authkey, aux);
+	up_read(&key->sem);
 
 	/* check that the actor called complete_request_key() prior to
 	 * returning an error */

[Severity: High]
Can holding key->sem across actor() deadlock against the /sbin/request-key userspace helper?

For key types without a custom ->request_key method, actor() is call_sbin_request_key(), which calls call_usermodehelper_keys(..., UMH_WAIT_PROC) and blocks until the userspace helper exits. Standard helpers (such as key.dns_resolver, nfsidmap, and cifs.upcall) invoke keyctl(KEYCTL_SET_TIMEOUT) on the key under construction before exiting, which keyctl_set_timeout() explicitly permits via KEY_LOOKUP_PARTIAL and KEY_AUTHTOKEN_OVERRIDE:

Requesting task /sbin/request-key helper
--------------- ------------------------
construct_key()
down_read(&key->sem)
call_sbin_request_key()

    call_usermodehelper_keys(..., UMH_WAIT_PROC)
                                  keyctl_set_timeout()
                                    key_set_timeout()
                                      down_write(&key->sem)  <-- blocks

up_read(&key->sem)

*** DEADLOCK ***

keyctl_setperm_key() and keyctl_chown_key() likewise look up keys under construction with KEY_LOOKUP_PARTIAL and take down_write(&key->sem).

Also, taking key->sem in construct_key() does not protect in-kernel request_key() callers against concurrent unregister_key_type(): the sole caller request_key_and_link() requires struct key_type *type to remain registered across the call and dereferences type without key->sem both before construct_key() (type->match_preparse and key_alloc()) and after it returns (type->match_free).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1