Heap-use-after-free in [@ MFMediaEngineChild::RecvNotifyError] due to cross-thread race between wrapper destruction and async OwnerDestroyed dispatch
Categories
(Core :: Audio/Video: Playback, defect)
Tracking
()
People
(Reporter: bugmon, Assigned: alwu, NeedInfo)
Details
(6 keywords, Whiteboard: [prefs-checked][adv-main151+r][adv-esr140.11+r])
Attachments
(3 files, 2 external files, 1 obsolete file)
|
932 bytes,
text/plain
|
Details | |
|
9.03 KB,
text/plain
|
Details | |
|
12.28 KB,
patch
|
Details | Diff | Splinter Review |
Summary
MFMediaEngineChild holds a raw non-owning pointer mOwner to its MFMediaEngineWrapper. The wrapper is owned by ExternalEngineStateMachine::mEngine (a UniquePtr) and is destroyed on the state machine's TaskQueue (OwnerThread()). The child actor's IPC Recv handlers run on a different thread — the RemoteMediaManagerChild manager thread. When the wrapper is destroyed, ~MFMediaEngineWrapper → OwnerDestroyed() dispatches an asynchronous task to the manager thread to null mOwner. The wrapper's memory is freed before that task runs. If a NotifyError IPC message was already queued on the manager thread before the null-out task was dispatched, RecvNotifyError (line 195) dereferences the freed wrapper.
Affected file: dom/media/ipc/MFMediaEngineChild.cpp, lines 189–197, 149–187, 199–215, 237–245, 274–281.
Affected Code
File: dom/media/ipc/MFMediaEngineChild.cpp, line 189
mozilla::ipc::IPCResult MFMediaEngineChild::RecvNotifyError(
const MediaResult& aError) {
AssertOnManagerThread(); // MOZ_ASSERT — no-op in release
if (mShutdown) { // mShutdown still false — Shutdown() task not yet run
return IPC_OK();
}
mOwner->NotifyError(aError); // UAF: mOwner points to freed MFMediaEngineWrapper
return IPC_OK();
}
The async teardown that creates the dangling window (line 263):
void MFMediaEngineChild::OwnerDestroyed() {
(void)ManagerThread()->Dispatch(NS_NewRunnableFunction(
"MFMediaEngineChild::OwnerDestroy", [self = RefPtr{this}, this] {
self->mOwner = nullptr; // Runs LATER on manager thread — wrapper already freed
if (CanSend()) {
MFMediaEngineChild::Send__delete__(this);
}
}));
}
And Shutdown is also async (line 335):
void MFMediaEngineWrapper::Shutdown() {
(void)ManagerThread()->Dispatch(
NS_NewRunnableFunction("MFMediaEngineWrapper::Shutdown",
[engine = mEngine] { engine->Shutdown(); }));
}
The raw pointer (MFMediaEngineChild.h line 65):
// Only modified on the manager thread.
MFMediaEngineWrapper* MOZ_NON_OWNING_REF mOwner;
The comment is accurate — mOwner IS only modified on the manager thread — but the invariant it implies ("therefore safe to read on the manager thread") is broken because the pointee is freed on a different thread before the modification task runs.
Other affected Recv handlers with the same defect:
RecvNotifyEvent(line 149) —mOwner->NotifyEvent(...)with onlymShutdownguardRecvNotifyHardwareReset(line 199) —mOwner->NotifyHardwareReset()RecvNotifyWaitingForKey(line 208) —mOwner->NotifyWaitingForKey()RecvNotifyResizing(line 237) —mOwner->NotifyResizing(...)IPDLActorDestroyed(line 274) —mOwner->NotifyError(...)when!mShutdown
Note: RecvRequestSample (line 124) checks if (!mOwner || mShutdown), but this does not fix the UAF — during the race window mOwner is dangling, not null. The null check only helps in a different ordering where the OwnerDestroyed task ran but Shutdown didn't.
Correct Pattern
The null-out must happen-before any possible IPC processing with the freed wrapper. Two viable approaches:
Option A — Synchronous null-out when already on manager thread, else dispatch+wait:
void MFMediaEngineChild::OwnerDestroyed() {
if (ManagerThread()->IsOnCurrentThread()) {
mOwner = nullptr;
if (CanSend()) { Send__delete__(this); }
return;
}
// Synchronously block until mOwner is nulled — wrapper memory stays valid until we return
SyncRunnable::DispatchToThread(ManagerThread(),
NS_NewRunnableFunction("MFMediaEngineChild::OwnerDestroy",
[self = RefPtr{this}] {
self->mOwner = nullptr;
if (self->CanSend()) { Send__delete__(self); }
}));
}
And add if (!mOwner) return IPC_OK(); to all Recv handlers.
Option B — WeakPtr: Make MFMediaEngineWrapper support WeakPtr, store WeakPtr<MFMediaEngineWrapper> mOwner, and check if (RefPtr<MFMediaEngineWrapper> owner = mOwner.get()) in each handler. This requires MFMediaEngineWrapper to be refcounted and SupportsWeakPtr.
Exploit Chain
-
Windows content process plays media using the Media Foundation engine.
ExternalEngineStateMachinecreates anMFMediaEngineWrapper, which creates anMFMediaEngineChildIPC actor bound to theRemoteMediaManagerChildmanager thread. IPC channel is established to theMF_MEDIA_ENGINE_CDMutility process. -
Attacker compromises the
MF_MEDIA_ENGINE_CDMutility process (e.g., via a vulnerability in Microsoft's Media Foundation CDM code, which runs in this process). -
Attacker sends
NotifyHardwareResetover IPC. On the content side,MFMediaEngineChild::RecvNotifyHardwareReset(manager thread) →mOwner->NotifyHardwareReset()→ dispatches toOwnerThread()(state machine TaskQueue). SeeExternalEngineStateMachine::NotifyHardwareReset. -
TaskQueue runs
RecoverFromHardwareReset: callsmEngine->Shutdown()(dispatches async task S to manager thread), thenInitEngine()→mEngine.reset(new MFMediaEngineWrapper(...)).UniquePtr::resetdestroys the old wrapper on the TaskQueue.~MFMediaEngineWrapper→OwnerDestroyed()dispatches async task N to manager thread.reset()returns. Old wrapper memory is freed. The oldMFMediaEngineChildis kept alive bymIPDLSelfRef. -
Meanwhile, attacker sends
NotifyErrorover IPC. If this message is received by the IO thread and dispatched to the manager thread's queue before step 4 dispatched task S, the manager thread queue is:[... RecvNotifyError, S, N]. -
Manager thread processes
RecvNotifyError:mShutdownis stillfalse(task S hasn't run).mOwneris still the old dangling pointer (task N hasn't run). Line 195 callsmOwner->NotifyError(aError)→ entersMFMediaEngineWrapper::NotifyErrorwiththispointing into freed memory → readsthis->mOwner(ExternalEngineStateMachine*at offset 8) → heap-use-after-free.
Alternative trigger: RecoverFromCDMProcessCrashIfNeeded (line 1321) calls InitEngine() without calling mEngine->Shutdown() first — so mShutdown is never set for the old child, making the window even wider.
Alternative trigger: Normal page teardown — state machine refcount hits zero, destructor runs mEngine's destructor on whatever thread held the last reference. Same race.
IPC Path
- IPDL protocol:
PMFMediaEngine, managed byPRemoteMediaManager - IPDL parent side (attacker):
MFMediaEngineParent, runs in theMF_MEDIA_ENGINE_CDMutility process (seeMFMediaEngineParent.cppline 95–96:MOZ_ASSERT(GetCurrentSandboxingKind() == ipc::SandboxingKind::MF_MEDIA_ENGINE_CDM)) - IPDL child side (victim):
MFMediaEngineChild, runs in the content process - Message:
async NotifyError(MediaResult error)— achild:message (parent → child direction), line 57 - Also exploitable:
NotifyEvent,NotifyHardwareReset,NotifyWaitingForKey,NotifyResizing(allchild:messages with the same missing guard)
Steps to Reproduce
This code is gated by MOZ_WMF_MEDIA_ENGINE and only compiles on Windows. The gtest-based demonstration on Linux requires applying the build-scaffolding patch (source_patch.diff) which wraps Windows-only code in #ifdef XP_WIN, stubs the MF_MEDIA_ENGINE_EVENT enum, and moves PMFMediaEngine.ipdl + MFMediaEngineChild.cpp into the unconditional build. Then:
cd /firefox
git apply source_patch.diff
MOZCONFIG=/app/mozconfigs/mozconfig.asan.fuzzing ./mach build
MOZCONFIG=/app/mozconfigs/mozconfig.asan.fuzzing ./mach gtest 'MFMediaEngineChild.*'
On Windows, a real reproduction would require instrumenting the MF_MEDIA_ENGINE_CDM utility process to send NotifyHardwareReset followed rapidly by NotifyError while loading the manager thread to widen the race window.
Security Impact
Severity: Moderate (sec-moderate)
- Attacker capability: A compromised
MF_MEDIA_ENGINE_CDMutility process can trigger a use-after-free of a 32-byte heap allocation in the content process. The first dereference reads anExternalEngineStateMachine*from freed memory; if the attacker controls the reclaimed 32-byte slot, this becomes an arbitrary-pointer call target via the subsequent->NotifyError()(which dispatches a runnable viaOwnerThread()->Dispatch, reading through the attacker-controlled pointer). - Preconditions:
- Windows only (
MOZ_WMF_MEDIA_ENGINE) - Attacker must first compromise the
MF_MEDIA_ENGINE_CDMutility process — this process runs Microsoft's Media Foundation CDM code, which has its own attack surface - Attacker must win a cross-thread race (feasible: spam
NotifyErrormessages during engine recovery)
- Windows only (
- Trust boundary: Utility process → content process. The
MF_MEDIA_ENGINE_CDMutility process has a tighter sandbox than the content process on Windows (Win32k lockdown, restricted token). This is a sandbox escape, though not the highest-value one (content → parent would be sec-high/critical). - Exploitability: The 32-byte allocation size is a common bucket; heap grooming from the utility process via IPC-controlled allocations is plausible. The race window is tight but expandable by flooding the manager thread with cheap IPC messages.
ASAN Report
==355430==ERROR: AddressSanitizer: heap-use-after-free on address 0x7abc670e24e8 at pc 0x7a8c443cd8ba bp 0x7ffd430f7c30 sp 0x7ffd430f7c28
READ of size 8 at 0x7abc670e24e8 thread T0
#0 0x7a8c443cd8b9 in mozilla::MFMediaEngineWrapper::NotifyError(mozilla::MediaResult const&) /firefox/dom/media/ipc/MFMediaEngineChild.cpp:404:3
#1 0x7a8c443cd605 in mozilla::MFMediaEngineChild::RecvNotifyError(mozilla::MediaResult const&) /firefox/dom/media/ipc/MFMediaEngineChild.cpp:195:11
#2 0x7a8c3888b7ec in MFMediaEngineChild_UseAfterFreeOnOwner_Test::TestBody() /firefox/dom/media/gtest/TestMFMediaEngineChild.cpp:85:10
#3 0x7a8c37f27047 in testing::Test::Run() /firefox/third_party/googletest/googletest/src/gtest.cc:2746:5
#4 0x7a8c37f28dde in testing::TestInfo::Run() /firefox/third_party/googletest/googletest/src/gtest.cc:2892:11
#5 0x7a8c37f2a9f2 in testing::TestSuite::Run() /firefox/third_party/googletest/googletest/src/gtest.cc:3070:30
#6 0x7a8c37f4d114 in testing::internal::UnitTestImpl::RunAllTests() /firefox/third_party/googletest/googletest/src/gtest.cc:6062:44
#7 0x7a8c37f4bdba in testing::UnitTest::Run() /firefox/third_party/googletest/googletest/src/gtest.cc:5602:10
#8 0x7a8c37ed54b2 in mozilla::RunGTestFunc(int*, char**) /firefox/testing/gtest/mozilla/GTestRunner.cpp:260:10
#9 0x7a8c4a33db71 in XREMain::XRE_mainStartup(bool*) /firefox/toolkit/xre/nsAppRunner.cpp:4816:16
#10 0x7a8c4a348cef in XREMain::XRE_main(int, char**, mozilla::BootstrapConfig const&) /firefox/toolkit/xre/nsAppRunner.cpp:6139:12
#11 0x7a8c4a349d23 in XRE_main(int, char**, mozilla::BootstrapConfig const&) /firefox/toolkit/xre/nsAppRunner.cpp:6237:21
#12 0x558e4bf662aa in main /firefox/browser/app/nsBrowserApp.cpp:534:16
0x7abc670e24e8 is located 8 bytes inside of 32-byte region [0x7abc670e24e0,0x7abc670e2500)
freed by thread T0 here:
#0 0x558e4bf206f6 in free /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:51:3
#1 0x7a8c3888b776 in operator delete(void*) /firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:64:10
#2 0x7a8c3888b776 in MFMediaEngineChild_UseAfterFreeOnOwner_Test::TestBody() /firefox/dom/media/gtest/TestMFMediaEngineChild.cpp:75:3
previously allocated by thread T0 here:
#0 0x558e4bf20994 in malloc /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:67:3
#1 0x558e4bf763a5 in moz_xmalloc /firefox/memory/mozalloc/mozalloc.cpp:48:15
#2 0x7a8c3888b716 in operator new(unsigned long) /firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:33:10
#3 0x7a8c3888b716 in MFMediaEngineChild_UseAfterFreeOnOwner_Test::TestBody() /firefox/dom/media/gtest/TestMFMediaEngineChild.cpp:61:7
SUMMARY: AddressSanitizer: heap-use-after-free /firefox/dom/media/ipc/MFMediaEngineChild.cpp:404:3 in mozilla::MFMediaEngineWrapper::NotifyError(mozilla::MediaResult const&)
Note: Frame #0 line number (404) is a debug-info artifact from WLOG macro expansion. The crash is in MFMediaEngineWrapper::NotifyError (lines 419–423) reading this->mOwner at offset 8 of the freed 32-byte wrapper.
Suggested Fix
Make the null-out synchronous so the wrapper memory stays valid until mOwner is cleared, and add !mOwner guards to all Recv handlers:
// MFMediaEngineChild.cpp
void MFMediaEngineChild::OwnerDestroyed() {
// CRITICAL: mOwner must be nulled BEFORE the wrapper's memory is freed.
// The wrapper destructor runs on the state machine TaskQueue, but IPC Recv
// handlers run on the manager thread. We must synchronously block here so
// that by the time ~MFMediaEngineWrapper returns, no Recv handler can see
// the dangling pointer.
RefPtr<MFMediaEngineChild> self = this;
auto clearOwner = [self]() {
self->mOwner = nullptr;
if (self->CanSend()) {
MFMediaEngineChild::Send__delete__(self);
}
};
if (ManagerThread()->IsOnCurrentThread()) {
clearOwner();
} else {
mozilla::SyncRunnable::DispatchToThread(
ManagerThread(),
NS_NewRunnableFunction("MFMediaEngineChild::OwnerDestroy",
std::move(clearOwner)));
}
}
// And add to RecvNotifyError, RecvNotifyEvent, RecvNotifyHardwareReset,
// RecvNotifyWaitingForKey, RecvNotifyResizing, IPDLActorDestroyed:
mozilla::ipc::IPCResult MFMediaEngineChild::RecvNotifyError(
const MediaResult& aError) {
AssertOnManagerThread();
if (!mOwner || mShutdown) { // Now safe: mOwner is nulled synchronously
return IPC_OK();
}
mOwner->NotifyError(aError);
return IPC_OK();
}
Important: Adding !mOwner checks alone (without the synchronous OwnerDestroyed) is insufficient — during the current race window mOwner is dangling, not null.
| Reporter | ||
Comment 1•8 months ago
|
||
| Reporter | ||
Comment 2•8 months ago
|
||
| Reporter | ||
Comment 3•8 months ago
|
||
Updated•8 months ago
|
Updated•8 months ago
|
Updated•8 months ago
|
Comment 5•8 months ago
|
||
Prefs: Supported config — no prefs required; media.wmf.media-engine.enabled defaults to 2 (encrypted playback) on Windows.
sec-moderate looks correct: real heap UAF (not NULL), no unusual user action, attack is compromised CDM utility → content (lower→higher privilege, explicitly in-scope). The attached source_patch.diff is Linux build scaffolding for the gtest demo only — the vulnerable code ships unpatched on Windows.
Fix: Not posting an auto-generated patch — MFMediaEngineChild.cpp only compiles under MOZ_WMF_MEDIA_ENGINE (Windows) and the race is IPC-level (compromised utility process), so it's not reproducible on the Linux ASAN harness. Needs manual attention. Source verified: the async OwnerDestroyed() dispatch at line 263 and the unguarded mOwner-> derefs in the Recv handlers match comment 0 exactly on current trunk.
This is an automated analysis result. If this result is incorrect please add a needinfo and feel free to correct the error.
| Assignee | ||
Updated•8 months ago
|
| Assignee | ||
Comment 7•8 months ago
|
||
Updated•7 months ago
|
Comment 9•7 months ago
|
||
Updated•7 months ago
|
Updated•7 months ago
|
| Assignee | ||
Comment 11•7 months ago
|
||
Updated•7 months ago
|
Comment 12•7 months ago
|
||
firefox-esr140 Uplift Approval Request
- User impact if declined/Reason for urgency: security fix.
- Code covered by automated testing?: no
- Fix verified in Nightly?: no
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: No behavior, structural changes.
- String changes made/needed?: No
- Is Android affected?: no
| Assignee | ||
Comment 13•7 months ago
|
||
Updated•7 months ago
|
Updated•7 months ago
|
Updated•7 months ago
|
Comment 14•7 months ago
|
||
| uplift | ||
Updated•6 months ago
|
Updated•6 months ago
|
Updated•3 months ago
|
Description
•