Linux Security Modules development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Guicai Ma" <maguicai@kylinos.cn>,
	"David Howells" <dhowells@redhat.com>,
	"Jarkko Sakkinen" <jarkko@kernel.org>
Cc: keyrings@vger.kernel.org, linux-security-module@vger.kernel.org,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E . Hallyn" <serge@hallyn.com>
Subject: Re: [PATCH] KEYS: Protect the instantiate method against type removal
Date: Tue, 24 Nov 2026 05:54:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-230565@kernel.org> (raw)
In-Reply-To: <20261124053303.940216-1-maguicai@kylinos.cn>

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

      reply	other threads:[~2026-11-24  5:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  5:33 [PATCH] KEYS: Protect the instantiate method against type removal Guicai Ma
2026-11-24  5:54 ` sashiko-bot [this message]

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=sashiko-outbox-230565@kernel.org \
    --to=sashiko-bot@kernel.org \
    --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=maguicai@kylinos.cn \
    --cc=paul@paul-moore.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.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