CVE-2019-6978: Double Free (CWE-415)
value: The GD Graphics Library (aka LibGD) 2.2.5 has a double free in the gdImage*Ptr() functions in gd_gif_out.c, gd_jpeg.c, and gd_wbmp.c. NOTE: PHP is unaffected.
CWE description: The product calls free() twice on the same memory address, potentially leading to modification of unexpected memory locations.
CVSS v3 base score: 9.8
Published: 2019-01-28T08:29Z
Repository: https://github.com/libgd/libgd
Language: C
Files changed: src/gd_gif_out.c, src/gd_jpeg.c, src/gd_wbmp.c, tests/jpeg/.gitignore, tests/jpeg/CMakeLists.txt, tests/jpeg/Makemodule.am, tests/jpeg/jpeg_ptr_double_free.c
Commit date: 2019-01-17 11:54:55 +0100
Commit message: Fix #492: Potential double-free in gdImage*Ptr()

Whenever `gdImage*Ptr()` calls `gdImage*Ctx()` and the latter fails, we
must not call `gdDPExtractData()`; otherwise a double-free would
happen.  Since `gdImage*Ctx()` are void functions, and we can't change
that for BC reasons, we're introducing static helpers which are used
internally.

We're adding a regression test for `gdImageJpegPtr()`, but not for
`gdImageGifPtr()` and `gdImageWbmpPtr()` since we don't know how to
trigger failure of the respective `gdImage*Ctx()` calls.

This potential security issue has been reported by Solmaz Salimi (aka.
Rooney).

Diff: diff --git a/src/gd_gif_out.c b/src/gd_gif_out.c
index 298a5812..d5a95346 100644
--- a/src/gd_gif_out.c
+++ b/src/gd_gif_out.c
@@ -97,10 +97,11 @@ static void cl_block(GifCtx *ctx);
 static void cl_hash(register count_int chsize, GifCtx *ctx);
 static void char_init(GifCtx *ctx);
 static void char_out(int c, GifCtx *ctx);
 static void flush_char(GifCtx *ctx);
 
+static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
 
 
 
 /*
   Function: gdImageGifPtr
@@ -129,12 +130,15 @@ static void flush_char(GifCtx *ctx);
 BGD_DECLARE(void *) gdImageGifPtr(gdImagePtr im, int *size)
 {
 	void *rv;
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
 	if (out == NULL) return NULL;
-	gdImageGifCtx(im, out);
-	rv = gdDPExtractData(out, size);
+	if (!_gdImageGifCtx(im, out)) {
+		rv = gdDPExtractData(out, size);
+	} else {
+		rv = NULL;
+	}
 	out->gd_free(out);
 	return rv;
 }
 
 /*
@@ -218,10 +222,16 @@ BGD_DECLARE(void) gdImageGif(gdImagePtr im, FILE *outFile)
 
     Nothing.
 
 */
 BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
+{
+	_gdImageGifCtx(im, out);
+}
+
+/* returns 0 on success, 1 on failure */
+static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
 {
 	gdImagePtr pim = 0, tim = im;
 	int interlace, BitsPerPixel;
 	interlace = im->interlace;
 
@@ -229,11 +239,11 @@ BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
 		/* Expensive, but the only way that produces an
 		acceptable result: mix down to a palette
 		based temporary image. */
 		pim = gdImageCreatePaletteFromTrueColor(im, 1, 256);
 		if(!pim) {
-			return;
+			return 1;
 		}
 		tim = pim;
 	}
 
 	BitsPerPixel = colorstobpp(tim->colorsTotal);
@@ -245,10 +255,12 @@ BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
 
 	if(pim) {
 		/* Destroy palette based temporary image. */
 		gdImageDestroy(	pim);
 	}
+
+	return 0;
 }
 
 
 /*
   Function: gdImageGifAnimBeginPtr
diff --git a/src/gd_jpeg.c b/src/gd_jpeg.c
index fc058420..96ef4302 100644
--- a/src/gd_jpeg.c
+++ b/src/gd_jpeg.c
@@ -115,10 +115,12 @@ static void fatal_jpeg_error(j_common_ptr cinfo)
 	}
 
 	exit(99);
 }
 
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality);
+
 /*
  * Write IM to OUTFILE as a JFIF-formatted JPEG image, using quality
  * QUALITY.  If QUALITY is in the range 0-100, increasing values
  * represent higher quality but also larger image size.  If QUALITY is
  * negative, the IJG JPEG library's default quality is used (which
@@ -229,12 +231,15 @@ BGD_DECLARE(void) gdImageJpeg(gdImagePtr im, FILE *outFile, int quality)
 BGD_DECLARE(void *) gdImageJpegPtr(gdImagePtr im, int *size, int quality)
 {
 	void *rv;
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
 	if (out == NULL) return NULL;
-	gdImageJpegCtx(im, out, quality);
-	rv = gdDPExtractData(out, size);
+	if (!_gdImageJpegCtx(im, out, quality)) {
+		rv = gdDPExtractData(out, size);
+	} else {
+		rv = NULL;
+	}
 	out->gd_free(out);
 	return rv;
 }
 
 void jpeg_gdIOCtx_dest(j_compress_ptr cinfo, gdIOCtx *outfile);
@@ -251,10 +256,16 @@ void jpeg_gdIOCtx_dest(j_compress_ptr cinfo, gdIOCtx *outfile);
     outfile - The output sink.
     quality - Image quality.
 
 */
 BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
