From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v2 3/5] l2cap-tester: Add tests for security of deferred setup
Date: Fri, 20 Nov 2026 10:47:48 -0500 [thread overview]
Message-ID: <20261120154750.1745643-4-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261120154750.1745643-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Add the following tests:
L2CAP BR/EDR Server - Security Block Pairing Failed
L2CAP BR/EDR Server - Security Block Downgrade
L2CAP BR/EDR Server - Defer Success
L2CAP BR/EDR Server - Defer Encryption Off
L2CAP BR/EDR Server - Defer Early Accept
L2CAP LE Server - Defer Success
Defer Encryption Off has the peer disabling encryption while the
connection waits for the listener to authorize it, and Defer Early
Accept has the listener accepting and authorizing the connection while
authentication, which then fails, is still pending. Both expect the
connection to be blocked instead of being established.
Security Block Pairing Failed covers the connection being blocked once
pairing fails after the request has been responded as pending, since
L2CAP BR/EDR Server - Security Block is rejected before that, and
Security Block Downgrade covers pairing succeeding with a key that
doesn't meet the security level required.
Assisted-by: OpenCode:claude-fable-5.2
---
tools/l2cap-tester.c | 278 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 278 insertions(+)
diff --git a/tools/l2cap-tester.c b/tools/l2cap-tester.c
index 4d2c5cd15c64..7004955a1af7 100644
--- a/tools/l2cap-tester.c
+++ b/tools/l2cap-tester.c
@@ -26,6 +26,8 @@
#include "monitor/bt.h"
#include "emulator/bthost.h"
+#include "emulator/btdev.h"
+#include "emulator/vhci.h"
#include "emulator/hciemu.h"
#include "src/shared/tester.h"
@@ -54,6 +56,8 @@ struct test_data {
int sk2;
int reencrypt_sk;
int opp_sk;
+ int listen_sk;
+ struct tester_io *defer_io;
bool opp_encrypted;
bool host_disconnected;
int step;
@@ -129,6 +133,12 @@ struct l2cap_data {
bool direct_advertising;
bool close_1;
bool defer;
+ bool hold_auth;
+ bool encrypt_off;
+ /* Follow a pending connection response until the final one */
+ bool pending_rsp;
+ /* Link key type the controller reports instead of the actual one */
+ uint8_t link_key_type;
bool shut_sock_wr;
@@ -355,6 +365,8 @@ static void test_data_free(void *test_data)
test_l2cap(name, HCIEMU_TYPE_BREDRLE70, data, setup, func)
static uint8_t pair_device_pin[] = { 0x30, 0x30, 0x30, 0x30 }; /* "0000" */
+/* "1111" */
+static uint8_t pair_device_wrong_pin[] = { 0x31, 0x31, 0x31, 0x31 };
static const struct l2cap_data client_connect_success_test = {
.client_psm = 0x1001,
@@ -614,6 +626,111 @@ static const struct l2cap_data l2cap_server_sec_block_test = {
.enable_ssp = true,
};
+static const uint8_t l2cap_defer_sec_block_rsp[] = {
+ 0x40, 0x00, /* dcid */
+ 0x41, 0x00, /* scid */
+ 0x03, 0x00, /* Sec Block */
+ 0x00, 0x00 /* status */
+};
+
+/* No response is expected so the test only passes once the socket is
+ * connected, after the connection has been authorized.
+ */
+static const struct l2cap_data l2cap_server_defer_success_test = {
+ .server_psm = 0x1001,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+};
+
+/* Pairing fails after the connection request has been answered as pending,
+ * unlike l2cap_server_sec_block_test which is rejected right away.
+ */
+static const struct l2cap_data l2cap_server_sec_block_pairing_test = {
+ .server_psm = 0x1001,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_device_pin,
+ .pin_len = sizeof(pair_device_pin),
+ .client_pin = pair_device_wrong_pin,
+ .client_pin_len = sizeof(pair_device_wrong_pin),
+ .pending_rsp = true,
+};
+
+/* "0000000000000000" */
+static uint8_t pair_device_pin16[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x30, 0x30, 0x30, 0x30 };
+
+/* Pairing succeeds but the controller reports an unauthenticated key, so
+ * encryption completes with a security level below the one required.
+ */
+static const struct l2cap_data l2cap_server_sec_block_downgrade_test = {
+ .server_psm = 0x1001,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_HIGH,
+ .pin = pair_device_pin16,
+ .pin_len = sizeof(pair_device_pin16),
+ .client_pin = pair_device_pin16,
+ .client_pin_len = sizeof(pair_device_pin16),
+ .link_key_type = 0x04,
+ .pending_rsp = true,
+};
+
+/* The peer disables encryption while the connection waits for userspace to
+ * authorize it, the link no longer meets the security level so the connection
+ * shall be blocked instead of being offered again.
+ */
+static const struct l2cap_data l2cap_server_defer_encrypt_off_test = {
+ .server_psm = 0x1001,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_device_pin,
+ .pin_len = sizeof(pair_device_pin),
+ .client_pin = pair_device_pin,
+ .client_pin_len = sizeof(pair_device_pin),
+ .client_encrypt = true,
+ .encrypt_off = true,
+};
+
+/* The listener accepts and authorizes the connection while authentication is
+ * still pending and then authentication fails, the connection shall be
+ * blocked.
+ */
+static const struct l2cap_data l2cap_server_defer_early_accept_test = {
+ .server_psm = 0x1001,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_device_pin,
+ .pin_len = sizeof(pair_device_pin),
+ .client_pin = pair_device_wrong_pin,
+ .client_pin_len = sizeof(pair_device_wrong_pin),
+ .hold_auth = true,
+};
+
static const uint8_t l2cap_nval_psm_rsp[] = { 0x00, 0x00, /* dcid */
0x41, 0x00, /* scid */
0x02, 0x00, /* nval PSM */
@@ -1120,6 +1237,17 @@ static const struct l2cap_data le_server_success_test = {
.expect_cmd_len = sizeof(le_connect_rsp),
};
+static const struct l2cap_data le_server_defer_success_test = {
+ .server_psm = 0x0080,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_LE_CONN_REQ,
+ .send_cmd = le_connect_req,
+ .send_cmd_len = sizeof(le_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_LE_CONN_RSP,
+ .expect_cmd = le_connect_rsp,
+ .expect_cmd_len = sizeof(le_connect_rsp),
+};
+
static const uint8_t nval_le_connect_req[] = { 0x80, 0x00, /* PSM */
0x01, 0x00, /* SCID */
0x20, 0x00, /* MTU */
@@ -2223,6 +2351,20 @@ static void setup_powered_common(void)
if (test && test->reject_ssp)
bthost_set_reject_user_confirm(bthost, true);
+ if (test && test->hold_auth)
+ bthost_hold_auth_request(bthost, true);
+
+ if (test && test->link_key_type) {
+ struct vhci *vhci = hciemu_get_vhci(data->hciemu);
+
+ btdev_set_link_key_type(vhci_get_btdev(vhci),
+ test->link_key_type);
+ }
+
+ if (test && (test->hold_auth || test->encrypt_off ||
+ test->pending_rsp))
+ bthost_set_l2cap_rsp_pending(bthost, true);
+
if (data->hciemu_type >= HCIEMU_TYPE_LE)
mgmt_send(data->mgmt, MGMT_OP_SET_LE, data->mgmt_index,
sizeof(param), param, NULL, NULL, NULL);
@@ -3938,6 +4080,102 @@ static bool defer_accept(struct test_data *data, struct tester_io *io,
return true;
}
+/* Keep an accepted socket open until the test ends */
+static void defer_keep(struct test_data *data, struct tester_io *io)
+{
+ if (!data->io_channels)
+ data->io_channels = queue_new();
+
+ queue_push_tail(data->io_channels, io);
+}
+
+/* Authorize a deferred connection without closing its socket */
+static void defer_authorize(struct test_data *data, struct tester_io *io)
+{
+ int sk = tester_io_get_fd(io);
+ ssize_t ret;
+ char c;
+
+ ret = recv(sk, &c, 1, MSG_DONTWAIT);
+
+ tester_print("Authorize deferred setup sk = %d: %zd (%s)", sk, ret,
+ ret < 0 ? strerror(errno) : "ok");
+}
+
+/* Behave like a listener that does not wait to be notified: accept and
+ * authorize whatever connection is queued.
+ */
+static void early_accept(struct test_data *data)
+{
+ struct tester_io *io;
+ int new_sk;
+
+ new_sk = accept(data->listen_sk, NULL, NULL);
+ if (new_sk < 0) {
+ tester_print("Nothing to accept while security is pending: "
+ "%s (%d)", strerror(errno), errno);
+ return;
+ }
+
+ tester_print("Accepted sk = %d while security is pending", new_sk);
+
+ io = tester_io_new(new_sk);
+ tester_io_set_close_on_unref(io, true);
+
+ defer_keep(data, io);
+ defer_authorize(data, io);
+}
+
+/* Handle the responses of the tests that follow a pending connection until
+ * its final response. Returns true if there is nothing else to check.
+ */
+static bool server_security_rsp(struct test_data *data,
+ const struct bt_l2cap_pdu_conn_rsp *rsp)
+{
+ const struct l2cap_data *l2data = data->test_data;
+ struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+ uint16_t result = le16_to_cpu(rsp->result);
+ uint16_t status = le16_to_cpu(rsp->status);
+
+ tester_print("Connection response result 0x%04x status 0x%04x",
+ result, status);
+
+ if (!result) {
+ tester_warn("Connection accepted without required security");
+ tester_test_failed();
+ return true;
+ }
+
+ /* Not pending, check the final response */
+ if (result != 0x0001)
+ return false;
+
+ /* Authentication pending */
+ if (l2data->hold_auth && status == 0x0001 && !data->step) {
+ data->step = 1;
+ early_accept(data);
+ bthost_release_auth_request(bthost);
+ return true;
+ }
+
+ /* Authorization pending */
+ if (l2data->encrypt_off && status == 0x0002) {
+ if (!data->step) {
+ data->step = 1;
+ tester_print("Disabling encryption");
+ bthost_set_conn_encrypt(bthost, data->handle, 0x00);
+ return true;
+ }
+
+ /* Offered again after encryption was disabled, authorize it */
+ data->step = 2;
+ if (data->defer_io)
+ defer_authorize(data, data->defer_io);
+ }
+
+ return true;
+}
+
static bool l2cap_listen_cb(struct tester_io *io, uint32_t cond,
void *user_data)
{
@@ -3967,6 +4205,15 @@ static bool l2cap_listen_cb(struct tester_io *io, uint32_t cond,
return false;
}
+ /* Authorize only after the peer has disabled encryption */
+ if (l2data->encrypt_off) {
+ data->defer_io = io;
+ defer_keep(data, io);
+ if (data->step > 1)
+ defer_authorize(data, io);
+ return false;
+ }
+
if (!defer_accept(data, io, l2cap_accept_cb)) {
tester_warn("Unable to accept deferred setup");
tester_test_failed();
@@ -3994,6 +4241,12 @@ static void client_l2cap_rsp(uint8_t code, const void *data, uint16_t len,
if (code == BT_L2CAP_PDU_CONN_RSP) {
const struct bt_l2cap_pdu_conn_rsp *rsp = data;
+
+ if ((l2data->hold_auth || l2data->encrypt_off ||
+ l2data->pending_rsp) && len >= sizeof(*rsp) &&
+ server_security_rsp(test_data, rsp))
+ return;
+
if (len == sizeof(rsp) && !rsp->result && !rsp->status)
return;
@@ -4196,6 +4449,8 @@ static void start_test_server(const void *test_data, bthost_new_conn_cb conn_cb,
return;
}
+ data->listen_sk = sk;
+
io = tester_io_new(sk);
tester_io_set_close_on_unref(io, true);
@@ -4557,6 +4812,26 @@ int main(int argc, char *argv[])
&l2cap_server_sec_block_test,
setup_powered_server, test_server);
+ test_l2cap_bredr("L2CAP BR/EDR Server - Security Block Pairing Failed",
+ &l2cap_server_sec_block_pairing_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Security Block Downgrade",
+ &l2cap_server_sec_block_downgrade_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Defer Success",
+ &l2cap_server_defer_success_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Defer Encryption Off",
+ &l2cap_server_defer_encrypt_off_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Defer Early Accept",
+ &l2cap_server_defer_early_accept_test,
+ setup_powered_server, test_server);
+
test_l2cap_bredr("L2CAP BR/EDR Server - Invalid PSM",
&l2cap_server_nval_psm_test,
setup_powered_server, test_server);
@@ -4722,6 +4997,9 @@ int main(int argc, char *argv[])
setup_powered_client, test_connect);
test_l2cap_le("L2CAP LE Server - Success", &le_server_success_test,
setup_powered_server, test_server);
+ test_l2cap_le("L2CAP LE Server - Defer Success",
+ &le_server_defer_success_test,
+ setup_powered_server, test_server);
test_l2cap_le("L2CAP LE Server - Nval SCID", &le_server_nval_scid_test,
setup_powered_server, test_server);
test_l2cap_le("L2CAP LE Server - PHY", &le_server_phy_test,
--
2.56.0
next prev parent reply other threads:[~2026-11-20 15:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 15:47 [PATCH BlueZ v2 0/5] Add tests for security of deferred setup Luiz Augusto von Dentz
2026-11-20 15:47 ` [PATCH BlueZ v2 1/5] emulator/bthost: Add helpers to test " Luiz Augusto von Dentz
2026-11-20 22:28 ` Add tests for " bluez.test.bot
2026-11-20 15:47 ` [PATCH BlueZ v2 2/5] emulator/btdev: Add btdev_set_link_key_type Luiz Augusto von Dentz
2026-11-20 15:47 ` Luiz Augusto von Dentz [this message]
2026-11-20 15:47 ` [PATCH BlueZ v2 4/5] rfcomm-tester: Add tests for deferred setup Luiz Augusto von Dentz
2026-11-20 15:47 ` [PATCH BlueZ v2 5/5] l2cap-tester: Set IO capability in Client SSP - Success 2 Luiz Augusto von Dentz
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=20261120154750.1745643-4-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.