Vulnerability: gd_xbm.c in the GD Graphics Library (aka libgd) before 2.2.0, as used in certain custom PHP 5.5.x configurations, allows context-dependent attackers to obtain sensitive information from process memory or cause a denial of service (stack-based buffer under-read and application crash) via a long name.
Vulnerability ID: CVE-2016-5116
CWE: CWE-119
Severity score: 4.9
Project: https://github.com/libgd/libgd
Published: 2016-08-07
File: src/gd_xbm.c
Commit message: xbm: avoid stack overflow (read) with large names #211

We use the name passed in to printf into a local stack buffer which is
limited to 4000 bytes.  So given a large enough value, lots of stack
data is leaked.  Rewrite the code to do simple memory copies with most
of the strings to avoid that issue, and only use stack buffer for small
numbers of constant size.

This closes #211.
Commit author: Mike Frysinger

Vulnerable version (before fix):
 BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm(FILE * fd) /* {{{ gdCtxPrintf */ static void gdCtxPrintf(gdIOCtx * out, const char *format, ...) { 	char buf[4096]; 	int len; 	va_list args;  static void gdCtxPrintf(gdIOCtx * out, const char *format, ...) } /* }}} */  /* {{{ gdImageXbmCtx */ BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out) { BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC 		} 	}  	gdCtxPrintf(out, "#define %s_width %d\n", name, gdImageSX(image)); 	gdCtxPrintf(out, "#define %s_height %d\n", name, gdImageSY(image)); 	gdCtxPrintf(out, "static unsigned char %s_bits[] = {\n  ", name);  	free(name);  BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC 			if ((b == 128) || (x == sx && y == sy)) { 				b = 1; 				if (p) { 					gdCtxPrintf(out, ", "); 					if (!(p%12)) { 						gdCtxPrintf(out, "\n  "); 						p = 12; 					} 				} BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC 			} 		} 	} 	gdCtxPrintf(out, "};\n"); } /* }}} */

Fixed version (after fix):
 BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm(FILE * fd) /* {{{ gdCtxPrintf */ static void gdCtxPrintf(gdIOCtx * out, const char *format, ...) { 	char buf[1024]; 	int len; 	va_list args;  static void gdCtxPrintf(gdIOCtx * out, const char *format, ...) } /* }}} */  /* The compiler will optimize strlen(constant) to a constant number. */ #define gdCtxPuts(out, s) out->putBuf(out, s, strlen(s))  /* {{{ gdImageXbmCtx */ BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out) { BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC 		} 	}  	/* Since "name" comes from the user, run it through a direct puts. 	 * Trying to printf it into a local buffer means we'd need a large 	 * or dynamic buffer to hold it all. */  	/* #define <name>_width 1234 */ 	gdCtxPuts(out, "#define "); 	gdCtxPuts(out, name); 	gdCtxPuts(out, "_width "); 	gdCtxPrintf(out, "%d\n", gdImageSX(image));  	/* #define <name>_height 1234 */ 	gdCtxPuts(out, "#define "); 	gdCtxPuts(out, name); 	gdCtxPuts(out, "_height "); 	gdCtxPrintf(out, "%d\n", gdImageSY(image));  	/* static unsigned char <name>_bits[] = {\n */ 	gdCtxPuts(out, "static unsigned char "); 	gdCtxPuts(out, name); 	gdCtxPuts(out, "_bits[] = {\n  ");  	free(name);  BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC 			if ((b == 128) || (x == sx && y == sy)) { 				b = 1; 				if (p) { 					gdCtxPuts(out, ", "); 					if (!(p%12)) { 						gdCtxPuts(out, "\n  "); 						p = 12; 					} 				} BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC 			} 		} 	} 	gdCtxPuts(out, "};\n"); } /* }}} */

Patch:
patch: @@ -180,7 +180,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm(FILE * fd)
 /* {{{ gdCtxPrintf */
 static void gdCtxPrintf(gdIOCtx * out, const char *format, ...)
 {
-	char buf[4096];
+	char buf[1024];
 	int len;
 	va_list args;
 
@@ -191,6 +191,9 @@ static void gdCtxPrintf(gdIOCtx * out, const char *format, ...)
 }
 /* }}} */
 
+/* The compiler will optimize strlen(constant) to a constant number. */
+#define gdCtxPuts(out, s) out->putBuf(out, s, strlen(s))
+
 /* {{{ gdImageXbmCtx */
 BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out)
 {
@@ -215,9 +218,26 @@ BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC
 		}
 	}
 
-	gdCtxPrintf(out, "#define %s_width %d\n", name, gdImageSX(image));
-	gdCtxPrintf(out, "#define %s_height %d\n", name, gdImageSY(image));
-	gdCtxPrintf(out, "static unsigned char %s_bits[] = {\n  ", name);
+	/* Since "name" comes from the user, run it through a direct puts.
+	 * Trying to printf it into a local buffer means we'd need a large
+	 * or dynamic buffer to hold it all. */
+
+	/* #define <name>_width 1234 */
+	gdCtxPuts(out, "#define ");
+	gdCtxPuts(out, name);
+	gdCtxPuts(out, "_width ");
+	gdCtxPrintf(out, "%d\n", gdImageSX(image));
+
+	/* #define <name>_height 1234 */
+	gdCtxPuts(out, "#define ");
+	gdCtxPuts(out, name);
+	gdCtxPuts(out, "_height ");
+	gdCtxPrintf(out, "%d\n", gdImageSY(image));
+
+	/* static unsigned char <name>_bits[] = {\n */
+	gdCtxPuts(out, "static unsigned char ");
+	gdCtxPuts(out, name);
+	gdCtxPuts(out, "_bits[] = {\n  ");
 
 	free(name);
 
@@ -234,9 +254,9 @@ BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC
 			if ((b == 128) || (x == sx && y == sy)) {
 				b = 1;
 				if (p) {
-					gdCtxPrintf(out, ", ");
+					gdCtxPuts(out, ", ");
 					if (!(p%12)) {
-						gdCtxPrintf(out, "\n  ");
+						gdCtxPuts(out, "\n  ");
 						p = 12;
 					}
 				}
@@ -248,6 +268,6 @@ BGD_DECLARE(void) gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOC
 			}
 		}
 	}
-	gdCtxPrintf(out, "};\n");
+	gdCtxPuts(out, "};\n");
 }
 /* }}} */
path: src/gd_xbm.c