* [PATCH] Bluetooth: hci_conn: Don't leak hci_conn when dequeuing its create command
@ 2026-11-26 23:47 Enjan Chou
0 siblings, 0 replies; only message in thread
From: Enjan Chou @ 2026-11-26 23:47 UTC (permalink / raw)
To: linux-bluetooth
Cc: Marcel Holtmann, Luiz Augusto von Dentz, Siwei Zhang,
linux-kernel, Enjan Chou, stable
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()")
Cc: stable@vger.kernel.org
Signed-off-by: Enjan Chou <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 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(-)
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
--
2.53.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-26 23:48 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 23:47 [PATCH] Bluetooth: hci_conn: Don't leak hci_conn when dequeuing its create command Enjan Chou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox