Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp71594pxb; Mon, 13 Sep 2021 13:14:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYyLxzBgiEMUWVYPlEC2JQ+bxif6svN7DDpL3Xwr7m1M6NjNAEt28dUtR4DRhO+wB+guT1 X-Received: by 2002:a17:906:1557:: with SMTP id c23mr13258678ejd.371.1631564067766; Mon, 13 Sep 2021 13:14:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631564067; cv=none; d=google.com; s=arc-20160816; b=01zz2LP/Mh3e+3KawYbEVadnh5/4Zuk1iSdfuqX9pcSGNiaPIHezVhmSe9xowCvqdM QJTpw7G3PLrQx6u2bD3iCeEONdxqCs1gUlOzpztLSl4nDAoIN63zmnRhwr23MGHd2+qG 1y4Qr3rOIAs99v2ZOYUl2xgNY9pc0um8qYi4kZdmTOB9c/4MZdEN6ztVmFEo2y+2Swc8 9JnODtlPXFG0dBVSBuDHxEXMVMw2Dc9i5WZlq9/g0UyMBKBzNLBK3bk2tlDa2dce/iCJ WUlynAn/olnjRZ3E24nnWvXWLpReEMQ1htmAG/252XEy5fdbV3DdDOmcdXk6oRsmj33c mxIQ== 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=IJIZ6DNWTwQrfnpVj5SCpiYZGZYHEQKcyIp0bjUMcaU=; b=VFOnP8299MeYUj9fINOyhr5VWB2oXWgOUJuVFn+SejAEuNv2eFao/ixxkEtxBVi+qM AHkU/lmaV3qeu/wvtfvXsmAWEtJoyzhJ+y4iJLaVjpsu99L1fAAcPEZMKEavSEvGQodG 2PTBhMB6O5Q5TUe//D+gE0st47kjbBWV3QFNhJSQEM01QQ/wDsWURRheOlC0xMaXbsLz 8hwBr3w35mwMqukE3wbOpI+sVxkj/cpvYZLblCnPiVhoeGEql3/wXAgiUy6KcwIuJJT9 xzevezVpAJXgjb/tiD9IHFRkfl0/STSAskDgGk86Xd3tDcZ00YeXrUtcXPj/ZCwqn+UC wgig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="YZ/Bs3MD"; 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 r25si8722338ejc.100.2021.09.13.13.14.03; Mon, 13 Sep 2021 13:14:27 -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="YZ/Bs3MD"; 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 S244845AbhIMNut (ORCPT + 99 others); Mon, 13 Sep 2021 09:50:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:51886 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243856AbhIMNq4 (ORCPT ); Mon, 13 Sep 2021 09:46:56 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AD04861528; Mon, 13 Sep 2021 13:32:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539936; bh=PZFijUnzFm1xU7t6cRsvOEqatyXFFIlW64bKY8NGXoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YZ/Bs3MDH+pkS8gXMecO4aJ4SMtv6g50GgkM4gWnHTbEToeGxdVRDvMbU+NN23gjS yf+20LM9UT291/ZQ30grGqEXcsH+HZ6Gk8K9fZUy0UrhbMcquWwHC6RC9geRQXFadj X4b6Gzj8PqYx9jCQiZEeKHIxoyX9YRFMsoMnI+AY= 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 5.10 221/236] tty: Fix data race between tiocsti() and flush_to_ldisc() Date: Mon, 13 Sep 2021 15:15:26 +0200 Message-Id: <20210913131107.877083865@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131100.316353015@linuxfoundation.org> References: <20210913131100.316353015@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 @@ -2257,8 +2257,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) @@ -2274,8 +2272,10 @@ static int tiocsti(struct tty_struct *tt ld = tty_ldisc_ref_wait(tty); if (!ld) return -EIO; + 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; }