UPSTREAM: Bluetooth: hci_ldisc: Fix null pointer derefence in case of early data
authorJacob Chen <jacob2.chen@rock-chips.com>
Fri, 7 Oct 2016 01:10:45 +0000 (09:10 +0800)
committerHuang, Tao <huangtao@rock-chips.com>
Fri, 7 Oct 2016 03:35:58 +0000 (11:35 +0800)
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>
Change-Id: Ibe366f3222cbe7a093cd08aaecbc0de1004088c8
Signed-off-by: Jacob Chen <jacob2.chen@rock-chips.com>
(cherry picked from commit 84cb3df02aea4b00405521e67c4c67c2d525c364)

drivers/bluetooth/hci_ldisc.c
drivers/bluetooth/hci_uart.h

index 96bcec5598c221e4cec2841bbc4eb521d20778c1..61bb2192a80b01b67b3ef5bf33031fe36ef819cb 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;
@@ -497,7 +497,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);
@@ -505,6 +505,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);
 }
@@ -531,7 +532,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);
 }
 
@@ -555,7 +556,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
@@ -643,9 +644,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 82c92f1b65b4af7c317c8a0af91723a481ac3874..a4d52fe704459b494f00ac380de6b924c8a3c1ff 100644 (file)
@@ -94,6 +94,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