Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2319422pxb; Mon, 20 Sep 2021 18:42:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoLef30Zwimm2vI7+0+W95HCSYMqCG8wdo99d/KOGFi+EbdIKRMvahue4WIYxW6oXnuuF7 X-Received: by 2002:a05:6602:22d7:: with SMTP id e23mr3419432ioe.63.1632188550819; Mon, 20 Sep 2021 18:42:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188550; cv=none; d=google.com; s=arc-20160816; b=EFJukTE1IXV7G/eaaHZiSTuO8akFidkqRV+DdRoQpX1YHutIJoEWiwzwZ3djHt6ITJ lvt6WSvShETUMxIH5BOnkj5Lyv33dAvaGD85PmDvycCPLQH3+52EoDUtKRXA1WdceX2m 9VqiQnIm4wC5TKcyn7iLGi4Azkb9eQNv60ZUA7fEWoBEvnXnBOVOKTVC3RSuAI6bLTmc 7tfhbO8e5f+WpS58y+GBE0VD9wX2Ij67Lwa+0/sX4LiwU4SE0LF1bVK1xrt+9lnXGNqf IHjrwtf2UWD9UkE9OLsWlKFRD/K4FGMm2wC+8nDAMnkworf4r1FPJeBO9XtNuqmkugcr xsWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=cmiW3UmEebf9lP6K5Fp64FtyUY15KYZU510Nca2lZUI=; b=1DtlORSwl/7EPOx5oOB1qSaY+03YrK3j6nLY6HG36DevkgnQ5VEvFvpDU/3BVwkT+h W1vFK1owcILJkiM/i5nZ6qGo7g2JS25HjjYEUy4Q5m7Isy1Egv6Fn3AQwM5Bgvbv+HXf T5bsOmP5sJ7EZ8ygE6kr5kNfZTehA7fI4WpNCBizKQHkvopNP+fdf+XDKpgcoZkjUq13 BtJe7G/8rN1o0uU39boqQ2liMqRic7nGX8cHg1ULOgv1KpIg3m3D4q+Yz+/h9lgXKhNR ty2CiDPH3ERhI2QxLlu4EAyJ8Ef4XcgeRfbHhz9lecOO9bTpEgBoG1fDRfaZO2WaFeDx SHEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qCRhGF3y; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s9si14186726jat.70.2021.09.20.18.42.19; Mon, 20 Sep 2021 18:42:30 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qCRhGF3y; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244540AbhITQub (ORCPT + 99 others); Mon, 20 Sep 2021 12:50:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:36970 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243737AbhITQtJ (ORCPT ); Mon, 20 Sep 2021 12:49:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2A1626126A; Mon, 20 Sep 2021 16:47:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632156456; bh=KznGqbpdKimRziCEd4TIKw06NWpwAcLxY32b4WXHJ68=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qCRhGF3yTcAgisqdgfZu3NoaF/YZLTlsdpEmaOpyqiYRul5OxKacwU3CWbFAcvq5k 0fMBAfRg5bVjO7g53Q2wVLyXXwwvt/NtW0NxtjZoFnjalVa4OtivhMBVqLVSsWMnQp 3AKJT98lvcPAevbWnXhxUisxQPkcHhw2SVX1sgWQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+97388eb9d31b997fe1d0@syzkaller.appspotmail.com, Jiri Slaby , Nguyen Dinh Phi Subject: [PATCH 4.4 068/133] tty: Fix data race between tiocsti() and flush_to_ldisc() Date: Mon, 20 Sep 2021 18:42:26 +0200 Message-Id: <20210920163914.871557859@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163912.603434365@linuxfoundation.org> References: <20210920163912.603434365@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nguyen Dinh Phi commit bb2853a6a421a052268eee00fd5d3f6b3504b2b1 upstream. The ops->receive_buf() may be accessed concurrently from these two functions. If the driver flushes data to the line discipline receive_buf() method while tiocsti() is waiting for the ops->receive_buf() to finish its work, the data race will happen. For example: tty_ioctl |tty_ldisc_receive_buf ->tioctsi | ->tty_port_default_receive_buf | ->tty_ldisc_receive_buf ->hci_uart_tty_receive | ->hci_uart_tty_receive ->h4_recv | ->h4_recv In this case, the h4 receive buffer will be overwritten by the latecomer, and we will lost the data. Hence, change tioctsi() function to use the exclusive lock interface from tty_buffer to avoid the data race. Reported-by: syzbot+97388eb9d31b997fe1d0@syzkaller.appspotmail.com Reviewed-by: Jiri Slaby Signed-off-by: Nguyen Dinh Phi Link: https://lore.kernel.org/r/20210823000641.2082292-1-phind.uet@gmail.com Cc: stable Signed-off-by: Greg Kroah-Hartman --- drivers/tty/tty_io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -2287,8 +2287,6 @@ static int tty_fasync(int fd, struct fil * Locking: * Called functions take tty_ldiscs_lock * current->signal->tty check is safe without locks - * - * FIXME: may race normal receive processing */ static int tiocsti(struct tty_struct *tty, char __user *p) @@ -2302,8 +2300,10 @@ static int tiocsti(struct tty_struct *tt return -EFAULT; tty_audit_tiocsti(tty, ch); ld = tty_ldisc_ref_wait(tty); + tty_buffer_lock_exclusive(tty->port); if (ld->ops->receive_buf) ld->ops->receive_buf(tty, &ch, &mbz, 1); + tty_buffer_unlock_exclusive(tty->port); tty_ldisc_deref(ld); return 0; }