* [PATCH v12 1/6] Bluetooth: hci_event: Disconnect if the encryption key size is not valid
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
@ 2026-12-01 19:03 ` Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted Luiz Augusto von Dentz
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-01 19:03 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
When the encryption key size can't be read, or is smaller than the
minimum, hci_conn_enc_key_size_set() clears the encryption flags,
downgrades the security level and notifies the upper layers with an
error, but unlike hci_change_encrypt() when the encryption fails it
doesn't disconnect the link.
The channels that were already established are not closed by the
upper layers for a status other than success, so they stay open over
a link that is not encrypted anymore.
Fix this by disconnecting the link with HCI_ERROR_AUTH_FAILURE, as
hci_change_encrypt() does, after notifying the upper layers.
Fixes: 04a342cc49a8 ("Bluetooth: Add more enc key size check")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
net/bluetooth/hci_event.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index a9b6a6ccaa03..7f86b71a3f80 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -782,6 +782,14 @@ static u8 hci_conn_enc_key_size_set(struct hci_dev *hdev, struct hci_conn *conn,
hci_encrypt_cfm(conn, status);
+ /* The link was reported as encrypted but it can't be used if the key
+ * size couldn't be read or is too small, so disconnect it as
+ * hci_change_encrypt() does when the encryption fails, now that the
+ * upper layers have been notified so they can cleanup.
+ */
+ if (status && conn->state == BT_CONNECTED)
+ hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
+
/* mgmt_security_level_changed() only reports the change for
* connections already announced to userspace (it checks
* HCI_CONN_MGMT_CONNECTED itself); otherwise it is a no-op here and
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v12 2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 1/6] Bluetooth: hci_event: Disconnect if the encryption key size is not valid Luiz Augusto von Dentz
@ 2026-12-01 19:03 ` Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Luiz Augusto von Dentz
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-01 19:03 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
When userspace authorizes a deferred LE Credit Based or Enhanced Credit
Based channel, l2cap_sock_defer() moves it to BT_CONNECTED directly
instead of with l2cap_chan_ready(), so the timer armed when the request
was received is not stopped and CONF_NOT_COMPLETE is not cleared.
The timer then fires on the established channel and disconnects it, and
since CONF_NOT_COMPLETE is still set l2cap_chan_del() returns before
purging its tx queue.
Fix this by completing LE Credit Based channels with l2cap_chan_ready()
once the response is sent, as done for the ones that are not deferred.
Enhanced Credit Based channels stay in BT_CONNECTED while the response
of their request is pending, so their timer is stopped when they are
authorized, as only the channels still pending shall time out, and the
rest of what l2cap_chan_ready() does is done once the response is sent.
Since the response may wait for other channels of the request, they are
also suspended until then so they can't send data before it, and their
configuration is cleared before deleting them if they end up refused,
so their queue is purged.
l2cap_sock_resume_cb() now locks the socket with the nesting level of
the channel, like l2cap_sock_teardown_cb(), since it is called by the
response with a sibling channel locked.
Fixes: 27e2d4c8d28b ("Bluetooth: Add basic LE L2CAP connect request receiving support")
Fixes: 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
include/net/bluetooth/l2cap.h | 3 ++-
net/bluetooth/l2cap_core.c | 27 +++++++++++++++++++++++++--
net/bluetooth/l2cap_sock.c | 16 +++++++++++++---
3 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
index 72d9d56c6f2a..e1820b7dca67 100644
--- a/include/net/bluetooth/l2cap.h
+++ b/include/net/bluetooth/l2cap.h
@@ -978,7 +978,8 @@ int l2cap_init_sockets(void);
void l2cap_cleanup_sockets(void);
bool l2cap_is_socket(struct socket *sock);
-void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
+void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
+ __must_hold(&chan->lock) __must_hold(&chan->conn->lock);
void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
__must_hold(&chan->lock) __must_hold(&chan->conn->lock);
void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 943effc86740..be07b7b9cfb0 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -4027,6 +4027,8 @@ static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
}
void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
+ __must_hold(&chan->lock)
+ __must_hold(&chan->conn->lock)
{
struct l2cap_le_conn_rsp rsp;
struct l2cap_conn *conn = chan->conn;
@@ -4042,6 +4044,11 @@ void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
&rsp);
chan->ident = 0;
+
+ /* The response establishes the channel, which also stops its timer
+ * and completes its configuration like for one that is not deferred.
+ */
+ l2cap_chan_ready(chan);
}
static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data)
@@ -4145,10 +4152,26 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM);
/* Include all channels pending with the same ident */
- if (!rsp->pdu.rsp.result)
+ if (!rsp->pdu.rsp.result) {
rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
- else
+
+ /* The response establishes the channel, which was already
+ * moved to BT_CONNECTED when userspace authorized it, so
+ * complete what l2cap_chan_ready() does, and let it send
+ * data, which was suspended until now, if it has credits.
+ */
+ chan->conf_state = 0;
+ if (!chan->tx_credits)
+ chan->ops->suspend(chan);
+ 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.
+ */
+ chan->conf_state = 0;
l2cap_chan_del(chan, ECONNRESET);
+ }
done:
if (locked)
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index d5663a4642a9..9ead47ca846e 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1287,11 +1287,17 @@ static int l2cap_sock_defer(struct sock *sk)
} else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) {
sk->sk_state = BT_CONNECTED;
chan->state = BT_CONNECTED;
+ /* Data can't be sent until the response of the request
+ * is sent, which may wait for other channels of it.
+ */
+ set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
release_sock(sk);
+ /* The channel is accepted, only the ones still pending
+ * shall time out while waiting for the response.
+ */
+ __clear_chan_timer(chan);
__l2cap_ecred_conn_rsp_defer(chan);
} else if (bdaddr_type_is_le(chan->src_type)) {
- sk->sk_state = BT_CONNECTED;
- chan->state = BT_CONNECTED;
release_sock(sk);
__l2cap_le_connect_rsp_defer(chan);
} else {
@@ -1894,7 +1900,11 @@ static void l2cap_sock_resume_cb(struct l2cap_chan *chan)
if (!sk)
return;
- lock_sock(sk);
+ /* Like l2cap_sock_teardown_cb(), lock the socket with the nesting level
+ * of the channel since this may be called with a sibling channel
+ * locked, e.g. by the response of an Enhanced Credit Based request.
+ */
+ lock_sock_nested(sk, atomic_read(&chan->nesting));
if (test_and_clear_bit(FLAG_PENDING_SECURITY, &chan->flags)) {
sk->sk_state = BT_CONNECTED;
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v12 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 1/6] Bluetooth: hci_event: Disconnect if the encryption key size is not valid Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted Luiz Augusto von Dentz
@ 2026-12-01 19:03 ` Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 4/6] Bluetooth: L2CAP: Fix leaking sockets released before their channel is deleted Luiz Augusto von Dentz
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-01 19:03 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. 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 <luiz.von.dentz@intel.com>
---
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
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v12 4/6] Bluetooth: L2CAP: Fix leaking sockets released before their channel is deleted
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
` (2 preceding siblings ...)
2026-12-01 19:03 ` [PATCH v12 3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused Luiz Augusto von Dentz
@ 2026-12-01 19:03 ` Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 5/6] Bluetooth: L2CAP: Fix accepting channels with insufficient security Luiz Augusto von Dentz
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-01 19:03 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
When a socket without an owner is closed while its channel is still
around, l2cap_sock_kill() doesn't kill it since it is not flagged
SOCK_ZAPPED yet, which only chan->ops->teardown() does. This happens
with deferred Enhanced Credit Based channels refused while other
channels of the same request are still waiting to be authorized, either
by userspace releasing the socket or, for the ones never accepted, by
the listener being closed.
The channels that are deleted by something else afterwards, like
l2cap_ecred_rsp_defer() does once it sends the response that includes
them, don't call chan->ops->close() either, so nobody kills the
orphaned socket anymore and it is leaked along with the references it
holds.
Fix this by calling l2cap_sock_kill() from l2cap_sock_teardown_cb()
once the socket is flagged SOCK_ZAPPED, which only kills it if it has
no owner. The ones closing the channel afterwards do nothing since the
socket is no longer linked to it. Like the rest of
l2cap_sock_teardown_cb(), the socket is locked with the nesting level
of the channel, since it may be called with a sibling channel locked,
e.g. by the response of an Enhanced Credit Based request.
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_sock.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 9ead47ca846e..e46487bfabf8 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1393,7 +1393,7 @@ static void l2cap_sock_put_chan(struct sock *sk)
/* Kill socket (only if zapped and orphan)
* Must be called on unlocked socket, with l2cap channel lock.
*/
-static void l2cap_sock_kill(struct sock *sk)
+static void __l2cap_sock_kill(struct sock *sk, int subclass)
{
if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
return;
@@ -1403,7 +1403,7 @@ static void l2cap_sock_kill(struct sock *sk)
/* Take lock to synchronize against access without owning sk->sk_socket,
* eg. in l2cap_sock_cleanup_listen(). proto_ops etc. don't need lock.
*/
- lock_sock(sk);
+ lock_sock_nested(sk, subclass);
l2cap_sock_put_chan(sk);
release_sock(sk);
@@ -1412,6 +1412,11 @@ static void l2cap_sock_kill(struct sock *sk)
sock_put(sk);
}
+static void l2cap_sock_kill(struct sock *sk)
+{
+ __l2cap_sock_kill(sk, 0);
+}
+
static int __l2cap_wait_ack(struct sock *sk, struct l2cap_chan *chan)
{
DECLARE_WAITQUEUE(wait, current);
@@ -1806,6 +1811,20 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
/* Only zap after cleanup to avoid use after free race */
sock_set_flag(sk, SOCK_ZAPPED);
+ /* A socket without an owner, either because it was released while the
+ * channel was still around or because it was never accepted, can't be
+ * killed before it is zapped, and nobody else will once the channel is
+ * deleted by something else, e.g. the response of a request that
+ * included it, so do it now. l2cap_sock_kill() does nothing if the
+ * socket still has an owner, and the ones that close the channel
+ * afterwards do nothing either since the socket is no longer linked
+ * to it.
+ *
+ * Like above, the socket is locked with the nesting level of the
+ * channel since this may be called with a sibling channel locked,
+ * e.g. by the response of an Enhanced Credit Based request.
+ */
+ __l2cap_sock_kill(sk, atomic_read(&chan->nesting));
}
static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v12 5/6] Bluetooth: L2CAP: Fix accepting channels with insufficient security
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
` (3 preceding siblings ...)
2026-12-01 19:03 ` [PATCH v12 4/6] Bluetooth: L2CAP: Fix leaking sockets released before their channel is deleted Luiz Augusto von Dentz
@ 2026-12-01 19:03 ` Luiz Augusto von Dentz
2026-12-01 19:03 ` [PATCH v12 6/6] Bluetooth: L2CAP: Don't offer deferred channels before security is met Luiz Augusto von Dentz
2026-12-01 20:50 ` [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security patchwork-bot+bluetooth
6 siblings, 0 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-01 19:03 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.
- The security level of the link is updated when authentication
completes, before encryption is enabled, so it can't be used alone
to tell if the link is secure enough.
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, which also applies when encryption is disabled
since l2cap_check_encryption() doesn't handle them. Channels in
BT_CONNECT2 and BT_CONNECT are only accepted, or started, if the link
is encrypted and meets their security level, and established channels
are not resumed if it doesn't. Authentication completing before
encryption is enabled is not considered encryption being disabled, the
channels just wait for it.
Since chan->sec_level follows the security level of the link once
encryption is enabled it can't tell what a channel requires, so keep
that in the new min_sec_level, which is set wherever the security level
is requested for the channel, otherwise channels that never asked for
more than BT_SECURITY_LOW, like SMP, would be considered downgraded
when the link is paired again with a lower security level.
Deferred channels wait in BT_CONNECT2 for userspace to authorize them,
which doesn't check the security level, so refuse them there if the
link no longer meets it, e.g. if the security level was raised with
BT_SECURITY and the authentication has not completed yet, with the
channel kept locked until it is closed so it can't be authorized
meanwhile. Only that channel is refused, the others of the same
Enhanced Credit Based request are responded individually.
The Enhanced Credit Based channels accepted while the response of their
request is pending are not established yet, so they are left alone by
l2cap_security_cfm(), which can't send the response while walking the
channels, and the response refuses them if the link doesn't meet their
security level by then. Outgoing deferred Enhanced Credit Based channels
the link doesn't meet the security level of are not included in the
request of the others, l2cap_security_cfm() refuses them instead.
A security upgrade requested for an established channel that fails
leaves the channel below its security level, so it is disconnected as
well instead of being left suspended, by disconnecting the link for the
fixed channels.
The security level requested with BT_SECURITY is only set once it is
accepted, so a request that fails is never seen by the other channels.
A deferred channel whose socket was suspended while a security level
requested with BT_SECURITY was pending is resumed once it is met, so
it doesn't stay suspended after being authorized.
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")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Mohammed Alquraini <mohammed.saad.alquraini@gmail.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
include/net/bluetooth/l2cap.h | 7 ++
net/bluetooth/l2cap_core.c | 170 ++++++++++++++++++++++++++++++++--
net/bluetooth/l2cap_sock.c | 40 ++++++--
net/bluetooth/rfcomm/core.c | 1 +
4 files changed, 202 insertions(+), 16 deletions(-)
diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
index e1820b7dca67..28a2c0f4eff8 100644
--- a/include/net/bluetooth/l2cap.h
+++ b/include/net/bluetooth/l2cap.h
@@ -544,7 +544,12 @@ struct l2cap_chan {
__u8 chan_type;
__u8 chan_policy;
+ /* The security level of the link, which sec_level follows once
+ * encryption is enabled, so it can't tell what the channel requires,
+ * that is what min_sec_level is for.
+ */
__u8 sec_level;
+ __u8 min_sec_level;
__u8 mic_len;
__u8 ident;
@@ -988,6 +993,8 @@ int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid);
struct l2cap_chan *l2cap_chan_create(void);
+bool l2cap_chan_sec_level_ok(struct l2cap_chan *chan);
+void l2cap_chan_close(struct l2cap_chan *chan, int reason);
void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason)
__must_not_hold(&chan->lock);
int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index ad5a3cba708c..6dbc0bdcbe3f 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -551,6 +551,7 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan)
chan->remote_tx_win = chan->tx_win;
chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
chan->sec_level = BT_SECURITY_LOW;
+ chan->min_sec_level = BT_SECURITY_LOW;
chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
@@ -567,6 +568,7 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan)
chan->tx_win = pchan->tx_win;
chan->tx_win_max = pchan->tx_win_max;
chan->sec_level = pchan->sec_level;
+ chan->min_sec_level = pchan->min_sec_level;
chan->mic_len = pchan->mic_len;
chan->conf_state = pchan->conf_state;
chan->flags = pchan->flags;
@@ -1483,6 +1485,13 @@ static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data)
chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT)
return;
+ /* Leave out the channels the link doesn't meet the security level of,
+ * they are refused by l2cap_security_cfm() instead of being included
+ * in a request they would be established by.
+ */
+ if (!l2cap_chan_sec_level_ok(chan))
+ return;
+
if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags))
return;
@@ -4208,7 +4217,7 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
* result: the refused ones are responded with no destination CID,
* which are dropped if all of them were refused.
*/
- if (chan->state == BT_CONNECTED) {
+ if (chan->state == BT_CONNECTED && l2cap_chan_sec_level_ok(chan)) {
rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid);
rsp->accepted++;
@@ -5265,6 +5274,8 @@ static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
sec_level = hcon->sec_level + 1;
if (chan->sec_level < sec_level)
chan->sec_level = sec_level;
+ if (chan->min_sec_level < sec_level)
+ chan->min_sec_level = sec_level;
/* We'll need to send a new Connect Request */
clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags);
@@ -5881,6 +5892,8 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,
sec_level = hcon->sec_level + 1;
if (chan->sec_level < sec_level)
chan->sec_level = sec_level;
+ if (chan->min_sec_level < sec_level)
+ chan->min_sec_level = sec_level;
/* We'll need to send a new Connect Request */
clear_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags);
@@ -8187,11 +8200,37 @@ static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
}
}
+/* Check if the link meets the security level required by the channel, which
+ * for anything above BT_SECURITY_LOW needs the link to be encrypted since the
+ * security level of the link is updated when authentication completes, before
+ * encryption is enabled.
+ */
+static bool l2cap_chan_sec_level_met(struct hci_conn *hcon,
+ struct l2cap_chan *chan)
+{
+ if (chan->min_sec_level <= BT_SECURITY_LOW)
+ return true;
+
+ return test_bit(HCI_CONN_ENCRYPT, hcon->flags) &&
+ hcon->sec_level >= chan->min_sec_level;
+}
+
+void l2cap_chan_close(struct l2cap_chan *chan, int reason)
+{
+ /* Context analysis: caller holds chan->lock and chan->conn->lock */
+ context_unsafe(__l2cap_chan_close(chan, reason));
+}
+
+bool l2cap_chan_sec_level_ok(struct l2cap_chan *chan)
+{
+ return l2cap_chan_sec_level_met(chan->conn->hcon, chan);
+}
+
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;
@@ -8200,7 +8239,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 unmet, encrypting, failed, downgrade = false;
+
l2cap_chan_lock(chan);
lockdep_assert_held(&chan->conn->lock);
@@ -8208,24 +8249,136 @@ 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;
+ /* Authentication completing before encryption is enabled
+ * updates the security level of the link, so it already meets
+ * the one required, but it is not encrypted yet: encryption is
+ * expected to follow instead of it having been disabled.
+ */
+ encrypting = !status && !encrypt &&
+ chan->min_sec_level > BT_SECURITY_LOW &&
+ !test_bit(HCI_CONN_ENCRYPT, hcon->flags) &&
+ hcon->sec_level >= chan->min_sec_level;
+
+ /* The requirement of the channel may change under chan->lock */
+ unmet = !status && !encrypting &&
+ !l2cap_chan_sec_level_met(hcon, chan);
+
+ /* A security upgrade requested for an established channel, e.g.
+ * with BT_SECURITY, that fails leaves it below its security
+ * level, the link security didn't change otherwise.
+ */
+ failed = status && !l2cap_chan_sec_level_met(hcon, chan);
+
+ 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 (unmet)
+ downgrade = true;
+ else
+ chan->sec_level = hcon->sec_level;
+ } else if ((unmet || failed) &&
+ chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
+ /* Unlike the other channels, l2cap_check_encryption()
+ * doesn't handle fixed ones, so encryption being
+ * disabled is a downgrade for them as well, and so is
+ * a security upgrade they requested that failed since
+ * they can't be disconnected either.
+ */
+ downgrade = true;
+ }
+
+ /* The response to an Enhanced Credit Based request is only sent
+ * once all of its channels are authorized or refused, so the
+ * ones accepted while waiting for it are not established yet
+ * and can't be disconnected, which would send the response
+ * while walking the channels: the response refuses them if the
+ * link doesn't meet their security level by then.
+ */
+ if (chan->state == BT_CONNECTED &&
+ l2cap_chan_ecred_rsp_pending(chan)) {
+ l2cap_chan_unlock(chan);
+ continue;
+ }
+
+ /* A channel that is not established yet is refused as soon as
+ * the link doesn't meet its security level, while an
+ * established one is only closed on a downgrade since
+ * l2cap_check_encryption() handles encryption being disabled.
+ *
+ * Enhanced Credit Based channels waiting in BT_CONNECT2 are
+ * refused when userspace authorizes them instead, since the
+ * response of the request is only sent once all of its
+ * channels have been authorized or refused.
+ */
+ if ((downgrade && (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED ||
+ chan->state == BT_CONNECTED ||
+ chan->state == BT_CONFIG)) ||
+ (failed && chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
+ (chan->state == BT_CONNECTED ||
+ chan->state == BT_CONFIG)) ||
+ (unmet && (chan->state == BT_CONNECT ||
+ (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;
+ }
+
+ /* A channel waiting for the encryption to be enabled has to
+ * wait for it before being accepted, started or resumed.
+ */
+ if (encrypting) {
+ l2cap_chan_unlock(chan);
+ continue;
+ }
if (!__l2cap_no_conn_pending(chan)) {
l2cap_chan_unlock(chan);
continue;
}
+ /* A deferred channel waiting for userspace to authorize it may
+ * have been suspended while a security level it requested with
+ * BT_SECURITY was pending, so resume it now that it is met.
+ */
+ if (!status && !unmet && chan->state == BT_CONNECT2 &&
+ test_bit(FLAG_DEFER_SETUP, &chan->flags))
+ chan->ops->resume(chan);
+
if (!status && (chan->state == BT_CONNECTED ||
chan->state == BT_CONFIG)) {
- chan->ops->resume(chan);
+ /* Don't resume a channel the link is not secure enough
+ * for, l2cap_check_encryption() deals with it.
+ */
+ if (!unmet)
+ chan->ops->resume(chan);
l2cap_check_encryption(chan, encrypt);
l2cap_chan_unlock(chan);
continue;
}
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);
@@ -8235,7 +8388,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;
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index e46487bfabf8..4e45da2b5975 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -47,6 +47,15 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
struct l2cap_chan *chan);
static void l2cap_sock_cleanup_listen(struct sock *parent);
+/* Set the security level required by the channel, as opposed to the one of the
+ * link that chan->sec_level follows.
+ */
+static void l2cap_sock_set_sec_level(struct l2cap_chan *chan, __u8 level)
+{
+ chan->sec_level = level;
+ chan->min_sec_level = level;
+}
+
bool l2cap_is_socket(struct socket *sock)
{
return sock && sock->ops == &l2cap_sock_ops;
@@ -143,15 +152,15 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr_unsized *addr, i
switch (chan->chan_type) {
case L2CAP_CHAN_CONN_LESS:
if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_3DSP)
- chan->sec_level = BT_SECURITY_SDP;
+ l2cap_sock_set_sec_level(chan, BT_SECURITY_SDP);
break;
case L2CAP_CHAN_CONN_ORIENTED:
if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
__le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
- chan->sec_level = BT_SECURITY_SDP;
+ l2cap_sock_set_sec_level(chan, BT_SECURITY_SDP);
break;
case L2CAP_CHAN_RAW:
- chan->sec_level = BT_SECURITY_SDP;
+ l2cap_sock_set_sec_level(chan, BT_SECURITY_SDP);
break;
case L2CAP_CHAN_FIXED:
/* Fixed channels default to the L2CAP core not holding a
@@ -867,11 +876,11 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
}
if (opt & L2CAP_LM_AUTH)
- chan->sec_level = BT_SECURITY_LOW;
+ l2cap_sock_set_sec_level(chan, BT_SECURITY_LOW);
if (opt & L2CAP_LM_ENCRYPT)
- chan->sec_level = BT_SECURITY_MEDIUM;
+ l2cap_sock_set_sec_level(chan, BT_SECURITY_MEDIUM);
if (opt & L2CAP_LM_SECURE)
- chan->sec_level = BT_SECURITY_HIGH;
+ l2cap_sock_set_sec_level(chan, BT_SECURITY_HIGH);
assign_bit(FLAG_ROLE_SWITCH, &chan->flags,
opt & L2CAP_LM_MASTER);
@@ -991,12 +1000,16 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
break;
}
- chan->sec_level = sec.level;
chan->mic_len = sec.mic_len;
+ /* The security level is only set once it is accepted, so the
+ * other channels never see a requirement that failed.
+ */
conn = l2cap_chan_conn(chan);
- if (!conn)
+ if (!conn) {
+ l2cap_sock_set_sec_level(chan, sec.level);
break;
+ }
/* change security for LE channels */
if (chan->scid == L2CAP_CID_ATT) {
@@ -1006,6 +1019,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
break;
}
+ l2cap_sock_set_sec_level(chan, sec.level);
set_bit(FLAG_PENDING_SECURITY, &chan->flags);
sk->sk_state = BT_CONFIG;
chan->state = BT_CONFIG;
@@ -1014,6 +1028,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
} else if ((sk->sk_state == BT_CONNECT2 &&
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))
set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
else
@@ -1284,6 +1299,15 @@ static int l2cap_sock_defer(struct sock *sk)
if (!have_conn) {
release_sock(sk);
err = -ENOTCONN;
+ } else if (!l2cap_chan_sec_level_ok(chan)) {
+ /* The link doesn't meet the security level required
+ * anymore, refuse the connection like closing it would,
+ * without releasing the channel so it can't be
+ * authorized meanwhile.
+ */
+ release_sock(sk);
+ l2cap_chan_close(chan, ECONNREFUSED);
+ err = -ECONNREFUSED;
} else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) {
sk->sk_state = BT_CONNECTED;
chan->state = BT_CONNECTED;
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index e6af6f5f5914..66c88390ca88 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -852,6 +852,7 @@ static struct socket *rfcomm_session_connect(bdaddr_t *src, bdaddr_t *dst,
/* Set MTU to 0 so L2CAP can auto select the MTU */
l2cap_pi(sk)->chan->imtu = 0;
l2cap_pi(sk)->chan->sec_level = sec_level;
+ l2cap_pi(sk)->chan->min_sec_level = sec_level;
if (l2cap_ertm)
l2cap_pi(sk)->chan->mode = L2CAP_MODE_ERTM;
release_sock(sk);
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v12 6/6] Bluetooth: L2CAP: Don't offer deferred channels before security is met
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
` (4 preceding siblings ...)
2026-12-01 19:03 ` [PATCH v12 5/6] Bluetooth: L2CAP: Fix accepting channels with insufficient security Luiz Augusto von Dentz
@ 2026-12-01 19:03 ` Luiz Augusto von Dentz
2026-12-01 20:50 ` [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security patchwork-bot+bluetooth
6 siblings, 0 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-01 19:03 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 4e45da2b5975..4e7787ec033c 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1861,7 +1861,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;
@@ -1929,6 +1935,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.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security
2026-12-01 19:03 [PATCH v12 0/6] L2CAP: Fix channels being established with insufficient security Luiz Augusto von Dentz
` (5 preceding siblings ...)
2026-12-01 19:03 ` [PATCH v12 6/6] Bluetooth: L2CAP: Don't offer deferred channels before security is met Luiz Augusto von Dentz
@ 2026-12-01 20:50 ` patchwork-bot+bluetooth
6 siblings, 0 replies; 8+ messages in thread
From: patchwork-bot+bluetooth @ 2026-12-01 20:50 UTC (permalink / raw)
To: Luiz Augusto von Dentz; +Cc: linux-bluetooth
Hello:
This series was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:
On Tue, 1 Dec 2026 14:03:19 -0500 you wrote:
> 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.
>
> [...]
Here is the summary with links:
- [v12,1/6] Bluetooth: hci_event: Disconnect if the encryption key size is not valid
https://git.kernel.org/bluetooth/bluetooth-next/c/8c8b8d6d180f
- [v12,2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted
https://git.kernel.org/bluetooth/bluetooth-next/c/943c69260f00
- [v12,3/6] Bluetooth: L2CAP: Fix rejecting all Enhanced Credit channels if one is refused
https://git.kernel.org/bluetooth/bluetooth-next/c/126a10afece0
- [v12,4/6] Bluetooth: L2CAP: Fix leaking sockets released before their channel is deleted
https://git.kernel.org/bluetooth/bluetooth-next/c/57c26424ea54
- [v12,5/6] Bluetooth: L2CAP: Fix accepting channels with insufficient security
https://git.kernel.org/bluetooth/bluetooth-next/c/7a1a7a5baa1e
- [v12,6/6] Bluetooth: L2CAP: Don't offer deferred channels before security is met
https://git.kernel.org/bluetooth/bluetooth-next/c/ef4d2b0ef0f2
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 8+ messages in thread