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=1795623602; b=FN5OAxSfR/NHt2YGNWDuU2WiWPTv7wQrztxC91cJ8VeCNtEjZ9zLpAUIUDSIG9Eq8B+o nIzQ76xoDtvl/r0ym+kc68dvW8FQym4Z8Nc/slWgC4fsvQYQzEvYNKQig1Z9CJjeHKcmc 4UMZc9Za4UrGlccZlen3n10Wm1qg5ToKxNOftnmHDuee8WwC+wjjUCwwGYHzErpnqEBFZ X9CseEbgB/gzpiKWbH3Yo2Bx26g1nYIPrOGdR0GdW+dV7akp/3UWc9RgTtPR8gZMe6/Wi Vmr9mX3+fKjV5kraVkcPjfzz1iNo4MGxtz+e9u+Wp7pBpwhAAOCysdef1h3Rw5dJCsw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795623602; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=JTV7cRLQT3jqNzXXBomShns3rh+fa6iqZLVKtSOEybE=; b=FvOxClC4D4IkW1dplhLP0XRl1LPuuyjmuCxidEyWzix3w9Wt/5J/GDOn6OCCxqzBwYnr 4nN+BibsXhDY69ybweGLClL6ynDcEYjvWSug8nPIHZGj62oBHAsh5M15OU980vbdQpasW FeyuoFcCll81b5Pn8MtyWtL1vNNGl8bzEauzZn5UfpViSBK90OsksDuKv2lnC+WrRI1yw BL1JQXxxh2xANUMPTkjjNPW9BfIjclqFXxZZoIvVsGPbAuCtW48s9vfVGgP+/j7LPyg3W Lggb+MpQyRC7B4NmMgbU2v1H2LCA2QdbGHBYk6U6lKzgq/+YH9/Mfl0JAYHnDohUULA== 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=R9jQ7lBN; 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 9EFDD606A331 for ; Wed, 25 Nov 2026 16:20:02 +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=R9jQ7lBN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86D501F000E1; Wed, 25 Nov 2026 16:20:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795623602; bh=JTV7cRLQT3jqNzXXBomShns3rh+fa6iqZLVKtSOEybE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=R9jQ7lBN6LKGs43eKn35Jf19k3jWrMhEZNClRFgiS36jQhbkK2ykQKYfkraKGPO9t M6VUx4BF3ukq3ltwvWKb4/twfMFOVV02Q1/MsXwmH/UG3iuW59hLfo4cmnjjy0Eflc HE5GtOZRIoA9vvUWdHQY4EmL5zuExEpJduJsNSrzIkyZjHW2UgSkt/llHkPk6JDLa4 B4z2OVqo5AaO2Yrl+aRKKx4thnmHSOI61DGXYkxjEAGRBje+dw05sn0JEYslepN8oo DM9MxuosK0y0C6whxAzQp6SesTUH5wQD8Z7xvwe8DtMZyeJNoPqyHCXXN39anKN19B QTvNx7SMWS1dg== From: Sasha Levin To: stable@vger.kernel.org Cc: Zhao Li , Luiz Augusto von Dentz , Sasha Levin Subject: [PATCH 6.6.y] Bluetooth: L2CAP: Only accept LE responses to outstanding requests Date: Wed, 25 Nov 2026 11:20:00 -0500 Message-ID: <20261125161930.765438-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026112521-oppressed-liquefy-0db5@gregkh> References: <2026112521-oppressed-liquefy-0db5@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 older code: dropped __must_hold/lockdep annotations, kept explicit chan lock/hold handling, and used a stack pdu struct with sizeof(pdu) in the ecred reconfigure request. ] Signed-off-by: Sasha Levin --- include/net/bluetooth/l2cap.h | 1 + net/bluetooth/l2cap_core.c | 78 +++++++++++++++++------------------ 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index f7f60d64933b7..6f91e1accda5f 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -746,6 +746,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 3fec67f521d10..92aaa06fb613b 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -149,23 +149,12 @@ static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, return c; } -static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, - u8 ident) -{ - struct l2cap_chan *c; - - list_for_each_entry(c, &conn->chan_l, list) { - if (c->ident == ident) - return c; - } - return NULL; -} - /* Our connection request has been sent and is still outstanding */ 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 @@ -177,7 +166,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; @@ -4870,7 +4859,7 @@ static int l2cap_le_connect_rsp(struct l2cap_conn *conn, BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x", dcid, mtu, mps, credits, result); - chan = __l2cap_get_chan_by_ident(conn, cmd->ident); + chan = __l2cap_get_connect_chan_by_ident(conn, cmd->ident); if (!chan) return -EBADSLT; @@ -5383,9 +5372,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); @@ -5600,23 +5590,24 @@ 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; - } - 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); @@ -5630,25 +5621,33 @@ 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; - 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: return 0; } @@ -7483,6 +7482,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