Re: [PATCH] tty: n_tty: Protect poll input checks against termios changes

From: Greg KH

Date: Thu Dec 03 2026 - 22:54:01 EST


On Wed, Nov 11, 2026 at 04:41:16PM +0800, Zhu Jun wrote:
> n_tty_poll() calls input_available_p() without holding termios_rwsem.
> It can therefore race with n_tty_set_termios() changing the canonical
> mode and resetting the buffer indices. In particular, the availability
> check can combine one input mode with indices from the other mode.
>
> Take the read side of termios_rwsem for each poll input check, as the
> read path already does. Release it before flushing the receive worker,
> since that worker also needs termios_rwsem.
>
> Fixes: 1da177e4c3f41 ("Linux-2.6.12-rc2")
> Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
> ---
> drivers/tty/n_tty.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
> index e6a0f5b40d0a..115bc4a67e8a 100644
> --- a/drivers/tty/n_tty.c
> +++ b/drivers/tty/n_tty.c
> @@ -2416,6 +2416,12 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
> return (b - buf) ? b - buf : retval;
> }
>
> +static bool n_tty_poll_input(struct tty_struct *tty)
> +{
> + guard(rwsem_read)(&tty->termios_rwsem);
> + return input_available_p(tty, 1);
> +}
> +
> /**
> * n_tty_poll - poll method for N_TTY
> * @tty: terminal device
> @@ -2437,11 +2443,11 @@ static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
>
> poll_wait(file, &tty->read_wait, wait);
> poll_wait(file, &tty->write_wait, wait);
> - if (input_available_p(tty, 1))
> + if (n_tty_poll_input(tty)) {
> mask |= EPOLLIN | EPOLLRDNORM;
> - else {
> + } else {
> tty_buffer_flush_work(tty->port);
> - if (input_available_p(tty, 1))
> + if (n_tty_poll_input(tty))

This change doesn't actually fix this properly. Be more careful next
time please and try to write a test to verify that your change will
actually work.

thanks,

greg k-h