Skip to contentSkip to conversation

Bluetooth: hci_sync: wait for directed advertising completion

PATCH Superseded Chengfeng Ye 2 participants 2 messages Aug 21

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Bluetooth: hci_sync: wait for directed advertising completion

le_conn_timeout is embedded in struct hci_conn, but queuing the work does not hold a reference to the connection. hci_conn_del() uses cancel_delayed_work() because synchronous cancellation would deadlock when le_conn_timeout() itself calls hci_conn_del() while holding hdev->lock.

This leaves the following interleaving possible:

CPU 0 CPU 1
le_conn_timeout()
hci_conn_del()
cancel_delayed_work() = false hci_conn_cleanup()
put_device()
kfree(conn)
hci_conn_failed(conn, ...)

The callback then dereferences the released connection. KASAN reported:

  BUG: KASAN: slab-use-after-free in hci_conn_failed+0x232/0x250
  Read of size 8 at addr ffff8881180e8e20 by task kworker/u33:1/111
  Workqueue: hci0 le_conn_timeout
  Call Trace:
   hci_conn_failed+0x232/0x250
   le_conn_timeout+0x23e/0x2c0
   process_one_work+0x61b/0xf50
   worker_thread+0x45b/0xd10

Remove le_conn_timeout instead of adding another connection reference. Have the legacy and extended directed-advertising enable commands wait for the appropriate LE Connection Complete event in hci_le_create_conn_sync(). The command-sync entry already holds a connection reference until its completion callback returns.

Command Status already keeps a command-sync request pending when the command succeeds and a later event was requested. Do the same for Command Complete so controllers that complete LE Set Advertising Enable with Command Complete do not bypass the requested LE Connection Complete wait.

Mark directed advertising as an in-flight connection attempt so teardown can cancel the wait. Disable advertising synchronously when that wait fails, and preserve HCI_ERROR_ADVERTISING_TIMEOUT for a software timeout. Clear the instance-0 extended advertising state when advertising is stopped or a connection completes so resuming paused advertising does not restart the directed advertising instance.

There is then no delayed callback that can race with connection deletion.

Fixes 980ffc0a2cec ("Bluetooth: Fix LE connection timeout deadlock")

---
Changes in v3:
- Keep command-sync requests pending on successful Command Complete when the caller requested a later event, matching the existing Command Status behavior and fixing the first Sashiko/Luiz report.
- Clear HCI_LE_ADV_0 when all extended advertising instances are disabled and when LE connection complete implicitly stops advertising, fixing the second Sashiko report about resuming stale directed advertising.

Changes in v2:
- Remove le_conn_timeout instead of adding references around delayed work.
- Wait for LE Connection Complete from both directed-advertising enable paths.
- Make the wait cancellable and disable advertising after a failed wait.

 include/net/bluetooth/hci_core.h |  1 -
 net/bluetooth/hci_conn.c         | 45 --------------------------------
 net/bluetooth/hci_event.c        | 44 ++++++++++---------------------
 net/bluetooth/hci_sync.c         | 42 ++++++++++++++++++++---------
 4 files changed, 44 insertions(+), 88 deletions(-)
include/net/bluetooth/hci_core.h +0 -1
index 3df59849dcbe..937c9ba1e6b7 100644
@@ -761,7 +761,6 @@ struct hci_conn {
 	struct delayed_work disc_work;
 	struct delayed_work auto_accept_work;
 	struct delayed_work idle_work;
-	struct delayed_work le_conn_timeout;
 
 	struct device	dev;
 	struct dentry	*debugfs;
net/bluetooth/hci_conn.c +0 -45

63 lines not on this page · raw message

net/bluetooth/hci_event.c +14 -30

91 lines not on this page · raw message

net/bluetooth/hci_sync.c +30 -12

84 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: net/bluetooth/hci_event.c:5721
error: net/bluetooth/hci_event.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth