diff options
| author | Peter Zijlstra <peterz@infradead.org> | 2017-03-16 08:47:48 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-03-26 07:05:57 -0400 |
| commit | c04a938229e79ff52c4b9f027b89f544aeb3234a (patch) | |
| tree | 8efc9ed9ddd7a01ec20dc6d1ddd7ff9572d07cd9 /kernel/events | |
| parent | 13695ce5b1024b38cfecbd334ec5ddcaac1b6807 (diff) | |
perf/core: Fix use-after-free in perf_release()
commit e552a8389aa409e257b7dcba74f67f128f979ccc upstream.
Dmitry reported syzcaller tripped a use-after-free in perf_release().
After much puzzlement Oleg spotted the below scenario:
Task1 Task2
fork()
perf_event_init_task()
/* ... */
goto bad_fork_$foo;
/* ... */
perf_event_free_task()
mutex_lock(ctx->lock)
perf_free_event(B)
perf_event_release_kernel(A)
mutex_lock(A->child_mutex)
list_for_each_entry(child, ...) {
/* child == B */
ctx = B->ctx;
get_ctx(ctx);
mutex_unlock(A->child_mutex);
mutex_lock(A->child_mutex)
list_del_init(B->child_list)
mutex_unlock(A->child_mutex)
/* ... */
mutex_unlock(ctx->lock);
put_ctx() /* >0 */
free_task();
mutex_lock(ctx->lock);
mutex_lock(A->child_mutex);
/* ... */
mutex_unlock(A->child_mutex);
mutex_unlock(ctx->lock)
put_ctx() /* 0 */
ctx->task && !TOMBSTONE
put_task_struct() /* UAF */
This patch closes the hole by making perf_event_free_task() destroy the
task <-> ctx relation such that perf_event_release_kernel() will no longer
observe the now dead task.
Spotted-by: Oleg Nesterov <oleg@redhat.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vince Weaver <vincent.weaver@maine.edu>
Cc: fweisbec@gmail.com
Cc: oleg@redhat.com
Fixes: c6e5b73242d2 ("perf: Synchronously clean up child events")
Link: http://lkml.kernel.org/r/20170314155949.GE32474@worktop
Link: http://lkml.kernel.org/r/20170316125823.140295131@infradead.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'kernel/events')
| -rw-r--r-- | kernel/events/core.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/kernel/events/core.c b/kernel/events/core.c index 4b3323151a2f..ef05f8c15f95 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c | |||
| @@ -10333,6 +10333,17 @@ void perf_event_free_task(struct task_struct *task) | |||
| 10333 | continue; | 10333 | continue; |
| 10334 | 10334 | ||
| 10335 | mutex_lock(&ctx->mutex); | 10335 | mutex_lock(&ctx->mutex); |
| 10336 | raw_spin_lock_irq(&ctx->lock); | ||
| 10337 | /* | ||
| 10338 | * Destroy the task <-> ctx relation and mark the context dead. | ||
| 10339 | * | ||
| 10340 | * This is important because even though the task hasn't been | ||
| 10341 | * exposed yet the context has been (through child_list). | ||
| 10342 | */ | ||
| 10343 | RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); | ||
| 10344 | WRITE_ONCE(ctx->task, TASK_TOMBSTONE); | ||
| 10345 | put_task_struct(task); /* cannot be last */ | ||
| 10346 | raw_spin_unlock_irq(&ctx->lock); | ||
| 10336 | again: | 10347 | again: |
| 10337 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, | 10348 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, |
| 10338 | group_entry) | 10349 | group_entry) |
