* [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl()
@ 2026-12-02 18:08 Jiayi Zhang
2026-12-04 8:47 ` Greg Kroah-Hartman
0 siblings, 1 reply; 4+ messages in thread
From: Jiayi Zhang @ 2026-12-02 18:08 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Ilpo Järvinen, Andy Shevchenko, linux-serial, linux-kernel,
syzbot+0479f67e520e4910d370, syzbot+2430ce6b487fe0119d03,
syzkaller-bugs
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 <zhangjiayi1014@gmail.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl()
2026-12-02 18:08 [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl() Jiayi Zhang
@ 2026-12-04 8:47 ` Greg Kroah-Hartman
2026-12-04 10:36 ` Pimyn Girgis
2026-12-05 8:25 ` Jiayi Zhang
0 siblings, 2 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-04 8:47 UTC (permalink / raw)
To: Jiayi Zhang
Cc: Jiri Slaby, Ilpo Järvinen, Andy Shevchenko, linux-serial,
linux-kernel, syzbot+0479f67e520e4910d370,
syzbot+2430ce6b487fe0119d03, syzkaller-bugs
On Wed, Dec 02, 2026 at 06:08:54PM +0000, Jiayi Zhang wrote:
> 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 <zhangjiayi1014@gmail.com>
> ---
> 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
>
>
What changed to cause this to be needed now? And why the "private"
syzbot reports?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl()
2026-12-04 8:47 ` Greg Kroah-Hartman
@ 2026-12-04 10:36 ` Pimyn Girgis
2026-12-05 8:25 ` Jiayi Zhang
1 sibling, 0 replies; 4+ messages in thread
From: Pimyn Girgis @ 2026-12-04 10:36 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Jiayi Zhang, Jiri Slaby, Ilpo Järvinen, Andy Shevchenko,
linux-serial, linux-kernel, syzbot+0479f67e520e4910d370,
syzbot+2430ce6b487fe0119d03, syzkaller-bugs
On Fri, Dec 4, 2026 at 10:02 AM 'Greg Kroah-Hartman' via
syzkaller-bugs <syzkaller-bugs@googlegroups.com> wrote:
> What changed to cause this to be needed now? And why the "private"
> syzbot reports?
Hi Greg,
Regarding your question about the "private" syzbot reports:
They are actually public. All bugs that reached the
syzkaller-upstream-moderation mailing list are visible on the
dashboard (via the Closes: links); they just haven't been forwarded to
the kernel subsystem lists yet.
For KCSAN data-race reports specifically, most races turn out to be
benign, so syzbot runs AI triage to classify them:
- Non-benign KCSAN reports are automatically forwarded to the kernel
subsystem lists.
- Benign ones stay on syzkaller-upstream-moderation indefinitely until
someone manually forwards them upstream with #syz upstream.
Best,
Pimyn
--
Ⲡⲟⲓⲙⲏⲛ Ⲅⲉⲱⲣⲅⲓⲟⲥ - Pimyn Girgis
Software Engineer
Kernel Dynamic Analysis
pimyn@google.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl()
2026-12-04 8:47 ` Greg Kroah-Hartman
2026-12-04 10:36 ` Pimyn Girgis
@ 2026-12-05 8:25 ` Jiayi Zhang
1 sibling, 0 replies; 4+ messages in thread
From: Jiayi Zhang @ 2026-12-05 8:25 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Jiri Slaby, Ilpo Järvinen, Andy Shevchenko,
Osama Abdelkader, Pimyn Girgis, linux-serial, linux-kernel,
syzbot+0479f67e520e4910d370, syzbot+2430ce6b487fe0119d03,
syzkaller-bugs
On Fri, Dec 04, 2026, Greg Kroah-Hartman wrote:
> What changed to cause this to be needed now?
Nothing changed. n_tty_lookahead_flow_ctrl() has read the termios and
updated ldata->lookahead_count without termios_rwsem since the
lookahead was added in 6bb6fa6908eb ("tty: Implement lookahead to
process XON/XOFF timely"), in v6.0. syzbot's first reports of the two
races are from August and September 2022. The reports in the Closes:
tags are the 5th and the 10th with these titles. syzbot closed each
earlier one as obsolete after it had not recurred for a while.
> And why the "private" syzbot reports?
Pimyn has explained how the moderation list works. The dashboard
pages show that syzbot's AI triage marked both races as benign, so
neither report was forwarded to the lists. I found them while going
through open syzbot reports on the dashboard.
For the hangup in the second report the benign rating is right:
tty_ldisc_hangup() closes the ldisc after the flush, and n_tty_close()
frees ldata. The other cases need backed-up input, because only then
does the lookahead run, and a concurrent TCIFLUSH or TCSETS. After
TCIFLUSH, a lost reset makes the receive path drop later START/STOP
characters without stopping or starting the tty. A TCSETS that clears
IXON during a lookahead pass can leave the tty stopped. I found these
by reading the code and have not seen them happen.
Sorry, I missed that Osama Abdelkader (Cc'd) sent the same change in
February for an earlier report of the flush race:
https://lore.kernel.org/all/20260211210838.45127-1-osama.abdelkader@gmail.com/
You replied then that it felt wrong and asked for a lot of testing
and validation. My testing was the syzbot reproducer with KCSAN
(described below the --- of my patch) and the same reproducer for
9 minutes on 5 VMs with lockdep enabled, which reported nothing.
Since Osama's patch came first, please drop mine. If you want the
change, please take his.
Thanks,
Jiayi
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-05 8:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 18:08 [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl() Jiayi Zhang
2026-12-04 8:47 ` Greg Kroah-Hartman
2026-12-04 10:36 ` Pimyn Girgis
2026-12-05 8:25 ` Jiayi Zhang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®