[PATCH v2 2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared

From: Jeremy Dean Clifton

Date: Mon Nov 16 2026 - 21:36:04 EST


hci_dev_close_sync() clears HCI_UP and later calls hci_conn_hash_flush()
to delete every connection. A connection created by __hci_conn_add()
after the flush has run is never deleted. It keeps the hci_dev_hold()
reference and the sysfs device taken in __hci_conn_add(), so the
hci_dev is never freed, and its works can still be queued after the
workqueue is drained.

Check HCI_UP in __hci_conn_add(), which serves both hci_conn_add() and
hci_conn_add_unset(). All callers hold hdev->lock: the HCI event
handlers, create_pa_complete() in hci_sync.c, and the l2cap, sco, iso
and mgmt paths that reach the hci_connect_*() and hci_bind_*() helpers.
hci_conn_hash_flush() runs under hdev->lock too, so a connection is
either added before the flush and deleted by it, or refused.

Also include lockdep_assert_held() so the locking rule is checked.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jeremy Dean Clifton <deaner92@xxxxxxxxx>
---
net/bluetooth/hci_conn.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index d729a72f2f77..7f0250653189 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -1001,6 +1001,15 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type,
struct hci_conn *conn;
struct smp_irk *irk = NULL;

+ lockdep_assert_held(&hdev->lock);
+
+ /* hci_dev_close_sync() clears HCI_UP before hci_conn_hash_flush().
+ * A connection added after that would never be flushed, so refuse
+ * it. The caller holds hdev->lock, which the flush also takes.
+ */
+ if (!test_bit(HCI_UP, &hdev->flags))
+ return ERR_PTR(-ENETDOWN);
+
switch (type) {
case ACL_LINK:
if (!hdev->acl_mtu)
--
2.43.0