aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Zijlstra <peterz@infradead.org>2017-01-26 10:39:55 -0500
committerIngo Molnar <mingo@kernel.org>2017-01-30 05:41:25 -0500
commita76a82a3e38c8d3fb6499e3dfaeb0949241ab588 (patch)
treeb5bc906278fe1ac66d75de984d26bf59b43b3ed8
parent566cf877a1fcb6d6dc0126b076aad062054c2637 (diff)
perf/core: Fix use-after-free bug
Dmitry reported a KASAN use-after-free on event->group_leader. It turns out there's a hole in perf_remove_from_context() due to event_function_call() not calling its function when the task associated with the event is already dead. In this case the event will have been detached from the task, but the grouping will have been retained, such that group operations might still work properly while there are live child events etc. This does however mean that we can miss a perf_group_detach() call when the group decomposes, this in turn can then lead to use-after-free. Fix it by explicitly doing the group detach if its still required. Reported-by: Dmitry Vyukov <dvyukov@google.com> Tested-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: Thomas Gleixner <tglx@linutronix.de> Cc: stable@vger.kernel.org # v4.5+ Cc: syzkaller <syzkaller@googlegroups.com> Fixes: 63b6da39bb38 ("perf: Fix perf_event_exit_task() race") Link: http://lkml.kernel.org/r/20170126153955.GD6515@twins.programming.kicks-ass.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--kernel/events/core.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 110b38a58493..4e1f4c0070ce 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1469,7 +1469,6 @@ ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1469static void 1469static void
1470list_add_event(struct perf_event *event, struct perf_event_context *ctx) 1470list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1471{ 1471{
1472
1473 lockdep_assert_held(&ctx->lock); 1472 lockdep_assert_held(&ctx->lock);
1474 1473
1475 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); 1474 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
@@ -1624,6 +1623,8 @@ static void perf_group_attach(struct perf_event *event)
1624{ 1623{
1625 struct perf_event *group_leader = event->group_leader, *pos; 1624 struct perf_event *group_leader = event->group_leader, *pos;
1626 1625
1626 lockdep_assert_held(&event->ctx->lock);
1627
1627 /* 1628 /*
1628 * We can have double attach due to group movement in perf_event_open. 1629 * We can have double attach due to group movement in perf_event_open.
1629 */ 1630 */
@@ -1697,6 +1698,8 @@ static void perf_group_detach(struct perf_event *event)
1697 struct perf_event *sibling, *tmp; 1698 struct perf_event *sibling, *tmp;
1698 struct list_head *list = NULL; 1699 struct list_head *list = NULL;
1699 1700
1701 lockdep_assert_held(&event->ctx->lock);
1702
1700 /* 1703 /*
1701 * We can have double detach due to exit/hot-unplug + close. 1704 * We can have double detach due to exit/hot-unplug + close.
1702 */ 1705 */
@@ -1895,9 +1898,29 @@ __perf_remove_from_context(struct perf_event *event,
1895 */ 1898 */
1896static void perf_remove_from_context(struct perf_event *event, unsigned long flags) 1899static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
1897{ 1900{
1898 lockdep_assert_held(&event->ctx->mutex); 1901 struct perf_event_context *ctx = event->ctx;
1902
1903 lockdep_assert_held(&ctx->mutex);
1899 1904
1900 event_function_call(event, __perf_remove_from_context, (void *)flags); 1905 event_function_call(event, __perf_remove_from_context, (void *)flags);
1906
1907 /*
1908 * The above event_function_call() can NO-OP when it hits
1909 * TASK_TOMBSTONE. In that case we must already have been detached
1910 * from the context (by perf_event_exit_event()) but the grouping
1911 * might still be in-tact.
1912 */
1913 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1914 if ((flags & DETACH_GROUP) &&
1915 (event->attach_state & PERF_ATTACH_GROUP)) {
1916 /*
1917 * Since in that case we cannot possibly be scheduled, simply
1918 * detach now.
1919 */
1920 raw_spin_lock_irq(&ctx->lock);
1921 perf_group_detach(event);
1922 raw_spin_unlock_irq(&ctx->lock);
1923 }
1901} 1924}
1902 1925
1903/* 1926/*