* [PATCH] KEYS: Protect the instantiate method against type removal
@ 2026-11-24 5:33 Guicai Ma
2026-11-24 5:54 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Guicai Ma @ 2026-11-24 5:33 UTC (permalink / raw)
To: David Howells, Jarkko Sakkinen
Cc: keyrings, linux-security-module, linux-kernel, Paul Moore,
James Morris, Serge E . Hallyn, Guicai Ma
key_instantiate_and_link() reads key->type->def_datalen and ->preparse
and calls ->instantiate without holding key->sem. A reference on the
key keeps it alive, but does not pin the module that provides its type.
unregister_key_type() waits for the key GC to process every remaining
key of the type. For each such key the GC takes key->sem for writing,
switches key->type to key_type_dead and destroys the payload. A key
that is still under construction is retyped as well: it stays in
KEY_IS_UNINSTANTIATED and its authorisation key remains valid, so
KEYCTL_INSTANTIATE from the upcall helper reaches
__key_instantiate_and_link() and calls key_type_dead.instantiate,
which is NULL:
Unable to handle kernel NULL pointer dereference at
virtual address 0000000000000000
Internal error: Oops: 0000000086000006 [#1] SMP
pc : 0x0
lr : __key_instantiate_and_link+0xb8/0x2f8
Call trace:
0x0 (P)
key_instantiate_and_link+0x2e8/0x36c
keyctl_instantiate_key_common+0x1cc/0x338
keyctl_instantiate_key+0xbc/0x120
__arm64_sys_keyctl+0x218/0x3f0
If the module is unloaded while ->preparse or ->instantiate is running,
execution can also return into freed module text, or skip
->free_preparse after the key is retyped and leak the preparsed
payload. While the request_key() syscall holds key_types_sem across the
upcall, in-kernel request_key() callers such as dns_query() do not, so
unregister_key_type() can run before KEYCTL_INSTANTIATE.
Hold key->sem for reading across key_instantiate_and_link() and return
-EOPNOTSUPP if the type has no ->instantiate method. Since
keyring_alloc() links a new keyring into a destination keyring with the
new keyring's semaphore held at subclass 0, take the destination
keyring's semaphore at subclass 1 in __key_link_lock(), matching the
0 -> 1 order in __key_move_lock().
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Guicai Ma <maguicai@kylinos.cn>
---
Tested on 7.4.0-rc3 arm64 in QEMU with KASAN and lockdep enabled by
unloading a test key-type module between an in-kernel request_key()
upcall and KEYCTL_INSTANTIATE. Unpatched Oopses at pc=0 on the first
round; patched returns -EOPNOTSUPP and passes 20 concurrent
instantiate-vs-unload rounds and nested keyring creation cleanly.
security/keys/key.c | 39 +++++++++++++++++++++++++++++----------
security/keys/keyring.c | 9 ++++++++-
2 files changed, 37 insertions(+), 11 deletions(-)
diff --git a/security/keys/key.c b/security/keys/key.c
index 3f1f0fc13710..12cbca31824c 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -422,9 +422,10 @@ static void mark_key_instantiated(struct key *key, int reject_error)
/*
* Instantiate a key and link it into the target keyring atomically. Must be
- * called with the target keyring's semaphore writelocked. The target key's
- * semaphore need not be locked as instantiation is serialised by
- * key_construction_mutex.
+ * called with the target keyring's semaphore writelocked. Either the target
+ * key's semaphore or key_types_sem must be held for reading so that the
+ * garbage collector cannot retype the key to key_type_dead while the type's
+ * methods are in use.
*/
static int __key_instantiate_and_link(struct key *key,
struct key_preparsed_payload *prep,
@@ -496,7 +497,8 @@ static int __key_instantiate_and_link(struct key *key,
*
* If successful, 0 is returned, the authorisation token is revoked and anyone
* waiting for the key is woken up. If the key was already instantiated,
- * -EBUSY will be returned.
+ * -EBUSY will be returned. If the key type no longer provides an instantiate
+ * method, -EOPNOTSUPP will be returned.
*/
int key_instantiate_and_link(struct key *key,
const void *data,
@@ -509,6 +511,20 @@ int key_instantiate_and_link(struct key *key,
int ret;
memset(&prep, 0, sizeof(prep));
+
+ /*
+ * A reference on the key does not pin the module that provides its
+ * type. unregister_key_type() waits for the garbage collector to
+ * retype every remaining key of the type to key_type_dead, whose
+ * ->instantiate is NULL, so the type must not change from here to the
+ * instantiate call.
+ */
+ down_read(&key->sem);
+
+ ret = -EOPNOTSUPP;
+ if (!key->type->instantiate)
+ goto out;
+
prep.orig_description = key->description;
prep.data = data;
prep.datalen = datalen;
@@ -517,17 +533,17 @@ int key_instantiate_and_link(struct key *key,
if (key->type->preparse) {
ret = key->type->preparse(&prep);
if (ret < 0)
- goto error;
+ goto out_free_preparse;
}
if (keyring) {
ret = __key_link_lock(keyring, &key->index_key);
if (ret < 0)
- goto error;
+ goto out_free_preparse;
ret = __key_link_begin(keyring, &key->index_key, &edit);
if (ret < 0)
- goto error_link_end;
+ goto out_link_end;
if (keyring->restrict_link && keyring->restrict_link->check) {
struct key_restriction *keyres = keyring->restrict_link;
@@ -535,19 +551,22 @@ int key_instantiate_and_link(struct key *key,
ret = keyres->check(keyring, key->type, &prep.payload,
keyres->key);
if (ret < 0)
- goto error_link_end;
+ goto out_link_end;
}
}
ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
-error_link_end:
+out_link_end:
if (keyring)
__key_link_end(keyring, &key->index_key, edit);
-error:
+out_free_preparse:
if (key->type->preparse)
key->type->free_preparse(&prep);
+
+out:
+ up_read(&key->sem);
return ret;
}
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 357f56360810..f545f56c84e7 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -1262,7 +1262,14 @@ int __key_link_lock(struct key *keyring,
if (keyring->type != &key_type_keyring)
return -ENOTDIR;
- down_write(&keyring->sem);
+ /*
+ * key_instantiate_and_link() links a newly allocated keyring into this
+ * one while holding the new keyring's semaphore for reading as
+ * subclass 0, so take the destination keyring at subclass 1 to keep
+ * the same 0 -> 1 order that __key_move_lock() uses. None of the
+ * other callers of this function hold a key semaphore.
+ */
+ down_write_nested(&keyring->sem, 1);
/* Serialise link/link calls to prevent parallel calls causing a cycle
* when linking two keyring in opposite orders.
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] KEYS: Protect the instantiate method against type removal
2026-11-24 5:33 [PATCH] KEYS: Protect the instantiate method against type removal Guicai Ma
@ 2026-11-24 5:54 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-24 5:54 UTC (permalink / raw)
To: Guicai Ma, David Howells, Jarkko Sakkinen
Cc: keyrings, linux-security-module, Paul Moore, James Morris,
Serge E . Hallyn
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] KEYS: lock ordering inversion in key_instantiate_and_link()
- [Medium] KEYS: missing KEY_FLAG_DEAD check in
key_instantiate_and_link()
- [Medium] KEYS: dangling module lockdep class key on key->sem after
retyping
--
commit 5aa637683f50c613cd3d30c6ae75645d9241a024
Author: Guicai Ma <maguicai@kylinos.cn>
KEYS: Protect the instantiate method against type removal
This commit takes key->sem for reading across key_instantiate_and_link()
and checks for a non-NULL ->instantiate method to prevent racing with
key type unregistration and garbage collection.
> diff --git a/security/keys/key.c b/security/keys/key.c
> index a438c4508595d..1a2025220196e 100644
> --- a/security/keys/key.c
> +++ b/security/keys/key.c
[ ... ]
> @@ -507,6 +509,20 @@ int key_instantiate_and_link(struct key *key,
> int ret;
>
> memset(&prep, 0, sizeof(prep));
> +
> + /*
> + * A reference on the key does not pin the module that provides its
> + * type. unregister_key_type() waits for the garbage collector to
> + * retype every remaining key of the type to key_type_dead, whose
> + * ->instantiate is NULL, so the type must not change from here to the
> + * instantiate call.
> + */
> + down_read(&key->sem);
[Severity: High]
Does taking key->sem before __key_link_lock() and __key_instantiate_and_link()
introduce an ABBA lock ordering inversion with encrypted_instantiate()?
When a user or trusted key is instantiated, key_instantiate_and_link() now
holds key->sem (key_type_user.lock_class or key_type_trusted.lock_class)
before taking keyring->sem and key_construction_mutex. Conversely, when an
encrypted key is instantiated, encrypted_instantiate() runs under
keyring->sem and key_construction_mutex and locks the master user or
trusted key's semaphore via request_master_key():
CPU0 CPU1
---- ----
down_read(&ukey->sem)
mutex_lock(&key_construction_mutex)
mutex_lock(&key_construction_mutex)
down_read(&ukey->sem)
*** DEADLOCK ***
CPU0: key_instantiate_and_link() -> __key_instantiate_and_link()
CPU1: __key_instantiate_and_link() -> encrypted_instantiate() -> ... ->
request_user_key()
Under CONFIG_PROVE_LOCKING this triggers a circular locking dependency
splat, and at runtime it can deadlock if a writer (such as key_update() or
key_set_timeout()) is queued on ukey->sem.
> +
> + ret = -EOPNOTSUPP;
> + if (!key->type->instantiate)
> + goto out;
[Severity: Medium]
Should this also reject keys that have KEY_FLAG_DEAD set?
During unregister_key_type(), key_garbage_collector() runs three passes:
pass 1 sets KEY_FLAG_DEAD without taking key->sem or changing key->type,
pass 2 unlinks KEY_FLAG_DEAD keys from all keyrings without taking
key->sem, and only pass 3 takes down_write(&key->sem) and switches
key->type to &key_type_dead.
CPU0 (key_garbage_collector) CPU1 (key_instantiate_and_link)
pass 1:
set_bit(KEY_FLAG_DEAD, &key->flags)
pass 2:
keyring_gc()
down_read(&key->sem)
/* key->type->instantiate is still valid */
__key_instantiate_and_link()
__key_link(keyring, key, &_edit)
up_read(&key->sem)
pass 3:
down_write(&key->sem)
key->type = &key_type_dead <-- mutates index_key.type while linked
up_write(&key->sem)
Because key->type aliases key->index_key.type in struct key, mutating it in
place while the key remains linked in keyring->keys corrupts the keyring's
assoc_array tree and leaves a dead key linked in the keyring.
> +
> prep.orig_description = key->description;
> prep.data = data;
> prep.datalen = datalen;
> @@ -515,17 +531,17 @@ int key_instantiate_and_link(struct key *key,
> if (key->type->preparse) {
> ret = key->type->preparse(&prep);
> if (ret < 0)
> - goto error;
> + goto out_free_preparse;
> }
>
> if (keyring) {
> ret = __key_link_lock(keyring, &key->index_key);
> if (ret < 0)
> - goto error;
> + goto out_free_preparse;
>
> ret = __key_link_begin(keyring, &key->index_key, &edit);
> if (ret < 0)
> - goto error_link_end;
> + goto out_link_end;
>
> if (keyring->restrict_link && keyring->restrict_link->check) {
> struct key_restriction *keyres = keyring->restrict_link;
> @@ -533,19 +549,22 @@ int key_instantiate_and_link(struct key *key,
> ret = keyres->check(keyring, key->type, &prep.payload,
> keyres->key);
> if (ret < 0)
> - goto error_link_end;
> + goto out_link_end;
> }
> }
>
> ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
>
> -error_link_end:
> +out_link_end:
> if (keyring)
> __key_link_end(keyring, &key->index_key, edit);
>
> -error:
> +out_free_preparse:
> if (key->type->preparse)
> key->type->free_preparse(&prep);
> +
> +out:
> + up_read(&key->sem);
[Severity: Medium]
Can locking key->sem after a modular key type has been unloaded trigger a
lockdep unlock imbalance warning?
key_alloc() binds key->sem to &type->lock_class in the module's data
section, and key_garbage_collector() does not rebind key->sem to
&key_type_dead.lock_class when retyping the key in destroy_dead_key:
security/keys/gc.c:key_garbage_collector() {
...
destroy_dead_key:
down_write(&key->sem);
key->type = &key_type_dead;
if (key_gc_dead_keytype->destroy)
key_gc_dead_keytype->destroy(key);
memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
up_write(&key->sem);
}
Once the module is freed, lockdep_free_key_range() zaps &type->lock_class.
When KEYCTL_INSTANTIATE later runs on the still-referenced uninstantiated
key, down_read(&key->sem) sees the dangling key->sem.dep_map.key pointer in
__lock_acquire() -> register_lock_class(), which fails static_obj() and
returns NULL without recording the lock in current->held_locks, causing
up_read(&key->sem) to hit print_unlock_imbalance_bug().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124053303.940216-1-maguicai@kylinos.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 5:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 5:33 [PATCH] KEYS: Protect the instantiate method against type removal Guicai Ma
2026-11-24 5:54 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox