Skip to contentSkip to conversation

Bluetooth: L2CAP: part 2 of l2cap_conn::chan_l locking fixes

PATCH Applied Pauli Virtanen 3 participants 8 messages Sep 1 – Sep 2

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

cover Bluetooth: L2CAP: part 2 of l2cap_conn::chan_l locking fixes

Rebase to fix conflict in remaining commits from series
https://lore.kernel.org/linux-bluetooth/[email protected]/

Fixed minor conflict vs. moved mutex_lock + l2cap_chan_lock in diff context
commit ddaccd985bb0 ("Bluetooth: L2CAP: fix out-of-bounds write in l2cap_ecred_connect")

No other changes than resolving that.

Pauli Virtanen (4):
Bluetooth: L2CAP: take lock for l2cap_chan_del in
l2cap_ecred_rsp_defer
Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks
Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn

 include/net/bluetooth/l2cap.h |  65 +++++++++++--------
 net/bluetooth/6lowpan.c       |   2 +
 net/bluetooth/l2cap_core.c    | 113 ++++++++++++++++++++++++++++++++--
 net/bluetooth/l2cap_sock.c    |   2 +
 4 files changed, 150 insertions(+), 32 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/4 Bluetooth: L2CAP: take lock for l2cap_chan_del in l2cap_ecred_rsp_defer

l2cap_ecred_rsp_defer() calls l2cap_chan_del without holding chan->lock, which ends up calling ops->teardown() with wrong lock context.

Fix by taking chan->lock in l2cap_ecred_rsp_defer(). AB-BA deadlocks between sibling l2cap_chan are avoided here via requiring l2cap_conn::lock to serialize all nested l2cap_chan locking on same nesting level.

In current code, there is no nested l2cap_chan locking on same nesting level, so we can add this new requirement.

Also return early from __l2cap_ecred_conn_rsp_defer() if chan did not have FLAG_DEFER_SETUP, as then no RSP shall be sent for it, to make sure SMP channels are excluded.

Also hold chan reference over l2cap_chan_del(), in case chan_l reference was the last.

---
 include/net/bluetooth/l2cap.h |  4 +++
 net/bluetooth/l2cap_core.c    | 60 +++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
