* [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap()
@ 2026-08-31 13:31 Yilin Zhang
2026-08-31 13:39 ` Peter Zijlstra
2026-08-31 13:50 ` sashiko-bot
0 siblings, 2 replies; 4+ messages in thread
From: Yilin Zhang @ 2026-08-31 13:31 UTC (permalink / raw)
To: peterz
Cc: Yilin Zhang, mingo, acme, namhyung, kylebot, david.lee,
linux-perf-users, stable, Kimi Security Team, Weiming Shi
perf_mmap_close() drops rb->mmap_count *without* holding
event->mmap_mutex (the refcount_dec_and_test() right before the
refcount_dec_and_mutex_lock() of event->mmap_count). A concurrent
perf_mmap_rb() can slot its entire "revival" path into that window
(perf_mmap holds event->mmap_mutex for its whole duration, including
rb_alloc):
munmap side (perf_mmap_close) mmap side (perf_mmap_rb)
----------------------------------- --------------------------------
rb->mmap_count 1 -> 0 (no lock) (holds event->mmap_mutex)
inc_not_zero(rb->mmap_count) fails
ring_buffer_attach(event, NULL)
rb_alloc() + attach new rb
refcount_set(&event->mmap_count, 1)
lock; event->mmap_count 1 -> 0
ring_buffer_attach(event, NULL)
ring_buffer_put() -> frees the *new* rb
The revival's refcount_set(&event->mmap_count, 1) is an invisible
1 -> 1 write: the close frees the just-revived buffer although the
other process still has it mapped -- a page-level use-after-free
allowing local privilege escalation to root by any unprivileged user
(default kernel.perf_event_paranoid=2).
Swap the order of the two counter updates: event->mmap_count is
dropped first via refcount_dec_and_mutex_lock(), so its 1 -> 0
transition and the ring_buffer_attach() stay serialized with
perf_mmap(). rb->mmap_count == 0 then implies every event using the
buffer is detached already, so the result of the rb->mmap_count drop
can gate the remaining teardown directly and detach_rest is no longer
needed.
An earlier fix for this race from Kyle Zeng and David Lee takes
event->mmap_mutex around both counter updates [0]; here the not-last
close stays lockless.
Fixes: 59741451b49c ("perf: Identify the 0->1 transition for event::mmap_count")
Link: https://lore.kernel.org/linux-perf-users/20260804060931.711308-1-david.lee@trailofbits.com/ [0]
Cc: stable@vger.kernel.org # 6.18+
Reported-by: Kimi Security Team <bug-report@moonshot.ai>
Tested-by: Weiming Shi <shiweiming@moonshot.ai>
Signed-off-by: Yilin Zhang <yilinzhang@moonshot.ai>
---
v2: https://lore.kernel.org/linux-perf-users/91825d0335d2f7cc017ee23886ffeef44c1cc633.f061f277.0045.493a.937e.53242f19a7de@feishu.cn/T/#u
v3: swap the counter drop order instead of serializing both under
mmap_mutex; keeps the lockless fast path, drops detach_rest, and
references the earlier independent fix [0]
kernel/events/core.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index a6c8e38a3110..f56f9d9e4f01 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7029,7 +7029,6 @@ static void perf_mmap_close(struct vm_area_struct *vma)
mapped_f unmapped = get_mapped(event, event_unmapped);
struct perf_buffer *rb = ring_buffer_get(event);
struct user_struct *mmap_user = rb->mmap_user;
- bool detach_rest = false;
/* FIXIES vs perf_pmu_unregister() */
if (unmapped)
@@ -7060,17 +7059,14 @@ static void perf_mmap_close(struct vm_area_struct *vma)
mutex_unlock(&rb->aux_mutex);
}
- if (refcount_dec_and_test(&rb->mmap_count))
- detach_rest = true;
-
- if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
- goto out_put;
-
- ring_buffer_attach(event, NULL);
- mutex_unlock(&event->mmap_mutex);
+ if (refcount_dec_and_mutex_lock(&event->mmap_count,
+ &event->mmap_mutex)) {
+ ring_buffer_attach(event, NULL);
+ mutex_unlock(&event->mmap_mutex);
+ }
/* If there's still other mmap()s of this buffer, we're done. */
- if (!detach_rest)
+ if (!refcount_dec_and_test(&rb->mmap_count))
goto out_put;
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap()
2026-08-31 13:31 [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap() Yilin Zhang
@ 2026-08-31 13:39 ` Peter Zijlstra
2026-08-31 15:13 ` Yilin Zhang
2026-08-31 13:50 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Peter Zijlstra @ 2026-08-31 13:39 UTC (permalink / raw)
To: Yilin Zhang
Cc: mingo, acme, namhyung, kylebot, david.lee, linux-perf-users,
stable, Kimi Security Team, Weiming Shi
On Mon, Aug 31, 2026 at 09:31:52PM +0800, Yilin Zhang wrote:
> perf_mmap_close() drops rb->mmap_count *without* holding
> event->mmap_mutex (the refcount_dec_and_test() right before the
> refcount_dec_and_mutex_lock() of event->mmap_count). A concurrent
> perf_mmap_rb() can slot its entire "revival" path into that window
> (perf_mmap holds event->mmap_mutex for its whole duration, including
> rb_alloc):
>
> munmap side (perf_mmap_close) mmap side (perf_mmap_rb)
> ----------------------------------- --------------------------------
> rb->mmap_count 1 -> 0 (no lock) (holds event->mmap_mutex)
> inc_not_zero(rb->mmap_count) fails
> ring_buffer_attach(event, NULL)
> rb_alloc() + attach new rb
> refcount_set(&event->mmap_count, 1)
> lock; event->mmap_count 1 -> 0
> ring_buffer_attach(event, NULL)
> ring_buffer_put() -> frees the *new* rb
>
> The revival's refcount_set(&event->mmap_count, 1) is an invisible
> 1 -> 1 write: the close frees the just-revived buffer although the
> other process still has it mapped -- a page-level use-after-free
> allowing local privilege escalation to root by any unprivileged user
> (default kernel.perf_event_paranoid=2).
>
> Swap the order of the two counter updates: event->mmap_count is
> dropped first via refcount_dec_and_mutex_lock(), so its 1 -> 0
> transition and the ring_buffer_attach() stay serialized with
> perf_mmap(). rb->mmap_count == 0 then implies every event using the
> buffer is detached already, so the result of the rb->mmap_count drop
> can gate the remaining teardown directly and detach_rest is no longer
> needed.
>
> An earlier fix for this race from Kyle Zeng and David Lee takes
> event->mmap_mutex around both counter updates [0]; here the not-last
> close stays lockless.
>
> Fixes: 59741451b49c ("perf: Identify the 0->1 transition for event::mmap_count")
> Link: https://lore.kernel.org/linux-perf-users/20260804060931.711308-1-david.lee@trailofbits.com/ [0]
> Cc: stable@vger.kernel.org # 6.18+
> Reported-by: Kimi Security Team <bug-report@moonshot.ai>
> Tested-by: Weiming Shi <shiweiming@moonshot.ai>
> Signed-off-by: Yilin Zhang <yilinzhang@moonshot.ai>
> ---
> v2: https://lore.kernel.org/linux-perf-users/91825d0335d2f7cc017ee23886ffeef44c1cc633.f061f277.0045.493a.937e.53242f19a7de@feishu.cn/T/#u
> v3: swap the counter drop order instead of serializing both under
> mmap_mutex; keeps the lockless fast path, drops detach_rest, and
> references the earlier independent fix [0]
I presume this means you and your AI agree with my pre-wakeup-juice
morning musings?
Also, you seem to have lost the 'helpful' comments that I drafted :-(
> kernel/events/core.c | 16 ++++++----------
> 1 file changed, 6 insertions(+), 10 deletions(-)
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index a6c8e38a3110..f56f9d9e4f01 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -7029,7 +7029,6 @@ static void perf_mmap_close(struct vm_area_struct *vma)
> mapped_f unmapped = get_mapped(event, event_unmapped);
> struct perf_buffer *rb = ring_buffer_get(event);
> struct user_struct *mmap_user = rb->mmap_user;
> - bool detach_rest = false;
>
> /* FIXIES vs perf_pmu_unregister() */
> if (unmapped)
> @@ -7060,17 +7059,14 @@ static void perf_mmap_close(struct vm_area_struct *vma)
> mutex_unlock(&rb->aux_mutex);
> }
>
> - if (refcount_dec_and_test(&rb->mmap_count))
> - detach_rest = true;
> -
> - if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
> - goto out_put;
> -
> - ring_buffer_attach(event, NULL);
> - mutex_unlock(&event->mmap_mutex);
> + if (refcount_dec_and_mutex_lock(&event->mmap_count,
> + &event->mmap_mutex)) {
> + ring_buffer_attach(event, NULL);
> + mutex_unlock(&event->mmap_mutex);
> + }
>
> /* If there's still other mmap()s of this buffer, we're done. */
> - if (!detach_rest)
> + if (!refcount_dec_and_test(&rb->mmap_count))
> goto out_put;
>
> /*
> --
> 2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap()
2026-08-31 13:31 [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap() Yilin Zhang
2026-08-31 13:39 ` Peter Zijlstra
@ 2026-08-31 13:50 ` sashiko-bot
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-08-31 13:50 UTC (permalink / raw)
To: Yilin Zhang; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Permanent leak of locked_vm accounting when mmapping the same perf event ring buffer or AUX buffer multiple times.
--
commit bd560c15b9fb2009d96bf58b9046c04989905b44
Author: Yilin Zhang <yilinzhang@moonshot.ai>
perf: Fix use-after-free when perf mmap() revival races with the last munmap()
This commit fixes a use-after-free vulnerability in the perf events subsystem
by swapping the order of counter decrements in perf_mmap_close(). The change
ensures that event->mmap_count is decremented first under the mmap_mutex,
closing a race window where a newly attached ring buffer could be mistakenly
freed.
Link: https://lore.kernel.org/linux-perf-users/20260804060931.711308-1-david.lee@trailofbits.com/
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index a6c8e38a31104..f56f9d9e4f01b 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -7060,17 +7059,14 @@ static void perf_mmap_close(struct vm_area_struct *vma)
> mutex_unlock(&rb->aux_mutex);
> }
>
> - if (refcount_dec_and_test(&rb->mmap_count))
> - detach_rest = true;
> -
> - if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
> - goto out_put;
> -
> - ring_buffer_attach(event, NULL);
> - mutex_unlock(&event->mmap_mutex);
> + if (refcount_dec_and_mutex_lock(&event->mmap_count,
> + &event->mmap_mutex)) {
> + ring_buffer_attach(event, NULL);
> + mutex_unlock(&event->mmap_mutex);
> + }
>
> /* If there's still other mmap()s of this buffer, we're done. */
> - if (!detach_rest)
> + if (!refcount_dec_and_test(&rb->mmap_count))
> goto out_put;
[Severity: High]
This is a pre-existing issue, but does the early return here leak the
locked_vm accounting when the same perf event ring buffer or AUX buffer is
mmapped multiple times?
Looking at the mmap side in perf_mmap_rb() and perf_mmap_aux(), secondary
mmaps repeatedly charge the user's locked_vm:
kernel/events/core.c:perf_mmap_rb() {
...
perf_mmap_account(vma, user_extra, extra);
refcount_inc(&event->mmap_count);
...
}
kernel/events/core.c:perf_mmap_aux() {
...
perf_mmap_account(vma, user_extra, extra);
refcount_inc(&event->mmap_count);
...
}
However, the unaccounting in perf_mmap_close() only happens exactly once when
the last mmap is closed and rb->mmap_count reaches zero:
kernel/events/core.c:perf_mmap_close() {
...
if (!refcount_dec_and_test(&rb->mmap_count))
goto out_put;
...
perf_mmap_unaccount(vma, rb);
out_put:
ring_buffer_put(rb);
}
Because locked_vm is used system-wide to enforce RLIMIT_MEMLOCK (e.g., by
io_uring, BPF, XDP, and secretmem), an attacker could trivially loop calling
mmap() on the same file descriptor to artificially inflate this counter until
the limit is exhausted.
Does this create a persistent Denial of Service for all processes running
under that UID, making the system fully unusable for the affected user's
services until all processes of that user exit?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260831133152.1231045-1-yilinzhang@moonshot.ai?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap()
2026-08-31 13:39 ` Peter Zijlstra
@ 2026-08-31 15:13 ` Yilin Zhang
0 siblings, 0 replies; 4+ messages in thread
From: Yilin Zhang @ 2026-08-31 15:13 UTC (permalink / raw)
To: peterz
Cc: Yilin Zhang, mingo, acme, namhyung, kylebot, david.lee, stable,
bug-report, shiweiming, linux-perf-users
Sorry for just posting v3 instead of replying to you here first. It
is your ordering, with one refinement.
In your version the lockless refcount_dec(&rb->mmap_count) can be
the one to drop it to 0: dec_not_one() drops event->mmap_count
without the mutex, so a second closer can zero the event count in
between.
That warns, and detach_rest is never set, so the last mapping skips
the detach from redirecting events. On 8 vCPU QEMU it hits within a
minute:
refcount_t: decrement hit 0; leaking memory.
RIP: perf_mmap_close+0x221 (refcount_warn_saturate(REFCOUNT_DEC_LEAK))
The rb drop itself now gates the teardown: rb->mmap_count == 0
implies every event is detached already.
> Also, you seem to have lost the 'helpful' comments that I drafted :-(
Sorry for dropping your comments and the Suggested-by tag. v4 restores
both and follows shortly.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-08-31 15:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-31 13:31 [PATCH v3] perf: Fix use-after-free when perf mmap() revival races with the last munmap() Yilin Zhang
2026-08-31 13:39 ` Peter Zijlstra
2026-08-31 15:13 ` Yilin Zhang
2026-08-31 13:50 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox