Fix #420: Potential infinite loop in gdImageCreateFromGifCtx

Due to a signedness confusion in `GetCode_` a corrupt GIF file can
trigger an infinite loop.  Furthermore we make sure that a GIF without
any palette entries is treated as invalid *after* open palette entries
have been removed.

CVE-2018-5711

See also https://bugs.php.net/bug.php?id=75571.
master
Christoph M. Becker 2017-11-29 19:37:38 +01:00
parent 7ff626c48a
commit a11f47475e
6 changed files with 38 additions and 6 deletions

View File

@ -335,11 +335,6 @@ terminated:
return 0;
}
if(!im->colorsTotal) {
gdImageDestroy(im);
return 0;
}
/* Check for open colors at the end, so
* we can reduce colorsTotal and ultimately
* BitsPerPixel */
@ -351,6 +346,11 @@ terminated:
}
}
if(!im->colorsTotal) {
gdImageDestroy(im);
return 0;
}
return im;
}
@ -447,7 +447,7 @@ static int
GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
{
int i, j, ret;
unsigned char count;
int count;
if(flag) {
scd->curbit = 0;

View File

@ -8,4 +8,5 @@
/gif_im2im
/gif_null
/ossfuzz5700
/php_bug_75571
/uninitialized_memory_read

View File

@ -4,6 +4,7 @@ LIST(APPEND TESTS_FILES
bug00227
gif_null
ossfuzz5700
php_bug_75571
uninitialized_memory_read
)

View File

@ -4,6 +4,7 @@ libgd_test_programs += \
gif/bug00227 \
gif/gif_null \
gif/ossfuzz5700 \
gif/php_bug_75571 \
gif/uninitialized_memory_read
if HAVE_LIBPNG
@ -26,4 +27,5 @@ EXTRA_DIST += \
gif/bug00066.gif \
gif/bug00066_exp.png \
gif/ossfuzz5700.gif \
gif/php_bug_75571.gif \
gif/unitialized_memory_read.gif

28
tests/gif/php_bug_75571.c Normal file
View File

@ -0,0 +1,28 @@
/**
* Test that GIF reading does not loop infinitely
*
* We are reading a crafted GIF image which has been truncated. This would
* trigger an infinite loop formerly, but know bails out early, returning
* NULL from gdImageCreateFromGif().
*
* See also https://bugs.php.net/bug.php?id=75571.
*/
#include "gd.h"
#include "gdtest.h"
int main()
{
gdImagePtr im;
FILE *fp;
fp = gdTestFileOpen2("gif", "php_bug_75571.gif");
gdTestAssert(fp != NULL);
im = gdImageCreateFromGif(fp);
gdTestAssert(im == NULL);
fclose(fp);
return gdNumFailures();
}

BIN
tests/gif/php_bug_75571.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB