linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Dmitry Vyukov <dvyukov@google.com>
Cc: Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	syzkaller <syzkaller@googlegroups.com>,
	Kostya Serebryany <kcc@google.com>,
	Alexander Potapenko <glider@google.com>,
	Eric Dumazet <edumazet@google.com>,
	Sasha Levin <sasha.levin@oracle.com>
Subject: Re: use-after-free in __perf_install_in_context
Date: Tue, 8 Dec 2015 17:22:27 +0100	[thread overview]
Message-ID: <20151208162227.GB6357@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <CACT4Y+bQqHPsxdh5nhSA154KeCxRirk8QGwRe-5pMUToGE8yAg@mail.gmail.com>

On Fri, Dec 04, 2015 at 09:04:35PM +0100, Dmitry Vyukov wrote:
> Hello,
> 
> While running syzkaller fuzzer I am seeing lots of the following
> use-after-free reports. Unfortunately all my numerous attempts to
> reproduce them in a controlled environment failed. They pop up during
> fuzzing periodically (once in several hours in a single VM), but
> whenever I try to stress-replay what happened in the VM before the
> report, the use-after-free does not reproduce. 

Typical that :/

> Can somebody knowledgeable in perf subsystem look at the report? Maybe
> it is possible to figure out what happened based purely on the report.
> I can pretty reliably test any proposed fixes.

So I'm still going over the code, but meanwhile I tried reproducing this
using the perf_fuzzer and some debug code, but no luck with that.

Since you seem to be able to reproduce, could you do a run with the
below patch in to see if it tickles something?


---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 36babfd..58e504c 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -872,13 +872,22 @@ static void free_ctx(struct rcu_head *head)
 	kfree(ctx);
 }
 
+#define for_each_task_context_nr(ctxn)					\
+	for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
+
 static void put_ctx(struct perf_event_context *ctx)
 {
 	if (atomic_dec_and_test(&ctx->refcount)) {
 		if (ctx->parent_ctx)
 			put_ctx(ctx->parent_ctx);
-		if (ctx->task)
+		if (ctx->task) {
+			int ctxn;
+
+			for_each_task_context_nr(ctxn)
+				WARN_ON(ctx->task->perf_event_ctxp[ctxn] == ctx);
+
 			put_task_struct(ctx->task);
+		}
 		call_rcu(&ctx->rcu_head, free_ctx);
 	}
 }
@@ -2649,9 +2658,6 @@ static void perf_pmu_sched_task(struct task_struct *prev,
 static void perf_event_switch(struct task_struct *task,
 			      struct task_struct *next_prev, bool sched_in);
 
-#define for_each_task_context_nr(ctxn)					\
-	for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
-
 /*
  * Called from scheduler to remove the events of the current task,
  * with interrupts disabled.
@@ -8931,6 +8937,8 @@ again:
 
 		mutex_unlock(&ctx->mutex);
 
+		task->perf_event_ctxp[ctxn] = NULL;
+
 		put_ctx(ctx);
 	}
 }

  parent reply	other threads:[~2015-12-08 16:22 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-04 20:04 use-after-free in __perf_install_in_context Dmitry Vyukov
2015-12-04 20:32 ` Alexei Starovoitov
2015-12-04 21:00   ` Dmitry Vyukov
2015-12-07 11:04     ` Dmitry Vyukov
2015-12-07 11:06       ` Dmitry Vyukov
2015-12-07 11:24         ` Dmitry Vyukov
2015-12-07 15:36 ` Peter Zijlstra
2015-12-07 16:09   ` Dmitry Vyukov
2015-12-08  3:24     ` Alexei Starovoitov
2015-12-08 16:12       ` Dmitry Vyukov
2015-12-08 17:54         ` Alexei Starovoitov
2015-12-08 17:56           ` Dmitry Vyukov
2015-12-08 18:05             ` Alexei Starovoitov
2015-12-08 18:35               ` Dmitry Vyukov
2015-12-08 19:56                 ` Alexei Starovoitov
2015-12-09  9:17                   ` Dmitry Vyukov
2015-12-10  3:54                     ` Alexei Starovoitov
2015-12-10  9:02                       ` Peter Zijlstra
2015-12-10 17:03                         ` Alexei Starovoitov
2015-12-11  8:14                           ` Ingo Molnar
2015-12-15 13:11                             ` Dmitry Vyukov
2015-12-08 16:44     ` Peter Zijlstra
2015-12-08 19:14       ` Dmitry Vyukov
2015-12-10 19:57         ` Peter Zijlstra
2015-12-15 13:09           ` Dmitry Vyukov
2015-12-17 14:06           ` Peter Zijlstra
2015-12-17 14:08             ` Dmitry Vyukov
2015-12-17 14:26               ` Peter Zijlstra
2015-12-17 14:28                 ` Peter Zijlstra
2015-12-17 14:35                   ` Dmitry Vyukov
2015-12-17 14:43                     ` Peter Zijlstra
2015-12-31 17:15                       ` Dmitry Vyukov
2016-01-05 12:17                         ` Peter Zijlstra
2016-01-08  8:40                           ` Dmitry Vyukov
2016-01-08 10:28                             ` Dmitry Vyukov
2016-01-06 18:46           ` [tip:perf/core] perf: Fix race in perf_event_exec() tip-bot for Peter Zijlstra
2016-01-06 18:56             ` Eric Dumazet
2016-01-07 13:40               ` Peter Zijlstra
2016-01-07 16:26                 ` Paul E. McKenney
2016-01-07 16:36                   ` Eric Dumazet
2016-01-07 16:46                     ` Paul E. McKenney
2015-12-08 16:22 ` Peter Zijlstra [this message]
2015-12-08 18:57   ` use-after-free in __perf_install_in_context Ingo Molnar
2015-12-09  9:05     ` Peter Zijlstra
2015-12-08 16:27 ` Peter Zijlstra
2015-12-08 16:50   ` Dmitry Vyukov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151208162227.GB6357@twins.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=dvyukov@google.com \
    --cc=edumazet@google.com \
    --cc=glider@google.com \
    --cc=kcc@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=sasha.levin@oracle.com \
    --cc=syzkaller@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).