php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46318 gdImageFill invalid stack overflow comparison
Submitted: 2008-10-16 19:30 UTC Modified: 2008-10-16 21:30 UTC
From: cndougla at linux dot vnet dot ibm dot com Assigned:
Status: Not a bug Package: GD related
PHP Version: 5.2.6 OS:
Private report: No CVE-ID: None
 [2008-10-16 19:30 UTC] cndougla at linux dot vnet dot ibm dot com
Description:
------------
In gdImageFill, a stack is created for the flood fill algorithm. Originally it seems the stack was created with space for 1,200,000 structures, but that has since been commented out and the stack is now created dynamically with the depth determined by the size of the image. The macro used to push structures onto the stack was checking for overflow based on checking the current stack pointer. Instead of comparing the stack pointer to the real size of the stack, the stack pointer was compared against the size of the structure (16 bytes) * 1,200,000 * 10. I have no idea why the factor of 10 was there. This large value wraps 32-bit arithmetic all the way around such that the comparison was no longer valid, and it always seemed the stack had overflowed even before anything was pushed onto it.

Reproduce code:
---------------
<?php
        $im = imagecreatetruecolor(30, 50);
        imagefill($im, 0, 0, 1); // Color every pixel 1
        $col = imagecolorat($im, 20, 20);
        echo "$col\n";
?>

Expected result:
----------------
1

Actual result:
--------------
0 when the bug shows up. I found it to fail on ppc64 when it was built as a ppc32 userspace library, while on a ppc32 or x86 or x86_64 system it passed just fine.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-16 19:32 UTC] cndougla at linux dot vnet dot ibm dot com
A patch to fix the issue:

diff -uNr -ur php-5.2.6.orig/ext/gd/libgd/gd.c php-5.2.6/ext/gd/libgd/gd.c
--- php-5.2.6.orig/ext/gd/libgd/gd.c    2007-11-04 17:56:00.000000000 -0600
+++ php-5.2.6/ext/gd/libgd/gd.c 2008-10-16 13:03:41.000000000 -0500
@@ -1938,9 +1938,9 @@
 struct seg {int y, xl, xr, dy;};

 /* max depth of stack */
-#define FILL_MAX 1200000
+#define FILL_MAX ((int)(im->sy*im->sx)/4)
 #define FILL_PUSH(Y, XL, XR, DY) \
-    if (sp<stack+FILL_MAX*10 && Y+(DY)>=0 && Y+(DY)<wy2) \
+    if (sp<stack+FILL_MAX && Y+(DY)>=0 && Y+(DY)<wy2) \
     {sp->y = Y; sp->xl = XL; sp->xr = XR; sp->dy = DY; sp++;}

 #define FILL_POP(Y, XL, XR, DY) \
 [2008-10-16 21:30 UTC] pajoye@php.net
Duplicate of libgd bug #177.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 00:01:32 2024 UTC