perf: Reject group leaders detached by CPU hot-unplug
PATCH 1/1 perf: Reject group leaders detached by CPU hot-unplug
When a CPU goes offline, __perf_event_exit_context() removes every event of that CPU's context with DETACH_GROUP. That clears PERF_ATTACH_GROUP and PERF_ATTACH_CONTEXT and leaves the event in PERF_EVENT_STATE_OFF, or in ERROR, while the file descriptor stays open and event->ctx keeps pointing at the per-CPU context; nothing re-attaches it when the CPU comes back. Commit fa091f46c383 ("perf: Reject exited events as group leaders") fixed the same defect for remove-on-exec, by rejecting a leader in a state <= PERF_EVENT_STATE_EXIT. OFF and ERROR are both above EXIT, so once the CPU is online again a leader detached by hot-unplug is still accepted and perf_group_attach() links the new event to it.
perf_group_detach() returns early when PERF_ATTACH_GROUP is already clear, which is right for the double detach it was written for, where no group is left to break up. With a sibling added after the hot-unplug detach it skips a sibling_list which is no longer empty: the leader is freed with that sibling still on it and still pointing at it through ->group_leader. Closing the sibling, or a group read or ioctl on it, then uses the freed event.
Commit 42c5ca1f0a28 ("perf/core: Fix group leader use-after-free after sibling detach") fixed the mirror image, a sibling detached by hot-unplug and the leader closed afterwards. It promotes the sibling from the paths below that early return, so a leader detached first and given a sibling afterwards still reaches the same free.
Reproduced on v7.4-rc2, which contains that commit, with KASAN in a two-CPU guest, as root:
BUG: KASAN: slab-use-after-free in perf_event_update_time Call Trace: perf_event_update_time __event_disable __perf_remove_from_context event_function Freed by task 149 from an RCU callback queued by perf_event_release_kernel(), via perf_release() and __fput().
Opening a CPU event needs CAP_PERFMON or perf_event_paranoid below 1, so an ordinary user cannot reach this on a default configuration.
Reject a leader that is no longer attached to its context, rather than promoting siblings of a detached leader in perf_group_detach(): the early return there is right, and the anomaly is a leader on no context taking new siblings for a group that can never be scheduled. Test PERF_ATTACH_CONTEXT, which only the __perf_remove_from_context() path clears, so every detached leader is covered whatever state it is left in, and test it under ctx->mutex, where the leader is already known to share this context and every detach path holds that mutex across the removal.
One open that works today fails as well: adding a hardware event to a detached, pure software leader takes the move_group path, which installs the leader again before the new event is linked to it, so that group was valid. Events removed by hot-unplug are not scheduled again, which the commit in the Fixes tag relies on, so it now gets ENODEV like any other open on a detached leader.
---
Tested on mainline b3dfbde62cf1 (kernel/events identical to v7.4-rc2), x86-64 with KASAN, PROVE_LOCKING and DEBUG_LIST, kasan_multi_shot=1, 2 vCPUs under KVM, on a kernel carrying only this patch. Without it a software sibling is accepted and the close produces 15 KASAN reports; with it both the software and the hardware open fail with ENODEV and no report appears. An unrelated test that moves an inherited software group to the hardware context still passes.
Not tested: other architectures, hotplug or suspend on real hardware, the perf tool's own test suite.
kernel/events/core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/events/core.c +12 -0
@@ -14274,6 +14274,18 @@ SYSCALL_DEFINE5(perf_event_open, goto err_locked; } +/* +* Likewise against CPU hot-unplug, which detaches the events +* of a CPU context and breaks up its groups without putting +* them in a terminal state. perf_group_detach() does nothing +* for a leader that is already detached, so a sibling added +* to one would stay linked to it after the leader is freed. +*/ +if (!(group_leader->attach_state &PERF_ATTACH_CONTEXT )) { + err = -ENODEV ; + goto err_locked; + } +/* * Only a group leader can be exclusive or pinned */
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] perf/core: KASLR bypass via…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] perf/core: KASLR bypass via attr.ksymbol in sys_perf_event_open() [1]
--
commit c3cb272ab78e2aa66bba3e8476259e9d9e767fba
Author: Daniel Wade <[email protected]>
perf: Reject group leaders detached by CPU hot-unplug
This commit rejects group leaders that are no longer attached to a context in perf_event_open() so siblings cannot be added to leaders detached by hot-unplug.
Quoted 9 lines
kernel/events/core.c +1
-0@@ -14288,6 +14288,18 @@ SYSCALL_DEFINE5(perf_event_open, goto err_locked; } +/*
kernel/events/core.c SYSCALL_DEFINE5(perf_event_open, · line 14301
Quoted 10 lines
kernel/events/core.c +10
-0+* Likewise against CPU hot-unplug, which detaches the events +* of a CPU context and breaks up its groups without putting +* them in a terminal state. perf_group_detach() does nothing +* for a leader that is already detached, so a sibling added +* to one would stay linked to it after the leader is freed. +*/ +if (!(group_leader->attach_state &PERF_ATTACH_CONTEXT )) { + err = -ENODEV ; + goto err_locked; + }
[Severity: Critical]
This problem wasn't introduced by this patch, but shouldn't the perf_allow_kernel() check earlier in perf_event_open() also cover attr.ksymbol and attr.bpf_event [1]?
An unprivileged user can open an event with exclude_kernel=1 alongside ksymbol=1 or bpf_event=1 to bypass perf_allow_kernel():
kernel/events/core.c:SYSCALL_DEFINE5(perf_event_open) {
...
if (!attr.exclude_kernel || attr.text_poke ||
((attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
!attr.exclude_callchain_kernel)) {
err = perf_allow_kernel();
if (err)
return err;
}
...
}When perf_event_ksymbol() or perf_event_bpf_event() later broadcasts sideband records via perf_iterate_sb(), perf_event_ksymbol_match() and perf_event_bpf_match() only test event->attr.ksymbol and event->attr.bpf_event without checking exclude_kernel, leaking raw kernel symbol and trampoline addresses to unprivileged users.
[1] https://sashiko.dev/#/bug/linux-8c1072ab-5584-4983-bcd9-f12f87444ea2
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 28 lines · Daniel Wade
When a CPU goes offline,
__perf_event_exit_context()removes every event of that CPU's context withDETACH_GROUP. That clearsPERF_ATTACH_GROUPandPERF_ATTACH_CONTEXTand leaves the event inPERF_EVENT_STATE_OFF, or in ERROR, while the file descriptor stays open andevent->ctxkeeps pointing at the per-CPU context; nothing re-attaches it when the CPU comes back. Commitfa091f46c383("perf: Reject exited events as group leaders") fixed the same defect for remove-on-exec, by rejecting a leader in a state <=PERF_EVENT_STATE_EXIT. OFF and ERROR are both above EXIT, so once the CPU is online again a leader detached by hot-unplug is still accepted andperf_group_attach()links the new event to it.
perf_group_detach()returns early whenPERF_ATTACH_GROUPis already clear, which is right for the double detach it was written for, where no group is left to break up. With a sibling added after the hot-unplug detach it skips asibling_listwhich is no longer empty: the leader is freed with that sibling still on it and still pointing at it through ->group_leader. Closing the sibling, or a group read or ioctl on it, then uses the freed event.Commit
42c5ca1f0a28("perf/core: Fix group leader use-after-free after sibling detach") fixed the mirror image, a sibling detached by hot-unplug and the leader closed afterwards. It promotes the sibling from the paths below that early return, so a leader detached first and given a sibling afterwards still reaches the same free.Reproduced on v7.4-rc2, which contains that commit, with KASAN in a two-CPU guest, as root:
Quoted 9 lines
BUG: KASAN: slab-use-after-free in perf_event_update_time Call Trace: perf_event_update_time __event_disable __perf_remove_from_context event_function Freed by task 149 from an RCU callback queued by perf_event_release_kernel(), via perf_release() and __fput().
Great find! Just for clarification and my reference:
A typical failing sequence is:
- A group contains leader L and sibling S.
- CPU hot-unplug detaches S and then L with DETACH_GROUP, which dismantles the group. 42c5ca1f0a28 ("perf/core: Fix group leader use-after-free after sibling detach") resets S->group_leader to S so that if L is freed, a PERF_IOC_FLAG_GROUP perf_ioctl() on S does not dereference the then stale S->group_leader.
- The CPU comes back online and sets cpuctx->online = 1, which allows perf_event_open() on that CPU again.
- perf_event_open() with group_fd = L attaches a new sibling S' to L,
so S'->group_leader points to L.
- L is closed; perf_group_detach() returns early because L is already detached, so the group is not dismantled, S' stays linked to L, and L is freed.
- Closing S', or a PERF_IOC_FLAG_GROUP perf_ioctl() on it, follows
S'->group_leader and dereferences the freed L.
Quoted 19 lines
Opening a CPU event needs
CAP_PERFMONorperf_event_paranoidbelow 1, so an ordinary user cannot reach this on a default configuration.Reject a leader that is no longer attached to its context, rather than promoting siblings of a detached leader in
perf_group_detach(): the early return there is right, and the anomaly is a leader on no context taking new siblings for a group that can never be scheduled. TestPERF_ATTACH_CONTEXT, which only the__perf_remove_from_context()path clears, so every detached leader is covered whatever state it is left in, and test it underctx->mutex, where the leader is already known to share this context and every detach path holds that mutex across the removal.One open that works today fails as well: adding a hardware event to a detached, pure software leader takes the
move_grouppath, which installs the leader again before the new event is linked to it, so that group was valid. Events removed by hot-unplug are not scheduled again, which the commit in the Fixes tag relies on, so it now gets ENODEV like any other open on a detached leader.
Quoted 3 lines
Fixes 226424eee809 ("perf: Fix corruption of sibling list with hotplug")Signed-off-by Daniel Wade <[email protected]>
Thank you,
Aditya
Peter Zijlstra
maintainerQuoted 19 lines · Daniel Wade
When a CPU goes offline,
__perf_event_exit_context()removes every event of that CPU's context withDETACH_GROUP. That clearsPERF_ATTACH_GROUPandPERF_ATTACH_CONTEXTand leaves the event inPERF_EVENT_STATE_OFF, or in ERROR, while the file descriptor stays open andevent->ctxkeeps pointing at the per-CPU context; nothing re-attaches it when the CPU comes back. Commitfa091f46c383("perf: Reject exited events as group leaders") fixed the same defect for remove-on-exec, by rejecting a leader in a state <=PERF_EVENT_STATE_EXIT. OFF and ERROR are both above EXIT, so once the CPU is online again a leader detached by hot-unplug is still accepted andperf_group_attach()links the new event to it.
perf_group_detach()returns early whenPERF_ATTACH_GROUPis already clear, which is right for the double detach it was written for, where no group is left to break up. With a sibling added after the hot-unplug detach it skips asibling_listwhich is no longer empty: the leader is freed with that sibling still on it and still pointing at it through ->group_leader. Closing the sibling, or a group read or ioctl on it, then uses the freed event.
Quoted 5 lines
Commit
42c5ca1f0a28("perf/core: Fix group leader use-after-free after sibling detach") fixed the mirror image, a sibling detached by hot-unplug and the leader closed afterwards. It promotes the sibling from the paths below that early return, so a leader detached first and given a sibling afterwards still reaches the same free.
Nice
Quoted 16 lines
kernel/events/core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)kernel/events/core.c +5
-0@@ -14274,6 +14274,18 @@ SYSCALL_DEFINE5(perf_event_open, goto err_locked; } +/* +* Likewise against CPU hot-unplug, which detaches the events +* of a CPU context and breaks up its groups without putting +* them in a terminal state. perf_group_detach() does nothing +* for a leader that is already detached, so a sibling added
kernel/events/core.c SYSCALL_DEFINE5(perf_event_open, · line 14291
Quoted 10 lines
kernel/events/core.c +7
-0+* to one would stay linked to it after the leader is freed. +*/ +if (!(group_leader->attach_state &PERF_ATTACH_CONTEXT )) { + err = -ENODEV ; + goto err_locked; + } +/* * Only a group leader can be exclusive or pinned */
It occurs to me that changeing __perf_event_exit_contxt() to use DETACH_EXIT | DETACH_GROUP, will also fix things, right?
In that case the STATE_EXIT check from fa091f46c383 will catch it.
Doing this makes the various cases (task-exit, CPU-offline) behave similarly.
Does that work for you?
Quoted 9 lines · Peter Zijlstra
It occurs to me that changeing
__perf_event_exit_contxt()to useDETACH_EXIT|DETACH_GROUP, will also fix things, right?In that case the
STATE_EXITcheck fromfa091f46c383will catch it.Doing this makes the various cases (task-exit, CPU-offline) behave similarly.
Does that work for you?
Yep this makes sense. Much cleaner. I will send a v2 doing that.
Although one small difference still: task exit wakes pollers and CPU offline doesn't. Which probably doesn't matter given they aren't woken today either.