Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH v6 0/2] L2CAP: Fix channels being established with insufficient security
@ 2026-11-25 18:58 Luiz Augusto von Dentz
  2026-11-25 18:58 ` [PATCH v6 1/2] Bluetooth: L2CAP: Fix accepting channels " Luiz Augusto von Dentz
  2026-11-25 18:58 ` [PATCH v6 2/2] Bluetooth: L2CAP: Don't offer deferred channels before security is met Luiz Augusto von Dentz
  0 siblings, 2 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-25 18:58 UTC (permalink / raw)
  To: linux-bluetooth

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

l2cap_security_cfm() lowers the security level of a channel to the one
of the link when encryption completes, and accepts channels waiting for
the security to be met as long as the status is success, so a channel
can end up established, or be offered to a listener using
BT_DEFER_SETUP, over a link that doesn't meet the security level it
requires, e.g. if the peer disables encryption while the channel waits
for userspace to authorize it.

The first patch makes l2cap_security_cfm() compare the security level
of the link with the one required by the channel, which is now kept
apart from the one of the link in min_sec_level: encryption completing
below it is a downgrade, so the channel is no longer lowered to match
it but disconnected, or refused if it was still pending, and the link is
disconnected if the channel is a fixed one since it can't be
disconnected at L2CAP. Channels waiting to be accepted or started only
proceed if the link is encrypted and meets their security level, and
deferred LE and Enhanced Credit Based channels are refused when
userspace authorizes them if it no longer does.

The second patch makes a BR/EDR channel visible to the listener only
once it is offered to it, after the security is met, instead of as soon
as the connection request is responded as pending, so a listener that
accepts without waiting to be notified can't authorize a connection
while authentication is still pending.

Tests for both are sent to the BlueZ mailing list as "[PATCH BlueZ v4
0/5] Add tests for security of deferred setup".

A channel requiring a security level that pairing can't provide, e.g.
BT_SECURITY_HIGH when the IO capability is NoInputNoOutput, is now
refused instead of being connected with the security level the link
ended up with.

v6:
 - Evaluate the requirement of the channel in l2cap_security_cfm() after
   taking chan->lock, since it can be changed by setsockopt() meanwhile.
 - Treat encryption being disabled as a downgrade for fixed channels,
   since l2cap_check_encryption() doesn't handle them, and don't resume
   channels the link doesn't meet the security level of.
 - Also refuse outgoing channels in BT_CONNECT on a downgrade.
 - Close a deferred channel refused when userspace authorizes it with the
   channel kept locked, so it can't be authorized by another thread
   meanwhile.
v5:
 - Require the link to be encrypted for channels above
   BT_SECURITY_LOW, since the security level of the link is updated when
   authentication completes, before encryption is enabled.
 - Disconnect the link if a fixed channel is downgraded no matter its
   state, so one still waiting for security doesn't bypass it.
 - Keep the security level required by a channel in min_sec_level
   instead of comparing with sec_level, which follows the one of the
   link, so channels that only asked for BT_SECURITY_LOW, like SMP, are
   not considered downgraded when the link is paired again with a lower
   security level.
 - Refuse deferred LE Credit Based and Enhanced Credit Based channels
   when userspace authorizes them if the link doesn't meet their
   security level, instead of closing them from l2cap_security_cfm(),
   since rejecting an Enhanced Credit Based channel deletes the others
   of the same request.
 - Refuse deferred LE Credit Based channels in l2cap_security_cfm() when
   the link doesn't meet their security level.
v4:
 - Disconnect the link instead of the channel if a fixed channel is
   downgraded, since it can't be disconnected at L2CAP.
 - Call chan->ops->close() when a channel was deleted by the downgrade,
   so its socket is not leaked, and also refuse deferred LE Credit
   Based channels in BT_CONNECT2.
 - Only start channels in BT_CONNECT if the link meets their security
   level.
v3:
 - Don't use l2cap_chan_check_security(), which may start
   authentication or encryption, compare the security levels instead.
 - Treat encryption completing below the security level of a channel as
   a downgrade and disconnect it instead of lowering its security
   level.
 - Only let accept() return children in BT_CONNECT2, as
   bt_accept_poll() does.
v2:
 - Drop BT_SK_ACCEPTED and the changes to l2cap_sock_defer().
 - Add the second patch for listeners accepting while the security is
   still pending.

Luiz Augusto von Dentz (1):
  Bluetooth: L2CAP: Don't offer deferred channels before security is met

Mohammed Alquraini (1):
  Bluetooth: L2CAP: Fix accepting channels with insufficient security

 include/net/bluetooth/l2cap.h |   7 +++
 net/bluetooth/af_bluetooth.c  |   6 +-
 net/bluetooth/l2cap_core.c    | 109 +++++++++++++++++++++++++++++++---
 net/bluetooth/l2cap_sock.c    |  44 +++++++++++---
 net/bluetooth/rfcomm/core.c   |   1 +
 5 files changed, 151 insertions(+), 16 deletions(-)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH v7 1/4] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused
@ 2026-11-25 21:12 Luiz Augusto von Dentz
  2026-11-26  3:44 ` L2CAP: Fix channels being established with insufficient security bluez.test.bot
  0 siblings, 1 reply; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-25 21:12 UTC (permalink / raw)
  To: linux-bluetooth

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

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.

Fixes: 9aa9d9473f15 ("Bluetooth: L2CAP: Fix responding with wrong PDU type")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 net/bluetooth/l2cap_core.c | 67 +++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 9 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 943effc86740..a15ffcdd1330 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -4065,6 +4065,34 @@ 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;
+
+	if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &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 {
@@ -4072,6 +4100,7 @@ struct l2cap_ecred_rsp_data {
 		__le16 scid[L2CAP_ECRED_MAX_CID];
 	} __packed pdu;
 	int count;
+	bool partial;
 };
 
 /* Lock @chan if it is not @locked_chan, and has same or lower nesting level.
@@ -4141,14 +4170,23 @@ 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, the ones refused
+	 * have no destination CID if only some of them were.
+	 */
+	if (chan->state == BT_CONNECTED) {
 		rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
-	else
+	} else {
+		if (rsp->partial)
+			rsp_flex->dcid[rsp->count++] = 0;
+
 		l2cap_chan_del(chan, ECONNRESET);
+	}
 
 done:
 	if (locked)
@@ -4161,8 +4199,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 +4219,24 @@ 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);
+			data.partial = true;
+		} 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH v4 1/2] Bluetooth: L2CAP: Fix accepting channels with insufficient security
@ 2026-11-24 20:50 Luiz Augusto von Dentz
  2026-11-25  2:54 ` L2CAP: Fix channels being established " bluez.test.bot
  0 siblings, 1 reply; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-24 20:50 UTC (permalink / raw)
  To: linux-bluetooth

From: Mohammed Alquraini <mohammed.saad.alquraini@gmail.com>

When the link security changes, l2cap_security_cfm() doesn't check
that the result meets the security level required by the channels:

 - If encryption completes it sets chan->sec_level to hcon->sec_level,
   so a channel requiring a higher security level than the one the
   link ended up with has its requirement lowered instead of being
   refused.

 - A channel in BT_CONNECT2 is accepted, or offered to the listener if
   it uses BT_DEFER_SETUP, as long as the status is success and the
   encryption key size is valid, e.g. if the peer disables encryption
   while the channel waits for userspace to authorize it, the channel
   is offered again and, once authorized, it is established over an
   unencrypted link.

 - A channel in BT_CONNECT is started as long as the status is
   success, no matter if the link meets its security level.

Fix this by treating encryption completing with a security level
below the one required by a channel as a downgrade: the security level
of the channel is no longer lowered and the channel is disconnected,
or refused with L2CAP_CR_SEC_BLOCK if it was pending. There is no way
to disconnect a fixed channel at L2CAP, so for those the link is
disconnected instead. Channels in BT_CONNECT2 and BT_CONNECT are only
accepted, or started, if the link meets their security level.

This means a channel requiring a security level that pairing can't
provide, e.g. BT_SECURITY_HIGH when the IO capability is
NoInputNoOutput, is now refused instead of being connected with the
security level the link ended up with.

AI assistance was used in finding and preparing this patch.

Fixes: f66dc81f44d9 ("Bluetooth: Add support for deferring L2CAP connection setup")
Signed-off-by: Mohammed Alquraini <mohammed.saad.alquraini@gmail.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 net/bluetooth/l2cap_core.c | 58 ++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 6 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 943effc86740..eea74188db97 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -8095,11 +8095,19 @@ static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
 	}
 }
 
+/* Check if the link meets the security level required by the channel */
+static bool l2cap_chan_sec_level_met(struct hci_conn *hcon,
+				     struct l2cap_chan *chan)
+{
+	return chan->sec_level <= BT_SECURITY_LOW ||
+	       hcon->sec_level >= chan->sec_level;
+}
+
 static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 	__must_hold(&hcon->hdev->lock)
 {
 	struct l2cap_conn *conn = hcon->l2cap_data;
-	struct l2cap_chan *chan;
+	struct l2cap_chan *chan, *tmp;
 
 	if (!conn)
 		return;
@@ -8108,7 +8116,9 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 
 	mutex_lock(&conn->lock);
 
-	list_for_each_entry(chan, &conn->chan_l, list) {
+	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
+		bool downgrade = false;
+
 		l2cap_chan_lock(chan);
 
 		lockdep_assert_held(&chan->conn->lock);
@@ -8116,14 +8126,48 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 		BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
 		       state_to_string(chan->state));
 
-		if (!status && encrypt)
-			chan->sec_level = hcon->sec_level;
+		if (!status && encrypt) {
+			/* Encryption completing with a security level below
+			 * the one the channel requires is a downgrade, don't
+			 * lower the requirement of the channel to match it.
+			 */
+			if (!l2cap_chan_sec_level_met(hcon, chan))
+				downgrade = true;
+			else
+				chan->sec_level = hcon->sec_level;
+		}
 
 		if (!__l2cap_no_conn_pending(chan)) {
 			l2cap_chan_unlock(chan);
 			continue;
 		}
 
+		if (downgrade && (chan->state == BT_CONNECTED ||
+				  chan->state == BT_CONFIG ||
+				  (chan->state == BT_CONNECT2 &&
+				   chan->mode == L2CAP_MODE_LE_FLOWCTL))) {
+			/* There is no way to disconnect a fixed channel at
+			 * L2CAP, so drop the link, which takes down the rest
+			 * of the channels as well.
+			 */
+			if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
+				hci_disconnect(hcon, HCI_ERROR_AUTH_FAILURE);
+				l2cap_chan_unlock(chan);
+				break;
+			}
+
+			/* The channel may be removed from the list */
+			l2cap_chan_hold(chan);
+			__l2cap_chan_close(chan, ECONNREFUSED);
+
+			if (test_bit(FLAG_DEL, &chan->flags))
+				chan->ops->close(chan);
+
+			l2cap_chan_unlock(chan);
+			l2cap_chan_put(chan);
+			continue;
+		}
+
 		if (!status && (chan->state == BT_CONNECTED ||
 				chan->state == BT_CONFIG)) {
 			chan->ops->resume(chan);
@@ -8133,7 +8177,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 		}
 
 		if (chan->state == BT_CONNECT) {
-			if (!status && l2cap_check_enc_key_size(hcon, chan))
+			if (!status && l2cap_chan_sec_level_met(hcon, chan) &&
+			    l2cap_check_enc_key_size(hcon, chan))
 				l2cap_start_connection(chan);
 			else
 				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
@@ -8143,7 +8188,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 			struct l2cap_conn_rsp rsp;
 			__u16 res, stat;
 
-			if (!status && l2cap_check_enc_key_size(hcon, chan)) {
+			if (!status && l2cap_check_enc_key_size(hcon, chan) &&
+			    l2cap_chan_sec_level_met(hcon, chan)) {
 				if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
 					res = L2CAP_CR_PEND;
 					stat = L2CAP_CS_AUTHOR_PEND;
-- 
2.56.0


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

end of thread, other threads:[~2026-11-26  3:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 18:58 [PATCH v6 0/2] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-11-25 18:58 ` [PATCH v6 1/2] Bluetooth: L2CAP: Fix accepting channels " Luiz Augusto von Dentz
2026-11-25 21:26   ` L2CAP: Fix channels being established " bluez.test.bot
2026-11-25 18:58 ` [PATCH v6 2/2] Bluetooth: L2CAP: Don't offer deferred channels before security is met Luiz Augusto von Dentz
  -- strict thread matches above, loose matches on Subject: below --
2026-11-25 21:12 [PATCH v7 1/4] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Luiz Augusto von Dentz
2026-11-26  3:44 ` L2CAP: Fix channels being established with insufficient security bluez.test.bot
2026-11-24 20:50 [PATCH v4 1/2] Bluetooth: L2CAP: Fix accepting channels " Luiz Augusto von Dentz
2026-11-25  2:54 ` L2CAP: Fix channels being established " bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox