Skip to content
Memory corruption Disclosed report CVE-2016-1974

Lack of status return from nsScannerString::AppendUnicodeTo causes out-of-bounds read in AppendErrorPointer

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 3 months
  • Paid in 2 months
  • Disclosed after 3 months
Text
Created attachment 8692130
poc.7z: unzip and use as described in comment 0 (1.1gb when unzipped)

nsScannerString::AppendUnicodeTo (parser\htmlparser\nsScannerString.cpp) calls nsAString::SetLength (which can fallibly fail under OOM conditions), but AppendUnicodeTo does not return status, so its callers incorrectly assume that it cannot fail. When the caller is AppendErrorPointer (parser\htmlparser\nsExpatDriver.cpp), and a document similar to the attached proof-of-concept is loaded, this omission causes AppendErrorPointer to read memory to which it has no right. AFAIKT this doesn't cause a meaningful information leak, only a broken error string and/or a crash. However, there are many other callers of nsScannerString::AppendUnicodeTo, plus there are callers of nsScannerString::CopyUnicodeTo, which has a similar bug. Probably some of these callers malfunction insecurely when AppendUnicodeTo or CopyUnicodeTo fail.

The bug is still present in the current trunk: http://hg.mozilla.org/mozilla-central/file/tip/parser/htmlparser/nsScannerString.cpp .


Details
-------

The bug is in the void return type:

485: void
486: AppendUnicodeTo( const nsScannerIterator& aSrcStart,
487:                  const nsScannerIterator& aSrcEnd,
488:                  nsAString& aDest )
489:   {
490:     nsAString::iterator writer;
491:     uint32_t oldLength = aDest.Length();
492:     if (!aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd), mozilla::fallible))
493:       return; // out of memory
494:     aDest.BeginWriting(writer).advance(oldLength);
495:     nsScannerIterator fromBegin(aSrcStart);
496:     
497:     copy_multifragment_string(fromBegin, aSrcEnd, writer);
498:   }

With the attached POC, this bug causes out-of-bounds reading in AppendErrorPointer. This occurs because nsExpatDriver::ConsumeToken, line 1126, updates its |mExpatParser.mPosition.col| member to account for the number of just-consumed characters (|consumed|) [1], Then it calls AppendUnicodeTo to append those characters to |mLastLine| (line 1139):

1115:     uint32_t consumed;
1116:     ParseBuffer(buffer, length, noMoreBuffers, &consumed);
1117:     if (consumed > 0) {
1118:       nsScannerIterator oldExpatPosition = currentExpatPosition;
1119:       currentExpatPosition.advance(consumed);
1120: 
1121:       // We consumed some data, we want to store the last line of data that
1122:       // was consumed in case we run into an error (to show the line in which
1123:       // the error occurred).
1124: 
1125:       // The length of the last line that Expat has parsed.
1126:       XML_Size lastLineLength = XML_GetCurrentColumnNumber(mExpatParser);
1127: 
1128:       if (lastLineLength <= consumed) {
1129:         // The length of the last line was less than what expat consumed, so
1130:         // there was at least one line break in the consumed data. Store the
1131:         // last line until the point where we stopped parsing.
1132:         nsScannerIterator startLastLine = currentExpatPosition;
1133:         startLastLine.advance(-((ptrdiff_t)lastLineLength));
1134:         CopyUnicodeTo(startLastLine, currentExpatPosition, mLastLine);
1135:       }
1136:       else {
1137:         // There was no line break in the consumed data, append the consumed
1138:         // data.
1139:         AppendUnicodeTo(oldExpatPosition, currentExpatPosition, mLastLine);
1140:       }
1141:     }

But this fails because the resulting string is longer than the maximum allowed by nsString (0x3ffffff9, enforced by nsTSubstring_CharT::MutatePrep). Because AppendUnicodeTo doesn't return status, ConsumeToken doesn't learn about this failure, and so doesn't correct |mPosition| to account for it. Thus, |mPosition| indexes data that's beyond the end of |mLastLine|. ConsumeToken then plows ahead, calling nsExpatDriver::HandleError (line 1184):

