Latent (?) read and write beyond bounds in nsTArray_Impl::AppendElements()
An integer overflow in nsTArray_Impl::AppendElements() can cause underallocation and out-of-bounds read/write. The author found the bug latent in Firefox 60.0.2 but identified a potential path via nsDataObj::CStream::OnDataAvailable() when dragging a file from Firefox to Explorer, though no working exploit was demonstrated.
Originally disclosed on Mozilla
- Fixed in 54 days
- Paid in 2 months
- Disclosed after 54 days
Created attachment 9001419
bug_1222_str_1.htm
nsTArray_Impl::AppendElements(size_type aCount) (xpcom\ds\nsTArray.h) can experience an integer overflow. If it does, it underallocates a buffer, constructs elements beyond bounds, and returns a pointer that callers can use to read and write beyond bounds. The bug is that line 1687 doesn't check for overflow on the addition: [1]
1684: template<typename ActualAlloc = Alloc>
1685: elem_type* AppendElements(size_type aCount) {
1686: if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
1687: Length() + aCount, sizeof(elem_type)))) {
1688: return nullptr;
1689: }
1690: elem_type* elems = Elements() + Length();
1691: size_type i;
1692: for (i = 0; i < aCount; ++i) {
1693: elem_traits::Construct(elems + i);
1694: }
1695: this->IncrementLength(aCount);
1696: return elems;
Since this overload of AppendElements() takes only a count (and not also an array), it can be called with an anomalously large count without the caller having allocated space for the data to be appended.
Having searched the callers, I find that the bug appears to be latent as of FF 60.0.2. Probably the closest thing to an exploitable attack is in nsDataObj::CStream::OnDataAvailable() (widget\windows\nsDataObj.cpp), which contains this code:
114: NS_IMETHODIMP
115: nsDataObj::CStream::OnDataAvailable(nsIRequest *aRequest,
116: nsISupports *aContext,
117: nsIInputStream *aInputStream,
118: uint64_t aOffset, // offset within the stream
119: uint32_t aCount) // bytes available on this call
120: {
121: // Extend the write buffer for the incoming data.
122: uint8_t* buffer = mChannelData.AppendElements(aCount, fallible);
123: if (!buffer) {
124: return NS_ERROR_OUT_OF_MEMORY;
125: }
126: NS_ASSERTION((mChannelData.Length() == (aOffset + aCount)),
127: "stream length mismatch w/write buffer");
128:
129: // Read() may not return aCount on a single call, so loop until we've
130: // accumulated all the data OnDataAvailable has promised.
131: nsresult rv;
132: uint32_t odaBytesReadTotal = 0;
133: do {
134: uint32_t bytesReadByCall = 0;
135: rv = aInputStream->Read((char*)(buffer + odaBytesReadTotal),
136: aCount, &bytesReadByCall
137: odaBytesReadTotal += bytesReadByCall;
138: } while (aCount < odaBytesReadTotal && NS_SUCCEEDED(rv));
139: return rv;
140: }
|aCount| in that function appears sometimes to come from other code that has not yet allocated space for the data. This appears to occur when reading a file via the file:// protocol (though not via http://). If, for example, you drag an image (even a broken one that FF refuses to display) from FF into Explorer, OnDataAvailable() is called repeatedly to read the data that's become available. If you set |network.buffer.cache.count| large enough, the chunks read can be very large, over 0x80000000 bytes. If the correct order of chunks occurs, line 122 will cause the overflow.
I have attached an STR that illustrates this issue, though I have not yet gotten a sequence of chunks that causes the overflow.
It would be easy unintentionally to write exploitable code by, say, reading 2 or more lengths out of a media file, creating an nsTArray to hold the corresponding data, and repeatedly calling AppendElements() to allocate storage for them, e.g.:
nsTArray <char> media;
uint32_t len1 = mediafile.Read32 (); // assume |len1| is 0x1000
char *pPart1 = media.AppendElements (len1);
uint32_t len2 = mediafile.Read32 (); // assume |len2| is 0xFFFFF000
// Oops, overflows to 0 and returns underallocated pointer in 32-bit builds!
char *pPart2 = media.AppendElements (len2);
To use the STR, first set |network.buffer.cache.count| to 262144. Now put the STR .htm file into a local folder, expand the .7z file into the same folder, and load the .htm file in FF using the local folder path.
Set a BP on nsDataObj::CStream::OnDataAvailable() and drag the broken image icon into Explorer. The BP should trigger multiple times, usually with different (sometimes very large) |aCount| values. If you are lucky, and are using the Win32 FF build, it might cause the overflow and crash FF. You might have to try this test several times to see large values, depending upon how the STR data file is allocated on your disk and how busy your system is.
[1] A similar bug also exists on all call-paths into
nsTArray_base::InsertSlotsAt() (xpcom\ds\nsTArray-inl.h), which has the same issue:
342: size_type newLen = Length() + aCount;
343:
344: EnsureCapacity<ActualAlloc>(newLen, aElemSize);
InsertElementsAt (index_type, size_type), for example, calls InsertSlotsAt().
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 addition Length() + aCount in AppendElements() can overflow, leading to an undersized allocation and subsequent out-of-bounds writes.
Impact
If the overflow is triggered, it could lead to out-of-bounds read and write, potentially causing a crash or arbitrary code execution, but the author has not achieved a reliable exploit.
What the attacker needed
Windows OS; Firefox 60.0.2 (Win32 build); network.buffer.cache.count set to 262144; local .htm file and data file
How it was found
- Set network.buffer.cache.count to 262144.
- Place the provided .htm file and extracted .7z data in a local folder.
- Load the .htm file in Firefox using the local folder path.
- Set a breakpoint on nsDataObj::CStream::OnDataAvailable().
- Drag the broken image icon into Explorer.
- Observe the breakpoint triggering with varying aCount values; if lucky, the overflow occurs and Firefox crashes.
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.
This report describes a bug in a Firefox internal data structure that can cause memory corruption. The author found a possible way to trigger it by dragging a file from Firefox to Windows Explorer, but could not get it to crash reliably. The bug is considered latent because no easy exploit was found.
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.