Write beyond bounds in StringBuilder::ToString()
Originally disclosed on Mozilla
- Fixed in 26 days
- Paid in 31 days
- Disclosed after 26 days
Created attachment 9030049
bug_1299_poc_2.htm
StringBuilder::ToString() (dom\base\nsContentUtils.cpp) can cause an integer overflow, with subsequent underallocation of an output buffer and writing beyond bounds. The bug became patent in FF 64b14 (or possibly an earlier beta of 64.0) when the object |BulkAppender appender| was added in place of directly appending to the output string |nsAString &aOut|. [1]
The bug is that the various StringBuilder::Append() functions accumulate a length into |uint32_t mLength| without checking for overflow (e.g, line 9195 below). If the document being serialized is sufficiently large, |mLength| overflows. When ToString() eventually is called, it initializes |BulkAppender appender| with the overflowed length (line 9220, below). If the document contains attribute values (e.g., <p a="......">) totalling > 4 GB, BulkAppender::Append(Span<const char16_t>) (line 9076-77, below), etc., memcpy() beyond the end of the buffer at |mHandle.Elements()|. (The assert on line 9071 is not present in release builds).
9189: void [StringBuilder::]AppendWithAttrEncode(nsAutoString* aString, uint32_t aLen)
9190: {
9191: Unit* u = AddUnit();
9192: u->mString = aString;
9193: u->mType = Unit::eStringWithEncode;
9194: u->mLength = aLen;
9195: mLength += aLen;
9196: }
9217: bool [StringBuilder::]ToString(nsAString& aOut)
9218: {
9219: nsresult rv;
9220: BulkAppender appender(aOut.BulkWrite(mLength, 0, true, rv));
9221: if (NS_FAILED(rv)) {
9222: return false;
9223: }
9224:
9225: for (StringBuilder* current = this; current; current = current->mNext) {
9226: uint32_t len = current->mUnits.Length();
9227: for (uint32_t i = 0; i < len; ++i) {
9228: Unit& u = current->mUnits[i];
9229: switch (u.mType) {
9230: case Unit::eAtom:
9231: appender.Append(*(u.mAtom));
9232: break;
9233: case Unit::eString:
9234: appender.Append(*(u.mString));
9235: break;
9236: case Unit::eStringWithEncode:
9237: EncodeAttrString(*(u.mString), appender);
9238: break;
...
9264: }
9265: }
9266: }
9267: appender.Finish();
9268: return true;
9269: }
9068: void [BulkAppender::]Append(Span<const char16_t> aStr)
9069: {
9070: size_t len = aStr.Length();
9071: MOZ_ASSERT(mPosition + len <= mHandle.Length());
9072: // Both mHandle.Elements() and aStr.Elements() are guaranteed
9073: // to be non-null (by the string implementation and by Span,
9074: // respectively), so not checking the pointers for null before
9075: // memcpy does not lead to UB even if len was zero.
9076: memcpy(
9077: mHandle.Elements() + mPosition, aStr.Elements(), len * sizeof(char16_t));
9078: mPosition += len;
9079: }
Attached is a POC that demonstrates the overwrite. Using it by starting FF 64.0b14 x64 on a machine with >= 16GB of memory, putting the POC files on a webserver, and loading bug_1299_poc_2.htm. Now attach a debugger to the FF processes and wait for FF to crash, which will take ~10 minutes. The debugger will indicate an access violation writing beyond the end of |mHandle.Elements()| with the data from the JS setAttribute() statement on POC line 14 et seq.
------
[1] The bug was latent in older versions of FF where ToString() used nsAString::SetLength() with the overflowed value, then nsAString::Append() to add the string data, since nsAString::Append() checked the output buffer length and increased it if necessary before appending the data. BulkAppender::Append(Span<const char16_t>), etc., in contrast, assume that the buffer is long enough.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A critical-severity memory corruption bug in DOM: Core & HTML. Mozilla made it public on 2 January 2019.
About 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.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.