Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2714455imj; Mon, 11 Feb 2019 07:23:48 -0800 (PST) X-Google-Smtp-Source: AHgI3IbcZnUXV7zFCLMBPLEoU0bzIbScwU6YPf3Pyi5xemo3KRndWMFFnDIz17KWoYyw5znj80qs X-Received: by 2002:a63:eb41:: with SMTP id b1mr33955061pgk.188.1549898628863; Mon, 11 Feb 2019 07:23:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549898628; cv=none; d=google.com; s=arc-20160816; b=UStT4LXqYXoJojJeTdYU9Iu7mHdFYAPeaYuH3W1ItsDODep55M8Bg5wL+x905SVFqD xXk2vE2GjH+ECTR5IPLV0KoaU7y9A2WQoMDCQm/VeM3EJFhUSwAv8IU4ZQaJSbS9sil3 FwhMyrDqi2xTM1lEJ9VCObobnJChoR84WVKn4A6Z8CZpmFWpq2f3ElQJ2dZBTPQ1y5KF dqWvjM5pzEIPi70IHmf+CU6XtABTZWK740wZTG3pMd9e2lw1f1PjLubYMweje/8HXiTz uBVAKqdrNUc+djNevCVx9LaJ5iCJMbWMjJF/B5gWGPDna6cl1qq0dPJ31z2HSrS0P8MH j78Q== 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=OBQ2dEuvUM8u+lwROadjbc0X56mNUUI314NK+griu7w=; b=i0BXo0Q9dG3uYvsiRcLM4GIC1N1DOoV/ksDAHKdLiK1t3+8dmFkWSQxDI94Cl9NKnt kd45Z54mTVyoD723gVbz4TyQz8AtcKv5pUCPPN2IuCa8wY5SOCtPohiOBP61+bg0ieh/ gid3BnkWiny6ptukLe0TL5CTO1IJZbZslrIi7pmuADh1o3FCHNchM4psRxZdX8a940PJ zaIcFgwQA9435maLuDTk+Y/dkCiYxqZlOSF5AlclVpVCwZfpeEZ31GDkUsL0qiFRrdVD vEtb1OmkdrUosqYV2APvI5feBPdwY14rHz3RpJMk+b50L/UMIPwbmuCy50BNXKI8MKC9 RDdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f6SdLwry; 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 g69si10503715pfg.225.2019.02.11.07.23.32; Mon, 11 Feb 2019 07:23:48 -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=f6SdLwry; 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 S2390086AbfBKPEt (ORCPT + 99 others); Mon, 11 Feb 2019 10:04:49 -0500 Received: from mail.kernel.org ([198.145.29.99]:52546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390150AbfBKPDq (ORCPT ); Mon, 11 Feb 2019 10:03:46 -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 966C9222B1; Mon, 11 Feb 2019 15:03:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549897426; bh=7DYFy6H9z+9OnSca/njosPuM7bp4PHkmHQu04MrtOV8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f6SdLwryeQbjRd7B3yna2uT8mFkP9cLMvXNlYCMJzpSbCiHPIzdttrSJSeZZio6bn JHwLUi5nuURRjjdCDL7INp06HYNCdbH/kh1dL2xL5c894oAIt+K2oDJqqsQeEF7nkQ v/+uy8NlmxgeBqGeNwACHrxApfFjOtVnB9tyvek0= 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.14 200/205] serial: fix race between flush_to_ldisc and tty_open Date: Mon, 11 Feb 2019 15:19:58 +0100 Message-Id: <20190211141841.503474574@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141827.214852402@linuxfoundation.org> References: <20190211141827.214852402@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.14-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 @@ -143,6 +143,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); @@ -2415,6 +2418,9 @@ static void uart_poll_put_char(struct tt struct uart_state *state = drv->state + line; struct uart_port *port; + if (!state) + return; + port = uart_port_ref(state); if (!port) return;