CVE-2018-14553: NULL Pointer Dereference (CWE-476)
value: gdImageClone in gd.c in libgd 2.1.0-rc2 through 2.2.5 has a NULL pointer dereference allowing attackers to crash an application via a specific function call sequence. Only affects PHP when linked with an external libgd (not bundled).
CWE description: The product dereferences a pointer that it expects to be valid but is NULL.
CVSS v3 base score: 7.5
Published: 2020-02-11T13:15Z
Repository: https://github.com/libgd/libgd
Language: C
Files changed: src/gd.c, tests/gdimageclone/.gitignore, tests/gdimageclone/CMakeLists.txt, tests/gdimageclone/Makemodule.am, tests/gdimageclone/style.c
Commit date: 2019-12-20 17:19:54 +0100
Commit message: Fix potential NULL pointer dereference in gdImageClone()

Diff: diff --git a/src/gd.c b/src/gd.c
index 592a0286..d564d1f9 100644
--- a/src/gd.c
+++ b/src/gd.c
@@ -2863,18 +2863,10 @@ BGD_DECLARE(gdImagePtr) gdImageClone (gdImagePtr src) {
 				dst->tpixels[i][x] = src->tpixels[i][x];
 			}
 		}
 	}
 
-	if (src->styleLength > 0) {
-		dst->styleLength = src->styleLength;
-		dst->stylePos    = src->stylePos;
-		for (i = 0; i < src->styleLength; i++) {
-			dst->style[i] = src->style[i];
-		}
-	}
-
 	dst->interlace   = src->interlace;
 
 	dst->alphaBlendingFlag = src->alphaBlendingFlag;
 	dst->saveAlphaFlag     = src->saveAlphaFlag;
 	dst->AA                = src->AA;
@@ -2905,10 +2897,11 @@ BGD_DECLARE(gdImagePtr) gdImageClone (gdImagePtr src) {
 		dst->tile = gdImageClone(src->tile);
 	}
 
 	if (src->style) {
 		gdImageSetStyle(dst, src->style, src->styleLength);
+		dst->stylePos = src->stylePos;
 	}
 
 	for (i = 0; i < gdMaxColors; i++) {
 		dst->brushColorMap[i] = src->brushColorMap[i];
 		dst->tileColorMap[i] = src->tileColorMap[i];
diff --git a/tests/gdimageclone/.gitignore b/tests/gdimageclone/.gitignore
index a70782d7..f4129cc7 100644
--- a/tests/gdimageclone/.gitignore
+++ b/tests/gdimageclone/.gitignore
@@ -1 +1,2 @@
 /bug00300
+/style
diff --git a/tests/gdimageclone/CMakeLists.txt b/tests/gdimageclone/CMakeLists.txt
index e6ccc318..662f4e96 100644
--- a/tests/gdimageclone/CMakeLists.txt
+++ b/tests/gdimageclone/CMakeLists.txt
@@ -1,5 +1,6 @@
 LIST(APPEND TESTS_FILES
 	bug00300
+	style
 )
 
 ADD_GD_TESTS()
diff --git a/tests/gdimageclone/Makemodule.am b/tests/gdimageclone/Makemodule.am
index 4b1b54c0..51abf5c1 100644
--- a/tests/gdimageclone/Makemodule.am
+++ b/tests/gdimageclone/Makemodule.am
@@ -1,5 +1,6 @@
 libgd_test_programs += \
-	gdimageclone/bug00300
+	gdimageclone/bug00300 \
+	gdimageclone/style
 
 EXTRA_DIST += \
 	gdimageclone/CMakeLists.txt
diff --git a/tests/gdimageclone/style.c b/tests/gdimageclone/style.c
new file mode 100644
index 00000000..c2b246ed
--- /dev/null
+++ b/tests/gdimageclone/style.c
@@ -0,0 +1,30 @@
+/**
+ * Cloning an image should exactly reproduce all style related data
+ */
+
+
+#include <string.h>
+#include "gd.h"
+#include "gdtest.h"
+
+
+int main()
+{
+    gdImagePtr im, clone;
+    int style[] = {0, 0, 0};
+
+    im = gdImageCreate(8, 8);
+    gdImageSetStyle(im, style, sizeof(style)/sizeof(style[0]));
+
+    clone = gdImageClone(im);
+    gdTestAssert(clone != NULL);
+
+    gdTestAssert(clone->styleLength == im->styleLength);
+    gdTestAssert(clone->stylePos == im->stylePos);
+    gdTestAssert(!memcmp(clone->style, im->style, sizeof(style)/sizeof(style[0])));
+
+    gdImageDestroy(clone);
+    gdImageDestroy(im);
+
+    return gdNumFailures();
+}

Vulnerable code: 	if (src->styleLength > 0) {
		dst->styleLength = src->styleLength;
		dst->stylePos    = src->stylePos;
		for (i = 0; i < src->styleLength; i++) {
			dst->style[i] = src->style[i];
		}
	}

	gdimageclone/bug00300
Fixed code: 		dst->stylePos = src->stylePos;
/style
	style
	gdimageclone/bug00300 \
	gdimageclone/style
/**
 * Cloning an image should exactly reproduce all style related data
 */


#include <string.h>
#include "gd.h"
#include "gdtest.h"


int main()
{
    gdImagePtr im, clone;
    int style[] = {0, 0, 0};

    im = gdImageCreate(8, 8);
    gdImageSetStyle(im, style, sizeof(style)/sizeof(style[0]));

    clone = gdImageClone(im);
    gdTestAssert(clone != NULL);

    gdTestAssert(clone->styleLength == im->styleLength);
    gdTestAssert(clone->stylePos == im->stylePos);
    gdTestAssert(!memcmp(clone->style, im->style, sizeof(style)/sizeof(style[0])));

    gdImageDestroy(clone);
    gdImageDestroy(im);

    return gdNumFailures();
}