* [PATCH v2] keys: serialise key instantiation against keyctl_chown_key()
@ 2026-11-17 12:34 Sinan Turunc
2026-11-17 12:49 ` Jarkko Sakkinen
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Sinan Turunc @ 2026-11-17 12:34 UTC (permalink / raw)
To: David Howells, Jarkko Sakkinen
Cc: paul, jmorris, serge, keyrings, linux-security-module,
linux-kernel, stable
keyctl_chown_key() looks up the target key by calling lookup_user_key()
with the KEY_LOOKUP_PARTIAL flag. 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() replaces key->user under key->sem
and drops the old reference with key_user_put().
__key_instantiate_and_link() and key_reject_and_link() hold
key_construction_mutex during instantiation, but neither function takes
key->sem. Both functions increment key->user->nikeys, and
__key_instantiate_and_link() also calls key->type->instantiate() without
holding key->sem. When a key is in quota and its size changes,
key_payload_reserve() reads key->user seven times without key->sem. Those
reads include spin_lock_irqsave(&key->user->lock, flags) at the start and
spin_unlock_irqrestore(&key->user->lock, flags) at the end.
If keyctl_chown_key() swaps key->user between those two calls,
key_payload_reserve() unlocks the new owner's spinlock and leaves the old
spinlock held. By source inspection, key_user_put() can also kfree() the
old struct key_user while instantiation still dereferences it. That struct
key_user use-after-free was found by reading the code and did not trigger
during our KASAN runs.
=====================================
WARNING: bad unlock balance detected!
7.4.0-rc2-g23c62304dde2 #3 Not tainted
-------------------------------------
request-key/163 is trying to release lock (&candidate->lock) at:
[<ffffffff82698fff>] key_payload_reserve+0x29f/0x370
but there are no more locks to release!
other info that might help us debug this:
locks held by request-key/163: 2, on CPU#3:
#0: ffffffff871c10a0 (key_construction_mutex){+.+.}-{4:4}, at: __key_instantiate_and_link+0x38/0x370
#1: ffff88800de4a8b8 (&candidate->lock){..-.}-{3:3}, at: key_payload_reserve+0x160/0x370
Call Trace:
lock_release+0x286/0x2e0
_raw_spin_unlock_irqrestore+0x1a/0x50
key_payload_reserve+0x29f/0x370
generic_key_instantiate+0x42/0x230
__key_instantiate_and_link+0xd7/0x370
key_instantiate_and_link+0x40d/0x4b0
keyctl_instantiate_key_common+0x2d2/0x450
keyctl_instantiate_key+0x167/0x180
__do_sys_keyctl+0x391/0x550
Take key->sem for write with down_write_nested(&key->sem, 2) inside
key_construction_mutex in __key_instantiate_and_link() and
key_reject_and_link(). Subclass 2 is an unused lockdep subclass that is
only ever taken inside key_construction_mutex. Subclasses 0 and 1 are
already taken outside keyring_serialise_link_lock by __key_link_lock() and
__key_move_lock().
An AI tool assisted with finding the race, writing the four-line fix and
the reproducer, and running the tests. I reviewed the kernel source and the
test logs and wrote this commit message myself.
Fixes: 5801649d8b83 ("[PATCH] keys: let keyctl_chown() change a key's owner")
Cc: stable@vger.kernel.org
Signed-off-by: Sinan Turunc <enscrpt@pm.me>
---
v2
Drop the inline comment changes from v1 and rewrite the commit message in
my own words.
Use subclass 2 in down_write_nested(&key->sem, 2) so instantiation cannot
form a lockdep cycle with __key_move_lock().
Update the Fixes tag from the initial git import commit to commit
5801649d8b83.
Tested with the reproducer and the lockdep stress workload on 7.4-rc2
(23c62304dde2) and on for-next-keys (1dbbde3bc4).
security/keys/key.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/security/keys/key.c b/security/keys/key.c
index 3f1f0fc..df31d10 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -441,6 +441,7 @@ static int __key_instantiate_and_link(struct key *key,
ret = -EBUSY;
mutex_lock(&key_construction_mutex);
+ down_write_nested(&key->sem, 2);
/* can't instantiate twice */
if (key->state == KEY_IS_UNINSTANTIATED) {
@@ -473,6 +474,7 @@ static int __key_instantiate_and_link(struct key *key,
}
}
+ up_write(&key->sem);
mutex_unlock(&key_construction_mutex);
/* wake up anyone waiting for a key to be constructed */
@@ -609,6 +611,7 @@ int key_reject_and_link(struct key *key,
}
mutex_lock(&key_construction_mutex);
+ down_write_nested(&key->sem, 2);
/* can't instantiate twice */
if (key->state == KEY_IS_UNINSTANTIATED) {
@@ -632,6 +635,7 @@ int key_reject_and_link(struct key *key,
key_invalidate(authkey);
}
+ up_write(&key->sem);
mutex_unlock(&key_construction_mutex);
if (keyring && link_ret == 0)
--
2.56.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] keys: serialise key instantiation against keyctl_chown_key()
2026-11-17 12:34 [PATCH v2] keys: serialise key instantiation against keyctl_chown_key() Sinan Turunc
@ 2026-11-17 12:49 ` Jarkko Sakkinen
2026-11-17 12:52 ` Jarkko Sakkinen
2026-11-17 14:53 ` Jarkko Sakkinen
2026-11-17 16:21 ` Jarkko Sakkinen
2 siblings, 1 reply; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 12:49 UTC (permalink / raw)
To: Sinan Turunc
Cc: David Howells, paul, jmorris, serge, keyrings,
linux-security-module, linux-kernel, stable
On Tue, Nov 17, 2026 at 12:34:12PM +0000, Sinan Turunc wrote:
> keyctl_chown_key() looks up the target key by calling lookup_user_key()
> with the KEY_LOOKUP_PARTIAL flag. 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() replaces key->user under key->sem
> and drops the old reference with key_user_put().
>
> __key_instantiate_and_link() and key_reject_and_link() hold
> key_construction_mutex during instantiation, but neither function takes
> key->sem. Both functions increment key->user->nikeys, and
> __key_instantiate_and_link() also calls key->type->instantiate() without
> holding key->sem. When a key is in quota and its size changes,
> key_payload_reserve() reads key->user seven times without key->sem. Those
> reads include spin_lock_irqsave(&key->user->lock, flags) at the start and
> spin_unlock_irqrestore(&key->user->lock, flags) at the end.
>
> If keyctl_chown_key() swaps key->user between those two calls,
> key_payload_reserve() unlocks the new owner's spinlock and leaves the old
> spinlock held. By source inspection, key_user_put() can also kfree() the
> old struct key_user while instantiation still dereferences it. That struct
> key_user use-after-free was found by reading the code and did not trigger
> during our KASAN runs.
OK now this is stating to make more sense. Good job.
>
> =====================================
> WARNING: bad unlock balance detected!
> 7.4.0-rc2-g23c62304dde2 #3 Not tainted
> -------------------------------------
> request-key/163 is trying to release lock (&candidate->lock) at:
> [<ffffffff82698fff>] key_payload_reserve+0x29f/0x370
> but there are no more locks to release!
>
> other info that might help us debug this:
> locks held by request-key/163: 2, on CPU#3:
> #0: ffffffff871c10a0 (key_construction_mutex){+.+.}-{4:4}, at: __key_instantiate_and_link+0x38/0x370
> #1: ffff88800de4a8b8 (&candidate->lock){..-.}-{3:3}, at: key_payload_reserve+0x160/0x370
>
> Call Trace:
> lock_release+0x286/0x2e0
> _raw_spin_unlock_irqrestore+0x1a/0x50
> key_payload_reserve+0x29f/0x370
> generic_key_instantiate+0x42/0x230
> __key_instantiate_and_link+0xd7/0x370
> key_instantiate_and_link+0x40d/0x4b0
> keyctl_instantiate_key_common+0x2d2/0x450
> keyctl_instantiate_key+0x167/0x180
> __do_sys_keyctl+0x391/0x550
>
> Take key->sem for write with down_write_nested(&key->sem, 2) inside
> key_construction_mutex in __key_instantiate_and_link() and
> key_reject_and_link(). Subclass 2 is an unused lockdep subclass that is
> only ever taken inside key_construction_mutex. Subclasses 0 and 1 are
> already taken outside keyring_serialise_link_lock by __key_link_lock() and
> __key_move_lock().
>
> An AI tool assisted with finding the race, writing the four-line fix and
> the reproducer, and running the tests. I reviewed the kernel source and the
> test logs and wrote this commit message myself.
Irrelevant in the commit message as metaphorically it is now a tool just
like text editor is a tool.
I can write a horrific 4chan post with a text editor but if I send it to
LKML and call it a kernel patch, I don't make claims that it was assisted
by a text editor and reviewed by me.
But yeah, I get the commit message better now, thank you.
>
> Fixes: 5801649d8b83 ("[PATCH] keys: let keyctl_chown() change a key's owner")
> Cc: stable@vger.kernel.org
> Signed-off-by: Sinan Turunc <enscrpt@pm.me>
> ---
> v2
> Drop the inline comment changes from v1 and rewrite the commit message in
> my own words.
> Use subclass 2 in down_write_nested(&key->sem, 2) so instantiation cannot
> form a lockdep cycle with __key_move_lock().
> Update the Fixes tag from the initial git import commit to commit
> 5801649d8b83.
> Tested with the reproducer and the lockdep stress workload on 7.4-rc2
> (23c62304dde2) and on for-next-keys (1dbbde3bc4).
>
> security/keys/key.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/security/keys/key.c b/security/keys/key.c
> index 3f1f0fc..df31d10 100644
> --- a/security/keys/key.c
> +++ b/security/keys/key.c
> @@ -441,6 +441,7 @@ static int __key_instantiate_and_link(struct key *key,
> ret = -EBUSY;
>
> mutex_lock(&key_construction_mutex);
> + down_write_nested(&key->sem, 2);
>
> /* can't instantiate twice */
> if (key->state == KEY_IS_UNINSTANTIATED) {
> @@ -473,6 +474,7 @@ static int __key_instantiate_and_link(struct key *key,
> }
> }
>
> + up_write(&key->sem);
> mutex_unlock(&key_construction_mutex);
>
> /* wake up anyone waiting for a key to be constructed */
> @@ -609,6 +611,7 @@ int key_reject_and_link(struct key *key,
> }
>
> mutex_lock(&key_construction_mutex);
> + down_write_nested(&key->sem, 2);
>
> /* can't instantiate twice */
> if (key->state == KEY_IS_UNINSTANTIATED) {
> @@ -632,6 +635,7 @@ int key_reject_and_link(struct key *key,
> key_invalidate(authkey);
> }
>
> + up_write(&key->sem);
> mutex_unlock(&key_construction_mutex);
>
> if (keyring && link_ret == 0)
> --
> 2.56.0
>
I think this is right but I will re-check once I have bandwith.
Thanks for addressing my complains.
Br, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] keys: serialise key instantiation against keyctl_chown_key()
2026-11-17 12:49 ` Jarkko Sakkinen
@ 2026-11-17 12:52 ` Jarkko Sakkinen
0 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 12:52 UTC (permalink / raw)
To: Sinan Turunc
Cc: David Howells, paul, jmorris, serge, keyrings,
linux-security-module, linux-kernel, stable
On Tue, Nov 17, 2026 at 02:50:03PM +0200, Jarkko Sakkinen wrote:
> > An AI tool assisted with finding the race, writing the four-line fix and
> > the reproducer, and running the tests. I reviewed the kernel source and the
> > test logs and wrote this commit message myself.
>
> Irrelevant in the commit message as metaphorically it is now a tool just
> like text editor is a tool.
>
> I can write a horrific 4chan post with a text editor but if I send it to
> LKML and call it a kernel patch, I don't make claims that it was assisted
> by a text editor and reviewed by me.
>
> But yeah, I get the commit message better now, thank you.
And on softer side: making a mistake is not a mistake ;-)
This is all about standing behind whatever you submit; not so much about
"performance". I make mistakes all the time.
Br, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] keys: serialise key instantiation against keyctl_chown_key()
2026-11-17 12:34 [PATCH v2] keys: serialise key instantiation against keyctl_chown_key() Sinan Turunc
2026-11-17 12:49 ` Jarkko Sakkinen
@ 2026-11-17 14:53 ` Jarkko Sakkinen
2026-11-17 16:21 ` Jarkko Sakkinen
2 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 14:53 UTC (permalink / raw)
To: Sinan Turunc
Cc: David Howells, paul, jmorris, serge, keyrings,
linux-security-module, linux-kernel, stable
On Tue, Nov 17, 2026 at 12:34:12PM +0000, Sinan Turunc wrote:
> diff --git a/security/keys/key.c b/security/keys/key.c
> index 3f1f0fc..df31d10 100644
> --- a/security/keys/key.c
> +++ b/security/keys/key.c
> @@ -441,6 +441,7 @@ static int __key_instantiate_and_link(struct key *key,
> ret = -EBUSY;
>
Hold on a moment. I have 1/2 for this.
I was going to make it as a review comment but it is separate bug.
I'll response and attach my fix as soon as I get it done, and after
that you should send a two-patch patch set with my patch as 1/2, and
yours as 2/2.
Br, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] keys: serialise key instantiation against keyctl_chown_key()
2026-11-17 12:34 [PATCH v2] keys: serialise key instantiation against keyctl_chown_key() Sinan Turunc
2026-11-17 12:49 ` Jarkko Sakkinen
2026-11-17 14:53 ` Jarkko Sakkinen
@ 2026-11-17 16:21 ` Jarkko Sakkinen
2026-11-17 16:23 ` Jarkko Sakkinen
2 siblings, 1 reply; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 16:21 UTC (permalink / raw)
To: Sinan Turunc
Cc: David Howells, paul, jmorris, serge, keyrings,
linux-security-module, linux-kernel, stable
On Tue, Nov 17, 2026 at 12:34:12PM +0000, Sinan Turunc wrote:
> Subclass 2 is an unused lockdep subclass that is
> only ever taken inside key_construction_mutex. Subclasses 0 and 1 are
> already taken outside keyring_serialise_link_lock by __key_link_lock() and
> __key_move_lock().
Delete also this as there was no bug associated with nesting levels.
And this does not need additional fixes despite I forewarned about that.
I'll wait for v3.
Br, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] keys: serialise key instantiation against keyctl_chown_key()
2026-11-17 16:21 ` Jarkko Sakkinen
@ 2026-11-17 16:23 ` Jarkko Sakkinen
0 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 16:23 UTC (permalink / raw)
To: Sinan Turunc
Cc: David Howells, paul, jmorris, serge, keyrings,
linux-security-module, linux-kernel, stable
On Tue, Nov 17, 2026 at 06:21:43PM +0200, Jarkko Sakkinen wrote:
> On Tue, Nov 17, 2026 at 12:34:12PM +0000, Sinan Turunc wrote:
> > Subclass 2 is an unused lockdep subclass that is
> > only ever taken inside key_construction_mutex. Subclasses 0 and 1 are
> > already taken outside keyring_serialise_link_lock by __key_link_lock() and
> > __key_move_lock().
>
> Delete also this as there was no bug associated with nesting levels.
>
> And this does not need additional fixes despite I forewarned about that.
>
> I'll wait for v3.
"Take key->sem for write with down_write_nested(&key->sem, 2) inside
key_construction_mutex in __key_instantiate_and_link() and
key_reject_and_link()."
This sentence tells everything about nesting that this patch requires.
Br, Jarkko
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-17 16:23 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 12:34 [PATCH v2] keys: serialise key instantiation against keyctl_chown_key() Sinan Turunc
2026-11-17 12:49 ` Jarkko Sakkinen
2026-11-17 12:52 ` Jarkko Sakkinen
2026-11-17 14:53 ` Jarkko Sakkinen
2026-11-17 16:21 ` Jarkko Sakkinen
2026-11-17 16:23 ` Jarkko Sakkinen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox