From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v7 1/4] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused
Date: Wed, 25 Nov 2026 16:12:07 -0500 [thread overview]
Message-ID: <20261125211210.1955598-2-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261125211210.1955598-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
l2cap_ecred_rsp_defer() responds to a request that has some of its
channels refused with a rejection for all of them, deleting the channels
that userspace has already accepted as well, e.g. when the listener
limits the number of channels, like bluetoothd does for EATT.
A L2CAP_CREDIT_BASED_CONNECTION_RSP can accept only some of the
channels of the request: the destination CID of the ones refused is set
to 0x0000 and the result tells that only some of the connections were
refused.
Fix this by responding with L2CAP_CR_LE_NO_MEM if only some of the
channels were refused, with the destination CID of the ones accepted and
none for the refused ones, and by deleting only the refused ones. If all
of them are refused the response is unchanged.
Fixes: 9aa9d9473f15 ("Bluetooth: L2CAP: Fix responding with wrong PDU type")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
net/bluetooth/l2cap_core.c | 67 +++++++++++++++++++++++++++++++++-----
1 file changed, 58 insertions(+), 9 deletions(-)
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 943effc86740..a15ffcdd1330 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -4065,6 +4065,34 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data)
}
}
+struct l2cap_ecred_defer_count {
+ int pending;
+ int accepted;
+ int refused;
+};
+
+static void l2cap_ecred_list_count(struct l2cap_chan *chan, void *data)
+{
+ struct l2cap_ecred_defer_count *count = data;
+
+ if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))
+ return;
+
+ switch (chan->state) {
+ case BT_CONNECT2:
+ /* Still pending accept */
+ count->pending++;
+ return;
+ case BT_CONNECTED:
+ count->accepted++;
+ return;
+ default:
+ /* If not connected or pending accept it has been refused */
+ count->refused++;
+ return;
+ }
+}
+
struct l2cap_ecred_rsp_data {
struct l2cap_chan *locked_chan;
struct {
@@ -4072,6 +4100,7 @@ struct l2cap_ecred_rsp_data {
__le16 scid[L2CAP_ECRED_MAX_CID];
} __packed pdu;
int count;
+ bool partial;
};
/* Lock @chan if it is not @locked_chan, and has same or lower nesting level.
@@ -4141,14 +4170,23 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
chan->ident = 0;
/* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */
- if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid)))
+ if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) {
rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM);
+ l2cap_chan_del(chan, ECONNRESET);
+ goto done;
+ }
- /* Include all channels pending with the same ident */
- if (!rsp->pdu.rsp.result)
+ /* Include all channels pending with the same ident, the ones refused
+ * have no destination CID if only some of them were.
+ */
+ if (chan->state == BT_CONNECTED) {
rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
- else
+ } else {
+ if (rsp->partial)
+ rsp_flex->dcid[rsp->count++] = 0;
+
l2cap_chan_del(chan, ECONNRESET);
+ }
done:
if (locked)
@@ -4161,8 +4199,8 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
{
struct l2cap_conn *conn = chan->conn;
struct l2cap_ecred_rsp_data data;
+ struct l2cap_ecred_defer_count count = {};
u16 id = chan->ident;
- int result = 0;
if (!id)
return;
@@ -4181,13 +4219,24 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS);
/* Verify that all channels are ready */
- __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result);
+ __l2cap_chan_list_id(conn, id, l2cap_ecred_list_count, &count);
- if (result > 0)
+ if (count.pending)
return;
- if (result < 0)
- data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION);
+ /* Only some of the channels may have been refused, e.g. if the
+ * listener limits the number of channels, in which case the others
+ * are accepted.
+ */
+ if (count.refused) {
+ if (count.accepted) {
+ data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM);
+ data.partial = true;
+ } else {
+ data.pdu.rsp.result =
+ cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION);
+ }
+ }
/* Build response */
__l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data);
--
2.56.0
next prev parent reply other threads:[~2026-11-25 21:12 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 21:12 [PATCH v7 0/4] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-11-25 21:12 ` Luiz Augusto von Dentz [this message]
2026-11-26 3:44 ` bluez.test.bot
2026-11-25 21:12 ` [PATCH v7 2/4] Bluetooth: L2CAP: Fix leaking the socket of refused Enhanced Credit channels Luiz Augusto von Dentz
2026-11-25 21:12 ` [PATCH v7 3/4] Bluetooth: L2CAP: Fix accepting channels with insufficient security Luiz Augusto von Dentz
2026-11-25 21:12 ` [PATCH v7 4/4] 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=20261125211210.1955598-2-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