Skip to contentSkip to conversation

Bluetooth: hci_conn: Fix null ptr deref in hci_abort_conn()

PATCH Applied Siwei Zhang 3 participants 5 messages Jun 15 – Jun 22

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

PATCH v6 1/2 Bluetooth: hci_conn: Fix null ptr deref in hci_abort_conn()

hci_abort_conn() read hci_skb_event(hdev->sent_cmd) when a connection was pending, but hdev->sent_cmd can be NULL while req_status is still HCI_REQ_PEND, leading to a NULL pointer dereference and a general protection fault from the hci_rx_work() receive path.

Instead of inspecting hdev->sent_cmd, track the in-flight create connection command with a new per-connection HCI_CONN_CREATE flag and route all cancellation through hci_cancel_connect_sync(), which dispatches to a dedicated per-type cancel function. The create command is in exactly one of two states: still queued, or in flight. The cancel function holds cmd_sync_work_lock across the whole decision: the worker takes this lock to dequeue every entry, so while it is held a queued command cannot start running and an in-flight command cannot complete and let the next command become pending. This keeps the flag test and hci_cmd_sync_cancel() atomic with respect to the worker, so a queued command is simply dequeued, and an in-flight command owned by this connection is cancelled without the risk of cancelling an unrelated command that became pending in the meantime. CIS uses the same flag mechanism via HCI_CONN_CREATE_CIS but cannot be dequeued per-connection.

hci_acl_create_conn_sync() and hci_le_create_conn_sync() clear HCI_CONN_CREATE after the create command completes, but the command status handler can free conn via hci_conn_del() (for example when the controller rejects the connection) while the worker is still blocked on the connection complete event. Hold a reference on conn across the create command so the flag can be cleared without a use-after-free.

Fixes a13f316e90fd ("Bluetooth: hci_conn: Consolidate code for aborting connections")
Assisted-by Claude:claude-opus-4-8
---
 include/net/bluetooth/hci_core.h |   1 +
 net/bluetooth/hci_conn.c         |  21 +----
 net/bluetooth/hci_sync.c         | 133 +++++++++++++++++++++++++++----
 3 files changed, 123 insertions(+), 32 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
index aa600fbf9a53..aa554c34f9ec 100644
@@ -988,6 +988,7 @@ enum {
 	HCI_CONN_AUTH_FAILURE,
 	HCI_CONN_PER_ADV,
 	HCI_CONN_BIG_CREATED,
+	HCI_CONN_CREATE,
 	HCI_CONN_CREATE_CIS,
 	HCI_CONN_CREATE_BIG_SYNC,
 	HCI_CONN_BIG_SYNC,
net/bluetooth/hci_conn.c +3 -18
index 54eabaa46960..eba4a548bef5 100644
@@ -3181,26 +3181,11 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)
 
 	conn->abort_reason = reason;
 
-	/* If the connection is pending check the command opcode since that
-	 * might be blocking on hci_cmd_sync_work while waiting its respective
-	 * event so we need to hci_cmd_sync_cancel to cancel it.
-	 *
-	 * hci_connect_le serializes the connection attempts so only one
-	 * connection can be in BT_CONNECT at time.
+	/* Cancel the connect attempt. A return of 0 means the create command
+	 * was still queued and got dequeued, so there is nothing to disconnect.
 	 */
-	if (conn->state == BT_CONNECT && READ_ONCE(hdev->req_status) == HCI_REQ_PEND) {
-		switch (hci_skb_event(hdev->sent_cmd)) {
-		case HCI_EV_CONN_COMPLETE:
-		case HCI_EV_LE_CONN_COMPLETE:
-		case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
-		case HCI_EVT_LE_CIS_ESTABLISHED:
-			hci_cmd_sync_cancel(hdev, ECANCELED);
-			break;
-		}
-	/* Cancel connect attempt if still queued/pending */
-	} else if (!hci_cancel_connect_sync(hdev, conn)) {
+	if (!hci_cancel_connect_sync(hdev, conn))
 		return 0;
-	}
 
 	/* Run immediately if on cmd_sync_work since this may be called
 	 * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does
net/bluetooth/hci_sync.c +119 -14

180 lines not on this page · raw message

Signature · 1 line
2.54.0
#1 lore raw

PATCH v6 2/2 Bluetooth: hci_sync: Remove unused hci_cmd_sync_dequeue_once()

hci_cmd_sync_dequeue_once() had a single in-tree caller, hci_cancel_connect_sync(), which now holds cmd_sync_work_lock across the in-flight create flag test and the dequeue and so open-codes the lookup and cancel under that lock. That leaves the exported hci_cmd_sync_dequeue_once() with no in-tree user, so remove it along with its declaration.

---
 include/net/bluetooth/hci_sync.h |  3 ---
 net/bluetooth/hci_sync.c         | 26 --------------------------
 2 files changed, 29 deletions(-)
include/net/bluetooth/hci_sync.h +0 -3
index 73e494b2591d..818e62d9fe9e 100644
@@ -84,9 +84,6 @@ void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
 			       struct hci_cmd_sync_work_entry *entry);
 bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
 			  void *data, hci_cmd_sync_work_destroy_t destroy);
-bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
-			      hci_cmd_sync_work_func_t func, void *data,
-			      hci_cmd_sync_work_destroy_t destroy);
 
 int hci_update_eir_sync(struct hci_dev *hdev);
 int hci_update_class_sync(struct hci_dev *hdev);
net/bluetooth/hci_sync.c +0 -26
index a3df69bdec1e..753198b401ac 100644
@@ -860,32 +860,6 @@ void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
 }
 EXPORT_SYMBOL(hci_cmd_sync_cancel_entry);
 
-/* Dequeue one HCI command entry:
- *
- * - Lookup and cancel first entry that matches.
- */
-bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
-			       hci_cmd_sync_work_func_t func,
-			       void *data, hci_cmd_sync_work_destroy_t destroy)
-{
-	struct hci_cmd_sync_work_entry *entry;
-
-	mutex_lock(&hdev->cmd_sync_work_lock);
-
-	entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
-	if (!entry) {
-		mutex_unlock(&hdev->cmd_sync_work_lock);
-		return false;
-	}
-
-	_hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
-
-	mutex_unlock(&hdev->cmd_sync_work_lock);
-
-	return true;
-}
-EXPORT_SYMBOL(hci_cmd_sync_dequeue_once);
-
 /* Dequeue HCI command entry:
  *
  * - Lookup and cancel any entry that matches by function callback or data or
Signature · 1 line
2.54.0
#2 lore raw
In reply to Siwei Zhang

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=1111791

---Test result---

Test Summary:

CheckPatch                    PASS      1.82 seconds
VerifyFixes                   PASS      0.09 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.47 seconds
SubjectPrefix                 PASS      0.16 seconds
BuildKernel                   PASS      26.58 seconds
CheckAllWarning               PASS      28.89 seconds
CheckSparse                   PASS      27.60 seconds
BuildKernel32                 PASS      25.49 seconds
TestRunnerSetup               PASS      571.42 seconds
TestRunner_l2cap-tester       PASS      58.94 seconds
TestRunner_iso-tester         PASS      96.66 seconds
TestRunner_bnep-tester        PASS      30.75 seconds
TestRunner_mgmt-tester        FAIL      207.20 seconds
TestRunner_rfcomm-tester      PASS      30.92 seconds
TestRunner_sco-tester         PASS      31.50 seconds
TestRunner_ioctl-tester       PASS      26.69 seconds
TestRunner_mesh-tester        FAIL      25.83 seconds
TestRunner_smp-tester         PASS      22.80 seconds
TestRunner_userchan-tester    PASS      19.22 seconds
TestRunner_6lowpan-tester     FAIL      45.81 seconds
IncrementalBuild              PASS      43.45 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.234 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.808 seconds
Mesh - Send cancel - 2                               Timed out    1.989 seconds

##############################
Test: TestRunner_6lowpan-tester - FAIL
Desc: Run 6lowpan-tester with test-runner Output:
Total: 8, Passed: 3 (37.5%), Failed: 5, Not Run: 0

Failed Test Cases

Client Connect - Disconnect                          Timed out    5.480 seconds
Client Recv Dgram - Success                          Timed out    4.991 seconds
Client Recv Raw - Success                            Timed out    4.993 seconds
Client Recv IPHC Dgram - Success                     Timed out    4.999 seconds
Client Recv IPHC Raw - Success                       Timed out    4.993 seconds

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

---
Regards,
Linux Bluetooth

#3 lore raw

Hi Luiz,

Quoted 41 lines · On Mon, Jun 15, 2026, at 1:04 PM, [email protected] wrote:

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=1111791

---Test result---

Test Summary:

CheckPatch                    PASS      1.82 seconds
VerifyFixes                   PASS      0.09 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.47 seconds
SubjectPrefix                 PASS      0.16 seconds
BuildKernel                   PASS      26.58 seconds
CheckAllWarning               PASS      28.89 seconds
CheckSparse                   PASS      27.60 seconds
BuildKernel32                 PASS      25.49 seconds
TestRunnerSetup               PASS      571.42 seconds
TestRunner_l2cap-tester       PASS      58.94 seconds
TestRunner_iso-tester         PASS      96.66 seconds
TestRunner_bnep-tester        PASS      30.75 seconds
TestRunner_mgmt-tester        FAIL      207.20 seconds
TestRunner_rfcomm-tester      PASS      30.92 seconds
TestRunner_sco-tester         PASS      31.50 seconds
TestRunner_ioctl-tester       PASS      26.69 seconds
TestRunner_mesh-tester        FAIL      25.83 seconds
TestRunner_smp-tester         PASS      22.80 seconds
TestRunner_userchan-tester    PASS      19.22 seconds
TestRunner_6lowpan-tester     FAIL      45.81 seconds
IncrementalBuild              PASS      43.45 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

Quoted 2 lines

Failed Test Cases
Read Exp Feature - Success Failed 0.234 seconds

The mgmt.c is not modified in this patch.

Quoted 9 lines

##############################
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.808 seconds
Mesh - Send cancel - 2                               Timed out    1.989 seconds

Also, the mesh path does not allocate hci_conn nor related to the code path changed.

Quoted 6 lines

##############################
Test: TestRunner_6lowpan-tester - FAIL
Desc: Run 6lowpan-tester with test-runner Output:
Total: 8, Passed: 3 (37.5%), Failed: 5, Not Run: 0

Quoted 8 lines

Failed Test Cases

Client Connect - Disconnect                          Timed out    5.480 seconds
Client Recv Dgram - Success                          Timed out    4.991 seconds
Client Recv Raw - Success                            Timed out    4.993 seconds
Client Recv IPHC Dgram - Success                     Timed out    4.999 seconds
Client Recv IPHC Raw - Success                       Timed out    4.993 seconds

Fixed by the recent commit d35e2950daaf8cd362beb4ecd42af2b0d9459136.

Quoted 5 lines

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

---
Regards,
Linux Bluetooth

Could you please review on this patch?

And also this patch I sent on Monday.
https://lore.kernel.org/linux-bluetooth/[email protected]/T/#t

Thanks.

Best,
Siwei

#4 lore raw
In reply to Siwei Zhang
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

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

Quoted 20 lines · On Mon, 15 Jun 2026 11:33:05 -0400 you wrote:

hci_abort_conn() read hci_skb_event(hdev->sent_cmd) when a connection was pending, but hdev->sent_cmd can be NULL while req_status is still HCI_REQ_PEND, leading to a NULL pointer dereference and a general protection fault from the hci_rx_work() receive path.

Instead of inspecting hdev->sent_cmd, track the in-flight create connection command with a new per-connection HCI_CONN_CREATE flag and route all cancellation through hci_cancel_connect_sync(), which dispatches to a dedicated per-type cancel function. The create command is in exactly one of two states: still queued, or in flight. The cancel function holds cmd_sync_work_lock across the whole decision: the worker takes this lock to dequeue every entry, so while it is held a queued command cannot start running and an in-flight command cannot complete and let the next command become pending. This keeps the flag test and hci_cmd_sync_cancel() atomic with respect to the worker, so a queued command is simply dequeued, and an in-flight command owned by this connection is cancelled without the risk of cancelling an unrelated command that became pending in the meantime. CIS uses the same flag mechanism via HCI_CONN_CREATE_CIS but cannot be dequeued per-connection.

Quoted 1 line

[...]

Here is the summary with links:
- [v6,1/2] Bluetooth: hci_conn: Fix null ptr deref in hci_abort_conn()

    https://git.kernel.org/bluetooth/bluetooth-next/c/76c2d047410b
  - [v6,2/2] Bluetooth: hci_sync: Remove unused hci_cmd_sync_dequeue_once()
    https://git.kernel.org/bluetooth/bluetooth-next/c/8047d832767f

You are awesome, thank you!

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