Linux Integrity Measurement development
 help / color / mirror / Atom feed
From: Zhenwen Liang <zhenwenliang@google.com>
To: Mimi Zohar <zohar@linux.ibm.com>,
	David Howells <dhowells@redhat.com>,
	 Jarkko Sakkinen <jarkko@kernel.org>
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
Subject: KEYS: encrypted: encrypted_rcu_free() callbacks can outlive the module
Date: Thu, 26 Nov 2026 02:35:38 +0000	[thread overview]
Message-ID: <20261126023538.4160950-1-zhenwenliang@google.com> (raw)

Hi,

encrypted_update() replaces the payload and frees the old one with

    rcu_assign_keypointer(key, new_epayload);
    call_rcu(&epayload->rcu, encrypted_rcu_free);

(security/keys/encrypted-keys/encrypted.c:898-899 on mainline
ebb7a8dc1e74), but the module exit path does not wait for those
callbacks:

    static void __exit cleanup_encrypted(void)
    {
            unregister_key_type(&key_type_encrypted);
    }

When ENCRYPTED_KEYS=m, an encrypted_rcu_free() callback queued by a
recent KEYCTL_UPDATE can therefore still be pending when
encrypted-keys.ko is unloaded. The RCU core later calls a function in
freed module text. unregister_key_type() garbage-collects the keys of
the type but, as far as I can see, nothing on that path waits for
already-queued call_rcu() callbacks (synchronize_rcu() is not enough for
that).

Sequence:
  1. a user who owns an encrypted key runs KEYCTL_UPDATE
     ("update <new master>"), which queues encrypted_rcu_free();
  2. before a grace period ends and the callback runs, root removes the
     module (no dependent module loaded);
  3. the callback runs from freed module memory.

The usual fix for this pattern is an rcu_barrier() in
cleanup_encrypted() after unregister_key_type(). trusted_core.c uses the
same call_rcu() pattern in trusted_update(); I have not checked its exit
path in detail.

Affected: the call_rcu() in encrypted_update() has been there since the
encrypted-keys code was moved to its own directory (61cf45d01990, 2011);
the code is unchanged in mainline ebb7a8dc1e74 and next-20261125.
Trigger: needs root to unload the module; queueing the callback needs
only a key owner. Impact: use-after-free of module text, i.e. a crash or
execution of reused memory, in a narrow window after the unload.

Found by a formal analysis tool we are developing that uses AI
assistance; I have reviewed the analysis and this report. The tool runs
verbatim copies of the kernel functions with stubs for the rest of the
kernel; it found the interleaving above, and the interleaving was
replayed natively on that harness under the tool's schedule. Functions
involved: encrypted_update(), encrypted_rcu_free(), cleanup_encrypted()
(RCU core and module unloading stubbed). It has not been reproduced on a
booted kernel.

Thanks,
Zhenwen Liang

             reply	other threads:[~2026-11-26  2:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  2:35 Zhenwen Liang [this message]
2026-11-26 19:07 ` KEYS: encrypted: encrypted_rcu_free() callbacks can outlive the module Jarkko Sakkinen
2026-11-26 20:00   ` Zhenwen Liang
2026-11-26 20:05     ` 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=20261126023538.4160950-1-zhenwenliang@google.com \
    --to=zhenwenliang@google.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=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