Skip to contentSkip to conversation

Bluetooth: fix memory leak in error path of hci_alloc_dev()

PATCH Applied Bharath Reddy 3 participants 3 messages Jun 1

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

PATCH v5 1/1 Bluetooth: fix memory leak in error path of hci_alloc_dev()

Early failures in Bluetooth HCI UART configuration leak SRCU percpu memory.

When device initialization fails before hci_register_dev() completes, the HCI_UNREGISTER flag is never set. As a result, when the device reference count reaches zero, bt_host_release() evaluates this flag as false and falls back to a direct kfree(hdev).

Because hci_release_dev() is bypassed, the SRCU struct initialized early in hci_alloc_dev() is never cleaned up, resulting in a leak of percpu memory.

Fix the leak by explicitly calling cleanup_srcu_struct() in the fallback (unregistered) branch of bt_host_release() before freeing the device.

Reported-by [email protected]
Fixes 1d6123102e9f ("Bluetooth: hci_core: Fix use-after-free in vhci_flush()")

---
Changes in v5:
- Removed trailing whitespace in the version history to fix GitLint error

Changes in v4:
- Included patch version history

Changes in v3:
- Added missing curly braces to the if/else block in bt_host_release() to resolve a checkpatch.pl warning
- Added Fixes and Closes tags

Changes in v2:
- Fixed a NULL pointer dereference caused by v1
- Moved cleanup_srcu_struct() to the fallback (unregistered) branch of bt_host_release() instead of unconditionally calling hci_release_dev()

Changes in v1:
- Initial patch (failed because it bypassed the HCI_UNREGISTER check)

 net/bluetooth/hci_sysfs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/bluetooth/hci_sysfs.c +4 -2
index 041ce9adc378..8957ce7c21b7 100644
@@ -83,10 +83,12 @@ static void bt_host_release(struct device *dev)
 {
 	struct hci_dev *hdev = to_hci_dev(dev);
 
-	if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
+	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
 		hci_release_dev(hdev);
-	else
+	} else {
+		cleanup_srcu_struct(&hdev->srcu);
 		kfree(hdev);
+	}
 	module_put(THIS_MODULE);
 }
 
Signature · 1 line
2.34.1
#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/project/bluetooth/list/?series=1103734

---Test result---

Test Summary:

CheckPatch                    PASS      0.59 seconds
VerifyFixes                   PASS      0.09 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.26 seconds
SubjectPrefix                 PASS      0.08 seconds
BuildKernel                   PASS      26.23 seconds
CheckAllWarning               PASS      29.19 seconds
CheckSparse                   PASS      27.87 seconds
BuildKernel32                 PASS      25.89 seconds
TestRunnerSetup               PASS      575.48 seconds
TestRunner_l2cap-tester       PASS      62.40 seconds
TestRunner_iso-tester         PASS      86.46 seconds
TestRunner_bnep-tester        PASS      20.30 seconds
TestRunner_mgmt-tester        FAIL      219.12 seconds
TestRunner_rfcomm-tester      PASS      25.38 seconds
TestRunner_sco-tester         PASS      32.48 seconds
TestRunner_ioctl-tester       PASS      26.31 seconds
TestRunner_mesh-tester        FAIL      25.89 seconds
TestRunner_smp-tester         PASS      23.69 seconds
TestRunner_userchan-tester    PASS      21.00 seconds
TestRunner_6lowpan-tester     PASS      23.35 seconds
IncrementalBuild              PASS      24.81 seconds

Details
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4

Failed Test Cases
Read Exp Feature - Success Failed 0.260 seconds
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0

Failed Test Cases

Mesh - Send cancel - 1                               Timed out    2.005 seconds
Mesh - Send cancel - 2                               Timed out    1.989 seconds

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

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Bharath Reddy
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 9 lines · On Mon, 1 Jun 2026 08:54:26 +0530 you wrote:

Early failures in Bluetooth HCI UART configuration leak SRCU percpu memory.

When device initialization fails before hci_register_dev() completes, the HCI_UNREGISTER flag is never set. As a result, when the device reference count reaches zero, bt_host_release() evaluates this flag as false and falls back to a direct kfree(hdev).

[...]

Here is the summary with links:
- [v5] Bluetooth: fix memory leak in error path of hci_alloc_dev()

    https://git.kernel.org/bluetooth/bluetooth-next/c/cf767a2d88f7

You are awesome, thank you!

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