Skip to content

Commit bb2853a

Browse files
ita93gregkh
authored andcommitted
tty: Fix data race between tiocsti() and flush_to_ldisc()
The ops->receive_buf() may be accessed concurrently from these two functions. If the driver flushes data to the line discipline receive_buf() method while tiocsti() is waiting for the ops->receive_buf() to finish its work, the data race will happen. For example: tty_ioctl |tty_ldisc_receive_buf ->tioctsi | ->tty_port_default_receive_buf | ->tty_ldisc_receive_buf ->hci_uart_tty_receive | ->hci_uart_tty_receive ->h4_recv | ->h4_recv In this case, the h4 receive buffer will be overwritten by the latecomer, and we will lost the data. Hence, change tioctsi() function to use the exclusive lock interface from tty_buffer to avoid the data race. Reported-by: [email protected] Reviewed-by: Jiri Slaby <[email protected]> Signed-off-by: Nguyen Dinh Phi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Cc: stable <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 74d2fb7 commit bb2853a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

drivers/tty/tty_io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,8 +2290,6 @@ static int tty_fasync(int fd, struct file *filp, int on)
22902290
* Locking:
22912291
* Called functions take tty_ldiscs_lock
22922292
* current->signal->tty check is safe without locks
2293-
*
2294-
* FIXME: may race normal receive processing
22952293
*/
22962294

22972295
static int tiocsti(struct tty_struct *tty, char __user *p)
@@ -2307,8 +2305,10 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
23072305
ld = tty_ldisc_ref_wait(tty);
23082306
if (!ld)
23092307
return -EIO;
2308+
tty_buffer_lock_exclusive(tty->port);
23102309
if (ld->ops->receive_buf)
23112310
ld->ops->receive_buf(tty, &ch, &mbz, 1);
2311+
tty_buffer_unlock_exclusive(tty->port);
23122312
tty_ldisc_deref(ld);
23132313
return 0;
23142314
}

0 commit comments

Comments
 (0)