Skip to contentSkip to conversation

monitor: Fix reads beyond advertising data

PATCH In review Roland Schober 2 participants 8 messages Nov 23 – Nov 24 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

v2 cover monitor: Fix reads beyond advertising data

print_eir() and the decoders it calls take advertising data, which a remote device chooses. This series fixes six places where btmon reads or writes beyond that data, or decodes it from the wrong place:

- a Shortened Local Name, Complete Local Name or Broadcast Name of 240 or more octets is copied beyond a buffer of 239 octets,
- the Flags and the Security Manager OOB Flags are read from an AD structure which has no data,
- an Unprovisioned Device Beacon of 18 octets is decoded although its OOB Information lacks an octet,
- the Apple identifier is printed as a string which nothing terminates,
- the Program Info metadata is printed without a limit to its length,
- a Public Broadcast Announcement is reported as invalid when it is well formed, and decoded from uninitialised variables when it is too short.

Copilot found the first one in its review of "monitor: Decode Periodic Advertising with Responses" v4, whose new decoders hand advertising data to print_eir() as well. The bug is older than that series and can be reached without it, e.g. by an LE Periodic Advertising Report, so the fixes come as a series of their own. A v5 of the decoding series will follow on top of it.

The others were found by feeding print_eir() one AD structure of every type with every length up to 250 octets, in a buffer of exactly that size with AddressSanitizer, and by review. The same test passes with the series applied. The reads of patches 1 to 5 show there as AddressSanitizer reports; patch 6 was tested with hand-made btsnoop traces only.

For v2 the advertising data was also fuzzed with libFuzzer and AddressSanitizer, part of the time with UndefinedBehaviorSanitizer as well, as LE advertising data and as an Extended Inquiry Response. Without the series it finds the reads of patches 1 to 5 within a minute, the one of patch 5 through a Basic Audio Announcement. With it, more than 200 million inputs, which run every line of print_eir(), found nothing.

v2:
- Add patch 5. Copilot found that the fix of the Public Broadcast Announcements, now patch 6, lets them reach the Program Info decoding, which reads beyond the string.
- Rebase.

Roland Schober (6):
monitor: Fix overflow of the name buffer in print_eir
monitor: Fix reading Flags beyond empty AD structures
monitor: Fix reading beyond a short Mesh Beacon
monitor: Fix reading beyond the Apple manufacturer data shared/bap: Fix reading beyond the Program Info
monitor: Fix decoding of Public Broadcast Announcements

 monitor/packet.c       | 29 ++++++++++++++++++-----------
 src/shared/bap-debug.c |  2 +-
 2 files changed, 19 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/6 monitor: Fix overflow of the name buffer in print_eir

print_eir() copies the Shortened Local Name, the Complete Local Name and the Broadcast Name into a buffer of 239 octets, which is enough for the 240 octets of an Extended Inquiry Response. Advertising data from other packets can have longer AD structures: an LE Periodic Advertising Report with a Complete Local Name of 240 octets makes AddressSanitizer report a write beyond the buffer.

Size the buffer for the largest AD structure.

