Integer overflow leading to a buffer overflow in nsScriptLoadHandler
An integer overflow in Firefox's script loading code allowed a malicious server to cause a buffer overflow by sending more than 4GB of script data. This could lead to memory corruption and potentially code execution, though the written data is not fully controlled.
Originally disclosed on Mozilla
- Fixed in 22 days
- Paid in 26 days
- Disclosed after 22 days
Created attachment 8787005
poc.zip
While loading external script data, the following code is executed in nsScriptLoadHandler (nsScriptLoader.cpp):
nsresult
nsScriptLoadHandler::TryDecodeRawData(const uint8_t* aData,
uint32_t aDataLength,
bool aEndOfStream)
{
int32_t srcLen = aDataLength;
const char* src = reinterpret_cast<const char *>(aData);
int32_t dstLen;
nsresult rv =
mDecoder->GetMaxLength(src, srcLen, &dstLen);
NS_ENSURE_SUCCESS(rv, rv);
uint32_t haveRead = mBuffer.length();
uint32_t capacity = haveRead + dstLen; // [[ 1 ]]
if (!mBuffer.reserve(capacity)) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = mDecoder->Convert(src,
&srcLen,
mBuffer.begin() + haveRead,
&dstLen);
NS_ENSURE_SUCCESS(rv, rv);
haveRead += dstLen;
MOZ_ASSERT(haveRead <= capacity, "mDecoder produced more data than expected");
MOZ_ALWAYS_TRUE(mBuffer.resizeUninitialized(haveRead));
return NS_OK;
}
The calculation of the new capacity at [[ 1 ]] can overflow if enough data is sent by the server (>4GB). In that case, the following call to mBuffer.reserve() won't change the size of mBuffer. Afterwards, mDecoder->Convert() will write data past the end of the 8GB buffer (mBuffer stores char16_t) [1].
poc.py (requires python 3.5) in the attached archive will trigger this condition and cause an access violation while writing data into an unmapped region following the buffer (tested on Firefox 48.0.1 and Aurora):
Process 97720 stopped
* thread #1: tid = 0x19afd5, 0x0000000101c66c68 XUL`nsOneByteDecoderSupport::Convert(char const*, int*, char16_t*, int*) [inlined] nsUnicodeDecodeHelper::ConvertByFastTable(aSrcLength=<unavailable>, aDestLength=<unavailable>) + 9 at nsUnicodeDecodeHelper.cpp:206, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x577000000)
frame #0: 0x0000000101c66c68 XUL`nsOneByteDecoderSupport::Convert(char const*, int*, char16_t*, int*) [inlined] nsUnicodeDecodeHelper::ConvertByFastTable(aSrcLength=<unavailable>, aDestLength=<unavailable>) + 9 at nsUnicodeDecodeHelper.cpp:206
203 }
204
205 for (; src<srcEnd;) {
-> 206 *dest = aFastTable[*src];
207 if (*dest == 0xfffd && aErrorSignal) {
208 res = NS_ERROR_ILLEGAL_INPUT;
209 break;
(lldb) bt
frame #0: 0x0000000101c66c68 XUL`nsOneByteDecoderSupport::Convert(char const*, int*, char16_t*, int*) [inlined] nsUnicodeDecodeHelper::ConvertByFastTable(aSrcLength=<unavailable>, aDestLength=<unavailable>) + 9 at nsUnicodeDecodeHelper.cpp:206
frame #1: 0x0000000101c66c5f XUL`nsOneByteDecoderSupport::Convert(this=0x0000000112a0e800, aSrc="", aSrcLength=0x00007fff5fbfcc6c, aDest=u"", aDestLength=0x00007fff5fbfcc68) + 207 at nsUCSupport.cpp:273
frame #2: 0x0000000102a5c3f2 XUL`nsScriptLoadHandler::TryDecodeRawData(this=0x00000001290eb740, aData="", aDataLength=<unavailable>, aEndOfStream=<unavailable>) + 146 at nsScriptLoader.cpp:2752
frame #3: 0x0000000102a5be73 XUL`nsScriptLoadHandler::OnIncrementalData(this=0x00000001290eb740, aLoader=<unavailable>, aContext=<unavailable>, aDataLength=101, aData="", aConsumedLength=0x00007fff5fbfccf4) + 83 at nsScriptLoader.cpp:2720
...
Some notes regarding exploitability:
- It is possible to use compression to reduce the network load. The attached PoC code does that and thus only needs to send about 18MB of data.
- It should be possible to place some objects behind the buffer, as mmap will usually allocate adjacent memory regions. This could be done as follows:
- The server sends the first 0xffffffff bytes of the payload, causing an allocation of an 8GB memory region
- JavaScript code executes and allocates lots of target objects. At some point new memory is requested via mmap, which should be placed behind the 8GB buffer
- The server sends the remaining data, overflowing into the previously allocated objects
- Due to page deduplication and/or compression beeing performed by the kernel, it should be possible to trigger the bug while having less than 8GB of available RAM. At least on my Mac this seems to be the case as memory usage stays relatively low even during the allocations.
- A 64-bit build of Firefox is required so the bug can be triggered.
- The data that is being written is not 100% controlled as it must be valid UTF-16
[1] There is a special case when the capacity wraps around to zero. In that case the process will (likely) not crash as the overflow only happens inside the 8GB chunk.
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
Impact
A malicious server could cause a buffer overflow, leading to memory corruption and potentially code execution, though the written data is not fully controlled and a 64-bit build is required.
What the attacker needed
Victim visits a malicious page; server sends >4GB of script data; 64-bit Firefox build
How it was found
- Set up a malicious server that sends a large amount of script data, possibly compressed.
- Cause the browser to load an external script from that server.
- The server sends more than 4GB of data, triggering the integer overflow.
- The decoder writes past the end of the allocated buffer, causing an access violation.
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 web page loads a script from a server, Firefox calculates how much memory to reserve. If the server sends a huge amount of data, that calculation can wrap around and reserve too little memory, so the data spills over into other memory. This is called a buffer overflow and can crash the browser or let an attacker run their own code.
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.