Closed Bug 1452253 Opened 8 years ago Closed 8 years ago

Latent allocator mismatch and heap corruption in FileReader

Categories

(Core :: DOM: File, enhancement)

58 Branch
enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla64
Tracking Status
firefox-esr60 --- wontfix
firefox62 --- wontfix
firefox63 --- wontfix
firefox64 --- fixed

People

(Reporter: mozillabugs, Assigned: baku)

Details

(Keywords: reporter-external, sec-low, Whiteboard: [post-critsmash-triage][adv-main64-])

Attachments

(1 file, 1 obsolete file)

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: }
"mismatches FreeFileData() on line 165 .h file:" => "mismatches FreeFileData() on line 165 in the .h file:"
Group: core-security → dom-core-security
Flags: sec-bounty?
Might happen in a distro build, but not a stock Firefox as used by 99.99% of users.
Flags: sec-bounty? → sec-bounty+
Keywords: sec-low
Attached patch file_free.patch (obsolete) — — Splinter Review
The issue here is about which free() function to use based on which malloc() function has been used. For FILE_AS_ARRAYBUFFER we use js_pod_malloc, and we should free memory using js_free.
Assignee: nobody → amarchesini
Attachment #9009658 - Flags: review?(bugs)
Comment on attachment 9009658 [details] [diff] [review] file_free.patch Empty patch
Attachment #9009658 - Flags: review?(bugs) → review-
Attached patch file_free.patch — — Splinter Review
Attachment #9009658 - Attachment is obsolete: true
Attachment #9009769 - Flags: review?(bugs)
Attachment #9009769 - Flags: review?(bugs) → review+
Group: dom-core-security → core-security-release
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla64
Flags: qe-verify-
Whiteboard: [post-critsmash-triage]
Whiteboard: [post-critsmash-triage] → [post-critsmash-triage][adv-main64-]
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: