Closed Bug 1893075 Opened 2 years ago Closed 2 years ago

Potential double free via CacheIndex::ParseRecords()

Categories

(Core :: Networking: Cache, defect, P1)

defect

Tracking

()

RESOLVED FIXED
127 Branch
Tracking Status
firefox-esr115 127+ fixed
firefox125 --- wontfix
firefox126 --- wontfix
firefox127 + fixed

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:

  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.
Flags: sec-bounty?
Group: core-security → network-core-security
Severity: -- → S2
Priority: -- → P1
Whiteboard: [necko-triaged] [necko-priority-queue]
Assignee: nobody → kershaw
Status: NEW → ASSIGNED

I'll mark it moderate because an attacker probably won't be able to cause cache errors in the parent process.

Attachment #9398823 - Attachment description: Bug 1893075 - Make callers of CacheFileIOManager::Write release buffer, r=#necko → Bug 1893075 - Make CacheFileIOManager::Write release buffer when no callback is provided, r=#necko
Attachment #9398823 - Attachment description: Bug 1893075 - Make CacheFileIOManager::Write release buffer when no callback is provided, r=#necko → Bug 1893075 - Refactor CacheFileIOManager::Write, r=#necko
Pushed by kjang@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/c957deb1eb91 Refactor CacheFileIOManager::Write, r=necko-reviewers,valentin
Group: network-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 127 Branch
Flags: sec-bounty? → sec-bounty+
QA Whiteboard: [post-critsmash-triage]
Flags: qe-verify-

Please request ESR uplift on this when you get a chance.

Flags: needinfo?(kershaw)
Attachment #9403427 - Flags: approval-mozilla-esr115?

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

(In reply to Ryan VanderMeulen [:RyanVM] from comment #5)

Please request ESR uplift on this when you get a chance.

Done. Thanks.

Flags: needinfo?(kershaw)

Comment on attachment 9403427 [details]
Bug 1893075 - Refactor CacheFileIOManager::Write, r=#necko

Approved for 115.12esr.

Attachment #9403427 - Flags: approval-mozilla-esr115? → approval-mozilla-esr115+

Absent knowing a possible, even if unlikely, situation where this could actually occur, I don't think it warrants a CVE.

Whiteboard: [necko-triaged] [necko-priority-queue] → [necko-triaged] [necko-priority-queue][adv-main127-]
Whiteboard: [necko-triaged] [necko-priority-queue][adv-main127-] → [necko-triaged] [necko-priority-queue][adv-main127-][adv-esr115.12-]
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: