Skip to contentSkip to conversation

Bluetooth: Fix Use-After-Free in hci_unregister_dev

PATCH In review Jordan Walters 3 participants 3 messages Jun 3 – Jun 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Bluetooth: Fix Use-After-Free in hci_unregister_dev

The hci_unregister_dev() function fails to safely cancel the cmd_timer and ncmd_timer before freeing the hci_dev structure. If an asynchronous event or timeout occurs during device teardown, the timer callbacks may execute after the device has been freed, leading to a KASAN slab-use-after-free panic.

This patch adds cancel_delayed_work_sync() calls at the end of hci_unregister_dev() after all teardown procedures have completed. This ensures the timers are fully flushed before the struct is freed, preventing any use-after-free conditions.

---
 net/bluetooth/hci_core.c | 3 +++
 1 file changed, 3 insertions(+)
net/bluetooth/hci_core.c +4 -0
index 28d7929dc59..3db1b3738b5 100644
@@ -2698,6 +2698,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
 		rfkill_unregister(hdev->rfkill);
 		rfkill_destroy(hdev->rfkill);
 	}
 
 	device_del(&hdev->dev);
+
+	cancel_delayed_work_sync(&hdev->cmd_timer);
+	cancel_delayed_work_sync(&hdev->ncmd_timer);
+

/* Actual cleanup is deferred until hci_release_dev(). */ hci_dev_put(hdev);
}

#1 lore raw

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: corrupt patch at line 23
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Jordan Walters

Hi Jordan,

Quoted 24 lines · Jordan Walters

The hci_unregister_dev() function fails to safely cancel the cmd_timer and ncmd_timer before freeing the hci_dev structure. If an asynchronous event or timeout occurs during device teardown, the timer callbacks may execute after the device has been freed, leading to a KASAN slab-use-after-free panic.

This patch adds cancel_delayed_work_sync() calls at the end of hci_unregister_dev() after all teardown procedures have completed. This ensures the timers are fully flushed before the struct is freed, preventing any use-after-free conditions.

---
 net/bluetooth/hci_core.c | 3 +++
 1 file changed, 3 insertions(+)
net/bluetooth/hci_core.c +0 -0
index 28d7929dc59..3db1b3738b5 100644
@@ -2698,6 +2698,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
                rfkill_unregister(hdev->rfkill);
                rfkill_destroy(hdev->rfkill);

net/bluetooth/hci_core.c void hci_unregister_dev(struct hci_dev *hdev)

Quoted 10 lines
net/bluetooth/hci_core.c +4 -0
        }
 
        device_del(&hdev->dev);
+
+       cancel_delayed_work_sync(&hdev->cmd_timer);
+       cancel_delayed_work_sync(&hdev->ncmd_timer);
+
        /* Actual cleanup is deferred until hci_release_dev(). */
        hci_dev_put(hdev);
 }

I'm not sure what's happening, but I cannot apply this change with git am. Also, why did you revert to using cancel instead of disable?

Signature · 1 line
Luiz Augusto von Dentz