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=1793210225; b=MinYPMxsUMIIKdzUA3vvwY4qkiwbG+AVH5Umi3iAlhUdAjJTcXjnAfSoFxwExnPwJ+Kv qe2bdu67ucGXBPpUuI0s57cARq4tt+Ysd9EYsEnUChiCQO9adD14mttvY9uiJ+tD3Xrdf GyB1qtFtVclOSLCovrhBeDTNyW+Q7zhaPf2Zvy0nyKez3xSaFJv6A9JMfm8PA6dfcJSB8 lfen+drxiRDuL3vfffmrs3kTXYqb/egtdDMIKEoMCN9PNqacjEbBM3jKBTYCwafc+CPhH lRS/haROQfyNe247ZHl7X0DQVb6QGS1DhND7bA4KCSKSpdxrzrm5N0tX1bcqqtHZ17g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793210225; 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=P8PJhNTKBsOBBPbskNVJHMW9eCXW/cfGjuBhUQnxJc0=; b=EXNAhre3EfApI7DBqWWlsb9v8DL09YGEonP15CyqTAvA8h3CUhBiePgItzv/Tniy9Ch6 TCOI3H3hHsNREcxh1y1vGdiEFrKACa1N+VGrtRvff8m8BYx/gU/KUu6Yjr66rPUOQqCyJ 32CAO9cZHCFBU8r793vcYHDWSThuxQMt7FS1ziSeybfxBLcaof3yplU6VZMwnop9HmPv2 BbslJpaL3rVRiEsl2QFJ0L5NjJqjECu4vuMU7iaq59Vsrzx6JDe8SfOaqdnHmFCiP8WWH C7vaCSWDt3VKzAFD2rz47+vXlWT0bo6nhFtMW8ig9IpvXKlbfGpFS17P1ocN0A+wjdA== 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=bl52fiEL; 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 91DEB606A31F; Wed, 28 Oct 2026 17:57:04 +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=bl52fiEL Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C61A1F01825; Wed, 28 Oct 2026 17:57:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1793210224; bh=P8PJhNTKBsOBBPbskNVJHMW9eCXW/cfGjuBhUQnxJc0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bl52fiELH+RQnmcaWCq0fqc/k568uwhQtyNEgFQIaMRj7YAotfqtp5IOpSwpcOrqI rO6HOIaXRTXB6XeTiIX/YusWblgJ5BgY17POIR5grj71M7uTg1CY1EXta3dPUTJiwR WpB+5kxvHz/+EpAMmiUSbk5pZHiP5C5PnFURv9Kg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Pauli Virtanen , Luiz Augusto von Dentz , Sasha Levin Subject: [PATCH 6.6 120/168] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn Date: Wed, 28 Oct 2026 18:00:56 +0100 Message-ID: <20261028165945.929156483@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261028165943.089486480@linuxfoundation.org> References: <20261028165943.089486480@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 6.6-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 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 c2bd2b25a809c..94fddc456ac57 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -625,6 +625,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); @@ -7332,7 +7336,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, } } - 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 c0da2b1168a1f..7d2e59e97c2c1 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -111,6 +111,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) { @@ -176,6 +177,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