Logic errors cause potential use of uninitialized data in nsDataObj::CStream::OnDataAvailable()
A Windows drag-and-drop data stream handler has a reversed loop condition that makes it exit early, so a short read leaves part of the buffer uninitialised. If that condition were corrected, a second logic error would let the loop request too many bytes and write past the end of the buffer.
Originally disclosed on Mozilla
- Fixed in 34 days
- Paid in 40 days
- Disclosed after 34 days
`nsDataObj::CStream::OnDataAvailable()` (`widget/windows/nsDataObj.cpp`) has a logic error that could cause it to return uninitialized data, and another logic error that, if the first error were corrected, could cause it to write beyond bounds.
'''
122: NS_IMETHODIMP
123: nsDataObj::CStream::OnDataAvailable(
124: nsIRequest* aRequest, nsIInputStream* aInputStream,
125: uint64_t aOffset, // offset within the stream
126: uint32_t aCount) // bytes available on this call
127: {
128: // Extend the write buffer for the incoming data.
129: uint8_t* buffer = mChannelData.AppendElements(aCount, fallible);
130: if (!buffer) {
131: return NS_ERROR_OUT_OF_MEMORY;
132: }
133: NS_ASSERTION((mChannelData.Length() == (aOffset + aCount)),
134: "stream length mismatch w/write buffer");
135:
136: // Read() may not return aCount on a single call, so loop until we've
137: // accumulated all the data OnDataAvailable has promised.
138: nsresult rv;
139: uint32_t odaBytesReadTotal = 0;
140: do {
141: uint32_t bytesReadByCall = 0;
142: rv = aInputStream->Read((char*)(buffer + odaBytesReadTotal), aCount,
143: &bytesReadByCall);
144: odaBytesReadTotal += bytesReadByCall;
145: } while (aCount < odaBytesReadTotal && NS_SUCCEEDED(rv));
146: return rv;
147: }
'''
The first bug is in line 145, where the condition `aCount < odaBytesReadTotal` always evaluates to `false`. If `aInputStream->Read()` returns fewer than `aCount` bytes, this bug causes the loop to exit early, leaving some elements of `buffer` uninitialized. Presumably the function should say:
'''
139.1 uint32_t initialCount = aCount;
'''
and the condition on line 145 should be `odaBytesReadTotal < initialCount`.
The second bug is that the loop doesn't update `aCount` to account for the number of bytes read so far. If the first bug were fixed to allow the loop to run more than once, the second and succeeding iterations would request (unadjusted) `aCount` bytes from `aInputStream->Read()`. If that function then returned more than `aCount - odaBytesReadTotal` bytes, it would overrun `buffer`.
I am working on a POC.
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 a reversed comparison in the loop condition, combined with a read size that is never reduced by the bytes already read.
Impact
The function could return uninitialised memory contents, and if the first flaw were fixed alone, a subsequent read could overrun the buffer; the text does not demonstrate either outcome.
What the attacker needed
Windows build of the browser; drag-and-drop data transfer path
The fix
The condition should compare the running total against the original count, and each read should request only the remaining bytes.
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.
When a program reads data in chunks, it usually loops until it has everything it asked for. Here the check that decides whether to keep looping is written the wrong way round, so the loop stops after the first chunk even if less data arrived than expected, and the leftover space holds whatever was in memory before. The author also notes that fixing only that check would expose a second mistake where the loop asks for too many bytes and could write past the buffer.
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.