From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796138939; b=FNOFzl9518azIpR91WOia8gkbRyOoIqgpAi7u7jSB0c6SEoj9/DaNeEvkfGWuTzoV1Gc kZSzmUKz3Il/WGDeUmqtCLofCY7Qic5A2aBMX2iKpo88a+S4huysqZIhUuGHylZTKPu+h vC3BP/PYUPbcMtDEMGcAUMG3Qmtxu02AO3/+GOcknhmUdU0npjFoPibkRR6mW0c5QFTlZ 1mOfXmQ/4UQCuAiwLY/PmBtnUIn/l5nPZdsazh4i9OLv7yN5Kv4z7UFIvZ0M/FAAcVmlO 6Pz+QpURO4ZOS7YwgcXjFtIIkb5zq3qm9KoDeBKOqbh431TcxzOJlIJxSseeYuAiteA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796138939; 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=VSeC5jI85Ky+9MgBvuoGpkGw0022u7OSCesTqD45aNg=; b=zBWln8z0wWptS2N6tN1ofCD1YTxaR1W/fjwCJmFsvDr90ffPcQle9B7dyiyQIm+z/EbP kx9B/HrTzpFvHKmpW2w+8YuZOuiXYir7lSlVrRt1uiNkzaZ0i3Z8mTfsmJTvly3ks582I udD3q6gXXzZLClAdibitxETMTJTGJ2HVC+3xdY7+RsCQz3IWUnoFyLoJxCoB5e3dMyNyZ QXGyhVZjG9XzyeQ4MWsBp/RyKT/Z6/Gb88DCvlIAF54dmjMqygjdE60PevDDHrEPwGiHl nZ1FuSxbtAeALK4m+vw7y+1hyQ2BUxQkjwcmAP0Tj3uABLau/7oc2Pq2e3+vL5KHzZg== 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=w1Fv8Och; arc=none smtp.remote-ip=209.85.222.46 Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) (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 5880C606A33F for ; Tue, 1 Dec 2026 15:28:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5880C606A33F 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=w1Fv8Och Received: by mail-ua1-f46.google.com with SMTP id a1e0cc1a2514c-9912c47367cso7681474241.1 for ; Tue, 01 Dec 2026 07:28:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796138938; x=1796743738; 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=VSeC5jI85Ky+9MgBvuoGpkGw0022u7OSCesTqD45aNg=; b=w1Fv8OchT7cOFT3PQtwxEmt+uduRXphFMibJBR25TNIhkVUnGRT/tBIpXCt/J9qKNQ OjwLqCQkBgQj2bLICUmZU3wi24yurn7Qn0X1a3Zu0qf51yEzZgAC8BaeGyNlarGRnrMr DLQ44ZIak8JC8a1AzDACB7qKTL60zUl0h5RftNoj5VHkK4Ytr3edcQ9+g/QIIRErSAc8 04cmDErzBWuo8kZWzJZULw3/ml+eoXicPmqm3Y731pysQc8upMMniTLWYsrGad8L/P4Z ItdBXtXsYtzVny3gcemdYZ1WL5HQZNpC4T8eRgaVIBfnIIy3R6sbO8Z0V6lqgwgxydS/ Kg6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796138938; x=1796743738; 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=VSeC5jI85Ky+9MgBvuoGpkGw0022u7OSCesTqD45aNg=; b=NhYolRo9YoN2hAwY8cPF96ZA3gBhEUogkp9giyukRPXtDJrz74eEQZpP0OtpCJK/BF l5TIHDNRke0ipT8AxhMe8dbilOgDRXt+Ipnibi1EKmOVHL79zLTFfaQYRF8k5DqsZh7M /x7z2vDFTBUoI1ia/RGMxAs+5tfAFCm9Xz/BaUO3ISTwAinXtQWnbEFl2sNjxUdAeG8g 7FM7riUtm7LV9lTC5OoAaE/pJFFnpOMDVWmxSMnGO0x9FaIn2rbNgJpG7OIHuU/Fa7zp 92pUpZXqqPDegDrllC8uhAUumU0XrQim5TiFlHIbiEy1lScFwjehhsM043GB84wYi83f AJbw== X-Gm-Message-State: APuU4Z+WyX550wqaBQvWytMwpLrQm4dqddUKa6HgBn4FRllWqFmLhmd9 gxxBYntkQV6t1EhpRVFS1ld0hWrilGmBbDYZZHuR8cTF02NiEsq9IychuJYWMuiWfN4= X-Gm-Gg: AZFdTZfOZVVOfNd3JXRYfndW8uGxgHzpi1UMdv56apu3quQI8h2Km1J3WspJ0tiy0zo 6QJKVX2H3F/v6DHL8sYXAM7WrMmfZfhnYTAPzwDURJ8wZhgQbVvgjf4MCWuDhB+JG75S1FIT1yV E5mj5LZf5+GNvEU7k3/qikgbi01dwmPuc6fi8Vp/Mw/p1DNx2GHdBu3VadyCCXtXF3s7Pyivhuh r/sZx11wIiLfzqHGYqbbhVVLLepdPQjzuShQJx2D0o9O1gGFC0xzKiwah/h7PYRuOXvLcPvpiii 5DUzpgPHuf7exFIPjPTES5w5UyZC/EIOtJtQFvlxaYM6wHN4BoqXWD7byzT1Q5uqaxu+PoNAjAs bj98PMY77Y+FLaUJrf96r1jdYqRaprX0KDV+2hsPFwh35jTLoTJG5LNCr2872CjGxMUk7tuJyxS ynVoIW77geybwRPyZFWjlysg+9zOaDYKHhgbx6NTWM25+9Pdr0Qjzj/n8/8lzHGu5DUaVzeM8/N L1gS6gGbU8MLEogxK5gGYFAxA6H6+Xo1pdRnF0WhSORcE+r8TJziJ/KtmTvz7MPWLioXbEhWU/7 v5Dg/z60gfT0xEyweLZSLi7O204ShMLVp53CxmlRQvCophQVz1B+zntCnb66HJ7zzm0h7j6YatG vQTQ7KPdFPqxV4E6mYH+QNkQo5mqJwHIov5CP0zwSrWTcxgzSTFicqFp+KoN3eIppLZKBe9fQNo ll5c4= X-Received: by 2002:a05:6122:4a1c:20b0:5ec:e7e4:a1b9 with SMTP id 71dfb90a1353d-5ee82399926mr14279535e0c.1.1796138938127; Tue, 01 Dec 2026 07:28:58 -0800 (PST) Received: from lvondent-mobl5.taila91cb8.ts.net ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-99e6df8a29csm37603297241.5.2026.12.01.07.28.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 07:28:57 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v11 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Date: Tue, 1 Dec 2026 10:28:50 -0500 Message-ID: <20261201152853.1118148-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261201152853.1118148-1-luiz.dentz@gmail.com> References: <20261201152853.1118148-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 only once, per channel, when building the response and the result follows from it, so the response always matches 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 | 86 ++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 411b26b3de72..b0c0541dc367 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); @@ -4070,6 +4099,23 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) } } +static void l2cap_ecred_list_pending(struct l2cap_chan *chan, void *data) +{ + int *pending = 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; + + /* Still pending accept */ + if (chan->state == BT_CONNECT2) + (*pending)++; +} + struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; struct { @@ -4077,6 +4123,8 @@ struct l2cap_ecred_rsp_data { __le16 scid[L2CAP_ECRED_MAX_CID]; } __packed pdu; int count; + int accepted; + int refused; }; /* Lock @chan if it is not @locked_chan, and has same or lower nesting level. @@ -4146,12 +4194,21 @@ 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); + rsp->refused++; + 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, which is decided + * per channel while building the response so it always matches the + * result: the refused ones are responded with no destination CID, + * which are dropped if all of them were refused. + */ + if (chan->state == BT_CONNECTED) { rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); + rsp->accepted++; /* The response establishes the channel, which was already * moved to BT_CONNECTED when userspace authorized it, so @@ -4161,6 +4218,9 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) if (!chan->tx_credits) chan->ops->suspend(chan); } else { + rsp_flex->dcid[rsp->count++] = 0; + rsp->refused++; + l2cap_chan_del(chan, ECONNRESET); } @@ -4175,8 +4235,8 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + int pending = 0; u16 id = chan->ident; - int result = 0; if (!id) return; @@ -4195,17 +4255,25 @@ 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_pending, &pending); - if (result > 0) + if (pending) return; - if (result < 0) - data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); - /* Build response */ __l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data); + /* 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 all of them were refused, none is included. + */ + if (!data.accepted) { + data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); + data.count = 0; + } else if (data.refused) { + data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); + } + l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); -- 2.56.0