KEYS: Protect user key quota access during instantiation
Subsystems (2)
PATCH 1/1 KEYS: Protect user key quota access during instantiation
The owner account must remain alive throughout payload quota reservation. The user key type calls generic_key_instantiate(), which dereferences key->user in key_payload_reserve(), without holding key->sem. The construction mutex excludes other instantiators but does not exclude keyctl_chown_key().
A kernel caller can allocate an in-quota user key, retain its reference and instantiate it with a nonempty payload after publishing its serial. A concurrent task with Setattr permission and CAP_SYS_ADMIN can chown the still-uninstantiated key through KEY_LOOKUP_PARTIAL. If the key holds the last reference to its non-root account, instantiation has no authorization key, and the new owner has sufficient quota, this ordering is possible:
Instantiator Chown task
key_instantiate_and_link() keyctl_chown_key()
lock key_construction_mutex down_write(key->sem)
generic_key_instantiate()
key_payload_reserve()
load old = key->user
transfer quota to new owner
replace key->user
up_write(key->sem)
key_put(key)
key_user_put(old)
kfree(old)
read old->uidChown's final free falls between the old pointer load and the uid read. The read can therefore access freed struct key_user storage despite the caller retaining the key itself. Hold the read side of key->sem in a local user instantiation callback across generic_key_instantiate(). Chown then cannot replace and release the account until the quota accesses finish. If chown goes first, instantiation takes the semaphore and uses the new account instead.
Keep locking out of the shared helper, which is also called by big_key_update() with key->sem write-locked.
KASAN report as below:
BUG: KASAN: slab-use-after-free in key_payload_reserve+0x54d/0x560
---
security/keys/user_defined.c +15 -1
@@ -15,6 +15,7 @@#include "internal.h" static int logon_vet_description (const char *desc); +static int user_instantiate (struct key *key,struct key_preparsed_payload *prep);/* * user defined keys take an arbitrary string as the description and an
@@ -24,7 +25,7 @@ struct key_type key_type_user = { .name = "user", .preparse = user_preparse, .free_preparse = user_free_preparse, - .instantiate = generic_key_instantiate, + .instantiate = user_instantiate, .update = user_update, .revoke = user_revoke, .destroy = user_destroy,
@@ -86,6 +87,19 @@ void user_free_preparse(struct key_preparsed_payload *prep) }EXPORT_SYMBOL_GPL (user_free_preparse); +/* +* Keep the owner account stable while reserving quota during instantiation. +*/ +static int user_instantiate (struct key *key,struct key_preparsed_payload *prep) +{ +int ret; + +down_read (&key->sem); + ret =generic_key_instantiate (key, prep); +up_read (&key->sem); +return ret; +} +static void user_free_payload_rcu (struct rcu_head *head) {struct user_key_payload *payload;
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
The owner account must remain alive throughout payload quota reservation. The user key type calls
generic_key_instantiate(), which dereferenceskey->userinkey_payload_reserve(), without holdingkey->sem. The construction mutex excludes other instantiators but does not excludekeyctl_chown_key().A kernel caller can allocate an in-quota user key, retain its reference and instantiate it with a nonempty payload after publishing its serial. A concurrent task with Setattr permission and
CAP_SYS_ADMINcan chown the still-uninstantiated key throughKEY_LOOKUP_PARTIAL. If the key holds the last reference to its non-root account, instantiation has no authorization key, and the new owner has sufficient quota, this ordering is possible:
Quoted 7 lines
Instantiator Chown task
[ ... ]BUG: KASAN: slab-use-after-free in key_payload_reserve+0x54d/0x560Fixes 5801649d8b83 ("[PATCH] keys: let keyctl_chown() change a key's owner")Signed-off-by Cen Zhang <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/pm-key-management-objects-candidate-0001-v6-9876bf3e2116d47c065e@gmail.com?part=1
Jarkko Sakkinen
maintainerQuoted 5 lines · Cen Zhang
The owner account must remain alive throughout payload quota reservation. The user key type calls
generic_key_instantiate(), which dereferenceskey->userinkey_payload_reserve(), without holdingkey->sem. The construction mutex excludes other instantiators but does not excludekeyctl_chown_key().
I'm not following the term "owner account". It is neither an actor in the concurrency scenario. What do you exactly mean by it?
Quoted 23 lines
A kernel caller can allocate an in-quota user key, retain its reference and instantiate it with a nonempty payload after publishing its serial. A concurrent task with Setattr permission and
CAP_SYS_ADMINcan chown the still-uninstantiated key throughKEY_LOOKUP_PARTIAL. If the key holds the last reference to its non-root account, instantiation has no authorization key, and the new owner has sufficient quota, this ordering is possible:Instantiator Chown task
key_instantiate_and_link()keyctl_chown_key()lock key_construction_mutex down_write(key->sem) generic_key_instantiate() key_payload_reserve() load old = key->user transfer quota to new owner replace key->user up_write(key->sem) key_put(key) key_user_put(old) kfree(old) read old->uid
Quoted 7 lines
Chown's final free falls between the old pointer load and the uid read. The read can therefore access freed struct
key_userstorage despite the caller retaining the key itself. Hold the read side ofkey->semin a local user instantiation callback acrossgeneric_key_instantiate(). Chown then cannot replace and release the account until the quota accesses finish. If chown goes first, instantiation takes the semaphore and uses the new account instead.
And account gets repeated here. I'm not sure hat accounts we are talking about here.
I.e.
1. I get the scenario.
2. I read all of the text but I do not understand what I'm reading.
Quoted 3 lines
Keep locking out of the shared helper, which is also called by
big_key_update()withkey->semwrite-locked.
On the other hand this could be a bit more verbose.
Quoted 34 lines
KASAN report as below:
BUG: KASAN: slab-use-after-free in key_payload_reserve+0x54d/0x560Fixes 5801649d8b83 ("[PATCH] keys: let keyctl_chown() change a key's owner")Signed-off-by Cen Zhang <[email protected]>Assisted-by LLM---
security/keys/user_defined.c +2
-1@@ -15,6 +15,7 @@#include "internal.h" static int logon_vet_description (const char *desc); +static int user_instantiate (struct key *key,struct key_preparsed_payload *prep);/* * user defined keys take an arbitrary string as the description and an @@ -24,7 +25,7 @@ struct key_type key_type_user = { .name = "user", .preparse = user_preparse, .free_preparse = user_free_preparse, - .instantiate = generic_key_instantiate, + .instantiate = user_instantiate, .update = user_update, .revoke = user_revoke, .destroy = user_destroy,
security/keys/user_defined.c void user_free_preparse(struct key_preparsed_payload *prep) · line 92
Quoted 6 lines
security/keys/user_defined.c +3
-0@@ -86,6 +87,19 @@ void user_free_preparse(struct key_preparsed_payload *prep) }EXPORT_SYMBOL_GPL (user_free_preparse); +/* +* Keep the owner account stable while reserving quota during instantiation. +*/
Also here as a kernel developer I do not comprehend what I do when I keep "an own account stable". I don't have kernel idioms that match.
security/keys/user_defined.c void user_free_preparse(struct key_preparsed_payload *prep) · line 105
Quoted 13 lines
security/keys/user_defined.c +10
-0+static int user_instantiate (struct key *key,struct key_preparsed_payload *prep) +{ +int ret; + +down_read (&key->sem); + ret =generic_key_instantiate (key, prep); +up_read (&key->sem); +return ret; +} +static void user_free_payload_rcu (struct rcu_head *head) {struct user_key_payload *payload;
Not disagreeing with the fix but yeah would really like something else than gibberish around the concurrency scenario.
Br, Jarkko
Jarkko Sakkinen
maintainerQuoted 100 lines · Jarkko Sakkinen
Quoted 5 lines · Cen Zhang
The owner account must remain alive throughout payload quota reservation. The user key type calls
generic_key_instantiate(), which dereferenceskey->userinkey_payload_reserve(), without holdingkey->sem. The construction mutex excludes other instantiators but does not excludekeyctl_chown_key().I'm not following the term "owner account". It is neither an actor in the concurrency scenario. What do you exactly mean by it?
Quoted 23 lines
A kernel caller can allocate an in-quota user key, retain its reference and instantiate it with a nonempty payload after publishing its serial. A concurrent task with Setattr permission and
CAP_SYS_ADMINcan chown the still-uninstantiated key throughKEY_LOOKUP_PARTIAL. If the key holds the last reference to its non-root account, instantiation has no authorization key, and the new owner has sufficient quota, this ordering is possible:Instantiator Chown task
key_instantiate_and_link()keyctl_chown_key()lock key_construction_mutex down_write(key->sem) generic_key_instantiate() key_payload_reserve() load old = key->user transfer quota to new owner replace key->user up_write(key->sem) key_put(key) key_user_put(old) kfree(old) read old->uidQuoted 7 lines
Chown's final free falls between the old pointer load and the uid read. The read can therefore access freed struct
key_userstorage despite the caller retaining the key itself. Hold the read side ofkey->semin a local user instantiation callback acrossgeneric_key_instantiate(). Chown then cannot replace and release the account until the quota accesses finish. If chown goes first, instantiation takes the semaphore and uses the new account instead.And account gets repeated here. I'm not sure hat accounts we are talking about here.
I.e.
1. I get the scenario.
2. I read all of the text but I do not understand what I'm reading.Quoted 3 lines
Keep locking out of the shared helper, which is also called by
big_key_update()withkey->semwrite-locked.On the other hand this could be a bit more verbose.
Quoted 34 lines
KASAN report as below:
BUG: KASAN: slab-use-after-free in key_payload_reserve+0x54d/0x560Fixes 5801649d8b83 ("[PATCH] keys: let keyctl_chown() change a key's owner")Signed-off-by Cen Zhang <[email protected]>Assisted-by LLM---
security/keys/user_defined.c +2
-1@@ -15,6 +15,7 @@#include "internal.h" static int logon_vet_description (const char *desc); +static int user_instantiate (struct key *key,struct key_preparsed_payload *prep);/* * user defined keys take an arbitrary string as the description and an @@ -24,7 +25,7 @@ struct key_type key_type_user = { .name = "user", .preparse = user_preparse, .free_preparse = user_free_preparse, - .instantiate = generic_key_instantiate, + .instantiate = user_instantiate, .update = user_update, .revoke = user_revoke, .destroy = user_destroy,security/keys/user_defined.c void user_free_preparse(struct key_preparsed_payload *prep) · line 92
Quoted 6 lines
security/keys/user_defined.c +3
-0@@ -86,6 +87,19 @@ void user_free_preparse(struct key_preparsed_payload *prep) }EXPORT_SYMBOL_GPL (user_free_preparse); +/* +* Keep the owner account stable while reserving quota during instantiation. +*/ Also here as a kernel developer I do not comprehend what I do when I keep "an own account stable". I don't have kernel idioms that match.
Quoted 16 lines
security/keys/user_defined.c void user_free_preparse(struct key_preparsed_payload *prep) · line 105
Quoted 13 lines
security/keys/user_defined.c +10
-0+static int user_instantiate (struct key *key,struct key_preparsed_payload *prep) +{ +int ret; + +down_read (&key->sem); + ret =generic_key_instantiate (key, prep); +up_read (&key->sem); +return ret; +} +static void user_free_payload_rcu (struct rcu_head *head) {struct user_key_payload *payload;Not disagreeing with the fix but yeah would really like something else than gibberish around the concurrency scenario.
I appreciate the fix but if I'm completely hones the commit message looks like direct LLM output, which is not acceptable.
It needs to be proof-readed and edited. For this reason, I want a better iteration before I apply this.
Br, Jarkko
Hi Jarkko,
Thanks very much for the detailed feedback and for your patience with this :).
Quoted 9 lines · Jarkko Sakkinen <[email protected]> 于2026年10月12日周一 21:03写道:
I appreciate the fix but if I'm completely hones the commit message looks like direct LLM output, which is not acceptable.
It needs to be proof-readed and edited. For this reason, I want a better iteration before I apply this.
Br, Jarkko
Thanks very much for the detailed feedback and for your patience with this.
Several people have mentioned that my commit messages look like they were generated by an LLM — and to be honest, I did write them with LLM assistance. However, they often gave me the feeling of being questioned rather than explained.
I spend most of my time looking for bugs, following my interest, so I’m more familiar
with bug patterns than with the deeper details of the subsystem. That means I can
make mistakes. I’m willing to listen to feedback and keep revising the patch until it’s in
good shape, but the pressure sometimes makes me afraid to continue the conversation,
worried about making more mistakes. Your patient explanation this time really means a
lot to me.
Of course, I also fully understand that busy developers feel uncomfortable when seeing
unclear or poorly written messages. I just wanted to express my thanks for taking the time
to gently and patiently explain things clearly.
I’ll carefully rework the commit message following your suggestions to make the concurrency
scenario and the reasoning clearer, and send a new patch once it’s ready. I’d appreciate it if
you could take another look then.
Best regards,
Cen Zhang
Hi Jarkko,
Sorry for the previous email — the line wrapping got a bit messy on my side. Hope it was still readable :).
Best regards,
Cen
Quoted 48 lines · Cen Zhang <[email protected]> 于2026年10月12日周一 21:30写道:
Hi Jarkko,
Thanks very much for the detailed feedback and for your patience with this :).
Quoted 9 lines · Jarkko Sakkinen <[email protected]> 于2026年10月12日周一 21:03写道:
I appreciate the fix but if I'm completely hones the commit message looks like direct LLM output, which is not acceptable.
It needs to be proof-readed and edited. For this reason, I want a better iteration before I apply this.
Br, Jarkko
Thanks very much for the detailed feedback and for your patience with this.
Several people have mentioned that my commit messages look like they were generated by an LLM — and to be honest, I did write them with LLM assistance. However, they often gave me the feeling of being questioned rather than explained.
I spend most of my time looking for bugs, following my interest, so I’m more familiar
with bug patterns than with the deeper details of the subsystem. That means I can
make mistakes. I’m willing to listen to feedback and keep revising the patch until it’s in
good shape, but the pressure sometimes makes me afraid to continue the conversation,
worried about making more mistakes. Your patient explanation this time really means a
lot to me.Of course, I also fully understand that busy developers feel uncomfortable when seeing
unclear or poorly written messages. I just wanted to express my thanks for taking the time
to gently and patiently explain things clearly.I’ll carefully rework the commit message following your suggestions to make the concurrency
scenario and the reasoning clearer, and send a new patch once it’s ready. I’d appreciate it if
you could take another look then.Best regards,
Cen Zhang
Jarkko Sakkinen
maintainerQuoted 4 lines · Cen Zhang
Hi Jarkko,
Sorry for the previous email — the line wrapping got a bit messy on my side. Hope it was still readable :).
All cool.
Quoted 3 lines
Best regards,
Cen
Br, Jarkko
Hi Jarkko,
Quoted 7 lines · Jarkko Sakkinen <[email protected]> 于2026年10月12日周一 23:01写道:
Quoted 4 lines · Cen Zhang
Hi Jarkko,
Sorry for the previous email — the line wrapping got a bit messy on my side. Hope it was still readable :).
Quoted 8 lines
All cool.
Quoted 3 lines
Best regards,
CenBr, Jarkko
After reviewing Chengfeng Ye’s v4 patch
(https://lore.kernel.org/r/[email protected]/),
it also address this issue as well. That fix looks good to me, so please disregard my earlier patch.
Thanks for your review and patience again.
Best regards,
Cen Zhang
Jarkko Sakkinen
maintainerQuoted 39 lines · Cen Zhang
Hi Jarkko,
Thanks very much for the detailed feedback and for your patience with this :).
Quoted 9 lines · Jarkko Sakkinen <[email protected]> 于2026年10月12日周一 21:03写道:
I appreciate the fix but if I'm completely hones the commit message looks like direct LLM output, which is not acceptable.
It needs to be proof-readed and edited. For this reason, I want a better iteration before I apply this.
Br, Jarkko
Thanks very much for the detailed feedback and for your patience with this.
Several people have mentioned that my commit messages look like they were generated by an LLM — and to be honest, I did write them with LLM assistance. However, they often gave me the feeling of being questioned rather than explained.
I spend most of my time looking for bugs, following my interest, so I’m more familiar
with bug patterns than with the deeper details of the subsystem. That means I can
make mistakes. I’m willing to listen to feedback and keep revising the patch until it’s in
good shape, but the pressure sometimes makes me afraid to continue the conversation,
worried about making more mistakes. Your patient explanation this time really means a
lot to me.Of course, I also fully understand that busy developers feel uncomfortable when seeing
unclear or poorly written messages. I just wanted to express my thanks for taking the time
to gently and patiently explain things clearly.
Quoted 5 lines
I’ll carefully rework the commit message following your suggestions to make the concurrency
scenario and the reasoning clearer, and send a new patch once it’s ready. I’d appreciate it if
you could take another look then.
While AI assistance is allowed, if this happens as a pattern, it is misuse and eager way to credit without due. I.e., you've done 50% of the work but ignored the human part.
I.e., you also must understand without AI assistance what the patch does. If you do, it should not a problem to write a commit message tht simply describes what it does.
To summarize:
1. Actual mistakes are fine and then patch just needs to be iterated.
2. Skipping work is not mistake and it is not cool.
Hope this clears things up.
Quoted 3 lines
Best regards,
Cen Zhang
Br, Jarkko
Quoted 9 lines · Jarkko Sakkinen <[email protected]> 于2026年10月12日周一 21:49写道:
While AI assistance is allowed, if this happens as a pattern, it is misuse and eager way to credit without due. I.e., you've done 50% of the work but ignored the human part.
I.e., you also must understand without AI assistance what the patch does. If you do, it should not a problem to write a commit message tht simply describes what it does.
Quoted 7 lines
To summarize:
1. Actual mistakes are fine and then patch just needs to be iterated.
2. Skipping work is not mistake and it is not cool.Hope this clears things up.
Thanks for the guidance. I’ll keep it in mind.
Best regards,
Cen