Skip to content
Memory corruption Disclosed report Advanced

Potential double free via CacheIndex::ParseRecords()

A cache index parsing routine frees a heap buffer after a synchronous write failure, but the write helper already frees that same buffer when no callback is supplied, so the buffer is freed twice. The reporter does not show a real trigger and only demonstrates the double free by forcing the error path in a debugger.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 10 days
  • Paid in 14 days
  • Disclosed after 10 days

CacheIndex::ParseRecords() (netwerk/cache2/CacheIndex.cpp) appears to be able to cause a double free in the chrome process if certain errors occur. I don't know whether these errors currently are possible. Attached in a debugger-based POC showing the potential double free.

The bug centers around a curious -- and incorrect -- comment in ParseRecords() (lines 2216-18) and the code it describes. From FIREFOX_124_0_2_RELEASE:

    2183: void CacheIndex::ParseRecords(const StaticMutexAutoLock& aProofOfLock) {
    ...
    2196:   if (!mSkipEntries) {
    ...
    2206:     if (NetworkEndian::readUint32(mRWBuf + pos)) {
    2207:       if (mJournalHandle) {
    2208:         CacheFileIOManager::DoomFile(mJournalHandle, nullptr);
    2209:         mJournalHandle = nullptr;
    2210:       }
    2211:     } else {
    2212:       uint32_t* isDirty =
    2213:           reinterpret_cast<uint32_t*>(moz_xmalloc(sizeof(uint32_t)));
    2214:       NetworkEndian::writeUint32(isDirty, 1);
    2215: 
    2216:       // Mark index dirty. The buffer is freed by CacheFileIOManager when
    2217:       // nullptr is passed as the listener and the call doesn't fail
    2218:       // synchronously.
    2219:       rv = CacheFileIOManager::Write(mIndexHandle, 2 * sizeof(uint32_t),
    2220:                                      reinterpret_cast<char*>(isDirty),
    2221:                                      sizeof(uint32_t), true, false, nullptr);
    2222:       if (NS_FAILED(rv)) {
    2223:         // This is not fatal, just free the memory
    2224:         free(isDirty);
    2225:       }
    2226:     }

The problem is that CacheFileIOManager::Write() actually free()s the buffer if it does fail synchronously, per lines 2032-39 from netwerk/cache2/CacheFileIOManager.cpp below:

    2021: nsresult CacheFileIOManager::Write(CacheFileHandle* aHandle, int64_t aOffset,
    2022:                                    const char* aBuf, int32_t aCount,
    2023:                                    bool aValidate, bool aTruncate,
    2024:                                    CacheFileIOListener* aCallback) {
    ...
    2029:   nsresult rv;
    2030:   RefPtr<CacheFileIOManager> ioMan = gInstance;
    2031: 
    2032:   if (aHandle->IsClosed() || (aCallback && aCallback->IsKilled()) || !ioMan) {
    2033:     if (!aCallback) {
    2034:       // When no callback is provided, CacheFileIOManager is responsible for
    2035:       // releasing the buffer. We must release it even in case of failure.
    2036:       free(const_cast<char*>(aBuf));
    2037:     }
    2038:     return NS_ERROR_NOT_INITIALIZED;
    2039:   }
    2040: 
    2041:   RefPtr<WriteEvent> ev = new WriteEvent(aHandle, aOffset, aBuf, aCount,
    2042:                                          aValidate, aTruncate, aCallback);
    2043:   rv = ioMan->mIOThread->Dispatch(ev, aHandle->mPriority
    2044:                                           ? CacheIOThread::WRITE_PRIORITY
    2045:                                           : CacheIOThread::WRITE);
    2046:   NS_ENSURE_SUCCESS(rv, rv);
    2047: 
    2048:   return NS_OK;
    2049: }

You can force the bug to manifest using a debugger thusly:

  1. Start FF.
  2. Load any page.
  3. Close FF.
  4. Start FF with -profilemanager.
  5. Attach a debugger to it and ensure that the debugger is configured to attach to child processes (use, e.g., Child Process Debugging for Visual Studio).
  6. Set a BP on ParseRecords() line 2219.
  7. Select the profile that you used in step 1 and start FF.
  8. When the BP fires, step into CacheFileIOManager::Write().
  9. Step through line 2032. Probably there will be no line 2032 error conditions. If there are not, set control to line 2033 to simulate such an error. If there are, you've found that this bug can actually be manifested.
  10. Step lines 2036-38. Record the address of the buffer that line 2036 free()ed.
  11. Step out into ParseRecords(), step and watch it free() the same buffer again.

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

Impact

If the early error conditions in the write helper can actually occur, the browser process could free the same memory twice, which may lead to memory corruption; the reporter does not demonstrate a real trigger.

What the attacker needed

Debugger attached to child processes; ability to force the error branch

The fix

The caller should not free the buffer after a failed write when no callback was passed, since the write helper already releases it.

How it was found

  1. Start the browser and load any page, then close it.
  2. Start the browser with the profile manager and attach a debugger that follows child processes.
  3. Set a breakpoint on the write call inside the cache index parsing routine.
  4. Select the earlier profile and start the browser, then step into the write helper when the breakpoint fires.
  5. Force the early error branch in the write helper and let it free the buffer.
  6. Step back into the caller and watch it free the same buffer 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.

When a program asks the operating system to write data, it sometimes hands over a block of memory and says 'you own this now'. If the write fails straight away, the helper frees that block, but the caller also frees it, so the same block is released twice. That can corrupt memory and, in the worst case, let an attacker run code, though here the reporter only showed it under a debugger.

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.