Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2678469imj; Mon, 11 Feb 2019 06:55:21 -0800 (PST) X-Google-Smtp-Source: AHgI3Iar6KQFun7nQE9jVQOXe/aGHcYyUYV8sULI77saXZCPIqnhxdT2fKAK1j4Iz9ELRkQJ7cm6 X-Received: by 2002:a17:902:8a91:: with SMTP id p17mr38110239plo.316.1549896921880; Mon, 11 Feb 2019 06:55:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549896921; cv=none; d=google.com; s=arc-20160816; b=SAM6toP+2I4xdo+O/Z++Luy7a1Ts54upmuXiZ0+hjlAkHOeX83PKHt96Prmyg5sW9P dWEX4ZbIsB7CUclBdl/VU8lodVCuOFf+qFcHrayybxpVvZsKUg2oGPztErFwDETODNwD aKAE9ZCcvN7ZDwDOyuxti0DvWaQ0AyqfpsrQxb7Ac1Z5MRQMAQgoFjQpgXSatjNyoMRa Wd9T8WRCbBOIw7BKNI/hkNyNhNaqR0xBYbRTkZWlHeeQ5QK+oloTxlQbXJBT9JHHhl2+ PnPCUJB4ztt1N5B3zkbGNN6VTP/wYAZ88HZ076dMS7PMX84zccVpU1IWMpNVWY0AehZ3 7lUQ== 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=Tg9q43sINOzSDKtAlYJDQBOipBUeLFZQWxHdUDLRIIc=; b=CxmBT0JLNyNwXuPWgu9HGrIDJaMEQK7l6gdN3m5KNSZZzJfEJhUOKzPIUCD6n+A1y7 8w5Ih6nE+KykKRXZVM/vXEvkIbhHXm/I9v9CgJB8OFBPMKgL83G704iItiJUSLlnjpQY 05MahvvonjbEy7uLlnMPeRAhMeK5erbqOT3vIhVu4Q73Y5qnVbrsscfdX6ofBwZTchWM ns85lWX7sS0fuvPdZcqofvZcnjcN5dt5NNglGGhU6Y5udet9aUB3bY4rw+Oia41y5btF GIXWLchgJBaZmorzXm846iWjs/Q9GribRRHPHbEwgWYRlHSIDapb5SG/V2Knsix+PDO7 rcAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yMDDEqVh; 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 o8si10089485plk.28.2019.02.11.06.55.05; Mon, 11 Feb 2019 06:55:21 -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=yMDDEqVh; 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 S2388638AbfBKOx3 (ORCPT + 99 others); Mon, 11 Feb 2019 09:53:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:39908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388017AbfBKOx2 (ORCPT ); Mon, 11 Feb 2019 09:53:28 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 DD0402081B; Mon, 11 Feb 2019 14:53:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549896807; bh=38PI0D/cGYK+R0LDQ/bUraObbQn1vDlqIOUHXnP7BJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yMDDEqVhxZrDE11wMaEkfjyPes334XXARM17oybXA6AWem8Ddrnb0h2grLqloWBxm V5ZEXC2+a3VWEQoZuVTV7R4qOVTp0TPKG2YzKlR01a80ZkkMT01dfeSR97SufniNfL 6+IDn9UQBezVou4aRSn1npgu7cU2yi5cTBO97gq8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Li RongQing , Wang Li , Zhang Yu Subject: [PATCH 4.19 308/313] serial: fix race between flush_to_ldisc and tty_open Date: Mon, 11 Feb 2019 15:19:48 +0100 Message-Id: <20190211141913.385429859@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141852.749630980@linuxfoundation.org> References: <20190211141852.749630980@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Greg Kroah-Hartman commit fedb5760648a291e949f2380d383b5b2d2749b5e upstream. There still is a race window after the commit b027e2298bd588 ("tty: fix data race between tty_init_dev and flush of buf"), and we encountered this crash issue if receive_buf call comes before tty initialization completes in tty_open and tty->driver_data may be NULL. CPU0 CPU1 ---- ---- tty_open tty_init_dev tty_ldisc_unlock schedule flush_to_ldisc receive_buf tty_port_default_receive_buf tty_ldisc_receive_buf n_tty_receive_buf_common __receive_buf uart_flush_chars uart_start /*tty->driver_data is NULL*/ tty->ops->open /*init tty->driver_data*/ it can be fixed by extending ldisc semaphore lock in tty_init_dev to driver_data initialized completely after tty->ops->open(), but this will lead to get lock on one function and unlock in some other function, and hard to maintain, so fix this race only by checking tty->driver_data when receiving, and return if tty->driver_data is NULL, and n_tty_receive_buf_common maybe calls uart_unthrottle, so add the same check. Because the tty layer knows nothing about the driver associated with the device, the tty layer can not do anything here, it is up to the tty driver itself to check for this type of race. Fix up the serial driver to correctly check to see if it is finished binding with the device when being called, and if not, abort the tty calls. [Description and problem report and testing from Li RongQing, I rewrote the patch to be in the serial layer, not in the tty core - gregkh] Reported-by: Li RongQing Tested-by: Li RongQing Signed-off-by: Wang Li Signed-off-by: Zhang Yu Signed-off-by: Li RongQing Cc: stable Signed-off-by: Greg Kroah-Hartman --- drivers/tty/serial/serial_core.c | 6 ++++++ 1 file changed, 6 insertions(+) --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -130,6 +130,9 @@ static void uart_start(struct tty_struct struct uart_port *port; unsigned long flags; + if (!state) + return; + port = uart_port_lock(state, flags); __uart_start(tty); uart_port_unlock(port, flags); @@ -727,6 +730,9 @@ static void uart_unthrottle(struct tty_s upstat_t mask = UPSTAT_SYNC_FIFO; struct uart_port *port; + if (!state) + return; + port = uart_port_ref(state); if (!port) return;