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 v2 03/10] unit/test-eir: Add tests for the longest local names
Date: Thu, 20 Aug 2026 14:30:30 -0400	[thread overview]
Message-ID: <20260820183037.2713973-4-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20260820183037.2713973-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Nothing covered a name anywhere near the size of the buffer it is copied
into, which is why the missing clamp went unnoticed.

Add two tests. The first uses a name of HCI_MAX_NAME_LENGTH bytes, the
longest one that fits, to pin the boundary down.

The second uses a name of 253 bytes, as large as eir_parse() can be
handed given the EIR length is a single byte, and which does not fit.
Run against the code before the previous patch, it dies with

  *** buffer overflow detected ***: terminated

Assisted-by: Claude:claude-opus-5
---
 unit/test-eir.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/unit/test-eir.c b/unit/test-eir.c
index 62164ca993f6..326bc899e251 100644
--- a/unit/test-eir.c
+++ b/unit/test-eir.c
@@ -440,6 +440,64 @@ static const struct test_data iso_2022_jp_name_test = {
 	.tx_power = 127,
 };
 
+/*
+ * A complete local name of HCI_MAX_NAME_LENGTH bytes, the longest one that
+ * fits the buffer eir_parse() copies the name into.
+ */
+static unsigned char max_name_data[HCI_MAX_NAME_LENGTH + 2];
+static char max_name[HCI_MAX_NAME_LENGTH + 1];
+
+static const struct test_data max_name_test = {
+	.eir_data = max_name_data,
+	.eir_size = sizeof(max_name_data),
+	.name = max_name,
+	.name_complete = true,
+	.tx_power = 127,
+};
+
+static void max_name_setup(const void *data)
+{
+	max_name_data[0] = sizeof(max_name_data) - 1;
+	max_name_data[1] = EIR_NAME_COMPLETE;
+	memset(max_name_data + 2, 'A', HCI_MAX_NAME_LENGTH);
+
+	memset(max_name, 'A', HCI_MAX_NAME_LENGTH);
+	max_name[HCI_MAX_NAME_LENGTH] = '\0';
+
+	tester_setup_complete();
+}
+
+/*
+ * The longest complete local name eir_parse() can be handed at all, which is
+ * bounded by the EIR length being a single byte. That is 253 bytes, more than
+ * the buffer it is copied into, so this used to overflow it.
+ */
+static unsigned char long_name_data[255];
+static char long_name[sizeof(long_name_data) - 2 + 1];
+
+/* The name does not fit, so it comes back clamped to HCI_MAX_NAME_LENGTH */
+#define LONG_NAME_LEN	HCI_MAX_NAME_LENGTH
+
+static const struct test_data long_name_test = {
+	.eir_data = long_name_data,
+	.eir_size = sizeof(long_name_data),
+	.name = long_name,
+	.name_complete = true,
+	.tx_power = 127,
+};
+
+static void long_name_setup(const void *data)
+{
+	long_name_data[0] = sizeof(long_name_data) - 1;
+	long_name_data[1] = EIR_NAME_COMPLETE;
+	memset(long_name_data + 2, 'B', sizeof(long_name_data) - 2);
+
+	memset(long_name, 'B', LONG_NAME_LEN);
+	long_name[LONG_NAME_LEN] = '\0';
+
+	tester_setup_complete();
+}
+
 static const unsigned char bluesc_data[] = {
 		0x02, 0x01, 0x06, 0x03, 0x02, 0x16, 0x18, 0x12,
 		0x09, 0x57, 0x61, 0x68, 0x6f, 0x6f, 0x20, 0x42,
@@ -756,6 +814,10 @@ int main(int argc, char *argv[])
 									NULL);
 	tester_add("/eir/iso-2022-jp-name", &iso_2022_jp_name_test, NULL,
 							test_parsing, NULL);
+	tester_add("/eir/max-name", &max_name_test, max_name_setup,
+							test_parsing, NULL);
+	tester_add("/eir/long-name", &long_name_test, long_name_setup,
+							test_parsing, NULL);
 	tester_add("/ad/bluesc", &bluesc_test, NULL, test_parsing, NULL);
 	tester_add("/ad/wahooscale", &wahoo_scale_test, NULL, test_parsing,
 									NULL);
-- 
2.54.0


  parent reply	other threads:[~2026-08-20 18:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-20 18:30 [PATCH BlueZ v2 00/10] Replace the name2utf8 copies with str2utf8 Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 01/10] eir: Fix stack buffer overflow when parsing the remote name Luiz Augusto von Dentz
2026-08-20 20:04   ` Replace the name2utf8 copies with str2utf8 bluez.test.bot
2026-08-20 18:30 ` [PATCH BlueZ v2 02/10] shared/ad: Fix reading past the name that was copied Luiz Augusto von Dentz
2026-08-20 18:30 ` Luiz Augusto von Dentz [this message]
2026-08-20 18:30 ` [PATCH BlueZ v2 04/10] shared/util: Make strnlenutf8 reject ill-formed sequences Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 05/10] shared/util: Add str2utf8 Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 06/10] unit/test-util: Add str2utf8 tests Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 07/10] Replace the name2utf8 copies with str2utf8 Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 08/10] device: Fix the name truncation splitting UTF-8 sequences Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 09/10] device: Rename btd_device_device_set_name to btd_device_set_name Luiz Augusto von Dentz
2026-08-20 18:30 ` [PATCH BlueZ v2 10/10] unit/test-util: Cover strtoutf8 with the str2utf8 tests 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=20260820183037.2713973-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox