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.

Fixes: d00a1c72f7f4 ("keys: add new trusted key-type")
Link: 
https://lore.kernel.org/all/[email protected]/
Cc: [email protected]
Signed-off-by: Zhenwen Liang <[email protected]>
---
 security/keys/trusted-keys/trusted_core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/security/keys/trusted-keys/trusted_core.c 
b/security/keys/trusted-keys/trusted_core.c
index 0509d9955f2a..5efd1e6ef111 100644
--- a/security/keys/trusted-keys/trusted_core.c
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -401,6 +401,7 @@ static void __exit cleanup_trusted(void)
 {
        if (trusted_key_exit)
                (*trusted_key_exit)();
+       rcu_barrier();
 }
 
 late_initcall(init_trusted);

Reply via email to