Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1659716ybj; Wed, 6 May 2020 02:53:00 -0700 (PDT) X-Google-Smtp-Source: APiQypKZVQWW0kGThyYOqwJQjgWbg0BMYISqoZqqPcueTDRr/deBlVY18/05UK5bYt9x8+7yn7Jj X-Received: by 2002:a05:6402:3076:: with SMTP id bs22mr6349297edb.112.1588758780557; Wed, 06 May 2020 02:53:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588758780; cv=none; d=google.com; s=arc-20160816; b=HR1QbvEgenGRwknWAO0fvV7IgwOik3JY0jT+ZH6aaQZ2NnK1rAFLKpt4k2Z0Y3nAfO 3NRnKFYr+vi/dAJNQMoqg17O8/PZYBXg289vMH+l7guoellPV5jCqQZ1YgVubElvFQzo m15h3hj6uuju4E5Er23AAQkq1LgTSTdQXnQoY4pYl+iPsoh307fjtnsY4oQfOawNX30T wNXlzdgA8Xz6ZVmzN4rpnP7AZQVdApzG4Qq8d9hxN9nmxsxPYaveMV6HHGNVBdw21byG /SM8ZCiph5YKvdGKDUlHbIrnHKsGlQ2eXU5/XoBWPOR7LugncZ2/qhrE8rNZ4Ty4SMkb M+Yg== 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=Bw9Jft9UaJSzIh1VgP25ZGG/0g8zxgNwtFep7+L+Jcs=; b=nz2+g9EpEYGw1l1nB1HLW8GbYmcgwofmtSW3MMabnfudSZveGlZTsOYZsxxJzdfnvf qxTC7Oj2FudU3zCdW9PT5fBR0X4JdLc3R490jil0zw4bjHR73YBFpWXJO85PobF1qCuJ K+SIYxrC4qXmiKI4RbawJJknflsp28SeQ7O7C0C8pnP9gUfb220BcRGar19LtZFiK466 unAO8A3BhDZliOJ5rEUUzTB5GieDMRf/L7pp7upC/rip2FhRoWIqkmtXPJsoL8KdfNma 2J5eCHjk5cE0wtzG1xhyYtnf4cFxiLdlAlR0XasoFjJ5CR8jqepjuO0YNHzPZVAm8YWh qPuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pwtFY++f; 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 r12si673735edw.278.2020.05.06.02.52.37; Wed, 06 May 2020 02:53:00 -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=pwtFY++f; 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 S1729060AbgEFJsy (ORCPT + 99 others); Wed, 6 May 2020 05:48:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:51950 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728640AbgEFJsy (ORCPT ); Wed, 6 May 2020 05:48:54 -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 7934F206DD; Wed, 6 May 2020 09:48:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588758533; bh=ZW1E79xPaZKAydY429DVMLY0e+c/wC5VYXN8YIirzjM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pwtFY++fxs2YNcNbbN+ZfgD+/9YnoIpehM4RYf+bS6ZuKVQf6QPag8gWpA0OrecEp G2ZCTCXXR3/YDgChIyUNUbIvPOKmS0Lf6irGRLNNWe1EfCOEz7QJ2p038mRUue0uSK cjs/cy+YpVrIdfXvi40mz6x60zA0GR0vRbom8Bb4= Date: Wed, 6 May 2020 11:48:51 +0200 From: Greg KH To: Raghavendra Rao Ananta 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: <20200506094851.GA2787548@kroah.com> References: <20200428032601.22127-1-rananta@codeaurora.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200428032601.22127-1-rananta@codeaurora.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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? Try just removing all of this logic and replacing it with a call to tty_port_open() and see if that fixes this issue. As "proof" of this, I don't see other serial drivers needing a single mutex for their open calls, do you? thanks, greg k-h