Latent read and write beyond bounds in FileReader::DoReadData()
FileReader::DoReadData() fails to initialise a variable that it passes to a stream read call. If the stream returns a closed error without setting that variable, the uninitialised value is used to copy data from a stack buffer, potentially causing out-of-bounds read or write.
Originally disclosed on Mozilla
- Fixed in 16 days
- Paid in 27 days
- Disclosed after 16 days
Created attachment 9327240
ffbug_2118.htm
`FileReader::DoReadData()` fails to initialize the variable `read` that it passes to `nsIAsyncInputStream::Read()` . If `nsIAsyncInputStream::Read()` were to return `NS_BASE_STREAM_CLOSED` without initializing `read`, `FileReader::DoReadData()` would use the resulting uninitialized stack variable to determine how much data to read from a stack buffer into an output buffer (line 326, below) and to update, among other things, a pointer to an output buffer (line 328, below), potentially causing a read and/or write beyond bounds. [1]
The bug appears to be latent. Using `FileReader::readAsBinaryString()` on `File` and `Blob` objects causes FF to call this function, but the relevant stream is always buffered (and thus handled by lines 303-06), which prevents control from reaching the buggy code. [2] I do not know whether there is another way to reach the buggy code.
The bug is on line 312, which doesn't initialize `read`:
'''
283: nsresult FileReader::DoReadData(uint64_t aCount) {
284: MOZ_ASSERT(mAsyncStream);
285:
286: uint32_t bytesRead = 0;
287:
288: if (mDataFormat == FILE_AS_BINARY) {
289: // Continuously update our binary string as data comes in
290: CheckedInt<uint64_t> size{mResult.Length()};
291: size += aCount;
292:
293: if (!size.isValid() || size.value() > UINT32_MAX || size.value() > mTotal) {
294: return NS_ERROR_OUT_OF_MEMORY;
295: }
296:
297: uint32_t lenBeforeRead = mResult.Length();
298: MOZ_ASSERT(lenBeforeRead == mDataLen, "unexpected mResult length");
299:
300: mResult.SetLength(lenBeforeRead + aCount);
301: char16_t* currentPos = mResult.BeginWriting() + lenBeforeRead;
302:
303: if (NS_InputStreamIsBuffered(mAsyncStream)) {
304: nsresult rv = mAsyncStream->ReadSegments(ReadFuncBinaryString, currentPos,
305: aCount, &bytesRead);
306: NS_ENSURE_SUCCESS(rv, NS_OK);
307: } else {
308: while (aCount > 0) {
309: char tmpBuffer[4096];
310: uint32_t minCount =
311: XPCOM_MIN(aCount, static_cast<uint64_t>(sizeof(tmpBuffer)));
312: uint32_t read;
313:
314: nsresult rv = mAsyncStream->Read(tmpBuffer, minCount, &read);
315: if (rv == NS_BASE_STREAM_CLOSED) {
316: rv = NS_OK;
317: }
318:
319: NS_ENSURE_SUCCESS(rv, NS_OK);
320:
321: if (read == 0) {
322: // The stream finished too early.
323: return NS_ERROR_OUT_OF_MEMORY;
324: }
325:
326: PopulateBufferForBinaryString(currentPos, tmpBuffer, read);
327:
328: currentPos += read;
329: aCount -= read;
330: bytesRead += read;
331: }
332: }
...
360: }
'''
[1] Some implementations of `Read()` can return `NS_BASE_STREAM_CLOSED` without initializing `read` (e.g., `ThrottleInputStream::Read()` (`netwerk/base/ThrottleQueue.cpp`) ) , despite the `nsIInputStream` contract prohibiting this return code (see `nsIInputStream.idl`)
[2] Attached is a page that causes FF to call `FileReader::DoReadData()` . Load it in FF and click "Browse" to select a text file, which causes FF to call it with a `File` object or click "click!" to cause FF to call it with a `Blob` object.
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
The local variable read is not initialised before being passed to nsIAsyncInputStream::Read(), and the code does not handle the case where Read() returns NS_BASE_STREAM_CLOSED without setting it.
Impact
The bug is latent because the stream used by readAsBinaryString() is always buffered, so the vulnerable code is not reached; if another way to reach it exists, it could cause a read and/or write beyond bounds.
What the attacker needed
Firefox browser; a way to make the stream unbuffered
How it was found
- Load the attached page in Firefox.
- Click 'Browse' to select a text file, causing FileReader::DoReadData() to be called with a File object.
- Alternatively, click 'click!' to cause FileReader::DoReadData() to be called with a Blob object.
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 a stream for data and expects the stream to fill in a number saying how many bytes it read. In one code path that number is never set before the call. If the stream returns an error without setting it, the program uses whatever leftover value is on the stack, which can make it copy too much data and read or write outside the intended memory.
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.