From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754526AbdCPPnb (ORCPT ); Thu, 16 Mar 2017 11:43:31 -0400 Received: from terminus.zytor.com ([65.50.211.136]:56592 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752690AbdCPPnX (ORCPT ); Thu, 16 Mar 2017 11:43:23 -0400 Date: Thu, 16 Mar 2017 08:19:26 -0700 From: tip-bot for Peter Zijlstra Message-ID: Cc: jolsa@redhat.com, dvyukov@google.com, vincent.weaver@maine.edu, alexander.shishkin@linux.intel.com, acme@kernel.org, acme@redhat.com, linux-kernel@vger.kernel.org, torvalds@linux-foundation.org, eranian@google.com, peterz@infradead.org, mathieu.desnoyers@efficios.com, oleg@redhat.com, hpa@zytor.com, mingo@kernel.org, tglx@linutronix.de Reply-To: mingo@kernel.org, tglx@linutronix.de, torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, acme@redhat.com, eranian@google.com, mathieu.desnoyers@efficios.com, peterz@infradead.org, hpa@zytor.com, oleg@redhat.com, vincent.weaver@maine.edu, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, dvyukov@google.com In-Reply-To: <20170316125823.140295131@infradead.org> References: <20170314155949.GE32474@worktop> <20170316125823.140295131@infradead.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] perf/core: Fix use-after-free in perf_release() Git-Commit-ID: e552a8389aa409e257b7dcba74f67f128f979ccc X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: e552a8389aa409e257b7dcba74f67f128f979ccc Gitweb: http://git.kernel.org/tip/e552a8389aa409e257b7dcba74f67f128f979ccc Author: Peter Zijlstra AuthorDate: Thu, 16 Mar 2017 13:47:48 +0100 Committer: Ingo Molnar CommitDate: Thu, 16 Mar 2017 14:16:52 +0100 perf/core: Fix use-after-free in perf_release() 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 Reported-by: Dmitry Vyukov Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Linus Torvalds Cc: Mathieu Desnoyers Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Vince Weaver Cc: fweisbec@gmail.com Cc: oleg@redhat.com Cc: stable@vger.kernel.org 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 --- kernel/events/core.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index 1031bdf..4742909 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10415,6 +10415,17 @@ void perf_event_free_task(struct task_struct *task) continue; mutex_lock(&ctx->mutex); + raw_spin_lock_irq(&ctx->lock); + /* + * Destroy the task <-> ctx relation and mark the context dead. + * + * This is important because even though the task hasn't been + * exposed yet the context has been (through child_list). + */ + RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL); + WRITE_ONCE(ctx->task, TASK_TOMBSTONE); + put_task_struct(task); /* cannot be last */ + raw_spin_unlock_irq(&ctx->lock); again: list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)