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 v3] Bluetooth: L2CAP: Use a reserved ident for L2CAP_LE_CREDITS
Date: Wed, 18 Nov 2026 09:27:32 -0500	[thread overview]
Message-ID: <20261118142732.102749-1-luiz.dentz@gmail.com> (raw)

From: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>

L2CAP_LE_CREDITS has no response, so an ident taken from conn->tx_ida for
it is never released by l2cap_put_ident() and l2cap_chan_le_send_credits()
has to free it right away after sending, otherwise the range is exhausted
after 254 packets and credits would be sent with the invalid ident 0, which
some remote stacks ignore, stalling the channel.

tx_ida only exists to track requests awaiting a response, so a command
without one has no business in it: every credit update allocates and
frees an ident, and the ident is visible in tx_ida while the credits are
in flight, so an answer to them (a L2CAP_COMMAND_REJ) is processed by
l2cap_put_ident() like the answer to a request.

Using a counter that doesn't consult tx_ida is not an option either, it can
return the ident of a request still outstanding, and a reject of the
credits would then release that request's ident and close its channel in
l2cap_le_command_rej().

Reserve ident 255 for L2CAP_LE_CREDITS instead: l2cap_get_ident() never
returns it (the BR/EDR range already ends at 128), so it can't collide with
any request, it doesn't have to be tracked in conn->tx_ida, and a reject of
it matches no outstanding request.

Fixes: 6e1930ece855 ("Bluetooth: L2CAP: fix tx ident leak for commands without a response")
Reported-by: syzbot+9dc8780713b11155dd74@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9dc8780713b11155dd74
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 include/net/bluetooth/l2cap.h |  5 +++++
 net/bluetooth/l2cap_core.c    | 28 +++++++++++-----------------
 2 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
index cf5c2df68af8..72d9d56c6f2a 100644
--- a/include/net/bluetooth/l2cap.h
+++ b/include/net/bluetooth/l2cap.h
@@ -45,6 +45,11 @@
 #define L2CAP_DEFAULT_ACC_LAT		0xFFFFFFFF
 #define L2CAP_BREDR_MAX_PAYLOAD		1019    /* 3-DH5 packet */
 #define L2CAP_LE_MIN_MTU		23
+
+/* Identifier used by L2CAP_LE_CREDITS, it has no response so it is not
+ * tracked in conn->tx_ida and is never allocated for any other command.
+ */
+#define L2CAP_IDENT_LE_CREDITS		255
 #define L2CAP_ECRED_CONN_SCID_MAX	5
 
 #define L2CAP_DISC_TIMEOUT		msecs_to_jiffies(100)
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 977b41229a7f..3ff40565a05f 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1046,9 +1046,11 @@ static int l2cap_get_ident(struct l2cap_conn *conn)
 	u8 max;
 	int ident;
 
-	/* LE link does not support tools like l2ping so use the full range */
+	/* LE link does not support tools like l2ping so use the full range,
+	 * except for L2CAP_IDENT_LE_CREDITS which is reserved.
+	 */
 	if (conn->hcon->type == LE_LINK)
-		max = 255;
+		max = L2CAP_IDENT_LE_CREDITS - 1;
 	/* Get next available identificator.
 	 *    1 - 128 are used by kernel.
 	 *  129 - 199 are reserved.
@@ -7204,7 +7206,6 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
 	struct l2cap_conn *conn = chan->conn;
 	struct l2cap_le_credits pkt;
 	u16 return_credits = l2cap_le_rx_credits(chan);
-	int ident;
 
 	if (chan->mode != L2CAP_MODE_LE_FLOWCTL &&
 	    chan->mode != L2CAP_MODE_EXT_FLOWCTL)
@@ -7222,21 +7223,14 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
 	pkt.cid     = cpu_to_le16(chan->scid);
 	pkt.credits = cpu_to_le16(return_credits);
 
-	/* Keeps l2cap_put_ident() from releasing ident before we do */
-	lockdep_assert_held(&conn->lock);
-
-	ident = l2cap_get_ident(conn);
-
-	l2cap_send_cmd(conn, ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
-
-	/* L2CAP_LE_CREDITS has no response so the ident is never released by
-	 * l2cap_put_ident() - release it right away, otherwise the tx_ida
-	 * range is exhausted after 254 packets and from then on credits are
-	 * sent with the invalid ident 0, which some remote stacks ignore,
-	 * stalling the channel.
+	/* L2CAP_LE_CREDITS has no response so it uses the reserved ident rather
+	 * than one from conn->tx_ida, which would have to be released right
+	 * away or would be exhausted after 254 packets, and could otherwise
+	 * collide with the ident of a request still outstanding. A reject of it
+	 * can't match any request either.
 	 */
-	if (ident > 0)
-		ida_free(&conn->tx_ida, ident);
+	l2cap_send_cmd(conn, L2CAP_IDENT_LE_CREDITS, L2CAP_LE_CREDITS,
+		       sizeof(pkt), &pkt);
 }
 
 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail)

base-commit: f42f5cae154b90a0cb6d8232dc84c7614ca2e6a0
-- 
2.56.0


                 reply	other threads:[~2026-11-18 14:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261118142732.102749-1-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