Skip to contentSkip to conversation

Bluetooth: hci_conn: Don't leak hci_conn when dequeuing its create command

PATCH In review Enjan Chou 2 participants 2 messages Nov 26 – Nov 27

Subsystems (3)
lore
Share & tools
lore

Enjan Chou

author
#0 lore raw

PATCH 1/1 Bluetooth: hci_conn: Don't leak hci_conn when dequeuing its create command

hci_abort_conn() returns without queuing abort_conn_sync() when hci_cancel_connect_sync() dequeues a create connection command that has not started yet. Nothing else fails the connection in that case: the destroy callback of the dequeued command only drops a reference, so the hci_conn stays in the connection hash with abort_reason set, which makes every later hci_abort_conn() on it return early too. For LE, its connection parameters also stay on pend_le_conns.

For an LE connection made through L2CAP (BT_CONNECT, still scanning) this leaves the adapter unable to make LE connections until it is powered off. A new connect to the same address reuses the stale hci_conn and never completes. Once the peer's advertising is reported again, check_pending_le_conn() queues the create command again, the controller connects, and le_conn_complete_evt() bails out because hci_conn_set_handle() refuses a connection with abort_reason set. The hci_conn then stays in BT_CONNECT, so hci_lookup_le_connect() keeps returning it and connection attempts to any other LE device never
start: hci_connect_le() returns -EBUSY, which check_pending_le_conn() ignores, and hci_update_passive_scan_sync() does not start the background scan they wait for. A dequeued BT_OPEN (ACL) connection is likewise left behind.

The early return was added by commit 881559af5f5c ("Bluetooth: hci_sync:
Attempt to dequeue connection attempt"), when hci_cancel_connect_sync() only tried to dequeue BT_OPEN connections. Commit 12917f591cea ("Bluetooth: hci_conn: Fix null ptr deref in hci_abort_conn()") dropped that state check, so LE connections in BT_CONNECT started to hit it.

Queue abort_conn_sync() whether or not the create command was dequeued. hci_abort_conn_sync() sends no HCI command for a connection that never reached the controller (BT_OPEN, or LE still scanning) and just calls hci_conn_failed(). If an advertising report queues the create command again in the meantime, that entry runs after abort_conn_sync() and fails hci_conn_valid().

Reproduced on bluetooth-next (ffb95458a0f7) with an RTL8761BU USB controller passed through to a KVM guest, without bluetoothd: an L2CAP ATT socket connects to an advertising LE peer while a BR/EDR connect to a non-existent address keeps the cmd_sync worker busy paging, and the LE socket is closed before the page times out. Without this patch every round that dequeued the create command (11 of 11) left the stale hci_conn, with or without discovery running; with it none of 60 rounds did (50 of them dequeued it) and LE connections kept working. KASAN, lockdep, DEBUG_LIST and DEBUG_OBJECTS reported nothing.

An AI coding assistant (Claude) helped find and analyse the problem, write the reproducer, and draft this patch and message.

Fixes 881559af5f5c ("Bluetooth: hci_sync: Attempt to dequeue connection attempt")
Fixes 12917f591cea ("Bluetooth: hci_conn: Fix null ptr deref in hci_abort_conn()")

---
Found on a desktop running Ubuntu's 7.0.0-44 kernel, which carries 12917f591cea: after repeated failed connection attempts to a BLE device the adapter could not make any LE connection (noticed when a BLE scale stopped connecting) until it was powered off. What kept the cmd_sync worker busy at that moment was not captured; the reproducer uses a page instead. The same reproducer also leaves the stale hci_conn on that kernel, and does not with this patch.

hci_cancel_connect_sync()'s return value is now unused (hci_abort_conn() is its only caller). I left it as is to keep the fix small for stable.

Reproducer:

# Reproducer for hci_abort_conn() leaking an hci_conn whose create command

  # was dequeued.  Python >= 3.14 (LE L2CAP addresses), run as a normal user.
  # Needs an advertising LE peer; bluetoothd and discovery are not required.
  import socket, subprocess, time

PEER = "E1:E9:83:B4:21:65" # advertising LE peer (random address)
NOBODY = "02:00:00:00:00:01" # BR/EDR address that does not exist

le = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET,
socket.BTPROTO_L2CAP)
le.bind(("00:00:00:00:00:00", 0, 4, socket.BDADDR_LE_PUBLIC)) le.setblocking(False)
page = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET,
socket.BTPROTO_L2CAP)
page.setblocking(False)

le.connect_ex((PEER, 0, 4, socket.BDADDR_LE_RANDOM))

  page.connect_ex((NOBODY, 1))    # pages ~5.12 s, keeping cmd_sync busy
  time.sleep(3.5)                 # peer's advertising queues the LE create
  le.close()                      # hci_abort_conn() dequeues it

time.sleep(3)
page.close()
time.sleep(30)
# Without the fix the LE connection to PEER is still listed (state 5). print(subprocess.run(["hcitool", "con"], capture_output=True,
text=True).stdout)

 net/bluetooth/hci_conn.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
net/bluetooth/hci_conn.c +5 -4
index e86b7a310a..3b172e8f13 100644
@@ -3311,11 +3311,12 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)
 
 	conn->abort_reason = reason;
 
-	/* Cancel the connect attempt. A return of 0 means the create command
-	 * was still queued and got dequeued, so there is nothing to disconnect.
+	/* Cancel the connect attempt. If the create command was still queued
+	 * and got dequeued, nothing reached the controller, but the connection
+	 * still has to be failed: abort_conn_sync() does that without sending
+	 * any HCI command.
 	 */
-	if (!hci_cancel_connect_sync(hdev, conn))
-		return 0;
+	hci_cancel_connect_sync(hdev, conn);
 
 	/* Run immediately if on cmd_sync_work since this may be called
 	 * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does
Signature · 1 line
2.53.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/1223800/

---Test result---

Test Summary:

CheckPatch                    PASS      0.52 seconds
VerifyFixes                   PASS      0.10 seconds
VerifySignedoff               PASS      0.10 seconds
GitLint                       PASS      0.24 seconds
SubjectPrefix                 PASS      0.09 seconds
BuildKernel                   PASS      18.33 seconds
CheckAllWarning               PASS      20.81 seconds
CheckSparse                   PASS      22.02 seconds
BuildKernel32                 PASS      18.54 seconds
CheckKernelLLVM               PASS      18.27 seconds
TestRunnerSetup               PASS      406.22 seconds
TestRunner_l2cap-tester       PASS      14.45 seconds
TestRunner_iso-tester         PASS      10.20 seconds
TestRunner_bnep-tester        PASS      2.73 seconds
TestRunner_mgmt-tester        PASS      81.95 seconds
TestRunner_rfcomm-tester      PASS      3.95 seconds
TestRunner_sco-tester         PASS      4.92 seconds
TestRunner_ioctl-tester       PASS      4.01 seconds
TestRunner_mesh-tester        PASS      3.97 seconds
TestRunner_smp-tester         PASS      3.89 seconds
TestRunner_userchan-tester    PASS      2.79 seconds
TestRunner_6lowpan-tester     PASS      3.94 seconds
IncrementalBuild              PASS      17.15 seconds

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

---
Regards,
Linux Bluetooth