linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Bluetooth:  Stop BCSP/H5 timer before cleaning up
@ 2014-02-18  8:48 Michael Knudsen
  2014-03-04  5:25 ` Michael Knudsen
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Knudsen @ 2014-02-18  8:48 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Michael Knudsen

When stopping BCSP/H5, stop the retransmission timer before proceeding
to clean up packet queues.  The previous code had a race condition where
the timer could trigger after the packet lists and protocol structure
had been removed which lead to dereferencing NULL or use-after-free bugs.

Signed-off-by: Michael Knudsen <m.knudsen@samsung.com>
Reported-by: Kirill Tkhai <ktkhai@parallels.com>
---
 drivers/bluetooth/hci_bcsp.c |    4 +++-
 drivers/bluetooth/hci_h5.c   |    4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/bluetooth/hci_bcsp.c b/drivers/bluetooth/hci_bcsp.c
index 0bc87f7..eee2fb2 100644
--- a/drivers/bluetooth/hci_bcsp.c
+++ b/drivers/bluetooth/hci_bcsp.c
@@ -715,6 +715,9 @@ static int bcsp_open(struct hci_uart *hu)
 static int bcsp_close(struct hci_uart *hu)
 {
 	struct bcsp_struct *bcsp = hu->priv;
+
+	del_timer_sync(&bcsp->tbcsp);
+
 	hu->priv = NULL;
 
 	BT_DBG("hu %p", hu);
@@ -722,7 +725,6 @@ static int bcsp_close(struct hci_uart *hu)
 	skb_queue_purge(&bcsp->unack);
 	skb_queue_purge(&bcsp->rel);
 	skb_queue_purge(&bcsp->unrel);
-	del_timer(&bcsp->tbcsp);
 
 	kfree(bcsp);
 	return 0;
diff --git a/drivers/bluetooth/hci_h5.c b/drivers/bluetooth/hci_h5.c
index f6f4974..afd759e 100644
--- a/drivers/bluetooth/hci_h5.c
+++ b/drivers/bluetooth/hci_h5.c
@@ -206,12 +206,12 @@ static int h5_close(struct hci_uart *hu)
 {
 	struct h5 *h5 = hu->priv;
 
+	del_timer_sync(&h5->timer);
+
 	skb_queue_purge(&h5->unack);
 	skb_queue_purge(&h5->rel);
 	skb_queue_purge(&h5->unrel);
 
-	del_timer(&h5->timer);
-
 	kfree(h5);
 
 	return 0;
-- 
1.7.9.5


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

* Re: [PATCH] Bluetooth:  Stop BCSP/H5 timer before cleaning up
  2014-02-18  8:48 [PATCH] Bluetooth: Stop BCSP/H5 timer before cleaning up Michael Knudsen
@ 2014-03-04  5:25 ` Michael Knudsen
  2014-03-04  7:14   ` Johan Hedberg
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Knudsen @ 2014-03-04  5:25 UTC (permalink / raw)
  To: linux-bluetooth

On 02/18/2014 09:48 AM, Michael Knudsen wrote:
> When stopping BCSP/H5, stop the retransmission timer before proceeding
> to clean up packet queues.  The previous code had a race condition where
> the timer could trigger after the packet lists and protocol structure
> had been removed which lead to dereferencing NULL or use-after-free bugs.

No interest?

-m.


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

* Re: [PATCH] Bluetooth:  Stop BCSP/H5 timer before cleaning up
  2014-03-04  5:25 ` Michael Knudsen
@ 2014-03-04  7:14   ` Johan Hedberg
  2014-03-04  8:19     ` Michael Knudsen
  0 siblings, 1 reply; 5+ messages in thread
From: Johan Hedberg @ 2014-03-04  7:14 UTC (permalink / raw)
  To: Michael Knudsen; +Cc: linux-bluetooth

Hi Michael,

On Tue, Mar 04, 2014, Michael Knudsen wrote:
> On 02/18/2014 09:48 AM, Michael Knudsen wrote:
> >When stopping BCSP/H5, stop the retransmission timer before proceeding
> >to clean up packet queues.  The previous code had a race condition where
> >the timer could trigger after the packet lists and protocol structure
> >had been removed which lead to dereferencing NULL or use-after-free bugs.
> 
> No interest?

I was just discussing this yesterday with Marcel (that we seem to have
forgotten about this patch). The only concern is whether it's safe to
sleep in the *_close callbacks (since you use del_timer_sync). Have you
verified that this doesn't cause any issues?

Johan


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

* Re: [PATCH] Bluetooth:  Stop BCSP/H5 timer before cleaning up
  2014-03-04  7:14   ` Johan Hedberg
@ 2014-03-04  8:19     ` Michael Knudsen
  2014-03-04  9:05       ` Johan Hedberg
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Knudsen @ 2014-03-04  8:19 UTC (permalink / raw)
  To: linux-bluetooth

On 03/04/2014 08:14 AM, Johan Hedberg wrote:
>>> When stopping BCSP/H5, stop the retransmission timer before proceeding
>>> to clean up packet queues.  The previous code had a race condition where
>>> the timer could trigger after the packet lists and protocol structure
>>> had been removed which lead to dereferencing NULL or use-after-free bugs.
>>
>> No interest?
>
> I was just discussing this yesterday with Marcel (that we seem to have
> forgotten about this patch). The only concern is whether it's safe to
> sleep in the *_close callbacks (since you use del_timer_sync). Have you
> verified that this doesn't cause any issues?

Our internal testing was reliably triggering the crash before and
has been stable since our fix went into local trees.  I expected
sleeping to be fine since the path is in process context, and I
found that slip uses the same approach:

	drivers/net/slip/slip.c:slip_close()

I think I saw other line disciplines that did the same but I don't
recall which ones.

Btw. if this is committed, there is a small typo in the message I
used:  Instead of ``lead'' it should be ``led''.

-m.

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

* Re: [PATCH] Bluetooth:  Stop BCSP/H5 timer before cleaning up
  2014-03-04  8:19     ` Michael Knudsen
@ 2014-03-04  9:05       ` Johan Hedberg
  0 siblings, 0 replies; 5+ messages in thread
From: Johan Hedberg @ 2014-03-04  9:05 UTC (permalink / raw)
  To: Michael Knudsen; +Cc: linux-bluetooth

Hi Michael,

On Tue, Mar 04, 2014, Michael Knudsen wrote:
> On 03/04/2014 08:14 AM, Johan Hedberg wrote:
> >>>When stopping BCSP/H5, stop the retransmission timer before proceeding
> >>>to clean up packet queues.  The previous code had a race condition where
> >>>the timer could trigger after the packet lists and protocol structure
> >>>had been removed which lead to dereferencing NULL or use-after-free bugs.
> >>
> >>No interest?
> >
> >I was just discussing this yesterday with Marcel (that we seem to have
> >forgotten about this patch). The only concern is whether it's safe to
> >sleep in the *_close callbacks (since you use del_timer_sync). Have you
> >verified that this doesn't cause any issues?
> 
> Our internal testing was reliably triggering the crash before and
> has been stable since our fix went into local trees.  I expected
> sleeping to be fine since the path is in process context, and I
> found that slip uses the same approach:
> 
> 	drivers/net/slip/slip.c:slip_close()
> 
> I think I saw other line disciplines that did the same but I don't
> recall which ones.
> 
> Btw. if this is committed, there is a small typo in the message I
> used:  Instead of ``lead'' it should be ``led''.

Thanks for the confirmation. I've now pushed the patch to the
bluetooth-next tree (with the typo fixed too).

Johan

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

end of thread, other threads:[~2014-03-04  9:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-18  8:48 [PATCH] Bluetooth: Stop BCSP/H5 timer before cleaning up Michael Knudsen
2014-03-04  5:25 ` Michael Knudsen
2014-03-04  7:14   ` Johan Hedberg
2014-03-04  8:19     ` Michael Knudsen
2014-03-04  9:05       ` Johan Hedberg

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).