Skip to contentSkip to conversation

Clear workqueue to avoid use-after-free

PATCH Superseded Zhengping Jiang 1 participant 3 messages Feb 22, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Clear workqueue to avoid use-after-free

After the hci_sync rework, cmd_sync_work was cleared when calling hci_unregister_dev, but not when powering off the adapter. Use-after-free errors happened because a work is still scheduled when cmd is freed by __mgmt_power_off.

Changes in v1:
- Clear cmd_sync_work queue before clearing the mgmt cmd list

Zhengping Jiang (1):
Bluetooth: hci_sync: clear workqueue before clear mgmt cmd

 net/bluetooth/hci_sync.c | 2 ++
 1 file changed, 2 insertions(+)
Signature · 1 line
2.39.2.637.g21b0678d19-goog
#1 lore raw

PATCH 1/1 Bluetooth: hci_sync: clear workqueue before clear mgmt cmd

Clear cmd_sync_work queue before clearing the mgmt cmd list to avoid racing conditions which cause use-after-free.

When powering off the adapter, the mgmt cmd list will be cleared. If a work is queued in the cmd_sync_work queue at the same time, it will cause the risk of use-after-free, as the cmd pointer is not checked before use.

---

Changes in v1:
- Clear cmd_sync_work queue before clearing the mgmt cmd list

 net/bluetooth/hci_sync.c | 2 ++
 1 file changed, 2 insertions(+)
net/bluetooth/hci_sync.c +2 -0
index 117eedb6f709..6609434e3125 100644
@@ -4840,6 +4840,8 @@ int hci_dev_close_sync(struct hci_dev *hdev)
 
 	auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
 
+	hci_cmd_sync_clear(hdev);
+
 	if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
 	    hci_dev_test_flag(hdev, HCI_MGMT))
Signature · 1 line
2.39.2.637.g21b0678d19-goog
#2 lore raw

Re: [kernel PATCH v1 1/1] Bluetooth: hci_sync: clear workqueue before clear mgmt cmd

Hi,

We need to revise this patch after a local test failure. I will update after finding the cause.

Thanks,
Zhengping

Quoted 34 lines · Zhengping Jiang

Clear cmd_sync_work queue before clearing the mgmt cmd list to avoid racing conditions which cause use-after-free.

When powering off the adapter, the mgmt cmd list will be cleared. If a work is queued in the cmd_sync_work queue at the same time, it will cause the risk of use-after-free, as the cmd pointer is not checked before use.

---

Changes in v1:
- Clear cmd_sync_work queue before clearing the mgmt cmd list

 net/bluetooth/hci_sync.c | 2 ++
 1 file changed, 2 insertions(+)
net/bluetooth/hci_sync.c +2 -1
index 117eedb6f709..6609434e3125 100644
@@ -4840,6 +4840,8 @@ int hci_dev_close_sync(struct hci_dev *hdev)
 
        auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
 
+       hci_cmd_sync_clear(hdev);
+
        if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
            !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
            hci_dev_test_flag(hdev, HCI_MGMT))
--

2.39.2.637.g21b0678d19-goog