Re: [PATCH v2] module: Fix use-after-free in module_put() tracepoint

From: Aaron Tomlin

Date: Mon Nov 09 2026 - 15:11:08 EST


On Mon, Nov 09, 2026 at 04:16:30PM +0100, Petr Pavlu wrote:
> > 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,
--
Aaron Tomlin