Skip to contentSkip to conversation

Bluetooth: l2cap: fix UAF race in l2cap_sock_cleanup_listen

PATCH In review Safa Karakuş 7 participants 19 messages Apr 28 – May 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bluetooth: l2cap: fix UAF race in l2cap_sock_cleanup_listen

l2cap_sock_cleanup_listen() dequeues child sockets via bt_accept_dequeue() without holding a reference on the returned sk. A concurrent HCI disconnect can trigger l2cap_conn_del() on CPU1 which, while holding chan->lock, calls:

teardown_cb -> sock_set_flag(sk, SOCK_ZAPPED)
close_cb -> l2cap_sock_kill(sk) -> sock_put(sk) -> kfree(sk)

all before CPU0 has a chance to acquire chan->lock. CPU0 then calls l2cap_chan_lock() on the now-freed sk's chan (already safe because l2cap_chan_hold() was called first) but subsequently passes the freed sk pointer to l2cap_sock_kill(), causing a use-after-free read on sk->sk_flags and sk->sk_socket.

Fix by calling sock_hold() immediately after bt_accept_dequeue() to prevent kfree(sk) from racing with our traversal. After acquiring chan->lock, check SOCK_DEAD: if l2cap_conn_del() already invoked l2cap_sock_kill() (which sets SOCK_DEAD), skip the duplicate call to avoid a double sock_put(). Drop the extra reference with sock_put() at the end of each loop iteration.

Fixes 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")
---
 net/bluetooth/l2cap_sock.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/bluetooth/l2cap_sock.c +16 -2
index 71e8c1b45..4475d3377 100644
@@ -1477,7 +1477,15 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 
 	/* Close not yet accepted channels */
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
-		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+		struct l2cap_chan *chan;
+
+		/* Hold sk across the chan->lock acquisition window.
+		 * A concurrent l2cap_conn_del() can call l2cap_sock_kill(sk)
+		 * -> kfree(sk) inside chan->lock before we acquire it,
+		 * leaving a dangling pointer.
+		 */
+		sock_hold(sk);
+		chan = l2cap_pi(sk)->chan;
 
 		BT_DBG("child chan %p state %s", chan,
 		       state_to_string(chan->state));
@@ -1487,10 +1495,16 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 
 		__clear_chan_timer(chan);
 		l2cap_chan_close(chan, ECONNRESET);
-		l2cap_sock_kill(sk);
+		/* l2cap_conn_del() may have already called l2cap_sock_kill()
+		 * (setting SOCK_DEAD); skip the duplicate to avoid a
+		 * double sock_put().
+		 */
+		if (!sock_flag(sk, SOCK_DEAD))
+			l2cap_sock_kill(sk);
 
 		l2cap_chan_unlock(chan);
 		l2cap_chan_put(chan);
+		sock_put(sk);
 	}
 }
 

--
2.34.1

#1 lore raw

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/project/bluetooth/list/?series=1087159

---Test result---

Test Summary:

CheckPatch                    FAIL      0.68 seconds
GitLint                       PASS      0.29 seconds
SubjectPrefix                 PASS      0.11 seconds
BuildKernel                   PASS      26.11 seconds
CheckAllWarning               PASS      28.61 seconds
CheckSparse                   PASS      27.17 seconds
BuildKernel32                 PASS      24.89 seconds
TestRunnerSetup               PASS      564.19 seconds
TestRunner_l2cap-tester       PASS      27.43 seconds
IncrementalBuild              PASS      24.06 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
Bluetooth: l2cap: fix UAF race in l2cap_sock_cleanup_listen
WARNING: From:/Signed-off-by: email name mismatch: 'From: Safa Karakuş <[email protected]>' != 'Signed-off-by: Safa Karakus<[email protected]>'

total: 0 errors, 1 warnings, 0 checks, 33 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.

/github/workspace/src/patch/14545605.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.

Wide character in print at /github/workspace/src/src/scripts/checkpatch.pl line 7853.

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

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Safa Karakuş

Hi Safa,

Quoted 69 lines · Safa Karakuş

l2cap_sock_cleanup_listen() dequeues child sockets via bt_accept_dequeue() without holding a reference on the returned sk. A concurrent HCI disconnect can trigger l2cap_conn_del() on CPU1 which, while holding chan->lock, calls:

teardown_cb -> sock_set_flag(sk, SOCK_ZAPPED)
close_cb -> l2cap_sock_kill(sk) -> sock_put(sk) -> kfree(sk)

all before CPU0 has a chance to acquire chan->lock. CPU0 then calls l2cap_chan_lock() on the now-freed sk's chan (already safe because l2cap_chan_hold() was called first) but subsequently passes the freed sk pointer to l2cap_sock_kill(), causing a use-after-free read on sk->sk_flags and sk->sk_socket.

Fix by calling sock_hold() immediately after bt_accept_dequeue() to prevent kfree(sk) from racing with our traversal. After acquiring chan->lock, check SOCK_DEAD: if l2cap_conn_del() already invoked l2cap_sock_kill() (which sets SOCK_DEAD), skip the duplicate call to avoid a double sock_put(). Drop the extra reference with sock_put() at the end of each loop iteration.

Fixes 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")
---
 net/bluetooth/l2cap_sock.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/bluetooth/l2cap_sock.c +16 -3
index 71e8c1b45..4475d3377 100644
@@ -1477,7 +1477,15 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 
        /* Close not yet accepted channels */
        while ((sk = bt_accept_dequeue(parent, NULL))) {
-               struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+               struct l2cap_chan *chan;
+
+               /* Hold sk across the chan->lock acquisition window.
+                * A concurrent l2cap_conn_del() can call l2cap_sock_kill(sk)
+                * -> kfree(sk) inside chan->lock before we acquire it,
+                * leaving a dangling pointer.
+                */
+               sock_hold(sk);
+               chan = l2cap_pi(sk)->chan;
 
                BT_DBG("child chan %p state %s", chan,
                       state_to_string(chan->state));
@@ -1487,10 +1495,16 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 
                __clear_chan_timer(chan);
                l2cap_chan_close(chan, ECONNRESET);
-               l2cap_sock_kill(sk);
+               /* l2cap_conn_del() may have already called l2cap_sock_kill()
+                * (setting SOCK_DEAD); skip the duplicate to avoid a
+                * double sock_put().
+                */
+               if (!sock_flag(sk, SOCK_DEAD))
+                       l2cap_sock_kill(sk);
 
                l2cap_chan_unlock(chan);
                l2cap_chan_put(chan);
+               sock_put(sk);
        }
 }
 
--
Quoted 1 line

2.34.1

sashiko flags 2 critical flaws with these changes:

https://sashiko.dev/#/patchset/AS8P250MB079109F82C16BEDC4F9FE584EB372%40AS8P250MB0791.EURP250.PROD.OUTLOOK.COM

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

PATCH v3 1/1 Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

bt_accept_dequeue() unlinks a not-yet-accepted child from the parent accept queue and release_sock()s it before returning, so the returned sk has no caller reference and is unlocked.

l2cap_sock_cleanup_listen() walks these children on listening-socket close. A concurrent HCI disconnect drives hci_rx_work -> l2cap_conn_del() which runs l2cap_chan_del() + l2cap_sock_kill() and frees the child sk and its l2cap_chan; cleanup_listen() then uses both:

  BUG: KASAN: slab-use-after-free in l2cap_sock_kill
    l2cap_sock_kill / l2cap_sock_cleanup_listen / __x64_sys_close
  Freed by: l2cap_conn_del -> l2cap_sock_close_cb -> l2cap_sock_kill

CVE-2025-39860 only serialised the two userspace threads racing bt_accept_dequeue() (cleanup_listen() under lock_sock() in l2cap_sock_release()); it does not cover l2cap_conn_del() from hci_rx_work, so this still reproduces on v7.0-rc5.

Take the reference at the source: bt_accept_dequeue() does sock_hold() while sk is still locked, before release_sock(); callers sock_put(). cleanup_listen() pins the chan with l2cap_chan_hold_unless_zero() under a brief child sk lock (serialising vs l2cap_sock_teardown_cb()), drops it before l2cap_chan_lock(), and skips a duplicate l2cap_sock_kill() on SOCK_DEAD. conn->lock is not taken here: cleanup_listen() runs under the parent sk lock and that would invert
conn->lock -> chan->lock -> sk_lock (lockdep).

KASAN/SMP: 12 use-after-free per run before, 0 and no lockdep over 1400+ raced iterations after.

Fixes 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")

---
Hi Luiz,

v3 - addresses the sashiko findings on v1. An interim approach using conn->lock closed the UAF but hit a lockdep inversion, so this pins the chan via a brief child sk lock instead.

Changes since v2:
- Take the ref inside bt_accept_dequeue() (v1/v2 added sock_hold() after it, racing the free); also fix the chan lifetime; no conn->lock (lockdep). Reproduced on v7.0-rc5 post CVE-2025-39860:
12 UAF/run -> 0.
Changes since v1: consistent From/Signed-off-by.

 net/bluetooth/af_bluetooth.c | 10 +++++++
 net/bluetooth/iso.c          |  9 ++++++-
 net/bluetooth/l2cap_sock.c   | 51 +++++++++++++++++++++++++++++++-----
 net/bluetooth/rfcomm/sock.c  |  9 ++++++-
 net/bluetooth/sco.c          |  9 ++++++-
 5 files changed, 78 insertions(+), 10 deletions(-)
net/bluetooth/af_bluetooth.c +10 -0
index 2b94e2077..10eafe7c1 100644
@@ -309,6 +309,16 @@ struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
 			if (newsock)
 				sock_graft(sk, newsock);
 
+			/* Hand the caller a reference taken while sk is still
+			 * locked.  bt_accept_unlink() just dropped the
+			 * accept-queue reference; without this hold a
+			 * concurrent teardown (e.g. l2cap_conn_del() ->
+			 * l2cap_sock_kill()) could free sk between
+			 * release_sock() and the caller using it.  Every
+			 * caller drops this with sock_put() when done.
+			 */
+			sock_hold(sk);
+
 			release_sock(sk);
 			return sk;
 		}
net/bluetooth/iso.c +8 -1
index be145e273..94732563d 100644
@@ -759,6 +759,8 @@ static void iso_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		iso_sock_close(sk);
 		iso_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	/* If listening socket has a hcon, properly disconnect it */
@@ -1364,8 +1366,13 @@ static int iso_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		ch = bt_accept_dequeue(sk, newsock);
-		if (ch)
+		if (ch) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps ch alive from here.
+			 */
+			sock_put(ch);
 			break;
