From: Sinan Turunc <enscrpt@pm.me>
To: Jarkko Sakkinen <jarkko@kernel.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, stable@vger.kernel.org
Subject: Re: [PATCH] keys: serialise instantiation against keyctl_chown_key
Date: Tue, 17 Nov 2026 12:31:30 +0000 [thread overview]
Message-ID: <20261117123056.1797-1-enscrpt@pm.me> (raw)
In-Reply-To: <avmoe41hNwe5GryN@kernel.org>
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
next prev parent reply other threads:[~2026-11-17 12:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-15 12:17 Sinan Turunc
2026-11-15 12:30 ` Jarkko Sakkinen
2026-11-15 12:33 ` Jarkko Sakkinen
2026-11-15 12:36 ` Jarkko Sakkinen
2026-11-15 12:41 ` Jarkko Sakkinen
2026-11-17 12:31 ` Sinan Turunc [this message]
2026-11-19 7:00 ` [syzbot ci] " syzbot ci
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=20261117123056.1797-1-enscrpt@pm.me \
--to=enscrpt@pm.me \
--cc=dhowells@redhat.com \
--cc=jarkko@kernel.org \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.com \
--cc=stable@vger.kernel.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
all inboxes | Powered by JetHome®