* [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept
@ 2026-11-18 21:33 Mohammed Alquraini
2026-11-18 23:04 ` Luiz Augusto von Dentz
2026-11-19 10:01 ` kernel test robot
0 siblings, 2 replies; 3+ messages in thread
From: Mohammed Alquraini @ 2026-11-18 21:33 UTC (permalink / raw)
To: linux-bluetooth
Cc: Marcel Holtmann, Luiz Augusto von Dentz, linux-kernel,
Mohammed Alquraini
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept
2026-11-18 21:33 [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept Mohammed Alquraini
@ 2026-11-18 23:04 ` Luiz Augusto von Dentz
2026-11-19 10:01 ` kernel test robot
1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-18 23:04 UTC (permalink / raw)
To: Mohammed Alquraini; +Cc: linux-bluetooth, Marcel Holtmann, linux-kernel
Hi Mohammed,
On Wed, Nov 18, 2026 at 4:34 PM Mohammed Alquraini
<mohammed.saad.alquraini@gmail.com> wrote:
>
> 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").
That is encryption key size, not security level, and why is it
mentioned here and doesn't have a Fixes tag if that was missed?
> 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;
I read this thru a couple of times and I still don't get how can you
get !status when security hasn't completed? Are you perhaps forcing
invalid behavior from the controller or something similar?
> + 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;
> + }
Why wouldn't we do it earlier? This is too late, the userspace has
been awaken by the likes of l2cap_sock_defer_cb when the client socket
should really have been rejected not accepted.
> sk->sk_state = BT_CONFIG;
> chan->state = BT_CONFIG;
> release_sock(sk);
> --
> 2.43.0
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept
2026-11-18 21:33 [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept Mohammed Alquraini
2026-11-18 23:04 ` Luiz Augusto von Dentz
@ 2026-11-19 10:01 ` kernel test robot
1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2026-11-19 10:01 UTC (permalink / raw)
To: Mohammed Alquraini, linux-bluetooth
Cc: llvm, oe-kbuild-all, Marcel Holtmann, Luiz Augusto von Dentz,
linux-kernel, Mohammed Alquraini
Hi Mohammed,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bluetooth/master]
[also build test WARNING on bluetooth-next/master linus/master v7.4-rc3 next-20261116]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Mohammed-Alquraini/Bluetooth-L2CAP-check-security-before-completing-a-deferred-BR-EDR-accept/20261119-003322
base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth.git master
patch link: https://lore.kernel.org/r/20261118213322.60067-1-mohammed.saad.alquraini%40gmail.com
patch subject: [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20261119/202611191741.wWf1jeoY-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 3b14d7c370558b46e5f0c6494a05c492e59bf32c)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261119/202611191741.wWf1jeoY-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202611191741.wWf1jeoY-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> net/bluetooth/l2cap_sock.c:1256:14: warning: acquiring mutex 'sk->chan->conn->lock' that is already held [-Wthread-safety-analysis]
1256 | have_conn = l2cap_chan_lock_conn(chan);
| ^
net/bluetooth/l2cap_sock.c:1256:14: note: mutex acquired here
>> net/bluetooth/l2cap_sock.c:1281:5: warning: mutex 'sk->chan->conn->lock' is not held on every path through here [-Wthread-safety-analysis]
1281 | l2cap_chan_unlock_conn(chan, have_conn);
| ^
include/net/bluetooth/l2cap.h:1016:2: note: expanded from macro 'l2cap_chan_unlock_conn'
1016 | ({ \
| ^
net/bluetooth/l2cap_sock.c:1256:14: note: mutex acquired here
1256 | have_conn = l2cap_chan_lock_conn(chan);
| ^
2 warnings generated.
vim +1256 net/bluetooth/l2cap_sock.c
ce60b9231b66710 Sebastian Urban 2024-05-01 1241
32d86e048d8efed Pauli Virtanen 2026-08-29 1242 static int l2cap_sock_defer(struct sock *sk)
6898325923f9571 Gustavo Padovan 2011-02-04 1243 {
32d86e048d8efed Pauli Virtanen 2026-08-29 1244 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
32d86e048d8efed Pauli Virtanen 2026-08-29 1245 bool have_conn;
32d86e048d8efed Pauli Virtanen 2026-08-29 1246 int err = 0;
6898325923f9571 Gustavo Padovan 2011-02-04 1247
32d86e048d8efed Pauli Virtanen 2026-08-29 1248 /* Fast path check */
32d86e048d8efed Pauli Virtanen 2026-08-29 1249 lock_sock(sk);
32d86e048d8efed Pauli Virtanen 2026-08-29 1250 if (sk->sk_state != BT_CONNECT2) {
32d86e048d8efed Pauli Virtanen 2026-08-29 1251 release_sock(sk);
32d86e048d8efed Pauli Virtanen 2026-08-29 1252 return 0;
32d86e048d8efed Pauli Virtanen 2026-08-29 1253 }
32d86e048d8efed Pauli Virtanen 2026-08-29 1254 release_sock(sk);
11770f41b8a7e12 Pauli Virtanen 2025-03-18 1255
32d86e048d8efed Pauli Virtanen 2026-08-29 @1256 have_conn = l2cap_chan_lock_conn(chan);
6898325923f9571 Gustavo Padovan 2011-02-04 1257 lock_sock(sk);
6898325923f9571 Gustavo Padovan 2011-02-04 1258
c5daa683f2d3315 Gustavo Padovan 2012-05-16 1259 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
c5daa683f2d3315 Gustavo Padovan 2012-05-16 1260 &bt_sk(sk)->flags)) {
32d86e048d8efed Pauli Virtanen 2026-08-29 1261 err = 1;
32d86e048d8efed Pauli Virtanen 2026-08-29 1262
32d86e048d8efed Pauli Virtanen 2026-08-29 1263 if (!have_conn) {
32d86e048d8efed Pauli Virtanen 2026-08-29 1264 release_sock(sk);
32d86e048d8efed Pauli Virtanen 2026-08-29 1265 err = -ENOTCONN;
32d86e048d8efed Pauli Virtanen 2026-08-29 1266 } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) {
15f02b91056253e Luiz Augusto von Dentz 2020-03-02 1267 sk->sk_state = BT_CONNECTED;
32d86e048d8efed Pauli Virtanen 2026-08-29 1268 chan->state = BT_CONNECTED;
32d86e048d8efed Pauli Virtanen 2026-08-29 1269 release_sock(sk);
32d86e048d8efed Pauli Virtanen 2026-08-29 1270 __l2cap_ecred_conn_rsp_defer(chan);
32d86e048d8efed Pauli Virtanen 2026-08-29 1271 } else if (bdaddr_type_is_le(chan->src_type)) {
3831971355d901c Johan Hedberg 2013-05-17 1272 sk->sk_state = BT_CONNECTED;
32d86e048d8efed Pauli Virtanen 2026-08-29 1273 chan->state = BT_CONNECTED;
32d86e048d8efed Pauli Virtanen 2026-08-29 1274 release_sock(sk);
32d86e048d8efed Pauli Virtanen 2026-08-29 1275 __l2cap_le_connect_rsp_defer(chan);
3831971355d901c Johan Hedberg 2013-05-17 1276 } else {
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 1277 /* Defer completion until security resolves. */
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 1278 if (!l2cap_chan_check_security(chan, false)) {
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 1279 set_bit(BT_SK_ACCEPTED, &bt_sk(sk)->flags);
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 1280 release_sock(sk);
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 @1281 l2cap_chan_unlock_conn(chan, have_conn);
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 1282 return err;
e9a22bfeb7fdd1c Mohammed Alquraini 2026-11-19 1283 }
8c1d787be4b62d2 Gustavo Padovan 2011-04-13 1284 sk->sk_state = BT_CONFIG;
32d86e048d8efed Pauli Virtanen 2026-08-29 1285 chan->state = BT_CONFIG;
32d86e048d8efed Pauli Virtanen 2026-08-29 1286 release_sock(sk);
32d86e048d8efed Pauli Virtanen 2026-08-29 1287 __l2cap_connect_rsp_defer(chan);
32d86e048d8efed Pauli Virtanen 2026-08-29 1288 }
32d86e048d8efed Pauli Virtanen 2026-08-29 1289 } else {
32d86e048d8efed Pauli Virtanen 2026-08-29 1290 release_sock(sk);
3831971355d901c Johan Hedberg 2013-05-17 1291 }
3831971355d901c Johan Hedberg 2013-05-17 1292
32d86e048d8efed Pauli Virtanen 2026-08-29 1293 l2cap_chan_unlock_conn(chan, have_conn);
32d86e048d8efed Pauli Virtanen 2026-08-29 1294 return err;
6898325923f9571 Gustavo Padovan 2011-02-04 1295 }
6898325923f9571 Gustavo Padovan 2011-02-04 1296
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 10:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 21:33 [PATCH] Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept Mohammed Alquraini
2026-11-18 23:04 ` Luiz Augusto von Dentz
2026-11-19 10:01 ` kernel test robot
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®