Skip to contentSkip to conversation

Fixed a buffer overflow found by OSS-Fuzz.

PATCH In review Oliver Chang 3 participants 6 messages Aug 8, 2025 1 message not in the archive Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

cover Fixed a buffer overflow found by OSS-Fuzz.

This fixes an old issue that OSS-Fuzz:
https://issues.oss-fuzz.com/issues/42516062

Oliver Chang (1):
Fixed heap-buffer-overflow in compute_seq_size.

 src/sdp-xml.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
Signature · 1 line
2.50.1.703.g449372360f-goog
#1 lore raw

PATCH 1/1 Fixed heap-buffer-overflow in `compute_seq_size`.

By adding checks for sequence/alternate types in element_end and ensuring proper cleanup in sdp_xml_parse_record error path.

https://issues.oss-fuzz.com/issues/42516062
https://oss-fuzz.com/testcase-detail/5896441415729152

---
 src/sdp-xml.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
src/sdp-xml.c +19 -0
index 5efa62ab8..b10882db2 100644
@@ -545,6 +545,13 @@ static void element_end(GMarkupParseContext *context,
 	}
 
 	if (!strcmp(element_name, "sequence")) {
+		if (!SDP_IS_SEQ(ctx_data->stack_head->data->dtd)) {
+			g_set_error(err, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
+					"Unexpected data type %d for sequence",
+					ctx_data->stack_head->data->dtd);
+			return;
+		}
+
 		ctx_data->stack_head->data->unitSize = compute_seq_size(ctx_data->stack_head->data);
 
 		if (ctx_data->stack_head->data->unitSize > USHRT_MAX) {
@@ -557,6 +564,13 @@ static void element_end(GMarkupParseContext *context,
 			ctx_data->stack_head->data->unitSize += sizeof(uint8_t);
 		}
 	} else if (!strcmp(element_name, "alternate")) {
+		if (!SDP_IS_ALT(ctx_data->stack_head->data->dtd)) {
+			g_set_error(err, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
+					"Unexpected data type %d for alternate",
+					ctx_data->stack_head->data->dtd);
+			return;
+		}
+
 		ctx_data->stack_head->data->unitSize = compute_seq_size(ctx_data->stack_head->data);
 
 		if (ctx_data->stack_head->data->unitSize > USHRT_MAX) {
@@ -621,6 +635,11 @@ sdp_record_t *sdp_xml_parse_record(const char *data, int size)
 	if (g_markup_parse_context_parse(ctx, data, size, NULL) == FALSE) {
 		error("XML parsing error");
 		g_markup_parse_context_free(ctx);
+		while (ctx_data->stack_head) {
+			struct sdp_xml_data *elem = ctx_data->stack_head;
+			ctx_data->stack_head = elem->next;
+			sdp_xml_data_free(elem);
+		}
 		sdp_record_free(record);
 		free(ctx_data);
 		return NULL;
Signature · 1 line
2.50.1.703.g449372360f-goog
#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/project/bluetooth/list/?series=989263

---Test result---

Test Summary:

CheckPatch                    PENDING   0.31 seconds
GitLint                       PENDING   0.27 seconds
BuildEll                      PASS      20.16 seconds
BluezMake                     PASS      2672.13 seconds
MakeCheck                     PASS      20.50 seconds
MakeDistcheck                 PASS      189.00 seconds
CheckValgrind                 PASS      235.94 seconds
CheckSmatch                   PASS      307.17 seconds
bluezmakeextell               PASS      128.65 seconds
IncrementalBuild              PENDING   0.34 seconds
ScanBuild                     PASS      906.31 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:

---
Regards,
Linux Bluetooth

#3 lore raw

Re: [PATCH BlueZ 1/1] Fixed heap-buffer-overflow in `compute_seq_size`.

In reply to Oliver Chang
Quoted 5 lines · Oliver Chang

By adding checks for sequence/alternate types in element_end and ensuring proper cleanup in sdp_xml_parse_record error path.

https://issues.oss-fuzz.com/issues/42516062
https://oss-fuzz.com/testcase-detail/5896441415729152

Neither of those URLs show any details about the problem that was detected.

It would be useful if there was a regression test in unit/ either in unit/sdp.c or as a separate sdp-xml.c test that, for example, showed the lost memory in a valgrind run, which the patch would fix.

Cheers

Quoted 64 lines

---
 src/sdp-xml.c | 19 +++++++++++++++++++

 1 file changed, 19 insertions(+)
src/sdp-xml.c +0 -0
index 5efa62ab8..b10882db2 100644
@@ -545,6 +545,13 @@ static void element_end(GMarkupParseContext
 *context,
  	}


  if (!strcmp(element_name, "sequence")) {

src/sdp-xml.c +2 -0
+		if (!SDP_IS_SEQ(ctx_data->stack_head->data->dtd)) {
+			g_set_error(err, G_MARKUP_ERROR,

G_MARKUP_ERROR_INVALID_CONTENT,

src/sdp-xml.c +1 -0
+					"Unexpected data type %d for

sequence",

src/sdp-xml.c +1 -0
+					ctx_data->stack_head->data-
Quoted 1 line

dtd);

src/sdp-xml.c +3 -0
+			return;
+		}
+

  ctx_data->stack_head->data->unitSize = compute_seq_size(ctx_data->stack_head->data);

  if (ctx_data->stack_head->data->unitSize > USHRT_MAX) {

src/sdp-xml.c +0 -0
@@ -557,6 +564,13 @@ static void element_end(GMarkupParseContext
 *context,

  ctx_data->stack_head->data->unitSize += sizeof(uint8_t);
  }
  } else if (!strcmp(element_name, "alternate")) {

src/sdp-xml.c +2 -0
+		if (!SDP_IS_ALT(ctx_data->stack_head->data->dtd)) {
+			g_set_error(err, G_MARKUP_ERROR,

G_MARKUP_ERROR_INVALID_CONTENT,

src/sdp-xml.c +1 -0
+					"Unexpected data type %d for

alternate",

src/sdp-xml.c +1 -0
+					ctx_data->stack_head->data-
Quoted 1 line

dtd);

src/sdp-xml.c +3 -0
+			return;
+		}
+

  ctx_data->stack_head->data->unitSize = compute_seq_size(ctx_data->stack_head->data);

  if (ctx_data->stack_head->data->unitSize > USHRT_MAX) {

src/sdp-xml.c +0 -0
@@ -621,6 +635,11 @@ sdp_record_t *sdp_xml_parse_record(const char
 *data, int size)

  if (g_markup_parse_context_parse(ctx, data, size, NULL) == FALSE) {
  error("XML parsing error"); g_markup_parse_context_free(ctx);

src/sdp-xml.c +2 -0
+		while (ctx_data->stack_head) {
+			struct sdp_xml_data *elem = ctx_data-
Quoted 1 line

stack_head;

src/sdp-xml.c +3 -0
+			ctx_data->stack_head = elem->next;
+			sdp_xml_data_free(elem);
+		}

  sdp_record_free(record);
  free(ctx_data);
  return NULL;

A message that is not in the archive (sent off-list or not yet received): CAFqAZOL4iKjuxa=ubwFCGHyfgtuGBCW7F1US=1sYSoeWiMZpTg@mail.gmail.com

#5 lore raw

Re: [PATCH BlueZ 1/1] Fixed heap-buffer-overflow in `compute_seq_size`.

(Apologies for the noise, I'm new to this. One more attempt to resend this as text-only for those who have seen this email multiple times).

Thank you for the feedback. The problem here is that there is a heap buffer overflow found by fuzzing with the following testcase:

<sequence><foo/><text/></sequence>

This causes the compute_seq_size(ctx_data->stack_head->data); to be called on ctx_data->stack_head->data that isn't a sequence type. This patch adds some type checks to guard against that.

I don't believe a regression test using valgrind would catch this -- we used AddressSanitizer to detect this.

While fixing this, we also discovered a memory leak in the error handling path touched by the patch ( if (g_markup_parse_context_parse(ctx, data, size, NULL) == FALSE) ), which we included a fix for.
Would it be better if we separated out the heap buffer overflow fix and the memory leak fix into 2 separate commits?

Best,
Oliver

#6 lore raw

Re: [PATCH BlueZ 1/1] Fixed heap-buffer-overflow in `compute_seq_size`.

Quoted 6 lines · Oliver Chang

(Apologies for the noise, I'm new to this. One more attempt to resend this as text-only for those who have seen this email multiple times).

Thank you for the feedback. The problem here is that there is a heap buffer overflow found by fuzzing with the following testcase:

Quoted 8 lines

<sequence><foo/><text/></sequence>

This causes the compute_seq_size(ctx_data->stack_head->data); to be called on ctx_data->stack_head->data that isn't a sequence type. This patch adds some type checks to guard against that.

I don't believe a regression test using valgrind would catch this -- we used AddressSanitizer to detect this.

I meant that there should be:
- a test for SDP XML
- valgrind run of that shows this specific memory being leaked (don't need ASAN for that...)
- patch that fixes the leak with the section of the valgrind log

We don't need the memory leak itself to be regression tested, don't think it's something that's easy to put in place right now.

Quoted 6 lines

While fixing this, we also discovered a memory leak in the error handling path touched by the patch ( if (g_markup_parse_context_parse(ctx, data, size, NULL) == FALSE) ), which we included a fix for.
Would it be better if we separated out the heap buffer overflow fix and the memory leak fix into 2 separate commits?

Separate commits would be useful.