From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752182AbeDIHih (ORCPT ); Mon, 9 Apr 2018 03:38:37 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:39910 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751770AbeDIHig (ORCPT ); Mon, 9 Apr 2018 03:38:36 -0400 Date: Mon, 9 Apr 2018 09:38:27 +0200 From: Peter Zijlstra To: Prashant Bhole Cc: Oleg Nesterov , Ingo Molnar , Steven Rostedt , Arnaldo Carvalho de Melo , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: Re: uprobes/perf: KASAN: use-after-free in uprobe_perf_close Message-ID: <20180409073827.GS4082@hirez.programming.kicks-ass.net> References: <4da123ee-1ad1-fbd3-d5c0-bd9f5ed26434@lab.ntt.co.jp> <20180222163715.GA1485@redhat.com> <20180222170427.GQ25181@hirez.programming.kicks-ass.net> <20180222170947.GW25235@hirez.programming.kicks-ass.net> <20180222174041.GA2802@redhat.com> <35f890f7-1194-4509-a1e4-d63d1c7d22ab@lab.ntt.co.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <35f890f7-1194-4509-a1e4-d63d1c7d22ab@lab.ntt.co.jp> User-Agent: Mutt/1.9.3 (2018-01-21) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 06, 2018 at 06:49:10PM +0900, Prashant Bhole wrote: > Sorry for late reply. I tried these changes. It didn't fix the problem. With He, sorry for completely forgetting about this one :/ > these changes, the use-after-free access of task_struct occurs at > _free_event() for the last remaining event. > > In your changes, I tried keeping get/put_task_struct() in > perf_alloc_context()/put_ctx() intact and The problem did not occur. Changes > are mentioned below. Yes, I think you're right in that this is the cleanest solution; it adds reference counting to the exact pointer we're using. > diff --git a/kernel/events/core.c b/kernel/events/core.c > index c98cce4ceebd..65889d2b5ae2 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -4109,6 +4109,8 @@ static void _free_event(struct perf_event *event) > > if (event->ctx) > put_ctx(event->ctx); > + if (event->hw.target) > + put_task_struct(event->hw.target); > > exclusive_event_destroy(event); > module_put(event->pmu->module); > @@ -9593,6 +9595,7 @@ perf_event_alloc(struct perf_event_attr *attr, int > cpu, > * and we cannot use the ctx information because we need the > * pmu before we get a ctx. > */ > + get_task_struct(task); > event->hw.target = task; > } > > @@ -9708,6 +9711,8 @@ perf_event_alloc(struct perf_event_attr *attr, int > cpu, > perf_detach_cgroup(event); > if (event->ns) > put_pid_ns(event->ns); > + if (task) Should this not too be 'event->hw.target', for consistency and clarity? > + put_task_struct(task); > kfree(event); > > return ERR_PTR(err);