Blame SOURCES/gd-2.2.5-potential-double-free.patch

572cf4
From d98e5a2ace46adcefa093c029663575fd677bf05 Mon Sep 17 00:00:00 2001
572cf4
From: Ondrej Dubaj <odubaj@redhat.com>
572cf4
Date: Tue, 4 Jun 2019 13:05:57 +0200
572cf4
Subject: [PATCH] Potential double-free in gdImage*Ptr()
572cf4
572cf4
Whenever `gdImage*Ptr()` calls `gdImage*Ctx()` and the latter fails, we
572cf4
must not call `gdDPExtractData()`; otherwise a double-free would
572cf4
happen.  Since `gdImage*Ctx()` are void functions, and we can't change
572cf4
that for BC reasons, we're introducing static helpers which are used
572cf4
internally.
572cf4
572cf4
We're adding a regression test for `gdImageJpegPtr()`, but not for
572cf4
`gdImageGifPtr()` and `gdImageWbmpPtr()` since we don't know how to
572cf4
trigger failure of the respective `gdImage*Ctx()` calls.
572cf4
572cf4
This potential security issue has been reported by Solmaz Salimi (aka.
572cf4
Rooney).
572cf4
---
572cf4
 src/gd_gif_out.c                  | 19 +++++++++++++++----
572cf4
 src/gd_jpeg.c                     | 20 ++++++++++++++++----
572cf4
 src/gd_wbmp.c                     | 21 ++++++++++++++++++---
572cf4
 tests/jpeg/CMakeLists.txt         |  1 +
572cf4
 tests/jpeg/Makemodule.am          |  3 ++-
572cf4
 tests/jpeg/jpeg_ptr_double_free.c | 31 +++++++++++++++++++++++++++++++
572cf4
 6 files changed, 83 insertions(+), 12 deletions(-)
572cf4
 create mode 100644 tests/jpeg/jpeg_ptr_double_free.c
572cf4
572cf4
diff --git a/src/gd_gif_out.c b/src/gd_gif_out.c
572cf4
index 6fe707d..4a05c09 100755
572cf4
--- a/src/gd_gif_out.c
572cf4
+++ b/src/gd_gif_out.c
572cf4
@@ -99,7 +99,7 @@ static void char_init(GifCtx *ctx);
572cf4
 static void char_out(int c, GifCtx *ctx);
572cf4
 static void flush_char(GifCtx *ctx);
572cf4
 
572cf4
-
572cf4
+static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
572cf4
 
572cf4
 
572cf4
 /*
572cf4
@@ -131,8 +131,11 @@ BGD_DECLARE(void *) gdImageGifPtr(gdImagePtr im, int *size)
572cf4
 	void *rv;
572cf4
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
572cf4
 	if (out == NULL) return NULL;
572cf4
-	gdImageGifCtx(im, out);
572cf4
-	rv = gdDPExtractData(out, size);
572cf4
+	if (!_gdImageGifCtx(im, out)) {
572cf4
+        rv = gdDPExtractData(out, size);
572cf4
+    } else {
572cf4
+        rv = NULL;
572cf4
+    }
572cf4
 	out->gd_free(out);
572cf4
 	return rv;
572cf4
 }
572cf4
@@ -220,6 +223,12 @@ BGD_DECLARE(void) gdImageGif(gdImagePtr im, FILE *outFile)
572cf4
 
572cf4
 */
572cf4
 BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
572cf4
+{
572cf4
+    _gdImageGifCtx(im, out);
572cf4
+}
572cf4
+
572cf4
+/* returns 0 on success, 1 on failure */
572cf4
+static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
572cf4
 {
572cf4
 	gdImagePtr pim = 0, tim = im;
572cf4
 	int interlace, BitsPerPixel;
572cf4
@@ -231,7 +240,7 @@ BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
572cf4
 		based temporary image. */
572cf4
 		pim = gdImageCreatePaletteFromTrueColor(im, 1, 256);
572cf4
 		if(!pim) {
572cf4
-			return;
572cf4
+			return 1;
572cf4
 		}
572cf4
 		tim = pim;
572cf4
 	}
572cf4
@@ -247,6 +256,8 @@ BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
572cf4
 		/* Destroy palette based temporary image. */
572cf4
 		gdImageDestroy(	pim);
572cf4
 	}
572cf4
+
572cf4
+    return 0;
572cf4
 }
572cf4
 
572cf4
 
