Linux Integrity Measurement development
 help / color / mirror / Atom feed
From: Zhenwen Liang <zhenwenliang@google.com>
To: Jarkko Sakkinen <jarkko@kernel.org>,
	Mimi Zohar <zohar@linux.ibm.com>,
	 David Howells <dhowells@redhat.com>,
	 James Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Paul Moore <paul@paul-moore.com>,
	James Morris <jmorris@namei.org>,
	 "Serge E. Hallyn" <serge@hallyn.com>,
	keyrings@vger.kernel.org,  linux-integrity@vger.kernel.org,
	linux-security-module@vger.kernel.org,  stable@vger.kernel.org
Subject: [PATCH 1/2] KEYS: encrypted: Wait for RCU callbacks before module unload
Date: Thu, 26 Nov 2026 20:01:02 +0000	[thread overview]
Message-ID: <20261126200103.2806716-1-zhenwenliang@google.com> (raw)

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.

Fixes: 7e70cb497850 ("keys: add new key-type encrypted")
Link: https://lore.kernel.org/all/20261126023538.4160950-1-zhenwenliang@google.com/
Cc: stable@vger.kernel.org
Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
---
 security/keys/encrypted-keys/encrypted.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c
index 63f0245e6f74..fca809819f8e 100644
--- a/security/keys/encrypted-keys/encrypted.c
+++ b/security/keys/encrypted-keys/encrypted.c
@@ -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);

             reply	other threads:[~2026-11-26 20:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 20:01 Zhenwen Liang [this message]
2026-11-26 20:01 ` [PATCH 2/2] KEYS: trusted: Wait for RCU callbacks before module unload Zhenwen Liang
2026-11-26 20:42   ` Jarkko Sakkinen
2026-11-26 20:44     ` Jarkko Sakkinen
2026-11-26 20:41 ` [PATCH 1/2] KEYS: encrypted: " Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126200103.2806716-1-zhenwenliang@google.com \
    --to=zhenwenliang@google.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=dhowells@redhat.com \
    --cc=jarkko@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=serge@hallyn.com \
    --cc=stable@vger.kernel.org \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox