Skip to content
Memory corruption Disclosed report Advanced

Write beyond stack bounds caused by nsScannerString functions

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 21 days
  • Paid in 27 days
  • Disclosed after 21 days
Text
nsScannerString::copy_multifragment_string() (parser\htmlparser\nsScannerString.cpp) can write beyond the destination string's bounds. This occurs when an nsScannerString object represents a string that is >= 4 GB long [1], In that case, nsScannerString::CopyUnicodeTo() or nsScannerString::AppendUnicodeTo() truncate the 64-bit |size_t| result from nsScannerString::Distance() to a uint32_t, then use the result to allocate space in the destination string using |aDest.SetLength()|. (SetLength() takes a |uint32_t|, so the truncation occurs in the call). nsScannerString::CopyUnicodeTo() or nsScannerString::AppendUnicodeTo() then call nsScannerString::copy_multifragment_string(), which copies the untruncated count of characters (which can come from an untrusted source) into the destination string, thus writing an integer multiple of 4 GB beyond its bounds.

* The string that gets overrun is the |nsFixedString| |theBuffer| in nsParser::WillBuildModel() (parser\htmlparser\nsParser.cpp), which uses a stack buffer, so the code overwrites the stack, not the heap. *

The bugs are in lines 475 and 509:

469: bool
470: CopyUnicodeTo( const nsScannerIterator& aSrcStart,
471:                const nsScannerIterator& aSrcEnd,
472:                nsAString& aDest )
473:   {
474:     nsAString::iterator writer;
475:     if (!aDest.SetLength(Distance(aSrcStart, aSrcEnd), mozilla::fallible)) {
476:       aDest.Truncate();
477:       return false; // out of memory
478:     }
479:     aDest.BeginWriting(writer);
480:     nsScannerIterator fromBegin(aSrcStart);
481:     
482:     copy_multifragment_string(fromBegin, aSrcEnd, writer);
483:     return true;
484:   }
...
501: 
502: bool
503: AppendUnicodeTo( const nsScannerIterator& aSrcStart,
504:                  const nsScannerIterator& aSrcEnd,
505:                  nsAString& aDest )
506:   {
507:     nsAString::iterator writer;
508:     uint32_t oldLength = aDest.Length();
509:     if (!aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd), mozilla::fallible))
510:       return false; // out of memory
511:     aDest.BeginWriting(writer).advance(oldLength);
512:     nsScannerIterator fromBegin(aSrcStart);
513:     
514:     copy_multifragment_string(fromBegin, aSrcEnd, writer);
515:     return true;
516:   }

The attached POC crashes FF with memory corruption; the string copied beyond bounds comes directly from a file loaded via an AJAX request. The POC causes a truncation from 0x100000005 to 0x5 in SetLength(). (BTW, because of a performance bug in nsScanner::AppendToBuffer(), the POC takes a few days to crash FF. You can speed it up by disabling Electrolysis and setting the buffer cache size (network.buffer.cache.size) to 1048560; in this case, the crash occurs within ~ 12 hours.)

FWIW, the performance bug is that nsScanner::AppendToBuffer() rescans its entire nsScannerString for non-whitespace characters from the beginning each time it appends a new buffer, rather than keeping track of what it's already scanned. Thus increasing network.buffer.cache.size speeds up the crash because it minimizes the number of times AppendToBuffer() must rescan the nsScannerString.

BTW, there is also a similar truncation bug in nsScanner (parser\htmlparser\nsScanner.cpp) on the 32-bit member variables |mCountRemaining| and |mFirstNonWhitespacePosition|, which sometimes are assigned |Distance()| (a |size_t| quantity). I will file a separate bug for that issue.


[1] Thus, this bug occurs only on x64 builds.

The original report as published on Mozilla's Bugzilla, by q1. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 medium-severity memory corruption bug in XML. Mozilla made it public on 16 February 2017.

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.

More by q1

View all

71 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.