* [PATCH BlueZ v1 0/2] l2cap-tester: Add tests for changing LE security with SMP pending
@ 2026-12-03 18:14 Luiz Augusto von Dentz
2026-12-03 18:14 ` [PATCH BlueZ v1 1/2] emulator: Add helpers to hold SMP and send LE credits Luiz Augusto von Dentz
2026-12-03 18:14 ` [PATCH BlueZ v1 2/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
0 siblings, 2 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-03 18:14 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This adds emulator helpers to hold SMP while a security upgrade is
pending and to send LE credits from the peer, and l2cap-tester cases
using them to check that credits don't resume an LE CoC before the
upgrade completes and that data received meanwhile is not dropped.
This is the companion userspace set of the kernel set:
[PATCH v17 0/2] Bluetooth: L2CAP: Fix LE credits resuming channels
pending security
Without the kernel set, L2CAP LE Client Change Sec - Credits fails with
"Channel resumed with security pending".
Luiz Augusto von Dentz (2):
emulator: Add helpers to hold SMP and send LE credits
l2cap-tester: Add tests for changing LE security with SMP pending
emulator/bthost.c | 57 ++++++++++++++
emulator/bthost.h | 4 +
tools/l2cap-tester.c | 179 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 240 insertions(+)
--
2.56.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH BlueZ v1 1/2] emulator: Add helpers to hold SMP and send LE credits
2026-12-03 18:14 [PATCH BlueZ v1 0/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
@ 2026-12-03 18:14 ` Luiz Augusto von Dentz
2026-12-03 18:14 ` [PATCH BlueZ v1 2/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-03 18:14 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Add bthost_hold_smp() and bthost_release_smp() to hold the first SMP
PDU received and only process it when released, so tests can act while
a security procedure is pending, and bthost_send_le_credits() to send
an LE Flow Control Credit packet for a given channel.
Assisted-by: OpenCode:claude-opus-5.5
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
emulator/bthost.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++
emulator/bthost.h | 4 ++++
2 files changed, 61 insertions(+)
diff --git a/emulator/bthost.c b/emulator/bthost.c
index afc83891778e..e0c70a3babc8 100644
--- a/emulator/bthost.c
+++ b/emulator/bthost.c
@@ -170,6 +170,8 @@ struct btconn {
struct iso_hook *iso_hook;
struct btconn *next;
void *smp_data;
+ void *smp_held;
+ uint16_t smp_held_len;
uint16_t recv_len;
uint16_t data_len;
void *recv_data;
@@ -276,6 +278,7 @@ struct bthost {
bool reject_user_confirm;
bool hold_auth_request;
bool auth_request_held;
+ bool hold_smp;
bool auth_request_pin;
bool l2cap_rsp_pending;
uint8_t auth_request_bdaddr[6];
@@ -330,6 +333,8 @@ static void btconn_free(struct btconn *conn)
if (conn->smp_data)
smp_conn_del(conn->smp_data);
+ free(conn->smp_held);
+
while (conn->l2conns) {
struct l2conn *l2conn = conn->l2conns;
@@ -3339,6 +3344,14 @@ static void process_l2cap(struct bthost *bthost, struct btconn *conn,
l2cap_le_sig(bthost, conn, data.iov_base, data.iov_len);
break;
case 0x0006:
+ /* Hold the first SMP PDU until bthost_release_smp() */
+ if (bthost->hold_smp && !conn->smp_held) {
+ conn->smp_held = util_memdup(data.iov_base,
+ data.iov_len);
+ conn->smp_held_len = data.iov_len;
+ break;
+ }
+
smp_data(conn->smp_data, data.iov_base, data.iov_len);
break;
case 0x0007:
@@ -4346,6 +4359,50 @@ void bthost_release_auth_request(struct bthost *bthost)
}
}
+void bthost_send_le_credits(struct bthost *bthost, uint16_t handle,
+ uint16_t cid, uint16_t credits)
+{
+ struct bt_l2cap_pdu_le_flowctl_creds creds;
+ struct btconn *conn;
+ struct l2conn *l2conn;
+
+ conn = bthost_find_conn(bthost, handle);
+ if (!conn)
+ return;
+
+ l2conn = btconn_find_l2cap_conn_by_dcid(conn, cid);
+ if (!l2conn)
+ return;
+
+ creds.cid = cpu_to_le16(l2conn->scid);
+ creds.credits = cpu_to_le16(credits);
+ l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_LE_FLOWCTL_CREDS, 0,
+ &creds, sizeof(creds));
+}
+
+void bthost_hold_smp(struct bthost *bthost, bool hold)
+{
+ bthost->hold_smp = hold;
+}
+
+void bthost_release_smp(struct bthost *bthost, uint16_t handle)
+{
+ struct btconn *conn;
+ void *pdu;
+
+ bthost->hold_smp = false;
+
+ conn = bthost_find_conn(bthost, handle);
+ if (!conn || !conn->smp_held)
+ return;
+
+ pdu = conn->smp_held;
+ conn->smp_held = NULL;
+
+ smp_data(conn->smp_data, pdu, conn->smp_held_len);
+ free(pdu);
+}
+
void bthost_set_l2cap_rsp_pending(struct bthost *bthost, bool enable)
{
bthost->l2cap_rsp_pending = enable;
diff --git a/emulator/bthost.h b/emulator/bthost.h
index 92aaf42305da..1253a24a367e 100644
--- a/emulator/bthost.h
+++ b/emulator/bthost.h
@@ -208,6 +208,10 @@ void bthost_set_auth_req(struct bthost *bthost, uint8_t auth_req);
uint8_t bthost_get_auth_req(struct bthost *bthost);
void bthost_hold_auth_request(struct bthost *bthost, bool hold);
void bthost_release_auth_request(struct bthost *bthost);
+void bthost_hold_smp(struct bthost *bthost, bool hold);
+void bthost_release_smp(struct bthost *bthost, uint16_t handle);
+void bthost_send_le_credits(struct bthost *bthost, uint16_t handle,
+ uint16_t cid, uint16_t credits);
void bthost_set_l2cap_rsp_pending(struct bthost *bthost, bool enable);
void bthost_set_conn_encrypt(struct bthost *bthost, uint16_t handle,
uint8_t encr_mode);
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH BlueZ v1 2/2] l2cap-tester: Add tests for changing LE security with SMP pending
2026-12-03 18:14 [PATCH BlueZ v1 0/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
2026-12-03 18:14 ` [PATCH BlueZ v1 1/2] emulator: Add helpers to hold SMP and send LE credits Luiz Augusto von Dentz
@ 2026-12-03 18:14 ` Luiz Augusto von Dentz
1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2026-12-03 18:14 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Add the following tests:
L2CAP LE Client Change Sec - Credits
L2CAP LE Client Change Sec - Data
Both raise the security level of a connected LE CoC while SMP is held,
the first sends credits from the peer and checks that they don't resume
the channel before the upgrade completes, the second sends data from the
peer and checks that it is not dropped while the upgrade is pending.
Assisted-by: OpenCode:claude-opus-5.5
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
tools/l2cap-tester.c | 179 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 179 insertions(+)
diff --git a/tools/l2cap-tester.c b/tools/l2cap-tester.c
index ca868e9d72ec..bc80d90c8835 100644
--- a/tools/l2cap-tester.c
+++ b/tools/l2cap-tester.c
@@ -56,6 +56,7 @@ struct test_data {
int sk2;
int reencrypt_sk;
int opp_sk;
+ int sec_sk;
int listen_sk;
struct tester_io *defer_io;
bool opp_encrypted;
@@ -171,6 +172,11 @@ struct l2cap_data {
/* Change security level to this after connecting */
int change_sec_level;
+
+ /* Hold SMP while changing the security level so the peer can send
+ * credits (write_data) or data (read_data) while it is pending.
+ */
+ bool hold_smp;
};
static void print_debug(const char *str, void *user_data)
@@ -1040,6 +1046,26 @@ static const struct l2cap_data le_client_change_sec_success_test = {
.change_sec_level = BT_SECURITY_MEDIUM,
};
+static const struct l2cap_data le_client_change_sec_credits_test = {
+ .client_psm = 0x0080,
+ .server_psm = 0x0080,
+ .sec_level = BT_SECURITY_LOW,
+ .change_sec_level = BT_SECURITY_MEDIUM,
+ .hold_smp = true,
+ .write_data = l2_data,
+ .data_len = sizeof(l2_data),
+};
+
+static const struct l2cap_data le_client_change_sec_data_test = {
+ .client_psm = 0x0080,
+ .server_psm = 0x0080,
+ .sec_level = BT_SECURITY_LOW,
+ .change_sec_level = BT_SECURITY_MEDIUM,
+ .hold_smp = true,
+ .read_data = l2_data,
+ .data_len = sizeof(l2_data),
+};
+
static const uint8_t cmd_reject_rsp[] = { 0x01, 0x01, 0x02, 0x00, 0x00, 0x00 };
static const struct l2cap_data le_client_connect_reject_test_1 = {
@@ -3168,6 +3194,145 @@ static void l2cap_write_data(struct test_data *data, struct tester_io *io,
}
}
+static bool change_sec_held_done(void *user_data)
+{
+ struct test_data *data = tester_get_data();
+ const struct l2cap_data *l2data = data->test_data;
+ struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+ struct bt_security sec;
+ socklen_t len = sizeof(sec);
+ ssize_t ret;
+
+ memset(&sec, 0, sizeof(sec));
+ if (getsockopt(data->sec_sk, SOL_BLUETOOTH, BT_SECURITY, &sec,
+ &len) < 0) {
+ tester_warn("getsockopt(BT_SECURITY): %s (%d)",
+ strerror(errno), errno);
+ tester_test_failed();
+ return false;
+ }
+
+ if (sec.level != l2data->change_sec_level)
+ return true;
+
+ if (!l2data->write_data) {
+ tester_test_passed();
+ goto done;
+ }
+
+ /* The upgrade completed, the data shall now be sent */
+ ret = write(data->sec_sk, l2data->write_data, l2data->data_len);
+ if (ret < 0 && errno == EAGAIN)
+ return true;
+
+ if (ret != l2data->data_len) {
+ tester_warn("Unable to write: %s (%d)", strerror(errno),
+ errno);
+ tester_test_failed();
+ goto done;
+ }
+
+ data->step = 1;
+ bthost_add_cid_hook(bthost, data->handle, data->dcid,
+ bthost_received_data, NULL);
+
+ return false;
+
+done:
+ close(data->sec_sk);
+ data->sec_sk = -1;
+ return false;
+}
+
+static bool change_sec_held(void *user_data)
+{
+ struct test_data *data = tester_get_data();
+ const struct l2cap_data *l2data = data->test_data;
+ struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+ uint8_t buf[64];
+ ssize_t ret;
+
+ if (l2data->write_data) {
+ /* The credits must not resume the channel before the upgrade
+ * completes.
+ */
+ ret = write(data->sec_sk, l2data->write_data,
+ l2data->data_len);
+ if (ret >= 0 || errno != EAGAIN) {
+ tester_warn("Channel resumed with security pending");
+ tester_test_failed();
+ return false;
+ }
+ }
+
+ if (l2data->read_data) {
+ /* Data received while the upgrade is pending must not be
+ * dropped.
+ */
+ ret = recv(data->sec_sk, buf, sizeof(buf), MSG_DONTWAIT);
+ if (ret != l2data->data_len ||
+ memcmp(buf, l2data->read_data, ret)) {
+ tester_warn("Data dropped with security pending");
+ tester_test_failed();
+ return false;
+ }
+ }
+
+ tester_print("Releasing SMP");
+
+ bthost_release_smp(bthost, data->handle);
+ timeout_add(50, change_sec_held_done, NULL, NULL);
+
+ return false;
+}
+
+static int change_sec_hold(struct test_data *data, int sk)
+{
+ const struct l2cap_data *l2data = data->test_data;
+ struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+ struct bt_security sec;
+ int err;
+
+ /* The socket is closed once the connect callback returns */
+ data->sec_sk = dup(sk);
+ if (data->sec_sk < 0)
+ return -errno;
+
+ bthost_hold_smp(bthost, true);
+
+ memset(&sec, 0, sizeof(sec));
+ sec.level = l2data->change_sec_level;
+
+ tester_print("Changing security level to %d with SMP held", sec.level);
+
+ if (setsockopt(sk, SOL_BLUETOOTH, BT_SECURITY, &sec, sizeof(sec)) < 0) {
+ err = -errno;
+ tester_warn("Can't set security level: %s (%d)",
+ strerror(errno), errno);
+ return err;
+ }
+
+ if (l2data->write_data) {
+ if (write(sk, l2data->write_data, l2data->data_len) >= 0 ||
+ errno != EAGAIN) {
+ tester_warn("Channel not suspended with security "
+ "pending");
+ return -EINVAL;
+ }
+
+ bthost_send_le_credits(bthost, data->handle, data->dcid, 10);
+ }
+
+ if (l2data->read_data)
+ bthost_send_cid(bthost, data->handle, data->dcid,
+ l2data->read_data, l2data->data_len);
+
+ /* Give the kernel time to process what the peer sent */
+ timeout_add(200, change_sec_held, NULL, NULL);
+
+ return 0;
+}
+
static bool l2cap_connect_cb(struct tester_io *io, uint32_t cond,
void *user_data)
{
@@ -3197,6 +3362,12 @@ static bool l2cap_connect_cb(struct tester_io *io, uint32_t cond,
return false;
}
+ if (l2data->hold_smp) {
+ if (change_sec_hold(data, sk) < 0)
+ tester_test_failed();
+ return false;
+ }
+
err = check_phys_and_sec(data, sk);
if (err < 0) {
if (err == -EINPROGRESS) {
@@ -3437,6 +3608,8 @@ static void test_connect(const void *test_data)
struct tester_io *io;
int sk;
+ data->sec_sk = -1;
+
if (l2data->opp_encrypt) {
data->opp_sk = -1;
hciemu_add_hook(data->hciemu, HCIEMU_HOOK_POST_EVT,
@@ -5071,6 +5244,12 @@ int main(int argc, char *argv[])
test_l2cap_le("L2CAP LE Client Change Sec - Success",
&le_client_change_sec_success_test,
setup_powered_client, test_connect);
+ test_l2cap_le("L2CAP LE Client Change Sec - Credits",
+ &le_client_change_sec_credits_test,
+ setup_powered_client, test_connect);
+ test_l2cap_le("L2CAP LE Client Change Sec - Data",
+ &le_client_change_sec_data_test,
+ setup_powered_client, test_connect);
test_l2cap_le("L2CAP LE Client - Command Reject",
&le_client_connect_reject_test_1,
setup_powered_client, test_connect);
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 18:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 18:14 [PATCH BlueZ v1 0/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
2026-12-03 18:14 ` [PATCH BlueZ v1 1/2] emulator: Add helpers to hold SMP and send LE credits Luiz Augusto von Dentz
2026-12-03 18:14 ` [PATCH BlueZ v1 2/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox