From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796151816; b=CwffA/Sn2JE6s+HkZFN0r1ZP1lCC5TY+AIyv1B8/3TtaACAde8sQKe1KD5pVNOaVp7tJ emxt5Kl2wTBe/XkBJurfdONHTy68pgdWKGeZKqvVzZK1pMLSLtWMxATolQ8X8nTMdJ5JV 94tsbvZe9ImXrXBAV5nKSWssbSxd1d2vomlzkCXDqD7oEhFlFTyYXDN7yrKgwWPfOO5W1 FBVLnCNFQddLS+JT6FO+m/VMefZQNBysaRmamutXrcZTq5peCqHZKrxLNhm55FbSkq19t O71dbv72GZ/i6hMvd5RYKRQlEQH2GE3T0KOC6BitonRXo9ANCdEczg1rkl7z7TFndOQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796151816; 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=ggwX2Okb7YBMsvFZXPNBB1clkekiTWEobTAVRD1EX14=; b=SFGyPWoEpvruhFTw0TTGq0hi37OTK3+ps0GnCKEYycuGY8FHxr4IznQF6UPxxDlUatYU EoP/c1k6kb2RCsnU3ljqH/b31haaLms3dNK9dvfNNjdyoZZkOfyo5KmP3nNjU6qgxS/+r DBUCHmYhF+Fbg/fEgm2VSp1zchQYaItJ0FyBiID5pDzK0nvqxtcI26/LIeUiEiiujE/5i xQlPRpRsgUO3F/wPnhab5caQKum70DV1hiCXgPMgc/M2Rb0nzakgNZ9QWEgKMoUybJfM7 +p6wJu+WzrdYMpN7iXSJVZdPKzDSzDp/0IcE0OSXJmJNIibLsMpt9Kz64npE0y7dCbQ== 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=EO4sWngc; arc=none smtp.remote-ip=209.85.222.51 Received: from mail-ua1-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) (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 5284A606A322 for ; Tue, 1 Dec 2026 19:03:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5284A606A322 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=EO4sWngc Received: by mail-ua1-f51.google.com with SMTP id a1e0cc1a2514c-9912c47367cso7840661241.1 for ; Tue, 01 Dec 2026 11:03:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796151815; x=1796756615; 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=ggwX2Okb7YBMsvFZXPNBB1clkekiTWEobTAVRD1EX14=; b=EO4sWngcRozrHO7z8TffgKKC3lI9fPCeoQuDYNqA9adGE7dAj4LyJXz5Dt8GueK/FT LEtH0fvjMRfkwVvqaFbnFMbzarDqNXlJW2CPDI6kK4W7JzmRzeZYE/lVTx+gmfO0MJab 5DyXDY0gbkTJMb4ih+CI+dFRs03Q/BXEBqSBzT4uR/UEpfRjvNWMuPON3KxWJQJ9+cRv Ifl8GwEtle+dyaah6x6ODK0/BLwKHW44VCZQLdrkCUTytEatZ1LhTYavW4X2DacDt2ba 2fkZ56/u3aRi4D4H38WSrWjODkbaXuCL48MWa4krP9P/DTTM3u+4XXMl7yZTaQ+9N9EN cCeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796151815; x=1796756615; 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=ggwX2Okb7YBMsvFZXPNBB1clkekiTWEobTAVRD1EX14=; b=WNyCSS+QxuvP9GawxsZVc3giTrMdWiOWk3SEuPtFNAKtQfu5Bj1r3TJCxDyLD7KhgN jbx6rtZpndtwHRBWj4xC5Jo9UUJAJ8ykSOSxvqaWv67ijepHkCtgQiEpDmReaY/9DsUK o0OaNv3IM+DNWicVsMHsipJPpDJ9WC0UY1TPkKK3l5IlNIBOtlhehDtMKAHSM2EU0BUN vLntyWEhB5oOZoTkL05QmLV+wmOvRmAVoks0t7EjWNikEa5vnTlwFqEoyWla1sH+yMxx k2H2DhtZoys30jv+sgTbsgRI8KjRZUH0c2aZLV85y8EXwUqfTo4PSMSMBBkVxlgiB0Z5 G6og== X-Gm-Message-State: APuU4Z/zR2rB//M2+RbFYKLsfR3a0QzLGK0Ho0iUXVQg1IB+3BK5gUtS UBoyWjraA2H0g2ClfDI8QcybjEoQjXmrhJr5UgX5XuFwcNIxgUM2+PvjdLjwJhohjjg= X-Gm-Gg: AZFdTZdhTx8Q10tBqV9EM2lHE2l0V2S4KqNtZU+Wpe9bpyN7Ayol1Eu2CqAEQ1oDTnH sZBsrxhl9vxLvxWGsiBT2iktzMGPK6lsRXlZFeN0dgzIIC/+kZI6pVNWT2fCfR9OS5wclrxquNR UgnuWSNnLTGDBzHu6UZvAu7fcbCvWwGN7ICVbdtSN40UxTUAakV6cw/8mvrfGGqt/6/P1OfCPBR fSTHuEfiMIYTAmg4yjMnu9IWZHVCuhj39muUOy0X2jPHnYPtr5pfIgKVz8WG2cD0SQEIXaUkGJc lT2PKzSJZhvSpRMlsE4MnISapUidBKo06moJQbbkc5UIQivvlKSO1zgtZOisWOcZOiKBOD4OkKv 0f2aRr3W7eBzFXOUJb5P3M0oyXKZZy5Wi0BmpGEBJnKJEgLjQrq/h25j3EA/yPKfMkfEmV8mZtJ svJc1An2cCHgY7+QEexpSNKCBrh06qRBpf78HYZTqkkBF8fFwkQ0YYybdqBDAunNKRrjvLgblia 6dVDI5kjwzrd59Y612lt7nZeDm5W6r5fEp/Juuf/BOf8uWt9ZJXLP4MXK4x2SbnSE1BqWBFFMAW jhhErJ06Gc6Iy0hkQIp4fIPXou9NV6x4kO0hjG8fEuzSHHEJHdJzTkDeTO9swlpIJ3MugSbkZor sOzNL0U+y35bTjkQpLS8SIq06XqV/yX1fStMvUVAnP9QLwWF0KtSOEHM= X-Received: by 2002:a05:6122:3a0b:b0:5ea:cf9d:a5fe with SMTP id 71dfb90a1353d-5ee827438acmr16969308e0c.14.1796151815017; Tue, 01 Dec 2026 11:03:35 -0800 (PST) Received: from lvondent-mobl5 ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-99e6dfc0b22sm38919229241.6.2026.12.01.11.03.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 11:03:34 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v12 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Date: Tue, 1 Dec 2026 14:03:22 -0500 Message-ID: <20261201190325.110874-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201190325.110874-1-luiz.dentz@gmail.com> References: <20261201190325.110874-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 | 91 +++++++++++++++++++++++++++++++++----- 1 file changed, 80 insertions(+), 11 deletions(-) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index be07b7b9cfb0..ad5a3cba708c 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); @@ -4072,6 +4101,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 { @@ -4079,6 +4125,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. @@ -4148,12 +4196,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 @@ -4166,8 +4223,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) else chan->ops->resume(chan); } else { - /* Nothing was sent by the channel, but clear its configuration - * so its queue is purged like for any other. + rsp_flex->dcid[rsp->count++] = 0; + rsp->refused++; + + /* The channel may have been accepted before being refused, + * clear its configuration so its queue is purged like for any + * other. */ chan->conf_state = 0; l2cap_chan_del(chan, ECONNRESET); @@ -4184,8 +4245,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; @@ -4204,17 +4265,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.1