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

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