Skip to content

Commit 32a7b4c

Browse files
jeremyclineholtmann
authored andcommitted
Bluetooth: hci_ldisc: Initialize hci_dev before open()
The hci_dev struct hdev is referenced in work queues and timers started by open() in some protocols. This creates a race between the initialization function and the work or timer which can result hdev being dereferenced while it is still null. The syzbot report contains a reliable reproducer which causes a null pointer dereference of hdev in hci_uart_write_work() by making the memory allocation for hdev fail. To fix this, ensure hdev is valid from before calling a protocol's open() until after calling a protocol's close(). Reported-by: [email protected] Signed-off-by: Jeremy Cline <[email protected]> Signed-off-by: Marcel Holtmann <[email protected]>
1 parent 035a960 commit 32a7b4c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

drivers/bluetooth/hci_ldisc.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ void hci_uart_init_work(struct work_struct *work)
207207
err = hci_register_dev(hu->hdev);
208208
if (err < 0) {
209209
BT_ERR("Can't register HCI device");
210+
clear_bit(HCI_UART_PROTO_READY, &hu->flags);
211+
hu->proto->close(hu);
210212
hdev = hu->hdev;
211213
hu->hdev = NULL;
212214
hci_free_dev(hdev);
213-
clear_bit(HCI_UART_PROTO_READY, &hu->flags);
214-
hu->proto->close(hu);
215215
return;
216216
}
217217

@@ -616,6 +616,7 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
616616
static int hci_uart_register_dev(struct hci_uart *hu)
617617
{
618618
struct hci_dev *hdev;
619+
int err;
619620

620621
BT_DBG("");
621622

@@ -659,11 +660,22 @@ static int hci_uart_register_dev(struct hci_uart *hu)
659660
else
660661
hdev->dev_type = HCI_PRIMARY;
661662

663+
/* Only call open() for the protocol after hdev is fully initialized as
664+
* open() (or a timer/workqueue it starts) may attempt to reference it.
665+
*/
666+
err = hu->proto->open(hu);
667+
if (err) {
668+
hu->hdev = NULL;
669+
hci_free_dev(hdev);
670+
return err;
671+
}
672+
662673
if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
663674
return 0;
664675

665676
if (hci_register_dev(hdev) < 0) {
666677
BT_ERR("Can't register HCI device");
678+
hu->proto->close(hu);
667679
hu->hdev = NULL;
668680
hci_free_dev(hdev);
669681
return -ENODEV;
@@ -683,17 +695,12 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
683695
if (!p)
684696
return -EPROTONOSUPPORT;
685697

686-
err = p->open(hu);
687-
if (err)
688-
return err;
689-
690698
hu->proto = p;
691699
set_bit(HCI_UART_PROTO_READY, &hu->flags);
692700

693701
err = hci_uart_register_dev(hu);
694702
if (err) {
695703
clear_bit(HCI_UART_PROTO_READY, &hu->flags);
696-
p->close(hu);
697704
return err;
698705
}
699706

0 commit comments

Comments
 (0)