From: Jarkko Sakkinen <jarkko@kernel.org>
To: Guicai Ma <maguicai@kylinos.cn>,
Linus Torvalds <torvalds@linux-foundation.org>
Cc: David Howells <dhowells@redhat.com>,
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,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] KEYS: Protect key instantiation and rejection against type removal
Date: Fri, 27 Nov 2026 15:26:11 +0200 [thread overview]
Message-ID: <awmE8ykDdfSqPxgw@kernel.org> (raw)
In-Reply-To: <awhb3uC83WDzQup3@kernel.org>
On Thu, Nov 26, 2026 at 06:18:38PM +0200, Jarkko Sakkinen wrote:
> On Wed, Nov 25, 2026 at 09:39:20PM -0500, Guicai Ma wrote:
> > key_instantiate_and_link() and key_reject_and_link() access key->type
> > and link keys without synchronizing against unregister_key_type(). If
> > the type is removed during construction, key_instantiate_and_link() can
> > call key_type_dead.instantiate, which is NULL, and either function can
> > link a dead key into the keyring before GC pass 3 retypes it.
> >
> > key->sem cannot be used here because it inverts lock order with
> > encrypted_instantiate() and uses a freed lockdep class after a modular
> > type unloads, while key_types_sem may be held and methods may sleep.
> > Guard both paths with key_type_srcu and a KEY_FLAG_DEAD check, waiting
> > with synchronize_srcu(&key_type_srcu) after GC pass 1; for a dead key in
> > key_reject_and_link(), just wake waiters and invalidate @authkey under
> > key_construction_mutex, leaving the key uninstantiated.
> >
> > Fixes: 0c061b5707ab ("KEYS: Correctly destroy key payloads when their keytype is removed")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Guicai Ma <maguicai@kylinos.cn>
> > ---
> > v4:
> > - Rebase onto jarkko/master.
> > - In key_reject_and_link(), handle KEY_FLAG_DEAD under
> > key_construction_mutex alone without taking key->sem or changing
> > key->state, and document the result in the kerneldoc.
> >
> > v3:
> > - Shorten the commit message and focus on why SRCU is used.
> > - Change Fixes tag to 0c061b5707ab.
> >
> > v2:
> > - Switch from key->sem to key_type_srcu and check KEY_FLAG_DEAD in
> > key_instantiate_and_link() and key_reject_and_link().
> >
> > security/keys/gc.c | 13 ++++
> > security/keys/internal.h | 2 +
> > security/keys/key.c | 160 ++++++++++++++++++++++++++++-----------
> > 3 files changed, 129 insertions(+), 46 deletions(-)
>
> It's unfortunate that the fix needs to be this complicated but as we've
> looked this through, also with failed alternatives, I still think this
> is a feasible fix.
>
> And it's good to now before I even consider moving forward with my
> keyring GC overhaul, which ties keys to their key type's LKM, and
> removes racy key types altogether.
>
> Linus, I'll give a forewarning on this fix because is not every day
> thing to introduce SRCU in stable fixes.
>
>
> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
I rebased my series on top of this and yeah, it will be erased by the
last commit:
https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git/commit/?h=keyring_gc&id=5028b0336d35f14981116926d9ee5e131ff7c7ed
That does not make the fix less important. On contrary, it is good to
have it in place for stable kernels.
Br, JArkko
prev parent reply other threads:[~2026-11-27 13:26 UTC|newest]
Thread overview: 12+ 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
2026-11-25 22:45 ` Jarkko Sakkinen
2026-11-25 17:19 ` Jarkko Sakkinen
2026-11-26 2:39 ` [PATCH v4] " Guicai Ma
2026-11-26 16:18 ` Jarkko Sakkinen
2026-11-27 13:26 ` Jarkko Sakkinen [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=awmE8ykDdfSqPxgw@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 \
--cc=torvalds@linux-foundation.org \
/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