Skip to content
Memory corruption Disclosed report Advanced

Overflows in nsSupportsArray could cause buffer overruns

The nsSupportsArray class does not check for integer overflow when growing its internal array, so adding a very large number of elements can cause an undersized buffer to be allocated and then written past its end. An attacker who can make an add-on or Firefox itself allocate a very large array could trigger a buffer overrun, though no proof of concept is provided.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 2 days
  • Paid in 5 days
  • Disclosed after 2 days

nsSupportsArray (xpcom\ds\nsSupportsArray.cpp/.h) neither checks for overflow when adding elements, nor limits the number of elements per instance of the class. It is, thus, possible for InsertElementsAt() or GrowArrayBy() to experience an integer overflow, causing failure to allocate a large-enough buffer, and subsequently a buffer overrun.

I do not have a POC at present, but since nsSupportsArray is used by add-ons, presumably a condition that causes an add-on to allocate very large arrays could manifest the bug(s). Also there are some uses of nsSupportsArray in FF itself.

The bugs are still present in trunk.

Details

The bugs are in InsertElementsAt() line 396 (can overflow) and GrowArrayBy() lines 117 (should use uint32_t), 127 (can overflow), 128 (can overflow), and 135 (can overflow).

There are 4 bug cases.

  1. If the addition on line 396 overflows, InsertElementsAt() doesn't attempt to allocate more space, but instead incorrectly assumes that the space already has been allocated, and proceeds to write beyond |mArray|'s end:

384: NS_IMETHODIMP_(bool) 385: nsSupportsArray::InsertElementsAt(nsISupportsArray* aElements, uint32_t aIndex) 386: { 387: if (!aElements) { 388: return false; 389: } 390: uint32_t countElements; 391: if (NS_FAILED(aElements->Count(&countElements))) { 392: return false; 393: } 394: 395: if (aIndex <= mCount) { 396: if (mArraySize < (mCount + countElements)) { 397: // need to grow the array 398: GrowArrayBy(countElements); 399: } 400: ... insert the elements [aElements...aElements+aIndex).

  1. If the addition on line 396 does not overflow, but |countElements| on line 398 is large enough to be interpreted as a negative |int32_t| by GrowArrayBy(), GrowArrayBy() allocates only kGrowArrayBy additional elements of space (lines 123-24 execute), which is insufficient and again causes InsertElementsAt() to overrun |mArray|:

116: void 117: nsSupportsArray::GrowArrayBy(int32_t aGrowBy) 118: { 119: // We have to grow the array. Grow by kGrowArrayBy slots if we're smaller 120: // than kLinearThreshold bytes, or a power of two if we're larger. 121: // This is much more efficient with most memory allocators, especially 122: // if it's very large, or of the allocator is binned. 123: if (aGrowBy < kGrowArrayBy) { 124: aGrowBy = kGrowArrayBy; 125: } 126: 127: uint32_t newCount = mArraySize + aGrowBy; // Minimum increase 128: uint32_t newSize = sizeof(mArray[0]) * newCount; 129: 130: if (newSize >= (uint32_t)kLinearThreshold) { 131: // newCount includes enough space for at least kGrowArrayBy new slots. 132: // Select the next power-of-two size in bytes above that if newSize is 133: // not a power of two. 134: if (newSize & (newSize - 1)) { 135: newSize = 1u << mozilla::CeilingLog2(newSize); 136: } 137: 138: newCount = newSize / sizeof(mArray[0]); 139: } 140: // XXX This would be far more efficient in many allocators if we used 141: // XXX PR_Realloc(), etc 142: nsISupports** oldArray = mArray; 143: 144: mArray = new nsISupports*[newCount]; 145: mArraySize = newCount; ...

  1. If the addition on line 396 does not overflow, and |countElements| is not large enough to be interpreted as a negative |int32_t| by GrowArrayBy(), but it is large enough for GrowArrayBy() line 128 to calculate a |newSize| >= 0x80000001, line 135 executes, causing |newSize| to become a small positive value, which again is insufficient and leads to a buffer overrun as above.

  2. The multiplication on line 128 also will overflow if |newCount| >= 0x20000000 (64-bit builds) or 0x40000000 (32-bit 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.

How it worked

Root cause

Likely the array-growth arithmetic uses signed and unsigned 32-bit values without overflow checks, so size calculations can wrap to small values and the subsequent writes exceed the allocated buffer.

Impact

An attacker who can cause a very large array to be allocated could trigger a buffer overrun, potentially leading to memory corruption, though the report provides no proof of concept and only speculates about reachability through add-ons.

What the attacker needed

Ability to make code allocate a very large array

The fix

The code should check for integer overflow in the size calculations and use unsigned types consistently so that an undersized buffer is never allocated.

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.

This is about a piece of code that stores a list of items in memory. When the list needs to grow, the code calculates how much extra space to reserve, but that calculation can wrap around to a small number, so the program reserves too little space and then writes past the end of it. Writing past the end of a buffer can crash the program or, in the worst case, let an attacker run their own code.

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.