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

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