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