+		}
 
 		if (!timeo) {
 			err = -EAGAIN;
net/bluetooth/l2cap_sock.c +44 -7

74 lines not on this page · raw message

net/bluetooth/rfcomm/sock.c +8 -1
index be6639cd6..bd7d959c6 100644
@@ -180,6 +180,8 @@ static void rfcomm_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		rfcomm_sock_close(sk);
 		rfcomm_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	parent->sk_state  = BT_CLOSED;
@@ -497,8 +499,13 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		nsk = bt_accept_dequeue(sk, newsock);
-		if (nsk)
+		if (nsk) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps nsk alive from here.
+			 */
+			sock_put(nsk);
 			break;
+		}
 
 		if (!timeo) {
 			err = -EAGAIN;
net/bluetooth/sco.c +8 -1
index 584e059de..72bcbf1da 100644
@@ -487,6 +487,8 @@ static void sco_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		sco_sock_close(sk);
 		sco_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	parent->sk_state  = BT_CLOSED;
@@ -743,8 +745,13 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		ch = bt_accept_dequeue(sk, newsock);
-		if (ch)
+		if (ch) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps ch alive from here.
+			 */
+			sock_put(ch);
 			break;
+		}
 
 		if (!timeo) {
 			err = -EAGAIN;
Signature · 1 line
2.34.1
#4 lore raw

RE: [v3] Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

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/af_bluetooth.c:309
error: net/bluetooth/af_bluetooth.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

#5 lore raw

PATCH v4 1/1 Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

In reply to Safa Karakuş

bt_accept_dequeue() unlinks a not-yet-accepted child from the parent accept queue and release_sock()s it before returning, so the returned sk has no caller reference and is unlocked.

l2cap_sock_cleanup_listen() walks these children on listening-socket close. A concurrent HCI disconnect drives hci_rx_work -> l2cap_conn_del() which runs l2cap_chan_del() + l2cap_sock_kill() and frees the child sk and its l2cap_chan; cleanup_listen() then uses both:

  BUG: KASAN: slab-use-after-free in l2cap_sock_kill
    l2cap_sock_kill / l2cap_sock_cleanup_listen / __x64_sys_close
  Freed by: l2cap_conn_del -> l2cap_sock_close_cb -> l2cap_sock_kill

This is distinct from the two fixes already in this area: commit e83f5e24da741 ("Bluetooth: serialize accept_q access") serialises the accept_q list/poll and takes temporary refs inside bt_accept_dequeue(), and CVE-2025-39860 serialises the userspace close()/accept() race by calling cleanup_listen() under lock_sock() in l2cap_sock_release(). Neither covers l2cap_conn_del() running from hci_rx_work, so this UAF still reproduces on current bluetooth/master.

Take the reference at the source: bt_accept_dequeue() does sock_hold() while sk is still locked, before release_sock(); callers sock_put(). cleanup_listen() pins the chan with l2cap_chan_hold_unless_zero() under a brief child sk lock (serialising vs l2cap_sock_teardown_cb()), drops it before l2cap_chan_lock(), and skips a duplicate l2cap_sock_kill() on SOCK_DEAD. conn->lock is not taken here: cleanup_listen() runs under the parent sk lock and that would invert
conn->lock -> chan->lock -> sk_lock (lockdep).

KASAN/SMP: an unprivileged listen/close vs HCI-disconnect race produced 12 use-after-free reports per run before this change; 0, and no lockdep report, over 1600+ raced iterations after it on bluetooth/master.

Fixes 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")

---
Hi Luiz,

v4 - rebased on current bluetooth/master (after e83f5e24d "serialize accept_q access"); the af_bluetooth.c hunk now sits in the reworked bt_accept_dequeue(). This residual (cleanup_listen vs l2cap_conn_del, not covered by e83f5e24d nor CVE-2025-39860) is unchanged from v3 and re-verified with KASAN on bluetooth/master: 12 UAF/run -> 0, no lockdep, over 1600+ raced iterations.

Changes since v3: rebased onto e83f5e24d; commit message notes why e83f5e24d/CVE-2025-39860 do not cover this path.
Changes since v2: fix at the source in bt_accept_dequeue() + chan lifetime via l2cap_chan_hold_unless_zero(); no conn->lock (lockdep). Changes since v1: consistent From/Signed-off-by.

 net/bluetooth/af_bluetooth.c | 10 +++++++
 net/bluetooth/iso.c          |  9 ++++++-
 net/bluetooth/l2cap_sock.c   | 51 +++++++++++++++++++++++++++++++-----
 net/bluetooth/rfcomm/sock.c  |  9 ++++++-
 net/bluetooth/sco.c          |  9 ++++++-
 5 files changed, 78 insertions(+), 10 deletions(-)
net/bluetooth/af_bluetooth.c +10 -0
index 9d68dd860..1a6aa3f8d 100644
@@ -340,6 +340,16 @@ struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
 			if (newsock)
 				sock_graft(sk, newsock);
 
+			/* Hand the caller a reference taken while sk is
+			 * still locked.  bt_accept_unlink() just dropped
+			 * the accept-queue reference; without this hold a
+			 * concurrent teardown (e.g. l2cap_conn_del() ->
+			 * l2cap_sock_kill()) could free sk between
+			 * release_sock() and the caller using it.  Every
+			 * caller drops this with sock_put() when done.
+			 */
+			sock_hold(sk);
+
 			release_sock(sk);
 			if (next)
 				sock_put(next);
net/bluetooth/iso.c +8 -1
index 7cb2864fe..812f9002d 100644
@@ -751,6 +751,8 @@ static void iso_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		iso_sock_close(sk);
 		iso_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	/* If listening socket has a hcon, properly disconnect it */
@@ -1356,8 +1358,13 @@ static int iso_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		ch = bt_accept_dequeue(sk, newsock);
-		if (ch)
+		if (ch) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps ch alive from here.
+			 */
+			sock_put(ch);
 			break;
+		}
 
 		if (!timeo) {
 			err = -EAGAIN;
net/bluetooth/l2cap_sock.c +44 -7

74 lines not on this page · raw message

net/bluetooth/rfcomm/sock.c +8 -1
index be6639cd6..bd7d959c6 100644
@@ -180,6 +180,8 @@ static void rfcomm_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		rfcomm_sock_close(sk);
 		rfcomm_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	parent->sk_state  = BT_CLOSED;
@@ -497,8 +499,13 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		nsk = bt_accept_dequeue(sk, newsock);
-		if (nsk)
+		if (nsk) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps nsk alive from here.
+			 */
+			sock_put(nsk);
 			break;
+		}
 
 		if (!timeo) {
 			err = -EAGAIN;
net/bluetooth/sco.c +8 -1
index eba44525d..f1799c6a6 100644
@@ -502,6 +502,8 @@ static void sco_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		sco_sock_close(sk);
 		sco_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	parent->sk_state  = BT_CLOSED;
@@ -765,8 +767,13 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		ch = bt_accept_dequeue(sk, newsock);
-		if (ch)
+		if (ch) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps ch alive from here.
+			 */
+			sock_put(ch);
 			break;
+		}
 
 		if (!timeo) {
 			err = -EAGAIN;
Signature · 1 line
2.34.1
#6 lore raw

RE: [v4] Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

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/project/bluetooth/list/?series=1095877

---Test result---

Test Summary:

CheckPatch                    PASS      1.32 seconds
GitLint                       PASS      0.22 seconds
SubjectPrefix                 PASS      0.08 seconds
BuildKernel                   PASS      23.87 seconds
CheckAllWarning               PASS      26.65 seconds
CheckSparse                   PASS      25.16 seconds
BuildKernel32                 PASS      23.25 seconds
TestRunnerSetup               PASS      518.74 seconds
TestRunner_l2cap-tester       PASS      375.73 seconds
TestRunner_iso-tester         PASS      596.04 seconds
TestRunner_bnep-tester        PASS      18.18 seconds
TestRunner_mgmt-tester        PASS      2021.60 seconds
TestRunner_rfcomm-tester      PASS      62.71 seconds
TestRunner_sco-tester         PASS      140.77 seconds
TestRunner_ioctl-tester       PASS      133.16 seconds
TestRunner_mesh-tester        PASS      59.48 seconds
TestRunner_smp-tester         PASS      17.61 seconds
TestRunner_userchan-tester    PASS      18.69 seconds
TestRunner_6lowpan-tester     PASS      50.66 seconds
IncrementalBuild              PASS      22.44 seconds

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

---
Regards,
Linux Bluetooth

#7 lore raw

Re: [PATCH v4] Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

In reply to Safa Karakuş
Quoted 4 lines · On Sat, 16 May 2026 21:15:04 +0300 Safa Karakus wrote:

bt_accept_dequeue() unlinks a not-yet-accepted child from the parent accept queue and release_sock()s it before returning, so the returned sk has no caller reference and is unlocked.

Quoted 9 lines

l2cap_sock_cleanup_listen() walks these children on listening-socket close. A concurrent HCI disconnect drives hci_rx_work -> l2cap_conn_del() which runs l2cap_chan_del() + l2cap_sock_kill() and frees the child sk and its l2cap_chan; cleanup_listen() then uses both:

  BUG: KASAN: slab-use-after-free in l2cap_sock_kill
    l2cap_sock_kill / l2cap_sock_cleanup_listen / __x64_sys_close
  Freed by: l2cap_conn_del -> l2cap_sock_close_cb -> l2cap_sock_kill

Feel free to add the regular KASAN uaf calltrace to help understand your fix.

Reviewed-by Siwei Zhang on v4 1/1
#8 lore raw

Re: [PATCH v4] Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

In reply to Safa Karakuş

Hi,

Quoted 251 lines · Safa Karakuş

bt_accept_dequeue() unlinks a not-yet-accepted child from the parent accept queue and release_sock()s it before returning, so the returned sk has no caller reference and is unlocked.

l2cap_sock_cleanup_listen() walks these children on listening-socket close. A concurrent HCI disconnect drives hci_rx_work -> l2cap_conn_del() which runs l2cap_chan_del() + l2cap_sock_kill() and frees the child sk and its l2cap_chan; cleanup_listen() then uses both:

  BUG: KASAN: slab-use-after-free in l2cap_sock_kill
    l2cap_sock_kill / l2cap_sock_cleanup_listen / __x64_sys_close
  Freed by: l2cap_conn_del -> l2cap_sock_close_cb -> l2cap_sock_kill

This is distinct from the two fixes already in this area: commit e83f5e24da741 ("Bluetooth: serialize accept_q access") serialises the accept_q list/poll and takes temporary refs inside bt_accept_dequeue(), and CVE-2025-39860 serialises the userspace close()/accept() race by calling cleanup_listen() under lock_sock() in l2cap_sock_release(). Neither covers l2cap_conn_del() running from hci_rx_work, so this UAF still reproduces on current bluetooth/master.

Take the reference at the source: bt_accept_dequeue() does sock_hold() while sk is still locked, before release_sock(); callers sock_put(). cleanup_listen() pins the chan with l2cap_chan_hold_unless_zero() under a brief child sk lock (serialising vs l2cap_sock_teardown_cb()), drops it before l2cap_chan_lock(), and skips a duplicate l2cap_sock_kill() on SOCK_DEAD. conn->lock is not taken here: cleanup_listen() runs under the parent sk lock and that would invert
conn->lock -> chan->lock -> sk_lock (lockdep).

KASAN/SMP: an unprivileged listen/close vs HCI-disconnect race produced 12 use-after-free reports per run before this change; 0, and no lockdep report, over 1600+ raced iterations after it on bluetooth/master.

Fixes 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced

Credit Based Mode")

---
Hi Luiz,

v4 - rebased on current bluetooth/master (after e83f5e24d "serialize accept_q access"); the af_bluetooth.c hunk now sits in the reworked bt_accept_dequeue(). This residual (cleanup_listen vs l2cap_conn_del, not covered by e83f5e24d nor CVE-2025-39860) is unchanged from v3 and re-verified with KASAN on bluetooth/master: 12 UAF/run -> 0, no lockdep, over 1600+ raced iterations.

Changes since v3: rebased onto e83f5e24d; commit message notes why e83f5e24d/CVE-2025-39860 do not cover this path.
Changes since v2: fix at the source in bt_accept_dequeue() + chan lifetime via l2cap_chan_hold_unless_zero(); no conn->lock (lockdep). Changes since v1: consistent From/Signed-off-by.

 net/bluetooth/af_bluetooth.c | 10 +++++++
 net/bluetooth/iso.c          |  9 ++++++-
 net/bluetooth/l2cap_sock.c   | 51 +++++++++++++++++++++++++++++++-----
 net/bluetooth/rfcomm/sock.c  |  9 ++++++-
 net/bluetooth/sco.c          |  9 ++++++-
 5 files changed, 78 insertions(+), 10 deletions(-)
net/bluetooth/af_bluetooth.c +10 -0
index 9d68dd860..1a6aa3f8d 100644
@@ -340,6 +340,16 @@ struct sock *bt_accept_dequeue(struct sock 
 *parent, struct socket *newsock)
 			if (newsock)
 				sock_graft(sk, newsock);
 
+			/* Hand the caller a reference taken while sk is
+			 * still locked.  bt_accept_unlink() just dropped
+			 * the accept-queue reference; without this hold a
+			 * concurrent teardown (e.g. l2cap_conn_del() ->
+			 * l2cap_sock_kill()) could free sk between
+			 * release_sock() and the caller using it.  Every
+			 * caller drops this with sock_put() when done.
+			 */
+			sock_hold(sk);
+
 			release_sock(sk);
 			if (next)
net/bluetooth/af_bluetooth.c +0 -0
 				sock_put(next);
net/bluetooth/iso.c +8 -1
index 7cb2864fe..812f9002d 100644
@@ -751,6 +751,8 @@ static void iso_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		iso_sock_close(sk);
 		iso_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	/* If listening socket has a hcon, properly disconnect it */
@@ -1356,8 +1358,13 @@ static int iso_sock_accept(struct socket *sock, 
 struct socket *newsock,
 		}
 
 		ch = bt_accept_dequeue(sk, newsock);
-		if (ch)
+		if (ch) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps ch alive from here.
+			 */
+			sock_put(ch);
 			break;
+		}
 
 		if (!timeo) {
net/bluetooth/iso.c +0 -0
 			err = -EAGAIN;
net/bluetooth/l2cap_sock.c +6 -1
index cf590a67d..b34e7da8d 100644
@@ -349,8 +349,13 @@ static int l2cap_sock_accept(struct socket *sock, 
 struct socket *newsock,
 		}
 
 		nsk = bt_accept_dequeue(sk, newsock);
-		if (nsk)
+		if (nsk) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps nsk alive from here.
+			 */
+			sock_put(nsk);
 			break;
+		}
 
 		if (!timeo) {
net/bluetooth/l2cap_sock.c +38 -6
 			err = -EAGAIN;
@@ -1475,22 +1480,54 @@ static void l2cap_sock_cleanup_listen(struct 
 sock *parent)
 	BT_DBG("parent %p state %s", parent,
 	       state_to_string(parent->sk_state));
 
-	/* Close not yet accepted channels */
+	/* Close not yet accepted channels.
+	 *
+	 * bt_accept_dequeue() now returns sk with an extra reference held
+	 * (taken while sk was still locked) so a concurrent l2cap_conn_del()
+	 * -> l2cap_sock_kill() cannot free sk under us.
+	 *
+	 * cleanup_listen() runs under the parent sk lock, so unlike
+	 * l2cap_sock_shutdown() we must NOT take conn->lock here: that would
+	 * establish sk_lock -> conn->lock and invert the established
+	 * conn->lock -> chan->lock -> sk_lock order (lockdep deadlock).
+	 *
+	 * Instead, briefly take the child sk lock to fetch and pin its chan.
+	 * l2cap_conn_del() reaches the chan free only via
+	 * l2cap_chan_del() -> l2cap_sock_teardown_cb(), which itself takes
+	 * the child sk lock; holding it across l2cap_chan_hold_unless_zero()
+	 * therefore guarantees the chan cannot be freed while we read and
+	 * pin it (hold_unless_zero() additionally skips a chan already past
+	 * its last reference).  We then drop the sk lock before taking
+	 * chan->lock, so sk and chan locks are never held together.
+	 */
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
-		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+		struct l2cap_chan *chan;
+
+		lock_sock_nested(sk, L2CAP_NESTING_NORMAL);
+		chan = l2cap_chan_hold_unless_zero(l2cap_pi(sk)->chan);
+		release_sock(sk);
+		if (!chan) {
+			/* l2cap_conn_del() already tearing this child down */
+			sock_put(sk);
+			continue;
+		}
 
 		BT_DBG("child chan %p state %s", chan,
 		       state_to_string(chan->state));
 
-		l2cap_chan_hold(chan);
 		l2cap_chan_lock(chan);
-
 		__clear_chan_timer(chan);
 		l2cap_chan_close(chan, ECONNRESET);
-		l2cap_sock_kill(sk);
-
+		/* l2cap_conn_del() may already have killed this socket
+		 * (it sets SOCK_DEAD); skip the duplicate to avoid a
+		 * double sock_put()/l2cap_chan_put().
+		 */
+		if (!sock_flag(sk, SOCK_DEAD))
+			l2cap_sock_kill(sk);
 		l2cap_chan_unlock(chan);
+
 		l2cap_chan_put(chan);
+		sock_put(sk);
 	}
 }
