All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v20 0/2] Bluetooth: L2CAP: Fix LE credits resuming channels pending security
@ 2026-12-04 15:31 Luiz Augusto von Dentz
  2026-12-04 15:31 ` [PATCH v20 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Luiz Augusto von Dentz
  2026-12-04 15:31 ` [PATCH v20 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Luiz Augusto von Dentz
  0 siblings, 2 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-04 15:31 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

An LE Flow Control Credit packet resumes the channel, which on a CoC
waiting for a security upgrade requested with BT_SECURITY drains queued
data and clears the socket suspension before the upgrade completes.

While the upgrade is pending, an established CoC is now moved to
BT_CONFIG with FLAG_PENDING_SECURITY set, like ATT, so credits are only
accounted, not acted upon, and the existing l2cap_security_cfm() path
resumes or closes the channel once the upgrade completes.

This set has a companion userspace set for BlueZ adding l2cap-tester
cases covering it:

  [PATCH BlueZ v1 0/2] l2cap-tester: Add tests for changing LE security
  with SMP pending

Changes in v20:
- Read the channel state under its lock in l2cap_chan_connect(), so
  connect() returning EISCONN doesn't race with BT_SECURITY.

Changes in v19:
- Ignore credits for an Enhanced Credit Based channel whose response
  has not been sent, the peer doesn't know its CID yet.
- Only clear FLAG_DEFER_SETUP of a sibling under its lock when sending
  the Enhanced Credit Based response, so BT_SECURITY can't move it to
  BT_CONFIG after seeing the response pending.
- Only complete a pending upgrade from BT_SECURITY if the link meets the
  security level of the channel.
- Return EISCONN from connect() while an upgrade is pending.
- Check for a NULL socket in l2cap_sock_suspend_cb().

Changes in v18:
- Ignore credits on non connection oriented channels.
- Don't move an Enhanced Credit Based channel whose response has not
  been sent to BT_CONFIG.
- Let senders racing with BT_SECURITY queue data while the upgrade is
  pending instead of failing with ENOTCONN.
- Return the RX credits not returned while the upgrade was pending, and
  only resume the socket once the held data is sent and credits remain.
- Complete a pending upgrade when BT_SECURITY sets a level the link
  already meets.
- Don't claim key size and MIC are enforced in the commit message.

v19: https://lore.kernel.org/linux-bluetooth/20261204140210.1149344-1-luiz.dentz@gmail.com/

Kyle Pepper (2):
  Bluetooth: L2CAP: Restrict LE credits to connected CoCs
  Bluetooth: L2CAP: Keep credits from completing security upgrades

 include/net/bluetooth/l2cap.h | 13 +++++
 net/bluetooth/l2cap_core.c    | 89 +++++++++++++++++++++++++++++++----
 net/bluetooth/l2cap_sock.c    | 37 +++++++++++++--
 3 files changed, 128 insertions(+), 11 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v20 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs
  2026-12-04 15:31 [PATCH v20 0/2] Bluetooth: L2CAP: Fix LE credits resuming channels pending security Luiz Augusto von Dentz
@ 2026-12-04 15:31 ` Luiz Augusto von Dentz
  2026-12-04 15:31 ` [PATCH v20 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-04 15:31 UTC (permalink / raw)
  To: linux-bluetooth

From: Kyle Pepper <cochcoder@proton.me>

Do not let credit indications change fixed channels or complete pending
connection setup. Validate the channel state and mode before accounting
credits, checking overflow or invoking its resume callback.

Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Signed-off-by: Kyle Pepper <cochcoder@proton.me>
Assisted-by: OpenCode:claude-opus-5.5
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 net/bluetooth/l2cap_core.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index edbfb775a375..6d1f56ff8059 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -5572,6 +5572,19 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
 
 	l2cap_chan_lock(chan);
 
+	/* Credits are only accounted for established channels, including
+	 * the ones waiting for a security upgrade requested with BT_SECURITY
+	 * so the credits granted by the peer meanwhile are not lost, but not
+	 * for an Enhanced Credit Based one whose response has not been sent
+	 * since the peer doesn't know its CID yet.
+	 */
+	if ((chan->state != BT_CONNECTED && chan->state != BT_CONFIG) ||
+	    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))
+		goto unlock;
+
 	max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
 	if (credits > max_credits) {
 		BT_ERR("LE credits overflow");
@@ -5585,6 +5598,12 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
 
 	chan->tx_credits += credits;
 
+	/* Don't resume a channel with a pending security upgrade, it is
+	 * resumed by l2cap_security_cfm() once the upgrade completes.
+	 */
+	if (chan->state != BT_CONNECTED)
+		goto unlock;
+
 	/* Resume sending */
 	l2cap_le_flowctl_send(chan);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v20 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades
  2026-12-04 15:31 [PATCH v20 0/2] Bluetooth: L2CAP: Fix LE credits resuming channels pending security Luiz Augusto von Dentz
  2026-12-04 15:31 ` [PATCH v20 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Luiz Augusto von Dentz
@ 2026-12-04 15:31 ` Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-04 15:31 UTC (permalink / raw)
  To: linux-bluetooth

From: Kyle Pepper <cochcoder@proton.me>

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 <cochcoder@proton.me>
Assisted-by: OpenCode:claude-opus-5.5
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 include/net/bluetooth/l2cap.h | 13 +++++++
 net/bluetooth/l2cap_core.c    | 70 +++++++++++++++++++++++++++++++----
 net/bluetooth/l2cap_sock.c    | 37 ++++++++++++++++--
 3 files changed, 109 insertions(+), 11 deletions(-)

diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
index 28a2c0f4eff8..70b01aaaa975 100644
--- a/include/net/bluetooth/l2cap.h
+++ b/include/net/bluetooth/l2cap.h
@@ -907,6 +907,17 @@ 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 &&
+	       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 +1015,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..47633f5d1896 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;
 		}
@@ -4179,10 +4187,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 +4211,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 +7392,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 +7581,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 +7851,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 +7901,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 +8431,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..164ce7d1adf8 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1029,10 +1029,38 @@ 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->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 +1922,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 +2026,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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 15:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:31 [PATCH v20 0/2] Bluetooth: L2CAP: Fix LE credits resuming channels pending security Luiz Augusto von Dentz
2026-12-04 15:31 ` [PATCH v20 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Luiz Augusto von Dentz
2026-12-04 15:31 ` [PATCH v20 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Luiz Augusto von Dentz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.