Blame SOURCES/libtiff-CVE-2014-8127.patch

460672
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
460672
index 19a26e2..6667228 100644
460672
--- a/libtiff/tif_dirread.c
460672
+++ b/libtiff/tif_dirread.c
460672
@@ -3881,6 +3881,32 @@ TIFFReadDirectory(TIFF* tif)
460672
 				if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
460672
 					goto bad;
460672
 			}
460672
+			/*
460672
+			 * SamplesPerPixel value has changed, adjust SMinSampleValue
460672
+			 * and SMaxSampleValue arrays if necessary
460672
+			 */
460672
+			{
460672
+				uint32 saved_flags;
460672
+				saved_flags = tif->tif_flags;
460672
+				tif->tif_flags &= ~TIFF_PERSAMPLE;
460672
+				if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
460672
+				{
460672
+					if (!TIFFSetField(tif,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_sminsamplevalue[0]))
460672
+					{
460672
+						tif->tif_flags = saved_flags;
460672
+						goto bad;
460672
+					}
460672
+				}
460672
+				if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
460672
+				{
460672
+					if (!TIFFSetField(tif,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_smaxsamplevalue[0]))
460672
+					{
460672
+						tif->tif_flags = saved_flags;
460672
+						goto bad;
460672
+					}
460672
+				}
460672
+				tif->tif_flags = saved_flags;
460672
+			}
460672
 		}
460672
 	}
460672
 	/*
460672
diff --git a/libtiff/tif_dirwrite.c b/libtiff/tif_dirwrite.c
460672
index fa20609..fa68d1c 100644
460672
--- a/libtiff/tif_dirwrite.c
460672
+++ b/libtiff/tif_dirwrite.c
460672
@@ -542,8 +542,12 @@ TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
460672
 			{
460672
 				if (!isTiled(tif))
460672
 				{
460672
-					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
460672
-						goto bad;
460672
+					/* td_stripoffset can be NULL even if td_nstrips == 1 due to OJPEG hack */
460672
+					if (tif->tif_dir.td_stripoffset)
460672
+					{
460672
+						if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
460672
+							goto bad;
460672
+					}
460672
 				}
460672
 				else
460672
 				{
460672
diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
460672
index 2ba822a..dfc5b07 100644
460672
--- a/libtiff/tif_read.c
460672
+++ b/libtiff/tif_read.c
460672
@@ -458,7 +458,7 @@ TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
460672
 		return ((tmsize_t)(-1));
460672
 	}
460672
 	bytecount = td->td_stripbytecount[strip];
460672
-	if (bytecount <= 0) {
460672
+	if ((int64)bytecount <= 0) {
460672
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
460672
 		TIFFErrorExt(tif->tif_clientdata, module,
460672
 			     "%I64u: Invalid strip byte count, strip %lu",
460672
@@ -498,7 +498,7 @@ TIFFFillStrip(TIFF* tif, uint32 strip)
460672
 	if ((tif->tif_flags&TIFF_NOREADRAW)==0)
460672
 	{
460672
 		uint64 bytecount = td->td_stripbytecount[strip];
460672
-		if (bytecount <= 0) {
460672
+		if ((int64)bytecount <= 0) {
460672
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
460672
 			TIFFErrorExt(tif->tif_clientdata, module,
460672
 				"Invalid strip byte count %I64u, strip %lu",
460672
@@ -801,7 +801,7 @@ TIFFFillTile(TIFF* tif, uint32 tile)
460672
 	if ((tif->tif_flags&TIFF_NOREADRAW)==0)
460672
 	{
460672
 		uint64 bytecount = td->td_stripbytecount[tile];
460672
-		if (bytecount <= 0) {
460672
+		if ((int64)bytecount <= 0) {
460672
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
460672
 			TIFFErrorExt(tif->tif_clientdata, module,
460672
 				"%I64u: Invalid tile byte count, tile %lu",
460672
diff --git a/tools/bmp2tiff.c b/tools/bmp2tiff.c
460672
index b5ed30b..376f4e6 100644
460672
--- a/tools/bmp2tiff.c
460672
+++ b/tools/bmp2tiff.c
460672
@@ -401,6 +401,24 @@ main(int argc, char* argv[])
460672
 		    return 0;
460672
 		}
460672
 
460672
+        if (info_hdr.iCompression == BMPC_RLE4 && info_hdr.iBitCount != 4)
460672
+        {
460672
+            TIFFError(infilename,
460672
+              "Cannot process BMP file with bit count %d and RLE 4-bit/pixel compression",
460672
+              info_hdr.iBitCount);
460672
+            close(fd);
460672
+            return 0;
460672
+        }
460672
+ 
460672
+        if (info_hdr.iCompression == BMPC_RLE8 && info_hdr.iBitCount != 8)
460672
+        {
460672
+            TIFFError(infilename,
460672
+              "Cannot process BMP file with bit count %d and RLE 8-bit/pixel compression",
460672
+              info_hdr.iBitCount);
460672
+            close(fd);
460672
+            return 0;
460672
+        }
460672
+
460672
 		width = info_hdr.iWidth;
460672
 		length = (info_hdr.iHeight > 0) ? info_hdr.iHeight : -info_hdr.iHeight;
460672
         if( width <= 0 || length <= 0 )
460672
diff --git a/tools/pal2rgb.c b/tools/pal2rgb.c
460672
index 8608aad..426bbc0 100644
460672
--- a/tools/pal2rgb.c
460672
+++ b/tools/pal2rgb.c
460672
@@ -372,7 +372,8 @@ static struct cpTag {
460672
     { TIFFTAG_CLEANFAXDATA,		1, TIFF_SHORT },
460672
     { TIFFTAG_CONSECUTIVEBADFAXLINES,	1, TIFF_LONG },
460672
     { TIFFTAG_INKSET,			1, TIFF_SHORT },
460672
-    { TIFFTAG_INKNAMES,			1, TIFF_ASCII },
460672
+    // disable INKNAMES tag, http://bugzilla.maptools.org/show_bug.cgi?id=2484 (CVE-2014-8127)
460672
+    //{ TIFFTAG_INKNAMES,			1, TIFF_ASCII },
460672
     { TIFFTAG_DOTRANGE,			2, TIFF_SHORT },
460672
     { TIFFTAG_TARGETPRINTER,		1, TIFF_ASCII },
460672
     { TIFFTAG_SAMPLEFORMAT,		1, TIFF_SHORT },
460672
diff --git a/tools/thumbnail.c b/tools/thumbnail.c
460672
index fd1cba5..06edf93 100644
460672
--- a/tools/thumbnail.c
460672
+++ b/tools/thumbnail.c
460672
@@ -257,7 +257,8 @@ static struct cpTag {
460672
     { TIFFTAG_CLEANFAXDATA,		1, TIFF_SHORT },
460672
     { TIFFTAG_CONSECUTIVEBADFAXLINES,	1, TIFF_LONG },
460672
     { TIFFTAG_INKSET,			1, TIFF_SHORT },
460672
-    { TIFFTAG_INKNAMES,			1, TIFF_ASCII },
460672
+    // disable INKNAMES tag, http://bugzilla.maptools.org/show_bug.cgi?id=2484 (CVE-2014-8127)
460672
+    //{ TIFFTAG_INKNAMES,			1, TIFF_ASCII },
460672
     { TIFFTAG_DOTRANGE,			2, TIFF_SHORT },
460672
     { TIFFTAG_TARGETPRINTER,		1, TIFF_ASCII },
460672
     { TIFFTAG_SAMPLEFORMAT,		1, TIFF_SHORT },
460672
@@ -585,7 +586,7 @@ generateThumbnail(TIFF* in, TIFF* out)
460672
     rowsize = TIFFScanlineSize(in);
460672
     rastersize = sh * rowsize;
460672
     fprintf(stderr, "rastersize=%u\n", (unsigned int)rastersize);
460672
-    raster = (unsigned char*)_TIFFmalloc(rastersize);
460672
+    raster = (unsigned char*)_TIFFmalloc(rastersize + 3);
460672
     if (!raster) {
460672
 	    TIFFError(TIFFFileName(in),
460672
 		      "Can't allocate space for raster buffer.");
460672
diff --git a/tools/tiff2bw.c b/tools/tiff2bw.c
460672
index c5dcb7c..02605df 100644
460672
--- a/tools/tiff2bw.c
460672
+++ b/tools/tiff2bw.c
460672
@@ -171,6 +171,11 @@ main(int argc, char* argv[])
460672
 		    argv[optind], samplesperpixel);
460672
 		return (-1);
460672
 	}
460672
+	if( photometric == PHOTOMETRIC_RGB && samplesperpixel != 3) {
460672
+		fprintf(stderr, "%s: Bad samples/pixel %u for PHOTOMETRIC_RGB.\n",
460672
+		    argv[optind], samplesperpixel);
460672
+		return (-1);
460672
+	}
460672
 	TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
460672
 	if (bitspersample != 8) {
460672
 		fprintf(stderr,