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=1793209540; b=BbWJX+2QtEU+EQibLZUNdDm9mHnDb3PxAKFkwmrkdsMyIrcFQmDfk/u1kiZxqQEpW44Y ar3XTxrEvikpzI66BpmZdjZejxbDRZ0PKHTAw3d3Yttbx0TeS9sJjCxPGcfi2RFz8Xs3T ZxSHzNMHiz6gsvtCykij80bsCk0aIM0NW4y0FTtOu4qrqJ8GT/qYOA36OYOj9TR1gKdPu O8wbVQIn8Yp0UZhQiu3zftX+u5dc51t6qTvnjb1v7pUv0Dobw73EOszda4fyGXf5jX5Ey +hqXYJTBTML2swXlMuTmDvvqw9mNbamE8ZqMZ3DnywBERE7+yNJADAwya3tgmmnctfg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793209540; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=BvIKkJgQupCKFbiWriP3dsH6hQ4szxq6vVNM0fBoLSI=; b=X0YwwVonzLC4uo2UzgOtdYR2g+CEOaPT+lMPNM65T6c8M4UT3Zchfy9rybwn7YcNw6oB B802nbh0CboyiUltmVYYqAHNLKYrMwLRaSQxepnDPIS5OZ4pr6MrgkdOc6QYHT6TcYg5k xIemebyMdnPLkndkArcqXjqOFrEsEHgtpReuOwSorHf1UtHRViFq/XvwMJFDBscQ13d5Z rnrVshT8UOPg/NuH8hIFK37maTCw00jlc8TPUbiV5BuWU7OxNqiv/DsexRd2P2Exrhd04 HfMXmiK8n1uFMrZCHfc40OvwoNhZP2F9F68nQcQMnSyb6/nIoDsRdt2nkBOmnbEtNjQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=s6lGR19Y; 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 3D9D3606A31D; Wed, 28 Oct 2026 17:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=s6lGR19Y Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E6211F01828; Wed, 28 Oct 2026 17:45:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1793209540; bh=BvIKkJgQupCKFbiWriP3dsH6hQ4szxq6vVNM0fBoLSI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=s6lGR19YIUAUzSNAU+6LamE0/Ak1Emm2VsgyS/IRmhiPqMMRD+W9Ffek9gpmmm/hv e2YQotuoDCWYK5dSb1UnUHO2w/fdtdJfgvVagiCmI9d9S3q2RDOQIPxi9/aisCWXCV 6ZQP5mZTaG4jsWd0ZZNYB1NuOc86rI64Z+GrqLYU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Pauli Virtanen , Luiz Augusto von Dentz , Lee Jones , Sasha Levin Subject: [PATCH 5.10 505/516] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn Date: Wed, 28 Oct 2026 18:00:57 +0100 Message-ID: <20261028170015.441337537@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261028170003.522232767@linuxfoundation.org> References: <20261028170003.522232767@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Pauli Virtanen [ Upstream commit 86b55991d3cd690e73c5ef8d20b75ae4905b357a ] l2cap_chan may be linked to l2cap_conn at most once. This is assumed in several places, eg l2cap_chan_del cleanup. There is a TOCTOU race where the invariant is violated: [Task 1] [Task 2] l2cap_chan_connect l2cap_sock_bind l2cap_chan_lock lock_sock l2cap_state_change if (sk->sk_state != BT_OPEN) chan->state = BT_CONNECT l2cap_sock_state_change_cb chan->state = BT_BOUND sk->sk_state = BT_BOUND lock_sock <------------------ release_sock sk->sk_state = BT_CONNECT l2cap_sock_connect() does not check sk->sk_state, so since chan->state is now BT_BOUND, subsequent connect() ends up with second __l2cap_chan_add. Explicitly document and check the invariant in __l2cap_chan_add with WARN_ON_ONCE. The only callsite where it could be hit is l2cap_chan_connect, so add pre-check there to avoid relying on chan->state. chan->state read/write is not properly guarded currently so there can be other TOCTOUC problems. Add l2cap_lock_chan in l2cap_sock_bind() to guard chan->state write. Fixes: b66774b48dd9 ("Bluetooth: L2CAP: Fix UAF in channel timeout by holding conn ref") Assisted-by: deepseek-4-flash # finding the race condition Signed-off-by: Pauli Virtanen Signed-off-by: Luiz Augusto von Dentz [Lee: Resolve context conflict around mutex_lock(&conn->chan_lock) prior to commit 56c2b5831d39 ("Bluetooth: L2CAP: fix out-of-bounds write in l2cap_ecred_connect")] Signed-off-by: Lee Jones Signed-off-by: Sasha Levin --- net/bluetooth/l2cap_core.c | 7 ++++++- net/bluetooth/l2cap_sock.c | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 15466df700193..587aad401256b 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -622,6 +622,10 @@ void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, __le16_to_cpu(chan->psm), chan->dcid); + /* Caller must ensure l2cap_chan is linked to l2cap_conn only once */ + if (WARN_ON_ONCE(chan->conn || test_bit(FLAG_DEL, &chan->flags))) + return; + conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; chan->conn = l2cap_conn_get(conn); @@ -8406,7 +8410,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, mutex_lock(&conn->chan_lock); l2cap_chan_lock(chan); - if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { + if ((cid && __l2cap_get_chan_by_dcid(conn, cid)) || chan->conn || + test_bit(FLAG_DEL, &chan->flags)) { hci_conn_drop(hcon); err = -EBUSY; goto chan_unlock; diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index 81faa83128a53..ca131b2267d8b 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -109,6 +109,7 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) return -EINVAL; } + l2cap_chan_lock(chan); lock_sock(sk); if (sk->sk_state != BT_OPEN) { @@ -174,6 +175,7 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) done: release_sock(sk); + l2cap_chan_unlock(chan); return err; } -- 2.53.0