mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: L2CAP: don't reserve an ident for L2CAP_LE_CREDITS
@ 2026-11-15 11:53 Nguyen Ngoc Thang
  2026-11-16 17:04 ` Luiz Augusto von Dentz
  2026-11-17 14:49 ` [PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock Nguyen Ngoc Thang
  0 siblings, 2 replies; 4+ messages in thread
From: Nguyen Ngoc Thang @ 2026-11-15 11:53 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: linux-bluetooth, linux-kernel, Stig Hornang, syzbot+9dc8780713b11155dd74

l2cap_chan_le_send_credits() allocates an ident from conn->tx_ida, sends
L2CAP_LE_CREDITS and then frees the ident unconditionally, since the
command has no response.

But the ident is visible in tx_ida while the command is in flight, and
the remote may answer it with L2CAP_COMMAND_REJ anyway. The RX path then
releases it in l2cap_put_ident() under conn->lock, while the credits path
runs from recvmsg() with only the socket lock held. Whichever ida_free()
comes second hits:

  ida_free called for id=3 which is not allocated.
  WARNING: lib/idr.c:594 at ida_free+0x24c/0x2d4 lib/idr.c:594
  Call trace:
   ida_free+0x24c/0x2d4 lib/idr.c:594
   l2cap_chan_le_send_credits+0x17c/0x29c net/bluetooth/l2cap_core.c:7113
   l2cap_chan_rx_avail+0xb4/0x134 net/bluetooth/l2cap_core.c:7126
   l2cap_publish_rx_avail+0x110/0x168 net/bluetooth/l2cap_sock.c
   l2cap_sock_recvmsg+0x280/0x7b8 net/bluetooth/l2cap_sock.c:1317

Worse, if the ident is reallocated for a real request in between, the
credits path frees that request's ident instead.

tx_ida only exists to track requests awaiting a response, so a command
without one has no business in it. Give L2CAP_LE_CREDITS a plain cyclic
ident instead, which never needs releasing. The ident is still never 0
and never exhausts the range, which is what commit 6e1930ece855
("Bluetooth: L2CAP: fix tx ident leak for commands without a response")
set out to fix.

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>
---
Tested in QEMU (x86_64, KASAN + lockdep) with the syzbot C reproducer,
15 runs per kernel:
  base (origin/master 23c62304dde2): 31x "ida_free called for id=N ..."
  patched:                           0 warnings, 1200 LE credits sent,
                                     none with ident 0 (ident wraps fine)

 net/bluetooth/l2cap_core.c | 44 +++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 7dcb0586a10b..0b861ced5703 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1028,21 +1028,36 @@ int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
 				 initiator);
 }
 
-static int l2cap_get_ident(struct l2cap_conn *conn)
+static u8 l2cap_ident_max(struct l2cap_conn *conn)
 {
-	u8 max;
-	int ident;
-
 	/* LE link does not support tools like l2ping so use the full range */
 	if (conn->hcon->type == LE_LINK)
-		max = 255;
+		return 255;
+
 	/* Get next available identificator.
 	 *    1 - 128 are used by kernel.
 	 *  129 - 199 are reserved.
 	 *  200 - 254 are used by utilities like l2ping, etc.
 	 */
-	else
-		max = 128;
+	return 128;
+}
+
+/* Ident for a command that has no response: it is not reserved in tx_ida,
+ * so nothing has to release it and it can't be released twice.
+ */
+static u8 l2cap_next_ident(struct l2cap_conn *conn)
+{
+	u8 ident = READ_ONCE(conn->tx_ident) % l2cap_ident_max(conn) + 1;
+
+	WRITE_ONCE(conn->tx_ident, ident);
+
+	return ident;
+}
+
+static int l2cap_get_ident(struct l2cap_conn *conn)
+{
+	u8 max = l2cap_ident_max(conn);
+	int ident;
 
 	/* Allocate ident using min as last used + 1 (cyclic) */
 	ident = ida_alloc_range(&conn->tx_ida, READ_ONCE(conn->tx_ident) + 1,
@@ -7081,7 +7096,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)
@@ -7099,18 +7113,8 @@ 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);
 
-	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.
-	 */
-	if (ident > 0)
-		ida_free(&conn->tx_ida, ident);
+	l2cap_send_cmd(conn, l2cap_next_ident(conn), L2CAP_LE_CREDITS,
+		       sizeof(pkt), &pkt);
 }
 
 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Bluetooth: L2CAP: don't reserve an ident for L2CAP_LE_CREDITS
  2026-11-15 11:53 [PATCH] Bluetooth: L2CAP: don't reserve an ident for L2CAP_LE_CREDITS Nguyen Ngoc Thang
@ 2026-11-16 17:04 ` Luiz Augusto von Dentz
  2026-11-17 14:49 ` [PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock Nguyen Ngoc Thang
  1 sibling, 0 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-16 17:04 UTC (permalink / raw)
  To: Nguyen Ngoc Thang
  Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, Stig Hornang,
	syzbot+9dc8780713b11155dd74

Hi Nguyen,

On Sun, Nov 15, 2026 at 6:53 AM Nguyen Ngoc Thang
<ngocthang2710.1999@gmail.com> wrote:
>
> l2cap_chan_le_send_credits() allocates an ident from conn->tx_ida, sends
> L2CAP_LE_CREDITS and then frees the ident unconditionally, since the
> command has no response.
>
> But the ident is visible in tx_ida while the command is in flight, and
> the remote may answer it with L2CAP_COMMAND_REJ anyway. The RX path then
> releases it in l2cap_put_ident() under conn->lock, while the credits path
> runs from recvmsg() with only the socket lock held. Whichever ida_free()
> comes second hits:
>
>   ida_free called for id=3 which is not allocated.
>   WARNING: lib/idr.c:594 at ida_free+0x24c/0x2d4 lib/idr.c:594
>   Call trace:
>    ida_free+0x24c/0x2d4 lib/idr.c:594
>    l2cap_chan_le_send_credits+0x17c/0x29c net/bluetooth/l2cap_core.c:7113
>    l2cap_chan_rx_avail+0xb4/0x134 net/bluetooth/l2cap_core.c:7126
>    l2cap_publish_rx_avail+0x110/0x168 net/bluetooth/l2cap_sock.c
>    l2cap_sock_recvmsg+0x280/0x7b8 net/bluetooth/l2cap_sock.c:1317
>
> Worse, if the ident is reallocated for a real request in between, the
> credits path frees that request's ident instead.
>
> tx_ida only exists to track requests awaiting a response, so a command
> without one has no business in it. Give L2CAP_LE_CREDITS a plain cyclic
> ident instead, which never needs releasing. The ident is still never 0
> and never exhausts the range, which is what commit 6e1930ece855
> ("Bluetooth: L2CAP: fix tx ident leak for commands without a response")
> set out to fix.
>
> 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>
> ---
> Tested in QEMU (x86_64, KASAN + lockdep) with the syzbot C reproducer,
> 15 runs per kernel:
>   base (origin/master 23c62304dde2): 31x "ida_free called for id=N ..."
>   patched:                           0 warnings, 1200 LE credits sent,
>                                      none with ident 0 (ident wraps fine)
>
>  net/bluetooth/l2cap_core.c | 44 +++++++++++++++++++++-----------------
>  1 file changed, 24 insertions(+), 20 deletions(-)
>
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 7dcb0586a10b..0b861ced5703 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -1028,21 +1028,36 @@ int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
>                                  initiator);
>  }
>
> -static int l2cap_get_ident(struct l2cap_conn *conn)
> +static u8 l2cap_ident_max(struct l2cap_conn *conn)
>  {
> -       u8 max;
> -       int ident;
> -
>         /* LE link does not support tools like l2ping so use the full range */
>         if (conn->hcon->type == LE_LINK)
> -               max = 255;
> +               return 255;
> +
>         /* Get next available identificator.
>          *    1 - 128 are used by kernel.
>          *  129 - 199 are reserved.
>          *  200 - 254 are used by utilities like l2ping, etc.
>          */
> -       else
> -               max = 128;
> +       return 128;
> +}
> +
> +/* Ident for a command that has no response: it is not reserved in tx_ida,
> + * so nothing has to release it and it can't be released twice.
> + */
> +static u8 l2cap_next_ident(struct l2cap_conn *conn)
> +{
> +       u8 ident = READ_ONCE(conn->tx_ident) % l2cap_ident_max(conn) + 1;
> +
> +       WRITE_ONCE(conn->tx_ident, ident);
> +
> +       return ident;
> +}
> +
> +static int l2cap_get_ident(struct l2cap_conn *conn)
> +{
> +       u8 max = l2cap_ident_max(conn);
> +       int ident;
>
>         /* Allocate ident using min as last used + 1 (cyclic) */
>         ident = ida_alloc_range(&conn->tx_ida, READ_ONCE(conn->tx_ident) + 1,
> @@ -7081,7 +7096,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)
> @@ -7099,18 +7113,8 @@ 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);
>
> -       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.
> -        */
> -       if (ident > 0)
> -               ida_free(&conn->tx_ida, ident);
> +       l2cap_send_cmd(conn, l2cap_next_ident(conn), L2CAP_LE_CREDITS,
> +                      sizeof(pkt), &pkt);
>  }
>
>  void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail)
> --
> 2.43.0

Sashiko flagged a problem:
https://sashiko.dev/#/patchset/20261115115341.316484-1-ngocthang2710.1999%40gmail.com


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock
  2026-11-15 11:53 [PATCH] Bluetooth: L2CAP: don't reserve an ident for L2CAP_LE_CREDITS Nguyen Ngoc Thang
  2026-11-16 17:04 ` Luiz Augusto von Dentz
@ 2026-11-17 14:49 ` Nguyen Ngoc Thang
  2026-11-17 18:50   ` patchwork-bot+bluetooth
  1 sibling, 1 reply; 4+ messages in thread
From: Nguyen Ngoc Thang @ 2026-11-17 14:49 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: linux-bluetooth, linux-kernel, Stig Hornang, syzbot+9dc8780713b11155dd74

l2cap_chan_le_send_credits() allocates an ident from conn->tx_ida, sends
L2CAP_LE_CREDITS and frees the ident right away, since the command has
no response. The RX path frees idents in l2cap_put_ident() under
conn->lock. That only works if the credits path holds conn->lock too,
so that no response can be processed between the allocation and the
free.

The RX path does, but l2cap_sock_recvmsg() calls l2cap_publish_rx_avail()
-> l2cap_chan_rx_avail() -> l2cap_chan_le_send_credits() with only the
socket lock held. If the remote answers the credits packet with
L2CAP_COMMAND_REJ, l2cap_put_ident() can free the ident first, and the
credits path then frees it again:

  ida_free called for id=3 which is not allocated.
  WARNING: lib/idr.c:594 at ida_free+0x24c/0x2d4 lib/idr.c:594
  Call trace:
   ida_free+0x24c/0x2d4 lib/idr.c:594
   l2cap_chan_le_send_credits+0x17c/0x29c net/bluetooth/l2cap_core.c:7113
   l2cap_chan_rx_avail+0xb4/0x134 net/bluetooth/l2cap_core.c:7126
   l2cap_publish_rx_avail+0x110/0x168 net/bluetooth/l2cap_sock.c
   l2cap_sock_recvmsg+0x280/0x7b8 net/bluetooth/l2cap_sock.c:1317

The same unlocked call also updates chan->rx_credits and reads chan->sdu
concurrently with the RX path.

Publish rx_avail from recvmsg under conn->lock and chan->lock, taken with
l2cap_chan_lock_conn() as l2cap_sock_defer() already does, before taking
the socket lock to keep the conn->lock -> chan->lock -> sk_lock order.
Assert conn->lock in l2cap_chan_le_send_credits() so that an unlocked
caller is caught by lockdep.

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>
---
v2:
- Drop the untracked cyclic ident from v1: it could collide with the
  ident of an in-flight request, so a REJ of the credits packet could
  free that ident and close the waiting channel (reported by Sashiko).
- Fix the root cause instead: return credits from recvmsg under the
  same locks as the RX path. This also covers the unlocked
  chan->rx_credits / chan->sdu access on this path that Sashiko noted.
v1: https://lore.kernel.org/all/20261115115341.316484-1-ngocthang2710.1999@gmail.com/

Tested in QEMU (x86_64, KASAN + lockdep) with the syzbot C reproducer,
15 runs per kernel on 666539eecdf4, credits counted via dynamic debug:
  base:    10191 LE credits sent, 49x "ida_free called for id=N ..."
  patched:  9330 LE credits sent, no warnings, no lockdep reports

 net/bluetooth/l2cap_core.c | 3 +++
 net/bluetooth/l2cap_sock.c | 8 ++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 7dcb0586a10b..12794af1cba4 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -7099,6 +7099,9 @@ 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);
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index e5c15c083c1a..3db1608dd71a 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1292,6 +1292,7 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 {
 	struct sock *sk = sock->sk;
 	struct l2cap_pinfo *pi = l2cap_pi(sk);
+	bool have_conn;
 	int err;
 
 	if (unlikely(flags & MSG_ERRQUEUE))
@@ -1312,9 +1313,12 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 	    pi->chan->mode != L2CAP_MODE_EXT_FLOWCTL)
 		return err;
 
-	lock_sock(sk);
-
+	/* Returning credits must be serialized with the RX path */
+	have_conn = l2cap_chan_lock_conn(pi->chan);
 	l2cap_publish_rx_avail(pi->chan);
+	l2cap_chan_unlock_conn(pi->chan, have_conn);
+
+	lock_sock(sk);
 
 	/* Attempt to put pending rx data in the socket buffer */
 	while (!list_empty(&pi->rx_busy)) {
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock
  2026-11-17 14:49 ` [PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock Nguyen Ngoc Thang
@ 2026-11-17 18:50   ` patchwork-bot+bluetooth
  0 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+bluetooth @ 2026-11-17 18:50 UTC (permalink / raw)
  To: Nguyen Ngoc Thang
  Cc: marcel, luiz.dentz, linux-bluetooth, linux-kernel, stig,
	syzbot+9dc8780713b11155dd74

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:

On Tue, 17 Nov 2026 21:49:37 +0700 you wrote:
> l2cap_chan_le_send_credits() allocates an ident from conn->tx_ida, sends
> L2CAP_LE_CREDITS and frees the ident right away, since the command has
> no response. The RX path frees idents in l2cap_put_ident() under
> conn->lock. That only works if the credits path holds conn->lock too,
> so that no response can be processed between the allocation and the
> free.
> 
> [...]

Here is the summary with links:
  - [v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock
    https://git.kernel.org/bluetooth/bluetooth-next/c/5beb86f98c3d

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-17 18:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 11:53 [PATCH] Bluetooth: L2CAP: don't reserve an ident for L2CAP_LE_CREDITS Nguyen Ngoc Thang
2026-11-16 17:04 ` Luiz Augusto von Dentz
2026-11-17 14:49 ` [PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock Nguyen Ngoc Thang
2026-11-17 18:50   ` patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®