net/bluetooth/rfcomm/sock.c +8 -1
index be6639cd6..bd7d959c6 100644
@@ -180,6 +180,8 @@ static void rfcomm_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		rfcomm_sock_close(sk);
 		rfcomm_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	parent->sk_state  = BT_CLOSED;
@@ -497,8 +499,13 @@ static int rfcomm_sock_accept(struct socket *sock, 
 struct socket *newsock,
 		}
 
 		nsk = bt_accept_dequeue(sk, newsock);
-		if (nsk)
+		if (nsk) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps nsk alive from here.
+			 */
+			sock_put(nsk);
 			break;
+		}
 
 		if (!timeo) {
net/bluetooth/rfcomm/sock.c +0 -0
 			err = -EAGAIN;
net/bluetooth/sco.c +8 -1
index eba44525d..f1799c6a6 100644
@@ -502,6 +502,8 @@ static void sco_sock_cleanup_listen(struct sock *parent)
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		sco_sock_close(sk);
 		sco_sock_kill(sk);
+		/* Drop the reference handed back by bt_accept_dequeue(). */
+		sock_put(sk);
 	}
 
 	parent->sk_state  = BT_CLOSED;
@@ -765,8 +767,13 @@ static int sco_sock_accept(struct socket *sock, 
 struct socket *newsock,
 		}
 
 		ch = bt_accept_dequeue(sk, newsock);
