Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v1 2/2] l2cap-tester: Add tests for changing LE security with SMP pending
Date: Thu,  3 Dec 2026 13:14:42 -0500	[thread overview]
Message-ID: <20261203181442.774979-3-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261203181442.774979-1-luiz.dentz@gmail.com>

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


      parent reply	other threads:[~2026-12-03 18:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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=20261203181442.774979-3-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox