Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto()
PATCH 1/1 Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto()
task A: task B: hci_uart_set_proto flush_to_ldisc - p->open(hu) -> h5_open //alloc h5 - receive_buf
- set_bit HCI_UART_PROTO_READY - tty_port_default_receive_buf
- hci_uart_register_dev - tty_ldisc_receive_buf
- hci_uart_tty_receive
- test_bit HCI_UART_PROTO_READY
- h5_recv
- clear_bit HCI_UART_PROTO_READY while() {
- p->open(hu) -> h5_close //free h5
- h5_rx_3wire_hdr
- h5_reset() //use-after-free
}
It could use ioctl to set hci uart proto, but there is a use-after-free issue when hci_uart_register_dev() fail in hci_uart_set_proto(), see stack above, fix this by setting HCI_UART_PROTO_READY bit only when hci_uart_register_dev() return success.
--- drivers/bluetooth/hci_ldisc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/bluetooth/hci_ldisc.c +1 -2
@@ -696,14 +696,13 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)return -EPROTONOSUPPORT ; 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);return err; } +set_bit (HCI_UART_PROTO_READY , &hu->flags);return 0 ; }
Signature · 1 line
2.20.1
Quoted 10 lines · Kefeng Wang
task A: task B: hci_uart_set_proto flush_to_ldisc - p->open(hu) -> h5_open //alloc h5 - receive_buf-
set_bitHCI_UART_PROTO_READY-tty_port_default_receive_buf
-hci_uart_register_dev-tty_ldisc_receive_buf
-hci_uart_tty_receive
-test_bitHCI_UART_PROTO_READY
-h5_recv
-clear_bitHCI_UART_PROTO_READYwhile(){
-p->open(hu)->h5_close//free h5
I think commit 32a7b4cbe93b ("Bluetooth: hci_ldisc: Initialize hci_dev before open()") in made this worse by inadvertently changing this from
- clear_bit HCI_UART_PROTO_READY
- p->close(hu) -> h5_close //free h5
to
- p->close(hu) -> h5_close //free h5
- clear_bit HCI_UART_PROTO_READY
because the close call got moved into hci_uart_register_dev(). I guess that made the race window sufficiently wide to be easily reproducible, but it looks like it was present even before that commit.
Quoted 39 lines
-
h5_rx_3wire_hdr
-h5_reset()//use-after-free
}It could use ioctl to set hci uart proto, but there is a use-after-free issue when
hci_uart_register_dev()fail inhci_uart_set_proto(), see stack above, fix this by settingHCI_UART_PROTO_READYbit only whenhci_uart_register_dev()return success.Reported-by [email protected]Signed-off-by Kefeng Wang <[email protected]>--- drivers/bluetooth/hci_ldisc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/bluetooth/hci_ldisc.c +1
-2@@ -696,14 +696,13 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)return -EPROTONOSUPPORT ; 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);return err; } +set_bit (HCI_UART_PROTO_READY , &hu->flags);return 0 ; }Signature · 1 line
2.20.1
For what it's worth:
Marcel Holtmann
maintainerHi Kefeng,
Quoted 20 lines
task A: task B:
hci_uart_set_protoflush_to_ldisc
-p->open(hu)->h5_open//alloc h5 -receive_buf
-set_bitHCI_UART_PROTO_READY-tty_port_default_receive_buf
-hci_uart_register_dev-tty_ldisc_receive_buf
-hci_uart_tty_receive
-test_bitHCI_UART_PROTO_READY
-h5_recv
-clear_bitHCI_UART_PROTO_READYwhile(){
-p->open(hu)->h5_close//free h5
-h5_rx_3wire_hdr
-h5_reset()//use-after-free
}It could use ioctl to set hci uart proto, but there is a use-after-free issue when
hci_uart_register_dev()fail inhci_uart_set_proto(), see stack above, fix this by settingHCI_UART_PROTO_READYbit only whenhci_uart_register_dev()return success.
Quoted 5 lines
Reported-by [email protected]Signed-off-by Kefeng Wang <[email protected]>---
drivers/bluetooth/hci_ldisc.c| 3 +--1 file changed, 1 insertion(+), 2 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel