Skip to content
Memory corruption Disclosed report Advanced

Potential read beyond bounds caused by WidevineVideoFrame::InitToBlack()

The WidevineVideoFrame::InitToBlack() function in Firefox's Widevine adapter uses 32-bit integer arithmetic to calculate the size of the chroma planes, which can overflow and underallocate a buffer. An attacker could potentially cause a read beyond bounds by supplying crafted frame dimensions, though the reporter could not confirm reachability.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 27 days
  • Paid in 39 days
  • Disclosed after 27 days
Text
`WidevineVideoFrame::InitToBlack()` (`dom/media/gmp/widevine-adapter/WidevineVideoFrame.cpp`) checks for overflow improperly, potentially underallocating a buffer and causing a consequent read beyond bounds. The bug is in line 107, which can -- despite the comment on line 106 -- experience an overflow because it uses `int32_t` math, while the `CheckedInt` expressions on lines 104-05 use `uint64_t` math (in the dominant 64-bit builds):
'''
   100: bool WidevineVideoFrame::InitToBlack(int32_t aWidth, int32_t aHeight,
   101:                                      int64_t aTimeStamp) {
   102:   MOZ_ASSERT(aWidth >= 0 && aHeight >= 0,
   103:              "Frame dimensions should be positive");
   104:   CheckedInt<size_t> ySizeChk = aWidth;
   105:   ySizeChk *= aHeight;
   106:   // If w*h didn't overflow, half of them won't.
   107:   const size_t uSize = ((aWidth + 1) / 2) * ((aHeight + 1) / 2);
   108:   CheckedInt<size_t> yuSizeChk = ySizeChk + uSize;
   109:   if (!yuSizeChk.isValid()) {
   110:     return false;
   111:   }
   112:   WidevineBuffer* buffer = new WidevineBuffer(yuSizeChk.value());
   113:   const size_t& ySize = ySizeChk.value();
   114:   // Black in YCbCr is (0,128,128).
   115:   memset(buffer->Data(), 0, ySize);
   116:   memset(buffer->Data() + ySize, 128, uSize); [won't WBB because uSize == 0]
   ...
   124:   SetPlaneOffset(cdm::VideoPlane::kYPlane, 0); [OK]
   125:   SetStride(cdm::VideoPlane::kYPlane, aWidth);
   126:   // Note: U and V planes are stored at the same place in order to
   127:   // save memory since their contents are the same.
   128:   SetPlaneOffset(cdm::VideoPlane::kUPlane, ySize); [Oh-oh, beyond bounds!]
   129:   SetStride(cdm::VideoPlane::kUPlane, (aWidth + 1) / 2);
   130:   SetPlaneOffset(cdm::VideoPlane::kVPlane, ySize); [Oh-oh, beyond bounds!]
   131:   SetStride(cdm::VideoPlane::kVPlane, (aWidth + 1) / 2);
   132:   SetTimestamp(aTimeStamp);
   133: 
   134:   return true;
   135: }
'''
I have verified the overflow in a sample program (attached) that uses `CheckedInt` with `aWidth` = `0x7fffffff`, `aHeight` == `7` and `aTimeStamp` == `0`. Under these conditions, `uSize` overflows to `0`, which would cause the frame's `U` and `V` planes to be located beyond the buffer's end (lines 128 and 130). The sample program builds in VS 2022. Change the path in the `#include` of `IntegerTypeTraits.h` appropriately for your build system.

So far I have been unable to cause FF to call this function; when I attempt to load Widevine content from `https://shaka-player-demo.appspot.com/demo` , FF's `plugin_container` process hangs forever inside the Widevine DLL on the call to `create()` from `ChromiumCDMAdapter::GMPGetAPI()`, so I don't know whether some other check prevents such a large `aWidth` from getting into `InitToBlack()`.

See also https://bugzilla.mozilla.org/show_bug.cgi?id=1349604 which didn't quite fix the problem...

The original report as published on Mozilla's Bugzilla, by mozillabugs. 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

Likely the use of 32-bit signed integer arithmetic for the chroma plane size calculation, which can overflow even though the luma size check uses a safe 64-bit type.

Impact

Could allow a read beyond the allocated buffer, but the reporter could not trigger the function in Firefox and does not know whether other checks prevent large dimensions from reaching it.

What the attacker needed

Ability to supply crafted frame dimensions; Widevine content playback in Firefox

The fix

Should be fixed by performing the chroma size calculation with checked 64-bit arithmetic, similar to the luma size check.

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.

When a video frame is created, the code calculates how much memory to reserve for the colour information. It uses a smaller number type than needed, so a very large width and height can wrap around to a tiny value, making the buffer too small. Later, the program sets pointers to parts of the buffer that are now outside its real size, which could lead to reading memory that does not belong to the frame.

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 mozillabugs

View all

41 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.