Skip to contentSkip to conversation

tty: Fix data race between tiocsti() and flush_to_ldisc()

PATCH In review Nguyen Dinh Phi 2 participants 2 messages Aug 23, 2021 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

---
V2:
- Remove FIXME comment.

 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index e8532006e960..6616d4a0d41d 100644
@@ -2290,8 +2290,6 @@ static int tty_fasync(int fd, struct file *filp, int on)
  *	Locking:
  *		Called functions take tty_ldiscs_lock
  *		current->signal->tty check is safe without locks
- *
- *	FIXME: may race normal receive processing
  */
 
 static int tiocsti(struct tty_struct *tty, char __user *p)
@@ -2307,8 +2305,10 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
 	ld = tty_ldisc_ref_wait(tty);
 	if (!ld)
 		return -EIO;
+	tty_buffer_lock_exclusive(tty->port);
 	if (ld->ops->receive_buf)
 		ld->ops->receive_buf(tty, &ch, &mbz, 1);
+	tty_buffer_unlock_exclusive(tty->port);
 	tty_ldisc_deref(ld);
 	return 0;
 }

--
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 12 lines · Nguyen Dinh Phi

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
Quoted 5 lines

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.

This makes sense. It mimics paste_selection() -- the lock order is preexisting. The normal path processing has the locks inverted, not sure why it doesn't matter :):
flush_to_ldisc
mutex_lock(&buf->lock); <--------- A (mutex)
receive_buf()

    tty_port_default_receive_buf()
      disc = tty_ldisc_ref(tty); <----- B (ldsem for READ)
Quoted 23 lines

---
V2:
- Remove FIXME comment.

  drivers/tty/tty_io.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +0 -2
index e8532006e960..6616d4a0d41d 100644
@@ -2290,8 +2290,6 @@ static int tty_fasync(int fd, struct file *filp, int on)
   *	Locking:
   *		Called functions take tty_ldiscs_lock
   *		current->signal->tty check is safe without locks
- *
- *	FIXME: may race normal receive processing
   */
 
  static int tiocsti(struct tty_struct *tty, char __user *p)

drivers/tty/tty_io.c static int tiocsti(struct tty_struct *tty, char __user *p) · line 2305

Quoted 1 line
drivers/tty/tty_io.c +0 -0
@@ -2307,8 +2305,10 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
  	ld = tty_ldisc_ref_wait(tty);

Here (and in paste_selection()), it is lock "B (ldsem) for READ".

drivers/tty/tty_io.c static int tiocsti(struct tty_struct *tty, char __user *p) · line 2308

Quoted 3 lines
drivers/tty/tty_io.c +1 -0
  	if (!ld)
  		return -EIO;
+	tty_buffer_lock_exclusive(tty->port);

and "A (mutex)".

drivers/tty/tty_io.c static int tiocsti(struct tty_struct *tty, char __user *p) · line 2314

Quoted 6 lines
drivers/tty/tty_io.c +1 -0
  	if (ld->ops->receive_buf)
  		ld->ops->receive_buf(tty, &ch, &mbz, 1);
+	tty_buffer_unlock_exclusive(tty->port);
  	tty_ldisc_deref(ld);
  	return 0;
  }

thanks,

Signature · 2 lines
js
suse labs