include/net/bluetooth/l2cap.h +4 -0
index 2315a3993c7b..c7e642abe404 100644
@@ -758,6 +758,10 @@ enum {
  * otherwise considers all channels equal and will e.g. complain about a
  * connection oriented channel triggering SMP procedures or a listening
  * channel creating and locking a child channel.
+ *
+ * Lock nesting of channels at the same nesting level is allowed if the channels
+ * have the same l2cap_chan::conn and l2cap_chan::conn.lock is taken before the
+ * nested locks. l2cap_chan_try_sibling_lock() must be used.
  */
 enum {
 	L2CAP_NESTING_SMP,
net/bluetooth/l2cap_core.c +60 -0

106 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 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=1155667

---Test result---

Test Summary:

CheckPatch                    FAIL      4.93 seconds
VerifyFixes                   PASS      0.14 seconds
VerifySignedoff               PASS      0.14 seconds
GitLint                       PASS      1.38 seconds
SubjectPrefix                 PASS      0.52 seconds
BuildKernel                   PASS      26.12 seconds
CheckAllWarning               PASS      28.68 seconds
CheckSparse                   PASS      27.83 seconds
BuildKernel32                 PASS      25.41 seconds
CheckKernelLLVM               SKIP      0.00 seconds
TestRunnerSetup               PASS      465.23 seconds
TestRunner_l2cap-tester       PASS      65.20 seconds
TestRunner_6lowpan-tester     PASS      23.15 seconds
IncrementalBuild              PASS      40.54 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[2/4] Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
ERROR: "foo * bar" should be "foo *bar"
#154: FILE: include/net/bluetooth/l2cap.h:657:
+ int (*recv) (struct l2cap_chan * chan,

WARNING: Unnecessary space before function pointer arguments #154: FILE: include/net/bluetooth/l2cap.h:657:
+ int (*recv) (struct l2cap_chan * chan,

WARNING: Unnecessary space before function pointer arguments #156: FILE: include/net/bluetooth/l2cap.h:659:
+ void (*teardown) (struct l2cap_chan *chan, int err)

WARNING: Unnecessary space before function pointer arguments #158: FILE: include/net/bluetooth/l2cap.h:661:
+ void (*close) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #159: FILE: include/net/bluetooth/l2cap.h:662:
+ void (*state_change) (struct l2cap_chan *chan,

WARNING: Unnecessary space before function pointer arguments #161: FILE: include/net/bluetooth/l2cap.h:664:
+ void (*ready) (struct l2cap_chan *chan)

WARNING: Unnecessary space before function pointer arguments #164: FILE: include/net/bluetooth/l2cap.h:667:
+ void (*defer) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #165: FILE: include/net/bluetooth/l2cap.h:668:
+ void (*resume) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #166: FILE: include/net/bluetooth/l2cap.h:669:
+ void (*suspend) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #167: FILE: include/net/bluetooth/l2cap.h:670:
+ void (*set_shutdown) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #168: FILE: include/net/bluetooth/l2cap.h:671:
+ long (*get_sndtimeo) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #169: FILE: include/net/bluetooth/l2cap.h:672:
+ struct pid *(*get_peer_pid) (struct l2cap_chan *chan);

WARNING: Unnecessary space before function pointer arguments #170: FILE: include/net/bluetooth/l2cap.h:673:
+ struct sk_buff *(*alloc_skb) (struct l2cap_chan *chan,

ERROR: "foo * bar" should be "foo *bar"
#173: FILE: include/net/bluetooth/l2cap.h:676:
+ int (*filter) (struct l2cap_chan * chan,

WARNING: Unnecessary space before function pointer arguments #173: FILE: include/net/bluetooth/l2cap.h:676:
+ int (*filter) (struct l2cap_chan * chan,

total: 2 errors, 13 warnings, 0 checks, 272 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/14782632.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.

[3/4] Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks
WARNING: Unnecessary space before function pointer arguments #116: FILE: include/net/bluetooth/l2cap.h:663:
+ void (*close) (struct l2cap_chan *chan)

WARNING: Unnecessary space before function pointer arguments #125: FILE: include/net/bluetooth/l2cap.h:671:
+ void (*resume) (struct l2cap_chan *chan)

WARNING: Unnecessary space before function pointer arguments #129: FILE: include/net/bluetooth/l2cap.h:674:
+ void (*set_shutdown) (struct l2cap_chan *chan)

total: 0 errors, 3 warnings, 0 checks, 37 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/14782634.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.

[4/4] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn
WARNING: Assisted-by expects 'AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]' format #119:

Assisted-by deepseek-4-flash # finding the race condition

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/14782635.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: CheckKernelLLVM - SKIP
Desc: Build kernel with LLVM + context analysis
Output:
Clang not found

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

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH 2/4 Bluetooth: L2CAP: annotate locking for l2cap_chan_del()

In reply to Pauli Virtanen

Add context analysis annotations for chan->lock and chan->conn->lock involving l2cap_chan_del() usage.

Add necessary annotations and related lockdep_assert_held to callers.

Move struct l2cap_ops definition after struct l2cap_conn, so that the callbacks can be annotated.

In l2cap_chan_close_unlocked() we consider chan->conn->lock as locked even if chan->conn == NULL, to avoid needing to define separate __l2cap_chan_close/del for this NULL case.

---
 include/net/bluetooth/l2cap.h | 56 +++++++++++++++++++----------------
 net/bluetooth/6lowpan.c       |  2 ++
 net/bluetooth/l2cap_core.c    | 49 +++++++++++++++++++++++++-----
 3 files changed, 74 insertions(+), 33 deletions(-)
include/net/bluetooth/l2cap.h +30 -26
index c7e642abe404..3e1e2b36d7b6 100644
@@ -614,31 +614,6 @@ struct l2cap_chan {
 	struct mutex		lock;
 };
 
-struct l2cap_ops {
-	char			*name;
-
-	int			(*new_connection)(struct l2cap_chan *chan,
-						  struct l2cap_chan *new_chan);
-	int			(*recv) (struct l2cap_chan * chan,
-					 struct sk_buff *skb);
-	void			(*teardown) (struct l2cap_chan *chan, int err);
-	void			(*close) (struct l2cap_chan *chan);
-	void			(*state_change) (struct l2cap_chan *chan,
-						 int state, int err);
-	void			(*ready) (struct l2cap_chan *chan);
-	void			(*defer) (struct l2cap_chan *chan);
-	void			(*resume) (struct l2cap_chan *chan);
-	void			(*suspend) (struct l2cap_chan *chan);
-	void			(*set_shutdown) (struct l2cap_chan *chan);
-	long			(*get_sndtimeo) (struct l2cap_chan *chan);
-	struct pid		*(*get_peer_pid) (struct l2cap_chan *chan);
-	struct sk_buff		*(*alloc_skb) (struct l2cap_chan *chan,
-					       unsigned long hdr_len,
-					       unsigned long len, int nb);
-	int			(*filter) (struct l2cap_chan * chan,
-					   struct sk_buff *skb);
-};
-
 struct l2cap_conn {
 	struct hci_conn		*hcon;
 	struct hci_chan		*hchan;
@@ -674,6 +649,34 @@ struct l2cap_conn {
 	struct list_head	users;
 };
 
+struct l2cap_ops {
+	char			*name;
+
+	int			(*new_connection)(struct l2cap_chan *chan,
+						  struct l2cap_chan *new_chan);
+	int			(*recv) (struct l2cap_chan * chan,
+					 struct sk_buff *skb);
+	void			(*teardown) (struct l2cap_chan *chan, int err)
+					__must_hold(&chan->lock);
+	void			(*close) (struct l2cap_chan *chan);
+	void			(*state_change) (struct l2cap_chan *chan,
+						 int state, int err);
+	void			(*ready) (struct l2cap_chan *chan)
+					__must_hold(&chan->lock)
+					__must_hold(&chan->conn->lock);
+	void			(*defer) (struct l2cap_chan *chan);
+	void			(*resume) (struct l2cap_chan *chan);
+	void			(*suspend) (struct l2cap_chan *chan);
+	void			(*set_shutdown) (struct l2cap_chan *chan);
+	long			(*get_sndtimeo) (struct l2cap_chan *chan);
+	struct pid		*(*get_peer_pid) (struct l2cap_chan *chan);
+	struct sk_buff		*(*alloc_skb) (struct l2cap_chan *chan,
+					       unsigned long hdr_len,
+					       unsigned long len, int nb);
+	int			(*filter) (struct l2cap_chan * chan,
+					   struct sk_buff *skb);
+};
+
 struct l2cap_user {
 	struct list_head list;
 	int (*probe) (struct l2cap_conn *conn, struct l2cap_user *user);
@@ -983,7 +986,8 @@ void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
 typedef void (*l2cap_chan_func_t)(struct l2cap_chan *chan, void *data);
 void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
 		     void *data);
-void l2cap_chan_del(struct l2cap_chan *chan, int err);
+void l2cap_chan_del(struct l2cap_chan *chan, int err)
+	__must_hold(&chan->lock) __must_hold(&chan->conn->lock);
 void l2cap_send_conn_req(struct l2cap_chan *chan);
 
 struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn);
net/bluetooth/6lowpan.c +2 -0
index ddcdd2aff91f..836add41f5d1 100644
@@ -722,6 +722,8 @@ static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
 }
 
 static inline void chan_ready_cb(struct l2cap_chan *chan)
+	__must_hold(&chan->lock)
+	__must_hold(&chan->conn->lock)
 {
 	struct lowpan_btle_dev *dev;
 	bool new_netdev = false;
net/bluetooth/l2cap_core.c +42 -7

190 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

PATCH 3/4 Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks

In reply to Pauli Virtanen

Annotate current locking context for l2cap_ops callbacks.

---
 include/net/bluetooth/l2cap.h | 13 +++++++++----
 net/bluetooth/l2cap_core.c    |  1 +
 2 files changed, 10 insertions(+), 4 deletions(-)
include/net/bluetooth/l2cap.h +9 -4
index 3e1e2b36d7b6..efb9b7f422d1 100644
@@ -653,21 +653,26 @@ struct l2cap_ops {
 	char			*name;
 
 	int			(*new_connection)(struct l2cap_chan *chan,
-						  struct l2cap_chan *new_chan);
+						  struct l2cap_chan *new_chan)
+					__must_hold(&chan->lock)
+					__must_hold(&new_chan->lock);
 	int			(*recv) (struct l2cap_chan * chan,
 					 struct sk_buff *skb);
 	void			(*teardown) (struct l2cap_chan *chan, int err)
 					__must_hold(&chan->lock);
-	void			(*close) (struct l2cap_chan *chan);
+	void			(*close) (struct l2cap_chan *chan)
+					__must_hold(&chan->lock);
 	void			(*state_change) (struct l2cap_chan *chan,
 						 int state, int err);
 	void			(*ready) (struct l2cap_chan *chan)
 					__must_hold(&chan->lock)
 					__must_hold(&chan->conn->lock);
 	void			(*defer) (struct l2cap_chan *chan);
-	void			(*resume) (struct l2cap_chan *chan);
+	void			(*resume) (struct l2cap_chan *chan)
+					__must_hold(&chan->lock);
 	void			(*suspend) (struct l2cap_chan *chan);
-	void			(*set_shutdown) (struct l2cap_chan *chan);
+	void			(*set_shutdown) (struct l2cap_chan *chan)
+					__must_hold(&chan->lock);
 	long			(*get_sndtimeo) (struct l2cap_chan *chan);
 	struct pid		*(*get_peer_pid) (struct l2cap_chan *chan);
 	struct sk_buff		*(*alloc_skb) (struct l2cap_chan *chan,
net/bluetooth/l2cap_core.c +1 -0
index 219d92431be0..b7d5fa6f6a83 100644
@@ -4214,6 +4214,7 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,
 static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,
 					       struct l2cap_chan *pchan)
 	__must_hold(&conn->lock)
+	__must_hold(&pchan->lock)
 {
 	struct l2cap_chan *chan;
 
Signature · 1 line
2.55.0
#5 lore raw

PATCH 4/4 Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn

In reply to Pauli Virtanen

l2cap_chan may be linked to l2cap_conn at most once. This is assumed in several places, eg l2cap_chan_del cleanup.

There is a TOCTOU race where the invariant is violated:

    [Task 1]                          [Task 2]
    l2cap_chan_connect                l2cap_sock_bind
      l2cap_chan_lock                   lock_sock
      l2cap_state_change                if (sk->sk_state != BT_OPEN)
        chan->state = BT_CONNECT
        l2cap_sock_state_change_cb      chan->state = BT_BOUND
                                        sk->sk_state = BT_BOUND
          lock_sock <------------------ release_sock
          sk->sk_state = BT_CONNECT

l2cap_sock_connect() does not check sk->sk_state, so since chan->state is now BT_BOUND, subsequent connect() ends up with second __l2cap_chan_add.

Explicitly document and check the invariant in __l2cap_chan_add with WARN_ON_ONCE. The only callsite where it could be hit is l2cap_chan_connect, so add pre-check there to avoid relying on chan->state. chan->state read/write is not properly guarded currently so there can be other TOCTOUC problems.

Add l2cap_lock_chan in l2cap_sock_bind() to guard chan->state write.

Fixes b66774b48dd9 ("Bluetooth: L2CAP: Fix UAF in channel timeout by holding conn ref")
Assisted-by deepseek-4-flash # finding the race condition
---
 net/bluetooth/l2cap_core.c | 7 ++++++-
 net/bluetooth/l2cap_sock.c | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)
net/bluetooth/l2cap_core.c +6 -1
index b7d5fa6f6a83..a22edd2baf5e 100644
@@ -623,6 +623,10 @@ void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
 	       __le16_to_cpu(chan->psm), chan->dcid);
 
+	/* Caller must ensure l2cap_chan is linked to l2cap_conn only once */
+	if (WARN_ON_ONCE(chan->conn || test_bit(FLAG_DEL, &chan->flags)))
+		return;
+
 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
 
 	chan->conn = l2cap_conn_get(conn);
@@ -7623,7 +7627,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
 		}
 	}
 
-	if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
+	if ((cid && __l2cap_get_chan_by_dcid(conn, cid)) || chan->conn ||
+	    test_bit(FLAG_DEL, &chan->flags)) {
 		hci_conn_drop(hcon);
 		err = -EBUSY;
 		goto chan_unlock;
net/bluetooth/l2cap_sock.c +2 -0
index dee3025f0ec2..278adb05c4c9 100644
@@ -109,6 +109,7 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr_unsized *addr, i
 			return -EINVAL;
 	}
 
+	l2cap_chan_lock(chan);
 	lock_sock(sk);
 
 	if (sk->sk_state != BT_OPEN) {
@@ -174,6 +175,7 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr_unsized *addr, i
 
 done:
 	release_sock(sk);
+	l2cap_chan_unlock(chan);
 	return err;
 }
 
Signature · 1 line
2.55.0
#6 lore raw
In reply to Pauli Virtanen

ke, 2026-09-02 kello 00:04 +0300, Pauli Virtanen kirjoitti:

Quoted 7 lines

Rebase to fix conflict in remaining commits from series
https://lore.kernel.org/linux-bluetooth/[email protected]/

Fixed minor conflict vs. moved mutex_lock + l2cap_chan_lock in diff context
commit ddaccd985bb0 ("Bluetooth: L2CAP: fix out-of-bounds write in l2cap_ecred_connect")

Quoted 7 lines

No other changes than resolving that.

Pauli Virtanen (4):
Bluetooth: L2CAP: take lock for l2cap_chan_del in
l2cap_ecred_rsp_defer
Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks

Sashiko report comments that some callbacks in functions in l2cap_ops are not marked with __must_hold(chan->lock) annotation.

That comment is not correct, e.g. l2cap_chan_send is called from 6lowpan.c and smp.c without holding chan->lock, so the annotations cannot be added, it'd need solving locking issues first.

Quoted 7 lines

Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn

 include/net/bluetooth/l2cap.h |  65 +++++++++++--------
 net/bluetooth/6lowpan.c       |   2 +
 net/bluetooth/l2cap_core.c    | 113 ++++++++++++++++++++++++++++++++--
 net/bluetooth/l2cap_sock.c    |   2 +
 4 files changed, 150 insertions(+), 32 deletions(-)
Signature · 1 line
Pauli Virtanen
#7 lore raw
In reply to Pauli Virtanen
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

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

Quoted 8 lines · On Wed, 2 Sep 2026 00:04:32 +0300 you wrote:

Rebase to fix conflict in remaining commits from series
https://lore.kernel.org/linux-bluetooth/[email protected]/

Fixed minor conflict vs. moved mutex_lock + l2cap_chan_lock in diff context
commit ddaccd985bb0 ("Bluetooth: L2CAP: fix out-of-bounds write in l2cap_ecred_connect")

[...]

Here is the summary with links:
- [1/4] Bluetooth: L2CAP: take lock for l2cap_chan_del in l2cap_ecred_rsp_defer

    https://git.kernel.org/bluetooth/bluetooth-next/c/c6d60c24cd7b
  - [2/4] Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
    https://git.kernel.org/bluetooth/bluetooth-next/c/6873eb51dcdd
  - [3/4] Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks
    https://git.kernel.org/bluetooth/bluetooth-next/c/4149ba2a806c
  - [4/4] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn
    https://git.kernel.org/bluetooth/bluetooth-next/c/6696072ffe07

You are awesome, thank you!

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