[tip: perf/urgent] perf: Fix use-after-free in error path
From: tip-bot2 for Peter Zijlstra
Date: Tue Dec 27 2022 - 06:51:36 EST
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: a551844e345ba2a1c533dee4b55cb0efddb1bcda
Gitweb: https://git.kernel.org/tip/a551844e345ba2a1c533dee4b55cb0efddb1bcda
Author: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
AuthorDate: Mon, 19 Dec 2022 15:40:04 +01:00
Committer: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
CommitterDate: Tue, 27 Dec 2022 12:44:01 +01:00
perf: Fix use-after-free in error path
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: bd2756811766 ("perf: Rewrite core context handling")
Reported-by: syzbot+b8e8c01c8ade4fe6e48f@xxxxxxxxxxxxxxxxxxxxxxxxx
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Tested-by: Chengming Zhou <zhouchengming@xxxxxxxxxxxxx>
Link: https://lkml.kernel.org/r/Y6B3xEgkbmFUCeni@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
---
kernel/events/core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 37c0f04..63d674c 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -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);
@@ -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);