From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795624738; b=kQcxMKRHk4Wh2Lg35qPjv0/KtsAvbhSrx2+Pe8JqD1hWc6jZReyBeL2H1WS7GPTN+ECc VhBIhEg5jJI4BvbyJ1xffN//BDAueVSY756Tkn0UGQ/yK1hBLz89w1CzTO0M1SkNrT+s3 /TePeSN0T9ebTaLgXco4kPVpp8VqpM80EYMdn+ikGwHde5hyPRihfZwpL/o7mPCNVUFR1 xH16MehwrCwXoP/9aLH5icxtl7nJINzoBVH/2NdN06jIAxE3CDtMlrVggJC8YWidhuVV+ UdMTwro+vo0YFcjoDj1JqwhstARNyywtk183K46ePeiZfDm26kSLD+g3lFigRc37eCw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795624738; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=BZm4qNUIXeKur3I3Af6n0t3IqACho/pd0phfQNjyWyI=; b=X3LHJcUhyVicO21M4b0IQe7yNslZCdQ72Zf/0Q25ilp5Rc8ba+tSXTHLqMI5JAC2B+v2 4O5MZtnWaVhtqe3lgUrUUSAXsh6/ZCvq+cULb6iyZ7Do+pBXbWz43/JM+rZMPMTEfQ6Wo WkYchK6B5y48KmDA0xLIZC0/IeMWLKCzCM+IzslxYyzqYfwJEtFj2lOIl5eGM4PD6T59X hGD394GTtRfbUQkzN3AVLsj80qQBoYkwa4FistVaaR7XCglB5H2k9Xnu+ql1NyYMStwBF Mmk6I8g/PZMLvKUlYfpmtI89T7X2pcfAF2wnOUt7cQ7YLAwDT4nEFmyMrKJ62sH5WUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=olBUE3GV; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D717606A685 for ; Wed, 25 Nov 2026 16:38:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=olBUE3GV Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4F7D1F000C0; Wed, 25 Nov 2026 16:38:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795624738; bh=BZm4qNUIXeKur3I3Af6n0t3IqACho/pd0phfQNjyWyI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=olBUE3GV5wFjz6l7F7OY1MzcihDtmKx8xyVborLuA/6Bdi4BUt3w/ShPsIEaK+vYF N96alsA5B4Y8p1dj6nL/nYUljTyFRD/ukMGU/QCn1bA7wrdf1CCWcnIcAuRNnRk9Pp EFNO0SX17mmxbsi3R5Bt1Bi1t9+5UpZ6OP36xKkHgqXWCeI8FC4PcY0Asyh57WcMl3 LW8JAqhEPCgUyucu8APevNHftogDuE+Z4dgT7jPx/+euMXNqs3fIntL4AN720pnDAK vwrXUUBz/xcLLGQlkP5/+bkb4KHnhA9XcEB+nRv9UEf21pHfoSSuhZylXlq7W0+g6F GXBjO2crYjz2A== From: Sasha Levin To: stable@vger.kernel.org Cc: Zhao Li , Luiz Augusto von Dentz , Sasha Levin Subject: [PATCH 5.15.y] Bluetooth: L2CAP: Only accept LE responses to outstanding requests Date: Wed, 25 Nov 2026 11:38:56 -0500 Message-ID: <20261125163833.818132-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026112522-footing-browbeat-6ce9@gregkh> References: <2026112522-footing-browbeat-6ce9@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Zhao Li [ Upstream commit f50b32aa233d4687af2d241ddcb572da969e8989 ] The LE and ECRED response handlers match channels by identifier without checking whether they sent the request. An incoming channel stores the identifier chosen by the peer, so an unsolicited response can bypass deferred setup, delete an established channel, or leave an unaccepted socket and channel allocated after l2cap_chan_del(). Extend the BR/EDR lookup from commit a518b9a9ef6e ("Bluetooth: L2CAP: Only accept connection responses to outstanding requests") to LE. Require the matching connect-request flag and add a reconfigure-request flag. Check reconfigure identifiers under the channel lock, since BT_RCVMTU can replace a request without the connection lock. Match all members of a rejected ECRED request, which share one identifier. A reconfigure request can outlive close(). Complete it even in BT_DISCONN, and call the close callback after deletion to release an orphaned socket. Otherwise deletion cancels the timer and removes the channel before the disconnect response or the channel timeout can close it. AI assistance was used. Fixes: f1496dee9cbd ("Bluetooth: Add initial code for LE L2CAP Connect Request") Fixes: 71fb419724fa ("Bluetooth: Fix handling of L2CAP Command Reject over LE") Fixes: 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode") Cc: # 5.7.x: a518b9a9ef6e: Bluetooth: L2CAP: Only accept connection responses to outstanding requests Cc: # 5.7.x Signed-off-by: Zhao Li Signed-off-by: Luiz Augusto von Dentz [ sashal: Adapted to the older locking: added chan_lock around the channel loop in l2cap_ecred_reconf_rsp(), dropped the conn->lock assertion, and kept __l2cap_get_chan_by_ident() since it still has a user. ] Signed-off-by: Sasha Levin --- include/net/bluetooth/l2cap.h | 1 + net/bluetooth/l2cap_core.c | 69 ++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 57315b9dc059e..16fdd88d61fa4 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -774,6 +774,7 @@ enum { FLAG_DEFER_SETUP, FLAG_LE_CONN_REQ_SENT, FLAG_ECRED_CONN_REQ_SENT, + FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, FLAG_HOLD_HCI_CONN, FLAG_DEL, diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 8aaa30e65b5b6..179559dfbcca2 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -189,7 +189,8 @@ static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, static bool l2cap_chan_connect_pending(struct l2cap_chan *chan) { return chan->state == BT_CONNECT && - test_bit(CONF_CONNECT_PEND, &chan->conf_state); + (test_bit(CONF_CONNECT_PEND, &chan->conf_state) || + test_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)); } /* Other channels may hold the same ident for a response they still have @@ -201,7 +202,7 @@ static struct l2cap_chan *__l2cap_get_connect_chan_by_ident(struct l2cap_conn *c struct l2cap_chan *c; list_for_each_entry(c, &conn->chan_l, list) { - if (c->ident == ident && l2cap_chan_connect_pending(c)) + if (l2cap_chan_connect_pending(c) && c->ident == ident) return c; } return NULL; @@ -5819,7 +5820,7 @@ static int l2cap_le_connect_rsp(struct l2cap_conn *conn, mutex_lock(&conn->chan_lock); - chan = __l2cap_get_chan_by_ident(conn, cmd->ident); + chan = __l2cap_get_connect_chan_by_ident(conn, cmd->ident); if (!chan) { err = -EBADSLT; goto unlock; @@ -6381,9 +6382,10 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn, struct l2cap_chan *orig; u16 dcid; - if (chan->ident != cmd->ident || + if (chan->state != BT_CONNECT || chan->mode != L2CAP_MODE_EXT_FLOWCTL || - chan->state == BT_CONNECTED) + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || + chan->ident != cmd->ident) continue; l2cap_chan_hold(chan); @@ -6600,28 +6602,33 @@ static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn, BT_DBG("result 0x%4.4x", result); - if (!result) { - list_for_each_entry(chan, &conn->chan_l, list) { - if (chan->ident == cmd->ident) - chan->ident = 0; - } - return 0; - } + mutex_lock(&conn->chan_lock); list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { - if (chan->ident != cmd->ident) + /* Reconfiguration updates ident with only chan->lock held. */ + if (data_race(READ_ONCE(chan->ident)) != cmd->ident) continue; if (!l2cap_chan_hold_unless_zero(chan)) continue; l2cap_chan_lock(chan); - l2cap_chan_del(chan, ECONNRESET); + if (chan->ident == cmd->ident && + test_and_clear_bit(FLAG_ECRED_RECONF_REQ_SENT, &chan->flags)) { + if (result) { + l2cap_chan_del(chan, ECONNRESET); + chan->ops->close(chan); + } else { + chan->ident = 0; + } + } l2cap_chan_unlock(chan); l2cap_chan_put(chan); } + mutex_unlock(&conn->chan_lock); + return 0; } @@ -6630,27 +6637,36 @@ static inline int l2cap_le_command_rej(struct l2cap_conn *conn, u8 *data) { struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; - struct l2cap_chan *chan; + struct l2cap_chan *chan, *tmp; if (cmd_len < sizeof(*rej)) return -EPROTO; mutex_lock(&conn->chan_lock); - chan = __l2cap_get_chan_by_ident(conn, cmd->ident); - if (!chan) - goto done; + list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { + /* Reconfiguration updates ident with only chan->lock held. */ + if (data_race(READ_ONCE(chan->ident)) != cmd->ident) + continue; - chan = l2cap_chan_hold_unless_zero(chan); - if (!chan) - goto done; + if (!l2cap_chan_hold_unless_zero(chan)) + continue; - l2cap_chan_lock(chan); - l2cap_chan_del(chan, ECONNREFUSED); - l2cap_chan_unlock(chan); - l2cap_chan_put(chan); + l2cap_chan_lock(chan); + + if (chan->ident == cmd->ident && + ((chan->state == BT_CONNECT && + (test_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags) || + test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))) || + test_bit(FLAG_ECRED_RECONF_REQ_SENT, &chan->flags))) { + l2cap_chan_del(chan, ECONNREFUSED); + chan->ops->close(chan); + } + + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } -done: mutex_unlock(&conn->chan_lock); return 0; } @@ -8493,6 +8509,7 @@ static void l2cap_ecred_reconfigure(struct l2cap_chan *chan) pdu.scid = cpu_to_le16(chan->scid); chan->ident = l2cap_get_ident(conn); + set_bit(FLAG_ECRED_RECONF_REQ_SENT, &chan->flags); l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_RECONF_REQ, sizeof(pdu), &pdu); -- 2.53.0