Skip to content
Memory corruption Disclosed report Advanced

Write beyond bounds caused by nsTSubstringTuple_CharT::Length()

The function that computes the total length of a tree of string fragments does not check for integer overflow, so callers can allocate a buffer that is too small and then write the concatenated fragments past its end. The author found two vulnerable callers but had not yet identified a caller that can definitely reach them with fragments long enough to trigger the overflow.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 14 days
  • Paid in 31 days
  • Disclosed after 14 days
Text
nsTSubstringTuple_CharT (xpcom\string\nsTSubstringTuple.h/.cpp), which represents binary trees of string fragments, can experience an integer overflow in nsTSubstringTuple_CharT::Length(), which computes the total length of a tree of string fragments. If Length() overflows, its callers can fail to allocate sufficient space to contain the concatenation of the string fragments, causing a write beyond bounds. I have found two susceptible callers: nsTSubstring_CharT::Assign(const substring_tuple_type &, fallible_t) and nsTSubstring_CharT::Replace(..., const substring_tuple_type &).

I have not yet found a caller of Assign() or Replace() that definitely can call one of those functions with a nsTSubstringTuple_CharT whose total length is long enough to cause the overflow, but nsTSubstringTuple_CharT is widely used, and I expect that such a caller exists. (BTW, the maximum-allowed length of |nsCString|s and |nsAString|s is 0x7ffffff5 characters and 0x3ffffff9 characters, respectively [1], so an overflow can be caused by only 3 |nsCString| fragments of maximum length) or 5 |nsAString| fragments of maximum length).


Details
-------

The bug is in Length(), which computes the total length of the tree's fragments into |len| without checking for overflow; presumably it should OOM_CRASH on overflow:

12: nsTSubstringTuple_CharT::size_type
13: nsTSubstringTuple_CharT::Length() const
14: {
15:   uint32_t len;
16:   if (mHead) {
17:     len = mHead->Length();
18:   } else {
19:     len = TO_SUBSTRING(mFragA).Length();
20:   }
21: 
22:   return len + TO_SUBSTRING(mFragB).Length();
23: }

The bug then propagates into, e.g., nsTSubstring_CharT::Assign(const substring_tuple_type &, fallible_t) (xpcom\string\nsTSubstring.h/.cpp), which uses Length() to compute the total length of the string that would result from concatenating all of the fragments in the source nsTSubstringTuple_CharT (line 460), and MutatePrep() (line 465) to allocate that amount of space:

451: bool
452: nsTSubstring_CharT::Assign(const substring_tuple_type& aTuple,
453:                            const fallible_t& aFallible)
454: {
455:   if (aTuple.IsDependentOn(mData, mData + mLength)) {
456:     // take advantage of sharing here...
457:     return Assign(string_type(aTuple), aFallible);
458:   }
459: 
460:   size_type length = aTuple.Length();
461: 
462:   // don't use ReplacePrep here because it changes the length
463:   char_type* oldData;
464:   uint32_t oldFlags;
465:   if (!MutatePrep(length, &oldData, &oldFlags)) {
466:     return false;
467:   }
468: 
469:   if (oldData) {
470:     ::ReleaseData(oldData, oldFlags);
471:   }
...

Assign() then uses nsTSubstringTuple_CharT::WriteTo() to write the data into the buffer |mData|:

472: 
473:   aTuple.WriteTo(mData, length);
474:   mData[length] = 0;
475:   mLength = length;
476:   return true;
477: }

But nsTSubstringTuple_CharT::WriteTo() does not check (except in debug builds) whether |mData| is long enough to contain the data, so }headLen| on line 38 can underflow, and |a.Length()| (line 45) and |b.Length()| (line 48) are used to copy fragments into the buffer without checking whether it is large enough to contain them:

32: void
33: nsTSubstringTuple_CharT::WriteTo(char_type* aBuf, uint32_t aBufLen) const
34: {
35:   const substring_type& b = TO_SUBSTRING(mFragB);
36: 
37:   NS_ASSERTION(aBufLen >= b.Length(), "buffer too small");
38:   uint32_t headLen = aBufLen - b.Length();
39:   if (mHead) {
40:     mHead->WriteTo(aBuf, headLen);
41:   } else {
42:     const substring_type& a = TO_SUBSTRING(mFragA);
43: 
44:     NS_ASSERTION(a.Length() == headLen, "buffer incorrectly sized");
45:     char_traits::copy(aBuf, a.Data(), a.Length());
46:   }
47: 
48:   char_traits::copy(aBuf + headLen, b.Data(), b.Length());
49: 
50: #if 0
...
69: #endif
70: }

[1] Both limits are enforced by nsTSubstring_CharT::MutatePrep().

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.

How it worked

Root cause

Likely the length calculation adds fragment lengths in a fixed-width integer without checking for overflow, and the write routine trusts the resulting length.

Impact

A write beyond bounds could corrupt memory, but the text does not demonstrate a reachable caller, so exploitation is not shown.

What the attacker needed

Fragments near the maximum string length; a caller that reaches Assign or Replace

The fix

The length calculation should detect overflow and crash rather than return a wrapped value, as the author suggests.

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.

Strings in this codebase can be built by joining several pieces together, and the code first adds up how long the joined result will be so it can reserve that much memory. If the total is so large that the number wraps around to a small value, too little memory is reserved and the pieces are copied past the end of the buffer, corrupting memory. The author shows the faulty addition but admits they have not yet found a real path that reaches it.

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.