[PATCH v3] KEYS: Protect key instantiation and rejection against type removal

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 25 03:48:12 UTC 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KEYS: key_reject_and_link() locks key->sem with freed modular
  lockdep class
--

commit c70f356b6ccdc592f27c6c5e32e3b0a02a265eb1
Author: Guicai Ma <maguicai at kylinos.cn>

KEYS: Protect key instantiation and rejection against type removal

This commit guards key_instantiate_and_link() and key_reject_and_link()
with key_type_srcu and a KEY_FLAG_DEAD check, and waits with
synchronize_srcu(&key_type_srcu) after pass 1 of key_garbage_collector()
marks dead keys.

> diff --git a/security/keys/key.c b/security/keys/key.c
> index df31d1024340e..7f346e87a818c 100644
> --- a/security/keys/key.c
> +++ b/security/keys/key.c

[ ... ]

> @@ -591,22 +619,42 @@ int key_reject_and_link(struct key *key,
>  	awaken = 0;
>  	ret = -EBUSY;
>  
> +	/*
> +	 * Hold key_type_srcu across linking into @keyring and
> +	 * key_set_expiry() (which reads key->type->flags).  If the key is
> +	 * already marked dead, skip linking and expiry but still complete
> +	 * negative instantiation so waiters are woken and @authkey is
> +	 * invalidated.
> +	 */
> +	srcu_idx = srcu_read_lock(&key_type_srcu);
> +	dead = test_bit(KEY_FLAG_DEAD, &key->flags);
> +
>  	if (keyring) {
> -		link_ret = __key_link_lock(keyring, &key->index_key);
> -		if (link_ret == 0) {
> -			/*
> -			 * Check the restriction under the keyring semaphore.
> -			 * keyring_restrict() installs it while holding the
> -			 * semaphore, so testing it beforehand races with a
> -			 * concurrent restriction install.
> -			 */
> -			if (keyring->restrict_link) {
> -				__key_link_end(keyring, &key->index_key, edit);
> -				return -EPERM;
> +		if (dead) {
> +			link_ret = -EKEYREVOKED;
> +		} else {
> +			link_ret = __key_link_lock(keyring, &key->index_key);
> +			if (link_ret == 0) {
> +				/*
> +				 * Check the restriction under the keyring
> +				 * semaphore. keyring_restrict() installs it
> +				 * while holding the semaphore, so testing it
> +				 * beforehand races with a concurrent
> +				 * restriction install.
> +				 */
> +				if (keyring->restrict_link) {
> +					__key_link_end(keyring, &key->index_key,
> +						       edit);
> +					ret = -EPERM;
> +					goto out;
> +				}
> +				link_ret = __key_link_begin(keyring,
> +							    &key->index_key,
> +							    &edit);
> +				if (link_ret < 0)
> +					__key_link_end(keyring, &key->index_key,
> +						       edit);
>  			}
> -			link_ret = __key_link_begin(keyring, &key->index_key, &edit);
> -			if (link_ret < 0)
> -				__key_link_end(keyring, &key->index_key, edit);
>  		}
>  	}
>  
> @@ -619,7 +667,8 @@ int key_reject_and_link(struct key *key,
>  		atomic_inc(&key->user->nikeys);
>  		mark_key_instantiated(key, -error);
>  		notify_key(key, NOTIFY_KEY_INSTANTIATED, -error);
> -		key_set_expiry(key, ktime_get_real_seconds() + timeout);
> +		if (!dead)
> +			key_set_expiry(key, ktime_get_real_seconds() + timeout);

[Severity: Medium]
When dead is true, does key_reject_and_link() still lock key->sem after
the modular key type's lockdep class has been freed?

If GC pass 1 already set KEY_FLAG_DEAD before key_reject_and_link()
entered srcu_read_lock(&key_type_srcu), pass 1's synchronize_srcu() does
not wait for this call.  Pass 3 retypes key->type to &key_type_dead
without resetting key->sem's lockdep class away from &type->lock_class:

security/keys/gc.c:key_garbage_collector() {
    ...
destroy_dead_key:
	spin_unlock(&key_serial_lock);
	kdebug("destroy key %d", key->serial);
	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 unregister_key_type() returns and the module unloads,
complete_request_key() -> key_negate_and_link() -> key_reject_and_link()
(invoked after key_instantiate_and_link() returns -EKEYREVOKED, or via
keyctl_reject_key()) still runs down_write_nested(&key->sem, 2).  With
CONFIG_LOCKDEP enabled, looking up subclass 2 on the freed
&type->lock_class fails in register_lock_class(), and the paired
up_write(&key->sem) triggers an unlock imbalance warning and disables
lockdep.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125032605.1327954-1-maguicai@kylinos.cn?part=1



More information about the Linux-security-module-archive mailing list