Skip to content
Memory corruption Disclosed report Advanced CVE-2026-4697

Heap buffer overflow in AudioData::CopyTo due to missing CheckedInt::isValid() check in WebCodecs

The AudioData::CopyTo method in Firefox's WebCodecs implementation computes a copy length using a 32-bit checked integer but fails to verify that the multiplication did not overflow, so a large frame count wraps the length to a small value that passes the destination bounds check. An attacker can then trigger a heap buffer overflow by copying far more data than the destination buffer holds, crashing the tab and potentially enabling remote code execution on release builds.

TheKng Follow

Originally disclosed on Mozilla

  • Fixed in 2 days
  • Paid in 16 days
  • Disclosed after 2 days
Text
Created attachment 9549114
poc_audiodata_overflow.html

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Steps to reproduce:

Heap buffer overflow in AudioData::CopyTo — RCE from web content on Release builds

Affected: All Firefox desktop Release builds with WebCodecs (130+). NOT affected on Nightly/Debug (MOZ_DIAGNOSTIC_ASSERT catches it).

File: dom/media/webcodecs/AudioData.cpp, line 571

The CopyTo method uses CheckedInt<uint32_t> to compute the copy length but calls .value() WITHOUT checking .isValid() first. In Release/Late Beta builds, MOZ_DIAGNOSTIC_ASSERT is a no-op (moz.configure:150-158), so .value() silently returns the truncated overflow result.

Compare with AllocationSize() at line 392 which does it CORRECTLY with CheckedInt<size_t> and .isValid() check.

Vulnerable code (line 568-571):

'''
uint32_t bytesPerSample = BytesPerSamples(destFormat.value());
CheckedInt<uint32_t> copyLength = bytesPerSample;
copyLength *= copyElementCount;       // uint64_t — overflows uint32
if (copyLength.value() > destLength)  // BUG: no .isValid() check
'''

Correct code in AllocationSize (line 392-399):

'''
CheckedInt<size_t> bytesPerSample = BytesPerSamples(destFormat.ref());
auto res = bytesPerSample * copyElementCount;
if (res.isValid()) { return res.value(); }  // checks isValid() first!
aRv.ThrowRangeError("Allocation size too large");
'''

Steps to reproduce:

1. Create AudioData with format='u8', numberOfFrames=0x20000001, numberOfChannels=2 (~1GB source buffer)
2. Call copyTo(16-byte ArrayBuffer, { planeIndex: 0, format: 'f32' })
3. copyElementCount = 0x40000002 (uint64_t), bytesPerSample = 4 (f32)
4. copyLength = CheckedInt<uint32_t>(4) * 0x40000002 = 0x100000008 — overflows uint32 to 8
5. In Release: .value() returns 8 (MOZ_DIAGNOSTIC_ASSERT is no-op)
6. Bounds check: 8 > 16 = false — CHECK PASSES
7. CopySamples (line 412) calls ConvertAudioSamples with RAW POINTERS via aDest.data() — bypasses Span MOZ_RELEASE_ASSERT bounds check
8. ConvertAudioSamples (AudioSampleFormat.h:242) writes 0x40000002 float values (~4GB) into 16-byte heap buffer
9. HEAP BUFFER OVERFLOW — tab crashes

Minimal PoC:

'''javascript
const src = new ArrayBuffer(0x40000002);
new Uint8Array(src).fill(0x41);
const ad = new AudioData({
  format: 'u8',
  sampleRate: 48000,
  numberOfFrames: 0x20000001,
  numberOfChannels: 2,
  timestamp: 0,
  data: src,
  transfer: [src]
});
const dest = new ArrayBuffer(16);
await ad.copyTo(dest, { planeIndex: 0, format: 'f32' });
// TAB CRASHES HERE
'''

Why ConvertAudioSamples bypasses Span bounds checking:

Span::operator[] has MOZ_RELEASE_ASSERT (mfbt/Span.h:764), but the interleaved-to-interleaved path at CopySamples line 412 calls:
  ConvertAudioSamples(aSource.data() + offset, aDest.data(), count)
This uses .data() to get raw pointers, completely bypassing Span bounds.
ConvertAudioSamples (AudioSampleFormat.h:242) is a simple loop with raw pointer indexing: aTo[i] = ConvertAudioSample<To>(aFrom[i])

Confirmed crash on:
Firefox Release x86_64-pc-mingw32, built from mozilla-release rev eff69d257e44ce2a29f947baf876e992af8bb9b0

Suggested fix (line 568-571):

'''cpp
// Replace CheckedInt<uint32_t> with CheckedInt<size_t> and add isValid() check:
CheckedInt<size_t> copyLength = BytesPerSamples(destFormat.value());
copyLength *= copyElementCount;
if (!copyLength.isValid() || copyLength.value() > destLength) {
    aRv.ThrowRangeError("destination buffer too small for copy"_ns);
    return;
}
'''

Impact:
- Heap buffer overflow with attacker-controlled size and partially-controlled data
- Reachable from ANY web page (WebCodecs enabled by default on desktop, no SecureContext required)
- Release-only: masked by MOZ_DIAGNOSTIC_ASSERT in Nightly/Debug
- RCE potential via heap object corruption



Actual results:


Tab crashes immediately. Heap buffer overflow: ConvertAudioSamples (AudioSampleFormat.h:242) writes ~4GB of float data through raw pointers (aTo[i]) into a 16-byte heap buffer. The overflow bypasses Span bounds checking because CopySamples line 412 uses aDest.data() to extract raw pointers before passing them to ConvertAudioSamples.



Expected results:

copyTo should check CheckedInt::isValid() before calling .value() and throw a RangeError on overflow, exactly like AllocationSize() already does correctly at line 392-399 of the same file. The fix is to use CheckedInt<size_t> instead of CheckedInt<uint32_t> and add the isValid() check before the bounds comparison.

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

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 code uses a 32-bit checked integer for the copy length but calls .value() without first checking .isValid(), so an overflow silently truncates the length and the subsequent bounds check compares the wrong value.

Impact

A web page can trigger a heap buffer overflow with an attacker-controlled size, crashing the tab and potentially leading to remote code execution on release builds; nightly and debug builds are protected by a diagnostic assertion.

What the attacker needed

Firefox release build with WebCodecs; Victim visits a malicious web page; No special permissions required

The fix

The code should use a size_t checked integer and check isValid() before comparing the copy length to the destination length, throwing a RangeError on overflow.

How it was found

  1. Create an AudioData object with a very large number of frames and two channels, using a roughly 1 GB source buffer.
  2. Call copyTo with a 16-byte destination ArrayBuffer, plane index 0, and format f32.
  3. The copy length calculation overflows a 32-bit integer and wraps to a small value.
  4. The bounds check passes because the wrapped value is smaller than the destination length.
  5. The copy routine writes the full large number of float values through raw pointers into the 16-byte buffer, causing a heap overflow and tab crash.

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.

A checked integer is a number type that records when a calculation goes out of range, but the code here ignores that warning and uses the wrapped-around result. Because the wrapped number looks small, a size check passes, and the program then writes a huge amount of data into a tiny memory buffer, corrupting memory. This can crash the browser tab and, in principle, let an attacker run their own code.

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.

More by TheKng

View all

1 more disclosed report, 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.