From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752012AbdCNOc3 (ORCPT ); Tue, 14 Mar 2017 10:32:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37618 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750909AbdCNOcH (ORCPT ); Tue, 14 Mar 2017 10:32:07 -0400 Date: Tue, 14 Mar 2017 15:30:11 +0100 From: Oleg Nesterov To: Peter Zijlstra Cc: Dmitry Vyukov , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , LKML , Mathieu Desnoyers , syzkaller Subject: Re: perf: use-after-free in perf_release Message-ID: <20170314143010.GA30351@redhat.com> References: <20170306131459.GC6515@twins.programming.kicks-ass.net> <20170307140414.GA31678@redhat.com> <20170307165131.GA6097@redhat.com> <20170314125508.GK3343@twins.programming.kicks-ass.net> <20170314140302.GA28146@redhat.com> <20170314140754.GG3328@twins.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170314140754.GG3328@twins.programming.kicks-ass.net> User-Agent: Mutt/1.5.18 (2008-05-17) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 14 Mar 2017 14:32:07 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/14, Peter Zijlstra wrote: > > On Tue, Mar 14, 2017 at 03:03:02PM +0100, Oleg Nesterov wrote: > > On 03/14, Peter Zijlstra wrote: > > > > > > Yes, this looks buggy. But I cannot explain how that would result in the > > > observed use-after-free. > > > > Yes... > > > > Suppose that copy_process() fails after perf_event_init_task(). In this > > case perf_event_free_task() does put_ctx(), but if this ctx has another > > reference (ctx->refcount > 1) then ctx->task will point to the already > > freed task, copy_process() does free_task() at the end of error path. > > And we can't replace it with put_task_struct(). > > > > I am looking at TASK_TOMBSTONE, perhaps perf_event_free_task() should > > use it too? > > The idea was that the task isn't visible when we use > perf_event_free_task(). But I'll have a look. I can be easily wrong, I do not understans this code. But. perf_event_init_task() adds child_event to parent_event->child_list. If perf_event_release_kernel(parent_event) is called before copy_process() does perf_event_free_task() which (in particular) removes it from child_list, perf_event_release_kernel() can find this child_event and do get_ctx(ctx) (under the list_for_each_entry(child, &event->child_list, child_list) loop). Then it does put_ctx(ctx), but ctx->task can be already freed by copy_process()->free_task() in this case. No? Oleg.