Integer overflow in [@ VideoFrame::CreateBlackImage] causes heap buffer overflow write with crafted video dimensions
Categories
(Core :: WebRTC: Audio/Video, defect, P1)
Tracking
()
People
(Reporter: bugmon, Assigned: jhlin)
References
Details
(5 keywords, Whiteboard: [adv-main151+r][adv-esr140.11+r] )
Attachments
(4 files, 3 external files)
Summary
Integer overflow in VideoFrame::CreateBlackImage at /firefox/dom/media/VideoSegment.cpp:54 leads to a heap buffer overflow write. The function multiplies attacker-controlled width * height as signed 32-bit integers without overflow checking. When the product exceeds INT32_MAX, yLen becomes negative; the subsequent allocation size yLen + 2 * cbcrLen wraps back to a small positive value (63104 bytes), but memset(frame, 0x10, yLen) interprets the negative length as ~18 exabytes, writing 0x10 far past the allocation bounds.
Affected Code
File: /firefox/dom/media/VideoSegment.cpp, lines 53–62
gfx::IntSize cbcrSize((aSize.width + 1) / 2, (aSize.height + 1) / 2);
int yLen = aSize.width * aSize.height; // [1] signed int32 overflow
int cbcrLen = cbcrSize.width * cbcrSize.height;
// Generate a black image.
auto frame = MakeUnique<uint8_t[]>(yLen + 2 * cbcrLen); // [2] wraps to 63104
// Fill Y plane.
memset(frame.get(), 0x10, yLen); // [3] negative -> huge size_t
// Fill Cb/Cr planes.
memset(frame.get() + yLen, 0x80, 2 * cbcrLen); // [4] also UB: ptr + negative
aSize is the video frame's intrinsic (display) size, not the coded size. It flows from MP4 container metadata (tkhd box dimensions) through the pixel-aspect-ratio calculation and is never validated against a safe bound before reaching this function. With aSize = (11930640, 240):
yLen = 11930640 * 240 = 2,863,353,600overflows to-1,431,613,696cbcrLen = 5965320 * 120 = 715,838,400(positive)yLen + 2*cbcrLen = 63,104→ allocation succeedsmemsetwith length(size_t)(-1431613696) ≈ 1.8 × 10^19bytes
Call site at /firefox/dom/media/imagecapture/CaptureTask.cpp:143:
if (chunk.mFrame.GetForceBlack()) {
// Create a black image.
image = VideoFrame::CreateBlackImage(chunk.mFrame.GetIntrinsicSize()); // unvalidated
}
Correct Pattern
already_AddRefed<Image> VideoFrame::CreateBlackImage(
const gfx::IntSize& aSize) {
// Reject invalid or dangerously large sizes before any arithmetic.
if (aSize.width <= 0 || aSize.height <= 0 ||
!gfx::Factory::AllowedSurfaceSize(aSize)) {
return nullptr;
}
CheckedInt<size_t> yLen =
CheckedInt<size_t>(aSize.width) * aSize.height;
gfx::IntSize cbcrSize((aSize.width + 1) / 2, (aSize.height + 1) / 2);
CheckedInt<size_t> cbcrLen =
CheckedInt<size_t>(cbcrSize.width) * cbcrSize.height;
CheckedInt<size_t> total = yLen + cbcrLen * 2;
if (!total.isValid()) {
return nullptr;
}
auto frame = MakeUniqueFallible<uint8_t[]>(total.value());
if (!frame) {
return nullptr;
}
memset(frame.get(), 0x10, yLen.value());
memset(frame.get() + yLen.value(), 0x80, 2 * cbcrLen.value());
// ...
}
Exploit Chain
- Attacker crafts an MP4 file containing VP9 video with actual coded frames of
320×240but with thetkhdbox patched to declare dimensions49711×1. - Victim loads the page;
<video>element plays the MP4 via blob URL. - The MP4 demuxer reads
tkhddimensions;VPXChangeMonitorcomputes pixel aspect ratio(49711·240)/(1·320) ≈ 37283.25. - When VP9 frames decode at
320×240,ApplyPixelAspectRatioscales the display width:mDisplay = (11930640, 240). - Page calls
video.mozCaptureStream(), obtains the video track, and constructsnew ImageCapture(track). - Page calls
ic.takePhoto()→CaptureTaskis attached as aDirectMediaTrackListeneron theSourceMediaTrack. - Page sets
track.enabled = false→ the listener's disabled mode becomesSILENCE_BLACK→ incoming chunks getmForceBlack = true. - A decoded frame arrives via
DecodedStream::SendVideo→SourceMediaTrack::AppendData→NotifyDirectConsumers→CaptureTask::NotifyRealtimeTrackData. - Because
GetForceBlack()is true,CaptureTaskcallsVideoFrame::CreateBlackImage(chunk.mFrame.GetIntrinsicSize())with(11930640, 240). - Integer overflow → 63104-byte allocation →
memsetwith size-1431613696→ heap overflow write of0x10bytes.
Steps to Reproduce
- Set pref
dom.imagecapture.enabled = true(default:false— W3C draft API) - Set pref
layers.gpu-process.enabled = false(avoids an unrelated GPU-process crash from masking this one during testing) - Load the provided
test.html; crash typically fires within ~10 seconds
Security Impact
Severity: High (sec-high)
Attacker capability: Heap buffer overflow write with a fixed byte value (0x10). The allocation size (63104 bytes) and overflow length are both derived from attacker-controlled MP4 metadata, giving some control over heap layout. While the write value is fixed, a wild write of 0x10 across the heap can corrupt object vtables, length fields, and pointers — a well-known exploitation primitive. The overflow runs on a MediaDecoder thread pool worker in the content process.
Preconditions:
dom.imagecapture.enabledmust betrue. This pref defaults tofalse, significantly reducing real-world exposure for this specific path.- However,
VideoFrame::CreateBlackImagehas a second caller at/firefox/dom/media/encoder/VP8TrackEncoder.cpp:649(theMediaRecorderWebM/VP8 encoder), which is enabled by default. The same integer overflow exists regardless of which caller reaches it, so the underlying bug should be assessed independently of the ImageCapture pref. - No user interaction required beyond visiting the attacker's page (
<video muted>+mozCaptureStreamrequires no autoplay permission).
ASAN Report
==461177==ERROR: AddressSanitizer: negative-size-param: (size=-1431613696)
#0 0x5632034ff448 in __asan_memset _asan_rtl_:3
#1 0x743238aab506 in mozilla::VideoFrame::CreateBlackImage(mozilla::gfx::IntSizeTyped<mozilla::gfx::UnknownUnits> const&) /firefox/dom/media/VideoSegment.cpp:61:3
#2 0x743238d3cd77 in mozilla::CaptureTask::NotifyRealtimeTrackData(mozilla::MediaTrackGraph*, long, mozilla::MediaSegment const&) /firefox/dom/media/imagecapture/CaptureTask.cpp:145:15
#3 0x74323898ba48 in mozilla::DirectMediaTrackListener::NotifyRealtimeTrackDataAndApplyTrackDisabling(mozilla::MediaTrackGraph*, long, mozilla::MediaSegment&) /firefox/dom/media/MediaTrackListener.cpp:56:3
#4 0x74323898b61c in mozilla::SourceMediaTrack::NotifyDirectConsumers(mozilla::MediaSegment*) /firefox/dom/media/MediaTrackGraph.cpp:3015:8
#5 0x74323898b11e in mozilla::SourceMediaTrack::AppendData(mozilla::MediaSegment*, mozilla::MediaSegment*) /firefox/dom/media/MediaTrackGraph.cpp:2983:3
#6 0x74323907049c in mozilla::DecodedStream::SendVideo(nsMainThreadPtrHandle<nsIPrincipal> const&) /firefox/dom/media/mediasink/DecodedStream.cpp:1087:54
...
0x77325b1f0400 is located 0 bytes inside of 63104-byte region [0x77325b1f0400,0x77325b1ffa80)
allocated by thread T34 here:
#0 0x563203501524 in __interceptor_malloc _asan_rtl_:3
#1 0x563203556f35 in moz_xmalloc /firefox/memory/mozalloc/mozalloc.cpp:51:15
#2 0x743238aab4e3 in operator new[](unsigned long) /firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/cxxalloc.h:42:10
#3 0x743238aab4e3 in std::_MakeUniq<unsigned char []>::__array std::make_unique<unsigned char []>(unsigned long) /root/.mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/unique_ptr.h:968:30
#4 0x743238aab4e3 in auto mozilla::MakeUnique<unsigned char [], int>(int&&) /firefox/obj-x86_64-pc-linux-gnu/dist/include/mozilla/UniquePtr.h:104:10
#5 0x743238aab4e3 in mozilla::VideoFrame::CreateBlackImage(mozilla::gfx::IntSizeTyped<mozilla::gfx::UnknownUnits> const&) /firefox/dom/media/VideoSegment.cpp:59:16
#6 0x743238d3cd77 in mozilla::CaptureTask::NotifyRealtimeTrackData(mozilla::MediaTrackGraph*, long, mozilla::MediaSegment const&) /firefox/dom/media/imagecapture/CaptureTask.cpp:145:15
#7 0x74323898ba48 in mozilla::DirectMediaTrackListener::NotifyRealtimeTrackDataAndApplyTrackDisabling(mozilla::MediaTrackGraph*, long, mozilla::MediaSegment&) /firefox/dom/media/MediaTrackListener.cpp:56:3
#8 0x74323898b61c in mozilla::SourceMediaTrack::NotifyDirectConsumers(mozilla::MediaSegment*) /firefox/dom/media/MediaTrackGraph.cpp:3015:8
#9 0x74323898b11e in mozilla::SourceMediaTrack::AppendData(mozilla::MediaSegment*, mozilla::MediaSegment*) /firefox/dom/media/MediaTrackGraph.cpp:2983:3
#10 0x74323907049c in mozilla::DecodedStream::SendVideo(nsMainThreadPtrHandle<nsIPrincipal> const&) /firefox/dom/media/mediasink/DecodedStream.cpp:1087:54
...
SUMMARY: AddressSanitizer: negative-size-param (/firefox/obj-x86_64-pc-linux-gnu/dist/bin/firefox+0x175448)
Suggested Fix
In /firefox/dom/media/VideoSegment.cpp, VideoFrame::CreateBlackImage:
/* static */
already_AddRefed<Image> VideoFrame::CreateBlackImage(
const gfx::IntSize& aSize) {
// Reject sizes that would overflow or exceed surface limits.
if (aSize.width <= 0 || aSize.height <= 0 ||
!gfx::Factory::AllowedSurfaceSize(aSize)) {
NS_WARNING("CreateBlackImage: invalid size");
return nullptr;
}
RefPtr<ImageContainer> container = MakeAndAddRef<ImageContainer>(
ImageUsageType::BlackImage, ImageContainer::ASYNCHRONOUS);
RefPtr<PlanarYCbCrImage> image = container->CreatePlanarYCbCrImage();
if (!image) {
return nullptr;
}
gfx::IntSize cbcrSize((aSize.width + 1) / 2, (aSize.height + 1) / 2);
CheckedInt<size_t> yLen = CheckedInt<size_t>(aSize.width) * aSize.height;
CheckedInt<size_t> cbcrLen = CheckedInt<size_t>(cbcrSize.width) * cbcrSize.height;
CheckedInt<size_t> totalLen = yLen + cbcrLen * 2;
if (!totalLen.isValid()) {
return nullptr;
}
auto frame = MakeUniqueFallible<uint8_t[]>(totalLen.value());
if (!frame) {
return nullptr;
}
memset(frame.get(), 0x10, yLen.value());
memset(frame.get() + yLen.value(), 0x80, 2 * cbcrLen.value());
// ... (rest unchanged, using .value() for offsets)
}
Both callers (CaptureTask.cpp:143 and VP8TrackEncoder.cpp:649) already handle a nullptr return, so no caller changes are required.
| Reporter | ||
Comment 1•8 months ago
|
||
| Reporter | ||
Comment 2•8 months ago
|
||
| Reporter | ||
Comment 3•8 months ago
|
||
| Reporter | ||
Comment 4•8 months ago
|
||
Updated•8 months ago
|
| Assignee | ||
Comment 6•8 months ago
|
||
Test case verified and the fix will be uploaded later.
| Assignee | ||
Comment 7•8 months ago
|
||
Move to WebRTC component where ImageCapture belongs.
| Assignee | ||
Comment 8•8 months ago
|
||
Also, print a warning message when fail to get an image to encode.
Comment 9•8 months ago
|
||
Imagecapture is disabled so can't warrant a sec-high, but perhaps the path in VP8TrackEncoder is equally susceptible.
Updated•8 months ago
|
Comment 10•8 months ago
|
||
The bug has a release status flag that shows some version of Firefox is affected, thus it will be considered confirmed.
| Assignee | ||
Comment 11•8 months ago
|
||
| Assignee | ||
Comment 12•8 months ago
|
||
Comment on attachment 9554763 [details]
(secure)
Security Approval Request
- How easily could an exploit be constructed based on the patch?: Seems easy as demonstrated in the test case.
- Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: No
- Which branches (beta, release, and/or ESR) are affected by this flaw, and do the release status flags reflect this affected/unaffected state correctly?: all
- If not all supported branches, which bug introduced the flaw?: None
- Do you have backports for the affected branches?: No
- If not, how different, hard to create, and risky will they be?: Same patch should apply.
- How likely is this patch to cause regressions; how much testing does it need?: Unlikely. Test cases prepared and is ready to land later.
- Is the patch ready to land after security approval is given?: Yes
- Is Android affected?: Yes
| Assignee | ||
Comment 13•8 months ago
|
||
test.html still crashes in debug builds because of assertion, but there is no buffer overflow.
Updated•8 months ago
|
Comment 14•8 months ago
|
||
Comment on attachment 9554763 [details]
(secure)
sec-approval+ to land without the test, and request uplifts. I'll set a bug reminder to land the test in May (a release cycle after we ship the fix)
Updated•8 months ago
|
Comment 15•8 months ago
|
||
Comment 16•8 months ago
|
||
I'm going to drop this to sec-moderate. The combination of it not being clear whether this can even be exploited by default and the fact that you can only write the value 0x10 seems to limit the possibilities here.
Comment 17•8 months ago
|
||
Updated•8 months ago
|
Updated•8 months ago
|
Comment 18•8 months ago
|
||
The patch landed in nightly and beta is affected.
:jhlin, is this bug important enough to require an uplift?
- If yes, please nominate the patch for beta approval.
- See https://wiki.mozilla.org/Release_Management/Requesting_an_Uplift for documentation on how to request an uplift.
- If no, please set
status-firefox150towontfix.
For more information, please visit BugBot documentation.
| Assignee | ||
Updated•8 months ago
|
Updated•8 months ago
|
Comment 20•7 months ago
|
||
firefox-esr140 Uplift Approval Request
- User impact if declined/Reason for urgency: Content/tab crash when using particular video as the input data for image capture or media recorder API.
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: The change is simple the API affected is not widely used.
- String changes made/needed?: N/A
- Is Android affected?: yes
| Assignee | ||
Comment 21•7 months ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D288601
Updated•7 months ago
|
Updated•7 months ago
|
Updated•7 months ago
|
Updated•7 months ago
|
Comment 22•7 months ago
|
||
| uplift | ||
Comment 23•7 months ago
|
||
a month ago, dveditz placed a reminder on the bug using the whiteboard tag [reminder-test 2026-05-05] .
jhlin, please refer to the original comment to better understand the reason for the reminder.
Comment 24•6 months ago
|
||
Comment 25•6 months ago
|
||
Updated•6 months ago
|
Updated•6 months ago
|
Updated•6 months ago
|
Updated•3 months ago
|
Description
•