* [PATCH v4] Bluetooth: hci_serdev: Unregister HCI before freeing the TX buffer
@ 2026-11-26 18:59 Michael McAllister
2026-11-26 22:03 ` [v4] " bluez.test.bot
0 siblings, 1 reply; 2+ messages in thread
From: Michael McAllister @ 2026-11-26 18:59 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, phone-devel, brgl, zhaojinming,
stable, me
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.
Flush controller TX after stopping UART work, before protocol and port
close, so previously accepted bytes are discarded rather than drained.
Fixes: 21139b3ac62a ("Bluetooth: hci_serdev: Fix use-after-free in hci_uart_unregister_device()")
Cc: stable@vger.kernel.org
Signed-off-by: Michael McAllister <me@michaelmcallister.org>
---
Split out from the WCN3950 series, as Bartosz requested. Rebased onto
f57f78086ab5, including Zhao's flush and close fixes. Keep the unregister
guard in the HCI flush callback so close still balances disabling and
enabling write_work if unregister starts while close is in progress.
The standalone fix now flushes controller TX itself, before protocol and
port close; that flush was previously part of the dependent QCA patch.
Previous version: https://lore.kernel.org/179484638195.54009.16928291772826938293.wcn3950-poweroff-v3-1@michaelmcallister.org/
drivers/bluetooth/hci_serdev.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c
index 9f038ab65..dfa921afc 100644
--- a/drivers/bluetooth/hci_serdev.c
+++ b/drivers/bluetooth/hci_serdev.c
@@ -124,6 +124,9 @@ static void __hci_uart_flush(struct hci_dev *hdev, bool resume_tx)
static int hci_uart_flush(struct hci_dev *hdev)
{
+ if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
+ return 0;
+
__hci_uart_flush(hdev, true);
return 0;
@@ -162,6 +165,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;
@@ -431,6 +437,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
@@ -452,16 +462,16 @@ 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);
+ if (proto_ready)
+ serdev_device_write_flush(hu->serdev);
/* Close the protocol before freeing hdev (intrinsically purges queues).
* Some protocol close handlers (e.g. qca_close) may still access the
base-commit: f57f78086ab54f3b3122fbaeba2c842ba7bb6d00
--
2.39.5
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 22:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 18:59 [PATCH v4] Bluetooth: hci_serdev: Unregister HCI before freeing the TX buffer Michael McAllister
2026-11-26 22:03 ` [v4] " bluez.test.bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox