Skip to content
Memory corruption Disclosed report Advanced CVE-2023-25752

Potential write beyond bounds caused by ThrottleInputStream::Read()/ReadSegments()

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 36 days
  • Paid in 39 days
  • Disclosed after 36 days
Text
Created attachment 9313375
ffbug_2080.htm

`ThrottleInputStream::Read()/ReadSegments()` (`netwerk/base/ThrottleQueue.cpp` ) ignore the caller's requested read size `aCount`, potentially causing lower layers to read data into memory beyond the end of a caller's buffer.

`ThrottleInputStream` is used only by `ThrottleQueue`. I have found one user (the throttling control on the Developer Tools panel) which can be used to observe usage of the incorrect read size, but which doesn't suffer a WBB (write beyond bounds) because the read size is not used to buffer data, only to send data out a socket. I am not sure whether there are other users, though `nsHttpTransaction::Init()` has the ability to use the class via its `eventSink` argument.

The bug is that `ThrottleInputStream::Read()/ReadSegments()` don't minimize the amount of data potentially available in the underlying stream (`realCount`) against the requested read size `aCount`. (While `Available()`, line 110, takes `aCount`, it doesn't use it):

'''
102: NS_IMETHODIMP
103: ThrottleInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
104:                                   uint32_t aCount, uint32_t* aResult) {
...
109:   uint32_t realCount;
110:   nsresult rv = mQueue->Available(aCount, &realCount);
111:   if (NS_FAILED(rv)) {
112:     return rv;
113:   }
114: 
115:   if (realCount == 0) {
116:     return NS_BASE_STREAM_WOULD_BLOCK;
117:   }
118: 
119:   rv = mStream->ReadSegments(aWriter, aClosure, realCount, aResult);
120:   if (NS_SUCCEEDED(rv) && *aResult > 0) {
121:     mQueue->RecordRead(*aResult);
122:   }
123:   return rv;
124: 
125: }
'''

[This function doesn't use `aRemaining`:]

'''
267: NS_IMETHODIMP
268: ThrottleQueue::Available(uint32_t aRemaining, uint32_t* aAvailable) {
269:   MOZ_ASSERT(OnSocketThread(), "not on socket thread");
270:   TimeStamp now = TimeStamp::Now();
271:   TimeStamp oneSecondAgo = now - TimeDuration::FromSeconds(1);
272:   size_t i;
273: 
274:   // Remove all stale events.
275:   for (i = 0; i < mReadEvents.Length(); ++i) {
276:     if (mReadEvents[i].mTime >= oneSecondAgo) {
277:       break;
278:     }
279:   }
280:   mReadEvents.RemoveElementsAt(0, i);
281: 
282:   uint32_t totalBytes = 0;
283:   for (i = 0; i < mReadEvents.Length(); ++i) {
284:     totalBytes += mReadEvents[i].mBytesRead;
285:   }
286: 
287:   uint32_t spread = mMaxBytesPerSecond - mMeanBytesPerSecond;
288:   double prob = static_cast<double>(rand()) / RAND_MAX;
289:   uint32_t thisSliceBytes =
290:       mMeanBytesPerSecond - spread + static_cast<uint32_t>(2 * spread * prob);
291: 
292:   if (totalBytes >= thisSliceBytes) {
293:     *aAvailable = 0;
294:   } else {
295:     *aAvailable = thisSliceBytes;
296:   }
297:   return NS_OK;
298: }
'''

You can verify that `ThrottleInputStream::ReadSegments()` can call the lower layer with a byte count exceeding `aCount` by doing the following:

   1. Start FF and attach a debugger to it.
   2. Set a BP in `ThrottleInputStream::ReadSegments()` on the call to `mStream->ReadSegments()`.
   3. Open FF's Web Developer Tools panel, then the network tab.
   4. Check "Disable Cache" and select "WiFi" from the throttling control.
   5. Put ffbug_2080.htm on a webserver. If the server is not running on 127.0.0.1, modify ffbug_2080.htm appropriately to do the GET request to the appropriate server.
   6. Load ffbug_2080.htm in FF.
   7. When the BP fires, examine `realCount`. Notice that it far exceeds `aCount` (in my test, `realCount` was `0x1e0000` and `aCount` was `0x8000`.
   8. Step into `mStream->ReadSegments()`. Control should end up in `nsStringInputStream::ReadSegments()`. Proceed to the line beginning
   
   '''
      nsresult = aWriter(...);
   '''

   and notice that `source` (as limited by this function's `aCount` and `maxCount`) contains the initial HTTP request to fetch ffbug_2080.htm. The request isn't very long, so it doesn't exceed the original `aCount` (0x8000).

   9. Proceed, take the BP again, step into `mStream->ReadSegments()` again, and proceed as in step 8. Notice that this function's `aCount` is now `0` because `maxCount` is `0`.

   10. Proceed and take the BP again, and step into `mStream->ReadSegments()` again, and proceed as in step 8. Notice that `source` contains the long `GET` request that the script in ffbug_2080.htm generated, and that the function's `aCount` and `maxCount` are both `0xc877`. This is much greater than the `aCount` that `ThrottleInputStream::ReadSegments()`'s caller requested (`0x8000`).

   11. You can trace all the way into `nsHttpConnection::OnReadSegment()` and find that `0xc877` bytes do get written to the socket.

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.

What is known

A medium-severity memory corruption bug in Networking. Mozilla made it public on 25 February 2023 as CVE-2023-25752.

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