tty: Prevent writing chars during tcsetattr TCSADRAIN/FLUSH
commit094fb49a2d
upstream. If userspace races tcsetattr() with a write, the drained condition might not be guaranteed by the kernel. There is a race window after checking Tx is empty before tty_set_termios() takes termios_rwsem for write. During that race window, more characters can be queued by a racing writer. Any ongoing transmission might produce garbage during HW's ->set_termios() call. The intent of TCSADRAIN/FLUSH seems to be preventing such a character corruption. If those flags are set, take tty's write lock to stop any writer before performing the lower layer Tx empty check and wait for the pending characters to be sent (if any). The initial wait for all-writers-done must be placed outside of tty's write lock to avoid deadlock which makes it impossible to use tty_wait_until_sent(). The write lock is retried if a racing write is detected. Fixes:1da177e4c3
("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Link: https://lore.kernel.org/r/20230317113318.31327-2-ilpo.jarvinen@linux.intel.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Родитель
fa30909fbd
Коммит
321e16a570
|
@ -62,6 +62,8 @@ int __tty_check_change(struct tty_struct *tty, int sig);
|
|||
int tty_check_change(struct tty_struct *tty);
|
||||
void __stop_tty(struct tty_struct *tty);
|
||||
void __start_tty(struct tty_struct *tty);
|
||||
void tty_write_unlock(struct tty_struct *tty);
|
||||
int tty_write_lock(struct tty_struct *tty, int ndelay);
|
||||
void tty_vhangup_session(struct tty_struct *tty);
|
||||
void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty);
|
||||
int tty_signal_session_leader(struct tty_struct *tty, int exit_session);
|
||||
|
|
|
@ -950,13 +950,13 @@ static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)
|
|||
return i;
|
||||
}
|
||||
|
||||
static void tty_write_unlock(struct tty_struct *tty)
|
||||
void tty_write_unlock(struct tty_struct *tty)
|
||||
{
|
||||
mutex_unlock(&tty->atomic_write_lock);
|
||||
wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
|
||||
}
|
||||
|
||||
static int tty_write_lock(struct tty_struct *tty, int ndelay)
|
||||
int tty_write_lock(struct tty_struct *tty, int ndelay)
|
||||
{
|
||||
if (!mutex_trylock(&tty->atomic_write_lock)) {
|
||||
if (ndelay)
|
||||
|
|
|
@ -421,22 +421,43 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
|
|||
tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
|
||||
tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
|
||||
|
||||
ld = tty_ldisc_ref(tty);
|
||||
if (opt & (TERMIOS_FLUSH|TERMIOS_WAIT)) {
|
||||
retry_write_wait:
|
||||
retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
|
||||
if (retval < 0)
|
||||
return retval;
|
||||
|
||||
if (ld != NULL) {
|
||||
if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
|
||||
ld->ops->flush_buffer(tty);
|
||||
tty_ldisc_deref(ld);
|
||||
if (tty_write_lock(tty, 0) < 0)
|
||||
goto retry_write_wait;
|
||||
|
||||
/* Racing writer? */
|
||||
if (tty_chars_in_buffer(tty)) {
|
||||
tty_write_unlock(tty);
|
||||
goto retry_write_wait;
|
||||
}
|
||||
|
||||
ld = tty_ldisc_ref(tty);
|
||||
if (ld != NULL) {
|
||||
if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
|
||||
ld->ops->flush_buffer(tty);
|
||||
tty_ldisc_deref(ld);
|
||||
}
|
||||
|
||||
if ((opt & TERMIOS_WAIT) && tty->ops->wait_until_sent) {
|
||||
tty->ops->wait_until_sent(tty, 0);
|
||||
if (signal_pending(current)) {
|
||||
tty_write_unlock(tty);
|
||||
return -ERESTARTSYS;
|
||||
}
|
||||
}
|
||||
|
||||
tty_set_termios(tty, &tmp_termios);
|
||||
|
||||
tty_write_unlock(tty);
|
||||
} else {
|
||||
tty_set_termios(tty, &tmp_termios);
|
||||
}
|
||||
|
||||
if (opt & TERMIOS_WAIT) {
|
||||
tty_wait_until_sent(tty, 0);
|
||||
if (signal_pending(current))
|
||||
return -ERESTARTSYS;
|
||||
}
|
||||
|
||||
tty_set_termios(tty, &tmp_termios);
|
||||
|
||||
/* FIXME: Arguably if tmp_termios == tty->termios AND the
|
||||
actual requested termios was not tmp_termios then we may
|
||||
want to return an error as no user requested change has
|
||||
|
|
Загрузка…
Ссылка в новой задаче