Linux bluetooth development
 help / color / mirror / Atom feed
* [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  1:07 ` bluez.test.bot
  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  1:07 ` bluez.test.bot
  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: 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  1:07 ` bluez.test.bot
  1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2026-11-19  1:07 UTC (permalink / raw)
  To: linux-bluetooth, mohammed.saad.alquraini

[-- Attachment #1: Type: text/plain, Size: 3898 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1215445/

---Test result---

Test Summary:
CheckPatch                    FAIL      1.26 seconds
VerifyFixes                   PASS      0.13 seconds
VerifySignedoff               PASS      0.13 seconds
GitLint                       PASS      0.34 seconds
SubjectPrefix                 PASS      0.12 seconds
BuildKernel                   PASS      30.69 seconds
CheckAllWarning               PASS      35.07 seconds
CheckSparse                   PASS      37.80 seconds
BuildKernel32                 PASS      31.01 seconds
CheckKernelLLVM               FAIL      27.00 seconds
TestRunnerSetup               PASS      746.24 seconds
TestRunner_l2cap-tester       PASS      15.07 seconds
TestRunner_iso-tester         PASS      13.16 seconds
TestRunner_bnep-tester        PASS      3.20 seconds
TestRunner_mgmt-tester        PASS      89.06 seconds
TestRunner_rfcomm-tester      PASS      4.51 seconds
TestRunner_sco-tester         PASS      5.66 seconds
TestRunner_ioctl-tester       PASS      4.72 seconds
TestRunner_mesh-tester        PASS      5.57 seconds
TestRunner_smp-tester         PASS      4.37 seconds
TestRunner_userchan-tester    PASS      3.24 seconds
TestRunner_6lowpan-tester     PASS      4.47 seconds
IncrementalBuild              PASS      28.98 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
Bluetooth: L2CAP: check security before completing a deferred BR/EDR accept
WARNING: Prefer a maximum 75 chars per line (possible unwrapped commit description?)
#122: 
The child channel is placed on the parent's accept queue at CONN_REQ time and

total: 0 errors, 1 warnings, 0 checks, 36 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/home/runner/work/bluetooth-next/bluetooth-next/src/patch/14998992.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: CheckKernelLLVM - FAIL
Desc: Build kernel with LLVM + context analysis
Output:

net/bluetooth/l2cap_sock.c:1277:14: error: acquiring mutex 'sk->chan->conn->lock' that is already held [-Werror,-Wthread-safety-analysis]
 1277 |         have_conn = l2cap_chan_lock_conn(chan);
      |                     ^
net/bluetooth/l2cap_sock.c:1277:14: note: mutex acquired here
net/bluetooth/l2cap_sock.c:1302:5: error: mutex 'sk->chan->conn->lock' is not held on every path through here [-Werror,-Wthread-safety-analysis]
 1302 |                                 l2cap_chan_unlock_conn(chan, have_conn);
      |                                 ^
./include/net/bluetooth/l2cap.h:1025:2: note: expanded from macro 'l2cap_chan_unlock_conn'
 1025 |         ({                                                              \
      |         ^
net/bluetooth/l2cap_sock.c:1277:14: note: mutex acquired here
 1277 |         have_conn = l2cap_chan_lock_conn(chan);
      |                     ^
2 errors generated.
make[4]: *** [scripts/Makefile.build:290: net/bluetooth/l2cap_sock.o] Error 1
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:572: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:572: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/home/runner/work/bluetooth-next/bluetooth-next/src/src/Makefile:2243: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2


https://github.com/bluez/bluetooth-next/pull/1153

---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-19  1:07 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  1:07 ` bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox