Skip to content

Commit 2c2fb72

Browse files
committed
multiboot.c: check bounds in debug function dump_tags
1 parent 7807f72 commit 2c2fb72

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/multiboot.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,20 @@ static void mb2_parse_info_request_tag(void* tag) {
315315
}
316316
}
317317

318-
static void mb2_dump_tags(void* mbTags) {
318+
static void mb2_dump_tags(void* mbTags, uint32_t tags_len) {
319319
struct mb2_tag* tag = (struct mb2_tag*)mbTags;
320+
uint8_t *end = (uint8_t*)mbTags + tags_len;
320321

321-
while (tag->type != 0) {
322+
while ((uint8_t*)tag + sizeof(*tag) <= end && tag->type != 0) {
322323
MB2_DEBUG_PRINTF("Tag Type: %u\r\n", tag->type);
323324
MB2_DEBUG_PRINTF("Tag Flags: 0x%x\r\n", tag->flags);
324325
MB2_DEBUG_PRINTF("Tag Size: %u\r\n", tag->size);
325326

326327
if (tag->type == MB2_TAG_TYPE_INFO_REQ)
327328
mb2_parse_info_request_tag(tag);
328329

330+
if (tag->size < sizeof(*tag))
331+
break;
329332
tag = (struct mb2_tag*)mb2_align_address_up((uint8_t*)tag + tag->size,
330333
8);
331334
}
@@ -341,7 +344,9 @@ static void mb2_dump_header(void* mbHeader) {
341344
MB2_DEBUG_PRINTF("Checksum: 0x%x\r\n", header->checksum);
342345

343346
tags = (uint8_t*)header + sizeof(*header);
344-
mb2_dump_tags(tags);
347+
if (header->header_length < sizeof(struct mb2_header))
348+
MB2_DEBUG_PRINTF("Invalid header length\r\n");
349+
mb2_dump_tags(tags, header->header_length - sizeof(*header));
345350
}
346351
#endif /* DEBUG_MB2 */
347352

0 commit comments

Comments
 (0)