Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp774007ybk; Sat, 9 May 2020 18:33:31 -0700 (PDT) X-Google-Smtp-Source: APiQypL6vdInx/TzvY+PABY+YZeoaqlXwirw0V2Nt1BCKeYH48KNnoG/DzCni451AgUDExPa2MoP X-Received: by 2002:a17:906:c348:: with SMTP id ci8mr8180194ejb.73.1589074411457; Sat, 09 May 2020 18:33:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589074411; cv=none; d=google.com; s=arc-20160816; b=JGvIieBa+TdCaPcX0FH9Sr0yrUy8wEoqJVhIKD77OSjUA6vDvj6kjctmneN/HLP7zO lNSc0PfcjQWsl78p2C0tczySlpzvpc+iIzvyRHgSb6lyFQKFHjPqZUcI1/XFUiIDjJXd 8AGnsFDCA6SEnYngRhceSAL/bhoFSFEF8pfoaSYIjcn1Hv3HPjW76X4P1omkoxt7pnaI 8eGLIHNblxnrGi1nGoOAzjndeAKNVO+NG/7hk+fE3kAPg7UjCRXaMwSiJfcOSYR6CKlC iAkHew1TpZ3+4tgrPGOwPMkp2qUeCvN3PzcVNyMJNcH/qKt/QhOOKLnMrQOhekkJiFwF g+Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:message-id:references :in-reply-to:subject:cc:to:from:date:content-transfer-encoding :mime-version:dkim-signature; bh=SWmr21JzBeyX3B6jEnsPhNXgkMNcdb2+W8/646mE2VI=; b=cT8iW9S6Oxxicun/iUHBr+gVPuVYN4TIX9ceT2A54sQVWISQVZiAc0yRDnJxNgW5Dg IK9bwKaIAHjJ967sOfeQo54zZCZR6qydo3H5/hAZVamKteS/UNFc1ByN5Krwhfth4p67 v/Rigvh1v/i/qNlmQW9kzfGhFf3+JbIwewXPEg7t+H+qv5bcUxoaxnd60H8f6db1BlJ9 2v1lVkuQnQxf0wlxcWrHFkLTRVITrVq/8PWv6715veK43lL3gJdKfvCYrKDtdaGR5hS6 o0+660sUth5d7qVJawEZcX0eQLCvi/LwtNnRFxwrT71roBO5l0HdSslyyFdBuuZvwE0e HjAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mg.codeaurora.org header.s=smtp header.b=XNNddfZb; 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 gj19si3601476ejb.297.2020.05.09.18.33.09; Sat, 09 May 2020 18:33:31 -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=fail header.i=@mg.codeaurora.org header.s=smtp header.b=XNNddfZb; 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 S1728918AbgEJBbI (ORCPT + 99 others); Sat, 9 May 2020 21:31:08 -0400 Received: from mail26.static.mailgun.info ([104.130.122.26]:57023 "EHLO mail26.static.mailgun.info" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726630AbgEJBbI (ORCPT ); Sat, 9 May 2020 21:31:08 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1589074267; h=Message-ID: References: In-Reply-To: Subject: Cc: To: From: Date: Content-Transfer-Encoding: Content-Type: MIME-Version: Sender; bh=SWmr21JzBeyX3B6jEnsPhNXgkMNcdb2+W8/646mE2VI=; b=XNNddfZbNJHW/DazX3/UqCVsatTha52pRab6vdNeO9k2aj1vFf2GGphA2ySzdlpj4EBMEGr2 JJbNtUXN8G3XxiNw3YAT8qRH8976oIUC1vGUPYja5mOrv8tWD1Xp1/mxTTRxLD6muL0VS8uB Kr6UYaULija4U4MrtkAQgX9lfKk= X-Mailgun-Sending-Ip: 104.130.122.26 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by mxa.mailgun.org with ESMTP id 5eb75952.7f38d4bdb458-smtp-out-n01; Sun, 10 May 2020 01:30:58 -0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 2513EC43636; Sun, 10 May 2020 01:30:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=2.0 tests=ALL_TRUSTED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.codeaurora.org (localhost.localdomain [127.0.0.1]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: rananta) by smtp.codeaurora.org (Postfix) with ESMTPSA id 772E0C433F2; Sun, 10 May 2020 01:30:56 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Sat, 09 May 2020 18:30:56 -0700 From: rananta@codeaurora.org To: Greg KH 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 In-Reply-To: <20200506094851.GA2787548@kroah.com> References: <20200428032601.22127-1-rananta@codeaurora.org> <20200506094851.GA2787548@kroah.com> Message-ID: <98bbe7afabf48d8e8fe839fdc9e836a5@codeaurora.org> X-Sender: rananta@codeaurora.org User-Agent: Roundcube Webmail/1.3.9 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(), 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. > > 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 Thank you. Raghavendra