Skip to content

Commit e593031

Browse files
kylebot-oaigregkh
authored andcommitted
perf: Reject exited events as group leaders
[ Upstream commit fa091f4 ] perf_event_remove_on_exec() sets remove-on-exec events to the EXIT state and detaches their group relationships. The event's file descriptor can remain open, however, and perf_event_open() currently accepts that event as a group leader because its early validation rejects only REVOKED and DEAD events. A new sibling can consequently be linked to the detached leader. When the leader is closed, perf_group_detach() observes that its PERF_ATTACH_GROUP bit is already clear and skips the new sibling. The sibling then retains a group_leader pointer to the freed event. Reject group leaders in the EXIT state. Perform the check while holding the shared context mutex so that an exec in the target task cannot detach the leader between validation and group attachment. [peterz: make the earlier test fully consistent] Fixes: 037a3c4 ("perf/core: Detach event groups during remove_on_exec") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Kyle Zeng <kylebot@openai.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://patch.msgid.link/20260806205655.75722-1-kylebot@openai.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 42a2949 commit e593031

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

kernel/events/core.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12939,6 +12939,10 @@ SYSCALL_DEFINE5(perf_event_open,
1293912939
if (err)
1294012940
goto err_fd;
1294112941
group_leader = fd_file(group)->private_data;
12942+
if (group_leader->state <= PERF_EVENT_STATE_EXIT) {
12943+
err = -ENODEV;
12944+
goto err_group_fd;
12945+
}
1294212946
if (flags & PERF_FLAG_FD_OUTPUT)
1294312947
output_event = group_leader;
1294412948
if (flags & PERF_FLAG_FD_NO_GROUP)
@@ -13066,6 +13070,12 @@ SYSCALL_DEFINE5(perf_event_open,
1306613070
if (group_leader->ctx != ctx)
1306713071
goto err_locked;
1306813072

13073+
/* Recheck under ctx::mutex to serialize against remove-on-exec. */
13074+
if (group_leader->state <= PERF_EVENT_STATE_EXIT) {
13075+
err = -ENODEV;
13076+
goto err_locked;
13077+
}
13078+
1306913079
/*
1307013080
* Only a group leader can be exclusive or pinned
1307113081
*/

0 commit comments

Comments
 (0)