Skip to contentSkip to conversation

Bluetooth: hci_sock: Serialize dead-device detachment

PATCH Applied Chengfeng Ye 3 participants 3 messages Oct 4 – Oct 6

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

PATCH 1/1 Bluetooth: hci_sock: Serialize dead-device detachment

Rebinding an HCI socket after its controller is unregistered can race monitor control replay and crash the kernel. hci_sock_bind() clears hci_pi(sk)->hdev and drops the device reference under only the socket lock, whereas send_monitor_control_replay() holds hci_sk_list.lock for reading.

Replay can observe a non-NULL hdev, then bind can clear it and drop the last reference before create_monitor_ctrl_open() reads hdev->id. This can cause a NULL pointer dereference if the pointer is reloaded, or a use-after-free if the old pointer is retained.

The kernel reported:

  KASAN: null-ptr-deref in range [0x0000000000000068-0x000000000000006f]
  RIP: 0010:create_monitor_ctrl_open+0x579/0x800
  Call Trace:
   hci_sock_bind+0xd96/0x1190
   __sys_bind+0x166/0x200
   __x64_sys_bind+0x6d/0xb0
   do_syscall_64+0xdd/0x4a0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

The same missing serialization lets hci_send_to_sock() select a socket for the old device and queue its frame after bind has detached the socket or installed a new binding.

Take hci_sk_list.lock for writing when clearing the device pointer and resetting the socket state. This waits for existing replay and delivery readers to finish before detachment and prevents later readers from using the old binding. Keep hci_dev_put() outside the critical section because the final device release can sleep.

Fixes e04480920d1e ("Bluetooth: defer cleanup of resources in hci_unregister_dev()")
Assisted-by GPT-6 Astra
---
 net/bluetooth/hci_sock.c | 2 ++
 1 file changed, 2 insertions(+)
net/bluetooth/hci_sock.c +2 -0
index 6d56c77741e1..81068b585764 100644
@@ -1223,8 +1223,10 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr_unsized *addr,
 	 */
 	hdev = hci_pi(sk)->hdev;
 	if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
+		write_lock(&hci_sk_list.lock);
 		hci_pi(sk)->hdev = NULL;
 		sk->sk_state = BT_OPEN;
+		write_unlock(&hci_sk_list.lock);
 		hci_dev_put(hdev);
 	}
 	hdev = NULL;
Signature · 1 line
2.43.0
#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/1179168/

---Test result---

Test Summary:

CheckPatch                    PASS      0.53 seconds
VerifyFixes                   PASS      0.11 seconds
VerifySignedoff               PASS      0.11 seconds
GitLint                       PASS      0.47 seconds
SubjectPrefix                 PASS      0.10 seconds
BuildKernel                   PASS      17.93 seconds
CheckAllWarning               PASS      21.07 seconds
CheckSparse                   PASS      22.36 seconds
BuildKernel32                 PASS      18.78 seconds
CheckKernelLLVM               PASS      20.96 seconds
TestRunnerSetup               PASS      461.36 seconds
TestRunner_l2cap-tester       PASS      12.94 seconds
TestRunner_iso-tester         PASS      8.82 seconds
TestRunner_bnep-tester        PASS      2.76 seconds
TestRunner_mgmt-tester        PASS      54.49 seconds
TestRunner_rfcomm-tester      PASS      3.97 seconds
TestRunner_sco-tester         PASS      4.95 seconds
TestRunner_ioctl-tester       PASS      4.09 seconds
TestRunner_mesh-tester        FAIL      7.91 seconds
TestRunner_smp-tester         PASS      3.91 seconds
TestRunner_userchan-tester    PASS      2.80 seconds
TestRunner_6lowpan-tester     PASS      3.96 seconds
IncrementalBuild              PASS      18.58 seconds

Details
##############################
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.709 seconds
Mesh - Send cancel - 2                               Timed out    2.000 seconds

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

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Chengfeng Ye
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 6 lines · On Mon, 5 Oct 2026 00:24:42 +0800 you wrote:

Rebinding an HCI socket after its controller is unregistered can race monitor control replay and crash the kernel. hci_sock_bind() clears hci_pi(sk)->hdev and drops the device reference under only the socket lock, whereas send_monitor_control_replay() holds hci_sk_list.lock for reading.

Quoted 6 lines

Replay can observe a non-NULL hdev, then bind can clear it and drop the last reference before create_monitor_ctrl_open() reads hdev->id. This can cause a NULL pointer dereference if the pointer is reloaded, or a use-after-free if the old pointer is retained.

[...]

Here is the summary with links:
- Bluetooth: hci_sock: Serialize dead-device detachment

    https://git.kernel.org/bluetooth/bluetooth-next/c/381a0925c556

You are awesome, thank you!

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