Re: [PATCH] keys: serialise instantiation against keyctl_chown_key

From
Sinan Turunc <hidden>
Date
2026-11-17 12:31:36
Also in
keyrings, lkml, stable
Reply to
Jarkko Sakkinen, 2026-11-15
Hi Jarkko,

Sorry for the sloppy v1 patch and for over-relying on an LLM to write
the commit message. That will not happen again. I have removed the
inline comments and rewritten the commit message in my own words.
Please let me know if any part of it is unclear, as I have tried to
describe it as clearly as I can.

The race happens because keyctl_chown_key() looks up the target key by
calling lookup_user_key() with KEY_LOOKUP_PARTIAL. That flag lets a
caller with CAP_SYS_ADMIN change the UID of an uninstantiated key during
construction. When the UID changes, keyctl_chown_key() swaps key->user
under key->sem and drops the old reference with key_user_put().
__key_instantiate_and_link() and key_reject_and_link() only hold
key_construction_mutex when they increment key->user->nikeys during
construction. __key_instantiate_and_link() also calls the key type
instantiate callback without holding key->sem on the key.

In key_payload_reserve(), when the key is in quota and its size changes,
key->user is read without key->sem. It reads key->user once for
spin_lock_irqsave(&key->user->lock, flags) and reads it again for
spin_unlock_irqrestore(&key->user->lock, flags). If keyctl_chown_key()
replaces key->user between those two reads, key_payload_reserve()
unlocks the new spinlock and leaves the old spinlock held.

As the Sashiko review noted on v1, taking down_write_nested(&key->sem,
1) also causes a lockdep cycle against __key_move_lock(). A lockdep test
on v1 triggered that warning on the first KEYCTL_MOVE call against the
keyring instantiation order. __key_move_lock() takes subclass 1 before
keyring_serialise_link_lock, while __key_link_lock() takes
keyring_serialise_link_lock before __key_instantiate_and_link() takes
key_construction_mutex. In v2 I switched down_write_nested() to subclass
2, which is an unused subclass taken only inside key_construction_mutex.
That change ran clean under lockdep on both 7.4-rc2 and your
for-next-keys branch across our test workloads.

I will send the v2 patch out to the list right after this message.

Thanks,
Sinan
Thread (7 messages) 7 messages, 3 authors, last reply 11h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help