Closed Bug 2027978 Opened 7 months ago Closed 7 months ago

Heap-use-after-free in [@ DatabaseFile::WriteSucceededClearBlobImpl] due to missing cross-database actor validation in TransactionBase::VerifyRequestParams

Categories

(Core :: Storage: IndexedDB, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2021894

People

(Reporter: bugmon, Assigned: hsingh)

Details

(7 keywords, Whiteboard: [prefs-checked])

Attachments

(3 files)

Summary

A heap-use-after-free in the parent process caused by missing validation in TransactionBase::VerifyRequestParams at /firefox/dom/indexedDB/ActorsParent.cpp line ~10398. The parent does not verify that PBackgroundIDBDatabaseFileParent actors referenced in FileAddInfo are managed by the same Database as the receiving transaction. A compromised content process can therefore inject a file actor belonging to Database A into an add/put request on Database B. Since each database runs its operations on a separate connection thread, the single-thread-owned DatabaseFile::mBlobImpl (an InitializedOnce<const RefPtr<BlobImpl>>) gets destroyed on one thread while still being read on another.

Affected Code

File: dom/indexedDB/ActorsParent.cpp, line ~10398

for (const FileAddInfo& fileAddInfo : aParams.fileAddInfos()) {
  const PBackgroundIDBDatabaseFileParent* file =
      fileAddInfo.file().AsParent();

  switch (fileAddInfo.type()) {
    case StructuredCloneFileBase::eBlob:
      if (NS_AUUF_OR_WARN_IF(!file)) {
        return false;
      }
      // MISSING: no check that file->Manager() == this transaction's Database
      break;

The IPDL layer only guarantees that file is some live PBackgroundIDBDatabaseFileParent on this channel — it does not guarantee it is managed by the specific PBackgroundIDBDatabaseParent that owns this transaction. Without an explicit manager check, a malicious child can pass any file actor from any database.

The consequence is in DatabaseFile at line 2486-2529:

// mBlobImpl's ownership lifecycle:
// - Initialized on the background thread at creation time.  Then
//   responsibility is handed off to the connection thread.
// - Checked and used by the connection thread to generate a stream...
// - Cleared on the connection thread once the file has successfully been
//   written to disk.
InitializedOnce<const RefPtr<BlobImpl>> mBlobImpl;
...
void WriteSucceededClearBlobImpl() {
  MOZ_ASSERT(!IsOnBackgroundThread());
  MOZ_ASSERT(*mBlobImpl);
  mBlobImpl.destroy();   // <-- not thread-safe; assumed single connection thread
}

InitializedOnce::destroy() and the corresponding GetInputStream() reads of mBlobImpl have no synchronization because the design assumes a single owning connection thread.

Correct Pattern

case StructuredCloneFileBase::eBlob:
  if (NS_AUUF_OR_WARN_IF(!file)) {
    return false;
  }

  // Reject actors managed by a different Database
  if (NS_AUUF_OR_WARN_IF(file->Manager() !=
                         static_cast<const PBackgroundIDBDatabaseParent*>(
                             &GetDatabase()))) {
    return false;
  }
  break;

Exploit Chain

  1. Compromised content process opens multiple IndexedDB databases (e.g., 16) under the same origin. Each gets its own Database actor and its own connection-thread TaskQueue in the parent's ConnectionPool.
  2. Content process creates a single PBackgroundIDBDatabaseFileChild actor on Database A (via PBackgroundIDBDatabaseFileConstructor).
  3. Content process sends ObjectStoreAddParams / ObjectStorePutParams to transactions on Databases B, C, D, … each containing a FileAddInfo that references the actor from step 2.
  4. Parent's TransactionBase::VerifyRequestParams accepts each request because it only checks !file, not file->Manager().
  5. Each database's connection thread runs ObjectStoreAddOrPutRequestOp::DoDatabaseWork, which calls StoredFileInfo::GetInputStream() → DatabaseFile::GetInputStream() (reads mBlobImpl) and, on successful write, StoredFileInfo::NotifyWriteSucceeded() → DatabaseFile::WriteSucceededClearBlobImpl() → mBlobImpl.destroy().
  6. Because steps 5 run concurrently on different connection threads against the same DatabaseFile, one thread frees the StreamBlobImpl while another dereferences it (or both call destroy() concurrently), producing a heap-use-after-free.

IPC Path

  • IPDL protocols: PBackgroundIDBDatabase (manages PBackgroundIDBDatabaseFile and PBackgroundIDBTransaction), PBackgroundIDBTransaction (manages PBackgroundIDBRequest)
  • Parent side: Database, TransactionBase, DatabaseFile, ObjectStoreAddOrPutRequestOp in dom/indexedDB/ActorsParent.cpp
  • Child side: IDBDatabase, IDBObjectStore in dom/indexedDB/
  • Message flow: child sends PBackgroundIDBDatabaseFileConstructor on DB-A → child sends PBackgroundIDBRequestConstructor(ObjectStorePutParams{ fileAddInfos: [ {file: actor-from-DB-A} ] }) on transactions belonging to DB-B…N → parent dispatches to each DB's connection thread without rejecting the foreign actor.

Steps to Reproduce

  • Apply the provided exploit patch to dom/indexedDB/IDBObjectStore.cpp (simulates a compromised content process by creating the file actor on the first-opened database and reusing it in all subsequent databases' put requests).
  • Rebuild Firefox.
  • Load the testcase HTML which opens 16 databases and loops 2000 iterations of concurrent put() of the same Blob into all databases. Race typically triggers within a few seconds.

Security Impact

  • Severity: High (sec-high). Parent-process heap-use-after-free reachable from a compromised content process → sandbox escape primitive.
  • Attacker capability: A content-process RCE can use this to corrupt parent-process heap memory. The freed StreamBlobImpl (184 bytes) can be reclaimed with attacker-controlled data between the free on one thread and the vtable dereference in CreateInputStream on another thread, potentially leading to parent-process code execution.
  • Preconditions: Requires a prior content-process compromise (standard sandbox-escape threat model). No non-default prefs required.

ASAN Report

==152677==ERROR: AddressSanitizer: heap-use-after-free on address 0x7633442897a0 at pc 0x753320b5d66f bp 0x753295f57190 sp 0x753295f57188
READ of size 8 at 0x7633442897a0 thread T86
    #0 0x753320b5d66e in mozilla::dom::StreamBlobImpl::CreateInputStream(nsIInputStream**, mozilla::ErrorResult&) const /firefox/dom/file/StreamBlobImpl.cpp:142:66
    #1 0x753323e09c26 in mozilla::dom::indexedDB::(anonymous namespace)::DatabaseFile::GetInputStream(mozilla::ErrorResult&) const /firefox/dom/indexedDB/ActorsParent.cpp:2569:17
    #2 0x753323e09c26 in mozilla::dom::indexedDB::(anonymous namespace)::ObjectStoreAddOrPutRequestOp::StoredFileInfo::GetInputStream()::$_1::operator()(RefPtr<mozilla::dom::indexedDB::(anonymous namespace)::DatabaseFile> const&) const /firefox/dom/indexedDB/ActorsParent.cpp:3929:43
    ...
    #8 0x753323e09c26 in mozilla::dom::indexedDB::(anonymous namespace)::ObjectStoreAddOrPutRequestOp::DoDatabaseWork(mozilla::dom::indexedDB::(anonymous namespace)::DatabaseConnection*) /firefox/dom/indexedDB/ActorsParent.cpp:19262:9
    #9 0x753323deb62c in mozilla::dom::indexedDB::(anonymous namespace)::TransactionDatabaseOperationBase::RunOnConnectionThread() /firefox/dom/indexedDB/ActorsParent.cpp:17304:14
    #10 0x753323de9770 in mozilla::dom::indexedDB::(anonymous namespace)::TransactionDatabaseOperationBase::Run() /firefox/dom/indexedDB/ActorsParent.cpp:17472:7
    #11 0x7533187e95f7 in mozilla::TaskQueue::Runner::Run() /firefox/xpcom/threads/TaskQueue.cpp:309:20
    #12 0x7533188391b7 in nsThreadPool::Run() /firefox/xpcom/threads/nsThreadPool.cpp:444:14

0x7633442897a0 is located 96 bytes inside of 184-byte region [0x763344289740,0x7633442897f8)
freed by thread T81 here:
    #0 0x58d7c77516d6 in __interceptor_free
    #1 0x753320b5a3f5 in mozilla::dom::BlobImpl::Release() /firefox/dom/file/BlobImpl.cpp:96:1
    #2 0x753320b5a3f5 in mozilla::dom::StreamBlobImpl::Release() /firefox/dom/file/StreamBlobImpl.cpp:22:1
    ...
    #9 0x753323e0ae4f in mozilla::dom::indexedDB::(anonymous namespace)::DatabaseFile::WriteSucceededClearBlobImpl() /firefox/dom/indexedDB/ActorsParent.cpp:2529:15
    #10 0x753323e0ae4f in mozilla::dom::indexedDB::(anonymous namespace)::ObjectStoreAddOrPutRequestOp::StoredFileInfo::NotifyWriteSucceeded() const /firefox/dom/indexedDB/ActorsParent.cpp:3909:11
    #11 0x753323e0ae4f in mozilla::dom::indexedDB::(anonymous namespace)::ObjectStoreAddOrPutRequestOp::DoDatabaseWork(mozilla::dom::indexedDB::(anonymous namespace)::DatabaseConnection*) /firefox/dom/indexedDB/ActorsParent.cpp:19312:26
    #12 0x753323deb62c in mozilla::dom::indexedDB::(anonymous namespace)::TransactionDatabaseOperationBase::RunOnConnectionThread() /firefox/dom/indexedDB/ActorsParent.cpp:17304:14

previously allocated by thread T9 here:
    #0 0x58d7c7751974 in __interceptor_malloc
    ...
    #3 0x753320b5a579 in mozilla::dom::StreamBlobImpl::Create(...) /firefox/dom/file/StreamBlobImpl.cpp:76:43
    #4 0x753320b6bcfa in mozilla::dom::IPCBlobUtils::Deserialize(mozilla::dom::IPCBlob const&) /firefox/dom/file/ipc/IPCBlobUtils.cpp:53:16
    #5 0x753323dd79eb in mozilla::dom::indexedDB::(anonymous namespace)::Database::AllocPBackgroundIDBDatabaseFileParent(mozilla::dom::IPCBlob const&) /firefox/dom/indexedDB/ActorsParent.cpp:9685:30

SUMMARY: AddressSanitizer: heap-use-after-free

Suggested Fix

Add a manager check in TransactionBase::VerifyRequestParams(const ObjectStoreAddPutParams&) immediately after the null check:

case StructuredCloneFileBase::eBlob:
  if (NS_AUUF_OR_WARN_IF(!file)) {
    return false;
  }

  // Reject actors managed by a different Database
  if (NS_AUUF_OR_WARN_IF(file->Manager() !=
                         static_cast<const PBackgroundIDBDatabaseParent*>(
                             &GetDatabase()))) {
    return false;
  }
  break;

This ensures every referenced file actor belongs to the transaction's own database, preserving the single-connection-thread invariant that DatabaseFile::mBlobImpl depends on.

Attached file test.html —
Attached patch exploit.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → dom-core-security

(In reply to Olli Pettay [:smaug][bugs@pettay.fi] from comment #5)

Smells like a dup of https://bugzilla.mozilla.org/show_bug.cgi?id=2021894

The bot also highlighted this. But that bug is fixed and the image should be newer. I'll let the triage bot actually try to reproduce and then close.

Severity: -- → S2
Whiteboard: [prefs-checked]

Confirming this is a duplicate of bug 2021894. The manager-chain check that comment 0 describes as missing is already present in current trunk at dom/indexedDB/ActorsParent.cpp:10407-10412:

// Reject actors managed by a different Database
if (NS_AUUF_OR_WARN_IF(file->Manager() !=
                       static_cast<const PBackgroundIDBDatabaseParent*>(
                           &GetDatabase()))) {
  return false;
}

This landed in rev 4f187245066f on 2026-03-26, five days before this bug was filed. The code snippet quoted in comment 0 shows the pre-fix state, so the reporter ran against a stale source snapshot. The fix covers the only case (eBlob) that dereferences the file actor; all other FileAddInfo types already return false unconditionally, so there is no remaining variant.

No prefs required ([prefs-checked] added); the exploit patch modifies only content-process code, which is the standard compromised-renderer threat model.

This is an automated analysis result. If this result is incorrect please add a needinfo and feel free to correct the error.

above analysis looks good to me and as an author of the duplicate bug, I am resolving this as a duplicate.

Status: UNCONFIRMED → RESOLVED
Closed: 7 months ago
Duplicate of bug: 2021894
Resolution: --- → DUPLICATE
Assignee: nobody → hsingh
Group: dom-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: