[PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept

From: Mohammed Alquraini

Date: Wed Nov 18 2026 - 16:34:15 EST


A BR/EDR L2CAP service that listens with BT_DEFER_SETUP asks the kernel to
enforce its BT_SECURITY level and only decides which peer to admit. The
deferred-accept completion path does not honour that.

The child channel is placed on the parent's accept queue at CONN_REQ time and
is reported pollable (bt_accept_poll() returns EPOLLIN for a BT_CONNECT2 child
on a BT_DEFER_SETUP listener) before the link security procedure resolves.
When userspace recvmsg()s the deferred child in that window, l2cap_sock_defer()
moves the channel to BT_CONFIG and __l2cap_connect_rsp_defer() replies
L2CAP_CR_SUCCESS with no security check. If the security procedure then fails,
l2cap_security_cfm() has no branch that matches a failed result on a BT_CONFIG
channel, so the channel is never torn down: a peer that failed the service's
required security level keeps a connected, data-passing channel.

The sibling setsockopt(BT_SECURITY) path already rechecks with
l2cap_chan_check_security(), and the non-deferred incoming path denies with
L2CAP_CR_SEC_BLOCK; only the BR/EDR basic/ERTM deferred-accept path was
missed. This is the BR/EDR counterpart of commit 522e9ed157e3 ("Bluetooth:
l2cap: Check encryption key size on incoming connection").

Gate the deferred completion on the security state. In l2cap_sock_defer(),
before completing a BR/EDR deferred accept, call l2cap_chan_check_security();
if the level is not yet met, mark the socket BT_SK_ACCEPTED, leave the channel
in BT_CONNECT2 and send nothing. l2cap_security_cfm() then resolves it: on
failure the existing BT_CONNECT2 branch denies with L2CAP_CR_SEC_BLOCK; on
success, a channel the daemon already accepted is completed through the same
path a non-deferred accept takes (state -> BT_CONFIG, CONN_RSP SUCCESS +
CONF_REQ) rather than re-signalling an accept already made, so exactly one
CONN_RSP is sent per channel and the on-air sequence stays strictly monotonic
(AUTHEN_PEND, then AUTHOR_PEND for a not-yet-accepted channel, then SUCCESS).

Validated on an arm64 QEMU rig with a BR/EDR peer emulated over /dev/vhci:
with the patch, an early accept followed by a genuine Authentication_Complete
failure is answered with CONN_RSP result=3 (SEC_BLOCK) and no data is ever
delivered, identical to the non-deferred control; an early accept followed by
a successful (keyless) auth completes only after the security chain finishes,
with exactly one CONN_RSP SUCCESS and no trailing PEND; a deferred listener
that accepts only after the security resolves goes through the designed
CR_PEND -> recvmsg -> SUCCESS path with the link at the required level.

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@xxxxxxxxx>
---
include/net/bluetooth/bluetooth.h | 1 +
net/bluetooth/l2cap_core.c | 10 +++++++++-
net/bluetooth/l2cap_sock.c | 7 +++++++
3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -407,6 +407,7 @@
BT_SK_SUSPEND,
BT_SK_PKT_STATUS,
BT_SK_PKT_SEQNUM,
+ BT_SK_ACCEPTED,
};

struct bt_sock_list {
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -8011,7 +8011,15 @@
__u16 res, stat;

if (!status && l2cap_check_enc_key_size(hcon, chan)) {
- if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
+ bool deferred = test_bit(FLAG_DEFER_SETUP, &chan->flags);
+ struct sock *sk = chan->data;
+
+ /* Complete inline if the daemon accepted early. */
+ if (deferred && sk &&
+ test_and_clear_bit(BT_SK_ACCEPTED, &bt_sk(sk)->flags))
+ deferred = false;
+
+ if (deferred) {
res = L2CAP_CR_PEND;
stat = L2CAP_CS_AUTHOR_PEND;
chan->ops->defer(chan);
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1274,6 +1274,13 @@
release_sock(sk);
__l2cap_le_connect_rsp_defer(chan);
} else {
+ /* Defer completion until security resolves. */
+ if (!l2cap_chan_check_security(chan, false)) {
+ set_bit(BT_SK_ACCEPTED, &bt_sk(sk)->flags);
+ release_sock(sk);
+ l2cap_chan_unlock_conn(chan, have_conn);
+ return err;
+ }
sk->sk_state = BT_CONFIG;
chan->state = BT_CONFIG;
release_sock(sk);
--
2.43.0