[PATCH v3 1/5] Bluetooth: hci_serdev: Unregister HCI before freeing the TX buffer

From: Michael McAllister

Date: Mon Nov 16 2026 - 11:26:42 EST


Unregister frees hu->tx_skb before hci_unregister_dev() waits for an
in-flight HCI close or reset. That path can still flush the same frame,
causing a use-after-free or double free.

Unregister HCI first, before clearing protocol readiness and cancelling
write_work. Skip the HCI flush and close callbacks during unregister;
the transport teardown frees the frame and closes the protocol and port
once setup and HCI close have finished.

Fixes: 21139b3ac62a ("Bluetooth: hci_serdev: Fix use-after-free in hci_uart_unregister_device()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Michael McAllister <me@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/bluetooth/hci_serdev.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c
index d2c48df34..e0e969228 100644
--- a/drivers/bluetooth/hci_serdev.c
+++ b/drivers/bluetooth/hci_serdev.c
@@ -97,6 +97,9 @@ static int hci_uart_flush(struct hci_dev *hdev)

BT_DBG("hdev %p serdev %p", hdev, hu->serdev);

+ if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
+ return 0;
+
if (hu->tx_skb) {
kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
}
@@ -144,6 +147,9 @@ static int hci_uart_close(struct hci_dev *hdev)

BT_DBG("hdev %p", hdev);

+ if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
+ return 0;
+
if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
return 0;

@@ -402,6 +408,10 @@ void hci_uart_unregister_device(struct hci_uart *hu)
/* Wait for init_ready to finish to prevent registration races */
cancel_work_sync(&hu->init_ready);

+ /* Drain HCI setup before clearing protocol readiness. */
+ if (test_bit(HCI_UART_REGISTERED, &hu->flags))
+ hci_unregister_dev(hdev);
+
proto_ready = test_bit(HCI_UART_PROTO_READY, &hu->flags);
if (proto_ready) {
/* Clear HCI_UART_PROTO_READY under the write lock so a
@@ -423,17 +433,14 @@ void hci_uart_unregister_device(struct hci_uart *hu)
cancel_work_sync(&hu->write_work);

/* Free any partially transmitted frame left over by write_work now
- * that the transmit path is fully quiesced. hci_uart_close() would
- * skip hci_uart_flush() because HCI_UART_PROTO_READY is cleared.
+ * that the transmit path is fully quiesced. The HCI close and flush
+ * callbacks leave transport teardown to us during unregister.
*/
if (hu->tx_skb) {
kfree_skb(hu->tx_skb);
hu->tx_skb = NULL;
}

- if (test_bit(HCI_UART_REGISTERED, &hu->flags))
- hci_unregister_dev(hdev);
-
/* Close the protocol before freeing hdev (intrinsically purges queues).
* Some protocol close handlers (e.g. qca_close) may still access the
* serdev device, so keep the serdev port open until this completes.
--
2.39.5