perf: Reject group leaders detached by CPU hot-unplug
PATCH v2 1/1 perf: Reject group leaders detached by CPU hot-unplug
CPU hot-unplug detaches the events of a CPU context but leaves them in OFF or ERROR, so once the CPU is back a detached leader is still accepted as group_fd. perf_group_detach() does nothing for a leader that is already detached, so closing it frees it with the new sibling still linked, and closing the sibling is a use-after-free.
Detach them with DETACH_EXIT as well, so that they end up in PERF_EVENT_STATE_EXIT and are rejected by the check from commit fa091f46c383 ("perf: Reject exited events as group leaders").
Side effect: poll() on a hot-unplugged event now reports EPOLLHUP, and it is refused as group_fd even with PERF_FLAG_FD_NO_GROUP.
---
v2: use DETACH_EXIT at hot-unplug instead of a new check, as Peter suggested.
v1: https://lore.kernel.org/all/[email protected]/
Tested on mainline b3dfbde62cf1, x86-64 with KASAN and PROVE_LOCKING, 2 vCPUs under KVM. Without the patch a sibling is accepted on a hot-unplugged leader and the close gives 15 KASAN reports. With it the open fails with ENODEV and no report appears.
kernel/events/core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/events/core.c +4 -2
@@ -15342,8 +15342,10 @@ static void __perf_event_exit_context(void *__info)raw_spin_lock (&ctx->lock);ctx_sched_out (ctx,NULL ,EVENT_TIME ); -list_for_each_entry (event, &ctx->event_list, event_entry) -__perf_remove_from_context (event, cpuctx, ctx, (void *)DETACH_GROUP ); +list_for_each_entry (event, &ctx->event_list, event_entry) { +__perf_remove_from_context (event, cpuctx, ctx, + (void *)(DETACH_EXIT |DETACH_GROUP )); + }raw_spin_unlock (&ctx->lock); }
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 10 lines
CPU hot-unplug detaches the events of a CPU context but leaves them in OFF or ERROR, so once the CPU is back a detached leader is still accepted as
group_fd.perf_group_detach()does nothing for a leader that is already detached, so closing it frees it with the new sibling still linked, and closing the sibling is a use-after-free.Detach them with
DETACH_EXITas well, so that they end up inPERF_EVENT_STATE_EXITand are rejected by the check from commitfa091f46c383("perf: Reject exited events as group leaders").
Quoted 6 lines
Side effect:
poll()on a hot-unplugged event now reports EPOLLHUP, and it is refused asgroup_fdeven withPERF_FLAG_FD_NO_GROUP.Fixes 226424eee809 ("perf: Fix corruption of sibling list with hotplug")Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Daniel Wade <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1