Skip to content
Memory corruption Disclosed report Advanced

Logic error and overflow in nsSegmentedBuffer causes underallocation and write beyond bounds (latent)

nsSegmentedBuffer::AppendNewSegment() checks the buffer's current size rather than the size after adding a new segment, and GetSize() computes the total with 32-bit arithmetic that overflows past 4 GiB. A caller that trusted GetSize() to size an allocation and then copied segments out would write beyond the allocated buffer.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 20 days
  • Paid in 31 days
  • Disclosed after 20 days
Text
Created attachment 9321207
Toolbar.js

`nsSegmentedBuffer::AppendNewSegment()` (`xpcom/io/nsSegmentedBuffer.cpp`) incorrectly checks its current size against its maximum allowed size, and `nsSegmentedBuffer::GetSize()` uses 32 bit math. The first bug allows an oversized allocation to occur, which then invokes the second bug, causing overflow in `nsSegmentedBuffer::GetSize()`. If other code were to trust `nsSegmentedBuffer::GetSize()` to return the correct size by using its return value to allocate memory, then were to copy out the object's contents by calling `nsSegmentedBuffer::GetSegment()` in a loop, it would write beyond bounds if the object contained > `0xffffffff` bytes of data.

I believe that this bug currently cannot be reached because searchfox says that `nsSegmentedBuffer::AppendNewSegment()` is the only caller of `nsSegmentedBuffer::GetSize()`.

The bugs are on line 24 of the .cpp file (which incorrectly tests against the object's current size instead of the requested new size) and on line 59 of the .h file (`nsSegmentedBuffer::GetSize()`), which incorrectly uses 32-bit math.

'''
(.cpp)
------
23: char* nsSegmentedBuffer::AppendNewSegment() {
24:   if (GetSize() >= mMaxSize) {
25:     return nullptr;
26:   }
27: ...
54:   char* seg = (char*)malloc(mSegmentSize);
55:   if (!seg) {
56:     return nullptr;
57:   }
58:   mSegmentArray[mLastSegmentIndex] = seg;
59:   mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
60:   return seg;
61: }

(.h)
------
59: inline uint32_t GetSize() { return GetSegmentCount() * mSegmentSize; }
'''

Attached is a POC that shows the oversized allocation and subsequent overflow. Use the POC thusly:

   1. Start with a built version of FF, not an installed one, because you will need to replace some JS files to create the chrome necessary to invoke `nsSegmentedBuffer::AppendNewSegment()`.

   2. Copy `toolbar.js` into `dist/bin/browser/chrome/devtools/modules/devtools/client/netmonitor/src/components/toolsbar.js`.

   3. Copy `har-menu-utils.js` into `dist/bin/browser/chrome/devtools/modules/devtools/client/netmonitor/src/har/har-menu-utils.js`.

   4. Start FF and open "tools/browser tools/web developer tools".
   
   5. Click the "network settings" star icon, then wait a minute or so. (This step helps avoid spurious breakpoints). Now attach a debugger to FF.

   6. Set a BP at the beginning of `nsSegmentednsSegmentedBuffer::AppendNewSegment()` and another on `nsPipe::nsPipe()` on the `MOZ_ALWAYS_SUCCEEDS` statement.

   7. Click the "network settings" star icon and select "netmonitor.context.exerciseabug".

   8. When the BP in `nsPipe::nsPipe()` fires, examine arguments `aSegmentSize` and `aSegmentCount`. If they are not `0x80000000` and `3`, respectively, proceed until you get those argument values.

   9. Doctor `mMaxAdvanceBufferSegmentCount` from `1` to `3`. This makes it possible to invoke the bug using this POC.

   10. Record the value of `&this->mBuffer` for later use. Proceed.

   11. When the BP on `nsSegmentedBuffer::AppendNewSegment()` fires, proceed until `this` matches the address you recorded in step 10. When that happens, step over the call to `GetSize()` and note what it returns. On the first BP, it should be `0`. Note that this isn't really what line 24 should be checking for! It should instead be checking whether the current size, plus the size of the new segment, would exceed `mMaxSize`. Proceed.

   12. You'll get another BP on `nsSegmentedBuffer::AppendNewSegment()` with a matching `this` pointer. Again step over `GetSize()` and notice that it returns `0x80000000`. Notice also that `mSegmentSize` is `0x80000000`. Proceed.

   13. You'll get a third BP on `nsSegmentedBuffer::AppendNewSegment()` with a matching `this` pointer. Again step over `GetSize()` and notice that it returns `0` (!), and that the `if` statement does not cause line 25 to be executed. This is because `GetSize()` overflowed because it uses 32-bit math.

The original report as published on Mozilla's Bugzilla, by mozillabugs. 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 size check compares the current size against the maximum instead of the current size plus the new segment, and GetSize() multiplies in 32-bit arithmetic that wraps for totals above 0xffffffff.

Impact

The overflow is latent: the text states the bug cannot currently be reached because the only caller of GetSize() is AppendNewSegment() itself, so no out-of-bounds write happens today.

What the attacker needed

Modified browser build; debugger attached; manual breakpoint adjustment

How it was found

  1. Build Firefox from source and replace the two devtools JavaScript files with the proof-of-concept versions.
  2. Start the browser, open the developer tools, and click the network settings star icon, then attach a debugger.
  3. Set breakpoints at the start of AppendNewSegment() and on the MOZ_ALWAYS_SUCCEEDS statement in nsPipe::nsPipe().
  4. Click the star icon again and choose the proof-of-concept menu entry.
  5. When the nsPipe breakpoint fires, adjust the maximum segment count from one to three and record the buffer address.
  6. At each AppendNewSegment() breakpoint for that address, step over GetSize() and observe it return 0, then 0x80000000, then 0 again.

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 buffer class keeps its data in fixed-size chunks and reports its total size. The check for 'is it full?' looked at the size before adding a chunk, not after, so one chunk too many could be added. The size calculation also used a 32-bit number, which wraps around to zero once the total passes about four gigabytes, so the buffer could claim to be empty while holding a lot of data.

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 mozillabugs

View all

41 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.