Fixes 854c945433fc ("monitor: Add full EIR parsing support")
Fixes 99deeea86f15 ("btmon: Decode Broadcast Name")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
monitor/packet.c +1 -1
index eddf8706a..bce3bb819 100644
@@ -4418,7 +4418,7 @@ static void print_eir(const char *label, const uint8_t *eir, uint8_t eir_len,
 		uint8_t field_len = eir[0];
 		const uint8_t *data = &eir[2];
 		uint8_t data_len;
-		char name[239], label[100];
+		char name[255], label[100];
 		uint8_t flags, mask;
 
 		/* Check for the end of EIR */
Signature · 1 line
2.47.3
#2 lore raw

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/1220153/

---Test result---

Test Summary:

CheckPatch                    PASS      2.62 seconds
GitLint                       PASS      1.83 seconds
BuildEll                      PASS      17.83 seconds
BluezMake                     PASS      267.66 seconds
MakeCheck                     PASS      12.32 seconds
MakeDistcheck                 PASS      136.21 seconds
CheckValgrind                 PASS      307.60 seconds
CheckSmatch                   WARNING   243.81 seconds
bluezmakeextell               PASS      89.05 seconds
TestFunctional                PASS      1095.63 seconds
IncrementalBuild              PASS      328.34 seconds
ScanBuild                     PASS      945.89 seconds

Details
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
monitor/packet.c:2292:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structuresmonitor/packet.c:2292:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structuresmonitor/packet.c:2292:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structuresmonitor/packet.c:2292:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structuresmonitor/packet.c:2292:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structures

https://github.com/bluez/bluez/pull/2890

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v2 2/6 monitor: Fix reading Flags beyond empty AD structures

In reply to Roland Schober

print_eir() reads the first data octet of the Flags and of the Security Manager OOB Flags without checking that there is one. An AD structure which only has its type, at the end of the advertising data, makes it read one octet beyond the data.

Fixes 854c945433fc ("monitor: Add full EIR parsing support")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 4 ++++
 1 file changed, 4 insertions(+)
monitor/packet.c +4 -0
index bce3bb819..4f63cc0fb 100644
@@ -4437,6 +4437,8 @@ static void print_eir(const char *label, const uint8_t *eir, uint8_t eir_len,
 
 		switch (eir[1]) {
 		case BT_EIR_FLAGS:
+			if (data_len < 1)
+				break;
 			flags = *data;
 
 			print_field("Flags: 0x%2.2x", flags);
@@ -4534,6 +4536,8 @@ static void print_eir(const char *label, const uint8_t *eir, uint8_t eir_len,
 			break;
 
 		case BT_EIR_SMP_OOB_FLAGS:
+			if (data_len < 1)
+				break;
 			print_field("SMP OOB Flags: 0x%2.2x", *data);
 			break;
 
Signature · 1 line
2.47.3
#4 lore raw

PATCH v2 3/6 monitor: Fix reading beyond a short Mesh Beacon

In reply to Roland Schober

The Unprovisioned Device Beacon has the beacon type, 16 octets of Device UUID and 2 octets of OOB Information, but print_mesh_beacon() decodes the OOB Information of a beacon with 18 octets, which lacks its second octet. Require 19 octets, and dump the octets after the OOB Information, not from its second octet on, if there is no URI Hash.

Fixes 0ba12681f3e4 ("monitor: Add basic decoding for Mesh Beacon")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
monitor/packet.c +2 -2
index 4f63cc0fb..6646dc147 100644
@@ -4205,7 +4205,7 @@ static void print_mesh_beacon(const uint8_t *data, uint8_t len)
 	switch (data[0]) {
 	case 0x00:
 		print_field("  Unprovisioned Device Beacon (0x00)");
-		if (len < 18) {
+		if (len < 19) {
 			packet_hexdump(data + 1, len - 1);
 			break;
 		}
@@ -4218,7 +4218,7 @@ static void print_mesh_beacon(const uint8_t *data, uint8_t len)
 		print_bitfield(4, oob, mesh_oob_table);
 
 		if (len < 23) {
-			packet_hexdump(data + 18, len - 18);
+			packet_hexdump(data + 19, len - 19);
 			break;
 		}
 
Signature · 1 line
2.47.3
#5 lore raw

PATCH v2 4/6 monitor: Fix reading beyond the Apple manufacturer data

In reply to Roland Schober

print_manufacturer_apple() reads the type before it checks that there is one, and prints the identifier as a string although nothing terminates it, so up to 98 octets which follow the AD structure are printed with it.

Check the length first and print the identifier with its length.

Fixes 35b0d0c7c281 ("monitor: Decode Apple identifier and iBeacon data structures")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
monitor/packet.c +4 -6
index 6646dc147..042a2ecb6 100644
@@ -3763,18 +3763,16 @@ static void print_fec(uint8_t fec)
 
 static void print_manufacturer_apple(const void *data, uint8_t data_len)
 {
-	uint8_t type = *((uint8_t *) data);
+	uint8_t type;
 
 	if (data_len < 1)
 		return;
 
-	if (type == 0x01) {
-		char identifier[100];
+	type = *((uint8_t *) data);
 
-		snprintf(identifier, sizeof(identifier) - 1, "%s",
+	if (type == 0x01) {
+		print_field("  Identifier: %.*s", data_len - 1,
 						(const char *) (data + 1));
-
-		print_field("  Identifier: %s", identifier);
 		return;
 	}
 
Signature · 1 line
2.47.3
#6 lore raw

PATCH v2 5/6 shared/bap: Fix reading beyond the Program Info

In reply to Roland Schober

The Program Info metadata is printed with "%*s", which sets a minimum field width and not a limit, so the string is read up to the next zero octet, beyond the metadata and possibly beyond the packet.

Print it with "%.*s" like the Program Info URI.

Fixes 6d4491649e06 ("shared/bap: Add debug helpers")
Assisted-by Claude:claude-fable-5-2
---
 src/shared/bap-debug.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/shared/bap-debug.c +1 -1
index b4352bdd3..772bd0a71 100644
@@ -501,7 +501,7 @@ static void ase_debug_program_info(const uint8_t *data, uint8_t len,
 		goto done;
 	}
 
-	util_debug(func, user_data, "Program Info: %*s", len, str);
+	util_debug(func, user_data, "Program Info: %.*s", len, str);
 
 done:
 	if (frame.iov_len)
Signature · 1 line
2.47.3
#7 lore raw

PATCH v2 6/6 monitor: Fix decoding of Public Broadcast Announcements

In reply to Roland Schober

print_pbp_announcement() takes the result of util_iov_pull_u8() the wrong way round: a well formed announcement is reported as "invalid features", and an empty one is decoded from uninitialised variables, which can hand a NULL pointer to bt_bap_debug_metadata().

Check the metadata length against the remaining data as well.

Fixes 3171f7fd3685 ("monitor: Add decoding support for Public Broadcast Announcements")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
monitor/packet.c +7 -2
index 042a2ecb6..7fdab7cc5 100644
@@ -4101,7 +4101,7 @@ static void print_pbp_announcement(const uint8_t *data, uint8_t data_len)
 	iov.iov_base = (void *) data;
 	iov.iov_len = data_len;
 
-	if (util_iov_pull_u8(&iov, &feat)) {
+	if (!util_iov_pull_u8(&iov, &feat)) {
 		print_text(COLOR_ERROR, "  invalid features");
 		return;
 	}
@@ -4113,7 +4113,7 @@ static void print_pbp_announcement(const uint8_t *data, uint8_t data_len)
 		print_text(COLOR_UNKNOWN_SERVICE_CLASS,
 					"  Unknown feature (0x%2.2x)", mask);
 
-	if (util_iov_pull_u8(&iov, &len)) {
+	if (!util_iov_pull_u8(&iov, &len)) {
 		print_text(COLOR_ERROR, "  invalid metadata length");
 		return;
 	}
@@ -4122,6 +4122,11 @@ static void print_pbp_announcement(const uint8_t *data, uint8_t data_len)
 		return;
 
 	meta.iov_base = util_iov_pull_mem(&iov, len);
+	if (!meta.iov_base) {
+		print_text(COLOR_ERROR, "  invalid metadata");
+		return;
+	}
+
 	meta.iov_len = len;
 
 	bt_bap_debug_metadata(meta.iov_base, meta.iov_len, print_ltv,
Signature · 1 line
2.47.3