From: sashiko-bot@kernel.org
To: "Guicai Ma" <maguicai@kylinos.cn>,
"David Howells" <dhowells@redhat.com>,
"Jarkko Sakkinen" <jarkko@kernel.org>
Cc: "Paul Moore" <paul@paul-moore.com>,
"James Morris" <jmorris@namei.org>,
"Serge E . Hallyn" <serge@hallyn.com>,
keyrings@vger.kernel.org, linux-security-module@vger.kernel.org
Subject: Re: [PATCH v3] KEYS: Protect key instantiation and rejection against type removal
Date: Wed, 25 Nov 2026 03:48:12 +0000 [thread overview]
Message-ID: <sashiko-outbox-232506@kernel.org> (raw)
In-Reply-To: <20261125032605.1327954-1-maguicai@kylinos.cn>
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@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
prev parent reply other threads:[~2026-11-25 3:48 UTC|newest]
Thread overview: 7+ 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
2026-11-24 9:12 ` [PATCH v2] KEYS: Protect key instantiation and rejection " Guicai Ma
2026-11-24 9:25 ` sashiko-bot
2026-11-24 16:27 ` Jarkko Sakkinen
2026-11-25 3:26 ` [PATCH v3] " Guicai Ma
2026-11-25 3:48 ` 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-232506@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