keys: Protect the type name while describing a key
Subsystems (2)
PATCH v2 1/1 keys: Protect the type name while describing a key
keyctl_describe_key() must keep the key type's name alive until kasprintf() has consumed it. The retained key reference keeps the key and its description alive, but does not pin the module providing its type. The name is passed to kasprintf() without holding key->sem.
kvasprintf() uses the saved name pointer in two formatting passes, with a GFP_KERNEL allocation between them. With AF_RXRPC=m, a task that passes View permission before type retirement can overlap a privileged module unload in this order:
1. KEYCTL_DESCRIBE looks up the key, saves key->type->name in the kasprintf() arguments and completes the first formatting pass.
2. While the formatter is delayed before its second pass, af_rxrpc_exit() calls unregister_key_type(), which removes the type from the registry and waits for key_gc_keytype().
3. The collector takes key->sem for writing, retypes the retained key to key_type_dead and completes retirement. Unregistration and module exit return, allowing free_module() to release the type and name.
4. The formatter resumes its second pass and reads the saved name from released module storage, which can fault in string().
Hold key->sem for reading across kasprintf(). The collector then cannot retype the key or complete retirement until both formatting passes have finished. If retirement wins the lock first, formatting uses the core key_type_dead name instead. Release the semaphore immediately after kasprintf(), including on allocation failure, before copying to userspace.
Oops report as below:
BUG: unable to handle page fault for address: fffffbfff8054794
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
RIP: 0010:string+0x23f/0x470
Call Trace:
vsnprintf+0x330/0x1110---
Changes in v2:
- Trim the Oops report to the fault location and vsnprintf frame.
Link to v1: https://lore.kernel.org/r/pm-key-management-objects-candidate-0009-v3-43bbf21d1c452f23d829@gmail.com
security/keys/keyctl.c +2 -0
@@ -677,12 +677,14 @@ long keyctl_describe_key(key_serial_t keyid,/* calculate how much information we're going to return */ ret = -ENOMEM ; +down_read (&key->sem); infobuf =kasprintf (GFP_KERNEL ,"%s;%d;%d;%08x;" , key->type->name,from_kuid_munged (current_user_ns (), key->uid),from_kgid_munged (current_user_ns (), key->gid), key->perm); +up_read (&key->sem);if (!infobuf) goto error2; infolen =strlen (infobuf);
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines
keyctl_describe_key()must keep the key type's name alive untilkasprintf()has consumed it. The retained key reference keeps the key and its description alive, but does not pin the module providing its type. The name is passed tokasprintf()without holdingkey->sem.
kvasprintf()uses the saved name pointer in two formatting passes, with aGFP_KERNELallocation between them. With AF_RXRPC=m, a task that passes View permission before type retirement can overlap a privileged module unload in this order:1.
KEYCTL_DESCRIBElooks up the key, saveskey->type->namein thekasprintf()arguments and completes the first formatting pass.
2. While the formatter is delayed before its second pass,af_rxrpc_exit()callsunregister_key_type(), which removes the type from the registry and waits forkey_gc_keytype().
[ ... ]
Quoted 5 lines
vsnprintf+0x330/0x1110Fixes aa9d4437893f ("KEYS: Fix the size of the key description passed to/from userspace")Assisted-by LLMSigned-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-0009-v4-6b537c5732fd957c33fa@gmail.com?part=1
Quoted 55 lines · Cen Zhang
keyctl_describe_key()must keep the key type's name alive untilkasprintf()has consumed it. The retained key reference keeps the key and its description alive, but does not pin the module providing its type. The name is passed tokasprintf()without holdingkey->sem.
kvasprintf()uses the saved name pointer in two formatting passes, with aGFP_KERNELallocation between them. With AF_RXRPC=m, a task that passes View permission before type retirement can overlap a privileged module unload in this order:1.
KEYCTL_DESCRIBElooks up the key, saveskey->type->namein thekasprintf()arguments and completes the first formatting pass.
2. While the formatter is delayed before its second pass,af_rxrpc_exit()callsunregister_key_type(), which removes the type from the registry and waits forkey_gc_keytype().
3. The collector takeskey->semfor writing, retypes the retained key tokey_type_deadand completes retirement. Unregistration and module exit return, allowingfree_module()to release the type and name.
4. The formatter resumes its second pass and reads the saved name from released module storage, which can fault instring().Hold
key->semfor reading acrosskasprintf(). The collector then cannot retype the key or complete retirement until both formatting passes have finished. If retirement wins the lock first, formatting uses the corekey_type_deadname instead. Release the semaphore immediately afterkasprintf(), including on allocation failure, before copying to userspace.Oops report as below:
BUG: unable to handle page fault for address: fffffbfff8054794 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page RIP: 0010:string+0x23f/0x470 Call Trace: vsnprintf+0x330/0x1110Fixes aa9d4437893f ("KEYS: Fix the size of the key description passed to/from userspace")Assisted-by LLMSigned-off-by Cen Zhang <[email protected]>---
Changes in v2:
- Trim the Oops report to the fault location and vsnprintf frame.Link to v1: https://lore.kernel.org/r/pm-key-management-objects-candidate-0009-v3-43bbf21d1c452f23d829@gmail.com
security/keys/keyctl.c +1
-0@@ -677,12 +677,14 @@ long keyctl_describe_key(key_serial_t keyid,/* calculate how much information we're going to return */ ret = -ENOMEM ; +down_read (&key->sem);
security/keys/keyctl.c long keyctl_describe_key(key_serial_t keyid, · line 690
Quoted 10 lines
security/keys/keyctl.c +1
-0infobuf =kasprintf (GFP_KERNEL ,"%s;%d;%d;%08x;" , key->type->name,from_kuid_munged (current_user_ns (), key->uid),from_kgid_munged (current_user_ns (), key->gid), key->perm); +up_read (&key->sem);if (!infobuf) goto error2; infolen =strlen (infobuf);
This would be even clear without scenario as it is quite obvious that key->type can swap but I do appreciate having one of the scenarios documented. Just saying that this is much better commit that would have gone through :-)
Great work, thank you.
Br, Jarkko