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

Heap overflow write in SkEdgeBuilder::buildPoly

An integer overflow in Skia's edge builder allowed a crafted path with a huge number of points to cause an undersized heap allocation, after which edges were written past the buffer. An attacker could crash the browser tab by opening a malicious page, and the invalid write suggests possible memory corruption.

abbaolk Follow

Originally disclosed on Mozilla

  • Fixed in 23 days
  • Paid in 31 days
  • Disclosed after 23 days
Text
Created attachment 8929568
poc.html

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Build ID: 20171115095126

Steps to reproduce:


System: Ubuntu 16.04.3 LTS (64bit)
Firefox version: 57.0 (64-bit)
RAM size:16376108 kB(16G) 


Root cause:

Int overflow could happen there:
https://searchfox.org/mozilla-central/source/gfx/skia/skia/src/core/SkEdgeBuilder.cpp#251
    int maxEdgeCount = path.countPoints();
    if (iclip) {
        // clipping can turn 1 line into (up to) kMaxClippedLineSegments, since
        // we turn portions that are clipped out on the left/right into vertical
        // segments.
        maxEdgeCount *= SkLineClipper::kMaxClippedLineSegments; // Int overflow
    } 
     size_t edgeSize = fAnalyticAA ? sizeof(SkAnalyticEdge) : sizeof(SkEdge);
    char* edge = fAnalyticAA ? (char*)fAlloc.makeArrayDefault<SkAnalyticEdge>(maxEdgeCount)
                             : (char*)fAlloc.makeArrayDefault<SkEdge>(maxEdgeCount);


When "maxEdgeCount" become negative,  "fAlloc.makeArrayDefault" will alloc much less memory than needed:
 makeArrayDefault(size_t count) {
        uint32_t safeCount = SkTo<uint32_t>(count); // incorrect conversion


Reproduce:
To trigger the vulnerability, we need to make the path have  about 0x80000000/3 points, which would consume about 6.1G RAM.
Hence the vulnerability can only be fast triggered on a computer with at least 8GB RAM.

Open attached poc.html, wait for about 30 seconds or less, the tab will crash.

With gdb attached and  firefox-dbg installed, we can see the stack backtrace:
(gdb) bt 8
#0  SkEdgeBuilder::CombineVertical (this=0x7ffd64103020, edge=0x7f1087747218, last=0x25700000000)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkEdgeBuilder.cpp:22
#1  0x00007f10a80b24b5 in SkEdgeBuilder::buildPoly (this=this@entry=0x7ffd64103020, path=..., 
    iclip=iclip@entry=0x7ffd64102f80, shiftUp=shiftUp@entry=2, canCullToTheRight=canCullToTheRight@entry=true)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkEdgeBuilder.cpp:284
#2  0x00007f10a80b642e in SkEdgeBuilder::build (this=this@entry=0x7ffd64103020, path=..., 
    iclip=iclip@entry=0x7ffd64102f80, shiftUp=shiftUp@entry=2, canCullToTheRight=<optimized out>, 
    analyticAA=analyticAA@entry=false)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkEdgeBuilder.cpp:352
#3  0x00007f10a811d3b4 in sk_fill_path (path=..., clipRect=..., blitter=blitter@entry=0x7ffd641031b0, start_y=-150, 
    stop_y=150, shiftEdgesUp=shiftEdgesUp@entry=2, pathContainedInClip=false)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkScan_Path.cpp:405
#4  0x00007f10a7e850a1 in SkScan::AntiFillPath (path=..., origClip=..., blitter=<optimized out>, 
    blitter@entry=0x7ffd64103cf8, forceRLE=forceRLE@entry=false)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkScan_AntiPath.cpp:726
#5  0x00007f10a7e855c8 in SkScan::AntiFillPath (path=..., clip=..., blitter=0x7ffd64103cf8)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkScan_AntiPath.cpp:783
#6  0x00007f10a801df10 in SkDraw::drawDevPath (this=this@entry=0x7ffd64104ac0, devPath=..., paint=..., 
    drawCoverage=drawCoverage@entry=false, customBlitter=customBlitter@entry=0x0, doFill=doFill@entry=true)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkDraw.cpp:1070
#7  0x00007f10a80203c0 in SkDraw::drawPath (this=this@entry=0x7ffd64104ac0, origSrcPath=..., origPaint=..., 
    prePathMatrix=prePathMatrix@entry=0x0, pathIsMutable=<optimized out>, pathIsMutable@entry=false, 
    drawCoverage=drawCoverage@entry=false, customBlitter=0x0)
    at /build/firefox-HLWaua/firefox-57.0+build4/gfx/skia/skia/src/core/SkDraw.cpp:1163

In function "SkEdgeBuilder::Combine SkEdgeBuilder::CombineVertical(const SkEdge* edge, SkEdge* last) ", "last" points to invalid memory.(First be read and then be written)

The original report as published on Mozilla's Bugzilla, by abbaolk. 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 a signed integer overflow when multiplying the point count by the maximum clipped segments per line, followed by an incorrect conversion to an unsigned size for the allocation.

Impact

The tab crashes, and because an out-of-bounds write occurs the flaw could potentially lead to memory corruption, though the report only demonstrates a crash.

What the attacker needed

Victim opens a malicious page; at least 8 GB RAM; about 30 seconds of rendering

How it was found

  1. Open the attached proof-of-concept HTML page in Firefox on a machine with at least 8 GB of RAM.
  2. Wait roughly 30 seconds or less for the tab to crash.
  3. Attach a debugger to observe the invalid pointer in the edge combining function.

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 browser draws a shape, it first works out how many line segments it needs and reserves memory for them. Here a multiplication of that count overflowed and became a small or negative number, so far too little memory was reserved, and the drawing code then wrote past the end of it. A web page that draws a very complex shape can therefore corrupt the browser's memory or crash it.

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 abbaolk

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.