Overflow causes out-of-bounds read attempts in nsZipArchive::BuildFileList
- Fixed in 7 months
- Paid in 46 days
- Disclosed after 7 months
nsZipArchive::BuildFileList (modules\libjar\nsZipArchive.cpp) can incur an integer overflow, which then causes the function to attempt to read memory to which it has no right. The addresses involved are in the range 0xfffffffc to somewhere in the lower part of the first page of memory (wraparound!) on 32 bit builds. The bug can't affect 64 bit builds because invoking it would require the use of non-canonical addresses, which AFAIK causes a fault on all x64 processors.
Because the affected addresses are likely always to be inaccessible, this bug should have little practical effect, but it should be fixed.
Details
-------
The bug is in lines 655-56:
613: nsresult nsZipArchive::BuildFileList(PRFileDesc *aFd)
614: {
615: // Get archive size using end pos
616: const uint8_t* buf;
617: const uint8_t* startp = mFd->mFileData;
618: const uint8_t* endp = startp + mFd->mLen;
619: MOZ_WIN_MEM_TRY_BEGIN
620: uint32_t centralOffset = 4;
.., [extract |centralOffset| from archive]
643:
644: if (!centralOffset)
645: return NS_ERROR_FILE_CORRUPTED;
646:
If |centralOffset| is such that |buf| becomes, e.g., 0xfffffffc on a 32-bit architecture:
647: buf = startp + centralOffset;
648:
control skips from line 650 to line 654:
649: // avoid overflow of startp + centralOffset.
650: if (buf < startp)
651: return NS_ERROR_FILE_CORRUPTED;
652:
653: //-- Read the central directory headers
and the "buf +..." in line 655 computes the value 0xfffffffc + 4 == 0, which is <= |endp| (BTW, what's with the cast?):
654: uint32_t sig = 0;
655: while (buf + int32_t(sizeof(uint32_t)) <= endp &&
so line 656 executes, attempting to read the bytes at addresses 0xffffffffc-0xffffffff. If those bytes happen to be valid and to contain |CENTRALSIG| [1]:
656: (sig = xtolong(buf)) == CENTRALSIG) {
then control goes to line 658. The expression |endp - 0xfffffffc| is >= |ZIPCENTRAL_SIZE| for any practical |buf|:
657: // Make sure there is enough data available.
658: if (endp - buf < ZIPCENTRAL_SIZE)
659: return NS_ERROR_FILE_CORRUPTED;
660:
So control then goes to line 662 et seq, which attempt to access fields of ZipCentral whose addresses wrap around to the beginning of the address space:
661: // Read the fixed-size data.
662: ZipCentral* central = (ZipCentral*)buf;
663:
664: uint16_t namelen = xtoint(central->filename_len);
665: uint16_t extralen = xtoint(central->extrafield_len);
666: uint16_t commentlen = xtoint(central->commentfield_len);
667: uint32_t diff = ZIPCENTRAL_SIZE + namelen + extralen + commentlen;
On most architectures one or more of these reads incurs an access violation, which then triggers an exception, which is handled by line 713:
713: MOZ_WIN_MEM_TRY_CATCH(return NS_ERROR_FAILURE)
A largely-similar sequence of events unfolds if the bytes tested by line 656 contain |ENDSIG|.
[1] This does not happen on WOW64 on Win7 SP1, which appears to reserve the last 128k of the 32 bit address space. It might occur on more-recent versions of Windows, or on Unixes, but I haven't tested it. Make sure to use the /LARGEADDRESSAWARE linker flag (as FF does) when testing this issue.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A low-severity memory corruption bug in Networking: JAR. Mozilla made it public on 25 May 2016.
About Memory corruption
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.