From: Mohammed Alquraini <mohammed.saad.alquraini@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Marcel Holtmann <marcel@holtmann.org>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
linux-kernel@vger.kernel.org,
Mohammed Alquraini <mohammed.saad.alquraini@gmail.com>
Subject: [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept
Date: Thu, 19 Nov 2026 00:33:22 +0300 [thread overview]
Message-ID: <20261118213322.60067-1-mohammed.saad.alquraini@gmail.com> (raw)
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@gmail.com>
---
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
next reply other threads:[~2026-11-18 21:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 21:33 Mohammed Alquraini [this message]
2026-11-18 23:04 ` Luiz Augusto von Dentz
2026-11-19 10:01 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118213322.60067-1-mohammed.saad.alquraini@gmail.com \
--to=mohammed.saad.alquraini@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®