b74969
Fix for CVE-2017-10168
b74969
Backported for 5.4 without test and binary patch
b74969
b74969
b74969
From d2274b01cbbadf5516b3ea87ad76fbae18834007 Mon Sep 17 00:00:00 2001
b74969
From: "Christoph M. Becker" <cmbecker69@gmx.de>
b74969
Date: Sat, 17 Dec 2016 17:06:58 +0100
b74969
Subject: [PATCH] Fix #73869: Signed Integer Overflow gd_io.c
b74969
b74969
GD2 stores the number of horizontal and vertical chunks as words (i.e. 2
b74969
byte unsigned). These values are multiplied and assigned to an int when
b74969
reading the image, what can cause integer overflows. We have to avoid
b74969
that, and also make sure that either chunk count is actually greater
b74969
than zero. If illegal chunk counts are detected, we bail out from
b74969
reading the image.
b74969
b74969
(cherry picked from commit 5b5d9db3988b829e0b121b74bb3947f01c2796a1)
b74969
---
b74969
 ext/gd/libgd/gd_gd2.c      |   4 ++++
b74969
 ext/gd/tests/bug73869.phpt |  19 +++++++++++++++++++
b74969
 ext/gd/tests/bug73869a.gd2 | Bin 0 -> 92 bytes
b74969
 ext/gd/tests/bug73869b.gd2 | Bin 0 -> 18 bytes
b74969
 4 files changed, 23 insertions(+)
b74969
 create mode 100644 ext/gd/tests/bug73869.phpt
b74969
 create mode 100644 ext/gd/tests/bug73869a.gd2
b74969
 create mode 100644 ext/gd/tests/bug73869b.gd2
b74969
b74969
diff --git a/ext/gd/libgd/gd_gd2.c b/ext/gd/libgd/gd_gd2.c
b74969
index 196b785..3eba6b3 100644
b74969
--- a/ext/gd/libgd/gd_gd2.c
b74969
+++ b/ext/gd/libgd/gd_gd2.c
b74969
@@ -136,6 +136,10 @@ static int _gd2GetHeader(gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, in
b74969
 	GD2_DBG(php_gd_error("%d Chunks vertically", *ncy));
b74969
 
b74969
 	if (gd2_compressed(*fmt)) {
b74969
+		if (*ncx <= 0 || *ncy <= 0 || *ncx > INT_MAX / *ncy) {
b74969
+			GD2_DBG(printf ("Illegal chunk counts: %d * %d\n", *ncx, *ncy));
b74969
+			goto fail1;
b74969
+		}
b74969
 		nc = (*ncx) * (*ncy);
b74969
 		GD2_DBG(php_gd_error("Reading %d chunk index entries", nc));
b74969
 		if (overflow2(sizeof(t_chunk_info), nc)) {
b74969
-- 
b74969
2.1.4
b74969