Skip to content
Memory corruption Disclosed report Advanced

Latent allocator mismatch and heap corruption in FileReader

FileReader allocates its data buffer with one allocator but frees it with another, so error paths that free the buffer mix the two allocators. If a custom allocator were enabled, this would corrupt both heaps; in the default build the two allocators appear to share the same heap.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 5 months
  • Paid in 2 months
  • Disclosed after 5 months
Text
FileReader::ReadFileContent() (dom\file\FileReader.h/.cpp) sometimes allocates memory using js_pod_malloc(), but it (and other FileReader functions) can free the same memory using free().

Without a custom allocator (without |JS_USE_CUSTOM_ALLOCATOR| defined), these functions actually appear to use the same heap, according to tracing the debug build with the debugger.

If, however, |JS_USE_CUSTOM_ALLOCATOR| were to be defined, the heaps would be different, and this usage would corrupt both heaps.

The bug is that js_pod_malloc() FileReader::ReadFileContent() line 470:

388: void
389: FileReader::ReadFileContent(Blob& aBlob,
390:                             const nsAString &aCharset,
391:                             eDataFormat aDataFormat,
392:                             ErrorResult& aRv)
393: {
...
468:   if (mDataFormat != FILE_AS_BINARY) {
469:     if (mDataFormat == FILE_AS_ARRAYBUFFER) {
470:       mFileData = js_pod_malloc<char>(mTotal);
471:     } else {
472:       mFileData = (char *) malloc(mTotal);
473:     }
474: 
475:     if (!mFileData) {
476:       NS_WARNING("Preallocation failed for ReadFileData");
477:       aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
478:       return;
479:     }
480:   }
481: 
482:   aRv = DoAsyncWait();
483:   if (NS_WARN_IF(aRv.Failed())) {
484:     FreeFileData();
485:     return;
486:   }
487: 
488:   //FileReader should be in loading state here
489:   mReadyState = LOADING;
490:   DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
491: }

mismatches FreeFileData() on line 165 .h file:

163: void FreeFileData()
164:   {
165:     free(mFileData);
166:     mFileData = nullptr;
167:     mDataLen = 0;
168:   }

Thus, if, e.g., line 482 returns failure, causing line 484 to call FreeFileData(), js_pod_malloc() will be matched with free(). This can also occur if there is, e.g., a file read error that gets reported to OnLoadEnd():

731: nsresult
732: FileReader::OnLoadEnd(nsresult aStatus)
733: {
734:   // Cancel the progress event timer
735:   ClearProgressEventTimer();
736: 
737:   // FileReader must be in DONE stage after an operation
738:   mReadyState = DONE;
739: 
740:   // Quick return, if failed.
741:   if (NS_FAILED(aStatus)) {
742:     FreeDataAndDispatchError(aStatus);
743:     return NS_OK;
744:   }
...
782: }

In that case, FreeDataAndDispatchError() line 608 calls FreeFileData(), again causing the allocator mismatch:

603: void
604: FileReader::FreeDataAndDispatchError()
605: {
606:   MOZ_ASSERT(mError);
607: 
608:   FreeFileData();
609:   mResult.SetIsVoid(true);
610:   mAsyncStream = nullptr;
611:   mBlob = nullptr;
612: 
613:   // Dispatch error event to signify load failure
614:   DispatchProgressEvent(NS_LITERAL_STRING(ERROR_STR));
615:   DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
616: }

The original report as published on Mozilla's Bugzilla, by mozillabugs. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the buffer is allocated with js_pod_malloc() on one branch but always released with free(), so the allocator used to allocate and the one used to free can differ.

Impact

If a custom allocator were enabled, the mismatched free would corrupt both heaps; in the default build the two allocators appear to share one heap, so no corruption was observed.

What the attacker needed

Custom allocator enabled (not default)

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A program asks the system for memory when it needs space, and later gives that memory back. There are different 'memory managers' that each keep their own records, and you must return memory to the same one you got it from. Here the code takes memory from one manager but hands it back to another, which would confuse both if they were separate.

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.

More by mozillabugs

View all

41 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.