Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp979161ybt; Wed, 17 Jun 2020 19:46:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEYrGXuTcMao6TCqj0iNlx/THiOWb2W2/P2GAZrvE/b4g3m+1tb51UMXqpmVdFIB50c+AH X-Received: by 2002:a17:906:ce47:: with SMTP id se7mr1944501ejb.149.1592448370604; Wed, 17 Jun 2020 19:46:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592448370; cv=none; d=google.com; s=arc-20160816; b=ghFWjeTfNRB+lfMMrmvBY/OyUTOvRcRu7kzWPsWzm2v9DFmfeOasoa3LIhWCYSLxQf 2qkyEtvQSDKOnW7sJ818cK+P8nmmYY+zkQSCiwnjCFLsANWGKkgtCP3STE8xBiXgvGa8 Kjn1U3hR8zYW7jyf0miWMLey2ZRvMvwicK7tqS9mn4wCUxQgVj1jNkALy6d3OAmPb31r ty9MwLMqQK0O/SQ4FFPkBRf3tyOCjT4cjhJHj9gt2bVQ+yyqWT2Wyep3jwpnad8KioY5 Cs+Fg0L3CP08xe+fK6VFq6n7fZuwa61SF5FT8HDs3wKr5KEm8aHimQBx80VmFP0xiWxc 0kWw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BvI2icn4+gvtbL63GgUVt6ggnRB4PB1mXdkcPJsD8uA=; b=d1YXGOqlbVDG0A/WKxXBHBa0JFZjVcKcdTwrCyG/nHNNXc0gAZjcjKNAJkOYrwRKoj 0zzW7Tz7K4Gs95Xg/c3n8JWrQWicSDlLvoTe3/kjM65WI4JbDhOrbqd9LKk96CM3Q501 4ynPDtfY9OMFijhzpLe4rvz559p4o0x24vMu+wh9tFfNIP9zhMvfhYq6A1W38FQWL3z1 rCH1mztTloImBHIFilbMCVwAp0XslBgFKfV4fh633JJTSZdWu3DmGazTO1/0jLFE9mTw R3xVdjPSYAqk8+N1mDWdfry+BevPToguns8cTz6gxO4WGCxLJaGWTUvTQ0ok4pBe9Ib7 7Ouw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eUAPoeeq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y2si1110157eds.47.2020.06.17.19.45.48; Wed, 17 Jun 2020 19:46:10 -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=eUAPoeeq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731120AbgFRCoM (ORCPT + 99 others); Wed, 17 Jun 2020 22:44:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:39104 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727045AbgFRBLT (ORCPT ); Wed, 17 Jun 2020 21:11:19 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AD4A3221E7; Thu, 18 Jun 2020 01:11:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592442678; bh=0wMSeq300+HLTPZeJnOyyYrhuUw76Ec0Zg0wYdFtlMg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eUAPoeeq1Gadk7NfuJZhG7ZT2MhW4UKzp/0bdOfZ/mAG2cJ5Swx+GLZ7jAvtv2TKM +/IDd+50rPdZGesWdjoPn1F0Ep+Cr3HZlQpGi5qY+P0cWA/bXk7TVI1fWdS3GZXayB NfnfMsgG5NvF2EYGJpKqedmPza3rac1G1+rqsdq4= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Raghavendra Rao Ananta , Greg Kroah-Hartman , Sasha Levin , linuxppc-dev@lists.ozlabs.org Subject: [PATCH AUTOSEL 5.7 146/388] tty: hvc: Fix data abort due to race in hvc_open Date: Wed, 17 Jun 2020 21:04:03 -0400 Message-Id: <20200618010805.600873-146-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200618010805.600873-1-sashal@kernel.org> References: <20200618010805.600873-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 cdcc64ea2554..f8e43a6faea9 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); @@ -383,6 +393,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