572cf4
diff --git a/src/gd_jpeg.c b/src/gd_jpeg.c
572cf4
index 271ef46..bd8fc27 100755
572cf4
--- a/src/gd_jpeg.c
572cf4
+++ b/src/gd_jpeg.c
572cf4
@@ -123,6 +123,8 @@ static void fatal_jpeg_error(j_common_ptr cinfo)
572cf4
 	exit(99);
572cf4
 }
572cf4
 
572cf4
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality);
572cf4
+
572cf4
 /*
572cf4
  * Write IM to OUTFILE as a JFIF-formatted JPEG image, using quality
572cf4
  * QUALITY.  If QUALITY is in the range 0-100, increasing values
572cf4
@@ -237,8 +239,11 @@ BGD_DECLARE(void *) gdImageJpegPtr(gdImagePtr im, int *size, int quality)
572cf4
 	void *rv;
572cf4
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
572cf4
 	if (out == NULL) return NULL;
572cf4
-	gdImageJpegCtx(im, out, quality);
572cf4
-	rv = gdDPExtractData(out, size);
572cf4
+	if (!_gdImageJpegCtx(im, out, quality)) {
572cf4
+		rv = gdDPExtractData(out, size);
572cf4
+	} else {
572cf4
+		rv = NULL;
572cf4
+	}
572cf4
 	out->gd_free(out);
572cf4
 	return rv;
572cf4
 }
572cf4
@@ -259,6 +264,12 @@ void jpeg_gdIOCtx_dest(j_compress_ptr cinfo, gdIOCtx *outfile);
572cf4
 
572cf4
 */
572cf4
 BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
572cf4
+{
572cf4
+	_gdImageJpegCtx(im, outfile, quality);
572cf4
+}
572cf4
+
572cf4
+/* returns 0 on success, 1 on failure */
572cf4
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
572cf4
 {
572cf4
 	struct jpeg_compress_struct cinfo;
572cf4
 	struct jpeg_error_mgr jerr;
572cf4
@@ -293,7 +304,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
572cf4
 		if(row) {
572cf4
 			gdFree(row);
572cf4
 		}
572cf4
-		return;
572cf4
+		return 1;
572cf4
 	}
572cf4
 
572cf4
 	cinfo.err->emit_message = jpeg_emit_message;
572cf4
@@ -334,7 +345,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
572cf4
 	if(row == 0) {
572cf4
 		gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
572cf4
 		jpeg_destroy_compress(&cinfo);
572cf4
-		return;
572cf4
+		return 1;
572cf4
 	}
572cf4
 
572cf4
 	rowptr[0] = row;
572cf4
@@ -411,6 +422,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
572cf4
 	jpeg_finish_compress(&cinfo);
572cf4
 	jpeg_destroy_compress(&cinfo);
572cf4
 	gdFree(row);
572cf4
+	return 0;
572cf4
 }
572cf4
 
572cf4
 
572cf4
diff --git a/src/gd_wbmp.c b/src/gd_wbmp.c
572cf4
index 0028273..341ff6e 100755
572cf4
--- a/src/gd_wbmp.c
572cf4
+++ b/src/gd_wbmp.c
572cf4
@@ -88,6 +88,8 @@ int gd_getin(void *in)
572cf4
 	return (gdGetC((gdIOCtx *)in));
572cf4
 }
572cf4
 
572cf4
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
572cf4
+
572cf4
 /*
572cf4
 	Function: gdImageWBMPCtx
572cf4
 
572cf4
@@ -100,6 +102,12 @@ int gd_getin(void *in)
572cf4
 		out   - the stream where to write
572cf4
 */
572cf4
 BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
572cf4
+{
572cf4
+	_gdImageWBMPCtx(image, fg, out);
572cf4
+}
572cf4
+
572cf4
+/* returns 0 on success, 1 on failure */
572cf4
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
572cf4
 {
572cf4
 	int x, y, pos;
572cf4
 	Wbmp *wbmp;
572cf4
@@ -107,7 +115,7 @@ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
572cf4
 	/* create the WBMP */
572cf4
 	if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
572cf4
 		gd_error("Could not create WBMP\n");
572cf4
-		return;
572cf4
+		return 1;
572cf4
 	}
572cf4
 
572cf4
 	/* fill up the WBMP structure */
572cf4
@@ -123,11 +131,15 @@ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
572cf4
 
572cf4
 	/* write the WBMP to a gd file descriptor */
572cf4
 	if(writewbmp(wbmp, &gd_putout, out)) {
572cf4
+		freewbmp(wbmp);
572cf4
 		gd_error("Could not save WBMP\n");
572cf4
+		return 1;
572cf4
 	}
572cf4
 
572cf4
 	/* des submitted this bugfix: gdFree the memory. */
572cf4
 	freewbmp(wbmp);
572cf4
+
572cf4
+	return 0;
572cf4
 }
572cf4
 
572cf4
 /*
572cf4
@@ -271,8 +283,11 @@ BGD_DECLARE(void *) gdImageWBMPPtr(gdImagePtr im, int *size, int fg)
572cf4
 	void *rv;
572cf4
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
572cf4
 	if (out == NULL) return NULL;
572cf4
-	gdImageWBMPCtx(im, fg, out);
572cf4
-	rv = gdDPExtractData(out, size);
572cf4
+	if (!_gdImageWBMPCtx(im, fg, out)) {
572cf4
+		rv = gdDPExtractData(out, size);
572cf4
+	} else {
572cf4
+		rv = NULL;
572cf4
+	}
572cf4
 	out->gd_free(out);
572cf4
 	return rv;
572cf4
 }
572cf4
diff --git a/tests/jpeg/CMakeLists.txt b/tests/jpeg/CMakeLists.txt
572cf4
index 19964b0..a8d8162 100755
572cf4
--- a/tests/jpeg/CMakeLists.txt
572cf4
+++ b/tests/jpeg/CMakeLists.txt
572cf4
@@ -2,6 +2,7 @@ IF(JPEG_FOUND)
572cf4
 LIST(APPEND TESTS_FILES
572cf4
 	jpeg_empty_file
572cf4
 	jpeg_im2im
572cf4
+	jpeg_ptr_double_free
572cf4
 	jpeg_null
572cf4
 )
572cf4
 
572cf4
diff --git a/tests/jpeg/Makemodule.am b/tests/jpeg/Makemodule.am
572cf4
index 7e5d317..b89e169 100755
572cf4
--- a/tests/jpeg/Makemodule.am
572cf4
+++ b/tests/jpeg/Makemodule.am
572cf4
@@ -2,7 +2,8 @@ if HAVE_LIBJPEG
572cf4
 libgd_test_programs += \
572cf4
 	jpeg/jpeg_empty_file \
572cf4
 	jpeg/jpeg_im2im \
572cf4
-	jpeg/jpeg_null
572cf4
+	jpeg/jpeg_null \
572cf4
+	jpeg/jpeg_ptr_double_free
572cf4
 
572cf4
 if HAVE_LIBPNG
572cf4
 libgd_test_programs += \
572cf4
diff --git a/tests/jpeg/jpeg_ptr_double_free.c b/tests/jpeg/jpeg_ptr_double_free.c
572cf4
new file mode 100644
572cf4
index 0000000..c80aeb6
572cf4
--- /dev/null
572cf4
+++ b/tests/jpeg/jpeg_ptr_double_free.c
572cf4
@@ -0,0 +1,31 @@
572cf4
+/**
572cf4
+ * Test that failure to convert to JPEG returns NULL
572cf4
+ *
572cf4
+ * We are creating an image, set its width to zero, and pass this image to
572cf4
+ * `gdImageJpegPtr()` which is supposed to fail, and as such should return NULL.
572cf4
+ *
572cf4
+ * See also <https://github.com/libgd/libgd/issues/381>
572cf4
+ */
572cf4
+
572cf4
+
572cf4
+#include "gd.h"
572cf4
+#include "gdtest.h"
572cf4
+
572cf4
+
572cf4
+int main()
572cf4
+{
572cf4
+    gdImagePtr src, dst;
572cf4
+    int size;
572cf4
+
572cf4
+    src = gdImageCreateTrueColor(1, 10);
572cf4
+    gdTestAssert(src != NULL);
572cf4
+
572cf4
+    src->sx = 0; /* this hack forces gdImageJpegPtr() to fail */
572cf4
+
572cf4
+    dst = gdImageJpegPtr(src, &size, 0);
572cf4
+    gdTestAssert(dst == NULL);
572cf4
+
572cf4
+    gdImageDestroy(src);
572cf4
+
572cf4
+    return gdNumFailures();
572cf4
+}
572cf4
\ No newline at end of file
572cf4
-- 
572cf4
2.17.1
572cf4
572cf4