Linux Serial subsystem development
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Jiayi Zhang <zhangjiayi1014@gmail.com>
Cc: "Jiri Slaby" <jirislaby@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Andy Shevchenko" <andy.shevchenko@gmail.com>,
	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: Re: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl()
Date: Fri, 4 Dec 2026 17:47:58 +0900	[thread overview]
Message-ID: <2026120429-sharper-approval-9326@gregkh> (raw)
In-Reply-To: <20261202180854.4154340-1-zhangjiayi1014@gmail.com>

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

      parent reply	other threads:[~2026-12-04  9:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 18:08 [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl() Jiayi Zhang
2026-12-02 18:18 ` sashiko-bot
2026-12-04  8:47 ` Greg Kroah-Hartman [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2026120429-sharper-approval-9326@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=andy.shevchenko@gmail.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=jirislaby@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=syzbot+0479f67e520e4910d370@syzkaller.appspotmail.com \
    --cc=syzbot+2430ce6b487fe0119d03@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=zhangjiayi1014@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox