Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v12 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused
Date: Tue,  1 Dec 2026 14:03:22 -0500	[thread overview]
Message-ID: <20261201190325.110874-4-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>

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. Whether a channel is
accepted is decided only once, per channel, when building the response
and the result follows from it, so the response always matches the
request.

A channel of the request can't be deleted before the response is sent
since it has to be included in it, nor be disconnected since the peer
doesn't know about it yet, so closing a channel that is waiting for the
response refuses it if it was accepted, and leaves it to be deleted by
the response if it was already refused, e.g. when it is closed again by
its owner or its timer.

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 | 91 +++++++++++++++++++++++++++++++++-----
 1 file changed, 80 insertions(+), 11 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index be07b7b9cfb0..ad5a3cba708c 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -864,12 +864,41 @@ static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
 	l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
 }
 
+/* Check if the channel is part of an Enhanced Credit Based request whose
+ * response has not been sent yet, which is only sent once all of its channels
+ * are authorized or refused and has to include all of them.
+ */
+static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan)
+{
+	return chan->mode == L2CAP_MODE_EXT_FLOWCTL &&
+	       test_bit(FLAG_DEFER_SETUP, &chan->flags) &&
+	       !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags);
+}
+
 static void __l2cap_chan_close(struct l2cap_chan *chan, int reason)
 {
 	struct l2cap_conn *conn = chan->conn;
 
 	BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
 
+	/* A channel waiting for the response of its request can't be
+	 * disconnected nor deleted since the peer doesn't know about it yet:
+	 * refuse it if it was accepted, and leave it to be deleted by the
+	 * response if it was already refused, e.g. when closed again by its
+	 * owner or its timer.
+	 */
+	if (l2cap_chan_ecred_rsp_pending(chan)) {
+		switch (chan->state) {
+		case BT_CONNECTED:
+			l2cap_chan_ecred_connect_reject(chan);
+			return;
+		case BT_DISCONN:
+			return;
+		default:
+			break;
+		}
+	}
+
 	switch (chan->state) {
 	case BT_LISTEN:
 		chan->ops->teardown(chan, 0);
@@ -4072,6 +4101,23 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data)
 	}
 }
 
+static void l2cap_ecred_list_pending(struct l2cap_chan *chan, void *data)
+{
+	int *pending = data;
+
+	/* The idents are not unique among the channels of the connection, so
+	 * only count the ones l2cap_ecred_rsp_defer() responds to.
+	 */
+	if (chan->mode != L2CAP_MODE_EXT_FLOWCTL ||
+	    test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) ||
+	    !test_bit(FLAG_DEFER_SETUP, &chan->flags))
+		return;
+
+	/* Still pending accept */
+	if (chan->state == BT_CONNECT2)
+		(*pending)++;
+}
+
 struct l2cap_ecred_rsp_data {
 	struct l2cap_chan *locked_chan;
 	struct {
@@ -4079,6 +4125,8 @@ struct l2cap_ecred_rsp_data {
 		__le16 scid[L2CAP_ECRED_MAX_CID];
 	} __packed pdu;
 	int count;
+	int accepted;
+	int refused;
 };
 
 /* Lock @chan if it is not @locked_chan, and has same or lower nesting level.
@@ -4148,12 +4196,21 @@ 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);
+		rsp->refused++;
+		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, which is decided
+	 * per channel while building the response so it always matches the
+	 * result: the refused ones are responded with no destination CID,
+	 * which are dropped if all of them were refused.
+	 */
+	if (chan->state == BT_CONNECTED) {
 		rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
+		rsp->accepted++;
 
 		/* The response establishes the channel, which was already
 		 * moved to BT_CONNECTED when userspace authorized it, so
@@ -4166,8 +4223,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
 		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.
+		rsp_flex->dcid[rsp->count++] = 0;
+		rsp->refused++;
+
+		/* The channel may have been accepted before being refused,
+		 * clear its configuration so its queue is purged like for any
+		 * other.
 		 */
 		chan->conf_state = 0;
 		l2cap_chan_del(chan, ECONNRESET);
@@ -4184,8 +4245,8 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
 {
 	struct l2cap_conn *conn = chan->conn;
 	struct l2cap_ecred_rsp_data data;
+	int pending = 0;
 	u16 id = chan->ident;
-	int result = 0;
 
 	if (!id)
 		return;
@@ -4204,17 +4265,25 @@ 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_pending, &pending);
 
-	if (result > 0)
+	if (pending)
 		return;
 
-	if (result < 0)
-		data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION);
-
 	/* Build response */
 	__l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data);
 
+	/* 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 all of them were refused, none is included.
+	 */
+	if (!data.accepted) {
+		data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION);
+		data.count = 0;
+	} else if (data.refused) {
+		data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM);
+	}
+
 	l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP,
 		       sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)),
 		       &data.pdu);
-- 
2.56.1


  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 ` [PATCH v12 2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted Luiz Augusto von Dentz
2026-12-01 19:03 ` Luiz Augusto von Dentz [this message]
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-4-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