From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49902C3F68F for ; Tue, 14 Jan 2020 10:15:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 21D9820678 for ; Tue, 14 Jan 2020 10:15:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578996901; bh=PXb/Yq6gAYSBh3gsLt+Df61Nx9TZS2MPBBw3RVnLXpM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=JEwjRagzdDcv6KOqEOfwUf/73TRWQox3+LTYbO6/NBXSvSH/GTqXGYGSpTiZeSpVX EoLooNvH9dgcJpZ0jrvx6dOXt8yEPvRC9WTNA1AhRS9mGCc4SvtCgADQmpXZhm26qn iQLcHD5SSyslqTK6D1jo6lFA9p3gp99pwKAShIBo= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731651AbgANKLf (ORCPT ); Tue, 14 Jan 2020 05:11:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:46862 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732567AbgANKLe (ORCPT ); Tue, 14 Jan 2020 05:11:34 -0500 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 D27B524676; Tue, 14 Jan 2020 10:11:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578996694; bh=PXb/Yq6gAYSBh3gsLt+Df61Nx9TZS2MPBBw3RVnLXpM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xZqRFYQL5+Poyx4jwYmOd7MvbsvQcLzd/qsp96Ed9BW5hg3+PJnDXMgeBES5xcZbJ tkDN8KPWxpghwCcMQejxSMVVblxO/QouzoVOQYk9c906e73/yEDm0FGN4tSh/ipFP7 lOz8nIk6uErXla51t1pVKL3C9xzif6XEouMpVsdc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Slaby , Sudip Mukherjee Subject: [PATCH 4.9 23/31] tty: link tty and port before configuring it as console Date: Tue, 14 Jan 2020 11:02:15 +0100 Message-Id: <20200114094344.277311138@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200114094334.725604663@linuxfoundation.org> References: <20200114094334.725604663@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Sudip Mukherjee commit fb2b90014d782d80d7ebf663e50f96d8c507a73c upstream. There seems to be a race condition in tty drivers and I could see on many boot cycles a NULL pointer dereference as tty_init_dev() tries to do 'tty->port->itty = tty' even though tty->port is NULL. 'tty->port' will be set by the driver and if the driver has not yet done it before we open the tty device we can get to this situation. By adding some extra debug prints, I noticed that: 6.650130: uart_add_one_port 6.663849: register_console 6.664846: tty_open 6.674391: tty_init_dev 6.675456: tty_port_link_device uart_add_one_port() registers the console, as soon as it registers, the userspace tries to use it and that leads to tty_open() but uart_add_one_port() has not yet done tty_port_link_device() and so tty->port is not yet configured when control reaches tty_init_dev(). Further look into the code and tty_port_link_device() is done by uart_add_one_port(). After registering the console uart_add_one_port() will call tty_port_register_device_attr_serdev() and tty_port_link_device() is called from this. Call add tty_port_link_device() before uart_configure_port() is done and add a check in tty_port_link_device() so that it only links the port if it has not been done yet. Suggested-by: Jiri Slaby Signed-off-by: Sudip Mukherjee Cc: stable Link: https://lore.kernel.org/r/20191212131602.29504-1-sudipm.mukherjee@gmail.com Signed-off-by: Greg Kroah-Hartman --- drivers/tty/serial/serial_core.c | 1 + drivers/tty/tty_port.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -2795,6 +2795,7 @@ int uart_add_one_port(struct uart_driver if (uport->cons && uport->dev) of_console_check(uport->dev->of_node, uport->cons->name, uport->line); + tty_port_link_device(port, drv->tty_driver, uport->line); uart_configure_port(drv, state, uport); port->console = uart_console(uport); --- a/drivers/tty/tty_port.c +++ b/drivers/tty/tty_port.c @@ -48,7 +48,8 @@ void tty_port_link_device(struct tty_por { if (WARN_ON(index >= driver->num)) return; - driver->ports[index] = port; + if (!driver->ports[index]) + driver->ports[index] = port; } EXPORT_SYMBOL_GPL(tty_port_link_device);