Blame SOURCES/gd-2.2.5-out-of-bounds-write-on-heap.patch

572cf4
From 5b026e3cc05d7041cbe47a8702f1b51ffbf0a99b Mon Sep 17 00:00:00 2001
572cf4
From: Ondrej Dubaj <odubaj@redhat.com>
572cf4
Date: Thu, 5 Mar 2020 11:02:27 +0100
572cf4
Subject: [PATCH] Imagecolormatch Out Of Bounds Write on Heap
572cf4
572cf4
At least some of the image reading functions may return images which
572cf4
use color indexes greater than or equal to im->colorsTotal.  We cater
572cf4
to this by always using a buffer size which is sufficient for
572cf4
`gdMaxColors` in `gdImageColorMatch()`.
572cf4
572cf4
Resolves: #1678104
572cf4
Version: 2.2.5-7
572cf4
---
572cf4
 src/gd_color_match.c                    |  4 ++--
572cf4
 tests/CMakeLists.txt                    |  1 +
572cf4
 tests/Makefile.am                       |  1 +
572cf4
 tests/gdimagecolormatch/CMakeLists.txt  |  5 +++++
572cf4
 tests/gdimagecolormatch/Makemodule.am   |  5 +++++
572cf4
 tests/gdimagecolormatch/cve_2019_6977.c | 25 +++++++++++++++++++++++++
572cf4
 6 files changed, 39 insertions(+), 2 deletions(-)
572cf4
 create mode 100644 tests/gdimagecolormatch/CMakeLists.txt
572cf4
 create mode 100644 tests/gdimagecolormatch/Makemodule.am
572cf4
 create mode 100644 tests/gdimagecolormatch/cve_2019_6977.c
572cf4
572cf4
diff --git a/src/gd_color_match.c b/src/gd_color_match.c
572cf4
index f0842b6..a94a841 100755
572cf4
--- a/src/gd_color_match.c
572cf4
+++ b/src/gd_color_match.c
572cf4
@@ -31,8 +31,8 @@ BGD_DECLARE(int) gdImageColorMatch (gdImagePtr im1, gdImagePtr im2)
572cf4
 		return -4; /* At least 1 color must be allocated */
572cf4
 	}
572cf4
 
572cf4
-	buf = (unsigned long *)gdMalloc(sizeof(unsigned long) * 5 * im2->colorsTotal);
572cf4
-	memset (buf, 0, sizeof(unsigned long) * 5 * im2->colorsTotal );
572cf4
+	buf = (unsigned long *)gdMalloc(sizeof(unsigned long) * 5 * gdMaxColors);
572cf4
+	memset (buf, 0, sizeof(unsigned long) * 5 * gdMaxColors );
572cf4
 
572cf4
 	for (x=0; x < im1->sx; x++) {
572cf4
 		for( y=0; y<im1->sy; y++ ) {
572cf4
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
572cf4
index 7eef4bf..6979416 100755
572cf4
--- a/tests/CMakeLists.txt
572cf4
+++ b/tests/CMakeLists.txt
572cf4
@@ -31,6 +31,7 @@ if (BUILD_TEST)
572cf4
 		gdimagecolordeallocate
572cf4
 		gdimagecolorexact
572cf4
 		gdimagecolorreplace
572cf4
+		gdimagecolormatch
572cf4
 		gdimagecolorresolve
572cf4
 		gdimagecolortransparent
572cf4
 		gdimagecontrast
572cf4
diff --git a/tests/Makefile.am b/tests/Makefile.am
572cf4
index 5f8b624..1a44112 100755
572cf4
--- a/tests/Makefile.am
572cf4
+++ b/tests/Makefile.am
572cf4
@@ -26,6 +26,7 @@ include gdimagecolorclosest/Makemodule.am
572cf4
 include gdimagecolordeallocate/Makemodule.am
572cf4
 include gdimagecolorexact/Makemodule.am
572cf4
 include gdimagecolorreplace/Makemodule.am
572cf4
+include gdimagecolormatch/Makemodule.am
572cf4
 include gdimagecolorresolve/Makemodule.am
572cf4
 include gdimagecolortransparent/Makemodule.am
572cf4
 include gdimagecontrast/Makemodule.am
572cf4
diff --git a/tests/gdimagecolormatch/CMakeLists.txt b/tests/gdimagecolormatch/CMakeLists.txt
572cf4
new file mode 100644
572cf4
index 0000000..591938f
572cf4
--- /dev/null
572cf4
+++ b/tests/gdimagecolormatch/CMakeLists.txt
572cf4
@@ -0,0 +1,5 @@
572cf4
+LIST(APPEND TESTS_FILES
572cf4
+	cve_2019_6977
572cf4
+)
572cf4
+
572cf4
+ADD_GD_TESTS()
572cf4
diff --git a/tests/gdimagecolormatch/Makemodule.am b/tests/gdimagecolormatch/Makemodule.am
572cf4
new file mode 100644
572cf4
index 0000000..e8e09a9
572cf4
--- /dev/null
572cf4
+++ b/tests/gdimagecolormatch/Makemodule.am
572cf4
@@ -0,0 +1,5 @@
572cf4
+libgd_test_programs += \
572cf4
+	gdimagecolormatch/cve_2019_6977
572cf4
+
572cf4
+EXTRA_DIST += \
572cf4
+	gdimagecolormatch/CMakeLists.txt
572cf4
diff --git a/tests/gdimagecolormatch/cve_2019_6977.c b/tests/gdimagecolormatch/cve_2019_6977.c
572cf4
new file mode 100644
572cf4
index 0000000..fdd7af5
572cf4
--- /dev/null
572cf4
+++ b/tests/gdimagecolormatch/cve_2019_6977.c
572cf4
@@ -0,0 +1,25 @@
572cf4
+/**
572cf4
+ * Test for CVE-2019-6977
572cf4
+ */
572cf4
+
572cf4
+#include "gd.h"
572cf4
+
572cf4
+int main()
572cf4
+{
572cf4
+	gdImagePtr im1;
572cf4
+	gdImagePtr im2;
572cf4
+
572cf4
+	im1 = gdImageCreateTrueColor(0xfff, 0xfff);
572cf4
+	im2 = gdImageCreate(0xfff, 0xfff);
572cf4
+	if (gdImageColorAllocate(im2, 0, 0, 0) < 0)
572cf4
+	{
572cf4
+		gdImageDestroy(im1);
572cf4
+		gdImageDestroy(im2);
572cf4
+		return 1;
572cf4
+	}
572cf4
+	gdImageSetPixel(im2, 0, 0, 255);
572cf4
+	gdImageColorMatch(im1, im2);
572cf4
+	gdImageDestroy(im1);
572cf4
+	gdImageDestroy(im2);
572cf4
+	return 0;
572cf4
+}
572cf4
-- 
572cf4
2.24.1
572cf4
572cf4