From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.8 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796234947; b=qLd041O/cudDLDE/PgnPABjjPDPu+1MBxSpqwrgCTsRy70dJg8s2cjvCmBbPrXAjUmmX w3F4LgyzmyR5/ZK8Mo9au9LztrMmyutobeRrFWwYWfPl2tLCG0QjqUck5GU22F+YLKJF4 EY4tCxSsW2tb0mak8M4JU4Ty8vs83j7iE/LZfV5PsSHRcFdUj+qwngYewI6jph+XCUhGc q+gqJi90ZLr1LeSuwVdd7kEHGO3Se7G+yNDM78acXZu8RQSuK1T8clswvHORI6DoXDEK9 7w5XToTJ2Fmbxwsp2J4wfU2nAtrm7/+EfcAJh32uW5jrw9QR5zSemR3qWYhBmxrUzuA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796234947; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=8JEbNjWk/mZq++fnVT7X6mdJBxI+cVQ3QCkVkpLq3aY=; b=I5V8ODQHn1G/0XFophovs62TFzJAHUUwHtoR0U3Ci+RYS1xAfwQYm+VMldU3+bR3c5I0 OQ07UoeHIQ33qPDKZmrlRewZKnkGaiBca3i9tQVmCfeIpeWbUikAhlef3DPvxUVgZskw7 6FveO1hV7veuXbWXLC/WqEs9wU2KA0jJxUU/TDnifkow7q2RXeWPEzDA+KL3pB6NyeXaD fDr4r08et7kG8Cy4ZD6zjPWtUwszXnKALpZstZ0+J3SH3/0bPz528NpT/bU/W3rb6AZ6z VKOjSRl4HlZYVvnOr0LCI8p7jbHrux9L1aYBVI6JyLwwnM/aiq/Asq3Ybw0rAhQcEiA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=G6N++gl6; arc=none smtp.remote-ip=74.125.229.8 Received: from mail-dy2-f8.google.com (mail-dy2-f8.google.com [74.125.229.8]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E0FB606A333 for ; Wed, 2 Dec 2026 18:09:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1E0FB606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=G6N++gl6 Received: by mail-dy2-f8.google.com with SMTP id 5a478bee46e88-36a6ce64b95so4551827eec.1 for ; Wed, 02 Dec 2026 10:09:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796234943; x=1796839743; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=8JEbNjWk/mZq++fnVT7X6mdJBxI+cVQ3QCkVkpLq3aY=; b=G6N++gl6pxhtNg5ZDsud9B51aUNDFuPUP52FkbHIcUxwmH1dkbSnFDjW+TtFaZB0W4 /WwyCKNaGPgtNIp2XWZb6VQnMzOt0h/0/UxBaFuXwdVX7WFTM8MjbFV9PWYP22sahXms 1bJR6vCB/aDco26RfIgJhLesl3IniWSONYaN34/vcUC/ij1CD6Ymm4aQwP2QwElbIZsf Zq9BvhIrE/89mqBhmxltPZOJWa/NkAWYK1Snhgj7moEYIHQoBXoAOI7H8i1Cb+ksWiPb czW0BldvjbMdBBP9rkm84ff4QaVnQggtdc1Iv5lnsEkIg4te5XPceBptKEgHqg+LUKiI lReg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796234943; x=1796839743; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=8JEbNjWk/mZq++fnVT7X6mdJBxI+cVQ3QCkVkpLq3aY=; b=OBJN7a1e4E26XX7UHGn7Oo6IaM/W5MkoFXBV4gBy8b7rRDBLqaQe4aiyLbqXQqWShD 7BQ5DXekKc7GXmq3zEgDP1VTvUVTt7bG6QliDMUe6K4Yx727/vmk5jliAVDZkC61Lt3S pKRmS14MdHFLfELnTNgsBURnvoOfWzyEm9KHXsqwKrYIixpU0FhRXf/+pGKc37WXAmY6 dPhyuwa+U4dCH4UfUC082QQeWpALmUmqGDaxZ3M3oWEIWa/JsgkqcF1YQNaaQqvNfiVn yKslYC0RwBpH/lj1CLVHiqIjnh5He67uoOhZRlQ6HKHwu4Qff/4TV6ID7BwD8aP+d4Kv XGng== X-Forwarded-Encrypted: i=1; AL+hErEICHEMdICXTIEHrygx6IXOoxwsieAU0qb1tmBGj4YGjDm48lMaJ87+O3HGdRPQw03MMYbvy8K8yPSeokY=@vger.kernel.org X-Gm-Message-State: APuU4Z8F2KEdz71Df3DMM8MhYeMT87ckTpJesp+kVK2/cvubGZNXzRR9 zV8siQTXuacYEadQg5NGNPmx6j3GF0P3sQfvg+t+gblx0d4VQwrdWKf1 X-Gm-Gg: AZFdTZeTel+kV2bxEDhadkV8VFNUz5fp4+/TxLjJ+ScFXlSX/916j2ICWURlISnPJZ4 tRDB81ssoHfBRtPb4RRnna8zl9P20B2KCQLc8bvYyNOcuEkVSe0+S1dv1zglxA4Gb5tTKMUjVjc 125oNdR1EIUBEZJJBnSvo5q+pF3JidZ0dloRK5PKcbRlqGn9wCKWW3uunqi98Gq4NC1/nVCmU4z bhgiYHhahk8G9bChcCS6yL+73h9zl5pXVH7Fm9F5CM7dVoHXrQiL7BLQr1q1Xxv68D4vJxtcwqP 8fajaeah1QoAYOGaIhIw1+gRYoVX0XWysvktw/2rQSmUWY8uBy2/kxd2akVOFBVSiIsNR+PHVh3 54w6oTSZtY07LNefKnn1pKOCKn06ei3gTVNld/tEfDnOQV1cDCYlM9QBX2e0PDFQQ3v3J+/4x21 doBo6gdQvHYtJ/7sOrmYxN1TC8pTaSrRC1yEC/mAATlvB5/qAUt8gUDHWoJES8QMLg+wdpnfP3V myXt9uLbhXUdIziFnlHhxvVjqptOx2l9qjNzMqHxEo3bSu6Lg9RxBbrpFSwp7AqcBya2jdPAopN mA3oyZamRBTSPe3A8U/aiMbTG/Yt2ALV6mq6EJhn7w1Y/EfG9d8+YipY X-Received: by 2002:a05:7300:a506:b0:386:c960:278 with SMTP id 5a478bee46e88-386c9600557mr33809103eec.17.1796234943080; Wed, 02 Dec 2026 10:09:03 -0800 (PST) Received: from jiayi-vm-sprl-server17.local ([155.69.195.24]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38730405b61sm7972804eec.24.2026.12.02.10.09.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 10:09:02 -0800 (PST) From: Jiayi Zhang To: Greg Kroah-Hartman , Jiri Slaby Cc: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Andy Shevchenko , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+0479f67e520e4910d370@syzkaller.appspotmail.com, syzbot+2430ce6b487fe0119d03@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com Subject: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl() Date: Wed, 2 Dec 2026 18:08:54 +0000 Message-ID: <20261202180854.4154340-1-zhangjiayi1014@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit syzbot reports a data race between the XON/XOFF lookahead and TCSETS: BUG: KCSAN: data-race in n_tty_lookahead_flow_ctrl / tty_set_termios write to 0xffff88811a4798e0 of 44 bytes by task 9666 on cpu 1: tty_set_termios+0xc0/0x900 drivers/tty/tty_ioctl.c:335 set_termios+0x36e/0x4b0 drivers/tty/tty_ioctl.c:516 ... read to 0xffff88811a4798fa of 1 bytes by task 9026 on cpu 0: n_tty_is_char_flow_ctrl drivers/tty/n_tty.c:1196 [inline] n_tty_receive_char_flow_ctrl drivers/tty/n_tty.c:1217 [inline] n_tty_lookahead_flow_ctrl+0x1b0/0x2d0 drivers/tty/n_tty.c:1492 tty_port_default_lookahead_buf+0x91/0xc0 drivers/tty/tty_port.c:59 lookahead_bufs drivers/tty/tty_buffer.c:435 [inline] flush_to_ldisc+0x28b/0x350 drivers/tty/tty_buffer.c:505 and a second one between the lookahead and a hangup: BUG: KCSAN: data-race in n_tty_flush_buffer / n_tty_lookahead_flow_ctrl read-write to 0xffffc90002eed270 of 8 bytes by task 368 on cpu 1: n_tty_lookahead_flow_ctrl+0x52/0x2d0 drivers/tty/n_tty.c:1483 tty_port_default_lookahead_buf+0x91/0xc0 drivers/tty/tty_port.c:59 ... write to 0xffffc90002eed270 of 8 bytes by task 19255 on cpu 0: reset_buffer_flags drivers/tty/n_tty.c:322 [inline] n_tty_flush_buffer+0xa8/0x180 drivers/tty/n_tty.c:350 tty_ldisc_hangup+0x77/0x480 drivers/tty/tty_ldisc.c:699 ... n_tty protects the termios and its buffer state with tty->termios_rwsem. tty_set_termios() replaces tty->termios and n_tty_flush_buffer() resets ldata->lookahead_count with the semaphore held for writing, and n_tty_receive_buf_common() holds it for reading while it processes received characters. n_tty_lookahead_flow_ctrl() reads the termios (I_IXON(), START_CHAR(), STOP_CHAR()) and updates lookahead_count without the semaphore. So the lookahead can see a TCSETS half done and start or stop the tty on a character that is no longer a flow control character. Its update of lookahead_count can also undo the reset done by a concurrent n_tty_flush_buffer(). For tcflush(TCIFLUSH), tty_ioctl() first empties the flip buffer, and the ldisc flush then resets the count, so a lost reset keeps counting characters that were discarded. The receive path then treats that many new characters as already handled by the lookahead and skips their flow control action. Take termios_rwsem for reading in n_tty_lookahead_flow_ctrl(), as n_tty_receive_buf_common() does. Both are called from flush_to_ldisc() with port->buf.lock held, so this adds no new lock order. Fixes: 6bb6fa6908eb ("tty: Implement lookahead to process XON/XOFF timely") Reported-by: syzbot+0479f67e520e4910d370@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=0479f67e520e4910d370 Reported-by: syzbot+2430ce6b487fe0119d03@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=2430ce6b487fe0119d03 Signed-off-by: Jiayi Zhang --- Both syzbot reports are still in moderation and were not sent to the list; the Closes: links show them. Tested in QEMU on 946d9ed1aa02 with the syzbot C reproducer and the kernel config of the first report. The second report has no reproducer and was not tested. The reproducer runs for 5 seconds and does not turn KCSAN on (the config does not set CONFIG_KCSAN_EARLY_ENABLE), so for the test it ran for 9 minutes, wrote "on" to /sys/kernel/debug/kcsan, and filtered four unrelated races (move_linked_works, queue_work_on, process_one_work, ksys_read). Without the patch 5 of 5 instances reported this race within seconds; with the patch none of 5 did. Three of the patched instances later reported a race between copy_from_read_buf() and n_tty_receive_buf_common(), which this patch does not touch. AI tools assisted with the analysis, the code and the changelog. drivers/tty/n_tty.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index e6a0f5b..725d6ed 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1480,6 +1480,8 @@ static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp, struct n_tty_data *ldata = tty->disc_data; u8 flag = TTY_NORMAL; + guard(rwsem_read)(&tty->termios_rwsem); + ldata->lookahead_count += count; if (!I_IXON(tty)) -- 2.53.0