From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.217.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796416165; b=nQrP74/uY5Wnik2ZmJyE5yrGDEqDw01FkCNuQJZvFierwtDiq4rykFha2H37l6eYwzck 2LR/ACmr+hmN8Fd0jgintHOtU5BFCzf//0cn3wSx0iLw71GHJy57ZcxWOwWyB3ms48slZ BtQGHn6C02UPU9s3c8YZvRELZc+S5nlQZReE3oUPxIHUdS3JQ5srrluwTNS0eGW+ju54V 9wUwPgZDUF/5513p1pV4FNTRx/+euuMhe6trw4XeQDUZY8typXZMT9DI80qoflHaAPDV/ mzx0K8F37wdp9wVMHPhuOVpBuUwTdtgItmS+vADWQQZnttyux6GDq3Q20cU8ggLeVSg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796416165; 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=BptWBjf98WkfQA3wnT+//YpnLMZX2mYRDJ18OOc5DN4=; b=eTtF9KQxzWk/vFcrYG9xdHup/x40LipMBURzRn+K2ivARitXuePEAwNr51x9SPTT7jt0 CDAQsd6h+dMWICVVJb6DbrWhGYRphC8/B6Vm5DVZLOLIZjZkt4PWeFwCIYy/E++uqeFoD PvwcKMErb6YnXcbZE+IrKAfaKxZ8/yLX/LfjtY9NT4dMIT9I9jvf5TrChaHX1vQJWg/P4 3JxlTKyQ1afvPyCVIq5g7JS7xNgtRdk4n0oM55ZKoQmeQQogHsgFLzOtbKs78hkBsKzET GEVddEQQTtapjAXi0XVjZJpgM3lK3ilC1uBn7tia2Z6gx4y1+0whRtY++LqXUdEHOww== 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=sKm5gjeF; arc=none smtp.remote-ip=209.85.217.48 Received: from mail-vs1-f48.google.com (mail-vs1-f48.google.com [209.85.217.48]) (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 E8E69606A32C for ; Fri, 4 Dec 2026 20:29:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E8E69606A32C 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=sKm5gjeF Received: by mail-vs1-f48.google.com with SMTP id ada2fe7eead31-7d2eba2b74aso774584137.2 for ; Fri, 04 Dec 2026 12:29:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796416164; x=1797020964; 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=BptWBjf98WkfQA3wnT+//YpnLMZX2mYRDJ18OOc5DN4=; b=sKm5gjeFdJWwr3PUMtsiYq2lXgNOTP/gVcJ5a/BmAAWIcmUI9ogYRtahl1FisQ5gXP pE7TF3ILSUPpQ9KwB6Ym8oc+2GnC3pOHLUFRRS3ju29EcZvid0gXW1jj45io/263jCsU sQi2EvW3wGqChucJDphz+reja1OwYddfUE49s+YUH/Y+1ho3G/VFCCyYGYj6Xc/nSiNt eG6MrPrK8nIJaS1z2ihJ+3ASFdDdkzonTcHfrlGDBxGfs8fcIuggi+IFZRuV/2tGnc+X Mp+3uVvuloSQrVnH2s3+rsuzWOItaF5dussDs44agqmse6fr4ZPHp5enVMZLSQMTQ4Pk NpuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796416164; x=1797020964; 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=BptWBjf98WkfQA3wnT+//YpnLMZX2mYRDJ18OOc5DN4=; b=PchKPcG/YkUmPxzGcHX5+8Y+DlCC5URL/HqBjy9cYmctLWhdadT4g6ei4Rp3uRfrFA 9hpDeAzO1GtbfARmjSyggktZJHR6KZnA9Ktc6cJoGaEgCEfx8RI3wA7JaoJg99itCoVd L3wPj0cEh4JVIe8kvXltXf41AjvvirOrCIEPJlAqmAVFMkdYq/Sz4igkHt5mHUGGe+Q/ Sd+xRHHWML5I+7OQS0O7HrsdhRGCiLIwONdR6R/NbwbuKRYsbu23p5PS+h50Vo3il7mT EYtbt83VSvMRwUi4YTijnC7kwPbK4OAkPbkr004rk8xmvh6KEMlAaHqIW+36lEBJ1wzJ zSug== X-Gm-Message-State: APuU4Z9Jywulljb5ykC/+X0h8zZJrWPtrN70vAIJm7tUE3L1EwtkOt+Y UVM3NrUC6LQy3hVqSAqRmm0gY0ZRP4VP6o4RsoD7lTvNCLnhoZlF37MRYS9AOanZ X-Gm-Gg: AZFdTZeufwHQSBHXiTw52oaF4qdtX/htnz8GNGp2t3N4HnUOaq6LCIvUR40P2pUCybN gN7V79uPA88mqpWCQg8rajjBbnN0KfVmjffeY6AaYznmIUzclRxG3bQ5ChT4lJiOeeEc+FX72bP 0yL74Xyfp0JN8urisJyJdY38a8aiH8/RP4DOKpHMuOlAl2rg6Vi1Kvm/PpM+/rkHBYlImixN89E 9E32idM4Wl75EEt5e0pbD6UbE7nBeeRrn2fB869v3MAaMFpNoYEhpZdcKn5mvztszGlZfYVYIn/ HWjQuMWbeWNFGxAuSKcPtDPez84yALsXMd8LrIWimeBsByOfZ3c2OhUVZSj5MWKGIZ6442DsDYc lsyD2zOlyt2ZFY9+bDMWdn8/CcKD9JZZW68JnWiqj6lU6sxcPRG+lfuL7FhjZ3ms8UL8FE3R4Yp HQ+uNk17F9BvjYtALq72qIT+milmhst3aF3QqLojIbqZUMYfPajccl4KXI8RYEvEVWasoTLJGVz DuCUU0inB5K2+t9lbQC7A/f0Ed59m5+pK6xHnSvi45SVvaRR9EZCEMa8yATOOTEh8jjUZz2Mz/p f1oP51j2KoXm5e852P1htGcSW5+JTPcvjUqUFDT9Vc+qc7G5tPvFELAjjPao9NknZ5U/qLHRRxF fg0fQ+wd8aKkUXdsdHBvJpg1Mmnp0j7xmWt5P4d8T7W8AF5WWsZyRqG8= X-Received: by 2002:a05:6102:54a8:b0:7d2:b199:6721 with SMTP id ada2fe7eead31-7daec7bf52dmr2574132137.19.1796416163944; Fri, 04 Dec 2026 12:29:23 -0800 (PST) Received: from lvondent-mobl5 ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-7dae97bd767sm1473104137.10.2026.12.04.12.29.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 12:29:23 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v21 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Date: Fri, 4 Dec 2026 15:29:12 -0500 Message-ID: <20261204202912.1336184-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204202912.1336184-1-luiz.dentz@gmail.com> References: <20261204202912.1336184-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: Kyle Pepper A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required security level is met. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Signed-off-by: Kyle Pepper Assisted-by: OpenCode:claude-opus-5.5 Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/l2cap.h | 14 +++++++ net/bluetooth/l2cap_core.c | 76 +++++++++++++++++++++++++++++++---- net/bluetooth/l2cap_sock.c | 38 ++++++++++++++++-- 3 files changed, 117 insertions(+), 11 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 28a2c0f4eff8..627b8c1f342f 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -907,6 +907,18 @@ static inline bool l2cap_clear_timer(struct l2cap_chan *chan, msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO)); #define __clear_ack_timer(c) l2cap_clear_timer(c, &c->ack_timer) +/* An established credit based channel waiting for a security upgrade + * requested with BT_SECURITY. + */ +static inline bool l2cap_chan_security_pending(struct l2cap_chan *chan) +{ + return chan->state == BT_CONFIG && + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && + test_bit(FLAG_PENDING_SECURITY, &chan->flags) && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); +} + static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2) { if (seq1 >= seq2) @@ -1004,6 +1016,8 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, const struct sockcm_cookie *sockc); void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); +bool l2cap_chan_security_resume(struct l2cap_chan *chan); +bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 6d1f56ff8059..8cad28597a8b 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -870,7 +870,7 @@ static void l2cap_chan_connect_reject(struct l2cap_chan *chan) * 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) +bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan) { return chan->mode == L2CAP_MODE_EXT_FLOWCTL && test_bit(FLAG_DEFER_SETUP, &chan->flags) && @@ -2751,6 +2751,10 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("chan %p", chan); + /* Hold the data while a security upgrade is pending */ + if (chan->state != BT_CONNECTED) + return; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2819,7 +2823,11 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); - if (chan->state != BT_CONNECTED) { + /* Data queued while a security upgrade is pending is held + * by l2cap_le_flowctl_send() until it completes. + */ + if (chan->state != BT_CONNECTED && + !l2cap_chan_security_pending(chan)) { __skb_queue_purge(&seg_queue); err = -ENOTCONN; } @@ -4096,6 +4104,12 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) return; + /* An established channel waiting for a security upgrade is still + * connected. + */ + if (l2cap_chan_security_pending(chan)) + return; + switch (chan->state) { case BT_CONNECT2: /* If channel still pending accept add to result */ @@ -4179,10 +4193,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) return; /* Check if channel for outgoing connection or if it wasn't deferred - * since in those cases it must be skipped. + * since in those cases it must be skipped. FLAG_DEFER_SETUP is only + * cleared under the channel lock, so BT_SECURITY can't move it to + * BT_CONFIG after seeing its response still pending. */ if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || - !test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) + !test_bit(FLAG_DEFER_SETUP, &chan->flags)) return; lockdep_assert_held(&rsp->locked_chan->lock); @@ -4201,6 +4217,9 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) lockdep_assert_held(&chan->lock); lockdep_assert_held(&chan->conn->lock); + if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) + goto done; + /* Reset ident so only one response is sent */ chan->ident = 0; @@ -7379,6 +7398,21 @@ void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail) l2cap_chan_le_send_credits(chan); } +/* Complete a pending security upgrade of a credit based channel once it is + * met: send the data held meanwhile and the credits not returned. Returns + * true if there are credits left so the caller should resume the channel. + */ +bool l2cap_chan_security_resume(struct l2cap_chan *chan) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + chan->state = BT_CONNECTED; + + l2cap_le_flowctl_send(chan); + l2cap_chan_le_send_credits(chan); + + return chan->tx_credits; +} + static int l2cap_ecred_recv(struct l2cap_chan *chan, struct sk_buff *skb) { int err; @@ -7553,7 +7587,10 @@ static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, if (chan->chan_type == L2CAP_CHAN_FIXED) l2cap_chan_ready(chan); - if (chan->state != BT_CONNECTED) + /* The peer may keep sending while a security upgrade requested on an + * established credit based channel is pending. + */ + if (chan->state != BT_CONNECTED && !l2cap_chan_security_pending(chan)) goto drop; switch (chan->mode) { @@ -7820,6 +7857,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, struct l2cap_conn *conn; struct hci_conn *hcon; struct hci_dev *hdev; + bool pending; + int state; int err; BT_DBG("%pMR -> %pMR (type %u) psm 0x%4.4x mode 0x%2.2x", &chan->src, @@ -7868,10 +7907,24 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, goto done; } - switch (chan->state) { + /* Serialize with BT_SECURITY moving an established channel to and + * from BT_CONFIG while a security upgrade is pending. + */ + l2cap_chan_lock(chan); + state = chan->state; + pending = l2cap_chan_security_pending(chan); + l2cap_chan_unlock(chan); + + switch (state) { case BT_CONNECT: case BT_CONNECT2: case BT_CONFIG: + /* Already connected, waiting for a security upgrade */ + if (pending) { + err = -EISCONN; + goto done; + } + /* Already connecting */ err = 0; goto done; @@ -8384,8 +8437,15 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) /* Don't resume a channel the link is not secure enough * for, l2cap_check_encryption() deals with it. */ - if (!unmet) - chan->ops->resume(chan); + if (!unmet) { + if (!l2cap_chan_security_pending(chan)) + chan->ops->resume(chan); + else if (l2cap_chan_security_resume(chan)) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); + } + l2cap_check_encryption(chan, encrypt); l2cap_chan_unlock(chan); continue; diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index 4e7787ec033c..aaa48fe689fb 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -1029,10 +1029,39 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || sk->sk_state == BT_CONNECTED) { l2cap_sock_set_sec_level(chan, sec.level); - if (!l2cap_chan_check_security(chan, true)) + if (!l2cap_chan_check_security(chan, true)) { set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); - else + + /* Like ATT, an established credit based channel + * is not ready until the upgrade completes, so + * credits and data don't resume it early. The + * socket stays connected so senders block on + * BT_SK_SUSPEND instead of failing. An Enhanced + * Credit Based channel whose response has not + * been sent is not established yet. + */ + if (chan->state == BT_CONNECTED && + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + !l2cap_chan_ecred_rsp_pending(chan)) { + set_bit(FLAG_PENDING_SECURITY, + &chan->flags); + chan->state = BT_CONFIG; + } + } else if (l2cap_chan_security_pending(chan) && + l2cap_chan_sec_level_ok(chan)) { + /* A level the link already meets was set while + * an upgrade was pending, l2cap_chan_check_security() + * may also succeed when it fails to start one. + */ + if (l2cap_chan_security_resume(chan)) + clear_bit(BT_SK_SUSPEND, + &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } else { sk->sk_state_change(sk); + } } else { err = -EINVAL; } @@ -1894,7 +1923,7 @@ static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan, /* Channel lock is released before requesting new skb and then * reacquired thus we need to recheck channel state. */ - if (chan->state != BT_CONNECTED) { + if (chan->state != BT_CONNECTED && !l2cap_chan_security_pending(chan)) { kfree_skb(skb); return ERR_PTR(-ENOTCONN); } @@ -1998,6 +2027,9 @@ static void l2cap_sock_suspend_cb(struct l2cap_chan *chan) { struct sock *sk = chan->data; + if (!sk) + return; + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); sk->sk_state_change(sk); } -- 2.56.1