From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751039AbdEEQpL (ORCPT ); Fri, 5 May 2017 12:45:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50946 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750995AbdEEQpJ (ORCPT ); Fri, 5 May 2017 12:45:09 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com ECA5DC059747 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=oleg@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com ECA5DC059747 Date: Fri, 5 May 2017 18:44:28 +0200 From: Oleg Nesterov To: Vegard Nossum Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Frederic Weisbecker , Jamie Iles , Peter Zijlstra , Thomas Gleixner , Andy Lutomirski Subject: Re: [PATCH] kthread: fix use-after-free if kthread fork fails Message-ID: <20170505164428.GA500@redhat.com> References: <20170505162034.4338-1-vegard.nossum@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170505162034.4338-1-vegard.nossum@oracle.com> 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.32]); Fri, 05 May 2017 16:45:09 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/05, Vegard Nossum wrote: > > If a kthread forks (e.g. usermodehelper since commit 1da5c46fa965) but > fails in copy_process() between calling dup_task_struct() and setting > p->set_child_tid, then the value of p->set_child_tid will be inherited > from the parent and get prematurely freed by free_kthread_struct(). Aaah... thanks! > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -518,6 +518,13 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) > atomic_set(&tsk->stack_refcount, 1); > #endif > > + /* > + * Forking kthreads (e.g. usermodehelper) should not inherit this > + * field since it's a pointer to a 'struct kthread' which is not > + * reference counted. > + */ > + tsk->set_child_tid = NULL; > + Can't we just move both p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; /* * Clear TID on mm_release()? */ p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; lines here? Oleg.