🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #7839

[STABLE v6.1-v5.10 2/2] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn

#7839
From: Lee Jones
Date: Tue, 27 Oct 2026 10:02
90 lines
11039 bytes
From: Pauli Virtanen <pav@iki.fi>

[ 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 <pav@iki.fi>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
[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 <lee@kernel.org>
---
 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 88ddd8eabae9..445a43c721f6 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -630,6 +630,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);
@@ -7431,7 +7435,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 b11558b0dfec..655922aa82f1 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.56.0.307.g5fe7bd680c-goog
Technical Details
Message-ID: <20261027100242.2886699-3-lee@kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-352342-linux-kernel-netdev=dewy.ru
References: <20261027100242.2886699-1-lee@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-352342-linux-kernel-netdev=dewy.ru
From: lee@kernel.org (Lee Jones)
Newsgroups: linux.kernel.netdev
Subject: [STABLE v6.1-v5.10 2/2] Bluetooth: L2CAP: refuse __l2cap_chan_add if chan already has conn
Date: Tue, 27 Oct 2026 10:02:40 +0000
Organization: Usenet Network
Message-ID: <20261027100242.2886699-3-lee@kernel.org>
References: <20261027100242.2886699-1-lee@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1508614"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:nAqs5q86T13rK5SsBb75aNrcUvk= sha256:Q7YYicF2/KdmA9Q+wwoAg+zFWINB53RMGclMS81GlbA=
	sha1:f+QVd70my0VqEjf/L9WcP6W9yh8= sha256:3ME5zoHsxwH3UJH+46KK7FGYIF/O/y+VEBvH6VAVaR4=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id EBCB5125BE7; Tue, 27 Oct 2026 13:05:45 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id EOcz-ZuWJA3A;
	Tue, 27 Oct 2026 13:05:40 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-352342-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 47D9C125BE5; Tue, 27 Oct 2026 13:05:32 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 721BA2C6BD; Tue, 27 Oct 2026 10:03:03 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793095383;
	b=i1cWuPJC4W+1AwNNMuzve4aSzbEIK2EVkowxEKhdQEAEhioUQBN9qFiIqwQh2hHGM3j7
	 GdkfITPaqZTCSmNRTCv9sK173Ubae91iwlAqPed2aWoFxP99r/l/R0s/znI++YKK4gXKJ
	 2TYXU3PleqTxE5pP4B+sCLR7yun2njhV6ImVffvbqm7DPu7pyRxudioOTXCnJMXHnO3vK
	 uhe63+rBwlnSiVnVbG9GG1skBEkFq3VjTVneBghOLuT/DUnvJCCLnKd9bH3u3ZGoj9N1e
	 EruKBIo/R5qrTajYIPJViOO5Pvy/ESGInHWdcOWLvSS8ocE3WCgqwCgLaAiUE/4sJDQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793095383;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding;
	bh=GGRD4rHxzGR/0Ca4q08YEFPKFTP39F+dpDG023MGztQ=;
	b=jzCJ0Otz3h0pA2bRhhARXTz6OM4sC6HgS7bta7KFrtbFdYohMjX+9IhwWQ033hp2r8Lr
	 bU6AcurW5HtTo6Qrafhh1ArMvVIFDd/HrCD3qATMFRDVeJLidf13sIzSwtrxfSwn8dQ3s
	 oyE3NOTn7IT9v3usMwBOUzO6/rmB5dZUwoUqsZu6mSZNfJ9sf0YhMW8/Cv1W+YkfwFxbJ
	 O+VUpqbPMmCfnvObv/Go4AfDfoQ+l4gmRS0+cwvCalWe/IFznMqYIT7daJQWEgwdisjMy
	 C5diCRRetXFARECuEI1+Uc6Glzt15h04H/f6S9sL++cW1ah7ljzGWU8WqGhflD7mZAw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NAV7po4c;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 0C5B3606A326;
	Tue, 27 Oct 2026 10:03:03 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NAV7po4c
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793095377;
	b=p9cikaIbed22+n5vWYrN106NzjbdXtcNthE3MVJq3y1IzqPSwzacY+128sN6LXLDWEa8
	 OZDEY9BxXhNGs7Nx5B4LqoSnZtecEoX9oLMDJtLUX2FBQzVAS336DiIzGm+yvdlpmZm7h
	 Bi6vpLAD/v5m76DxYcDSTFPhATM2XBmYAVfX2uki9ze9FMjG0qWwe2X/D+Cs88sfST9oY
	 292IJotY66n707boZ2vgZAJqYMuVrl3B7Mc8bRhDb5A7Y/vWx8/FFSTV9UwX+4K4XtKZl
	 byaLgLUDg1cts3GyglcHetxkPSUc6OpOx1PefWJN4H1m2Gg5vyVUk+/X1WzRnImCRrg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793095377;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 In-Reply-To:References:MIME-Version:Content-Transfer-Encoding;
	bh=GGRD4rHxzGR/0Ca4q08YEFPKFTP39F+dpDG023MGztQ=;
	b=LEs9T1bpK1RtHE085TB1NH0LhE45JqWqz8Sx/mNjR7ztXVSWqLqia1Cs6e2yWBepwwMn
	 9y/dx+Sq3Lun4eu7LJtokboU/jWJ8BTp1t32D7nXFyYcG6uC5xcW/GmVy3vZFYRUPnKst
	 OdSx7Pt78sa6ncJKExfiSTbY621CUqBZ5BAn77jXkF0itphNF14YW1bWFz8a1V5WGqVNb
	 bekMDMeJ/Q2teRp0hRCFN+ARYAkdyBfZ/UQjDsupA8rFBfe/r1T1LEJhlHf6//CirPPii
	 u3N2LdnPqo57n8tDfV9/FN+xtcUqmpW51prfdEMVCRqTkZjKJ4MenuohVS5CUKUbrGQ==
X-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=NAV7po4c;
	arc=none smtp.remote-ip=100.103.45.18
X-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 E1865606A331;
	Tue, 27 Oct 2026 10:02:56 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACBA71F031E2;
	Tue, 27 Oct 2026 10:02:53 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1793095376;
	bh=GGRD4rHxzGR/0Ca4q08YEFPKFTP39F+dpDG023MGztQ=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References;
	b=NAV7po4clxqLOB0eFidpYCyquPlfwwLy3U5lnPcK+H4LLuBw0jnMIum8/5BA49Ksc
	 lg6lmt6cfNZJlbbtAlRaCiqtUpuO6DoZIVUALueWJ5JzhMlRkV5f31f61wcUNdSBoK
	 EjTNsl9LQZ/4G3EWSXk8WtF3wk5bDG/sgS4fVzT4tZLLF5/TI7OD3fC9HV2YEKnxk1
	 uqZxNRKLejFQ0Hl/sX3vmpu6X9F5ZIFsbd81idFC7utwcYr+u7cvl9Q1da44GlxI4M
	 GfGsJsDZQoouXZseBHsYzbK0+D9UAy6cfwBPrtu5gBZQOe6nCYfhhVL5E86fJIH2u1
	 Fs/VvBq6g3LHg==
X-Cc: stable@vger.kernel.org,
	Pauli Virtanen <pav@iki.fi>,
	Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog
X-In-Reply-To: <20261027100242.2886699-1-lee@kernel.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list