From: Jarkko Sakkinen <jarkko@kernel.org>
To: Guicai Ma <maguicai@kylinos.cn>
Cc: keyrings@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org,
David Howells <dhowells@redhat.com>,
James Morris <jmorris@namei.org>,
Paul Moore <paul@paul-moore.com>,
"Serge E. Hallyn" <serge@hallyn.com>
Subject: Re: [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link()
Date: Wed, 25 Nov 2026 01:24:13 +0200 [thread overview]
Message-ID: <awYcnWU4I8vYGBaQ@kernel.org> (raw)
In-Reply-To: <20261124230016.1211738-1-maguicai@kylinos.cn>
On Tue, Nov 24, 2026 at 06:00:16PM -0500, Guicai Ma wrote:
> On Tue, 24 Nov 2026 19:40:35 +0200, Jarkko Sakkinen wrote:
> > @@ -610,11 +628,12 @@ int key_reject_and_link(struct key *key,
> > }
> > }
> >
> > - mutex_lock(&key_construction_mutex);
> > down_write_nested(&key->sem, 2);
> >
> > /* can't instantiate twice */
> > if (key->state == KEY_IS_UNINSTANTIATED) {
> > + mutex_lock(&key_construction_mutex);
>
> Here __key_link_begin() still runs before key->sem is taken and there is
> no KEY_FLAG_DEAD check, so the link path is still taken: on 7.4-rc3 with
> KASAN and PROVE_LOCKING, KEYCTL_REJECT with a target keyring on a dead
> key returns 0 instead of -EKEYREVOKED. If pass 3 retypes the key before
> key->sem is taken, __key_link() inserts it with an edit built from the
> old type.
>
> Taking key->sem before __key_link_begin() and skipping the link with
> link_ret = -EKEYREVOKED when KEY_FLAG_DEAD is set avoids that, and still
> lets the negative instantiation finish so request_key() waiters wake up.
>
> In key_instantiate_and_link():
> > + down_write_nested(&key->sem, 2);
> > +
> > + if (test_bit(KEY_FLAG_DEAD, &key->flags)) {
> > + ret = -EKEYREVOKED;
> > + goto out_unlock;
> > + }
>
> After a modular type is unloaded, key->sem still carries the module's
> lock_class_key, which lockdep_free_key_range() dropped. In the post-rmmod
> instantiate test up_write() here warns "trying to release lock
> (&type->lock_class) ... no more locks to release". Module pinning would
> remove this; it still matters for stable backports.
>
> The 200-round instantiate/reject race and encrypted-key instantiation
> pass.
I'm with you on backports part of course, and apparently you are right
what you're saying. Thanks for pointing this out.
Addin SCRU just feels a bit heavy measure but the concurrency case is
also somewhat nasty. But in the end of the day it's only +10 LOC and
I understand why it works, and I also under why this does not :-)
It was not that much more complicated. Can you put the commit message
more brief and exactly put emphasis on SCRU part?
>
> Thanks,
> Guicai
Br, Jarkko
next prev parent reply other threads:[~2026-11-24 23:24 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 17:40 [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link() Jarkko Sakkinen
2026-11-24 17:44 ` Jarkko Sakkinen
2026-11-24 23:00 ` Guicai Ma
2026-11-24 23:24 ` Jarkko Sakkinen [this message]
2026-11-24 23:38 ` Jarkko Sakkinen
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=awYcnWU4I8vYGBaQ@kernel.org \
--to=jarkko@kernel.org \
--cc=dhowells@redhat.com \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=maguicai@kylinos.cn \
--cc=paul@paul-moore.com \
--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