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 v9 2/5] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused
Date: Mon, 30 Nov 2026 16:12:25 -0500	[thread overview]
Message-ID: <20261130211228.621145-3-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261130211228.621145-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 per channel when building the response, so it
always has as many destination CIDs as 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 | 104 +++++++++++++++++++++++++++++++++----
 1 file changed, 95 insertions(+), 9 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 943effc86740..1767de519089 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);
@@ -4065,6 +4094,39 @@ 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;
+
+	/* 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;
+
+	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 {
@@ -4141,14 +4203,29 @@ 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. Unless all of them
+	 * were refused, the refused ones are responded with no destination
+	 * CID, which is decided per channel so the response always has as
+	 * many as the request.
+	 */
+	if (rsp->pdu.rsp.result != cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION) &&
+	    chan->state == BT_CONNECTED) {
 		rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
-	else
+	} else {
+		if (rsp->pdu.rsp.result !=
+		    cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION)) {
+			rsp_flex->dcid[rsp->count++] = 0;
+			rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM);
+		}
+
 		l2cap_chan_del(chan, ECONNRESET);
+	}
 
 done:
 	if (locked)
@@ -4161,8 +4238,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 +4258,22 @@ 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);
+		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


  parent reply	other threads:[~2026-11-30 21:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 21:12 [PATCH v9 0/5] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-11-30 21:12 ` [PATCH v9 1/5] Bluetooth: hci_event: Disconnect if the encryption key size is not valid Luiz Augusto von Dentz
2026-12-01  7:02   ` L2CAP: Fix channels being established with insufficient security bluez.test.bot
2026-11-30 21:12 ` Luiz Augusto von Dentz [this message]
2026-11-30 21:12 ` [PATCH v9 3/5] Bluetooth: L2CAP: Fix leaking sockets released before their channel is deleted Luiz Augusto von Dentz
2026-11-30 21:12 ` [PATCH v9 4/5] Bluetooth: L2CAP: Fix accepting channels with insufficient security Luiz Augusto von Dentz
2026-11-30 21:12 ` [PATCH v9 5/5] 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=20261130211228.621145-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