linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto()
@ 2019-02-23  4:33 Kefeng Wang
  2019-02-24  1:12 ` Jeremy Cline
  2019-02-26  8:56 ` Marcel Holtmann
  0 siblings, 2 replies; 3+ messages in thread
From: Kefeng Wang @ 2019-02-23  4:33 UTC (permalink / raw)
  To: Marcel Holtmann, linux-bluetooth, linux-kernel
  Cc: Jeremy Cline, Johan Hedberg, Kefeng Wang, syzbot+899a33dc0fa0dbaf06a6

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.

Reported-by: syzbot+899a33dc0fa0dbaf06a6@syzkaller.appspotmail.com
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 drivers/bluetooth/hci_ldisc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c
index 4918fefc4a6f..9562e72c1ae5 100644
--- a/drivers/bluetooth/hci_ldisc.c
+++ b/drivers/bluetooth/hci_ldisc.c
@@ -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;
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto()
  2019-02-23  4:33 [PATCH] Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto() Kefeng Wang
@ 2019-02-24  1:12 ` Jeremy Cline
  2019-02-26  8:56 ` Marcel Holtmann
  1 sibling, 0 replies; 3+ messages in thread
From: Jeremy Cline @ 2019-02-24  1:12 UTC (permalink / raw)
  To: Kefeng Wang
  Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, Johan Hedberg,
	syzbot+899a33dc0fa0dbaf06a6

On Sat, Feb 23, 2019 at 12:33:27PM +0800, Kefeng Wang wrote:
> 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

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.

> 				              - 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.
> 
> Reported-by: syzbot+899a33dc0fa0dbaf06a6@syzkaller.appspotmail.com
> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
> ---
>  drivers/bluetooth/hci_ldisc.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c
> index 4918fefc4a6f..9562e72c1ae5 100644
> --- a/drivers/bluetooth/hci_ldisc.c
> +++ b/drivers/bluetooth/hci_ldisc.c
> @@ -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;
>  }
>  
> -- 
> 2.20.1
> 

For what it's worth:

Reviewed-by: Jeremy Cline <jcline@redhat.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto()
  2019-02-23  4:33 [PATCH] Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto() Kefeng Wang
  2019-02-24  1:12 ` Jeremy Cline
@ 2019-02-26  8:56 ` Marcel Holtmann
  1 sibling, 0 replies; 3+ messages in thread
From: Marcel Holtmann @ 2019-02-26  8:56 UTC (permalink / raw)
  To: Kefeng Wang
  Cc: open list:BLUETOOTH DRIVERS, linux-kernel, Jeremy Cline,
	Johan Hedberg, syzbot+899a33dc0fa0dbaf06a6

Hi Kefeng,

> 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.
> 
> Reported-by: syzbot+899a33dc0fa0dbaf06a6@syzkaller.appspotmail.com
> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
> ---
> drivers/bluetooth/hci_ldisc.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-02-26  8:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-23  4:33 [PATCH] Bluetooth: hci_ldisc: Postpone HCI_UART_PROTO_READY bit set in hci_uart_set_proto() Kefeng Wang
2019-02-24  1:12 ` Jeremy Cline
2019-02-26  8:56 ` Marcel Holtmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).