From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796369051; b=zEVItSYLGogU88A+sSBWj6Ty3iinmMTScSfU8wf7NvbCFVRQXnYzBN6O7tBN/E4yNHjG XWpIeFsSqfrIekJr6IUQfyrMR9/7QCZSE+tVaD2++dmPJquxjrOz9pxS16tH/refztlPq 1O20hb0yk5YGVX5PvxbU+4jN4kNWwPa0Mc7Y7KjjbNzouYWJgoPKpOasOYBF/AymyQaSu KQoQiQfTqjvVNE9QeBzK7CHstQGFzfxMddyzI7DTjdtxcFh3OXDVKtfMJOSrrlsAcMglB Yi09TNmA7I7oDDB7Kb6EkY4qPBe7r+1pqn4LPRx0GkIcCC+6otdzO4mNDMPAIYdOz7Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796369051; h=Received:DKIM-Signature:Received:From:To:Cc:Subject:Date:Message-ID: Content-Type:MIME-Version:X-Mailer:X-Developer-Signature: X-Developer-Key:X-Endpoint-Received:X-Original-From:Reply-To: Content-Transfer-Encoding; bh=Zs2v173kW7Ov+PrwsfuBdft7cYXnAWZsOCca3RKhuCY=; b=csxHGHqQECqHY+IZ+m3SnfkBWkHva/5067FmlvdJpnEpYwr1ZuR6kiI4nppuXF5Un11Y KAn+PSFZ/GnF6rIBeFpW0PLnMLHwPB1seKmpp0XV7/mCnujCK5UjbSlLrBmgnMCLc93oG ppT3gl2YxWBoEfinK3V7213tZxKxFNQpumVrfUbFNTJOBPcuruc0futEg2MPBnfvpfmwJ JvUO7Y3wp9R01YEcKPvKJtWtVGOjFBj8NkaHm8bHj4n/qNpXMdECKYnoohQL98bONvWfJ 5SNk3lDhXF0GMG9s3EhNv52obPQMg5mzcKO0Hm+U52oeJ6NdLT0kVecZxo7/jSx0D+w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=abEranx8; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 465BB606A322; Fri, 4 Dec 2026 07:24:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=abEranx8 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796369050; b=GQObv8wsmDLqMGFxeJ8zDEJ1R0nhcEI0F7m57Sn3gtb0Mejxi9O5dIDGqTpRq/2tW7UX TcQYPKH3YdIhOpasfHP+PO17UaQSUDZduiah41V+pu7KFdPKV70je7PQjGak++V6h8pWm tn9wSqhT+ZsRln4ehT38auMmIQDvlgI50mogciU3THIM26k0zRRAp/FUd7zNIoLMuhVAa fPsP0vpPIUK8X4DABngbb/xP2Svsg9R3V6Axixtc/c7Yv9tAExq+wqZUgOKjQfEGUm5XZ cjtI4rSn2hmydLU4li8GyYQmepKOcI8IltYp1Rjr9hewoR4VLnZtdiDb+Z2ciciJsgg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796369050; h=DKIM-Signature:Received:From:To:Cc:Subject:Date:Message-ID: Content-Type:MIME-Version:X-Mailer:X-Developer-Signature: X-Developer-Key:X-Endpoint-Received:X-Original-From:Reply-To: Content-Transfer-Encoding; bh=Zs2v173kW7Ov+PrwsfuBdft7cYXnAWZsOCca3RKhuCY=; b=ScSQP8G7EMZ0QRwPVAYcD8RXe5ZVvLNGSNKGC4ZkxEVB0IlkW24Cz1i3VZzMSlo91/ia RBFerxriAAzYvSjzVF7s0zGEzSjc4mUdqsVVzBo2tDdiypkeEGZfvkP4iafHCzYUocKdp 5YEBjKtsZI91qMtB9KdlDVjhMTVzv5eMhNsyTnbi2+Tv2grPBAsGvKPWaoGsEJoGpZGzB HrhkXsQ4LXdmwyKT8hpxnbDRrWTfprKniO4Cav1A5kLImrmIVuSfoegXdpKHt+8e8nlqp aBE4mt/5mjgURlBCbiu8qTg0CgW2Rcd93Qs7y87lU357v5YMC+BzFqZgJbgp0iJioDw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id F30441F000CE; Fri, 4 Dec 2026 07:24:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796369050; bh=Zs2v173kW7Ov+PrwsfuBdft7cYXnAWZsOCca3RKhuCY=; h=From:To:Cc:Subject:Date:Reply-To; b=abEranx8sANKFAyZmxMJkcg31sQGuGCLZzU2Ro6CczGC7ncv3MXYtjMM3xICqzmhz awYFAXYnfxaKyX0R8kGi/BpTFCBnqyv1b9KSG3FbbwGE8z+zjXoaNheDEsdv1EJwIx Ov6D61HzIGrOr7wjQ0LM8wE31qjg+fMHeHmlx8oMCJsoS8OXZnVfQLfrNFZ0ffCNW2 +XqIZjJLDezty9ZHEVK57ndhxztdRJ2VBkWl54CBTYYWcP6W1Hkv/ONqzq6yIzBTGG +w1llUnI2rJku7dyFhwNTmkIj/6u21RolvtBCq4dI4R2KG++n5EpvdPN8MxkolkBsu 0yiD3zPu/pwgw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 577BE4D993; Fri, 4 Dec 2026 07:24:09 +0000 (UTC) From: Bin Luo via B4 Relay To: Marcel Holtmann , Luiz Augusto von Dentz Cc: Archie Pusaka , linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] Bluetooth: hci_conn: fix use-after-free in le_conn_timeout() Date: Fri, 04 Dec 2026 15:24:08 +0800 Message-ID: <20261204072408.486-1-luob87709@gmail.com> Content-Type: text/plain; charset="utf-8" X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796369048; l=7159; i=luob87709@gmail.com; h=from:subject:message-id; bh=8KIozGG6c49Cf74QY3fTxgh2vyJfL5L7VkxqFSwg1Hg=; b=uQIDfH6SOQRtFsP0gfU+SFdIvS14RexKya/j4I1EfFBQnbfE+vren2srBkntzBpQqkHq4RMHL T5cQ8gTpAa5CfwB0MqmCL6WEWN/gAdmGVqdD9kHU2s9eb91mrFSKfRh X-Developer-Key: i=luob87709@gmail.com; a=ed25519; pk=qac/JyfPpjPD91IsbcZY5AvHG2LdM5meIPeVzh4kuN0= X-Endpoint-Received: by B4 Relay for luob87709@gmail.com/default with auth_id=1739 X-Original-From: "Bin Luo (University of Electronic Science and Technology of China)" Reply-To: luob87709@gmail.com Content-Transfer-Encoding: 7bit From: "Bin Luo (University of Electronic Science and Technology of China)" When initiating an LE connection via directed advertising, hci_cc_le_set_adv_enable() and hci_cc_le_set_ext_adv_enable() queue conn->le_conn_timeout onto hdev->workqueue without holding a reference on conn. Meanwhile, hci_conn_del() only cancels conn->le_conn_timeout asynchronously via cancel_delayed_work() before freeing conn through hci_conn_cleanup() -> hci_conn_del_sysfs() -> put_device(&conn->dev). If hci_conn_del() runs from another context (such as hci_abort_conn_sync() on hdev->req_workqueue) while le_conn_timeout() is already executing on hdev->workqueue, cancel_delayed_work() does not wait for the running worker, conn is freed by bt_link_release(), and le_conn_timeout() then dereferences the freed conn in hci_conn_failed() or hci_abort_conn(): BUG: KASAN: slab-use-after-free in hci_conn_failed+0x26e/0x290 Read of size 8 at addr ffff8880077291b0 by task kworker/u17:1/70 Workqueue: hci0 le_conn_timeout Call Trace: hci_conn_failed+0x26e/0x290 le_conn_timeout+0x17c/0x1d0 process_one_work+0x8cf/0x19c0 Freed by task 69: kfree+0x308/0x530 bt_link_release+0x3c/0x50 device_release+0xcd/0x240 kobject_put+0x14d/0x280 hci_cmd_sync_work+0x191/0x2e0 Switching hci_conn_del() to cancel/disable_delayed_work_sync() is not safe because hci_conn_del() is called with hdev->lock held (including from le_conn_timeout() itself on the HCI_ROLE_SLAVE path), which would cause an AB-BA lock inversion with hdev->lock or a self-deadlock. Fix this by keeping conn alive with hci_conn_get()/hci_conn_put() across the lifetime of conn->le_conn_timeout, disabling the work via disable_delayed_work() in hci_conn_del() and hci_dev_close_sync() (putting the reference if a pending work item was cancelled), and re-validating conn with hci_conn_valid() and conn->state == BT_CONNECT under hdev->lock inside le_conn_timeout() before calling le_disable_advertising(), hci_conn_failed(), or hci_abort_conn(). Also move flush_delayed_work(&conn->le_conn_timeout) in create_le_conn_complete() before acquiring hdev->lock (where conn is already pinned by the hci_cmd_sync reference) to avoid a lock ordering inversion with le_conn_timeout() acquiring hdev->lock. This issue was found during a static audit of asynchronous work teardown paths and reproduced on v7.4-rc4 using a virtual HCI (/dev/vhci) reproducer in QEMU KVM with KASAN, Lockdep, and DEBUG_OBJECTS enabled. With the patch applied, 3 x 150 concurrent timeout/close iterations plus additional LE connection-complete cancel, create-error flush, and device power-cycle tests completed cleanly with no KASAN, ODEBUG, or Lockdep warnings. Fixes: 9489eca4ab2f ("Bluetooth: Add timeout for LE connection attempts") Fixes: 3c857757ef6e ("Bluetooth: Add directed advertising support through connect()") Fixes: 5cd39700de9b ("Bluetooth: Make sure LE create conn cancel is sent when timeout") Cc: stable@vger.kernel.org Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) --- 0 files changed diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -1739,6 +1739,26 @@ put_device(&conn->dev); } +static inline void hci_conn_queue_le_timeout(struct hci_conn *conn) +{ + hci_conn_get(conn); + if (!queue_delayed_work(conn->hdev->workqueue, &conn->le_conn_timeout, + conn->conn_timeout)) + hci_conn_put(conn); +} + +static inline void hci_conn_cancel_le_timeout(struct hci_conn *conn) +{ + if (cancel_delayed_work(&conn->le_conn_timeout)) + hci_conn_put(conn); +} + +static inline void hci_conn_disable_le_timeout(struct hci_conn *conn) +{ + if (disable_delayed_work(&conn->le_conn_timeout)) + hci_conn_put(conn); +} + static inline struct hci_conn *hci_conn_hold(struct hci_conn *conn) { BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt)); diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -736,6 +736,12 @@ BT_DBG(""); + hci_dev_lock(hdev); + if (!hci_conn_valid(hdev, conn) || conn->state != BT_CONNECT) { + hci_dev_unlock(hdev); + goto done; + } + /* We could end up here due to having done directed advertising, * so clean up the state if necessary. This should however only * happen with broken hardware or if low duty cycle was used @@ -744,13 +750,16 @@ if (conn->role == HCI_ROLE_SLAVE) { /* Disable LE Advertising */ le_disable_advertising(hdev); - hci_dev_lock(hdev); hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); hci_dev_unlock(hdev); - return; + goto done; } + hci_dev_unlock(hdev); hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); + +done: + hci_conn_put(conn); } struct iso_list_data { @@ -1345,7 +1354,7 @@ hdev->acl_cnt += conn->sent; break; case LE_LINK: - cancel_delayed_work(&conn->le_conn_timeout); + hci_conn_disable_le_timeout(conn); if (hdev->le_pkts) { if (!hci_conn_num(hdev, LE_LINK) || diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -1619,9 +1619,7 @@ conn = hci_lookup_le_connect(hdev); if (conn) - queue_delayed_work(hdev->workqueue, - &conn->le_conn_timeout, - conn->conn_timeout); + hci_conn_queue_le_timeout(conn); } else { hci_dev_clear_flag(hdev, HCI_LE_ADV); } @@ -1695,9 +1693,7 @@ conn = hci_lookup_le_connect(hdev); if (conn) - queue_delayed_work(hdev->workqueue, - &conn->le_conn_timeout, - conn->conn_timeout); + hci_conn_queue_le_timeout(conn); } else { if (cp->num_of_sets) { hci_le_ext_adv_set_enabled(hdev, set->handle, false); @@ -5969,7 +5965,7 @@ } } } else { - cancel_delayed_work(&conn->le_conn_timeout); + hci_conn_cancel_le_timeout(conn); } /* The HCI_LE_Connection_Complete event is only sent once per connection. diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c --- a/net/bluetooth/hci_sync.c +++ b/net/bluetooth/hci_sync.c @@ -5852,7 +5852,7 @@ disable_delayed_work(&conn->disc_work); disable_delayed_work(&conn->auto_accept_work); disable_delayed_work(&conn->idle_work); - disable_delayed_work(&conn->le_conn_timeout); + hci_conn_disable_le_timeout(conn); } hci_dev_unlock(hdev); @@ -7573,6 +7573,13 @@ if (err == -ECANCELED) goto done; + /* Flush to make sure we send create conn cancel command if needed. + * Flush before taking hdev->lock since le_conn_timeout() acquires + * hdev->lock. + */ + if (err) + flush_delayed_work(&conn->le_conn_timeout); + hci_dev_lock(hdev); if (!hci_conn_valid(hdev, conn)) @@ -7592,8 +7599,6 @@ if (conn->state != BT_CONNECT) goto unlock; - /* Flush to make sure we send create conn cancel command if needed */ - flush_delayed_work(&conn->le_conn_timeout); hci_conn_failed(conn, bt_status(err)); unlock: