Skip to content

Commit 6ea83d9

Browse files
jeremyclinegregkh
authored andcommitted
Bluetooth: hci_ldisc: Initialize hci_dev before open()
commit 32a7b4c upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3df00eb commit 6ea83d9

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 @@ static 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

@@ -612,6 +612,7 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
612612
static int hci_uart_register_dev(struct hci_uart *hu)
613613
{
614614
struct hci_dev *hdev;
615+
int err;
615616

616617
BT_DBG("");
617618

@@ -655,11 +656,22 @@ static int hci_uart_register_dev(struct hci_uart *hu)
655656
else
656657
hdev->dev_type = HCI_PRIMARY;
657658

659+
/* Only call open() for the protocol after hdev is fully initialized as
660+
* open() (or a timer/workqueue it starts) may attempt to reference it.
661+
*/
662+
err = hu->proto->open(hu);
663+
if (err) {
664+
hu->hdev = NULL;
665+
hci_free_dev(hdev);
666+
return err;
667+
}
668+
658669
if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
659670
return 0;
660671

661672
if (hci_register_dev(hdev) < 0) {
662673
BT_ERR("Can't register HCI device");
674+
hu->proto->close(hu);
663675
hu->hdev = NULL;
664676
hci_free_dev(hdev);
665677
return -ENODEV;
@@ -679,17 +691,12 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
679691
if (!p)
680692
return -EPROTONOSUPPORT;
681693

682-
err = p->open(hu);
683-
if (err)
684-
return err;
685-
686694
hu->proto = p;
687695
set_bit(HCI_UART_PROTO_READY, &hu->flags);
688696

689697
err = hci_uart_register_dev(hu);
690698
if (err) {
691699
clear_bit(HCI_UART_PROTO_READY, &hu->flags);
692-
p->close(hu);
693700
return err;
694701
}
695702

0 commit comments

Comments
 (0)