From 2bb97f407c1145c850416a3bfbcc8cf124e68a19 Mon Sep 17 00:00:00 2001
From: Mike Frysinger <vapier@gentoo.org>
Date: Sat, 16 Apr 2016 03:51:22 -0400
Subject: [PATCH] gd2: handle corrupt images better (CVE-2016-3074)

Make sure we do some range checking on corrupted chunks.

Thanks to Hans Jerry Illikainen <hji@dyntopia.com> for indepth report
and reproducer information.  Made for easy test case writing :).
---
 .gitignore                     |   1 +
 src/gd_gd2.c                   |   2 ++
 tests/Makefile.am              |   3 ++-
 tests/gd2/gd2_read_corrupt.c   |  25 +++++++++++++++++++++++++
 tests/gd2/invalid_neg_size.gd2 | Bin 0 -> 1676 bytes
 5 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 tests/gd2/gd2_read_corrupt.c
 create mode 100644 tests/gd2/invalid_neg_size.gd2

Index: libgd2-2.1.0/src/gd_gd2.c
===================================================================
--- libgd2-2.1.0.orig/src/gd_gd2.c	2016-05-26 09:29:00.574149713 -0400
+++ libgd2-2.1.0/src/gd_gd2.c	2016-05-26 09:29:00.574149713 -0400
@@ -167,6 +167,8 @@
 			if (gdGetInt (&cidx[i].size, in) != 1) {
 				goto fail2;
 			};
+			if (cidx[i].offset < 0 || cidx[i].size < 0)
+				goto fail2;
 		};
 		*chunkIdx = cidx;
 	};
