Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp903557ybk; Sat, 9 May 2020 23:51:06 -0700 (PDT) X-Google-Smtp-Source: APiQypJaEdiTb2SXh7NNrhQlLu4UDG9CpwxqSOMsjvdIZeGA7yd/zJCrvkKAwvTh3B7s/Hvs4oAj X-Received: by 2002:a17:907:392:: with SMTP id ss18mr2446415ejb.156.1589093466631; Sat, 09 May 2020 23:51:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589093466; cv=none; d=google.com; s=arc-20160816; b=SOXNZxYDH21nvDDsK7ricV9iqcN4iK7fBatfggcosEIx1+U/nvm3yd9l4s3c7z8dPJ swKr6FWM1oZwCMPe3CdIP9uAi4hYlro8Msk2xLWgUk2xapE/iAL1yA3EKOPM4QN6pPTY U+oqgxJUsDjkOv4GIUaN3su6fzMWtTO5QChxDIS/sXaVGowN+Po+WIVMAqQGj7VR2GcV rmurApfnAJ8PpuS2xxQ6l2ejKQe9E8FHDBtb2mfXid1qNz/uBnPsrpuoGmpLjTpdQCfh TLrxI42mseuiHx1SFfu+8QiG/dni1VEz6f4yoUswB6iYpYVnejWWMvHvybhLPxeHL99L VpdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=HQ+RpfNfgRzKfhq3E7DL5XDmLT8BrZJ4UrAN5K2WSS0=; b=i8OcdZadvDlYGq+uFL4Je2cFiPaIgG5Qhw2BaFaLVNVVS1JHufWEp/LWrabVGQ18/q PmBaEjjy4Ksanz+AUDsI/ClpbAmRoPN+HsTWSJBxkpnmsnG4F227b8ik6Jr3NUedCQsk rW1t0gOo5cD/T0l7KSLzJSuf5v8jR/8MPNyFX2NzrrWAwV/CcnoWxhfeUXZrWNHia9Z0 q3YQ2jaxhKjEn9BlRarA425fpCV8j3k7h6sb0EvaoocO7wwHZkFfYgomcZ+Y2yjpbpYR 41jkv6K6kUlHoOjCDjyp7nlZXs1mYgi4+FH94UxhsOVbBnYKaLzGmeKxHrT0znqAzxdJ 13xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="RxXU+/m2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qt4si3980636ejb.447.2020.05.09.23.50.27; Sat, 09 May 2020 23:51:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="RxXU+/m2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726629AbgEJGsY (ORCPT + 99 others); Sun, 10 May 2020 02:48:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:53762 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725810AbgEJGsY (ORCPT ); Sun, 10 May 2020 02:48:24 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DF9B120746; Sun, 10 May 2020 06:48:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589093303; bh=Jt2ZmI4jxEXSvlstEfwyzyeZhTT/glbADv+7sZp1TwM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=RxXU+/m2DIjg87amFaxqurQLKs8quLyPScABwqfg6oUVQYOgHAk22GwA8BT4I/ZOg vo0hj6YZunBgBahCuG69MHoD4rN8o9pa80PXNORzq+RacX1jBiDvZEbEBVs5N6nWCn /tH+qpAMgsDVE0olYy6weZqNYg1ccGSbjgU9K6nQ= Date: Sun, 10 May 2020 08:48:19 +0200 From: Greg KH To: rananta@codeaurora.org Cc: jslaby@suse.com, andrew@daynix.com, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] tty: hvc: Fix data abort due to race in hvc_open Message-ID: <20200510064819.GB3400311@kroah.com> References: <20200428032601.22127-1-rananta@codeaurora.org> <20200506094851.GA2787548@kroah.com> <98bbe7afabf48d8e8fe839fdc9e836a5@codeaurora.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <98bbe7afabf48d8e8fe839fdc9e836a5@codeaurora.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, May 09, 2020 at 06:30:56PM -0700, rananta@codeaurora.org wrote: > On 2020-05-06 02:48, Greg KH wrote: > > On Mon, Apr 27, 2020 at 08:26:01PM -0700, Raghavendra Rao Ananta wrote: > > > Potentially, hvc_open() can be called in parallel when two tasks calls > > > open() on /dev/hvcX. In such a scenario, if the > > > hp->ops->notifier_add() > > > callback in the function fails, where it sets the tty->driver_data to > > > NULL, the parallel hvc_open() can see this NULL and cause a memory > > > abort. > > > Hence, serialize hvc_open and check if tty->private_data is NULL > > > before > > > proceeding ahead. > > > > > > The issue can be easily reproduced by launching two tasks > > > simultaneously > > > that does nothing but open() and close() on /dev/hvcX. > > > For example: > > > $ ./simple_open_close /dev/hvc0 & ./simple_open_close /dev/hvc0 & > > > > > > Signed-off-by: Raghavendra Rao Ananta > > > --- > > > drivers/tty/hvc/hvc_console.c | 16 ++++++++++++++-- > > > 1 file changed, 14 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/tty/hvc/hvc_console.c > > > b/drivers/tty/hvc/hvc_console.c > > > index 436cc51c92c3..ebe26fe5ac09 100644 > > > --- a/drivers/tty/hvc/hvc_console.c > > > +++ b/drivers/tty/hvc/hvc_console.c > > > @@ -75,6 +75,8 @@ static LIST_HEAD(hvc_structs); > > > */ > > > static DEFINE_MUTEX(hvc_structs_mutex); > > > > > > +/* Mutex to serialize hvc_open */ > > > +static DEFINE_MUTEX(hvc_open_mutex); > > > /* > > > * This value is used to assign a tty->index value to a hvc_struct > > > based > > > * upon order of exposure via hvc_probe(), when we can not match it > > > to > > > @@ -346,16 +348,24 @@ static int hvc_install(struct tty_driver > > > *driver, struct tty_struct *tty) > > > */ > > > static int hvc_open(struct tty_struct *tty, struct file * filp) > > > { > > > - struct hvc_struct *hp = tty->driver_data; > > > + struct hvc_struct *hp; > > > unsigned long flags; > > > int rc = 0; > > > > > > + mutex_lock(&hvc_open_mutex); > > > + > > > + hp = tty->driver_data; > > > + if (!hp) { > > > + rc = -EIO; > > > + goto out; > > > + } > > > + > > > spin_lock_irqsave(&hp->port.lock, flags); > > > /* Check and then increment for fast path open. */ > > > if (hp->port.count++ > 0) { > > > spin_unlock_irqrestore(&hp->port.lock, flags); > > > hvc_kick(); > > > - return 0; > > > + goto out; > > > } /* else count == 0 */ > > > spin_unlock_irqrestore(&hp->port.lock, flags); > > > > Wait, why isn't this driver just calling tty_port_open() instead of > > trying to open-code all of this? > > > > Keeping a single mutext for open will not protect it from close, it will > > just slow things down a bit. There should already be a tty lock held by > > the tty core for open() to keep it from racing things, right? > The tty lock should have been held, but not likely across ->install() and > ->open() callbacks, thus resulting in a race between hvc_install() and > hvc_open(), How? The tty lock is held in install, and should not conflict with open(), otherwise we would be seeing this happen in all tty drivers, right? > where hvc_install() sets a data and the hvc_open() clears it. hvc_open() > doesn't > check if the data was set to NULL and proceeds. What data is being set that hvc_open is checking? And you are not grabbing a lock in your install callback, you are only serializing your open call here, I don't see how this is fixing anything other than perhaps slowing down your codepaths. As an arument why this isn't correct, can you answer why this same type of change wouldn't be required for all tty drivers in the tree? thanks, greg k-h