Skip to content

Commit 517e6a3

Browse files
author
Peter Zijlstra
committed
perf: Fix perf_pending_task() UaF
Per syzbot it is possible for perf_pending_task() to run after the event is free()'d. There are two related but distinct cases: - the task_work was already queued before destroying the event; - destroying the event itself queues the task_work. The first cannot be solved using task_work_cancel() since perf_release() itself might be called from a task_work (____fput), which means the current->task_works list is already empty and task_work_cancel() won't be able to find the perf_pending_task() entry. The simplest alternative is extending the perf_event lifetime to cover the task_work. The second is just silly, queueing a task_work while you know the event is going away makes no sense and is easily avoided by re-arranging how the event is marked STATE_DEAD and ensuring it goes through STATE_OFF on the way down. Reported-by: [email protected] Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Marco Elver <[email protected]>
1 parent 030a976 commit 517e6a3

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

kernel/events/core.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,7 @@ event_sched_out(struct perf_event *event,
22912291
!event->pending_work) {
22922292
event->pending_work = 1;
22932293
dec = false;
2294+
WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount));
22942295
task_work_add(current, &event->pending_task, TWA_RESUME);
22952296
}
22962297
if (dec)
@@ -2336,6 +2337,7 @@ group_sched_out(struct perf_event *group_event,
23362337

23372338
#define DETACH_GROUP 0x01UL
23382339
#define DETACH_CHILD 0x02UL
2340+
#define DETACH_DEAD 0x04UL
23392341

23402342
/*
23412343
* Cross CPU call to remove a performance event
@@ -2356,12 +2358,20 @@ __perf_remove_from_context(struct perf_event *event,
23562358
update_cgrp_time_from_cpuctx(cpuctx, false);
23572359
}
23582360

2361+
/*
2362+
* Ensure event_sched_out() switches to OFF, at the very least
2363+
* this avoids raising perf_pending_task() at this time.
2364+
*/
2365+
if (flags & DETACH_DEAD)
2366+
event->pending_disable = 1;
23592367
event_sched_out(event, cpuctx, ctx);
23602368
if (flags & DETACH_GROUP)
23612369
perf_group_detach(event);
23622370
if (flags & DETACH_CHILD)
23632371
perf_child_detach(event);
23642372
list_del_event(event, ctx);
2373+
if (flags & DETACH_DEAD)
2374+
event->state = PERF_EVENT_STATE_DEAD;
23652375

23662376
if (!ctx->nr_events && ctx->is_active) {
23672377
if (ctx == &cpuctx->ctx)
@@ -5121,9 +5131,7 @@ int perf_event_release_kernel(struct perf_event *event)
51215131

51225132
ctx = perf_event_ctx_lock(event);
51235133
WARN_ON_ONCE(ctx->parent_ctx);
5124-
perf_remove_from_context(event, DETACH_GROUP);
51255134

5126-
raw_spin_lock_irq(&ctx->lock);
51275135
/*
51285136
* Mark this event as STATE_DEAD, there is no external reference to it
51295137
* anymore.
@@ -5135,8 +5143,7 @@ int perf_event_release_kernel(struct perf_event *event)
51355143
* Thus this guarantees that we will in fact observe and kill _ALL_
51365144
* child events.
51375145
*/
5138-
event->state = PERF_EVENT_STATE_DEAD;
5139-
raw_spin_unlock_irq(&ctx->lock);
5146+
perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD);
51405147

51415148
perf_event_ctx_unlock(event, ctx);
51425149

@@ -6577,6 +6584,8 @@ static void perf_pending_task(struct callback_head *head)
65776584
if (rctx >= 0)
65786585
perf_swevent_put_recursion_context(rctx);
65796586
preempt_enable_notrace();
6587+
6588+
put_event(event);
65806589
}
65816590

65826591
#ifdef CONFIG_GUEST_PERF_EVENTS

0 commit comments

Comments
 (0)