* [PATCH] Bluetooth: L2CAP: Fix use-after-free in l2cap_chan_timeout()
@ 2026-03-19 15:14 Hyunwoo Kim
2026-03-19 15:55 ` Luiz Augusto von Dentz
2026-03-19 16:36 ` bluez.test.bot
0 siblings, 2 replies; 3+ messages in thread
From: Hyunwoo Kim @ 2026-03-19 15:14 UTC (permalink / raw)
To: marcel, johan.hedberg, luiz.dentz; +Cc: linux-bluetooth, imv4bel
l2cap_chan_timeout() reads chan->conn without holding any lock and
without taking a reference on the connection. If l2cap_conn_del()
frees the connection concurrently, mutex_lock(&conn->lock) operates
on freed memory. The existing NULL check is insufficient as it cannot
prevent the connection from being freed after the check passes, and
the early return also leaks the channel reference held by the timer.
Fix by reading chan->conn under l2cap_chan_lock() and holding the
connection with l2cap_conn_get(). After acquiring locks in the correct
order (conn->lock then chan->lock), re-verify chan->conn in case
l2cap_conn_del() already cleaned up the channel.
Fixes: 3df91ea20e74 ("Bluetooth: Revert to mutexes from RCU list")
Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
---
net/bluetooth/l2cap_core.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index abd091155d04..a432e94281dc 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -406,13 +406,20 @@ static void l2cap_chan_timeout(struct work_struct *work)
{
struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
chan_timer.work);
- struct l2cap_conn *conn = chan->conn;
+ struct l2cap_conn *conn;
int reason;
BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
- if (!conn)
+ l2cap_chan_lock(chan);
+ conn = chan->conn;
+ if (!conn) {
+ l2cap_chan_unlock(chan);
+ l2cap_chan_put(chan);
return;
+ }
+ l2cap_conn_get(conn);
+ l2cap_chan_unlock(chan);
mutex_lock(&conn->lock);
/* __set_chan_timer() calls l2cap_chan_hold(chan) while scheduling
@@ -420,6 +427,17 @@ static void l2cap_chan_timeout(struct work_struct *work)
*/
l2cap_chan_lock(chan);
+ /* Recheck since l2cap_conn_del() may have cleaned up the channel
+ * while we were waiting for conn->lock.
+ */
+ if (!chan->conn) {
+ l2cap_chan_unlock(chan);
+ l2cap_chan_put(chan);
+ mutex_unlock(&conn->lock);
+ l2cap_conn_put(conn);
+ return;
+ }
+
if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
reason = ECONNREFUSED;
else if (chan->state == BT_CONNECT &&
@@ -436,6 +454,7 @@ static void l2cap_chan_timeout(struct work_struct *work)
l2cap_chan_put(chan);
mutex_unlock(&conn->lock);
+ l2cap_conn_put(conn);
}
struct l2cap_chan *l2cap_chan_create(void)
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] Bluetooth: L2CAP: Fix use-after-free in l2cap_chan_timeout()
2026-03-19 15:14 [PATCH] Bluetooth: L2CAP: Fix use-after-free in l2cap_chan_timeout() Hyunwoo Kim
@ 2026-03-19 15:55 ` Luiz Augusto von Dentz
2026-03-19 16:36 ` bluez.test.bot
1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-03-19 15:55 UTC (permalink / raw)
To: Hyunwoo Kim; +Cc: marcel, johan.hedberg, linux-bluetooth
Hi Hyunwoo,
On Thu, Mar 19, 2026 at 11:14 AM Hyunwoo Kim <imv4bel@gmail.com> wrote:
>
> l2cap_chan_timeout() reads chan->conn without holding any lock and
> without taking a reference on the connection. If l2cap_conn_del()
> frees the connection concurrently, mutex_lock(&conn->lock) operates
> on freed memory. The existing NULL check is insufficient as it cannot
> prevent the connection from being freed after the check passes, and
> the early return also leaks the channel reference held by the timer.
>
> Fix by reading chan->conn under l2cap_chan_lock() and holding the
> connection with l2cap_conn_get(). After acquiring locks in the correct
> order (conn->lock then chan->lock), re-verify chan->conn in case
> l2cap_conn_del() already cleaned up the channel.
>
> Fixes: 3df91ea20e74 ("Bluetooth: Revert to mutexes from RCU list")
> Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
> ---
> net/bluetooth/l2cap_core.c | 23 +++++++++++++++++++++--
> 1 file changed, 21 insertions(+), 2 deletions(-)
>
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index abd091155d04..a432e94281dc 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -406,13 +406,20 @@ static void l2cap_chan_timeout(struct work_struct *work)
> {
> struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
> chan_timer.work);
> - struct l2cap_conn *conn = chan->conn;
> + struct l2cap_conn *conn;
> int reason;
>
> BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
>
> - if (!conn)
> + l2cap_chan_lock(chan);
> + conn = chan->conn;
> + if (!conn) {
> + l2cap_chan_unlock(chan);
> + l2cap_chan_put(chan);
> return;
> + }
> + l2cap_conn_get(conn);
> + l2cap_chan_unlock(chan);
>
> mutex_lock(&conn->lock);
> /* __set_chan_timer() calls l2cap_chan_hold(chan) while scheduling
> @@ -420,6 +427,17 @@ static void l2cap_chan_timeout(struct work_struct *work)
> */
> l2cap_chan_lock(chan);
>
> + /* Recheck since l2cap_conn_del() may have cleaned up the channel
> + * while we were waiting for conn->lock.
> + */
> + if (!chan->conn) {
> + l2cap_chan_unlock(chan);
> + l2cap_chan_put(chan);
> + mutex_unlock(&conn->lock);
> + l2cap_conn_put(conn);
> + return;
> + }
> +
> if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
> reason = ECONNREFUSED;
> else if (chan->state == BT_CONNECT &&
> @@ -436,6 +454,7 @@ static void l2cap_chan_timeout(struct work_struct *work)
> l2cap_chan_put(chan);
>
> mutex_unlock(&conn->lock);
> + l2cap_conn_put(conn);
> }
>
> struct l2cap_chan *l2cap_chan_create(void)
> --
> 2.43.0
Actually maybe this is because we don't use cancel_delayed_work_sync
to wait for the work to complete on l2cap_chan_del, actually that
should use disable_delayed_work_sync to prevent the timer from being
rearmed, and that should be done to all the timers since l2cap_chan is
about to be destroyed.
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 3+ messages in thread* RE: Bluetooth: L2CAP: Fix use-after-free in l2cap_chan_timeout()
2026-03-19 15:14 [PATCH] Bluetooth: L2CAP: Fix use-after-free in l2cap_chan_timeout() Hyunwoo Kim
2026-03-19 15:55 ` Luiz Augusto von Dentz
@ 2026-03-19 16:36 ` bluez.test.bot
1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2026-03-19 16:36 UTC (permalink / raw)
To: linux-bluetooth, imv4bel
[-- Attachment #1: Type: text/plain, Size: 2834 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/project/bluetooth/list/?series=1069344
---Test result---
Test Summary:
CheckPatch PENDING 0.43 seconds
GitLint PENDING 0.35 seconds
SubjectPrefix PASS 0.10 seconds
BuildKernel PASS 25.02 seconds
CheckAllWarning PASS 27.79 seconds
CheckSparse PASS 26.74 seconds
BuildKernel32 PASS 24.54 seconds
TestRunnerSetup PASS 530.44 seconds
TestRunner_l2cap-tester PASS 27.95 seconds
TestRunner_iso-tester FAIL 31.28 seconds
TestRunner_bnep-tester PASS 6.51 seconds
TestRunner_mgmt-tester FAIL 112.54 seconds
TestRunner_rfcomm-tester PASS 9.59 seconds
TestRunner_sco-tester FAIL 14.31 seconds
TestRunner_ioctl-tester PASS 10.19 seconds
TestRunner_mesh-tester FAIL 12.46 seconds
TestRunner_smp-tester PASS 10.69 seconds
TestRunner_userchan-tester PASS 6.99 seconds
IncrementalBuild PENDING 0.80 seconds
Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:
##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:
##############################
Test: TestRunner_iso-tester - FAIL
Desc: Run iso-tester with test-runner
Output:
BUG: KASAN: slab-use-after-free in le_read_features_complete+0x7e/0x2b0
Total: 141, Passed: 141 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner
Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4
Failed Test Cases
Read Exp Feature - Success Failed 0.106 seconds
##############################
Test: TestRunner_sco-tester - FAIL
Desc: Run sco-tester with test-runner
Output:
WARNING: possible circular locking dependency detected
BUG: sleeping function called from invalid context at net/core/sock.c:3782
Total: 30, Passed: 30 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner
Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 2.754 seconds
Mesh - Send cancel - 2 Timed out 2.000 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:
---
Regards,
Linux Bluetooth
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-03-19 16:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-19 15:14 [PATCH] Bluetooth: L2CAP: Fix use-after-free in l2cap_chan_timeout() Hyunwoo Kim
2026-03-19 15:55 ` Luiz Augusto von Dentz
2026-03-19 16:36 ` 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