When hci_cancel_connect_sync() dequeues a create connection command, hci_abort_conn() returns without queuing abort_conn_sync(). Nothing else cleans up the hci_conn, so it is left behind with abort_reason set and can no longer be aborted. If an LE connection made through L2CAP is left like this, the next create command for it connects in the controller, but hci_conn_set_handle() refuses the handle because abort_reason is set. The hci_conn then stays in BT_CONNECT and blocks all other LE connection attempts until the adapter is powered off. Commit 881559af5f5c ("Bluetooth: hci_sync: Attempt to dequeue connection attempt") added the early return when only BT_OPEN connections were dequeued. Commit 12917f591cea ("Bluetooth: hci_conn: Fix null ptr deref in hci_abort_conn()") dropped that state check, so LE connections in BT_CONNECT hit it too. Always queue abort_conn_sync(). If nothing reached the controller (BT_OPEN, or LE still scanning), it sends no HCI command and just fails the connection. hci_conn_timeout() calls hci_abort_conn() without hdev->lock, so a new connect() can still reuse the hci_conn and queue a create command ahead of abort_conn_sync(). If the peer accepts, the handle is refused and nothing disconnects the link. Make the ACL and LE create callbacks return -ECANCELED when abort_reason is set. It is set before the dequeue takes cmd_sync_work_lock, so a create command queued after that always sees it. Tested on bluetooth.git (0b70f26a31f3) with an RTL8761BU in a KVM guest: the hci_conn was left behind in all 6 LE and 3 ACL rounds without this patch, and in none of 42 LE and 5 ACL rounds with it. With an emulated controller and a test-only sleep after the dequeue, connecting again during the sleep left a link in all 10 rounds without the abort_reason checks, and in none of 10 with them. 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()") Cc: stable@vger.kernel.org Signed-off-by: Enjan Chou --- v2: - Return -ECANCELED from hci_acl_create_conn_sync() and hci_le_create_conn_sync() when abort_reason is set, to close the window Sashiko found between the dequeue and queuing abort_conn_sync(). Describe and test that window. - Shorten the commit message and move the test details below. - Rebase on bluetooth.git master (0b70f26a31f3). v1: https://lore.kernel.org/linux-bluetooth/20261126234701.215190-1-zhouer@gmail.com/ 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 v1 of 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. A create callback that has passed the abort_reason check but has not set HCI_CONN_CREATE yet when hci_abort_conn() runs can still leave such a link. That window was there before this patch and I did not change it. Test details: - RTL8761BU, passed through to the 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. The LE create command was dequeued in every round, with or without discovery running, and LE connections kept working with the patch. For ACL, a second BR/EDR connect queued behind the page is closed. - Emulated controller (VHCI): the sleep was one second, between the dequeue and queuing abort_conn_sync(), with 5 ACL and 5 LE rounds per kernel. For LE, connect() can only reuse the hci_conn in that window while HCI_ADVERTISING is set. - KASAN, lockdep, DEBUG_LIST and DEBUG_OBJECTS reported nothing in any of these tests. 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 +++++---- net/bluetooth/hci_sync.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index e86b7a310a..3b172e8f13 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -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 diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c index 23e151cbd9..184549764b 100644 --- a/net/bluetooth/hci_sync.c +++ b/net/bluetooth/hci_sync.c @@ -7368,7 +7368,10 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data) int err; struct hci_conn *conn = data; - if (!hci_conn_valid(hdev, conn)) + /* Nothing to do if the connection is gone or is being aborted. In the + * latter case abort_conn_sync() takes care of cleaning it up. + */ + if (!hci_conn_valid(hdev, conn) || conn->abort_reason) return -ECANCELED; bt_dev_dbg(hdev, "conn %p", conn); @@ -7712,7 +7715,10 @@ static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data) struct hci_cp_create_conn cp; int err; - if (!hci_conn_valid(hdev, conn)) + /* Nothing to do if the connection is gone or is being aborted. In the + * latter case abort_conn_sync() takes care of cleaning it up. + */ + if (!hci_conn_valid(hdev, conn) || conn->abort_reason) return -ECANCELED; /* Many controllers disallow HCI Create Connection while it is doing -- 2.53.0