Latent (?) write beyond bounds in nsTString_CharT::ReplaceSubstring()
The ReplaceSubstring() function in nsTString_CharT can experience an integer overflow when calculating the new string length, leading to a buffer that is too small and subsequent writes beyond its bounds. The bug is latent because no current callers use a sufficiently long replacement string to trigger the overflow.
- Fixed in 14 days
- Paid in 35 days
- Disclosed after 14 days
Created attachment 8810668
bug_629_poc_3.7z
nsTString_CharT::ReplaceSubstring() (xpcom\string\nsTStringObsolete.cpp) can experience an integer overflow. If it does, it writes data beyond the end of its |mData| character array.
The bug appears to be latent because no callers appear to use a sufficiently-long replacement (|aNewValue|) string.
Details
-------
When preparing to replace the instances of string |aTarget| in the destination string with string |aNewValue|, ReplaceSubstring() calculates the character count of the resulting string into |newLength| (lines 493-515). However, |newLength| isa uint32_t, and ReplaceSubstring() does not check whether the calculations used to compute it overflow.
If the destination string is long enough (e.g., 0x10000000 characters; this is permissible for nsAString), and it contains enough substrings equal to |aTarget| (e.g., 0x10000000 1-character substrings) and the ratio |aNewValue.Length() / aTarget.Length()| is large enough (e.g., 16), |newLength| will overflow (in this example, on line 507), causing line 531 to allocate a buffer large enough for only the existing destination string. The code in lines 555-71 then executes, writing beyond the end of the destination string.
This bug appears to be latent because AutoTArray (which lines 493-515 use) implicitly limits the number of replacements to 0x1073ffff (load the attached POC in FF x64 to see this limit in action). Since the largest ratio of |aNewValue.Length() / aTarget.Length()| in existing code appears to be 6 (line 697-8 of dom\base\nsXMLContentSerializer.cpp replaces "\"" with """), the maximum-computed value for |newLength| is ~ 0x62B7FFFA, which is safe.
However, adding an apparently-innocuous call to ReplaceSubstring() with a sufficiently-larger ratio would cause this bug to become exploitable.
485: bool
486: nsTString_CharT::ReplaceSubstring(const self_type& aTarget,
487: const self_type& aNewValue,
488: const fallible_t&)
489: {
490: if (aTarget.Length() == 0)
491: return true;
492:
493: // Remember all of the non-matching parts.
494: AutoTArray<Segment, 16> nonMatching;
495: uint32_t i = 0;
496: uint32_t newLength = 0;
497: while (true)
498: {
499: int32_t r = FindSubstring(mData + i, mLength - i, static_cast<const char_type*>(aTarget.Data()), aTarget.Length(), false);
500: int32_t until = (r == kNotFound) ? mLength - i : r;
501: nonMatching.AppendElement(Segment(i, until));
502: newLength += until;
503: if (r == kNotFound) {
504: break;
505: }
506:
507: newLength += aNewValue.Length();
508: i += r + aTarget.Length();
509: if (i >= mLength) {
510: // Add an auxiliary entry at the end of the list to help as an edge case
511: // for the algorithms below.
512: nonMatching.AppendElement(Segment(mLength, 0));
513: break;
514: }
515: }
516:
517: // If there's only one non-matching segment, then the target string was not
518: // found, and there's nothing to do.
519: if (nonMatching.Length() == 1) {
520: MOZ_ASSERT(nonMatching[0].mBegin == 0 && nonMatching[0].mLength == mLength,
521: "We should have the correct non-matching segment.");
522: return true;
523: }
524:
525: // Make sure that we can mutate our buffer.
526: // Note that we always allocate at least an mLength sized buffer, because the
527: // rest of the algorithm relies on having access to all of the original
528: // string. In other words, we over-allocate in the shrinking case.
529: char_type* oldData;
530: uint32_t oldFlags;
531: if (!MutatePrep(XPCOM_MAX(mLength, newLength), &oldData, &oldFlags))
532: return false;
533: if (oldData) {
534: // Copy all of the old data to the new buffer.
535: char_traits::copy(mData, oldData, mLength);
536: ::ReleaseData(oldData, oldFlags);
537: }
538:
539: if (aTarget.Length() >= aNewValue.Length()) {
540: // In the shrinking case, start filling the buffer from the beginning.
...
555: } else {
556: // In the growing case, start filling the buffer from the end.
557: const uint32_t delta = (aNewValue.Length() - aTarget.Length());
558: for (i = nonMatching.Length() - 1; i > 0; --i) {
559: // When we move the i'th non-matching segment into position, we need to
560: // account for the characters added by the previous |i| replacements by
561: // adding |i * delta|.
562: const char_type* sourceSegmentPtr = mData + nonMatching[i].mBegin;
563: char_type* destinationSegmentPtr = mData + nonMatching[i].mBegin + i * delta;
564: char_traits::move(destinationSegmentPtr, sourceSegmentPtr,
565: nonMatching[i].mLength);
566: // Write the i'th replacement immediately before the new i'th non-matching
567: // segment.
568: char_traits::copy(destinationSegmentPtr - aNewValue.Length(),
569: aNewValue.Data(), aNewValue.Length());
570: }
571: }
572:
...
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
Root cause
The newLength variable is a 32-bit unsigned integer, and the calculations to compute it do not check for overflow, leading to an undersized buffer allocation.
Impact
If triggered, the overflow could lead to writing beyond the allocated buffer, potentially causing a crash or arbitrary code execution, but the bug is latent because no current callers use a sufficiently long replacement string.
What the attacker needed
A call to ReplaceSubstring with a long replacement string; destination string long enough to cause overflow
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.
A string replacement function in Firefox's code can miscalculate the size of the result when the replacement text is much longer than the text being replaced. This miscalculation can cause the program to write data past the end of an allocated memory buffer, which could crash the browser or potentially allow an attacker to run malicious code. However, no current part of Firefox uses a long enough replacement to trigger this bug, so it is not currently exploitable.
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.