Skip to contentSkip to conversation

monitor: Fix reads beyond advertising data

PATCH Superseded Roland Schober 3 participants 8 messages Nov 20 – Nov 23 Bluetooth drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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 five 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,
- 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 4 show there as AddressSanitizer reports; patch 5 was tested with hand-made btsnoop traces only.

Roland Schober (5):
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
monitor: Fix decoding of Public Broadcast Announcements

 monitor/packet.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/5 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/1217829/

---Test result---

Test Summary:

CheckPatch                    PASS      1.97 seconds
GitLint                       PASS      1.37 seconds
BuildEll                      PASS      21.72 seconds
BluezMake                     PASS      320.55 seconds
MakeCheck                     PASS      0.94 seconds
MakeDistcheck                 PASS      149.82 seconds
CheckValgrind                 PASS      160.43 seconds
CheckSmatch                   WARNING   322.69 seconds
bluezmakeextell               PASS      102.01 seconds
TestFunctional                PASS      1252.88 seconds
IncrementalBuild              PASS      364.47 seconds
ScanBuild                     PASS      1179.33 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/2861

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH 2/5 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 3/5 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 4/5 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 5/5 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
#7 lore raw
In reply to Roland Schober

Hi Roland,

Quoted 38 lines · Roland Schober

print_eir() and the decoders it calls take advertising data, which a remote device chooses. This series fixes five 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,
- 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 4 show there as AddressSanitizer reports; patch 5 was tested with hand-made btsnoop traces only.

Roland Schober (5):
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
monitor: Fix decoding of Public Broadcast Announcements

Quoted 7 lines
 monitor/packet.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

--
2.47.3

Copilot flagged another problem:
https://github.com/bluez/bluez/pull/2861#pullrequestreview-6025049968

Signature · 1 line
Luiz Augusto von Dentz