-		if (ch)
+		if (ch) {
+			/* Drop the bridging ref from bt_accept_dequeue();
+			 * the grafted socket keeps ch alive from here.
+			 */
+			sock_put(ch);
 			break;
+		}
 
 		if (!timeo) {
Quoted 3 lines

err = -EAGAIN;

Signature · 1 line
2.34.1

I reported the same issue privately to the maintainers on April 11th. The fix looks correct for the sk-lifetime UAF.

This patch leaves the conn->chan_l list-corruption race open (l2cap_chan_close without conn->lock). I'll send a follow-up patch on top that addresses it.

Best,
Siwei

#9 lore raw

Re: [PATCH v4] Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

In reply to Safa Karakuş
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Sat, 16 May 2026 21:15:04 +0300 you wrote:

bt_accept_dequeue() unlinks a not-yet-accepted child from the parent accept queue and release_sock()s it before returning, so the returned sk has no caller reference and is unlocked.

l2cap_sock_cleanup_listen() walks these children on listening-socket close. A concurrent HCI disconnect drives hci_rx_work -> l2cap_conn_del() which runs l2cap_chan_del() + l2cap_sock_kill() and frees the child sk and its l2cap_chan; cleanup_listen() then uses both:

[...]

Here is the summary with links:
- [v4] Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()

    https://git.kernel.org/bluetooth/bluetooth-next/c/0b580042a1a5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#10 lore raw

PATCH 1/1 Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

In reply to Safa Karakuş

l2cap_chan_close() removes the channel from conn->chan_l, which must be done under conn->lock. cleanup_listen() runs under the parent sk_lock, so acquiring conn->lock would invert the established conn->lock -> chan->lock -> sk_lock order.

Instead of calling l2cap_chan_close() directly, schedule l2cap_chan_timeout with delay 0 to close the channel asynchronously. The timeout handler already acquires conn->lock and chan->lock in the correct order.

The timer is only armed when chan->conn is still set: if it is already NULL, l2cap_conn_del() has already processed this channel (l2cap_chan_del + l2cap_sock_teardown_cb + l2cap_sock_close_cb), so there is nothing left to do. If l2cap_conn_del() races in after the timer is armed, __clear_chan_timer() inside l2cap_chan_del() cancels it; if the timer has already fired, the handler returns harmlessly because chan->conn was cleared.

Fixes 3df91ea20e74 ("Bluetooth: Revert to mutexes from RCU list")
---
 net/bluetooth/l2cap_sock.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
net/bluetooth/l2cap_sock.c +9 -7
index 4ed745a9c2cf..025329636353 100644
@@ -1512,6 +1512,10 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 	 * pin it (hold_unless_zero() additionally skips a chan already past
 	 * its last reference).  We then drop the sk lock before taking
 	 * chan->lock, so sk and chan locks are never held together.
+	 *
+	 * Since we cannot call l2cap_chan_close() without conn->lock,
+	 * schedule l2cap_chan_timeout to close the channel; it already
+	 * acquires conn->lock -> chan->lock in the correct order.
 	 */
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		struct l2cap_chan *chan;
@@ -1529,14 +1533,12 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 		       state_to_string(chan->state));
 
 		l2cap_chan_lock(chan);
