Potential double free via CacheIndex::ParseRecords()
Categories
(Core :: Networking: Cache, defect, P1)
Tracking
()
People
(Reporter: mozillabugs, Assigned: kershaw)
Details
(Keywords: csectype-uaf, reporter-external, sec-moderate, Whiteboard: [necko-triaged] [necko-priority-queue][adv-main127-][adv-esr115.12-])
Attachments
(2 external files)
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.
Updated•2 years ago
|
| Assignee | ||
Updated•2 years ago
|
| Assignee | ||
Comment 1•2 years ago
|
||
Updated•2 years ago
|
Comment 2•2 years ago
|
||
I'll mark it moderate because an attacker probably won't be able to cause cache errors in the parent process.
Updated•2 years ago
|
Updated•2 years ago
|
Comment 4•2 years ago
|
||
Updated•2 years ago
|
Updated•2 years ago
|
Updated•2 years ago
|
Comment 5•2 years ago
|
||
Please request ESR uplift on this when you get a chance.
| Assignee | ||
Comment 6•2 years ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D208739
Updated•2 years ago
|
Comment 7•2 years ago
|
||
esr115 Uplift Approval Request
- User impact if declined: Possible crash
- Code covered by automated testing: yes
- Fix verified in Nightly: yes
- Needs manual QE test: no
- Steps to reproduce for manual QE testing: N/A
- Risk associated with taking this patch: Low
- Explanation of risk level: This patch is straightforward and not likely to cause regression.
- String changes made/needed: N/A
- Is Android affected?: yes
| Assignee | ||
Comment 8•2 years ago
|
||
(In reply to Ryan VanderMeulen [:RyanVM] from comment #5)
Please request ESR uplift on this when you get a chance.
Done. Thanks.
Comment 9•2 years ago
|
||
Comment on attachment 9403427 [details]
Bug 1893075 - Refactor CacheFileIOManager::Write, r=#necko
Approved for 115.12esr.
Comment 10•2 years ago
|
||
| uplift | ||
Updated•2 years ago
|
Updated•2 years ago
|
Comment 11•2 years ago
|
||
Absent knowing a possible, even if unlikely, situation where this could actually occur, I don't think it warrants a CVE.
Updated•2 years ago
|
Updated•1 year ago
|
Description
•