From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.217.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796108794; b=5bHaBiiEZ3P24gkE1yRl8bh0VK+j2V9BOYTttP1jH6UdNiLvELpBfqOXRo2gQNTCjTNS gtwWgvvDQ5ERXJkrJMQeRZxTcMbJ+JvFnZXrWP6nQ5JxE+FFFOKnAj1xCYflgpDg0M7SD pMOKtIZ0h6TfhA6MI6cVNl/mxzxeDw/X5p/TdF0V1LUZMk5lrcc3xdD8qyvZczleI884B BhUzw4AkWj5UDM4+P64GF75tIQmt3S6LxE0Y2QEJ1KjYcala+fjhnH9qnXlAcrUyiV00R r1hzPgJnDVSypTBHM1K3q7Q4dFJQt+PXUaryeqCkAWLsSN9UTSRsrP+smXGuibBUKqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796108794; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=MBtQ6yWkR1apqck8cVJk/uTLKAt5sJ62L9jMNGTAiAI=; b=sHBxvlXtt0EAVOSDR+shhW2GEpGaGbewBWphYY0zgWN0piYMzUH1vcHFepHhS+KFZxaa iJQ3DLTFL4755LV30Cd43ou57Q1RL1w8FUGa+1DrIpu48qU+PIT11hbDkbiiWw6WE4V/h fJvnHLenvhGkO/NoDDg6y/nkXn/rS13qSgNuD50Rzg9z0QinTnCtdIFGUoL9KhtJVx7Yz N88jdXS6ufQ6LgRBqSXGk/xaeLG60z33lFBu4JqUfpUZpDSvnhU++Uz61x8+jzCffAq95 tLa1deakcw8wdGr1I/WvfEwEsWhYVKXrQW7g+Odm5WAUSZQq8SR2jsJfKLf9LQRLOHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lK7GzTSu; arc=none smtp.remote-ip=209.85.217.47 Received: from mail-vs1-f47.google.com (mail-vs1-f47.google.com [209.85.217.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3138606A31A for ; Tue, 1 Dec 2026 07:06:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3138606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lK7GzTSu Received: by mail-vs1-f47.google.com with SMTP id ada2fe7eead31-7d2b25786d9so5910802137.0 for ; Mon, 30 Nov 2026 23:06:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796108793; x=1796713593; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=MBtQ6yWkR1apqck8cVJk/uTLKAt5sJ62L9jMNGTAiAI=; b=lK7GzTSu91hFhYAY9Qmq+S89/2MA4Dt9iGDEdVQY9TE1g6OIzYGEwrpW/DJPnvLSB9 sNKNBstlS6Z8ViUpE5hEU8J2cahinxQm4EB66CwGvLZEo/Ek2P/FZzoaAUtQU9X6iaGG O7CsLtc1RweYZ7BPsH+/+aRGBpPdnYfWXE1/LJEZLRc250lH+YfhRjNfIS2dk7uwD+0x sAoeAuwkvlMjhAg2438J8U+/pqLZ+bsisdQi+d6Jow6QlxfG13l1tWLWzTI9ASXaX9Tn kYr/jl5ZpkQ0xmUcq/VHofmzk3wVk+bsb81E8v5RsFXP0NBAzmSUZwj5oLpdUJOFpd/l s/IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796108793; x=1796713593; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MBtQ6yWkR1apqck8cVJk/uTLKAt5sJ62L9jMNGTAiAI=; b=oomAfd9SeFKaNYCIiXiE+vVd/oTwbZj3mPDRtxpPMJqcqtMnNPyLbSLcmdofQfL34D twUzUCqoGInHBwi3wev3mRSL+mM6iDlplqhtYuNVDUY6RXHT4Txq7M8v/pCbKaPf4cdv 1dtlGo3bpTS6aLh2HRsso/IdTDQ0tWsdzPwW8h4ocCQ7W8vY7I3sdIikAMET2BFsgMDt Q4Rui4QikVm/wUnHHLGup6hloqf4qnzWB5u9Yux3Qtm4Gktr2YboOj7DiyiG9JvT9+Yr pkHN6muTBAz9pC7dX2vlUG3ck8lPk/r/NgiADIcAeo69AlvZtyTplhVCtpcv3UK4QkBy dsuw== X-Forwarded-Encrypted: i=1; AL+hErHP+ogjOfQjl9BuukxTW7jad3RG4RopjAT5u06iw2dyaZlpbMa4HmRipRYpQEpDLlB5r7yVPKP4IGs0K1o=@vger.kernel.org X-Gm-Message-State: APuU4Z++v3QcSy840cMa+PJbnrN4KWJWT5581RSNIM08Tydod8Y1CUpR piudHDNvQQYqNuyS41dS83KsDEteSydCiTkHl++EsdTuZJRVrb99qVknIkaKi/Z0 X-Gm-Gg: AZFdTZeGbbCSmXH4AgHXhCFLVLwCCBexZ8b4VUXWwma9h+TrQHix+m/p+lMECA9lNIF JYu/yEXLufitVFj1OgVgBjZRfAqMtkntosZWLW85xps1pRdIfOGSHL8xTSEa+3G2TTSVFsN48RD v0Vd+baPejGUDl0Mi2ZeCw6F6rfx3zVFMQ+WniT+2mzV3NGn+DsGcA8ZBFq3epFeieL1oXJ2ysS AkSBQlT3qKZJzxJ8/cHV3MvLjEQaEqbXZoxgoLWjRhvpJ2XaXx5PL46ndnrs/l19j1iUDagzarx NYovZwv29jkz47/AUWKDKv7ShBxZAYI+TQb6vrCUIwy7wh9CLbFp8wIh0fK49thBJ+/tothpsDI KeLoVnrOjVkZa24HR3HOSUUgMHFmyU0Cz3Yx7fKQ+ljLQDetakheSDeygmn4niEfy9JwGh9LXWg SxxRbOzM5RAdk0jZMM73lJcxvo+5K4sO1gzAp3nikNhXXzCFpJ1Ms3OoJ0bg9kDd44Kaev1VfbN 2k3OVG7qiU6wIgT646xcJS8KOEI1nZJ08l1LnAIj7Vg95JFaPaT19zABOKxuIFeb3vSY+j3ZoMx pcqFVl4ySfDZ7jlgQtjsFOo9UUtraHBu4zPMv8o7NHLrNj2NqDwI5e+G5G4gq2xZ4s2Qcl03zO9 Xbk4pyD34oMMDG/c8LZgGqhgtzRiOOfg= X-Received: by 2002:a05:622a:1aaa:b0:536:6586:4f93 with SMTP id d75a77b69052e-55d76a624efmr658286071cf.42.1796108432004; Mon, 30 Nov 2026 23:00:32 -0800 (PST) Received: from Apollo.mynetworksettings.com ([2600:4040:a89a:c500:ff86:5e4e:eb13:93a7]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-55d78451407sm306483811cf.2.2026.11.30.23.00.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 23:00:26 -0800 (PST) From: Enjan Chou To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Luiz Augusto von Dentz , Siwei Zhang , linux-kernel@vger.kernel.org, Enjan Chou , stable@vger.kernel.org Subject: [PATCH v2] Bluetooth: hci_conn: Don't leak hci_conn when dequeuing its create command Date: Tue, 1 Dec 2026 01:59:27 -0500 Message-ID: <20261201065927.1189739-1-zhouer@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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