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.
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:
- Start FF.
- Load any page.
- Close FF.
- Start FF with -profilemanager.
- 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).
- Set a BP on
ParseRecords()line 2219. - Select the profile that you used in step 1 and start FF.
- When the BP fires, step into
CacheFileIOManager::Write(). - 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.
- Step lines 2036-38. Record the address of the buffer that line 2036
free()ed. - Step out into
ParseRecords(), step and watch itfree()the same buffer again.
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
- Start the browser and load any page, then close it.
- Start the browser with the profile manager and attach a debugger that follows child processes.
- Set a breakpoint on the write call inside the cache index parsing routine.
- Select the earlier profile and start the browser, then step into the write helper when the breakpoint fires.
- Force the early error branch in the write helper and let it free the buffer.
- 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.