Skip to contentSkip to conversation

KEYS: encrypted: encrypted_rcu_free() callbacks can outlive the module

DISCUSSION Zhenwen Liang 2 participants 4 messages Nov 26

Subsystems (3)
lore
Share & tools
lore
#0 lore 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

maintainer
#1 lore raw
Quoted 16 lines · Zhenwen Liang

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);
    }
Quoted 7 lines

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).

This will be ultimately addressed by:

https://lore.kernel.org/keyrings/[email protected]/

As then operations will be always drained.

It's actually somewhat tedious to address this particular issue without this type of architectural change but I'm open for fixes.

Just giving that as a point of reflection.

Quoted 29 lines

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.

Quoted 2 lines

Thanks,
Zhenwen Liang

I think the issue is legit.

Br, Jarkko

#2 lore raw
Quoted 1 line · Jarkko Sakkinen

This will be ultimately addressed by:

[...]

Quoted 4 lines

As then operations will be always drained.

It's actually somewhat tedious to address this particular issue without this type of architectural change but I'm open for fixes.

Thanks. For trees that will not get the series, I think a minimal fix is possible here, because unregister_key_type() already guarantees that no new ->update() can run once it returns: key_create_or_update() holds key_types_sem across __key_update(), key_update() checks key->type->update under key->sem, and the dead-type GC pass retypes each remaining key to key_type_dead under key->sem before unregister_key_type() returns. So every encrypted_rcu_free() has been queued by then, and an rcu_barrier() after unregister_key_type() in cleanup_encrypted() waits for all of them. trusted.ko has the same pattern (trusted_update() uses call_rcu(), and none of the exit paths waits for pending callbacks), so cleanup_trusted() needs the same.

I reproduced it in QEMU with KASAN and CONFIG_RCU_LAZY=y: add an encrypted key, update it 20 times, and unload the module right away. The rcuop kthread hit int3 at encrypted_rcu_free+0x1 in the freed module text in 5 of 5 runs. With the barrier, 5 of 5 runs were clean.

With your series applied, the barrier is still needed as far as I can
see: the GC calls synchronize_rcu() before the module_put() in key_gc_unused_keys(), and key_gc_flush() is only used by dns_resolver, NFS idmap, the SMB client and rxrpc. On top of the series, the barrier could become a key_gc_flush() call.

I will send two small patches (encrypted, trusted) with Fixes: tags and
Cc: stable; feel free to drop the stable tag if you think it is not worth it.

Thanks,
Zhenwen Liang

maintainer
#3 lore raw
Quoted 31 lines · Zhenwen Liang
Quoted 1 line · Jarkko Sakkinen

This will be ultimately addressed by:

[...]

Quoted 4 lines

As then operations will be always drained.

It's actually somewhat tedious to address this particular issue without this type of architectural change but I'm open for fixes.

Thanks. For trees that will not get the series, I think a minimal fix is possible here, because unregister_key_type() already guarantees that no new ->update() can run once it returns: key_create_or_update() holds key_types_sem across __key_update(), key_update() checks key->type->update under key->sem, and the dead-type GC pass retypes each remaining key to key_type_dead under key->sem before unregister_key_type() returns. So every encrypted_rcu_free() has been queued by then, and an rcu_barrier() after unregister_key_type() in cleanup_encrypted() waits for all of them. trusted.ko has the same pattern (trusted_update() uses call_rcu(), and none of the exit paths waits for pending callbacks), so cleanup_trusted() needs the same.

I reproduced it in QEMU with KASAN and CONFIG_RCU_LAZY=y: add an encrypted key, update it 20 times, and unload the module right away. The rcuop kthread hit int3 at encrypted_rcu_free+0x1 in the freed module text in 5 of 5 runs. With the barrier, 5 of 5 runs were clean.

With your series applied, the barrier is still needed as far as I can
see: the GC calls synchronize_rcu() before the module_put() in key_gc_unused_keys(), and key_gc_flush() is only used by dns_resolver, NFS idmap, the SMB client and rxrpc. On top of the series, the barrier could become a key_gc_flush() call.

Quoted 6 lines

I will send two small patches (encrypted, trusted) with Fixes: tags and
Cc: stable; feel free to drop the stable tag if you think it is not worth it.

Thanks,
Zhenwen Liang

I saw the patches. I'll look at them with time as soon as I have appropriate bandwidth (might next up til next week not sure).

Br, Jarkko