Missing ArrayBuffer detach in RTCEncodedAudioFrame/RTCEncodedVideoFrame destructor leads to use-after-free
A destructor ordering bug in Firefox's WebRTC Encoded Transform frame wrappers meant the ArrayBuffer aliasing native frame memory was never detached before that memory was freed. JavaScript code holding references to the frame's data buffer could then read or write freed heap memory, which AddressSanitizer detected as a heap-buffer-overflow.
- Fixed in 24 days
- Paid in 31 days
- Disclosed after 24 days
Created attachment 9550863 asan.log
Missing ArrayBuffer detach in RTCEncodedAudioFrame/RTCEncodedVideoFrame destructor leads to use-after-free
Summary
The WebRTC Encoded Transform API exposes encoded frame payloads as ArrayBuffers backed by native memory. A destructor ordering issue in RTCEncodedAudioFrame and RTCEncodedVideoFrame prevents the base class from detaching the ArrayBuffer before the backing store is freed, leaving a dangling reference accessible from JavaScript. This affects all platforms. The preference media.peerconnection.scripttransform.enabled must be true, which is enabled by default in Nightly builds.
Affected Versions
The vulnerable code was introduced in commit 837490d82a91 on 2023-07-20 (Bug 1631263: Implement RTCRtpScriptTransform). That commit added RTCEncodedAudioFrame, RTCEncodedVideoFrame, and the base class RTCEncodedFrameBase with the destructor ordering issue described below. All Firefox versions from that point onward are affected. Testing was performed on mozilla-central commit 9deea8b0a7f3 (Firefox 150.0a1, Nightly build 20260307210637).
Root Cause
The constructor in RTCEncodedFrameBase (dom/media/webrtc/jsapi/RTCEncodedFrameBase.cpp, lines 48 through 50) creates an ArrayBuffer that directly aliases the webrtc frame payload memory:
mData = JS::NewArrayBufferWithUserOwnedContents(
jsapi.cx(), mState.mFrame->GetData().size(),
(void*)(mState.mFrame->GetData().data()));
SpiderMonkey's contract for NewArrayBufferWithUserOwnedContents requires the caller to invoke JS::DetachArrayBuffer before freeing the backing store. The base class destructor attempts to honor this contract by calling its DetachData() method:
RTCEncodedFrameBase::~RTCEncodedFrameBase() { DetachData(); }
void RTCEncodedFrameBase::DetachData() {
if (mGlobal) {
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(mGlobal))) {
return;
}
JS::Rooted<JSObject*> rootedData(jsapi.cx(), mData);
if (rootedData) {
JS::DetachArrayBuffer(jsapi.cx(), rootedData);
}
}
}
However, both derived class destructors clear mData before the base class destructor runs:
RTCEncodedAudioFrame::~RTCEncodedAudioFrame() {
mData = nullptr;
mozilla::DropJSObjects(this);
}
RTCEncodedVideoFrame::~RTCEncodedVideoFrame() {
mData = nullptr;
mozilla::DropJSObjects(this);
}
C++ destruction order is derived first, so the derived destructor sets mData to nullptr, and then the base destructor's DetachData() finds rootedData is null and skips the detach call entirely. After that, the RTCEncodedFrameState destructor frees the webrtc frame payload through unique_ptr destruction. Any JavaScript ArrayBuffer references obtained from frame.data before the object was collected now point to freed memory.
The cycle collector UNLINK path (RTCEncodedFrameBase.cpp, lines 18 through 23) is safe because it calls DetachData() before clearing mData. The problem occurs only when objects are destroyed via the C++ destructor chain triggered by GC or CC deletion, where the ArrayBuffer is never detached.
Reproduce
Testing was performed on Firefox Nightly 150.0a1 (BuildID 20260307210637) from mozilla-central revision 9deea8b0a7f33f6410ce3ee1b718a9bc3b394ea5, downloaded via fuzzfetch as an ASAN-optimized build for Linux x86-64. No source modifications are required.
The proof of concept consists of two files: poc.html and poc_worker.js. The poc.html creates a loopback WebRTC connection using an AudioContext oscillator as the media source and attaches an RTCRtpScriptTransform to the sender. The poc_worker.js handles the rtctransform event, extracts frame.data ArrayBuffer references and saves them to a global array without retaining the frame objects themselves. After accumulating several references, it forces garbage collection through allocation pressure to trigger destruction of the frame wrappers. Once the frame wrappers are collected, the saved ArrayBuffer references point to freed payload memory. Subsequent reads or writes through these ArrayBuffers access freed heap memory, detected by AddressSanitizer.
To reproduce, serve the PoC files over HTTP from the directory containing poc.html and poc_worker.js:
python3 -m http.server 8080
Create a temporary profile with the preferences required to trigger the vulnerability. The media.navigator.streams.fake preference provides a synthetic audio source so that a real microphone is not needed. The media.autoplay.default preference set to 0 allows the AudioContext to start without a user gesture. The javascript.options.mem.gc_allocation_threshold_mb preference lowers the GC threshold so that the allocation pressure in the worker triggers garbage collection sooner, making the dangling ArrayBuffer references observable more quickly.
TMPDIR=$(mktemp -d)
cat > "$TMPDIR/user.js" << 'PREFS'
user_pref("media.navigator.streams.fake", true);
user_pref("media.autoplay.default", 0);
user_pref("javascript.options.mem.gc_allocation_threshold_mb", 1);
PREFS
Then launch Firefox with quarantine disabled to force immediate memory reuse:
ASAN_OPTIONS="detect_leaks=0,quarantine_size_mb=0" \
./firefox --no-remote \
--profile "$TMPDIR" \
http://localhost:8080/poc.html
The crash typically appears within 10 to 15 seconds.
The following ASAN output was observed from the fuzzfetch build (full log in asan.log):
==2717227==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c50a573e3c0 at pc 0x558d5bb9c35b bp 0x7b6068ffb490 sp 0x7b6068ffac50
READ of size 8 at 0x7c50a573e3c0 thread T22
#0 __asan_memcpy
#1 TypedArrayBitwiseSlice js/src/vm/TypedArrayObject.cpp:3930:7
#2 TypedArray_slice js/src/vm/TypedArrayObject.cpp:4118:9
#3 CallNonGenericMethod<&IsTypedArrayObject, &TypedArray_slice>
#4 TypedArray_slice js/src/vm/TypedArrayObject.cpp:4146:10
...
0x7c50a573e3c0 is located 790 bytes after 170-byte region [0x7c50a573e000,0x7c50a573e0aa)
SUMMARY: AddressSanitizer: heap-buffer-overflow js/src/vm/TypedArrayObject.cpp:3930:7 in TypedArrayBitwiseSlice
The shadow byte at the access address is 0xfa (heap left redzone), confirming the ArrayBuffer's backing store was freed and the memory has been reused by a different allocation.
Fix
The root cause is the derived class destructors clearing mData before the base class destructor can use it for DetachArrayBuffer. The fix should move the DetachData() call into the derived destructors before clearing mData, matching the order used by the cycle collector UNLINK path.
--- a/dom/media/webrtc/jsapi/RTCEncodedAudioFrame.cpp
+++ b/dom/media/webrtc/jsapi/RTCEncodedAudioFrame.cpp
@@ -90,8 +90,9 @@ RTCEncodedAudioFrame::RTCEncodedAudioFrame(nsIGlobalObject* aGlobal,
}
RTCEncodedAudioFrame::~RTCEncodedAudioFrame() {
- // Clear JS::Heap<> members before unregistering as a script holder,
- // so their destructors don't barrier against a finalized JS object.
+ // Detach the ArrayBuffer before clearing mData, so the base class
+ // DetachData() can still see it. This matches the CC UNLINK order.
+ DetachData();
mData = nullptr; // from RTCEncodedFrameBase (protected)
// Base class needs this, but can't do it itself because of an assertion in
// the cycle-collector.
--- a/dom/media/webrtc/jsapi/RTCEncodedVideoFrame.cpp
+++ b/dom/media/webrtc/jsapi/RTCEncodedVideoFrame.cpp
@@ -115,8 +115,9 @@ void RTCEncodedVideoFrame::InitMetadata() {
}
RTCEncodedVideoFrame::~RTCEncodedVideoFrame() {
- // Clear JS::Heap<> members before unregistering as a script holder,
- // so their destructors don't barrier against a finalized JS object.
+ // Detach the ArrayBuffer before clearing mData, so the base class
+ // DetachData() can still see it. This matches the CC UNLINK order.
+ DetachData();
mData = nullptr; // from RTCEncodedFrameBase (protected)
// Base class needs this, but can't do it itself because of an assertion in
// the cycle-collector.
This ensures the ArrayBuffer is detached before both mData is cleared and the backing store is freed, regardless of whether destruction happens through the cycle collector UNLINK path or the C++ destructor chain.
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
Root cause
The derived frame destructors set the ArrayBuffer member to null before the base class destructor could call DetachArrayBuffer, so the detach was skipped and the backing store was freed while still referenced.
Impact
A web page could read or write freed heap memory through the dangling buffer, which the report demonstrates as a heap-buffer-overflow crash under ASAN; exploitation beyond that is not shown, and the feature requires the script transform preference, enabled by default only in Nightly.
What the attacker needed
Nightly build with script transform enabled; Victim visits a malicious page; Page runs a worker script
The fix
The fix should call DetachData() in the derived destructors before clearing mData, matching the cycle collector UNLINK order.
How it was found
- Serve the two proof-of-concept files over local HTTP.
- Create a temporary profile enabling fake media streams, autoplay, and a low GC allocation threshold.
- Launch the ASAN build with quarantine disabled and open the page.
- The page sets up a loopback WebRTC connection with an oscillator source and attaches a script transform to the sender.
- The worker saves frame.data ArrayBuffers without keeping the frame objects, then forces garbage collection via allocation pressure.
- Reading the saved buffers after collection touches freed memory, which ASAN reports within 10 to 15 seconds.
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.
Firefox lets web pages handle raw audio and video frames through a special buffer object. That buffer pointed at memory owned by the browser, and when the frame object was cleaned up the browser freed the memory without first telling JavaScript the buffer was dead. A page that kept a reference to the buffer could then read or write memory that no longer belonged to it, which is a use-after-free.
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.