Skip to contentSkip to conversation

perf: Reject group leaders detached by CPU hot-unplug

PATCH New Daniel Wade 2 participants 2 messages Nov 19 perf events

lore
Share & tools
lore
#0 lore raw

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.

Fixes 226424eee809 ("perf: Fix corruption of sibling list with hotplug")

---
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
index f5f2261f1fe6..2de4c3cc37ac 100644
@@ -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);
 }
 
#1 lore raw
Sashiko 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_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").

Quoted 6 lines

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.

Fixes 226424eee809 ("perf: Fix corruption of sibling list with hotplug")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1