[PATCH] KEYS: Protect the instantiate method against type removal
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Tue Nov 24 05:54:33 UTC 2026
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 at 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
More information about the Linux-security-module-archive
mailing list