-		__clear_chan_timer(chan);
-		l2cap_chan_close(chan, ECONNRESET);
-		/* l2cap_conn_del() may already have killed this socket
-		 * (it sets SOCK_DEAD); skip the duplicate to avoid a
-		 * double sock_put()/l2cap_chan_put().
+		/* Since we cannot call l2cap_chan_close() without
+		 * conn->lock, schedule its timer to trigger the close
+		 * and cleanup of this channel.
 		 */
-		if (!sock_flag(sk, SOCK_DEAD))
-			l2cap_sock_kill(sk);
+		if (chan->conn)
+			__set_chan_timer(chan, 0);
 		l2cap_chan_unlock(chan);
 
 		l2cap_chan_put(chan);
Signature · 1 line
2.54.0
#11 lore raw

Re: [PATCH] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

Hi Siwei,

Quoted 48 lines · Siwei Zhang

l2cap_chan_close() removes the channel from conn->chan_l, which must be done under conn->lock. cleanup_listen() runs under the parent sk_lock, so acquiring conn->lock would invert the established conn->lock -> chan->lock -> sk_lock order.

Instead of calling l2cap_chan_close() directly, schedule l2cap_chan_timeout with delay 0 to close the channel asynchronously. The timeout handler already acquires conn->lock and chan->lock in the correct order.

The timer is only armed when chan->conn is still set: if it is already NULL, l2cap_conn_del() has already processed this channel (l2cap_chan_del + l2cap_sock_teardown_cb + l2cap_sock_close_cb), so there is nothing left to do. If l2cap_conn_del() races in after the timer is armed, __clear_chan_timer() inside l2cap_chan_del() cancels it; if the timer has already fired, the handler returns harmlessly because chan->conn was cleared.

Fixes 3df91ea20e74 ("Bluetooth: Revert to mutexes from RCU list")
---
 net/bluetooth/l2cap_sock.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
net/bluetooth/l2cap_sock.c +4 -3
index 4ed745a9c2cf..025329636353 100644
@@ -1512,6 +1512,10 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
         * pin it (hold_unless_zero() additionally skips a chan already past
         * its last reference).  We then drop the sk lock before taking
         * chan->lock, so sk and chan locks are never held together.
+        *
+        * Since we cannot call l2cap_chan_close() without conn->lock,
+        * schedule l2cap_chan_timeout to close the channel; it already
+        * acquires conn->lock -> chan->lock in the correct order.
         */
        while ((sk = bt_accept_dequeue(parent, NULL))) {
                struct l2cap_chan *chan;
@@ -1529,14 +1533,12 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
                       state_to_string(chan->state));
 
                l2cap_chan_lock(chan);
-               __clear_chan_timer(chan);
-               l2cap_chan_close(chan, ECONNRESET);
-               /* l2cap_conn_del() may already have killed this socket

net/bluetooth/l2cap_sock.c static void l2cap_sock_cleanup_listen(struct sock *parent) · line 1541

Quoted 10 lines
net/bluetooth/l2cap_sock.c +5 -4
-                * (it sets SOCK_DEAD); skip the duplicate to avoid a
-                * double sock_put()/l2cap_chan_put().
+               /* Since we cannot call l2cap_chan_close() without
+                * conn->lock, schedule its timer to trigger the close
+                * and cleanup of this channel.
                 */
-               if (!sock_flag(sk, SOCK_DEAD))
-                       l2cap_sock_kill(sk);
+               if (chan->conn)
+                       __set_chan_timer(chan, 0);

Great that seems a lot easier to understand than the previous changes. I just don't quite follow why you are removing SOCK_DEAD handling with this?

Quoted 6 lines
net/bluetooth/l2cap_sock.c +0 -1
                l2cap_chan_unlock(chan);
 
                l2cap_chan_put(chan);
--

2.54.0

Signature · 1 line
Luiz Augusto von Dentz
#12 lore raw

Re: [PATCH] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

Hi Luiz,

Quoted 57 lines · Luiz Augusto von Dentz

Hi Siwei,

Quoted 54 lines · Siwei Zhang

l2cap_chan_close() removes the channel from conn->chan_l, which must be done under conn->lock. cleanup_listen() runs under the parent sk_lock, so acquiring conn->lock would invert the established conn->lock -> chan->lock -> sk_lock order.

Instead of calling l2cap_chan_close() directly, schedule l2cap_chan_timeout with delay 0 to close the channel asynchronously. The timeout handler already acquires conn->lock and chan->lock in the correct order.

The timer is only armed when chan->conn is still set: if it is already NULL, l2cap_conn_del() has already processed this channel (l2cap_chan_del + l2cap_sock_teardown_cb + l2cap_sock_close_cb), so there is nothing left to do. If l2cap_conn_del() races in after the timer is armed, __clear_chan_timer() inside l2cap_chan_del() cancels it; if the timer has already fired, the handler returns harmlessly because chan->conn was cleared.

Fixes 3df91ea20e74 ("Bluetooth: Revert to mutexes from RCU list")
---
 net/bluetooth/l2cap_sock.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
net/bluetooth/l2cap_sock.c +7 -5
index 4ed745a9c2cf..025329636353 100644
@@ -1512,6 +1512,10 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
         * pin it (hold_unless_zero() additionally skips a chan already past
         * its last reference).  We then drop the sk lock before taking
         * chan->lock, so sk and chan locks are never held together.
+        *
+        * Since we cannot call l2cap_chan_close() without conn->lock,
+        * schedule l2cap_chan_timeout to close the channel; it already
+        * acquires conn->lock -> chan->lock in the correct order.
         */
        while ((sk = bt_accept_dequeue(parent, NULL))) {
                struct l2cap_chan *chan;
@@ -1529,14 +1533,12 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
                       state_to_string(chan->state));
 
                l2cap_chan_lock(chan);
-               __clear_chan_timer(chan);
-               l2cap_chan_close(chan, ECONNRESET);
-               /* l2cap_conn_del() may already have killed this socket
-                * (it sets SOCK_DEAD); skip the duplicate to avoid a
-                * double sock_put()/l2cap_chan_put().
+               /* Since we cannot call l2cap_chan_close() without
+                * conn->lock, schedule its timer to trigger the close
+                * and cleanup of this channel.
                 */
Quoted 9 lines

net/bluetooth/l2cap_sock.c static void l2cap_sock_cleanup_listen(struct sock *parent) · line 1541

Quoted 4 lines
net/bluetooth/l2cap_sock.c +2 -2
-               if (!sock_flag(sk, SOCK_DEAD))
-                       l2cap_sock_kill(sk);
+               if (chan->conn)
+                       __set_chan_timer(chan, 0);

Great that seems a lot easier to understand than the previous changes. I just don't quite follow why you are removing SOCK_DEAD handling with this?

l2cap_sock_cleanup_listen() [holds: parent sk_lock]

    bt_accept_dequeue()                           → returns child sk + ref
    lock_sock(child sk)
    l2cap_chan_hold_unless_zero(chan)
    release_sock(child sk)
    l2cap_chan_lock(chan)
    __set_chan_timer(chan, 0)                      → schedules delayed work
    l2cap_chan_unlock(chan)
    l2cap_chan_put(chan)
    sock_put(sk)
        ↓ (deferred, on workqueue)
    l2cap_chan_timeout()                           
      mutex_lock(conn->lock)                      ← proper lock order
      l2cap_chan_lock(chan)
      l2cap_chan_close(chan, reason)
        l2cap_chan_del(chan, reason)
          chan->ops->teardown(chan, err)           → l2cap_sock_teardown_cb()
            lock_sock(child sk)
            sk->sk_state = BT_CLOSED
            sock_set_flag(sk, SOCK_ZAPPED)
            release_sock(sk)
      chan->ops->close(chan)                       → l2cap_sock_close_cb()
        l2cap_sock_kill(sk)
          chan->data = NULL
          l2cap_chan_put(chan)
          sock_set_flag(sk, SOCK_DEAD)
          sock_put(sk)

