Subject: [PATCH 4.19 19/58] Bluetooth: fix the erroneous flush_work() order

From: Greg Kroah-Hartman <[email-masked]>
Date: Tue, 8 Jun 2021 20:27:00 +0200

From: Lin Ma <[email-masked]>

commit 6a137caec23aeb9e036cdfd8a46dd8a366460e5d upstream.

In the cleanup routine for failed initialization of HCI device,
the flush_work(&hdev->rx_work) need to be finished before the
flush_work(&hdev->cmd_work). Otherwise, the hci_rx_work() can
possibly invoke new cmd_work and cause a bug, like double free,
in late processings.

This was assigned CVE-2021-3564.

This patch reorder the flush_work() to fix this bug.

Cc: Marcel Holtmann <[email-masked]>
Cc: Johan Hedberg <[email-masked]>
Cc: Luiz Augusto von Dentz <[email-masked]>
Cc: "David S. Miller" <[email-masked]>
Cc: Jakub Kicinski <[email-masked]>
Cc: [email-masked]
Cc: [email-masked]
Cc: [email-masked]
Signed-off-by: Lin Ma <[email-masked]>
Signed-off-by: Hao Xiong <[email-masked]>
Cc: stable <[email-masked]>
Signed-off-by: Marcel Holtmann <[email-masked]>
Signed-off-by: Greg Kroah-Hartman <[email-masked]>
---
 net/bluetooth/hci_core.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -1498,8 +1498,13 @@ static int hci_dev_do_open(struct hci_de
 	} else {
 		/* Init failed, cleanup */
 		flush_work(&hdev->tx_work);
-		flush_work(&hdev->cmd_work);
+
+		/* Since hci_rx_work() is possible to awake new cmd_work
+		 * it should be flushed first to avoid unexpected call of
+		 * hci_cmd_work()
+		 */
 		flush_work(&hdev->rx_work);
+		flush_work(&hdev->cmd_work);
 
 		skb_queue_purge(&hdev->cmd_q);
 		skb_queue_purge(&hdev->rx_q);