]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
tty: Prevent writing chars during tcsetattr TCSADRAIN/FLUSH
authorIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Fri, 17 Mar 2023 11:33:17 +0000 (13:33 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 11 May 2023 14:10:50 +0000 (23:10 +0900)
commit86cac5eb2ff8be1fbbb22b53313b61b01dbb9dee
treeb241e368f74a04909b4a8e7c7c1456ab3d231c05
parent72863799f1ced837a76ff037a2e9454e314ad788
tty: Prevent writing chars during tcsetattr TCSADRAIN/FLUSH

commit 094fb49a2d0d6827c86d2e0840873e6db0c491d2 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: 1da177e4c3f4 ("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>
drivers/tty/tty.h
drivers/tty/tty_io.c
drivers/tty/tty_ioctl.c