[PATCH v2] Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock
From: Nguyen Ngoc Thang
Date: Tue Nov 17 2026 - 09:49:51 EST
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@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=9dc8780713b11155dd74
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@xxxxxxxxx>
---
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@xxxxxxxxx/
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