Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2331502ybz; Thu, 23 Apr 2020 16:10:36 -0700 (PDT) X-Google-Smtp-Source: APiQypJrTpS9+JUKslqyXJKmK+/ae5SfFjT7Wc4ForFQjlL30mX6ftRnH6xqwojD1lxEiAveYgT+ X-Received: by 2002:aa7:c714:: with SMTP id i20mr4833529edq.230.1587683436137; Thu, 23 Apr 2020 16:10:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587683436; cv=none; d=google.com; s=arc-20160816; b=lXTki3QKv2/kBESnF1+9O6/9svl0iIBJqb7xyxYEGsxVHPf94ARYa4UOki8bWJZVyP a+0XYOQ4IBsSS3W96/1QcDSBpHkbmRGhPXl6exgcShb8caotxLAG0ctCd2Xmb8GEoiqg a2AZ9u1e+sfsrhmQQ3wmnP7JNkuXM7NJFeiWJgxdGi+7gK0/Q8quL2e5+FloYhMOiK+f kvX6mb9nOgqWXsTLVELM8Gt8xeLj6z0OTCAcmqDuGXnaKoTXJFzHi1Z1DSHWcAWxZc8j Zp3xChtcUhx/TeE5DwqraVGQ4ueTNvV/eRfVofHpUZIPIiDmbAFoJNtjPcCI3rIX8RCF +BZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=4wThqmEZ2KHW8Yw8NgTS5lsF+IGBUC49JOUsqas8p28=; b=K1v7k0vetRNVzFHefMtxtwCT1VkcwzdbgFncog0HGITMjCVHhYRmpee8fY4FGG8kdW MTJGVWFYldQZT0VAXINes3nPEo/CBx6x8yvunUz83FfioiNzQGQtoqQC9L87MbRxDqnT QxGobj4oyHWbACzfSbRw+1dvWEd9E/Q9renHP25SOMS0QUaaLt+da9iKbLC6W+k2Izq3 R67lirwICjlEkzGG87KZ2yzFB3b1hP1PQHPk2O9zqs2N8n67c6X7K2l0gQFULOJKW9Le PdKe8BhaBte/6I8nY4dFIYyOjYMcBO3nzXUWMCWSWxOt9cEehhsl/Z0yS9ocmLPQDnHT rR9A== ARC-Authentication-Results: i=1; mx.google.com; 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 i18si2167325ejz.35.2020.04.23.16.10.13; Thu, 23 Apr 2020 16:10:36 -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; 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 S1728763AbgDWXHI (ORCPT + 99 others); Thu, 23 Apr 2020 19:07:08 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:49674 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728407AbgDWXGr (ORCPT ); Thu, 23 Apr 2020 19:06:47 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1jRkvU-0004hX-E1; Fri, 24 Apr 2020 00:06:36 +0100 Received: from ben by deadeye with local (Exim 4.93) (envelope-from ) id 1jRkvR-00E6pT-Fe; Fri, 24 Apr 2020 00:06:33 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Sudip Mukherjee" , "Greg Kroah-Hartman" , "Jiri Slaby" Date: Fri, 24 Apr 2020 00:05:57 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 130/245] tty: link tty and port before configuring it as console In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.83-rc1 review patch. If anyone has any objections, please let me know. ------------------ 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 Link: https://lore.kernel.org/r/20191212131602.29504-1-sudipm.mukherjee@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Ben Hutchings --- 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 @@ -2639,6 +2639,7 @@ int uart_add_one_port(struct uart_driver lockdep_set_class(&uport->lock, &port_lock_key); } + tty_port_link_device(port, drv->tty_driver, uport->line); uart_configure_port(drv, state, uport); /* --- a/drivers/tty/tty_port.c +++ b/drivers/tty/tty_port.c @@ -49,7 +49,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);