Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp10754ybt; Tue, 23 Jun 2020 13:57:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzc+TgEhxgo6zNBvTUFhBw8MsE1TLf21k4yPw8TwPEhQhvFQSc+0p0T0Jd8Xrl7uyfC5oF X-Received: by 2002:aa7:df08:: with SMTP id c8mr14050020edy.372.1592945824486; Tue, 23 Jun 2020 13:57:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592945824; cv=none; d=google.com; s=arc-20160816; b=jTviql9/OTpEvG0cCh8mCA7sICGzNJOAP728Il//phtd3O1TIpvMyAPeeqNC+Yy8YN AmBVG08l4AzorpxVqGaegraz5DluHj/lrdIIUdvnGiozujAJRdMNQKefJ1C09EoLolNf BGCpnbtRXoeLVm2U49mYkeRfE3Butgb8fXoxrqxsYARhV5lEDczK35l7M420FQISP32F t43nb+jnojoRIx5D3lhl4srm3tNYim+MzZP2Y0JYWibf2bVoKjHABXWZ5KDufrLMd7wT oIyEZgvbayUri0qnOXZenSJdjpeN50UeDNjihmU/glMJK5EV/dgeKp6WAeSXCtp289em 7cfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uT0CxJzu1umJyl24pwVcUCZvPiJghH0iGx2O6NY9uOA=; b=ZBccPAr7fRVCpHe4mHq/O+X0EbLrsvieYGTrNOfx1Hav2tmqmsNKmKd8GmFwWrdG+Q z69S0cL7PTjeIAAYS8XMJvOppe4YOH6iMmsTOycgvtFQwLXNJgC1SnF/tdx1Ob8azBat b5L12ZBSmq9+RH7Q4D76S4ErekVWwa3DGru9kGMwVGalbU8mqKpZRaZ4lvNKm/LVUapE r3l8wx+d+GwqfcVf279bMifn1Q3ShW70lgkQ4mGjVc9VAHzO16yj59AMnVshFhih4CSh ET4nCzs31r7R3oLgfCov34H1Omy6kgVRsgYv2edH6mUVLDpE/9EeDpMNNeOikbSn9R8/ MQNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hN1Loo52; 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 v5si1519460ejf.336.2020.06.23.13.56.40; Tue, 23 Jun 2020 13:57:04 -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=hN1Loo52; 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 S2392951AbgFWUz3 (ORCPT + 99 others); Tue, 23 Jun 2020 16:55:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:42376 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392714AbgFWUpC (ORCPT ); Tue, 23 Jun 2020 16:45:02 -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 5CD8C21BE5; Tue, 23 Jun 2020 20:45:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592945102; bh=uAKSTt9G16LBVtwwzGyB3Dma+X5Cueok5yUx4LnhSw0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hN1Loo52O6d1+GuDBq6PCYBUuj7mSSVlJkuGIYGg5/9H/21iK1F2E+FhkIihQbYam yP3qgn+tjRWHBMxjLSKrMjSAfjVsMuJnfRruLUqcDl3wpWu7QGy0I9HKWutfnMEaqb z46lJj3XtOCEv7Y71ZynCr6n6ZYxZ/MMTGtFB9OM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Raghavendra Rao Ananta , Sasha Levin Subject: [PATCH 4.14 041/136] tty: hvc: Fix data abort due to race in hvc_open Date: Tue, 23 Jun 2020 21:58:17 +0200 Message-Id: <20200623195305.714413417@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200623195303.601828702@linuxfoundation.org> References: <20200623195303.601828702@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Raghavendra Rao Ananta [ Upstream commit e2bd1dcbe1aa34ff5570b3427c530e4332ecf0fe ] 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 Link: https://lore.kernel.org/r/20200428032601.22127-1-rananta@codeaurora.org Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- 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 d52221ae1b85a..663cbe3669e11 100644 --- a/drivers/tty/hvc/hvc_console.c +++ b/drivers/tty/hvc/hvc_console.c @@ -88,6 +88,8 @@ static LIST_HEAD(hvc_structs); */ static DEFINE_SPINLOCK(hvc_structs_lock); +/* 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 @@ -332,16 +334,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); @@ -369,6 +379,8 @@ static int hvc_open(struct tty_struct *tty, struct file * filp) /* Force wakeup of the polling thread */ hvc_kick(); +out: + mutex_unlock(&hvc_open_mutex); return rc; } -- 2.25.1