Skip to content
Memory corruption Disclosed report CVE-2015-4487

Overflow nsTSubstring::ReplacePrep causes memory-safety bugs in string library

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 33 days
  • Paid in 46 days
  • Disclosed after 33 days
Text
User Agent: Mozilla/5.0 (Windows NT 5.1; rv:36.0) Gecko/20100101 Firefox/36.0
Build ID: 20150305021524

Steps to reproduce:

nsTSubstring::ReplacePrep (C:\docs\dev\firefox\source\38.0.1\xpcom\string\nsTSubstring.h) can cause memory to be written that the destination string object does not own. This bug could permit an attacker to write portions of Firefox's address space, potentially allowing her to execute code of her choice.

The bug is in line 1003:

 998:  NS_WARN_UNUSED_RESULT bool ReplacePrep(index_type aCutStart,
 999:                                         size_type aCutLength,
1000:                                         size_type aNewLength)
1001:  {
1002:    aCutLength = XPCOM_MIN(aCutLength, mLength - aCutStart);
1003:    uint32_t newTotalLen = mLength - aCutLength + aNewLength;
1004:    if (aCutStart == mLength && Capacity() > newTotalLen) {
1005:      mFlags &= ~F_VOIDED;
1006:      mData[newTotalLen] = char_type(0);
1007:      mLength = newTotalLen;
1008:      return true;
1009:    }
1010:    return ReplacePrepInternal(aCutStart, aCutLength, aNewLength, newTotalLen);
1011:  }

If aCutStart > mLength, aCutLength becomes a very large uint32_t. The same thing happens if aCutLength is very large, or if aNewLength is very large. In all of these cases, newTotalLen can overflow, resulting in a call to ReplacePrepInternal with a too-small newTotalLen.


The simplest case is where aNewLength is very large.

Using aNewLength == 0xfffffffe and mLength == 10 for the existing string, imagine a call to Append (&s, 0xfffffffe). (The same kind of thing works on AppendASCII, etc.). Append lives at nsTSubstring.h line 531:

531:  void Append(const char_type* aData, size_type aLength = size_type(-1))
532:  {
533:    Replace(mLength, 0, aData, aLength);
534:  }
 
The call to Replace resolves to the function at nsTSubstring.cpp line 502 with arguments (10, 0, &s, 0xfffffffe):

502: void
503: nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
504:                             const char_type* aData, size_type aLength)
505: {
506:   if (!Replace(aCutStart, aCutLength, aData, aLength,
507:                mozilla::fallible)) {
508:     AllocFailed(Length() - aCutLength + 1);
509:   }
510: }

which then chains to the function at nsTSubstring.cpp line 512 with arguments (10, 0, &s, 0xfffffffe, mozilla::fallible)):

512: bool
513: nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
514:                             const char_type* aData, size_type aLength,
515:                             const fallible_t& aFallible)
516: {
517:   // unfortunately, some callers pass null :-(
518:   if (!aData) {
519:     aLength = 0;
520:   } else {
521:     if (aLength == size_type(-1)) {
522:       aLength = char_traits::length(aData);
523:     }
524: 
525:     if (IsDependentOn(aData, aData + aLength)) {
526:       nsTAutoString_CharT temp(aData, aLength);
527:       return Replace(aCutStart, aCutLength, temp, aFallible);
528:     }
529:   }
530: 
531:   aCutStart = XPCOM_MIN(aCutStart, Length());
532: 
533:   bool ok = ReplacePrep(aCutStart, aCutLength, aLength);
534:   if (!ok) {
535:     return false;
536:   }
537: 
538:   if (aLength > 0) {
539:     char_traits::copy(mData + aCutStart, aData, aLength);
540:   }
541: 
542:   return true;
543: }

Control passes to line 525, which returns false because aData does not overlap mData. Line 531 then computes 10 for aCutStart, so line 533 calls ReplacePrep (10, 0, 0xfffffffe). That function:

 998:  NS_WARN_UNUSED_RESULT bool ReplacePrep(index_type aCutStart,
 999:                                         size_type aCutLength,
1000:                                         size_type aNewLength)
1001:  {
1002:    aCutLength = XPCOM_MIN(aCutLength, mLength - aCutStart);
1003:    uint32_t newTotalLen = mLength - aCutLength + aNewLength;
1004:    if (aCutStart == mLength && Capacity() > newTotalLen) {
1005:      mFlags &= ~F_VOIDED;
1006:      mData[newTotalLen] = char_type(0);
1007:      mLength = newTotalLen;
1008:      return true;
1009:    }
1010:    return ReplacePrepInternal(aCutStart, aCutLength, aNewLength, newTotalLen);
1011:  }

computes aCutLength == 0 on line 1002, and newTotalLen == 10 - 0 + 0xfffffffe == 8 on line 1003. The test on line 1004 passes, line 1006 spuriously writes a 0 to mData [8], and control returns to the caller (nsTSubstring_CharT::Replace) at line 534. Control then eventually passes to line 539:

539:     char_traits::copy(mData + aCutStart, aData, aLength);

which copies 0xfffffffe bytes from aData into the unallocated region beginning at mData + 10.

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 XPCOM. Mozilla made it public on 7 July 2015 as CVE-2015-4487.

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.