The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] tty: n_tty: Protect poll input checks against termios changes
@ 2026-11-11  8:41 Zhu Jun
  2026-12-03 22:35 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Zhu Jun @ 2026-11-11  8:41 UTC (permalink / raw)
  To: jirislaby
  Cc: gregkh, linux-serial, linux-kernel, syzbot+3a7eb6d437d568d1c34d,
	Zhu Jun

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@cmss.chinamobile.com>
---
 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))
 			mask |= EPOLLIN | EPOLLRDNORM;
 	}
 	if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
-- 
2.51.0




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] tty: n_tty: Protect poll input checks against termios changes
  2026-11-11  8:41 [PATCH] tty: n_tty: Protect poll input checks against termios changes Zhu Jun
@ 2026-12-03 22:35 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2026-12-03 22:35 UTC (permalink / raw)
  To: Zhu Jun; +Cc: jirislaby, linux-serial, linux-kernel,
	syzbot+3a7eb6d437d568d1c34d

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@cmss.chinamobile.com>
> ---
>  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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04  3:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11  8:41 [PATCH] tty: n_tty: Protect poll input checks against termios changes Zhu Jun
2026-12-03 22:35 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox