Skip to content

Commit 7c8636f

Browse files
committed
Fixed manifest header boundary checks
Added sanity check against address-space wrap-around Revert "Added sanity check against address-space wrap-around" This reverts commit cf81b32.
1 parent d897a8b commit 7c8636f

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/libwolfboot.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,15 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr)
342342
{
343343
uint8_t *p = haystack;
344344
uint16_t len;
345-
while (((p[0] != 0) || (p[1] != 0)) && ((p - haystack) < IMAGE_HEADER_SIZE)) {
345+
const uint8_t *max_p = (haystack - IMAGE_HEADER_OFFSET) + IMAGE_HEADER_SIZE;
346+
347+
while ((p + 4) < max_p) {
348+
if ((p[0] == 0) && (p[1] == 0)) {
349+
/* Explicit end of options reached */
350+
break;
351+
}
346352
if (*p == HDR_PADDING) {
353+
/* Padding byte (skip one position) */
347354
p++;
348355
continue;
349356
}
@@ -353,6 +360,10 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr)
353360
continue;
354361
}
355362
len = p[2] | (p[3] << 8);
363+
if (p + 4 + len > max_p) {
364+
/* This field is too large and would overflow the image header */
365+
break;
366+
}
356367
if ((p[0] | (p[1] << 8)) == type) {
357368
*ptr = (p + 4);
358369
return len;

0 commit comments

Comments
 (0)