USB: serial: keyspan_pda: fix receive sanity checks
authorJohan Hovold <johan@kernel.org>
Tue, 31 Jan 2017 16:17:29 +0000 (17:17 +0100)
committerBen Hutchings <ben@decadent.org.uk>
Mon, 5 Jun 2017 20:13:46 +0000 (21:13 +0100)
commit c528fcb116e61afc379a2e0a0f70906b937f1e2c upstream.

Make sure to check for short transfers before parsing the receive buffer
to avoid acting on stale data.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
[bwh: Backported to 3.2:
 - Adjust context
 - Keep the check for !tty in the data case]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/usb/serial/keyspan_pda.c

index 65a0906..611904f 100644 (file)
@@ -212,6 +212,7 @@ static void keyspan_pda_rx_interrupt(struct urb *urb)
        struct usb_serial_port *port = urb->context;
        struct tty_struct *tty;
        unsigned char *data = urb->transfer_buffer;
        struct usb_serial_port *port = urb->context;
        struct tty_struct *tty;
        unsigned char *data = urb->transfer_buffer;
+       unsigned int len = urb->actual_length;
        int retval;
        int status = urb->status;
        struct keyspan_pda_private *priv;
        int retval;
        int status = urb->status;
        struct keyspan_pda_private *priv;
@@ -234,20 +235,28 @@ static void keyspan_pda_rx_interrupt(struct urb *urb)
                goto exit;
        }
 
                goto exit;
        }
 
+       if (len < 1) {
+               dev_warn(&port->dev, "short message received\n");
+               goto exit;
+       }
+
        /* see if the message is data or a status interrupt */
        switch (data[0]) {
        case 0:
                tty = tty_port_tty_get(&port->port);
                 /* rest of message is rx data */
        /* see if the message is data or a status interrupt */
        switch (data[0]) {
        case 0:
                tty = tty_port_tty_get(&port->port);
                 /* rest of message is rx data */
-               if (tty && urb->actual_length) {
-                       tty_insert_flip_string(tty, data + 1,
-                                               urb->actual_length - 1);
-                       tty_flip_buffer_push(tty);
-               }
+               if (!tty || len < 2)
+                       break;
+               tty_insert_flip_string(tty, data + 1, len - 1);
+               tty_flip_buffer_push(tty);
                tty_kref_put(tty);
                break;
        case 1:
                /* status interrupt */
                tty_kref_put(tty);
                break;
        case 1:
                /* status interrupt */
+               if (len < 3) {
+                       dev_warn(&port->dev, "short interrupt message received\n");
+                       break;
+               }
                dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
                switch (data[1]) {
                case 1: /* modemline change */
                dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
                switch (data[1]) {
                case 1: /* modemline change */