Closed Bug 2024441 Opened 8 months ago Closed 8 months ago

Integer overflow in [@ VideoFrame::CreateBlackImage] causes heap buffer overflow write with crafted video dimensions

Categories

(Core :: WebRTC: Audio/Video, defect, P1)

defect

Tracking

()

RESOLVED FIXED
151 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox-esr140 151+ fixed
firefox148 --- wontfix
firefox149 --- wontfix
firefox150 --- wontfix
firefox151 + fixed

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,600 overflows to -1,431,613,696
  • cbcrLen = 5965320 * 120 = 715,838,400 (positive)
  • yLen + 2*cbcrLen = 63,104 → allocation succeeds
  • memset with length (size_t)(-1431613696) ≈ 1.8 × 10^19 bytes

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

  1. Attacker crafts an MP4 file containing VP9 video with actual coded frames of 320×240 but with the tkhd box patched to declare dimensions 49711×1.
  2. Victim loads the page; <video> element plays the MP4 via blob URL.
  3. The MP4 demuxer reads tkhd dimensions; VPXChangeMonitor computes pixel aspect ratio (49711·240)/(1·320) ≈ 37283.25.
  4. When VP9 frames decode at 320×240, ApplyPixelAspectRatio scales the display width: mDisplay = (11930640, 240).
  5. Page calls video.mozCaptureStream(), obtains the video track, and constructs new ImageCapture(track).
  6. Page calls ic.takePhoto() → CaptureTask is attached as a DirectMediaTrackListener on the SourceMediaTrack.
  7. Page sets track.enabled = false → the listener's disabled mode becomes SILENCE_BLACK → incoming chunks get mForceBlack = true.
  8. A decoded frame arrives via DecodedStream::SendVideo → SourceMediaTrack::AppendData → NotifyDirectConsumers → CaptureTask::NotifyRealtimeTrackData.
  9. Because GetForceBlack() is true, CaptureTask calls VideoFrame::CreateBlackImage(chunk.mFrame.GetIntrinsicSize()) with (11930640, 240).
  10. Integer overflow → 63104-byte allocation → memset with size -1431613696 → heap overflow write of 0x10 bytes.

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.enabled must be true. This pref defaults to false, significantly reducing real-world exposure for this specific path.
  • However, VideoFrame::CreateBlackImage has a second caller at /firefox/dom/media/encoder/VP8TrackEncoder.cpp:649 (the MediaRecorder WebM/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> + mozCaptureStream requires 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.

Attached file README.md —
Attached file Crash stack trace —
Attached file Testcase: test.html —
Group: core-security → media-core-security

Test case verified and the fix will be uploaded later.

Assignee: nobody → jolin
Severity: -- → S1
Priority: -- → P1

Move to WebRTC component where ImageCapture belongs.

Component: Audio/Video → WebRTC: Audio/Video
Attached file (secure) —

Also, print a warning message when fail to get an image to encode.

Imagecapture is disabled so can't warrant a sec-high, but perhaps the path in VP8TrackEncoder is equally susceptible.

The bug has a release status flag that shows some version of Firefox is affected, thus it will be considered confirmed.

Status: UNCONFIRMED → NEW
Ever confirmed: true
Attached file (secure) —

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
Attachment #9554763 - Flags: sec-approval?

test.html still crashes in debug builds because of assertion, but there is no buffer overflow.

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)

Attachment #9554763 - Flags: sec-approval? → sec-approval+
Flags: in-testsuite?
Whiteboard: [reminder-test 2026-05-05]

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.

Group: media-core-security → core-security-release
Status: NEW → RESOLVED
Closed: 8 months ago
Resolution: --- → FIXED
Target Milestone: --- → 151 Branch
Severity: S1 → S3
Priority: P1 → --
Priority: -- → P1

The patch landed in nightly and beta is affected.
:jhlin, is this bug important enough to require an uplift?

For more information, please visit BugBot documentation.

Flags: needinfo?(jolin)
Flags: needinfo?(jolin)

Please add uplift requests for esr140

Flags: needinfo?(jolin)

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
Attachment #9573278 - Flags: approval-mozilla-esr140?
Attached file (secure) —
QA Whiteboard: [sec] [qa-triage-done-c152/b151]
Flags: needinfo?(jolin)
Attachment #9573278 - Flags: approval-mozilla-esr140? → approval-mozilla-esr140+

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.

Flags: needinfo?(jolin)
Whiteboard: [reminder-test 2026-05-05]
Flags: needinfo?(jolin)
Whiteboard: [adv-main151+r][adv-main151+r][adv-esr115.36+r][adv-esr115.36+r][adv-esr140.11+r][adv-esr140.11+r]
Whiteboard: [adv-main151+r][adv-main151+r][adv-esr115.36+r][adv-esr115.36+r][adv-esr140.11+r][adv-esr140.11+r] → [adv-main151+r][adv-esr140.11+r]
Duplicate of this bug: 2040640
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: