Bluetooth: L2CAP: fix and annotate l2cap_conn::chan_l locking
Subsystems (2)
cover Bluetooth: L2CAP: fix and annotate l2cap_conn::chan_l locking
The intent in current code is that:
- l2cap_conn::chan_l read/write is guarded by l2cap_conn::lock
- l2cap_chan_del called with held l2cap_chan::lock (l2cap_sock.c uses this)
This is currently not done right in several code paths.
This series fixes the locking vs. the above, and adds context analysis annotations to make more sure it is systematic.
The last three patches fix some miscellaneous locking related issues.
Mostly straightforward, but two more complicated parts:
- race condition in acquiring l2cap_conn::lock & l2cap_chan::lock
- __l2cap_ecred_conn_rsp_defer(chan) may l2cap_chan_del other l2cap_chan
To deal with these, this series adds
- l2cap_chan_lock_conn / l2cap_chan_unlock_conn
- l2cap_chan_close_unlocked
- l2cap_chan_try_sibling_lock-
- new nesting rule for l2cap_conn::lock
The context analysis annotations for l2cap_chan_close() declare via context_unsafe() that __must_hold(&chan->conn->lock) even if chan->conn is NULL. This is OK for the current static locking analysis, doesn't affect code generation. It could be cleaner by adding separate l2cap_chan_del() variant assuming chan->conn == NULL, but playing loose here avoids such duplication.
LLM tools were used for patch review, catching some some corner cases before sending this series out. No code / commit messages are generated by them.
Finding the bugs here is mostly based on adding the context analysis annotations which make it obvious where appropriate locks are missing.
Pauli Virtanen (16):
Bluetooth: L2CAP: take chan->lock for l2cap_chan_add/ready/del
Bluetooth: L2CAP: add l2cap_chan_close_unlocked() and locking helpers
Bluetooth: L2CAP: fix race condition in l2cap_sock_shutdown()
Bluetooth: 6lowpan: use l2cap_chan_close_unlocked()
Bluetooth: L2CAP: remove unused l2cap_chan_close()
Bluetooth: 6lowpan: avoid concurrent peer_del() in
bt_6lowpan_disconnect
Bluetooth: L2CAP: hold conn->lock for __l2cap_ecred_conn_rsp_defer
Bluetooth: L2CAP: hold l2cap_conn::lock in l2cap_connect_cfm()
Bluetooth: L2CAP: add annotations for l2cap_chan list locking
Bluetooth: L2CAP: take lock for l2cap_chan_del in
l2cap_ecred_rsp_defer
Bluetooth: L2CAP: hold chan in l2cap_ecred_conn_rsp()
Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks
Bluetooth: L2CAP: make concurrent l2cap_set_timer() refcounting safe
Bluetooth: L2CAP: remove conditional locking from l2cap_connect()
Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn
include/net/bluetooth/l2cap.h | 100 ++++++++----- net/bluetooth/6lowpan.c | 44 +++--- net/bluetooth/l2cap_core.c | 257 +++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 95 +++++++------ 4 files changed, 384 insertions(+), 112 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/16 Bluetooth: L2CAP: take chan->lock for l2cap_chan_add/ready/del
chan->lock must be held for __l2cap_chan_add as eg. calls to l2cap_chan_close assume chan->conn writes are guarded by it.
It must be held for l2cap_chan_del() due to l2cap_sock.c:l2cap_chan_conn, l2cap_monitor_timeout, etc.
Similarly it should be held for l2cap_ops::ready (assumed in 6lowpan.c). Also teardown usually has chan->lock held, it should always have it held to have the same locking context.
The lock is not correctly held by l2cap_core in several places.
Add the missing locks for l2cap_chan_del/add/ready(), except in l2cap_ecred_rsp_defer() which needs separate fix as it needs lock nesting.
--- include/net/bluetooth/l2cap.h | 3 ++- net/bluetooth/l2cap_core.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
include/net/bluetooth/l2cap.h +2 -1
@@ -973,7 +973,8 @@ int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);void l2cap_chan_set_defaults (struct l2cap_chan *chan,struct l2cap_chan *pchan);int l2cap_ertm_init (struct l2cap_chan *chan);void l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan); -void __l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan); +void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) + __must_hold(&chan->lock);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);
net/bluetooth/l2cap_core.c +15 -0
@@ -665,7 +665,9 @@ void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)void l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan) {mutex_lock (&conn->lock); +l2cap_chan_lock (chan);__l2cap_chan_add (conn, chan); +l2cap_chan_unlock (chan);mutex_unlock (&conn->lock); }
@@ -4065,6 +4067,8 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,if (!chan)return NULL ; +l2cap_chan_lock (chan); +l2cap_chan_set_defaults (chan, pchan); chan->ops = pchan->ops;
@@ -4073,10 +4077,13 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,if (pchan->ops->new_connection && pchan->ops->new_connection (pchan, chan) <0 ) {l2cap_chan_del (chan,0 ); +l2cap_chan_unlock (chan);l2cap_chan_put (chan);return NULL ; } +l2cap_chan_unlock (chan); +return chan; }
@@ -5047,6 +5054,8 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, goto response_unlock; } +l2cap_chan_lock (chan); +bacpy (&chan->src, &conn->hcon->src);bacpy (&chan->dst, &conn->hcon->dst); chan->src_type =bdaddr_src_type (conn->hcon);
@@ -5079,6 +5088,8 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, result =L2CAP_CR_LE_SUCCESS ; } +l2cap_chan_unlock (chan); + response_unlock:l2cap_chan_unlock (pchan);l2cap_chan_put (pchan);
@@ -5271,6 +5282,8 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,continue ; } +l2cap_chan_lock (chan); +bacpy (&chan->src, &conn->hcon->src);bacpy (&chan->dst, &conn->hcon->dst); chan->src_type =bdaddr_src_type (conn->hcon);
@@ -5303,6 +5316,8 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, }else {l2cap_chan_ready (chan); } + +l2cap_chan_unlock (chan); } unlock:
Signature · 1 line
2.55.0
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=1153622
---Test result---
Test Summary:
CheckPatch FAIL 9.75 seconds VerifyFixes PASS 0.09 seconds VerifySignedoff PASS 0.09 seconds GitLint PASS 3.65 seconds SubjectPrefix PASS 1.36 seconds BuildKernel PASS 22.58 seconds CheckAllWarning PASS 25.40 seconds CheckSparse PASS 24.17 seconds BuildKernel32 PASS 22.14 seconds CheckKernelLLVM SKIP 0.00 seconds TestRunnerSetup PASS 379.29 seconds TestRunner_l2cap-tester PASS 58.14 seconds TestRunner_6lowpan-tester PASS 19.38 seconds IncrementalBuild PASS 74.33 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[02/16] Bluetooth: L2CAP: add l2cap_chan_close_unlocked() and locking helpers
WARNING: The commit message has 'syzkaller', perhaps it also needs a 'Fixes:' tag?
total: 0 errors, 1 warnings, 0 checks, 132 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/14774767.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.
[03/16] Bluetooth: L2CAP: fix race condition in l2cap_sock_shutdown()
WARNING: Reported-by: should be immediately followed by Closes: or Link: with a URL to the report #113:
total: 0 errors, 1 warnings, 0 checks, 31 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/14774781.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.
[11/16] Bluetooth: L2CAP: hold chan in l2cap_ecred_conn_rsp()
WARNING: Assisted-by expects 'AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]' format #104:
total: 0 errors, 1 warnings, 0 checks, 29 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/14774789.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.
[12/16] Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
ERROR: "foo * bar" should be "foo *bar"
#156: 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:657:
+ int (*recv) (struct l2cap_chan * chan,
WARNING: Unnecessary space before function pointer arguments #158: FILE: include/net/bluetooth/l2cap.h:659:
+ void (*teardown) (struct l2cap_chan *chan, int err)
WARNING: Unnecessary space before function pointer arguments #160: FILE: include/net/bluetooth/l2cap.h:661:
+ void (*close) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #161: FILE: include/net/bluetooth/l2cap.h:662:
+ void (*state_change) (struct l2cap_chan *chan,
WARNING: Unnecessary space before function pointer arguments #163: FILE: include/net/bluetooth/l2cap.h:664:
+ void (*ready) (struct l2cap_chan *chan)
WARNING: Unnecessary space before function pointer arguments #166: FILE: include/net/bluetooth/l2cap.h:667:
+ void (*defer) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #167: FILE: include/net/bluetooth/l2cap.h:668:
+ void (*resume) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #168: FILE: include/net/bluetooth/l2cap.h:669:
+ void (*suspend) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #169: FILE: include/net/bluetooth/l2cap.h:670:
+ void (*set_shutdown) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #170: FILE: include/net/bluetooth/l2cap.h:671:
+ long (*get_sndtimeo) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #171: FILE: include/net/bluetooth/l2cap.h:672:
+ struct pid *(*get_peer_pid) (struct l2cap_chan *chan);
WARNING: Unnecessary space before function pointer arguments #172: FILE: include/net/bluetooth/l2cap.h:673:
+ struct sk_buff *(*alloc_skb) (struct l2cap_chan *chan,
ERROR: "foo * bar" should be "foo *bar"
#175: FILE: include/net/bluetooth/l2cap.h:676:
+ int (*filter) (struct l2cap_chan * chan,
WARNING: Unnecessary space before function pointer arguments #175: 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/14774790.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.
[13/16] Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks
WARNING: Unnecessary space before function pointer arguments #118: FILE: include/net/bluetooth/l2cap.h:663:
+ void (*close) (struct l2cap_chan *chan)
WARNING: Unnecessary space before function pointer arguments #127: FILE: include/net/bluetooth/l2cap.h:671:
+ void (*resume) (struct l2cap_chan *chan)
WARNING: Unnecessary space before function pointer arguments #131: 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/14774791.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.
[16/16] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn
WARNING: Assisted-by expects 'AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]' format #121:
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/14774794.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/664
---
Regards,
Linux Bluetooth
PATCH 2/16 Bluetooth: L2CAP: add l2cap_chan_close_unlocked() and locking helpers
In reply to Pauli Virtanenl2cap_chan_close() requires holding chan->lock and chan->conn->lock if associated chan->conn exists, to guard eg. conn->chan_l. Taking the locks with right ordering requires handling a race condition.
Add helper function l2cap_chan_(un)lock_conn that do the locking right.
Add l2cap_chan_close_unlocked() that does not require locks to be held, as all callsites do this lock -> close -> unlock pattern.
--- include/net/bluetooth/l2cap.h | 17 ++++++++++ net/bluetooth/l2cap_core.c | 61 ++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 5 deletions(-)
include/net/bluetooth/l2cap.h +17 -0
@@ -962,6 +962,8 @@ int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid);struct l2cap_chan *l2cap_chan_create (void );void l2cap_chan_close (struct l2cap_chan *chan,int reason); +void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) + __must_not_hold(&chan->lock);int l2cap_chan_connect (struct l2cap_chan *chan,__le16 psm,u16 cid,bdaddr_t *dst,u8 dst_type,u16 timeout);int l2cap_chan_reconfigure (struct l2cap_chan *chan,__u16 mtu);
@@ -988,4 +990,19 @@ void l2cap_conn_put(struct l2cap_conn *conn);int l2cap_register_user (struct l2cap_conn *conn,struct l2cap_user *user);void l2cap_unregister_user (struct l2cap_conn *conn,struct l2cap_user *user); +bool l2cap_chan_lock_conn(struct l2cap_chan *chan) + __acquires(&chan->lock) __cond_acquires(true, &chan->conn->lock); + +/* Release macro for l2cap_chan_lock_conn, so context analysis understands it */ +#define l2cap_chan_unlock_conn (chan, conn_locked) \ + ({ \ + struct l2cap_chan *__chan = (chan); \ + struct l2cap_conn *__conn = __chan->conn; \ + l2cap_chan_unlock(__chan); \ + if (conn_locked) { \ + mutex_unlock(&__conn->lock); \ + l2cap_conn_put(__conn); \ + } \ + }) +#endif /* __L2CAP_H */
net/bluetooth/l2cap_core.c +56 -5
105 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 3/16 Bluetooth: L2CAP: fix race condition in l2cap_sock_shutdown()
In reply to Pauli Virtanenl2cap_sock_shutdown() has the race condition
[Task 1] [Task 2]
l2cap_sock_shutdown l2cap_sock_connect
l2cap_chan_lock l2cap_chan_connect
conn = ... /* == NULL*/
l2cap_chan_unlock ------------> l2cap_chan_lock
if (conn) /* false */
__l2cap_chan_add(conn, chan)
l2cap_chan_lock <-------------- l2cap_chan_unlock
l2cap_chan_close /* chan->conn->lock not held! */conn->lock protects conn->chan_l and is not properly held here.
Use the l2cap_chan_close_unlocked() helper that ensures conn->lock is held for l2cap_chan_close().
--- net/bluetooth/l2cap_sock.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-)
net/bluetooth/l2cap_sock.c +1 -18
@@ -1406,7 +1406,6 @@ static int l2cap_sock_shutdown(struct socket *sock, int how) {struct sock *sk = sock->sk;struct l2cap_chan *chan; -struct l2cap_conn *conn;int err =0 ;BT_DBG ("sock %p, sk %p, how %d" , sock, sk, how);
@@ -1463,23 +1462,7 @@ static int l2cap_sock_shutdown(struct socket *sock, int how) sk->sk_shutdown |=SEND_SHUTDOWN ;release_sock (sk); -l2cap_chan_lock (chan); -/* prevent conn structure from being freed */ - conn =l2cap_conn_hold_unless_zero (chan->conn); -l2cap_chan_unlock (chan); - -if (conn) -/* mutex lock must be taken before l2cap_chan_lock() */ -mutex_lock (&conn->lock); - -l2cap_chan_lock (chan); -l2cap_chan_close (chan,0 ); -l2cap_chan_unlock (chan); - -if (conn) { -mutex_unlock (&conn->lock); -l2cap_conn_put (conn); - } +l2cap_chan_close_unlocked (chan,0 );lock_sock (sk);
Signature · 1 line
2.55.0
PATCH 4/16 Bluetooth: 6lowpan: use l2cap_chan_close_unlocked()
In reply to Pauli Virtanen6lowpan.c is using l2cap_chan_close() without taking chan->conn->lock, so it may modify conn->chan_l without holding the guarding lock.
Fix the locking by using the l2cap_chan_close_unlocked() helper that acquires the necessary locks.
--- net/bluetooth/6lowpan.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
net/bluetooth/6lowpan.c +9 -18
@@ -921,9 +921,7 @@ static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)BT_DBG ("peer %p chan %p" , peer, peer->chan); -l2cap_chan_lock (peer->chan); -l2cap_chan_close (peer->chan,ENOENT ); -l2cap_chan_unlock (peer->chan); +l2cap_chan_close_unlocked (peer->chan,ENOENT );return 0 ; }
@@ -1025,9 +1023,9 @@ static void disconnect_all_peers(void)struct lowpan_peer *peer;int nchans; -/* l2cap_chan_close() cannot be called from RCU, and lock ordering -* chan->lock > devices_lock prevents taking write side lock, so copy -* then close. +/* l2cap_chan_close_unlocked() cannot be called from RCU, and lock +* ordering chan->lock > devices_lock prevents taking write side lock, +* so copy then close. */ rcu_read_lock ();
@@ -1062,9 +1060,7 @@ static void disconnect_all_peers(void)spin_unlock (&devices_lock);for (i =0 ; i < nchans; ++i) { -l2cap_chan_lock (chans[i]); -l2cap_chan_close (chans[i],ENOENT ); -l2cap_chan_unlock (chans[i]); +l2cap_chan_close_unlocked (chans[i],ENOENT );l2cap_chan_put (chans[i]); } }while (nchans);
@@ -1082,9 +1078,7 @@ static void do_enable_set(bool flag)mutex_lock (&set_lock);if (listen_chan) { -l2cap_chan_lock (listen_chan); -l2cap_chan_close (listen_chan,0 ); -l2cap_chan_unlock (listen_chan); +l2cap_chan_close_unlocked (listen_chan,0 );l2cap_chan_put (listen_chan); }
@@ -1132,9 +1126,7 @@ static ssize_t lowpan_control_write(struct file *fp,mutex_lock (&set_lock);if (listen_chan) { -l2cap_chan_lock (listen_chan); -l2cap_chan_close (listen_chan,0 ); -l2cap_chan_unlock (listen_chan); +l2cap_chan_close_unlocked (listen_chan,0 );l2cap_chan_put (listen_chan); listen_chan =NULL ; }
@@ -1303,10 +1295,9 @@ static void __exit bt_6lowpan_exit(void)debugfs_remove (lowpan_control_debugfs);if (listen_chan) { -l2cap_chan_lock (listen_chan); -l2cap_chan_close (listen_chan,0 ); -l2cap_chan_unlock (listen_chan); +l2cap_chan_close_unlocked (listen_chan,0 );l2cap_chan_put (listen_chan); + listen_chan =NULL ; }disconnect_devices ();
Signature · 1 line
2.55.0
PATCH 5/16 Bluetooth: L2CAP: remove unused l2cap_chan_close()
In reply to Pauli Virtanenl2cap_chan_close() is now unused, and l2cap_chan_close_unlocked() should be used instead.
Remove l2cap_chan_close().
--- include/net/bluetooth/l2cap.h | 1 - net/bluetooth/l2cap_core.c | 6 ------ 2 files changed, 7 deletions(-)
include/net/bluetooth/l2cap.h +0 -1
@@ -961,7 +961,6 @@ int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);int l2cap_add_scid (struct l2cap_chan *chan,__u16 scid);struct l2cap_chan *l2cap_chan_create (void ); -void l2cap_chan_close (struct l2cap_chan *chan,int reason); void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) __must_not_hold(&chan->lock); int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
net/bluetooth/l2cap_core.c +0 -6
@@ -880,12 +880,6 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) } } -void l2cap_chan_close (struct l2cap_chan *chan,int reason) -{ -__l2cap_chan_close (chan, reason); -} -EXPORT_SYMBOL (l2cap_chan_close); -/* Take chan->lock. If chan->conn is non-NULL, take new reference on it, take * chan->conn->lock, and return true. Otherwise return false. */
Signature · 1 line
2.55.0
PATCH 6/16 Bluetooth: 6lowpan: avoid concurrent peer_del() in bt_6lowpan_disconnect
In reply to Pauli Virtanenbt_6lowpan_disconnect() looks up and accesses peer->chan, without holding locks guaranteeing peer_del() cannot free the peer concurrently.
Take devices_lock to ensure peer can be dereferenced safely.
--- net/bluetooth/6lowpan.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
net/bluetooth/6lowpan.c +14 -3
@@ -912,16 +912,27 @@ static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)static int bt_6lowpan_disconnect (struct l2cap_conn *conn,u8 dst_type) {struct lowpan_peer *peer; +struct l2cap_chan *chan;BT_DBG ("conn %p dst type %u" , conn, dst_type); +spin_lock (&devices_lock); + peer =lookup_peer (conn); -if (!peer) +if (!peer) { +spin_unlock (&devices_lock);return -ENOENT ; + } -BT_DBG ("peer %p chan %p" , peer, peer->chan); + chan = peer->chan; +l2cap_chan_hold (chan); -l2cap_chan_close_unlocked (peer->chan,ENOENT ); +spin_unlock (&devices_lock); + +BT_DBG ("peer %p chan %p" , peer, chan); + +l2cap_chan_close_unlocked (chan,ENOENT ); +l2cap_chan_put (chan);return 0 ; }
Signature · 1 line
2.55.0
PATCH 7/16 Bluetooth: L2CAP: hold conn->lock for __l2cap_ecred_conn_rsp_defer
In reply to Pauli Virtanen__l2cap_ecred_conn_rsp_defer() > __l2cap_chan_list_id() accesses conn->chan_l which is guarded by conn->lock. The lock fails to be held when calling from l2cap_sock.c.
Fix by using l2cap_chan_conn_lock(), and taking the locks in required order l2cap_conn::lock > l2cap_chan::lock > sk. Leave fast path with sk->sk_state precheck. Move the L2CAP defer handling to l2cap_sock_defer().
The code should also take l2cap_chan_lock() for sibling channels, but that needs separate fix due to lock nesting.
--- net/bluetooth/l2cap_sock.c | 74 ++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 23 deletions(-)
net/bluetooth/l2cap_sock.c +51 -23
@@ -1243,6 +1243,54 @@ static void l2cap_publish_rx_avail(struct l2cap_chan *chan)l2cap_chan_rx_avail (chan,-1 ); } +static int l2cap_sock_defer (struct sock *sk) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; +bool have_conn; +int err =0 ; + +/* Fast path check */ +lock_sock (sk); +if (sk->sk_state !=BT_CONNECT2 ) { +release_sock (sk); +return 0 ; + } +release_sock (sk); + + have_conn =l2cap_chan_lock_conn (chan); +lock_sock (sk); + +if (sk->sk_state ==BT_CONNECT2 &&test_bit (BT_SK_DEFER_SETUP , + &bt_sk (sk)->flags)) { + err =1 ; + +if (!have_conn) { +release_sock (sk); + err = -ENOTCONN ; + }else if (chan->mode ==L2CAP_MODE_EXT_FLOWCTL ) { + sk->sk_state =BT_CONNECTED ; + chan->state =BT_CONNECTED ; +release_sock (sk); +__l2cap_ecred_conn_rsp_defer (chan); + }else if (bdaddr_type_is_le (chan->src_type)) { + sk->sk_state =BT_CONNECTED ; + chan->state =BT_CONNECTED ; +release_sock (sk); +__l2cap_le_connect_rsp_defer (chan); + }else { + sk->sk_state =BT_CONFIG ; + chan->state =BT_CONFIG ; +release_sock (sk); +__l2cap_connect_rsp_defer (chan); + } + }else { +release_sock (sk); + } + +l2cap_chan_unlock_conn (chan, have_conn); +return err; +} +static int l2cap_sock_recvmsg (struct socket *sock,struct msghdr *msg,size_t len,int flags) {
@@ -1254,29 +1302,9 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,return sock_recv_errqueue (sk, msg, len,SOL_BLUETOOTH ,BT_SCM_ERROR ); -lock_sock (sk); - -if (sk->sk_state ==BT_CONNECT2 &&test_bit (BT_SK_DEFER_SETUP , - &bt_sk (sk)->flags)) { -if (pi->chan->mode ==L2CAP_MODE_EXT_FLOWCTL ) { - sk->sk_state =BT_CONNECTED ; - pi->chan->state =BT_CONNECTED ; -__l2cap_ecred_conn_rsp_defer (pi->chan); - }else if (bdaddr_type_is_le (pi->chan->src_type)) { - sk->sk_state =BT_CONNECTED ; - pi->chan->state =BT_CONNECTED ; -__l2cap_le_connect_rsp_defer (pi->chan); - }else { - sk->sk_state =BT_CONFIG ; - pi->chan->state =BT_CONFIG ; -__l2cap_connect_rsp_defer (pi->chan); - } - - err =0 ; - goto done; - } - -release_sock (sk); + err =l2cap_sock_defer (sk); +if (err) +return err <0 ? err :0 ;if (sock->type ==SOCK_STREAM ) err =bt_sock_stream_recvmsg (sock, msg, len, flags);
Signature · 1 line
2.55.0
PATCH 8/16 Bluetooth: L2CAP: hold l2cap_conn::lock in l2cap_connect_cfm()
In reply to Pauli Virtanenl2cap_new_connection() -> __l2cap_chan_add() modifies l2cap_conn::chan_l, which is guarded by l2cap_conn::lock. The lock is not held in l2cap_connect_cfm().
Fix by holding conn->lock in l2cap_connect_cfm() to make the locking systematic.
--- net/bluetooth/l2cap_core.c | 4 ++++ 1 file changed, 4 insertions(+)
net/bluetooth/l2cap_core.c +4 -0
@@ -7629,6 +7629,8 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)* we left off, because the list lock would prevent calling the * potentially sleeping l2cap_chan_lock() function. */ +mutex_lock (&conn->lock); + pchan =l2cap_global_fixed_chan (NULL , hcon);while (pchan) {struct l2cap_chan *chan, *next;
@@ -7653,6 +7655,8 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) pchan = next; } +mutex_unlock (&conn->lock); +l2cap_conn_ready (conn); }
Signature · 1 line
2.55.0
PATCH 9/16 Bluetooth: L2CAP: add annotations for l2cap_chan list locking
In reply to Pauli VirtanenAdd context analysis annotations for l2cap_conn::chan_l and chan_list locking.
Add corresponding required annotations to accessors and callers.
This is not complete chan_l annotation, l2cap_chan::list and l2cap_chan_del() locking is currently not fully correct, and needs separate fix + annotations.
--- include/net/bluetooth/l2cap.h | 7 ++-- net/bluetooth/l2cap_core.c | 61 ++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 7 deletions(-)
include/net/bluetooth/l2cap.h +4 -3
@@ -668,7 +668,7 @@ struct l2cap_conn {struct l2cap_chan *smp; -struct list_head chan_l; + struct list_head chan_l __guarded_by(&lock);struct mutex lock;struct kref ref;struct list_head users;
@@ -954,7 +954,8 @@ void l2cap_cleanup_sockets(void);bool l2cap_is_socket (struct socket *sock);void __l2cap_le_connect_rsp_defer (struct l2cap_chan *chan); -void __l2cap_ecred_conn_rsp_defer (struct l2cap_chan *chan); +void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock);void __l2cap_connect_rsp_defer (struct l2cap_chan *chan);int l2cap_add_psm (struct l2cap_chan *chan,bdaddr_t *src,__le16 psm);
@@ -975,7 +976,7 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan);int l2cap_ertm_init (struct l2cap_chan *chan);void l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan); void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) - __must_hold(&chan->lock); + __must_hold(&conn->lock) __must_hold(&chan->lock);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);
net/bluetooth/l2cap_core.c +57 -4
350 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 10/16 Bluetooth: L2CAP: take lock for l2cap_chan_del in l2cap_ecred_rsp_defer
In reply to Pauli Virtanenl2cap_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
@@ -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
PATCH 11/16 Bluetooth: L2CAP: hold chan in l2cap_ecred_conn_rsp()
In reply to Pauli Virtanenl2cap_chan_del() calls l2cap_chan_put() to drop the conn->chan_l reference. If this was the last reference, UAF follows.
l2cap_ecred_conn_rsp() iterates chan_l list and calls l2cap_chan_del() on some members, without holding chan reference.
Fix by holding refcount while using chan after l2cap_chan_del().
Since orig is looked up by dcid provided by remote, it's also possible orig == chan, so reference needs to be held also after orig use.
--- net/bluetooth/l2cap_core.c | 5 +++++ 1 file changed, 5 insertions(+)
net/bluetooth/l2cap_core.c +5 -0
@@ -5513,12 +5513,14 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn, chan->state == BT_CONNECTED)continue ; +l2cap_chan_hold (chan);l2cap_chan_lock (chan);/* Check that there is a dcid for each pending channel */ if (cmd_len <sizeof (dcid)) {l2cap_chan_del (chan,ECONNREFUSED );l2cap_chan_unlock (chan); +l2cap_chan_put (chan);continue ; }
@@ -5557,6 +5559,8 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,__set_chan_timer (orig,0 );l2cap_chan_unlock (orig); } + +l2cap_chan_put (chan);continue ; }
@@ -5602,6 +5606,7 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn, }l2cap_chan_unlock (chan); +l2cap_chan_put (chan); }return err;
Signature · 1 line
2.55.0
PATCH 12/16 Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
In reply to Pauli VirtanenAdd 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
@@ -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);
@@ -984,7 +987,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
@@ -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
PATCH 13/16 Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks
In reply to Pauli VirtanenAnnotate 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
@@ -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
@@ -4200,6 +4200,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
PATCH 14/16 Bluetooth: L2CAP: make concurrent l2cap_set_timer() refcounting safe
In reply to Pauli VirtanenSince l2cap_set_timer() does not check return value of schedule_delayed_work(), two concurrent calls may result to l2cap_chan refcount leak.
Change the refcounting by using mod_delayed_work() and checking its return value.
Code paths aside from l2cap_chan_busy() hold chan->lock, so this has little correctness impact.
--- include/net/bluetooth/l2cap.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
include/net/bluetooth/l2cap.h +4 -5
@@ -859,12 +859,11 @@ static inline void l2cap_set_timer(struct l2cap_chan *chan,BT_DBG ("chan %p state %s timeout %ld" , chan,state_to_string (chan->state), timeout); -/* If delayed work cancelled do not hold(chan) -since it is already done with previous set_timer */ -if (!cancel_delayed_work (work)) -l2cap_chan_hold (chan); +l2cap_chan_hold (chan); -schedule_delayed_work (work, timeout); +/* put(chan) if timer was already queued so it already has a ref */ +if (mod_delayed_work (system_percpu_wq, work, timeout)) +l2cap_chan_put (chan); } static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
Signature · 1 line
2.55.0
PATCH 15/16 Bluetooth: L2CAP: remove conditional locking from l2cap_connect()
In reply to Pauli VirtanenContext analysis does not understand conditional locking.
Restructure l2cap_connect() by removing conditional locking at the cost of some code duplication, so that static analysis can see its content.
--- net/bluetooth/l2cap_core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
net/bluetooth/l2cap_core.c +7 -5
@@ -4233,7 +4233,6 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,static void l2cap_connect (struct l2cap_conn *conn,struct l2cap_cmd_hdr *cmd,u8 *data,u8 rsp_code) __must_hold(&conn->lock) -__context_unsafe (/* conditional locking */ ) {struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;struct l2cap_conn_rsp rsp;
@@ -4250,7 +4249,13 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, &conn->hcon->dst, ACL_LINK);if (!pchan) { result =L2CAP_CR_BAD_PSM ; - goto response; + + rsp.scid =cpu_to_le16 (scid); + rsp.dcid =cpu_to_le16 (dcid); + rsp.result =cpu_to_le16 (result); + rsp.status =cpu_to_le16 (status); +l2cap_send_cmd (conn, cmd->ident, rsp_code,sizeof (rsp), &rsp); +return ; }l2cap_chan_lock (pchan);
@@ -4332,9 +4337,6 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, rsp.status =cpu_to_le16 (status);l2cap_send_cmd (conn, cmd->ident, rsp_code,sizeof (rsp), &rsp); -if (!pchan) -return ; -if (result ==L2CAP_CR_PEND && status ==L2CAP_CS_NO_INFO ) {struct l2cap_info_req info; info.type =cpu_to_le16 (L2CAP_IT_FEAT_MASK );
Signature · 1 line
2.55.0
PATCH 16/16 Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn
In reply to Pauli Virtanenl2cap_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_CONNECTl2cap_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.
--- 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
@@ -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);
@@ -7604,7 +7608,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,mutex_lock (&conn->lock);l2cap_chan_lock (chan); -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
@@ -109,6 +109,7 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr_unsized *addr, ireturn -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
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 Sat, 29 Aug 2026 17:19:55 +0300 you wrote:
The intent in current code is that:
-
l2cap_conn::chan_lread/write is guarded byl2cap_conn::lock-
l2cap_chan_delcalled with heldl2cap_chan::lock(l2cap_sock.cuses this)[...]
Here is the summary with links:
- [01/16] Bluetooth: L2CAP: take chan->lock for l2cap_chan_add/ready/del
https://git.kernel.org/bluetooth/bluetooth-next/c/ca52f4764c87 - [02/16] Bluetooth: L2CAP: add l2cap_chan_close_unlocked() and locking helpers https://git.kernel.org/bluetooth/bluetooth-next/c/368dc7fcaced - [03/16] Bluetooth: L2CAP: fix race condition in l2cap_sock_shutdown() https://git.kernel.org/bluetooth/bluetooth-next/c/86b377359826 - [04/16] Bluetooth: 6lowpan: use l2cap_chan_close_unlocked() https://git.kernel.org/bluetooth/bluetooth-next/c/4e64bd587b64 - [05/16] Bluetooth: L2CAP: remove unused l2cap_chan_close() https://git.kernel.org/bluetooth/bluetooth-next/c/c5123fddfef1 - [06/16] Bluetooth: 6lowpan: avoid concurrent peer_del() in bt_6lowpan_disconnect https://git.kernel.org/bluetooth/bluetooth-next/c/02122fd80029 - [07/16] Bluetooth: L2CAP: hold conn->lock for __l2cap_ecred_conn_rsp_defer https://git.kernel.org/bluetooth/bluetooth-next/c/62df59925278 - [08/16] Bluetooth: L2CAP: hold l2cap_conn::lock in l2cap_connect_cfm() https://git.kernel.org/bluetooth/bluetooth-next/c/604e2f6d1e95 - [09/16] Bluetooth: L2CAP: add annotations for l2cap_chan list locking https://git.kernel.org/bluetooth/bluetooth-next/c/886931f0c7e6 - [10/16] Bluetooth: L2CAP: take lock for l2cap_chan_del in l2cap_ecred_rsp_defer (no matching commit) - [11/16] Bluetooth: L2CAP: hold chan in l2cap_ecred_conn_rsp() https://git.kernel.org/bluetooth/bluetooth-next/c/26836086682a - [12/16] Bluetooth: L2CAP: annotate locking for l2cap_chan_del() (no matching commit) - [13/16] Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks (no matching commit) - [14/16] Bluetooth: L2CAP: make concurrent l2cap_set_timer() refcounting safe https://git.kernel.org/bluetooth/bluetooth-next/c/761224d13f8a - [15/16] Bluetooth: L2CAP: remove conditional locking from l2cap_connect() https://git.kernel.org/bluetooth/bluetooth-next/c/2c127ee79a0b
- [16/16] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Luiz Augusto von Dentz
maintainerHi Pauli,
Quoted 47 lines · On Tue, Sep 1, 2026 at 3:11 PM <[email protected]> wrote:
Hello:
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 8 lines · On Sat, 29 Aug 2026 17:19:55 +0300 you wrote:
The intent in current code is that:
-
l2cap_conn::chan_lread/write is guarded byl2cap_conn::lock-
l2cap_chan_delcalled with heldl2cap_chan::lock(l2cap_sock.cuses this)[...]
Here is the summary with links:
- [01/16] Bluetooth: L2CAP: takechan->lockfor l2cap_chan_add/ready/delhttps://git.kernel.org/bluetooth/bluetooth-next/c/ca52f4764c87 - [02/16] Bluetooth: L2CAP: add l2cap_chan_close_unlocked() and locking helpers https://git.kernel.org/bluetooth/bluetooth-next/c/368dc7fcaced - [03/16] Bluetooth: L2CAP: fix race condition in l2cap_sock_shutdown() https://git.kernel.org/bluetooth/bluetooth-next/c/86b377359826 - [04/16] Bluetooth: 6lowpan: use l2cap_chan_close_unlocked() https://git.kernel.org/bluetooth/bluetooth-next/c/4e64bd587b64 - [05/16] Bluetooth: L2CAP: remove unused l2cap_chan_close() https://git.kernel.org/bluetooth/bluetooth-next/c/c5123fddfef1 - [06/16] Bluetooth: 6lowpan: avoid concurrent peer_del() in bt_6lowpan_disconnect https://git.kernel.org/bluetooth/bluetooth-next/c/02122fd80029 - [07/16] Bluetooth: L2CAP: hold conn->lock for __l2cap_ecred_conn_rsp_defer https://git.kernel.org/bluetooth/bluetooth-next/c/62df59925278 - [08/16] Bluetooth: L2CAP: hold l2cap_conn::lock in l2cap_connect_cfm() https://git.kernel.org/bluetooth/bluetooth-next/c/604e2f6d1e95 - [09/16] Bluetooth: L2CAP: add annotations for l2cap_chan list locking https://git.kernel.org/bluetooth/bluetooth-next/c/886931f0c7e6 - [10/16] Bluetooth: L2CAP: take lock for l2cap_chan_del in l2cap_ecred_rsp_defer (no matching commit) - [11/16] Bluetooth: L2CAP: hold chan in l2cap_ecred_conn_rsp() https://git.kernel.org/bluetooth/bluetooth-next/c/26836086682a - [12/16] Bluetooth: L2CAP: annotate locking for l2cap_chan_del() (no matching commit) - [13/16] Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks (no matching commit) - [14/16] Bluetooth: L2CAP: make concurrent l2cap_set_timer() refcounting safe https://git.kernel.org/bluetooth/bluetooth-next/c/761224d13f8a - [15/16] Bluetooth: L2CAP: remove conditional locking from l2cap_connect() https://git.kernel.org/bluetooth/bluetooth-next/c/2c127ee79a0b
Quoted 8 lines
- [16/16] Bluetooth: L2CAP: refuse
__l2cap_chan_addif chan already has conn (no matching commit)You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
I've run into some conflicts; this is why some changes were not applied. Could you please look into it and rebase them?
Signature · 1 line
Luiz Augusto von Dentz
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 Sat, 29 Aug 2026 17:19:55 +0300 you wrote:
The intent in current code is that:
-
l2cap_conn::chan_lread/write is guarded byl2cap_conn::lock-
l2cap_chan_delcalled with heldl2cap_chan::lock(l2cap_sock.cuses this)[...]
Here is the summary with links:
- [01/16] Bluetooth: L2CAP: take chan->lock for l2cap_chan_add/ready/del (no matching commit)
- [02/16] Bluetooth: L2CAP: add l2cap_chan_close_unlocked() and locking helpers (no matching commit)
- [03/16] Bluetooth: L2CAP: fix race condition in l2cap_sock_shutdown() (no matching commit)
- [04/16] Bluetooth: 6lowpan: use l2cap_chan_close_unlocked()
(no matching commit)
- [05/16] Bluetooth: L2CAP: remove unused l2cap_chan_close()
(no matching commit)
- [06/16] Bluetooth: 6lowpan: avoid concurrent peer_del() in bt_6lowpan_disconnect (no matching commit)
- [07/16] Bluetooth: L2CAP: hold conn->lock for __l2cap_ecred_conn_rsp_defer (no matching commit)
- [08/16] Bluetooth: L2CAP: hold l2cap_conn::lock in l2cap_connect_cfm() (no matching commit)
- [09/16] Bluetooth: L2CAP: add annotations for l2cap_chan list locking (no matching commit)
- [10/16] Bluetooth: L2CAP: take lock for l2cap_chan_del in l2cap_ecred_rsp_defer (no matching commit)
- [11/16] Bluetooth: L2CAP: hold chan in l2cap_ecred_conn_rsp()
(no matching commit)
- [12/16] Bluetooth: L2CAP: annotate locking for l2cap_chan_del()
https://git.kernel.org/bluetooth/bluetooth-next/c/6873eb51dcdd - [13/16] Bluetooth: L2CAP: annotate locking for l2cap_ops callbacks https://git.kernel.org/bluetooth/bluetooth-next/c/4149ba2a806c
- [14/16] Bluetooth: L2CAP: make concurrent l2cap_set_timer() refcounting safe (no matching commit)
- [15/16] Bluetooth: L2CAP: remove conditional locking from l2cap_connect() (no matching commit)
- [16/16] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html