Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5023848ybl; Tue, 14 Jan 2020 02:09:38 -0800 (PST) X-Google-Smtp-Source: APXvYqyz3p4XEpENehsjm8T+KvBmyxZBB2hjG3QDHJK0ZyfQPu7cWym8d5ACv4q7VlkVs5FcC94W X-Received: by 2002:a05:6830:1d5b:: with SMTP id p27mr15546058oth.263.1578996578169; Tue, 14 Jan 2020 02:09:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578996578; cv=none; d=google.com; s=arc-20160816; b=nPKJYpBp/xDgu5YJVvfi6TS+kJ1i5vvK++JNGIdPyONLEzdKgVOVv83K63s5jTneTM YREi9T/ezKzMFtS8O0jzG6SUQG/GhfiZmWvikkAY4LWCic5yvuJlLk7kDEvjf1v7YugR 9WTvn9/nl4+BaLqKZ5tNxVPX82zrtZwN9G0k/QPV7lmHEc4IbjJym+xSHDgXZ5AdKyou sgPa7nFr0EQnL6RfYcwBxPkKRnEYpD8k8k1JevtAy8HTuE4FmVzNN78UedH8wkeGPNyS 0cbpE2rsDCL2hluNUwO6O/BU08ySAaH4NIrfKC6syB9gwii1/HB7WcF+Kb6MQQqqPetb RIVg== 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=nEceshvwK0AM6Mrcq/w0v/OmfvD7zxYEYimw1mhP+UU=; b=0gCtKJHyhGJ1Jvn/4ptp+u99FRxq7hPtzl3HPvPbZHYwbZqm+4dFaP14MA/22ukc8Q d+S3H/rivjxjvxDP8HhfKeA/Z+c03wnZlxivNKd4Qi1CO7+rh5644/tUASW5bYRQvtI9 Rf5FUYSPJ+ua3VuC79Qts6d2EpIuMjH4ID06pYrWU8DGxZNXSRBamxA4d62Uw9m0PcuN QeMwYHatYCaJWT8pwKGX85mBj92uxLHockfnor4GQc+Z+tY9Cf0hdAX0F0NVg6nAZ5eh iQybjKATIPq1j6KiKv74aAWSwJnUvAhsFUkYv74i1+HZgsyz/rMpZiDhFQqJFt6oK8TJ 2GVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HHFbVyUc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v23si8098766otk.321.2020.01.14.02.09.27; Tue, 14 Jan 2020 02:09:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HHFbVyUc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731002AbgANKIa (ORCPT + 99 others); Tue, 14 Jan 2020 05:08:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:39946 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729094AbgANKI3 (ORCPT ); Tue, 14 Jan 2020 05:08:29 -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 00E9F207FF; Tue, 14 Jan 2020 10:08:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578996508; bh=cJ6wEibUShMousPc+02xkZVy1kRYitQYZDUUI9Aah7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HHFbVyUcpAy/QCVZG9n6y6FjiVqJ3MKEtWH8RlGvgYpOy1Ht0iv2AuuHZPytWtoqa CYcy7pGX4WKvC/7LzmKiNzG8lkt223JzrGuGwCEQb47OR1/A20TevU3nBbAavWZMwM 18+nmPvjJT6EPjm5JygSRpObpxkTcH32J1GO1ShI= 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.19 33/46] tty: link tty and port before configuring it as console Date: Tue, 14 Jan 2020 11:01:50 +0100 Message-Id: <20200114094346.995735914@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200114094339.608068818@linuxfoundation.org> References: <20200114094339.608068818@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: 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 @@ -2786,6 +2786,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 @@ -89,7 +89,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);