Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2565553lkm; Mon, 20 Sep 2021 18:51:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbJfFRAdi3qJ8Dz30l+uvOXMY8Qn7Y7fM6DNhgHrsgDHHtCsAA1n+LYA1es1ZajlCrNU0y X-Received: by 2002:a6b:ef0d:: with SMTP id k13mr17015261ioh.54.1632188991876; Mon, 20 Sep 2021 18:49:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188991; cv=none; d=google.com; s=arc-20160816; b=BR4tLGy9i6XjybUjrlWkn9ZET5avsPs+Tg33NhsYr1/btJcnuo97AGElzEqUcrAqB1 goMhl1uyV8i0sx98UMynQc+XlfKGyUA2lheJ+4u1yIA9Vqa5Inu/hO4Sh80fhATh+NiY EeDzUCGl2vkHQ8d61DVTg1JaI16QZnr9Xq9AYHETv3qp23K3wY4RjHaZ1vVvWvQR6psG bevPmpdYTeRzInR+ELm7qiXHBPzfeiuIrgFpL1XAvv3AMHakTHyiCuQ39iRzP+rob91j 0PRhJQY41WgM5mcJZIcHUvVXJG+zDmTvQwo2ibPWLbC1qLZit+e731XdqVPEKnm9HLSw hzcQ== 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=2l00MoKoRs99KQjJ2xOdZlWoUjHgvYqNSNl7ZRtKY6E=; b=qRGteZl53bo7xvs191vDUYctJzh7LNajdWNgGHRTBvokafoBsF6g/1h6ZQRwW9HWrW i9Eg90j/k3890CDrLnb2b3dy3gbvc/1+s0UJ+AEgEYlgGaQmmR8oeq9TPFpxbADozKly CR58jLKVOUsEWedVUwoLM/dE9FU/5KDwGaZsTk0s2vseCCBfosxp4UDz2IH7H5gy362q 2CU0S4mehgNkPygYkLl1uq5MZpVA1keKLvl0jSGH2PbpAEJlPVazPLl8XF1KYHlQPrgq 4wlArkSH+/8x9YjKua+hON8pLCqkG7B/tLWho+ch1jANcT5V43XyrrisUEL6aTkHbgZ4 QOKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="ebGMR/Ov"; 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 s26si15522836iov.21.2021.09.20.18.49.40; Mon, 20 Sep 2021 18:49:51 -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="ebGMR/Ov"; 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 S1349228AbhITRps (ORCPT + 99 others); Mon, 20 Sep 2021 13:45:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:46376 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349087AbhITRnm (ORCPT ); Mon, 20 Sep 2021 13:43:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8A24F61B93; Mon, 20 Sep 2021 17:09:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157768; bh=0EvU4tjBtZSWD6BkJEsyl0gwl32bFhZMxJSb1nmUdFc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ebGMR/Ov/po3O47qbCGxLYDohW2HIs/ATBjZc3fxGfn7FWS4xyoqyjp5aEE0xSFTU y3D9ELjjaR6u2IMPXvM0qIVuEnKEGu+xKpiLN3s9in/DlysYI1LvKJX0T5vZote3TT QSvC38JEKZDiGEV2E31sIBHBbVUU9iZENTQUK+Yk= 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.19 110/293] tty: Fix data race between tiocsti() and flush_to_ldisc() Date: Mon, 20 Sep 2021 18:41:12 +0200 Message-Id: <20210920163937.029188185@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163933.258815435@linuxfoundation.org> References: <20210920163933.258815435@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 @@ -2173,8 +2173,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) @@ -2190,8 +2188,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; }