Blame SOURCES/libtiff-CVE-2012-4564.patch

6456d8
Upstream patch for CVE-2012-4564.
6456d8
6456d8
6456d8
diff -Naur tiff-3.9.4.orig/tools/ppm2tiff.c tiff-3.9.4/tools/ppm2tiff.c
6456d8
--- tiff-3.9.4.orig/tools/ppm2tiff.c	2010-06-08 14:50:44.000000000 -0400
6456d8
+++ tiff-3.9.4/tools/ppm2tiff.c	2012-12-10 16:16:05.154045877 -0500
6456d8
@@ -68,6 +68,17 @@
6456d8
 	exit(-2);
6456d8
 }
6456d8
 
6456d8
+static tsize_t
6456d8
+multiply_ms(tsize_t m1, tsize_t m2)
6456d8
+{
6456d8
+	tsize_t bytes = m1 * m2;
6456d8
+
6456d8
+	if (m1 && bytes / m1 != m2)
6456d8
+		bytes = 0;
6456d8
+
6456d8
+	return bytes;
6456d8
+}
6456d8
+
6456d8
 int
6456d8
 main(int argc, char* argv[])
6456d8
 {
6456d8
@@ -85,6 +96,7 @@
6456d8
 	int c;
6456d8
 	extern int optind;
6456d8
 	extern char* optarg;
6456d8
+	tsize_t scanline_size;
6456d8
 
6456d8
 	if (argc < 2) {
6456d8
 	    fprintf(stderr, "%s: Too few arguments\n", argv[0]);
6456d8
@@ -217,7 +229,8 @@
6456d8
 	}
6456d8
 	switch (bpp) {
6456d8
 		case 1:
6456d8
-			linebytes = (spp * w + (8 - 1)) / 8;
6456d8
+			/* if round-up overflows, result will be zero, OK */
6456d8
+			linebytes = (multiply_ms(spp, w) + (8 - 1)) / 8;
6456d8
 			if (rowsperstrip == (uint32) -1) {
6456d8
 				TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, h);
6456d8
 			} else {
6456d8
@@ -226,15 +239,31 @@
6456d8
 			}
6456d8
 			break;
6456d8
 		case 8:
6456d8
-			linebytes = spp * w;
6456d8
+			linebytes = multiply_ms(spp, w);
6456d8
 			TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
6456d8
 			    TIFFDefaultStripSize(out, rowsperstrip));
6456d8
 			break;
6456d8
 	}
6456d8
-	if (TIFFScanlineSize(out) > linebytes)
6456d8
+	if (linebytes == 0) {
6456d8
+		fprintf(stderr, "%s: scanline size overflow\n", infile);
6456d8
+		(void) TIFFClose(out);
6456d8
+		exit(-2);					
6456d8
+	}
6456d8
+	scanline_size = TIFFScanlineSize(out);
6456d8
+	if (scanline_size == 0) {
6456d8
+		/* overflow - TIFFScanlineSize already printed a message */
6456d8
+		(void) TIFFClose(out);
6456d8
+		exit(-2);					
6456d8
+	}
6456d8
+	if (scanline_size < linebytes)
6456d8
 		buf = (unsigned char *)_TIFFmalloc(linebytes);
6456d8
 	else
6456d8
-		buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
6456d8
+		buf = (unsigned char *)_TIFFmalloc(scanline_size);
6456d8
+	if (buf == NULL) {
6456d8
+		fprintf(stderr, "%s: Not enough memory\n", infile);
6456d8
+		(void) TIFFClose(out);
6456d8
+		exit(-2);
6456d8
+	}
6456d8
 	if (resolution > 0) {
6456d8
 		TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
6456d8
 		TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);