module: Fix use-after-free in module_put() tracepoint
Aaron Tomlin
reviewerPATCH v2 1/1 module: Fix use-after-free in module_put() tracepoint
A use-after-free condition can arise in module_put() should a thread be preempted immediately after decrementing the module's reference count while the module is concurrently being unloaded.
In module_put(), the module's reference count is decremented via atomic_dec_if_positive() before the module pointer is passed to trace_module_put(). Consider a scenario where CPU 0 decrements the reference count without reaching the baseline MODULE_REF_BASE level and is immediately preempted: it relinquishes its reference while still retaining a local pointer to the module. If a concurrent CPU 1 subsequently drops the final active reference, the module's reference count reaches MODULE_REF_BASE. This permits the module unload sequence (via delete_module() or the initialisation-failure tear-down path) to proceed and invoke free_module().
Function free_module() invokes synchronize_rcu() and subsequently releases the memory backing struct module via free_mod_mem(). Because CPU 0 was preempted outside an RCU read-side critical section, synchronize_rcu() does not await CPU 0. When CPU 0 eventually resumes execution, it passes the dangling module pointer to trace_module_put(), which dereferences mod->name and mod->refcnt, culminating in a use-after-free. This sequence of events is illustrated below:
CPU 0 CPU 1
----- -----
module_put(module)
atomic_dec_if_positive()
[preempted]
module_put(module)
atomic_dec_if_positive()
// drops final reference
free_module(module)
synchronize_rcu()
free_mod_mem(module)
trace_module_put(module, ...)
mod->nameResolve this by moving trace_module_put() prior to atomic_dec_if_positive(). Because the caller holds an active reference to the module at this point, the module structure and its name are guaranteed to remain valid throughout the tracepoint invocation without requiring preemption disabling or RCU synchronisation.
---
Changes since v1:
- Drop guard(rcu)() and relocate trace_module_put() before the atomic decrement instead, avoiding RCU overhead on every module_put() while safely relying on the caller's held reference (Petr Pavlu)
- Link to v1: https://lore.kernel.org/all/[email protected]/
--- kernel/module/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/module/main.c +1 -1
@@ -953,9 +953,9 @@ void module_put(struct module *module)int ret;if (module) { +trace_module_put (module, _RET_IP_); ret =atomic_dec_if_positive (&module->refcnt);WARN_ON (ret <0 );/* Failed to put refcount */ -trace_module_put (module, _RET_IP_); } }EXPORT_SYMBOL (module_put);
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] module: trace_module_put() records pre-decrement refcnt in module_put() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] module: trace_module_put() records pre-decrement refcnt in module_put() --
commit 2c168275e42fc7a4482d549cb0c9f1fc9f71e395
Author: Aaron Tomlin <[email protected]>
module: Fix use-after-free in module_put() tracepoint
This commit moves trace_module_put() before atomic_dec_if_positive() in module_put() to prevent a use-after-free if the thread is preempted after decrementing the module reference count while the module is being unloaded.
kernel/module/main.c void module_put(struct module *module) · line 959
Quoted 12 lines
kernel/module/main.c +1
-1@@ -954,9 +954,9 @@ void module_put(struct module *module)int ret;if (module) { +trace_module_put (module, _RET_IP_); ret =atomic_dec_if_positive (&module->refcnt);WARN_ON (ret <0 );/* Failed to put refcount */ -trace_module_put (module, _RET_IP_);
[Severity: Medium]
Does moving trace_module_put() before atomic_dec_if_positive() in module_put() cause the module_put trace event to record a reference count that is off by one?
In include/trace/events/module.h, both module_get and module_put use the module_refcnt event class, which reads mod->refcnt directly inside TP_fast_assign():
include/trace/events/module.h:
DECLARE_EVENT_CLASS(module_refcnt,
... TP_fast_assign( __entry->ip = ip; __entry->refcnt = atomic_read(&mod->refcnt); __assign_str(name); ),
In kernel/module/main.c, __module_get() and try_module_get() call trace_module_get() after incrementing module->refcnt, recording the post-increment value:
kernel/module/main.c:__module_get() {
if (module) {
atomic_inc(&module->refcnt);
trace_module_get(module, _RET_IP_);
}
}With trace_module_put() now invoked before atomic_dec_if_positive() in module_put() when a non-null module pointer is passed and the module_put tracepoint is enabled, TP_fast_assign() reads atomic_read(&mod->refcnt) before the decrement occurs. As a result, a matched get and put pair on a live module logs the pre-decrement refcnt on module_put instead of the post-decrement refcnt.
Quoted 3 lines
kernel/module/main.c +0
-0} }EXPORT_SYMBOL (module_put);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Aaron Tomlin
reviewerQuoted 20 lines · On Thu, Oct 22, 2026 at 06:52:48PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] module:trace_module_put()records pre-decrement refcnt inmodule_put()--commit
2c168275e42fc7a4482d549cb0c9f1fc9f71e395
Author: Aaron Tomlin <[email protected]>module: Fix use-after-free in
module_put()tracepointThis commit moves
trace_module_put()beforeatomic_dec_if_positive()inmodule_put()to prevent a use-after-free if the thread is preempted after decrementing the module reference count while the module is being unloaded.Quoted 7 lines
kernel/module/main.c +0
-0@@ -954,9 +954,9 @@ void module_put(struct module *module)int ret;
Quoted 10 lines
kernel/module/main.c void module_put(struct module *module) · line 958
Quoted 5 lines
kernel/module/main.c +1
-1if (module) { +trace_module_put (module, _RET_IP_); ret =atomic_dec_if_positive (&module->refcnt);WARN_ON (ret <0 );/* Failed to put refcount */ -trace_module_put (module, _RET_IP_);[Severity: Medium]
Does movingtrace_module_put()beforeatomic_dec_if_positive()inmodule_put()cause themodule_puttrace event to record a reference count that is off by one?
Yes, moving trace_module_put() prior to atomic_dec_if_positive() means it records the pre-decrement reference count rather than the post-decrement count. Introducing unconditional RCU read-side locks on every call across the kernel solely to keep an inactive debug tracepoint post-decrement introduces unnecessary overhead. Relocating the tracepoint compiles down to a zero-overhead static jump label when tracing is disabled.
Signature · 1 line
Aaron Tomlin
Quoted 44 lines · Aaron Tomlin
A use-after-free condition can arise in
module_put()should a thread be preempted immediately after decrementing the module's reference count while the module is concurrently being unloaded.In
module_put(), the module's reference count is decremented viaatomic_dec_if_positive()before the module pointer is passed totrace_module_put(). Consider a scenario where CPU 0 decrements the reference count without reaching the baselineMODULE_REF_BASElevel and is immediately preempted: it relinquishes its reference while still retaining a local pointer to the module. If a concurrent CPU 1 subsequently drops the final active reference, the module's reference count reachesMODULE_REF_BASE. This permits the module unload sequence (viadelete_module()or the initialisation-failure tear-down path) to proceed and invokefree_module().Function
free_module()invokessynchronize_rcu()and subsequently releases the memory backing struct module viafree_mod_mem(). Because CPU 0 was preempted outside an RCU read-side critical section,synchronize_rcu()does not await CPU 0. When CPU 0 eventually resumes execution, it passes the dangling module pointer totrace_module_put(), which dereferencesmod->nameandmod->refcnt, culminating in a use-after-free. This sequence of events is illustrated below:CPU 0 CPU 1 ----- ----- module_put(module) atomic_dec_if_positive() [preempted] module_put(module) atomic_dec_if_positive() // drops final reference free_module(module) synchronize_rcu() free_mod_mem(module) trace_module_put(module, ...) mod->nameResolve this by moving
trace_module_put()prior toatomic_dec_if_positive(). Because the caller holds an active reference to the module at this point, the module structure and its name are guaranteed to remain valid throughout the tracepoint invocation without requiring preemption disabling or RCU synchronisation.
Quoted 3 lines
Fixes cb0b50b813f6 ("module: Remove preempt_disable() from module reference counting.")Signed-off-by Aaron Tomlin <[email protected]>
This version #2 looks ok to me. I plan to pick it up on the modules tree.
The additional version #3 [1] seems unnecessarily complicated to me. It is ok to report the value of module->refcnt before it is decremented. As noted earlier, other code, such as xfs_group_put() and objagg_obj_put(), takes the same approach.
[1] https://lore.kernel.org/linux-modules/[email protected]/
Signature · 2 lines
Thanks, Petr
Aaron Tomlin
reviewerQuoted 45 lines · Petr Pavlu
Quoted 44 lines · Aaron Tomlin
A use-after-free condition can arise in
module_put()should a thread be preempted immediately after decrementing the module's reference count while the module is concurrently being unloaded.In
module_put(), the module's reference count is decremented viaatomic_dec_if_positive()before the module pointer is passed totrace_module_put(). Consider a scenario where CPU 0 decrements the reference count without reaching the baselineMODULE_REF_BASElevel and is immediately preempted: it relinquishes its reference while still retaining a local pointer to the module. If a concurrent CPU 1 subsequently drops the final active reference, the module's reference count reachesMODULE_REF_BASE. This permits the module unload sequence (viadelete_module()or the initialisation-failure tear-down path) to proceed and invokefree_module().Function
free_module()invokessynchronize_rcu()and subsequently releases the memory backing struct module viafree_mod_mem(). Because CPU 0 was preempted outside an RCU read-side critical section,synchronize_rcu()does not await CPU 0. When CPU 0 eventually resumes execution, it passes the dangling module pointer totrace_module_put(), which dereferencesmod->nameandmod->refcnt, culminating in a use-after-free. This sequence of events is illustrated below:CPU 0 CPU 1 ----- ----- module_put(module) atomic_dec_if_positive() [preempted] module_put(module) atomic_dec_if_positive() // drops final reference free_module(module) synchronize_rcu() free_mod_mem(module) trace_module_put(module, ...) mod->nameResolve this by moving
trace_module_put()prior toatomic_dec_if_positive(). Because the caller holds an active reference to the module at this point, the module structure and its name are guaranteed to remain valid throughout the tracepoint invocation without requiring preemption disabling or RCU synchronisation.
Quoted 8 lines
Quoted 3 lines
Fixes cb0b50b813f6 ("module: Remove preempt_disable() from module reference counting.")Signed-off-by Aaron Tomlin <[email protected]>This version #2 looks ok to me. I plan to pick it up on the modules tree.
Reviewed-by Petr Pavlu <[email protected]>
Hi Petr,
Quoted 6 lines
The additional version #3 [1] seems unnecessarily complicated to me. It is ok to report the value of
module->refcntbefore it is decremented. As noted earlier, other code, such asxfs_group_put()andobjagg_obj_put(), takes the same approach.[1] https://lore.kernel.org/linux-modules/[email protected]/
Thank you.
However, I do prefer the next iteration [1], since the use-after-free is resolved without altering existing tracepoint semantics or introducing an off-by-one discrepancy in the recorded reference count. Finally, the fast code path is fully preserved when the tracepoint is disabled.
I found an example in dma_fence_wait_timeout(). The following is an excerpt of drivers/dma-buf/dma-fence.c:555-559:
if (trace_dma_fence_wait_end_enabled()) {
rcu_read_lock();
trace_dma_fence_wait_end(fence);
rcu_read_unlock();
}Uses trace_dma_fence_wait_end_enabled() to avoid entering an RCU read-side critical section unless the tracepoint is active, protecting access to RCU-freed dma_fence state inside the tracepoint.
[1]: https://lore.kernel.org/linux-modules/[email protected]/
Kind regards,
Signature · 1 line
Aaron Tomlin
Petr Pavlu
maintainerQuoted 62 lines · Aaron Tomlin
Quoted 45 lines · Petr Pavlu
Quoted 44 lines · Aaron Tomlin
A use-after-free condition can arise in
module_put()should a thread be preempted immediately after decrementing the module's reference count while the module is concurrently being unloaded.In
module_put(), the module's reference count is decremented viaatomic_dec_if_positive()before the module pointer is passed totrace_module_put(). Consider a scenario where CPU 0 decrements the reference count without reaching the baselineMODULE_REF_BASElevel and is immediately preempted: it relinquishes its reference while still retaining a local pointer to the module. If a concurrent CPU 1 subsequently drops the final active reference, the module's reference count reachesMODULE_REF_BASE. This permits the module unload sequence (viadelete_module()or the initialisation-failure tear-down path) to proceed and invokefree_module().Function
free_module()invokessynchronize_rcu()and subsequently releases the memory backing struct module viafree_mod_mem(). Because CPU 0 was preempted outside an RCU read-side critical section,synchronize_rcu()does not await CPU 0. When CPU 0 eventually resumes execution, it passes the dangling module pointer totrace_module_put(), which dereferencesmod->nameandmod->refcnt, culminating in a use-after-free. This sequence of events is illustrated below:CPU 0 CPU 1 ----- ----- module_put(module) atomic_dec_if_positive() [preempted] module_put(module) atomic_dec_if_positive() // drops final reference free_module(module) synchronize_rcu() free_mod_mem(module) trace_module_put(module, ...) mod->nameResolve this by moving
trace_module_put()prior toatomic_dec_if_positive(). Because the caller holds an active reference to the module at this point, the module structure and its name are guaranteed to remain valid throughout the tracepoint invocation without requiring preemption disabling or RCU synchronisation.Quoted 8 lines
Quoted 3 lines
Fixes cb0b50b813f6 ("module: Remove preempt_disable() from module reference counting.")Signed-off-by Aaron Tomlin <[email protected]>This version #2 looks ok to me. I plan to pick it up on the modules tree.
Reviewed-by Petr Pavlu <[email protected]>Hi Petr,
Quoted 5 lines
The additional version #3 [1] seems unnecessarily complicated to me. It is ok to report the value of
module->refcntbefore it is decremented. As noted earlier, other code, such asxfs_group_put()andobjagg_obj_put(), takes the same approach.
Quoted 8 lines
Quoted 1 line
[1] https://lore.kernel.org/linux-modules/[email protected]/
Thank you.
However, I do prefer the next iteration [1], since the use-after-free is resolved without altering existing tracepoint semantics or introducing an off-by-one discrepancy in the recorded reference count. Finally, the fast code path is fully preserved when the tracepoint is disabled.
Why is it important to preserve what this tracepoint reports? The value isn't off by one, rather the tracepoint would now report the value before the decrement.
I'm trying to understand why this case matters enough to justify the additional complexity in version #3.
Signature · 2 lines
Cheers, Petr
Aaron Tomlin
reviewerQuoted 10 lines · Petr Pavlu
Quoted 4 lines
However, I do prefer the next iteration [1], since the use-after-free is resolved without altering existing tracepoint semantics or introducing an off-by-one discrepancy in the recorded reference count. Finally, the fast code path is fully preserved when the tracepoint is disabled.
Why is it important to preserve what this tracepoint reports? The value isn't off by one, rather the tracepoint would now report the value before the decrement.
I'm trying to understand why this case matters enough to justify the additional complexity in version #3.
Fair enough.
The main reason is consistency with trace_module_get().
It shares the module_refcnt event class and records the post-operation reference count after atomic_inc()/atomic_inc_not_zero().
Moving trace_module_put() before atomic_dec_if_positive() means module_get() logs the post-operation count while module_put logs the pre-operation count. So, a paired get and put (1 -> 2 -> 1) reports "refcnt=2" for both events and never shows the count returning to MODULE_REF_BASE (refcnt=1).
Fortunately, gating guard(rcu)() behind trace_module_put_enabled() (i.e. similar to dma_fence_wait_timeout()) preserves the existing post-decrement reporting with no fast-path cost when tracing is disabled.
Kind regards,
Signature · 1 line
Aaron Tomlin