Skip to contentSkip to conversation

Bluetooth: hci_serdev: Unregister HCI before freeing the TX buffer

PATCH Applied Michael McAllister 3 participants 3 messages Nov 26 – Nov 30

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

PATCH v4 1/1 Bluetooth: hci_serdev: Unregister HCI before freeing the TX buffer

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()")

---
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(-)
drivers/bluetooth/hci_serdev.c +14 -4
index 9f038ab65..dfa921afc 100644
@@ -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

Signature · 1 line
2.39.5
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1223637/

---Test result---

Test Summary:

CheckPatch                    PASS      0.38 seconds
VerifyFixes                   PASS      0.04 seconds
VerifySignedoff               PASS      0.04 seconds
GitLint                       PASS      0.15 seconds
SubjectPrefix                 PASS      0.04 seconds
BuildKernel                   PASS      20.67 seconds
CheckAllWarning               PASS      23.42 seconds
CheckSparse                   PASS      24.98 seconds
BuildKernel32                 PASS      20.57 seconds
CheckKernelLLVM               PASS      21.97 seconds
TestRunnerSetup               PASS      480.64 seconds
IncrementalBuild              PASS      19.06 seconds

https://github.com/bluez/bluetooth-next/pull/1234

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Michael McAllister
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Thu, 26 Nov 2026 18:59:41 +0000 you wrote:

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.

[...]

Here is the summary with links:
- [v4] Bluetooth: hci_serdev: Unregister HCI before freeing the TX buffer

    https://git.kernel.org/bluetooth/bluetooth-next/c/0b70f26a31f3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html