* [PATCH v4 1/2] Bluetooth: L2CAP: Fix accepting channels with insufficient security
2026-11-24 20:50 [PATCH v4 0/2] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
@ 2026-11-24 20:50 ` Luiz Augusto von Dentz
2026-11-24 20:50 ` [PATCH v4 2/2] Bluetooth: L2CAP: Don't offer deferred channels before security is met Luiz Augusto von Dentz
1 sibling, 0 replies; 3+ 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] 3+ messages in thread* [PATCH v4 2/2] Bluetooth: L2CAP: Don't offer deferred channels before security is met
2026-11-24 20:50 [PATCH v4 0/2] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-11-24 20:50 ` [PATCH v4 1/2] Bluetooth: L2CAP: Fix accepting channels " Luiz Augusto von Dentz
@ 2026-11-24 20:50 ` Luiz Augusto von Dentz
1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-24 20:50 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
A BR/EDR channel is moved to BT_CONNECT2 as soon as the connection
request is answered as pending, no matter if it is waiting for the
information exchange, for the link security or for userspace to
authorize it, and the socket state follows the channel state.
bt_accept_poll() assumes that a child in BT_CONNECT2 of a listener
using BT_DEFER_SETUP is waiting to be authorized, and
bt_accept_dequeue() returns any child of such a listener, so although
the listener is only notified once the security is met, a listener
that doesn't wait for it can accept() the child and then authorize it
with recvmsg() while authentication is still pending.
l2cap_sock_defer() then moves the channel to BT_CONFIG and responds
with L2CAP_CR_SUCCESS, and if authentication fails afterwards the
channel is left open since l2cap_security_cfm() doesn't close channels
in BT_CONFIG.
Fix this by only moving the socket to BT_CONNECT2 when the channel is
offered to the listener with l2cap_sock_defer_cb(), which is only
called once the security is met, and by only letting accept() return a
child of a listener using BT_DEFER_SETUP once it is in BT_CONNECT2, as
bt_accept_poll() does. The child stays in the accept queue so the
backlog limit and the cleanup done when the listener is closed are not
affected.
Fixes: f66dc81f44d9 ("Bluetooth: Add support for deferring L2CAP connection setup")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
net/bluetooth/af_bluetooth.c | 6 +++++-
net/bluetooth/l2cap_sock.c | 10 +++++++++-
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 7758e9ea3848..103d61d13fbf 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -330,8 +330,12 @@ struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
continue;
}
+ /* With deferred setup only a child in BT_CONNECT2 has been
+ * offered to the listener, see bt_accept_poll().
+ */
if (sk->sk_state == BT_CONNECTED || !newsock ||
- test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) {
+ (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) &&
+ sk->sk_state == BT_CONNECT2)) {
bt_accept_unlink(sk);
if (newsock)
sock_graft(sk, newsock);
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index d5663a4642a9..57ada00f76eb 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1812,7 +1812,13 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,
lock_sock(sk);
- sk->sk_state = state;
+ /* BT_CONNECT2 also covers a channel still waiting for the information
+ * exchange or link security. Only l2cap_sock_defer_cb() moves the
+ * socket to BT_CONNECT2, once the channel can be offered to the
+ * listener.
+ */
+ if (state != BT_CONNECT2)
+ sk->sk_state = state;
if (err)
sk->sk_err = err;
@@ -1880,6 +1886,8 @@ static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
lock_sock(sk);
+ sk->sk_state = BT_CONNECT2;
+
parent = bt_sk(sk)->parent;
if (parent)
parent->sk_data_ready(parent);
--
2.56.0
^ permalink raw reply related [flat|nested] 3+ messages in thread