From 46b7fb01bf41770acfb860682069df4b95990cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Trojnara?= Date: Sat, 14 Nov 2026 23:19:20 +0100 Subject: [PATCH] Prevent excessive CPU use on corrupt MSI sector chains get_next_sector() rejected only values at or above FREESECT, so ENDOFCHAIN was returned as if it were a sector number and locate_final_sector() kept walking past the end of a chain instead of stopping at it. The tiny resulting FAT sector number, 0xFFFFFFFE divided by the entries per sector, then made get_fat_sector_location() subtract entriesPerSector once per loop iteration for millions of iterations, each resolving another sector. On a 98 KiB file this costs roughly 0.4 seconds in a release build and 1.8 seconds with sanitizers. Malformed MSI input therefore causes unnecessary CPU work during signing and verification. Treat ENDOFCHAIN and the other reserved markers as chain terminators, and reject FAT sector numbers that the header does not account for before the DIFAT walk, which bounds the loop by a validated value rather than by a 32-bit input. read_mini_stream() also looked up a next mini sector after copying the last bytes of a stream, unlike read_stream(); skip that lookup so the stricter terminator check cannot reject valid streams. --- NEWS.md | 1 + msi.c | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9711a5c..96b5782 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ cannot be loaded - fixed allocation-failure crashes when decoding Base64 script signatures and timestamp responses, or creating signature content +- fixed excessive CPU use when parsing malformed MSI sector chains ### 2.16 (2026.11.13) diff --git a/msi.c b/msi.c index f2bed0e..0b1515b 100644 --- a/msi.c +++ b/msi.c @@ -822,6 +822,13 @@ static uint32_t get_fat_sector_location(MSI_FILE *msi, uint32_t fatSectorNumber) uint32_t entriesPerSector, difatSectorLocation, fatSectorLocation; const u_char *address; + /* Reject numbers the header does not account for before walking the DIFAT + * chain: a corrupt sector reference can otherwise be as large as 0xFFFFFFFF, + * which made the subtraction loop below iterate millions of times. */ + if (fatSectorNumber >= msi->m_hdr->numFATSector) { + fprintf(stderr, "Invalid FAT sector number 0x%08X\n", fatSectorNumber); + return NOSTREAM; /* FAILED */ + } if (fatSectorNumber < DIFAT_IN_HEADER) { return LE_UINT32(msi->m_hdr->headerDIFAT[fatSectorNumber]); } else { @@ -869,7 +876,9 @@ static uint32_t get_next_sector(MSI_FILE *msi, uint32_t sector) return NOSTREAM; /* FAILED */ } nextSectorLocation = GET_UINT32_LE(address); - if (nextSectorLocation == 0 || nextSectorLocation >= FREESECT) { + /* ENDOFCHAIN terminates a chain; reaching it means the chain is shorter than + * the requested offset, so it must not be treated as a sector number. */ + if (nextSectorLocation == 0 || nextSectorLocation >= ENDOFCHAIN) { fprintf(stderr, "Get corrupted sector location 0x%08X\n", nextSectorLocation); return NOSTREAM; /* FAILED */ } @@ -961,7 +970,7 @@ static uint32_t get_next_mini_sector(MSI_FILE *msi, uint32_t miniSector) return NOSTREAM; /* FAILED */ } nextMiniSectorLocation = GET_UINT32_LE(address); - if (nextMiniSectorLocation == 0 || nextMiniSectorLocation >= FREESECT) { + if (nextMiniSectorLocation == 0 || nextMiniSectorLocation >= ENDOFCHAIN) { fprintf(stderr, "Get corrupted sector location 0x%08X\n", nextMiniSectorLocation); return NOSTREAM; /* FAILED */ } @@ -1003,6 +1012,8 @@ static int read_mini_stream(MSI_FILE *msi, uint32_t sector, uint32_t offset, cha memcpy(buffer, address, copylen); buffer += copylen; len -= copylen; + if (len == 0) + break; /* the last mini sector of the stream has been read */ sector = get_next_mini_sector(msi, sector); if (sector == NOSTREAM) { fprintf(stderr, "Failed to get a next mini sector\n");