From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796073153; b=k9CuOBawrwQuxzykHXv32tVIYQeYbUBFdkFoW+gsHSYN8IRSWGXaSEwItBfta5N3L5Bm o+xE4B7oWxezH72vuvu3y+HTWOLPl/25w6xcjktsIVhoSDaDYlPRoc01mVOZMdRRjehRc aGwffV2wBuKmWXFK0BFAvR8ICHC87A10iD9q0gtebUb13Vqxz50sJiUob2dSNY8pBZihN vJMlTQKtqxRjG7GVwqIFvkXg7ZMwEvBTuUem4P4qdWTxLb+VwO/K29uN4HPTQxRVVRXuK HJd46N3NDu2A6DPQnFGqliLrv/hzlCUitMaZrqaAuIjjgDG1c82F6J77pTdpoUzzWqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796073153; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=iLwyy554AoA6aHO/D+/tC5ZyJAW7nBIjsvBOrXONl64=; b=n9biZe6tTBADjvBrN9pesCiREW9PAVHufHPMZ+OGyqPtUbEvTx/HYCzOxnmZ7usB8Jmd cDlBREfXHpuMf1afnTxyqOXCE+OEGl/ittKOFuvg2FlnbrViMeQNw6j6Jwk+A4v7IwwC7 Ut4c0AFMHXu4ZMDX0PU6O6TbVBzGG2hEXdxJs6jWLc3FB/6uSUcMgAaaBymDVI+rbSpZ/ /aIQq/KlPAczrc6GSjSHkxrPgy45KS7QV59KrwG27y8RCQghUvVavW/TDrgxKkspFaZLt zHUxxClPji6I/eEb4xa3bANBdUtLNEFeNEpGh1MxehHeHPj0PLgVsChz1e9RfwEJgcQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=KDAUxXht; arc=none smtp.remote-ip=209.85.221.182 Received: from mail-vk1-f182.google.com (mail-vk1-f182.google.com [209.85.221.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7879E606A32F for ; Mon, 30 Nov 2026 21:12:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7879E606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=KDAUxXht Received: by mail-vk1-f182.google.com with SMTP id 71dfb90a1353d-5ed2f163a2fso3547868e0c.1 for ; Mon, 30 Nov 2026 13:12:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796073152; x=1796677952; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=iLwyy554AoA6aHO/D+/tC5ZyJAW7nBIjsvBOrXONl64=; b=KDAUxXhtUTniQcxEpRL2AXC07SyxMWRWY9PMgRI+6eY+xFZKUTgYh/pATPDTSoMvga uesNMJI45I9GexZ4L8vlcoztwg8nLERYLIHOFHVA0QUzWwEUUVUwyjBedZ5lv1Vnswo3 V1+hbVrGNtSh8X2xsgYMwYQZRmG5oNb3fss3h8G0QIZ96HrRdAJcBNWfwKjorwC4ASc6 0CflTyRq8dj7s85xmCiTR0kGn6F7jACHh/5x/SW24ccY5x3D+Q8JIXfutnIEJB9Sbvm+ 5wIQ/hWGZoVn3PuJxGl+w2dKak5Hpce6gnNQLd65TifJ2sLoNp+tI0+k+pCRyoJZFtC5 ukxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796073152; x=1796677952; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=iLwyy554AoA6aHO/D+/tC5ZyJAW7nBIjsvBOrXONl64=; b=RxDsfsS/2T6mkZT9YXobrQzVOgO2QOBVNj4ZqOaq119kCcOmmjGt5CrKEX/mEAnTRk f3wesclphaievJ03EvPnS1L1nKlylLwC84Gl9ifCjfcnE8u61vuvpCMgI/ItDaqL55i4 A6jKkTqawvDZNipM67xh+qI1NICVsMmQvRXsgtXMlOXxhWRQJZXng9A1+a7HpyoEFqpJ mPXCUuREhld2JNsXD7AyUgFUCvekIP4FO0QSSKxrm3rR9irnB1AHyjBtTZ2VfNBVwMTf 1uQVbanrkuaSl5zcIRAxbkPDTKvPhStmz+ar2SyIMd5uXJsrh8pLdsnDed1DKo0DvhyW qdvQ== X-Gm-Message-State: APuU4Z9lvzwleqjrngSlVr/y2wJ8gjbCqj99HToqrfIiC2rj1e8R/xYZ 9gH7AKwGZS9BwEtuXFC861FPxE7mgfMZs8a42eLEjK0LIF5a8zU0QEYQ9VuGidv5DMM= X-Gm-Gg: AZFdTZdeveTKihaLCA8Ucpwfc2aRuJ+WqfW83meA5vonvCwP34kM9zgfOVMW0j5tXbz z+uMnU3TfBcmFvb3h2fdhp8xqk8yxFyo5hBw/ATvtQrckpuM9gnCIZtWVmgtSatZkEDaacIVGY7 A01eiKbHoMCGo7qAW8bcCI8cKyxWAG21tW4VRTDgGAErGLObdR7WWuY0d6dIoNuN2iKrbSMAgJq JqihNF8YejoEjDLvfRhHrMysIX20YWCVt+3p+WjPTmPQXZmn8pc2F137DHtCtWRJJfttCQ33tlJ fSY9qw+NRc3aiY/5ZYZn/qzFeJld9xcuqsSqDlwco0iKH8nysNlk762zUR4C5a82Nt6Ts9I130b 0YND+xEiPRpZY3JypGKFlsajAObJIJVnZbfBzrgvfXonFQY6C4+dyU8i81fRTzV2W9/4EkKVuf9 olUjVyhKuYowRaEOSQ/rrx32NfFQA6rXdFkXvBw/Mg1nfnQfVT7RBqqx9V6fbj7uti3x8j7pHoC fZTkJbMjlfJU0JpVbduSkHOCnKwipA0VJNrpimLReqmS5SE4J+XDRjL6/8TU6o7ObdOMnL3IAZu FAs84sUPTBl9moo1LuKenbaXNWbCXvr1Qlk9NtPWbHSo6ouvq8Mg5MK3S9OezRXB+R7vDd0ihgB PLAKfkK/421b5IlA1mFTrUqF+XjudKEVOc8Y8LVY5W+RDA1UIIbkaMg0K3hzgqHftWTYdO3Mx7Q P7tO8= X-Received: by 2002:a05:6122:1348:b0:5ea:c214:b5cf with SMTP id 71dfb90a1353d-5ee8adf3624mr22446985e0c.1.1796073152328; Mon, 30 Nov 2026 13:12:32 -0800 (PST) Received: from lvondent-mobl5.taila91cb8.ts.net ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5ee8b609bc7sm35472261e0c.12.2026.11.30.13.12.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 13:12:32 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v9 2/5] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Date: Mon, 30 Nov 2026 16:12:25 -0500 Message-ID: <20261130211228.621145-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261130211228.621145-1-luiz.dentz@gmail.com> References: <20261130211228.621145-1-luiz.dentz@gmail.com> X-Mailing-List: linux-bluetooth@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: Luiz Augusto von Dentz l2cap_ecred_rsp_defer() responds to a request that has some of its channels refused with a rejection for all of them, deleting the channels that userspace has already accepted as well, e.g. when the listener limits the number of channels, like bluetoothd does for EATT. A L2CAP_CREDIT_BASED_CONNECTION_RSP can accept only some of the channels of the request: the destination CID of the ones refused is set to 0x0000 and the result tells that only some of the connections were refused. Fix this by responding with L2CAP_CR_LE_NO_MEM if only some of the channels were refused, with the destination CID of the ones accepted and none for the refused ones, and by deleting only the refused ones. If all of them are refused the response is unchanged. Whether a channel is accepted is decided per channel when building the response, so it always has as many destination CIDs as the request. A channel of the request can't be deleted before the response is sent since it has to be included in it, nor be disconnected since the peer doesn't know about it yet, so closing a channel that is waiting for the response refuses it if it was accepted, and leaves it to be deleted by the response if it was already refused, e.g. when it is closed again by its owner or its timer. Fixes: 9aa9d9473f15 ("Bluetooth: L2CAP: Fix responding with wrong PDU type") Assisted-by: OpenCode:claude-fable-5.2 Signed-off-by: Luiz Augusto von Dentz --- net/bluetooth/l2cap_core.c | 104 +++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 9 deletions(-) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 943effc86740..1767de519089 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -864,12 +864,41 @@ static void l2cap_chan_connect_reject(struct l2cap_chan *chan) l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); } +/* Check if the channel is part of an Enhanced Credit Based request whose + * response has not been sent yet, which is only sent once all of its channels + * are authorized or refused and has to include all of them. + */ +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan) +{ + return chan->mode == L2CAP_MODE_EXT_FLOWCTL && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags); +} + static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) { struct l2cap_conn *conn = chan->conn; BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); + /* A channel waiting for the response of its request can't be + * disconnected nor deleted since the peer doesn't know about it yet: + * refuse it if it was accepted, and leave it to be deleted by the + * response if it was already refused, e.g. when closed again by its + * owner or its timer. + */ + if (l2cap_chan_ecred_rsp_pending(chan)) { + switch (chan->state) { + case BT_CONNECTED: + l2cap_chan_ecred_connect_reject(chan); + return; + case BT_DISCONN: + return; + default: + break; + } + } + switch (chan->state) { case BT_LISTEN: chan->ops->teardown(chan, 0); @@ -4065,6 +4094,39 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) } } +struct l2cap_ecred_defer_count { + int pending; + int accepted; + int refused; +}; + +static void l2cap_ecred_list_count(struct l2cap_chan *chan, void *data) +{ + struct l2cap_ecred_defer_count *count = data; + + /* The idents are not unique among the channels of the connection, so + * only count the ones l2cap_ecred_rsp_defer() responds to. + */ + if (chan->mode != L2CAP_MODE_EXT_FLOWCTL || + test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || + !test_bit(FLAG_DEFER_SETUP, &chan->flags)) + return; + + switch (chan->state) { + case BT_CONNECT2: + /* Still pending accept */ + count->pending++; + return; + case BT_CONNECTED: + count->accepted++; + return; + default: + /* If not connected or pending accept it has been refused */ + count->refused++; + return; + } +} + struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; struct { @@ -4141,14 +4203,29 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) chan->ident = 0; /* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */ - if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) + if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) { rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); + l2cap_chan_del(chan, ECONNRESET); + goto done; + } - /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + /* Include all channels pending with the same ident. Unless all of them + * were refused, the refused ones are responded with no destination + * CID, which is decided per channel so the response always has as + * many as the request. + */ + if (rsp->pdu.rsp.result != cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION) && + chan->state == BT_CONNECTED) { rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else + } else { + if (rsp->pdu.rsp.result != + cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION)) { + rsp_flex->dcid[rsp->count++] = 0; + rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); + } + l2cap_chan_del(chan, ECONNRESET); + } done: if (locked) @@ -4161,8 +4238,8 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_ecred_defer_count count = {}; u16 id = chan->ident; - int result = 0; if (!id) return; @@ -4181,13 +4258,22 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS); /* Verify that all channels are ready */ - __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); + __l2cap_chan_list_id(conn, id, l2cap_ecred_list_count, &count); - if (result > 0) + if (count.pending) return; - if (result < 0) - data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); + /* Only some of the channels may have been refused, e.g. if the + * listener limits the number of channels, in which case the others + * are accepted. + */ + if (count.refused) { + if (count.accepted) + data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); + else + data.pdu.rsp.result = + cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); + } /* Build response */ __l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data); -- 2.56.0