Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: hci_conn: fix use-after-free in le_conn_timeout()
@ 2026-12-04  7:24 Bin Luo via B4 Relay
  2026-12-04 14:28 ` bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  7:24 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Archie Pusaka, linux-bluetooth, linux-kernel, stable

From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

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) <luob87709@gmail.com>
---
 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:



^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Bluetooth: hci_conn: fix use-after-free in le_conn_timeout()
  2026-12-04  7:24 [PATCH] Bluetooth: hci_conn: fix use-after-free in le_conn_timeout() Bin Luo via B4 Relay
@ 2026-12-04 14:28 ` bluez.test.bot
  0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2026-12-04 14:28 UTC (permalink / raw)
  To: linux-bluetooth, luob87709

[-- Attachment #1: Type: text/plain, Size: 3070 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1231618/

---Test result---

Test Summary:
CheckPatch                    FAIL      1.34 seconds
VerifyFixes                   PASS      0.12 seconds
VerifySignedoff               PASS      0.12 seconds
GitLint                       FAIL      0.33 seconds
SubjectPrefix                 PASS      0.12 seconds
BuildKernel                   PASS      28.88 seconds
CheckAllWarning               PASS      32.53 seconds
CheckSparse                   WARNING   34.66 seconds
BuildKernel32                 PASS      28.72 seconds
CheckKernelLLVM               PASS      30.76 seconds
TestRunnerSetup               PASS      669.89 seconds
TestRunner_l2cap-tester       PASS      16.01 seconds
TestRunner_iso-tester         PASS      16.75 seconds
TestRunner_bnep-tester        PASS      3.13 seconds
TestRunner_mgmt-tester        PASS      87.20 seconds
TestRunner_rfcomm-tester      PASS      4.43 seconds
TestRunner_sco-tester         PASS      5.53 seconds
TestRunner_ioctl-tester       PASS      4.52 seconds
TestRunner_mesh-tester        PASS      4.40 seconds
TestRunner_smp-tester         PASS      4.35 seconds
TestRunner_userchan-tester    PASS      3.17 seconds
TestRunner_6lowpan-tester     PASS      4.34 seconds
IncrementalBuild              PASS      27.34 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
Bluetooth: hci_conn: fix use-after-free in le_conn_timeout()
WARNING: Prefer a maximum 75 chars per line (possible unwrapped commit description?)
#117: 
From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

total: 0 errors, 1 warnings, 0 checks, 121 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/home/runner/work/bluetooth-next/bluetooth-next/src/patch/15053372.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
Bluetooth: hci_conn: fix use-after-free in le_conn_timeout()

3: B1 Line exceeds max length (96>80): "From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>"
60: B1 Line exceeds max length (103>80): "Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>"
##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:
net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):


https://github.com/bluez/bluetooth-next/pull/1288

---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 14:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  7:24 [PATCH] Bluetooth: hci_conn: fix use-after-free in le_conn_timeout() Bin Luo via B4 Relay
2026-12-04 14:28 ` bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox