KEYS: encrypted: Wait for RCU callbacks before module unload
Subsystems (5)
PATCH 1/2 KEYS: encrypted: Wait for RCU callbacks before module unload
encrypted_update() frees the old payload with call_rcu(), and the callback, encrypted_rcu_free(), is part of encrypted-keys.ko. cleanup_encrypted() only calls unregister_key_type(), which does not wait for callbacks that are already queued. If the module is removed shortly after a KEYCTL_UPDATE, the RCU core later calls encrypted_rcu_free() in freed module memory.
No new callback can be queued once unregister_key_type() has returned:key_create_or_update() holds key_types_sem across __key_update(), key_update() checks key->type->update with key->sem held, and the garbage collector switches every remaining key of the type to key_type_dead under key->sem before unregister_key_type() returns. An rcu_barrier() at that point therefore waits for all pending encrypted_rcu_free() callbacks, as Documentation/RCU/rcubarrier.rst describes for modules that use call_rcu().
Tested in QEMU (x86_64, TCG) on mainline (7.4-rc4+) with KASAN, ENCRYPTED_KEYS=m and CONFIG_RCU_LAZY=y, so that callbacks are deferred:
add an encrypted key, update it 20 times, then call delete_module() right away. Without this patch, the rcuop kthread hit int3 at encrypted_rcu_free+0x1 in the freed module text in 5 of 5 runs. With it, 5 of 5 runs were clean, and the unload took about 30 ms longer.
The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch.
--- security/keys/encrypted-keys/encrypted.c | 1 + 1 file changed, 1 insertion(+)
security/keys/encrypted-keys/encrypted.c +1 -0
@@ -1000,6 +1000,7 @@ static int __init init_encrypted(void) static void __exit cleanup_encrypted(void) {unregister_key_type (&key_type_encrypted); +rcu_barrier (); }late_initcall (init_encrypted);
PATCH 2/2 KEYS: trusted: Wait for RCU callbacks before module unload
trusted_update() frees the old payload with call_rcu(), and the callback, trusted_rcu_free(), is part of trusted.ko. cleanup_trusted() calls the trust source's exit callback, which unregisters the key type, but nothing waits for callbacks that are already queued, so the module can be freed while a trusted_rcu_free() callback is still pending.
Add an rcu_barrier() after the exit callback. As for encrypted keys, no new callback can be queued once unregister_key_type() has returned. If no trust source was found, the key type was never registered and trusted_key_exit is NULL; the barrier is then harmless.
Tested in QEMU (x86_64, TCG) without a trust source, so only the trusted_key_exit == NULL case ran: trusted.ko loads and unloads cleanly with this patch (5 runs). The trusted_update() path itself was not run, because no TPM was available.
The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch.
--- security/keys/trusted-keys/trusted_core.c | 1 + 1 file changed, 1 insertion(+)
security/keys/trusted-keys/trusted_core.c +1 -0
@@ -401,6 +401,7 @@ static void __exit cleanup_trusted(void) {if (trusted_key_exit) (*trusted_key_exit)(); +rcu_barrier (); }late_initcall (init_trusted);
Re: [PATCH 2/2] KEYS: trusted: Wait for RCU callbacks before module unload
Quoted 5 lines · Zhenwen Liang
trusted_update()frees the old payload withcall_rcu(), and the callback,trusted_rcu_free(), is part of trusted.ko.cleanup_trusted()calls the trust source's exit callback, which unregisters the key type, but nothing waits for callbacks that are already queued, so the module can be freed while atrusted_rcu_free()callback is still pending.
+1
Quoted 28 lines
Add an
rcu_barrier()after the exit callback. As for encrypted keys, no new callback can be queued onceunregister_key_type()has returned. If no trust source was found, the key type was never registered andtrusted_key_exitis NULL; the barrier is then harmless.Tested in QEMU (
x86_64, TCG) without a trust source, so only thetrusted_key_exit== NULL case ran: trusted.ko loads and unloads cleanly with this patch (5 runs). Thetrusted_update()path itself was not run, because no TPM was available.The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch.
Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Zhenwen Liang <[email protected]>--- security/keys/trusted-keys/trusted_core.c | 1 + 1 file changed, 1 insertion(+)security/keys/trusted-keys/trusted_core.c +0
-0
security/keys/trusted-keys/trusted_core.c static void __exit cleanup_trusted(void) · line 407
Quoted 7 lines
security/keys/trusted-keys/trusted_core.c +1
-0@@ -401,6 +401,7 @@ static void __exit cleanup_trusted(void) {if (trusted_key_exit) (*trusted_key_exit)(); +rcu_barrier (); }late_initcall (init_trusted);
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH 2/2] KEYS: trusted: Wait for RCU callbacks before module unload
Quoted 43 lines · Jarkko Sakkinen
Quoted 5 lines · Zhenwen Liang
trusted_update()frees the old payload withcall_rcu(), and the callback,trusted_rcu_free(), is part of trusted.ko.cleanup_trusted()calls the trust source's exit callback, which unregisters the key type, but nothing waits for callbacks that are already queued, so the module can be freed while atrusted_rcu_free()callback is still pending.+1
Quoted 34 lines
Add an
rcu_barrier()after the exit callback. As for encrypted keys, no new callback can be queued onceunregister_key_type()has returned. If no trust source was found, the key type was never registered andtrusted_key_exitis NULL; the barrier is then harmless.Tested in QEMU (
x86_64, TCG) without a trust source, so only thetrusted_key_exit== NULL case ran: trusted.ko loads and unloads cleanly with this patch (5 runs). Thetrusted_update()path itself was not run, because no TPM was available.The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch.
Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Zhenwen Liang <[email protected]>--- security/keys/trusted-keys/trusted_core.c | 1 + 1 file changed, 1 insertion(+)security/keys/trusted-keys/trusted_core.c +1
-0@@ -401,6 +401,7 @@ static void __exit cleanup_trusted(void) {if (trusted_key_exit) (*trusted_key_exit)(); +rcu_barrier (); }
Quoted 3 lines
security/keys/trusted-keys/trusted_core.c static void __exit cleanup_trusted(void) · line 407
Quoted 1 line
security/keys/trusted-keys/trusted_core.c +0
-0late_initcall (init_trusted);Reviewed-by Jarkko Sakkinen <[email protected]>
Pushed (to master for the time being).
Br, Jarkko
Re: [PATCH 2/2] KEYS: trusted: Wait for RCU callbacks before module unload
Quoted 44 lines · Jarkko Sakkinen
Quoted 43 lines · Jarkko Sakkinen
Quoted 5 lines · Zhenwen Liang
trusted_update()frees the old payload withcall_rcu(), and the callback,trusted_rcu_free(), is part of trusted.ko.cleanup_trusted()calls the trust source's exit callback, which unregisters the key type, but nothing waits for callbacks that are already queued, so the module can be freed while atrusted_rcu_free()callback is still pending.+1
Quoted 34 lines
Add an
rcu_barrier()after the exit callback. As for encrypted keys, no new callback can be queued onceunregister_key_type()has returned. If no trust source was found, the key type was never registered andtrusted_key_exitis NULL; the barrier is then harmless.Tested in QEMU (
x86_64, TCG) without a trust source, so only thetrusted_key_exit== NULL case ran: trusted.ko loads and unloads cleanly with this patch (5 runs). Thetrusted_update()path itself was not run, because no TPM was available.The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch.
Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Zhenwen Liang <[email protected]>--- security/keys/trusted-keys/trusted_core.c | 1 + 1 file changed, 1 insertion(+)security/keys/trusted-keys/trusted_core.c +1
-0@@ -401,6 +401,7 @@ static void __exit cleanup_trusted(void) {if (trusted_key_exit) (*trusted_key_exit)(); +rcu_barrier (); }
Quoted 5 lines
Quoted 3 lines
security/keys/trusted-keys/trusted_core.c static void __exit cleanup_trusted(void) · line 407
Quoted 1 line
security/keys/trusted-keys/trusted_core.c +0
-0late_initcall (init_trusted);Reviewed-by Jarkko Sakkinen <[email protected]>Pushed (to master for the time being).
Pushed to for-next-keys i.e., I'll piggyback them to a PR next week.
Thank you.
Br, Jarkko
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Quoted 40 lines · Zhenwen Liang
encrypted_update()frees the old payload withcall_rcu(), and the callback,encrypted_rcu_free(), is part of encrypted-keys.ko.cleanup_encrypted()only callsunregister_key_type(), which does not wait for callbacks that are already queued. If the module is removed shortly after aKEYCTL_UPDATE, the RCU core later callsencrypted_rcu_free()in freed module memory.No new callback can be queued once
unregister_key_type()has returned:key_create_or_update()holdskey_types_semacross__key_update(),key_update()checkskey->type->updatewithkey->semheld, and the garbage collector switches every remaining key of the type tokey_type_deadunderkey->sembeforeunregister_key_type()returns. Anrcu_barrier()at that point therefore waits for all pendingencrypted_rcu_free()callbacks, asDocumentation/RCU/rcubarrier.rstdescribes for modules that usecall_rcu().Tested in QEMU (
x86_64, TCG) on mainline (7.4-rc4+) with KASAN, ENCRYPTED_KEYS=m andCONFIG_RCU_LAZY=y, so that callbacks are deferred:
add an encrypted key, update it 20 times, then calldelete_module()right away. Without this patch, the rcuop kthread hit int3 atencrypted_rcu_free+0x1in the freed module text in 5 of 5 runs. With it, 5 of 5 runs were clean, and the unload took about 30 ms longer.The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch.
Fixes 7e70cb497850 ("keys: add new key-type encrypted")Signed-off-by Zhenwen Liang <[email protected]>--- security/keys/encrypted-keys/encrypted.c | 1 + 1 file changed, 1 insertion(+)security/keys/encrypted-keys/encrypted.c +0
-0
security/keys/encrypted-keys/encrypted.c static int __init init_encrypted(void) · line 1006
Quoted 7 lines
security/keys/encrypted-keys/encrypted.c +1
-0@@ -1000,6 +1000,7 @@ static int __init init_encrypted(void) static void __exit cleanup_encrypted(void) {unregister_key_type (&key_type_encrypted); +rcu_barrier (); }late_initcall (init_encrypted);
This is obvious enough. I anyway decided to go these through to get them out of the way.
Br, Jarkko