1160:     if (NS_FAILED(mInternalState)) {
1161:       if (XML_GetErrorCode(mExpatParser) != XML_ERROR_NONE) {
1162:         NS_ASSERTION(mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING,
1163:                      "Unexpected error");
...
1184:         HandleError();
1185:       }
1186: 
1187:       return mInternalState;
1188:     }

which calls XML_GetCurrentColumnNumber (line 946):

884: nsresult
885: nsExpatDriver::HandleError()
886: {
887:   int32_t code = XML_GetErrorCode(mExpatParser);
...
945:   // Adjust the column number so that it is one based rather than zero based.
946:   uint32_t colNumber = XML_GetCurrentColumnNumber(mExpatParser) + 1;
947:   uint32_t lineNumber = XML_GetCurrentLineNumber(mExpatParser);
948: 
949:   nsAutoString errorText;
950:   CreateErrorText(description.get(), XML_GetBase(mExpatParser), lineNumber,
951:                   colNumber, errorText);
952: 
953:   NS_ASSERTION(mSink, "no sink?");

which reads the updated |mPosition| member. Then HandleError passes the resulting (out-of-bounds) value to AppendErrorPointer:

954: 
955:   nsAutoString sourceText(mLastLine);
956:   AppendErrorPointer(colNumber, mLastLine.get(), sourceText);

which then reads from the out-of-bounds locations in |mLastLine| (line 868):

856: static nsresult
857: AppendErrorPointer(const int32_t aColNumber,
858:                    const char16_t *aSourceLine,
859:                    nsString& aSourceString)
860: {
861:   aSourceString.Append(char16_t('\n'));
862: 
863:   // Last character will be '^'.
864:   int32_t last = aColNumber - 1;
865:   int32_t i;
866:   uint32_t minuses = 0;
867:   for (i = 0; i < last; ++i) {
868:     if (aSourceLine[i] == '\t') {
869:       // Since this uses |white-space: pre;| a tab stop equals 8 spaces.
870:       uint32_t add = 8 - (minuses % 8);
871:       aSourceString.AppendASCII("--------", add);
872:       minuses += add;
873:     }
874:     else {
875:       aSourceString.Append(char16_t('-'));
876:       ++minuses;
877:     }
878:   }
879:   aSourceString.Append(char16_t('^'));
880: 
881:   return NS_OK;
882: }

Since the read is used only to decide what to put into the error string |aSourceString|, there is no meaningful data leak.


To use the POC, run FF 42.0 Win64 and attach a debugger to it. Set a breakpoint on nsExpatDriver line 1118 with the condition |consumed==0x5f|, then load the POC. When the BP fires, notice how line 1126 updates |mExpatParser.mPosition.col| to 0x4000000d. Then step to line 1139 and notice that |oldLength| is 0x3fffffae, and that the call to SetLength fails, leaving |aDest| with length 0x3fffffae. Step out, and see that |mLastLine| is also thus 0x3fffffae characters long. Step past lines 1160-1182 (which append one more character to mLastLine, which append succeeds because the resulting length doesn't exceeed nsAString's limit), then step into HandleError. Notice |colNumber| at line 946 is 0x4000000e. Step into AppendErrorPointer, noticing that line 864 assigns 0x4000000d to |last|. Examine |aSourceLine [last-1]| and notice it's inaccessible memory (or sometimes uninitialized memory). Let execution proceed and watch the probable read-access violation crash. It might take a minute or two to crash because AppendErrorPointer appends one character at a time to the error message string.

(Note that the POC has the invalid character 0x1a following "</text>", which is necessary to cause the parse error that causes HandleError to be called).

[1] Yes, |XML_GetCurrentColumnNumber(mExpatParser)| updates |mExpatParser.mPosition.col|.

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 high-severity memory corruption bug in DOM: HTML Parser. Mozilla made it public on 10 February 2016 as CVE-2016-1974.

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.