Re: [PATCH v2] KEYS: Protect key instantiation and rejection against type removal
From: Jarkko Sakkinen
Date: Tue Nov 24 2026 - 11:27:27 EST
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