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