l2cap_chan_timeout() will do the l2cap_sock_kill() job for us. Previously in Safa's patch, because that does not have conn-lock protection, which might result in double kill so there is a defensive check. In our version, this is totally not required.

Quoted 10 lines
Quoted 6 lines
net/bluetooth/l2cap_sock.c +0 -1
                l2cap_chan_unlock(chan);
 
                l2cap_chan_put(chan);
--

2.54.0

Signature · 1 line
Luiz Augusto von Dentz

Best,
Siwei

#13 lore raw

PATCH v2 1/1 Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

In reply to Luiz Augusto von Dentz

l2cap_chan_close() removes the channel from conn->chan_l, which must be done under conn->lock. cleanup_listen() runs under the parent sk_lock, so acquiring conn->lock would invert the established conn->lock -> chan->lock -> sk_lock order.

Instead of calling l2cap_chan_close() directly, schedule l2cap_chan_timeout with delay 0 to close the channel asynchronously. The timeout handler already acquires conn->lock and chan->lock in the correct order.

The timer is only armed when chan->conn is still set: if it is already NULL, l2cap_conn_del() has already processed this channel (l2cap_chan_del + l2cap_sock_teardown_cb + l2cap_sock_close_cb), so there is nothing left to do. If l2cap_conn_del() races in after the timer is armed, __clear_chan_timer() inside l2cap_chan_del() cancels it; if the timer has already fired, the handler returns harmlessly because chan->conn was cleared.

Fixes 3df91ea20e74 ("Bluetooth: Revert to mutexes from RCU list")
Cc <[email protected]> # 0b58004: Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()
---
 net/bluetooth/l2cap_sock.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
net/bluetooth/l2cap_sock.c +9 -7
index 4ed745a9c2cf..025329636353 100644
@@ -1512,6 +1512,10 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 	 * pin it (hold_unless_zero() additionally skips a chan already past
 	 * its last reference).  We then drop the sk lock before taking
 	 * chan->lock, so sk and chan locks are never held together.
+	 *
+	 * Since we cannot call l2cap_chan_close() without conn->lock,
+	 * schedule l2cap_chan_timeout to close the channel; it already
+	 * acquires conn->lock -> chan->lock in the correct order.
 	 */
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		struct l2cap_chan *chan;
@@ -1529,14 +1533,12 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 		       state_to_string(chan->state));
 
 		l2cap_chan_lock(chan);
-		__clear_chan_timer(chan);
-		l2cap_chan_close(chan, ECONNRESET);
-		/* l2cap_conn_del() may already have killed this socket
-		 * (it sets SOCK_DEAD); skip the duplicate to avoid a
-		 * double sock_put()/l2cap_chan_put().
+		/* Since we cannot call l2cap_chan_close() without
+		 * conn->lock, schedule its timer to trigger the close
+		 * and cleanup of this channel.
 		 */
-		if (!sock_flag(sk, SOCK_DEAD))
-			l2cap_sock_kill(sk);
+		if (chan->conn)
+			__set_chan_timer(chan, 0);
 		l2cap_chan_unlock(chan);
 
 		l2cap_chan_put(chan);
Signature · 1 line
2.54.0
#14 lore raw

RE: [v2] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

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/l2cap_sock.c:1512
error: net/bluetooth/l2cap_sock.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

#15 lore raw

Re: [v2] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

Quoted 15 lines · On Wed, May 20, 2026, at 11:57 PM, [email protected] wrote:

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/l2cap_sock.c:1512
error: net/bluetooth/l2cap_sock.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Quoted 6 lines

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

I think it is false positive as I rebased against latest bluetooth-next.

Best,
Siwei

#16 lore raw

RE: [v2] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

In reply to Siwei Zhang

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/project/bluetooth/list/?series=1098389

---Test result---

Test Summary:

CheckPatch                    PASS      0.66 seconds
GitLint                       FAIL      0.28 seconds
SubjectPrefix                 PASS      0.10 seconds
BuildKernel                   PASS      25.36 seconds
CheckAllWarning               PASS      28.35 seconds
CheckSparse                   PASS      26.31 seconds
BuildKernel32                 PASS      24.49 seconds
TestRunnerSetup               PASS      525.81 seconds
TestRunner_l2cap-tester       FAIL      59.03 seconds
IncrementalBuild              PASS      24.10 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v2] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
21: B1 Line exceeds max length (109>80): "Cc: <[email protected]> # 0b58004: Bluetooth: fix UAF in l2cap_sock_cleanup_listen() vs l2cap_conn_del()"
##############################
Test: TestRunner_l2cap-tester - FAIL
Desc: Run l2cap-tester with test-runner
Output:
Total: 96, Passed: 95 (99.0%), Failed: 1, Not Run: 0

Failed Test Cases
L2CAP BR/EDR Server - Set PHY 1M Failed 0.261 seconds

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

---
Regards,
Linux Bluetooth

#17 lore raw

Re: [PATCH v2] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

In reply to Siwei Zhang
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 11 lines · On Wed, 20 May 2026 22:12:20 -0400 you wrote:

l2cap_chan_close() removes the channel from conn->chan_l, which must be done under conn->lock. cleanup_listen() runs under the parent sk_lock, so acquiring conn->lock would invert the established conn->lock -> chan->lock -> sk_lock order.

Instead of calling l2cap_chan_close() directly, schedule l2cap_chan_timeout with delay 0 to close the channel asynchronously. The timeout handler already acquires conn->lock and chan->lock in the correct order.

[...]

Here is the summary with links:
- [v2] Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

    https://git.kernel.org/bluetooth/bluetooth-next/c/75780ca4c6a8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#18 lore raw

RE: Bluetooth: L2CAP: use chan timer to close channels in cleanup_listen()

In reply to Siwei Zhang

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/l2cap_sock.c:1512
error: net/bluetooth/l2cap_sock.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