Skip to content
Memory corruption Disclosed report Advanced CVE-2018-12361

Buffer Overflow in gfx::SwizzleCopy

Integer overflow in gfx::SwizzleCopy and its caller allows rowLength and gap calculations to become negative, leading to out-of-bounds memory access via memcpy. An attacker could trigger crashes or potentially exploit memory corruption by supplying crafted graphics dimensions.

r2 Follow

Originally disclosed on Mozilla

Critical View original
  • Fixed in 4 days
  • Paid in 14 days
  • Disclosed after 4 days
Text
User Agent: Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0
Build ID: 20100101

Steps to reproduce:

gfx::SwizzleCopy (gfx/2d/Swizzle.cpp) is unsafe:

static void
SwizzleCopy(const uint8_t* aSrc, int32_t aSrcGap,
            uint8_t* aDst, int32_t aDstGap,
            IntSize aSize, int32_t aBPP)
{
  if (aSrc != aDst) {
    int32_t rowLength = aBPP * aSize.width; // (1)
    for (int32_t height = aSize.height; height > 0; height--) {
      memcpy(aDst, aSrc, rowLength); // (0)
      aSrc += rowLength + aSrcGap; // (2)
      aDst += rowLength + aDstGap; // (3)
    }
  }
}

At (1), rowLength can overflow and become a very large negative value. For example, if aSize.width = 0x20000000, it will overflow to -2147483648 at 4 BPP. Then, the overflowed value is used at (2) and (3) to calculate pointers, that may either underflow the currently owned memory, or even wrap around aSrc/aDst on x86 arch. Then, the call to memcpy() at (0) with wrongly calculated pointers and count can cause an out of bound access.

In addition, both aSrcGap and aDstGap are calculated in the same unsafe manner in the caller:

bool
SwizzleData(const uint8_t* aSrc, int32_t aSrcStride, SurfaceFormat aSrcFormat,
            uint8_t* aDst, int32_t aDstStride, SurfaceFormat aDstFormat,
            const IntSize& aSize)
{
  if (aSize.IsEmpty()) {
    return true;
  }
  IntSize size = CollapseSize(aSize, aSrcStride, aDstStride); // (4)
  // Find gap from end of row to the start of the next row.
  int32_t srcGap = aSrcStride - BytesPerPixel(aSrcFormat) * aSize.width; // (5)
  int32_t dstGap = aDstStride - BytesPerPixel(aDstFormat) * aSize.width; // (6)
  MOZ_ASSERT(srcGap >= 0 && dstGap >= 0); // (7)

[...]

  if (aSrcFormat == aDstFormat) {
    // If the formats match, just do a generic copy.
    SwizzleCopy(aSrc, srcGap, aDst, dstGap, size, BytesPerPixel(aSrcFormat));
    return true;
  }

[...]
}

At (5) and (6), srcGap and dstGap can overflow in exactly the same way as described above. The possibly overflowed values are then passed to SwizzleCopy and used to calculate pointer values.

Here an example of the crash on Firefox 59.0.2 which happens when aDst underflows and wraps around:
https://crash-stats.mozilla.com/report/index/19b58581-7c59-4227-95c3-9d0b30180422

And the crashes which happen when aDst or aSrc point to unowned memory:
aSrc: https://crash-stats.mozilla.com/report/index/aa3f07a1-6ed3-4aad-ae17-ed1170180424
aDst: https://crash-stats.mozilla.com/report/index/905535d1-49f9-4657-8e3a-8b73d0180427

The input graphics region which causes incorrect calculations is sanitized in a few places before the call to SwizzleCopy, but it is not sanitized properly. For example, at (4) the call to CollapseSize (/gfx/2d/Swizzle.cpp) does check that aSize.width * aSize.height is within safe bounds of int32, but the check only happens if specific conditions are met. Also, it does not account for the BPP multiplier which is used at (1), (5), (6):

static inline IntSize
CollapseSize(const IntSize& aSize, int32_t aSrcStride, int32_t aDstStride)
{
  if (aSrcStride == aDstStride &&
      aSrcStride == 4 * aSize.width) {
    CheckedInt32 area = CheckedInt32(aSize.width) * CheckedInt32(aSize.height);
    if (area.isValid()) {
      return IntSize(area.value(), 1);
    }
  }
  return aSize;
}

One more check at (7) is a correct assert, however it's compiled out in release builds. A few more checks happen inside the CopyRect (gfx/2d/DataSurfaceHelpers.cpp) caller of SwizzleData, that again they do not account for the BytesPerPixel multiplier.

It would be optimal to insert early checks, for every acquired graphics region, that both values of width * bpp and height * bpp do not overflow int32, ideally with a margin. Because the value of BytesPerPixel is relatively small, such a check would automatically rule out majority of other possible overflows.

The original report as published on Mozilla's Bugzilla, by r2. 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 multiplication of width by bytes per pixel is not checked for integer overflow before being used in pointer arithmetic and memcpy.

Impact

The out-of-bounds access can cause crashes (as shown in crash reports) and may allow memory corruption, though the text does not demonstrate code execution.

What the attacker needed

Ability to supply crafted graphics dimensions

The fix

It should be fixed by adding early checks that width * bpp and height * bpp do not overflow int32, ideally with a margin.

How it was found

  1. Supply a graphics region with a very large width (e.g., 0x20000000) and 4 bytes per pixel.
  2. The rowLength calculation overflows to a negative value.
  3. The overflowed value is used to compute source and destination pointers.
  4. memcpy is called with the wrong pointers and count, causing an out-of-bounds access.

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 program calculates the size of an image row by multiplying width by bytes per pixel, the result can overflow and become a huge negative number. This wrong size is then used to copy memory, causing the program to read or write outside the intended buffer, which can crash it or let an attacker corrupt memory.

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 r2

View all

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