Skip to contentSkip to conversation

keys: Avoid the owner account dereference in named keyring lookup

PATCH In review Cen Zhang 4 participants 9 messages Oct 9 – Oct 19

Subsystems (2)
lore
Share & tools
lore

Cen Zhang

author
#0 lore raw

PATCH v2 1/1 keys: Avoid the owner account dereference in named keyring lookup

Named keyring lookup must keep the storage containing the owner UID alive through the namespace mapping check. find_keyring_by_name() reads keyring->user->uid under keyring_name_lock, but that lock protects the keyring's name entry and allocation, not its separate key_user.

A named session-keyring join can overlap a privileged KEYCTL_CHOWN on another CPU. If the keyring holds the last reference to its old dynamic key_user and the ownership transfer succeeds, the following ordering is possible:

    Named join                         Chown
    find_keyring_by_name()              keyctl_chown_key()
      read_lock(keyring_name_lock)       down_write(key->sem)
      load old keyring->user
                                         replace key->user and key->uid
                                         up_write(key->sem)
                                         key_put(key)
                                         key_user_put(old user): free
      read old user->uid
      read_unlock(keyring_name_lock)

Chown neither takes keyring_name_lock nor key_session_mutex, so it can free the old account between the pointer load and the UID read. The lookup then reads freed memory even though the keyring itself is alive.

Use keyring->uid for the mapping check. key_alloc() initializes this inline owner UID and keyctl_chown_key() updates it on chown. The existing name lock keeps its containing keyring allocated throughout the check, so lookup no longer depends on the account's lifetime. This also matches the owner UID used by the permission check.

KASAN report as below:

    BUG: KASAN: slab-use-after-free in find_keyring_by_name+0x577/0x5d0
    Read of size 4 at addr ffff8881128ae2ec by task keycase/500
Fixes 2ea190d0a006 ("keys: skip keys from another user namespace")
Assisted-by LLM

---
Changes in v2:
- Trim the KASAN report to its two-line fault summary.

Link to v1: https://lore.kernel.org/r/pm-key-management-objects-candidate-0002-v2-dd6c20a1d92928b33ab7@gmail.com

security/keys/keyring.c +1 -1
index 15bf4af8f28218ec3f12c97630d1c76939af7eca..46f774be72967a9bd16b0ddfdf323eda36edeff4 100644
@@ -1158,7 +1158,7 @@ struct key *find_keyring_by_name(const char *name, bool uid_keyring)
 	 * grants Search permission and that hasn't been revoked
 	 */
 	list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
-		if (!kuid_has_mapping(ns, keyring->user->uid))
+		if (!kuid_has_mapping(ns, keyring->uid))
 			continue;
 
 		if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 18 lines

Named keyring lookup must keep the storage containing the owner UID alive through the namespace mapping check. find_keyring_by_name() reads keyring->user->uid under keyring_name_lock, but that lock protects the keyring's name entry and allocation, not its separate key_user.

A named session-keyring join can overlap a privileged KEYCTL_CHOWN on another CPU. If the keyring holds the last reference to its old dynamic key_user and the ownership transfer succeeds, the following ordering is possible:

    Named join                         Chown
    find_keyring_by_name()              keyctl_chown_key()
      read_lock(keyring_name_lock)       down_write(key->sem)
      load old keyring->user
                                         replace key->user and key->uid
[ ... ]
    Read of size 4 at addr ffff8881128ae2ec by task keycase/500
Quoted 3 lines
Fixes 2ea190d0a006 ("keys: skip keys from another user namespace")
Assisted-by LLM

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-0002-v3-2baa8cad2bb554364e10@gmail.com?part=1
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Cen Zhang
Quoted 50 lines · Cen Zhang

Named keyring lookup must keep the storage containing the owner UID alive through the namespace mapping check. find_keyring_by_name() reads keyring->user->uid under keyring_name_lock, but that lock protects the keyring's name entry and allocation, not its separate key_user.

A named session-keyring join can overlap a privileged KEYCTL_CHOWN on another CPU. If the keyring holds the last reference to its old dynamic key_user and the ownership transfer succeeds, the following ordering is possible:

    Named join                         Chown
    find_keyring_by_name()              keyctl_chown_key()
      read_lock(keyring_name_lock)       down_write(key->sem)
      load old keyring->user
                                         replace key->user and key->uid
                                         up_write(key->sem)
                                         key_put(key)
                                         key_user_put(old user): free
      read old user->uid
      read_unlock(keyring_name_lock)

Chown neither takes keyring_name_lock nor key_session_mutex, so it can free the old account between the pointer load and the UID read. The lookup then reads freed memory even though the keyring itself is alive.

Use keyring->uid for the mapping check. key_alloc() initializes this inline owner UID and keyctl_chown_key() updates it on chown. The existing name lock keeps its containing keyring allocated throughout the check, so lookup no longer depends on the account's lifetime. This also matches the owner UID used by the permission check.

KASAN report as below:

    BUG: KASAN: slab-use-after-free in find_keyring_by_name+0x577/0x5d0
    Read of size 4 at addr ffff8881128ae2ec by task keycase/500
Fixes 2ea190d0a006 ("keys: skip keys from another user namespace")
Assisted-by LLM

---
Changes in v2:
- Trim the KASAN report to its two-line fault summary.

Link to v1: https://lore.kernel.org/r/pm-key-management-objects-candidate-0002-v2-dd6c20a1d92928b33ab7@gmail.com

security/keys/keyring.c +0 -0
index 15bf4af8f28218ec3f12c97630d1c76939af7eca..46f774be72967a9bd16b0ddfdf323eda36edeff4 100644

security/keys/keyring.c struct key *find_keyring_by_name(const char *name, bool uid_keyring) · line 1164

Quoted 8 lines
security/keys/keyring.c +1 -1
@@ -1158,7 +1158,7 @@ struct key *find_keyring_by_name(const char *name, bool uid_keyring)
 	 * grants Search permission and that hasn't been revoked
 	 */
 	list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
-		if (!kuid_has_mapping(ns, keyring->user->uid))
+		if (!kuid_has_mapping(ns, keyring->uid))
 			continue;
 
 		if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))

Thanks.

Br, Jarkko

#3 lore raw
In reply to Cen Zhang
Quoted 31 lines · Cen Zhang

Named keyring lookup must keep the storage containing the owner UID alive through the namespace mapping check. find_keyring_by_name() reads keyring->user->uid under keyring_name_lock, but that lock protects the keyring's name entry and allocation, not its separate key_user.

A named session-keyring join can overlap a privileged KEYCTL_CHOWN on another CPU. If the keyring holds the last reference to its old dynamic key_user and the ownership transfer succeeds, the following ordering is possible:

    Named join                         Chown
    find_keyring_by_name()              keyctl_chown_key()
      read_lock(keyring_name_lock)       down_write(key->sem)
      load old keyring->user
                                         replace key->user and key->uid
                                         up_write(key->sem)
                                         key_put(key)
                                         key_user_put(old user): free
      read old user->uid
      read_unlock(keyring_name_lock)

Chown neither takes keyring_name_lock nor key_session_mutex, so it can free the old account between the pointer load and the UID read. The lookup then reads freed memory even though the keyring itself is alive.

Use keyring->uid for the mapping check. key_alloc() initializes this inline owner UID and keyctl_chown_key() updates it on chown. The existing name lock keeps its containing keyring allocated throughout the check, so lookup no longer depends on the account's lifetime. This also matches the owner UID used by the permission check.

Quoted 8 lines

KASAN report as below:

    BUG: KASAN: slab-use-after-free in find_keyring_by_name+0x577/0x5d0
    Read of size 4 at addr ffff8881128ae2ec by task keycase/500
Fixes 2ea190d0a006 ("keys: skip keys from another user namespace")
Assisted-by LLM

Don't key_serial_next() and find_ge_key() (used when reading from /proc/keys) have the same bug?

- Eric

author
#4 lore raw

Hi Eric,

Quoted 5 lines · Eric Biggers <[email protected]> 于2026年10月17日周六 11:51写道:

Don't key_serial_next() and find_ge_key() (used when reading from /proc/keys) have the same bug?

- Eric

Thansk a lot for the reivew. You're right — key_serial_next() and find_ge_key() have
the same issue.

I'll send a follow-up patch to fix those as well.

Thanks for the catch again.

Best regards,
Cen Zhang

maintainer
#5 lore raw
Quoted 13 lines · Cen Zhang

Hi Eric,

Quoted 5 lines · Eric Biggers <[email protected]> 于2026年10月17日周六 11:51写道:

Don't key_serial_next() and find_ge_key() (used when reading from /proc/keys) have the same bug?

- Eric

Thansk a lot for the reivew. You're right — key_serial_next() and find_ge_key() have
the same issue.

Quoted 6 lines

I'll send a follow-up patch to fix those as well.

Thanks for the catch again.

Best regards,
Cen Zhang

Yeah, please do.

You put so much effort to scenario and you are nailing the bug that way, and I do appreciate that, so here's what you should do with the commit messages:

1. Do the first half like you always do. That's great. And I've also said that.
2. Think just a moment. If you get anxiety from that something that should be mentioned e.g., something that you did not get immediately yourself at first, write remarks those down. I'm 100% sure you know when it might be good tip on something non-obvious.
3. Send that. If we feel it is lacking some info needed, let's iterate that. It's not a big deal.

I'm just underlining this because so little would have to be improved to make this right :-)

Br, Jarkko

maintainer
#6 lore raw
Quoted 37 lines · Jarkko Sakkinen
Quoted 13 lines · Cen Zhang

Hi Eric,

Quoted 5 lines · Eric Biggers <[email protected]> 于2026年10月17日周六 11:51写道:

Don't key_serial_next() and find_ge_key() (used when reading from /proc/keys) have the same bug?

- Eric

Thansk a lot for the reivew. You're right — key_serial_next() and find_ge_key() have
the same issue.

Quoted 6 lines

I'll send a follow-up patch to fix those as well.

Thanks for the catch again.

Best regards,
Cen Zhang

Yeah, please do.

You put so much effort to scenario and you are nailing the bug that way, and I do appreciate that, so here's what you should do with the commit messages:

1. Do the first half like you always do. That's great. And I've also said that.
2. Think just a moment. If you get anxiety from that something that should be mentioned e.g., something that you did not get immediately yourself at first, write remarks those down. I'm 100% sure you know when it might be good tip on something non-obvious.
3. Send that. If we feel it is lacking some info needed, let's iterate that. It's not a big deal.

Quoted 2 lines

I'm just underlining this because so little would have to be improved to make this right :-)

And also: I would not complain about anything if I did not want you to send patches because it does not make sense to waste time like that. So take it this from that angle. And they bring value thanks to well described scenarios.

Br, Jarkko

author
#7 lore raw

Hi Jarkko,

Quoted 42 lines · Jarkko Sakkinen <[email protected]> 于2026年10月18日周日 00:31写道:
Quoted 37 lines · Jarkko Sakkinen
Quoted 13 lines · Cen Zhang

Hi Eric,

Quoted 5 lines · Eric Biggers <[email protected]> 于2026年10月17日周六 11:51写道:

Don't key_serial_next() and find_ge_key() (used when reading from /proc/keys) have the same bug?

- Eric

Thansk a lot for the reivew. You're right — key_serial_next() and find_ge_key() have
the same issue.

Quoted 6 lines

I'll send a follow-up patch to fix those as well.

Thanks for the catch again.

Best regards,
Cen Zhang

Yeah, please do.

You put so much effort to scenario and you are nailing the bug that way, and I do appreciate that, so here's what you should do with the commit messages:

1. Do the first half like you always do. That's great. And I've also said that.
2. Think just a moment. If you get anxiety from that something that should be mentioned e.g., something that you did not get immediately yourself at first, write remarks those down. I'm 100% sure you know when it might be good tip on something non-obvious.
3. Send that. If we feel it is lacking some info needed, let's iterate that. It's not a big deal.

Quoted 2 lines

I'm just underlining this because so little would have to be improved to make this right :-)

Quoted 6 lines

And also: I would not complain about anything if I did not want you to send patches because it does not make sense to waste time like that. So take it this from that angle. And they bring value thanks to well described scenarios.

Br, Jarkko

Thanks for taking the time to explain this, and for the encouragement. This helps me a lot.
I'll keep the scenario descriptions and pay more attention to explaining the non-obvious parts.
I really appreciate your patience and guidance.

Best regards,
Cen Zhang

maintainer
#8 lore raw
In reply to Eric Biggers
Quoted 37 lines · Eric Biggers
Quoted 36 lines · Cen Zhang

Named keyring lookup must keep the storage containing the owner UID alive through the namespace mapping check. find_keyring_by_name() reads keyring->user->uid under keyring_name_lock, but that lock protects the keyring's name entry and allocation, not its separate key_user.

A named session-keyring join can overlap a privileged KEYCTL_CHOWN on another CPU. If the keyring holds the last reference to its old dynamic key_user and the ownership transfer succeeds, the following ordering is possible:

    Named join                         Chown
    find_keyring_by_name()              keyctl_chown_key()
      read_lock(keyring_name_lock)       down_write(key->sem)
      load old keyring->user
                                         replace key->user and key->uid
                                         up_write(key->sem)
                                         key_put(key)
                                         key_user_put(old user): free
      read old user->uid
      read_unlock(keyring_name_lock)

Chown neither takes keyring_name_lock nor key_session_mutex, so it can free the old account between the pointer load and the UID read. The lookup then reads freed memory even though the keyring itself is alive.

Use keyring->uid for the mapping check. key_alloc() initializes this inline owner UID and keyctl_chown_key() updates it on chown. The existing name lock keeps its containing keyring allocated throughout the check, so lookup no longer depends on the account's lifetime. This also matches the owner UID used by the permission check.

KASAN report as below:

    BUG: KASAN: slab-use-after-free in find_keyring_by_name+0x577/0x5d0
    Read of size 4 at addr ffff8881128ae2ec by task keycase/500
Quoted 6 lines
Quoted 3 lines
Fixes 2ea190d0a006 ("keys: skip keys from another user namespace")
Assisted-by LLM

Don't key_serial_next() and find_ge_key() (used when reading from /proc/keys) have the same bug?

With just a quick Saturday skim, yep, I think so.

Quoted 2 lines

- Eric

Br, Jarkko