Re: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl()
From: Greg Kroah-Hartman
Date: Fri Dec 04 2026 - 04:02:15 EST
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@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=0479f67e520e4910d370
> Reported-by: syzbot+2430ce6b487fe0119d03@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=2430ce6b487fe0119d03
> Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
> ---
> 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