imagecolormatch Out Of Bounds Write on Heap
The imagecolormatch function allocates a buffer based on the number of colours in the second image, but then writes to it using pixel colour values as an index, which can be up to 255. An attacker can cause an out-of-bounds heap write of up to 1200 bytes with controlled data, potentially leading to a local safe mode bypass.
Originally disclosed on HackerOne
- Triaged in 4 months
- Fixed in 21 months
- Paid in 21 months
- Disclosed after 21 months
The link to the PHP bug: https://bugs.php.net/bug.php?id=77270
This is possible to exploit in PHP 7.0.33 and 5.6.39. I used this vulnerability to write a local safe mode bypass exploit.
It is possible to write up to 1200 bytes over the boundaries of a buffer allocated in the imagecolormatch function, which then calls gdImageColorMatch()
The function takes two gdImagePtr as arguments and wants to compare both of them. It then allocates a dynamic buffer with the following calculation:
buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);
im2->colorsTotal is under the control of an attacker. By simply allocating only one color to the second image, the calculation becomes sizeof(unsigned long) (8 byte on a 64 bit system) * 5 * 1, which results in a buffer of 40 bytes.
The buffer is then written to in a for loop.
for (x=0; x<im1->sx; x++) {
for( y=0; y<im1->sy; y++ ) {
color = im2->pixels[y][x];
rgb = im1->tpixels[y][x];
bp = buf + (color * 5);
(*(bp++))++;
*(bp++) += gdTrueColorGetRed(rgb);
*(bp++) += gdTrueColorGetGreen(rgb);
*(bp++) += gdTrueColorGetBlue(rgb);
*(bp++) += gdTrueColorGetAlpha(rgb);
}
The buffer is written to by means of a color being the index:
color = im2->pixels[y][x];
..
bp = buf + (color * 5);
However, an attacker can set the value of color to be at maximum 255 (since it is a char). This would result in bp pointing at buffer + 1275 bytes. Since buffer is only 40 bytes big, this leads to an out of bounds write with data that is also under the control of the attacker.
PoC PHP script:
$img1 = imagecreatetruecolor(0xfff, 0xfff);
$img2 = imagecreate(0xfff, 0xfff);
imagecolorallocate($img2, 0, 0, 0);
imagesetpixel($img2, 0, 0, 255);
imagecolormatch($img1, $img2);
Impact
This vulnerability allows attackers to bypass local security restrictions such as disabled functions.
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 buffer size is calculated from the number of colours in the second image, but the write index is derived from pixel colour values that can exceed that number, leading to an out-of-bounds write.
Impact
This vulnerability allows attackers to bypass local security restrictions such as disabled functions.
What the attacker needed
PHP with GD extension; ability to call imagecolormatch
How it was found
- Create a truecolor image with large dimensions.
- Create a palette image with large dimensions and allocate only one colour.
- Set a pixel in the palette image to a colour index of 255.
- Call imagecolormatch with the two images.
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.
The imagecolormatch function in PHP tries to match colours between two images. It reserves a small memory area based on how many colours the second image has, but then uses the colour value of each pixel as a position to write into that area. Since a colour value can be much larger than the reserved area, the function writes past the end of the memory, which can corrupt other data and be used to bypass security restrictions.
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.