Bluetooth: hci_ldisc: Fix null pointer derefence in case of early data
authorLoic Poulain <loic.poulain@intel.com>
Mon, 4 Apr 2016 08:48:13 +0000 (10:48 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 8 Apr 2016 16:58:56 +0000 (18:58 +0200)
HCI_UART_PROTO_SET flag is set before hci_uart_set_proto call. If we
receive data from tty layer during this procedure, proto pointer may
not be assigned yet, leading to null pointer dereference in rx method
hci_uart_tty_receive.

This patch fixes this issue by introducing HCI_UART_PROTO_READY flag in
order to avoid any proto operation before proto opening and assignment.

Signed-off-by: Loic Poulain <loic.poulain@intel.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
drivers/bluetooth/hci_ldisc.c
drivers/bluetooth/hci_uart.h

index c00168a5bb800f3e74f6924f197e668020f02eb9..49b3e1e2d236ef6fa45c652b8c917532ba62ba06 100644 (file)
@@ -227,7 +227,7 @@ static int hci_uart_flush(struct hci_dev *hdev)
        tty_ldisc_flush(tty);
        tty_driver_flush_buffer(tty);
 
-       if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
+       if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
                hu->proto->flush(hu);
 
        return 0;
@@ -492,7 +492,7 @@ static void hci_uart_tty_close(struct tty_struct *tty)
 
        cancel_work_sync(&hu->write_work);
 
-       if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
+       if (test_and_clear_bit(HCI_UART_PROTO_READY, &hu->flags)) {
                if (hdev) {
                        if (test_bit(HCI_UART_REGISTERED, &hu->flags))
                                hci_unregister_dev(hdev);
@@ -500,6 +500,7 @@ static void hci_uart_tty_close(struct tty_struct *tty)
                }
                hu->proto->close(hu);
        }
+       clear_bit(HCI_UART_PROTO_SET, &hu->flags);
 
        kfree(hu);
 }
@@ -526,7 +527,7 @@ static void hci_uart_tty_wakeup(struct tty_struct *tty)
        if (tty != hu->tty)
                return;
 
-       if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
+       if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
                hci_uart_tx_wakeup(hu);
 }
 
@@ -550,7 +551,7 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
        if (!hu || tty != hu->tty)
                return;
 
-       if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
+       if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
                return;
 
        /* It does not need a lock here as it is already protected by a mutex in
@@ -638,9 +639,11 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
                return err;
 
        hu->proto = p;
+       set_bit(HCI_UART_PROTO_READY, &hu->flags);
 
        err = hci_uart_register_dev(hu);
        if (err) {
+               clear_bit(HCI_UART_PROTO_READY, &hu->flags);
                p->close(hu);
                return err;
        }
index 4814ff08f4270156c70aaced894ec0cb2a5d5f63..839bad1d81522a2718245517c1e1dca74e2d336b 100644 (file)
@@ -95,6 +95,7 @@ struct hci_uart {
 /* HCI_UART proto flag bits */
 #define HCI_UART_PROTO_SET     0
 #define HCI_UART_REGISTERED    1
+#define HCI_UART_PROTO_READY   2
 
 /* TX states  */
 #define HCI_UART_SENDING       1