serial: core: Fix x_char race
authorPeter Hurley <peter@hurleysoftware.com>
Tue, 2 Sep 2014 21:39:13 +0000 (17:39 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 8 Sep 2014 23:22:42 +0000 (16:22 -0700)
The UART driver is expected to clear port->x_char after
transmission while holding the port->lock. However, the serial
core fails to take the port->lock before assigning port->xchar.
This allows for the following race

CPU 0                         |  CPU 1
                              |
                              | serial8250_handle_irq
                              |   ...
                              |   serial8250_tx_chars
                              |     if (port->x_char)
                              |       serial_out(up, UART_TX, port->x_char)
uart_send_xchar               |
  port->x_char = ch           |
                              |       port->x_char = 0
  port->ops->start_tx()       |
                              |

The x_char on CPU 0 will never be sent.

Take the port->lock in uart_send_xchar() before assigning port->x_char.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/serial/serial_core.c

index 0742f77ac4104e98b29e8d9ab9a50cda9054bf97..bd20cf51e912c326d67f02631b078035d20b15b4 100644 (file)
@@ -600,12 +600,11 @@ static void uart_send_xchar(struct tty_struct *tty, char ch)
        if (port->ops->send_xchar)
                port->ops->send_xchar(port, ch);
        else {
+               spin_lock_irqsave(&port->lock, flags);
                port->x_char = ch;
-               if (ch) {
-                       spin_lock_irqsave(&port->lock, flags);
+               if (ch)
                        port->ops->start_tx(port);
-                       spin_unlock_irqrestore(&port->lock, flags);
-               }
+               spin_unlock_irqrestore(&port->lock, flags);
        }
 }