+{
+	_gdImageJpegCtx(im, outfile, quality);
+}
+
+/* returns 0 on success, 1 on failure */
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
 {
 	struct jpeg_compress_struct cinfo;
 	struct jpeg_error_mgr jerr;
 	int i, j, jidx;
 	/* volatile so we can gdFree it on return from longjmp */
@@ -285,11 +296,11 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
 	if(setjmp(jmpbufw.jmpbuf) != 0) {
 		/* we're here courtesy of longjmp */
 		if(row) {
 			gdFree(row);
 		}
-		return;
+		return 1;
 	}
 
 	cinfo.err->emit_message = jpeg_emit_message;
 	cinfo.err->error_exit = fatal_jpeg_error;
 
@@ -326,11 +337,11 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
 
 	row = (JSAMPROW)gdCalloc(1, cinfo.image_width * cinfo.input_components * sizeof(JSAMPLE));
 	if(row == 0) {
 		gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
 		jpeg_destroy_compress(&cinfo);
-		return;
+		return 1;
 	}
 
 	rowptr[0] = row;
 
 	jpeg_start_compress(&cinfo, TRUE);
@@ -403,10 +414,11 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
 	}
 
 	jpeg_finish_compress(&cinfo);
 	jpeg_destroy_compress(&cinfo);
 	gdFree(row);
+	return 0;
 }
 
 
 
 
diff --git a/src/gd_wbmp.c b/src/gd_wbmp.c
index f19a1c96..a49bdbec 100644
--- a/src/gd_wbmp.c
+++ b/src/gd_wbmp.c
@@ -86,10 +86,12 @@ void gd_putout(int i, void *out)
 int gd_getin(void *in)
 {
 	return (gdGetC((gdIOCtx *)in));
 }
 
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
+
 /*
 	Function: gdImageWBMPCtx
 
 	Write the image as a wbmp file
 
@@ -98,18 +100,24 @@ int gd_getin(void *in)
 		fg    - the index of the foreground color. any other value will be
 				considered as background and will not be written
 		out   - the stream where to write
 */
 BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
+{
+	_gdImageWBMPCtx(image, fg, out);
+}
+
+/* returns 0 on success, 1 on failure */
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
 {
 	int x, y, pos;
 	Wbmp *wbmp;
 
 	/* create the WBMP */
 	if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
 		gd_error("Could not create WBMP\n");
-		return;
+		return 1;
 	}
 
 	/* fill up the WBMP structure */
 	pos = 0;
 	for(y = 0; y < gdImageSY(image); y++) {
@@ -121,15 +129,19 @@ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
 		}
 	}
 
 	/* write the WBMP to a gd file descriptor */
 	if(writewbmp(wbmp, &gd_putout, out)) {
+		freewbmp(wbmp);
 		gd_error("Could not save WBMP\n");
+		return 1;
 	}
 
 	/* des submitted this bugfix: gdFree the memory. */
 	freewbmp(wbmp);
+
+	return 0;
 }
 
 /*
   Function: gdImageCreateFromWBMPCtx
 
@@ -269,10 +281,13 @@ BGD_DECLARE(void) gdImageWBMP(gdImagePtr im, int fg, FILE *outFile)
 BGD_DECLARE(void *) gdImageWBMPPtr(gdImagePtr im, int *size, int fg)
 {
 	void *rv;
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
 	if (out == NULL) return NULL;
-	gdImageWBMPCtx(im, fg, out);
-	rv = gdDPExtractData(out, size);
+	if (!_gdImageWBMPCtx(im, fg, out)) {
+		rv = gdDPExtractData(out, size);
+	} else {
+		rv = NULL;
+	}
 	out->gd_free(out);
 	return rv;
 }
diff --git a/tests/jpeg/.gitignore b/tests/jpeg/.gitignore
index c28aa87c..13bcf049 100644
--- a/tests/jpeg/.gitignore
+++ b/tests/jpeg/.gitignore
@@ -1,7 +1,8 @@
 /bug00338
 /bug_github_18
 /jpeg_empty_file
 /jpeg_im2im
 /jpeg_null
+/jpeg_ptr_double_free
 /jpeg_read
 /jpeg_resolution
diff --git a/tests/jpeg/CMakeLists.txt b/tests/jpeg/CMakeLists.txt
index 19964b0c..a8d8162f 100644
--- a/tests/jpeg/CMakeLists.txt
+++ b/tests/jpeg/CMakeLists.txt
@@ -1,9 +1,10 @@
 IF(JPEG_FOUND)
 LIST(APPEND TESTS_FILES
 	jpeg_empty_file
 	jpeg_im2im
+	jpeg_ptr_double_free
 	jpeg_null
 )
 
 IF(PNG_FOUND)
 LIST(APPEND TESTS_FILES
diff --git a/tests/jpeg/Makemodule.am b/tests/jpeg/Makemodule.am
index 7e5d317b..b89e1695 100644
--- a/tests/jpeg/Makemodule.am
+++ b/tests/jpeg/Makemodule.am
@@ -1,10 +1,11 @@
 if HAVE_LIBJPEG
 libgd_test_programs += \
 	jpeg/jpeg_empty_file \
 	jpeg/jpeg_im2im \
-	jpeg/jpeg_null
+	jpeg/jpeg_null \
+	jpeg/jpeg_ptr_double_free
 
 if HAVE_LIBPNG
 libgd_test_programs += \
 	jpeg/bug_github_18 \
 	jpeg/bug00338 \
diff --git a/tests/jpeg/jpeg_ptr_double_free.c b/tests/jpeg/jpeg_ptr_double_free.c
new file mode 100644
index 00000000..df5a510b
--- /dev/null
+++ b/tests/jpeg/jpeg_ptr_double_free.c
@@ -0,0 +1,31 @@
+/**
+ * Test that failure to convert to JPEG returns NULL
+ *
+ * We are creating an image, set its width to zero, and pass this image to
+ * `gdImageJpegPtr()` which is supposed to fail, and as such should return NULL.
+ *
+ * See also <https://github.com/libgd/libgd/issues/381>
+ */
+
+
+#include "gd.h"
+#include "gdtest.h"
+
+
+int main()
+{
+    gdImagePtr src, dst;
+    int size;
+
+    src = gdImageCreateTrueColor(1, 10);
+    gdTestAssert(src != NULL);
+
+    src->sx = 0; /* this hack forces gdImageJpegPtr() to fail */
+
+    dst = gdImageJpegPtr(src, &size, 0);
+    gdTestAssert(dst == NULL);
+
+    gdImageDestroy(src);
+
+    return gdNumFailures();
+}

Vulnerable code: 	gdImageGifCtx(im, out);
	rv = gdDPExtractData(out, size);
			return;
	gdImageJpegCtx(im, out, quality);
	rv = gdDPExtractData(out, size);
		return;
		return;
		return;
	gdImageWBMPCtx(im, fg, out);
	rv = gdDPExtractData(out, size);
	jpeg/jpeg_null
Fixed code: static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
	if (!_gdImageGifCtx(im, out)) {
		rv = gdDPExtractData(out, size);
	} else {
		rv = NULL;
	}
{
	_gdImageGifCtx(im, out);
}

/* returns 0 on success, 1 on failure */
static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
			return 1;

	return 0;
static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality);

	if (!_gdImageJpegCtx(im, out, quality)) {
		rv = gdDPExtractData(out, size);
	} else {
		rv = NULL;
	}
{
	_gdImageJpegCtx(im, outfile, quality);
}

/* returns 0 on success, 1 on failure */
static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
		return 1;
		return 1;
	return 0;
static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);

{
	_gdImageWBMPCtx(image, fg, out);
}

/* returns 0 on success, 1 on failure */
static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
		return 1;
		freewbmp(wbmp);
		return 1;

	return 0;
	if (!_gdImageWBMPCtx(im, fg, out)) {
		rv = gdDPExtractData(out, size);
	} else {
		rv = NULL;
	}
/jpeg_ptr_double_free
	jpeg_ptr_double_free
	jpeg/jpeg_null \
	jpeg/jpeg_ptr_double_free
/**
 * Test that failure to convert to JPEG returns NULL
 *
 * We are creating an image, set its width to zero, and pass this image to
 * `gdImageJpegPtr()` which is supposed to fail, and as such should return NULL.
 *
 * See also <https://github.com/libgd/libgd/issues/381>
 */


#include "gd.h"
#include "gdtest.h"


int main()
{
    gdImagePtr src, dst;
    int size;

    src = gdImageCreateTrueColor(1, 10);
    gdTestAssert(src != NULL);

    src->sx = 0; /* this hack forces gdImageJpegPtr() to fail */

    dst = gdImageJpegPtr(src, &size, 0);
    gdTestAssert(dst == NULL);

    gdImageDestroy(src);

    return gdNumFailures();
}