From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v12 2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted
Date: Tue, 1 Dec 2026 14:03:21 -0500 [thread overview]
Message-ID: <20261201190325.110874-3-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261201190325.110874-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
When userspace authorizes a deferred LE Credit Based or Enhanced Credit
Based channel, l2cap_sock_defer() moves it to BT_CONNECTED directly
instead of with l2cap_chan_ready(), so the timer armed when the request
was received is not stopped and CONF_NOT_COMPLETE is not cleared.
The timer then fires on the established channel and disconnects it, and
since CONF_NOT_COMPLETE is still set l2cap_chan_del() returns before
purging its tx queue.
Fix this by completing LE Credit Based channels with l2cap_chan_ready()
once the response is sent, as done for the ones that are not deferred.
Enhanced Credit Based channels stay in BT_CONNECTED while the response
of their request is pending, so their timer is stopped when they are
authorized, as only the channels still pending shall time out, and the
rest of what l2cap_chan_ready() does is done once the response is sent.
Since the response may wait for other channels of the request, they are
also suspended until then so they can't send data before it, and their
configuration is cleared before deleting them if they end up refused,
so their queue is purged.
l2cap_sock_resume_cb() now locks the socket with the nesting level of
the channel, like l2cap_sock_teardown_cb(), since it is called by the
response with a sibling channel locked.
Fixes: 27e2d4c8d28b ("Bluetooth: Add basic LE L2CAP connect request receiving support")
Fixes: 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
include/net/bluetooth/l2cap.h | 3 ++-
net/bluetooth/l2cap_core.c | 27 +++++++++++++++++++++++++--
net/bluetooth/l2cap_sock.c | 16 +++++++++++++---
3 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
index 72d9d56c6f2a..e1820b7dca67 100644
--- a/include/net/bluetooth/l2cap.h
+++ b/include/net/bluetooth/l2cap.h
@@ -978,7 +978,8 @@ int l2cap_init_sockets(void);
void l2cap_cleanup_sockets(void);
bool l2cap_is_socket(struct socket *sock);
-void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
+void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
+ __must_hold(&chan->lock) __must_hold(&chan->conn->lock);
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);
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 943effc86740..be07b7b9cfb0 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -4027,6 +4027,8 @@ static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
}
void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
+ __must_hold(&chan->lock)
+ __must_hold(&chan->conn->lock)
{
struct l2cap_le_conn_rsp rsp;
struct l2cap_conn *conn = chan->conn;
@@ -4042,6 +4044,11 @@ void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
&rsp);
chan->ident = 0;
+
+ /* The response establishes the channel, which also stops its timer
+ * and completes its configuration like for one that is not deferred.
+ */
+ l2cap_chan_ready(chan);
}
static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data)
@@ -4145,10 +4152,26 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM);
/* Include all channels pending with the same ident */
- if (!rsp->pdu.rsp.result)
+ if (!rsp->pdu.rsp.result) {
rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
- else
+
+ /* The response establishes the channel, which was already
+ * moved to BT_CONNECTED when userspace authorized it, so
+ * complete what l2cap_chan_ready() does, and let it send
+ * data, which was suspended until now, if it has credits.
+ */
+ chan->conf_state = 0;
+ if (!chan->tx_credits)
+ chan->ops->suspend(chan);
+ else
+ chan->ops->resume(chan);
+ } else {
+ /* Nothing was sent by the channel, but clear its configuration
+ * so its queue is purged like for any other.
+ */
+ chan->conf_state = 0;
l2cap_chan_del(chan, ECONNRESET);
+ }
done:
if (locked)
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index d5663a4642a9..9ead47ca846e 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1287,11 +1287,17 @@ static int l2cap_sock_defer(struct sock *sk)
} else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) {
sk->sk_state = BT_CONNECTED;
chan->state = BT_CONNECTED;
+ /* Data can't be sent until the response of the request
+ * is sent, which may wait for other channels of it.
+ */
+ set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
release_sock(sk);
+ /* The channel is accepted, only the ones still pending
+ * shall time out while waiting for the response.
+ */
+ __clear_chan_timer(chan);
__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 {
@@ -1894,7 +1900,11 @@ static void l2cap_sock_resume_cb(struct l2cap_chan *chan)
if (!sk)
return;
- lock_sock(sk);
+ /* Like l2cap_sock_teardown_cb(), lock the socket with the nesting level
+ * of the channel since this may be called with a sibling channel
+ * locked, e.g. by the response of an Enhanced Credit Based request.
+ */
+ lock_sock_nested(sk, atomic_read(&chan->nesting));
if (test_and_clear_bit(FLAG_PENDING_SECURITY, &chan->flags)) {
sk->sk_state = BT_CONNECTED;
--
2.56.1
next prev parent reply other threads:[~2026-12-01 19:03 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 1/6] Bluetooth: hci_event: Disconnect if the encryption key size is not valid Luiz Augusto von Dentz
2026-12-01 19:03 ` Luiz Augusto von Dentz [this message]
2026-12-01 19:03 ` [PATCH v12 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 4/6] Bluetooth: L2CAP: Fix leaking sockets released before their channel is deleted Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 5/6] Bluetooth: L2CAP: Fix accepting channels with insufficient security Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 6/6] Bluetooth: L2CAP: Don't offer deferred channels before security is met Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201190325.110874-3-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox