Blame SOURCES/libtiff-CVE-2018-18661.patch

21ff78
From 20dbecdf69cf0209ad0246707aaf142bb1fee96e Mon Sep 17 00:00:00 2001
21ff78
From: Even Rouault <even.rouault@spatialys.com>
21ff78
Date: Tue, 30 Oct 2018 18:50:27 +0100
21ff78
Subject: [PATCH] tiff2bw: avoid null pointer dereference in case of out of
21ff78
 memory situation. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2819 /
21ff78
 CVE-2018-18661
21ff78
21ff78
---
21ff78
 libtiff/tiffiop.h |  1 +
21ff78
 tools/tiff2bw.c   | 30 ++++++++++++++++++++++++++----
21ff78
 tools/tiffcrop.c  |  5 -----
21ff78
 3 files changed, 27 insertions(+), 9 deletions(-)
21ff78
21ff78
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
21ff78
index daa291c..08e5dc4 100644
21ff78
--- a/libtiff/tiffiop.h
21ff78
+++ b/libtiff/tiffiop.h
21ff78
@@ -72,6 +72,7 @@ extern int snprintf(char* str, size_t size, const char* format, ...);
21ff78
 #endif
21ff78
 
21ff78
 #define    streq(a,b)      (strcmp(a,b) == 0)
21ff78
+#define    strneq(a,b,n)   (strncmp(a,b,n) == 0)
21ff78
 
21ff78
 #ifndef TRUE
21ff78
 #define	TRUE	1
21ff78
diff --git a/tools/tiff2bw.c b/tools/tiff2bw.c
21ff78
index dad54af..1f3bb2c 100644
21ff78
--- a/tools/tiff2bw.c
21ff78
+++ b/tools/tiff2bw.c
21ff78
@@ -40,9 +40,7 @@
21ff78
 #endif
21ff78
 
21ff78
 #include "tiffio.h"
21ff78
-
21ff78
-#define	streq(a,b)	(strcmp((a),(b)) == 0)
21ff78
-#define	strneq(a,b,n)	(strncmp(a,b,n) == 0)
21ff78
+#include "tiffiop.h"
21ff78
 
21ff78
 /* x% weighting -> fraction of full color */
21ff78
 #define	PCT(x)	(((x)*256+50)/100)
21ff78
@@ -223,6 +221,11 @@ main(int argc, char* argv[])
21ff78
 	TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
21ff78
 	TIFFSetField(out, TIFFTAG_SOFTWARE, "tiff2bw");
21ff78
 	outbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
21ff78
+        if( !outbuf )
21ff78
+        {
21ff78
+            fprintf(stderr, "Out of memory\n");
21ff78
+            goto tiff2bw_error;
21ff78
+        }
21ff78
 	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
21ff78
 	    TIFFDefaultStripSize(out, rowsperstrip));
21ff78
 
21ff78
@@ -246,6 +249,11 @@ main(int argc, char* argv[])
21ff78
 #undef CVT
21ff78
 		}
21ff78
 		inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
21ff78
+                if( !inbuf )
21ff78
+                {
21ff78
+                    fprintf(stderr, "Out of memory\n");
21ff78
+                    goto tiff2bw_error;
21ff78
+                }
21ff78
 		for (row = 0; row < h; row++) {
21ff78
 			if (TIFFReadScanline(in, inbuf, row, 0) < 0)
21ff78
 				break;
21ff78
@@ -256,6 +264,11 @@ main(int argc, char* argv[])
21ff78
 		break;
21ff78
 	case pack(PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG):
21ff78
 		inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
21ff78
+                if( !inbuf )
21ff78
+                {
21ff78
+                    fprintf(stderr, "Out of memory\n");
21ff78
+                    goto tiff2bw_error;
21ff78
+                }
21ff78
 		for (row = 0; row < h; row++) {
21ff78
 			if (TIFFReadScanline(in, inbuf, row, 0) < 0)
21ff78
 				break;
21ff78
@@ -265,8 +278,16 @@ main(int argc, char* argv[])
21ff78
 		}
21ff78
 		break;
21ff78
 	case pack(PHOTOMETRIC_RGB, PLANARCONFIG_SEPARATE):
21ff78
+        {
21ff78
+                tmsize_t inbufsize;
21ff78
 		rowsize = TIFFScanlineSize(in);
21ff78
-		inbuf = (unsigned char *)_TIFFmalloc(3*rowsize);
21ff78
+                inbufsize = TIFFSafeMultiply(tmsize_t, 3, rowsize);
21ff78
+		inbuf = (unsigned char *)_TIFFmalloc(inbufsize);
21ff78
+                if( !inbuf )
21ff78
+                {
21ff78
+                    fprintf(stderr, "Out of memory\n");
21ff78
+                    goto tiff2bw_error;
21ff78
+                }
21ff78
 		for (row = 0; row < h; row++) {
21ff78
 			for (s = 0; s < 3; s++)
21ff78
 				if (TIFFReadScanline(in,
21ff78
@@ -278,6 +299,7 @@ main(int argc, char* argv[])
21ff78
 				break;
21ff78
 		}
21ff78
 		break;
21ff78
+        }
21ff78
 	}
21ff78
 #undef pack
21ff78
         if (inbuf)
21ff78
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
21ff78
index c60cb38..3862b1c 100644
21ff78
--- a/tools/tiffcrop.c
21ff78
+++ b/tools/tiffcrop.c
21ff78
@@ -150,11 +150,6 @@ extern int getopt(int argc, char * const argv[], const char *optstring);
21ff78
 
21ff78
 #define TIFF_UINT32_MAX     0xFFFFFFFFU
21ff78
 
21ff78
-#ifndef streq
21ff78
-#define	streq(a,b)	(strcmp((a),(b)) == 0)
21ff78
-#endif
21ff78
-#define	strneq(a,b,n)	(strncmp((a),(b),(n)) == 0)
21ff78
-
21ff78
 #define	TRUE	1
21ff78
 #define	FALSE	0
21ff78
 
21ff78
-- 
21ff78
2.17.2
21ff78