3220ab
From a11f47475e6443b7f32d21f2271f28f417e2ac04 Mon Sep 17 00:00:00 2001
3220ab
From: "Christoph M. Becker" <cmbecker69@gmx.de>
3220ab
Date: Wed, 29 Nov 2017 19:37:38 +0100
3220ab
Subject: [PATCH] Fix #420: Potential infinite loop in gdImageCreateFromGifCtx
3220ab
3220ab
Due to a signedness confusion in `GetCode_` a corrupt GIF file can
3220ab
trigger an infinite loop.  Furthermore we make sure that a GIF without
3220ab
any palette entries is treated as invalid *after* open palette entries
3220ab
have been removed.
3220ab
3220ab
CVE-2018-5711
3220ab
3220ab
See also https://bugs.php.net/bug.php?id=75571.
3220ab
---
3220ab
 src/gd_gif_in.c             |  12 ++++++------
3220ab
 tests/gif/.gitignore        |   1 +
3220ab
 tests/gif/CMakeLists.txt    |   1 +
3220ab
 tests/gif/Makemodule.am     |   2 ++
3220ab
 tests/gif/php_bug_75571.c   |  28 ++++++++++++++++++++++++++++
3220ab
 tests/gif/php_bug_75571.gif | Bin 0 -> 1731 bytes
3220ab
 6 files changed, 38 insertions(+), 6 deletions(-)
3220ab
 create mode 100644 tests/gif/php_bug_75571.c
3220ab
 create mode 100644 tests/gif/php_bug_75571.gif
3220ab
3220ab
diff --git a/src/gd_gif_in.c b/src/gd_gif_in.c
3220ab
index daf26e79..0a8bd717 100644
3220ab
--- a/src/gd_gif_in.c
3220ab
+++ b/src/gd_gif_in.c
3220ab
@@ -335,11 +335,6 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromGifCtx(gdIOCtxPtr fd)
3220ab
 		return 0;
3220ab
 	}
3220ab
 
3220ab
-	if(!im->colorsTotal) {
3220ab
-		gdImageDestroy(im);
3220ab
-		return 0;
3220ab
-	}
3220ab
-
3220ab
 	/* Check for open colors at the end, so
3220ab
 	 * we can reduce colorsTotal and ultimately
3220ab
 	 * BitsPerPixel */
3220ab
@@ -351,6 +346,11 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromGifCtx(gdIOCtxPtr fd)
3220ab
 		}
3220ab
 	}
3220ab
 
3220ab
+	if(!im->colorsTotal) {
3220ab
+		gdImageDestroy(im);
3220ab
+		return 0;
3220ab
+	}
3220ab
+
3220ab
 	return im;
3220ab
 }
3220ab
 
3220ab
@@ -447,7 +447,7 @@ static int
3220ab
 GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
3220ab
 {
3220ab
 	int i, j, ret;
3220ab
-	unsigned char count;
3220ab
+	int count;
3220ab
 
3220ab
 	if(flag) {
3220ab
 		scd->curbit = 0;
3220ab