Skip to content

Commit

Permalink
perf: Fix use-after-free in error path
Browse files Browse the repository at this point in the history
The syscall error path has a use-after-free; put_pmu_ctx() will
reference ctx, therefore we must ensure ctx is destroyed after pmu_ctx
is.

Fixes: bd27568 ("perf: Rewrite core context handling")
Reported-by: [email protected]
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Tested-by: Chengming Zhou <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
  • Loading branch information
Peter Zijlstra committed Dec 27, 2022
1 parent 0824567 commit a551844
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -12671,7 +12671,8 @@ SYSCALL_DEFINE5(perf_event_open,
return event_fd;

err_context:
/* event->pmu_ctx freed by free_event() */
put_pmu_ctx(event->pmu_ctx);
event->pmu_ctx = NULL; /* _free_event() */
err_locked:
mutex_unlock(&ctx->mutex);
perf_unpin_context(ctx);
Expand Down Expand Up @@ -12784,6 +12785,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,

err_pmu_ctx:
put_pmu_ctx(pmu_ctx);
event->pmu_ctx = NULL; /* _free_event() */
err_unlock:
mutex_unlock(&ctx->mutex);
perf_unpin_context(ctx);
Expand Down

0 comments on commit a551844

Please sign in to comment.