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: