From: Jarkko Sakkinen <jarkko@kernel.org>
To: Guicai Ma <maguicai@kylinos.cn>
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 v2] KEYS: Protect key instantiation and rejection against type removal
Date: Tue, 24 Nov 2026 18:27:15 +0200 [thread overview]
Message-ID: <awW640zaT8Bnqd0-@kernel.org> (raw)
In-Reply-To: <20261124091243.1019217-1-maguicai@kylinos.cn>
On Tue, Nov 24, 2026 at 04:12:43AM -0500, Guicai Ma wrote:
> key_instantiate_and_link() reads key->type->def_datalen and ->preparse,
> passes key->type to restrict_link->check(), and invokes ->instantiate()
> and ->free_preparse() without synchronizing against key type removal.
> A reference on the key keeps the key object alive, but does not pin the
> module that provides its type.
>
> unregister_key_type() removes the type from key_types_list and waits for
> key_garbage_collector() to run three passes: pass 1 sets KEY_FLAG_DEAD
> and clears key->perm, pass 2 unlinks dead keys from keyrings, and pass 3
> takes key->sem for writing, retypes key->type to &key_type_dead, and
> destroys the payload. A key still under construction stays in
> KEY_IS_UNINSTANTIATED with a valid authorisation key, so an upcall
> helper calling KEYCTL_INSTANTIATE after pass 3 reaches
> __key_instantiate_and_link() and jumps to key_type_dead.instantiate,
> which is NULL:
>
> Unable to handle kernel NULL pointer dereference at
> virtual address 0000000000000000
> Internal error: Oops: 0000000086000006 [#1] SMP
> pc : 0x0
> lr : __key_instantiate_and_link+0xb8/0x2f8
> Call trace:
> 0x0 (P)
> key_instantiate_and_link+0x2e8/0x36c
> keyctl_instantiate_key_common+0x1cc/0x338
> keyctl_instantiate_key+0xbc/0x120
> __arm64_sys_keyctl+0x218/0x3f0
>
> If the module unloads while ->preparse() or ->instantiate() is running,
> execution can also return into freed module text or skip
> ->free_preparse(). While the request_key() syscall holds key_types_sem
> across the upcall, in-kernel request_key() callers do not, so
> unregister_key_type() can run before KEYCTL_INSTANTIATE or
> KEYCTL_REJECT.
>
> key_reject_and_link() shares the same window: it links the key into
> @keyring and calls key_set_expiry() (which reads key->type->flags)
> without checking KEY_FLAG_DEAD. Linking a dead key between GC pass 2 and
> pass 3 leaves it in the keyring's assoc_array while pass 3 overwrites
> key->type (which aliases key->index_key.type) in place.
>
> Neither key->sem nor key_types_sem can be taken here: holding key->sem
> across __key_link_lock() and __key_instantiate_and_link() inverts lock
> order against encrypted_instantiate() (which locks the master key's
> semaphore under keyring->sem and key_construction_mutex), and
> key_instantiate_and_link() is already called with key_types_sem held for
> reading (e.g. from request_key_auth_new()).
>
> Add key_type_srcu and call synchronize_srcu(&key_type_srcu) at the end
> of GC pass 1 (KEY_GC_REAPING_DEAD_1) whenever dead keys were marked.
> SRCU rather than RCU because ->preparse() and ->instantiate() may sleep.
> Hold key_type_srcu across the full body of key_instantiate_and_link()
> and key_reject_and_link() and check KEY_FLAG_DEAD:
>
> - Callers that observe !KEY_FLAG_DEAD finish all key->type accesses and
> any keyring linking before pass 2 unlinks dead keys and pass 3 retypes
> them to key_type_dead.
> - Callers entering after synchronize_srcu() observe KEY_FLAG_DEAD:
> key_instantiate_and_link() returns -EKEYREVOKED (matching
> key_validate()), and key_reject_and_link() skips linking (returning
> -EKEYREVOKED if @keyring was given) and key_set_expiry() while still
> completing negative instantiation so waiters wake up and @authkey is
> invalidated.
>
> Also return -EOPNOTSUPP in key_instantiate_and_link() if a live key type
> does not provide ->instantiate().
The commit message is incomprehensible.
Br, Jarkko
prev parent reply other threads:[~2026-11-24 16:27 UTC|newest]
Thread overview: 5+ 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 [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=awW640zaT8Bnqd0-@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