CVE-2016-9537: Out-of-bounds Write (CWE-787)
value: tools/tiffcrop.c in libtiff 4.0.6 has out-of-bounds write vulnerabilities in buffers. Reported as MSVR 35093, MSVR 35096, and MSVR 35097.
CWE description: The product writes data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 9.8
Published: 2016-11-22T19:59Z
Repository: https://github.com/vadz/libtiff
Language: C
Files changed: ChangeLog, libtiff/tif_pixarlog.c, libtiff/tif_write.c, tools/tiff2pdf.c, tools/tiffcrop.c
Commit date: 2016-09-23 22:12:18 +0000
Commit message: * tools/tiffcrop.c: fix various out-of-bounds write vulnerabilities
in heap or stack allocated buffers. Reported as MSVR 35093,
MSVR 35096 and MSVR 35097. Discovered by Axel Souchet and Vishal
Chauhan from the MSRC Vulnerabilities & Mitigations team.
* tools/tiff2pdf.c: fix out-of-bounds write vulnerabilities in
heap allocate buffer in t2p_process_jpeg_strip(). Reported as MSVR
35098. Discovered by Axel Souchet and Vishal Chauhan from the MSRC
Vulnerabilities & Mitigations team.
* libtiff/tif_pixarlog.c: fix out-of-bounds write vulnerabilities
in heap allocated buffers. Reported as MSVR 35094. Discovered by
Axel Souchet and Vishal Chauhan from the MSRC Vulnerabilities &
Mitigations team.
* libtiff/tif_write.c: fix issue in error code path of TIFFFlushData1()
that didn't reset the tif_rawcc and tif_rawcp members. I'm not
completely sure if that could happen in practice outside of the odd
behaviour of t2p_seekproc() of tiff2pdf). The report points that a
better fix could be to check the return value of TIFFFlushData1() in
places where it isn't done currently, but it seems this patch is enough.
Reported as MSVR 35095. Discovered by Axel Souchet & Vishal Chauhan &
Suha Can from the MSRC Vulnerabilities & Mitigations team.

Diff: diff --git a/ChangeLog b/ChangeLog
index 8e7dea30f..26d6f47dd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,28 @@
+2016-09-23 Even Rouault <even.rouault at spatialys.com>
+
+	* tools/tiffcrop.c: fix various out-of-bounds write vulnerabilities
+	in heap or stack allocated buffers. Reported as MSVR 35093,
+	MSVR 35096 and MSVR 35097. Discovered by Axel Souchet and Vishal
+	Chauhan from the MSRC Vulnerabilities & Mitigations team.
+	* tools/tiff2pdf.c: fix out-of-bounds write vulnerabilities in
+	heap allocate buffer in t2p_process_jpeg_strip(). Reported as MSVR
+	35098. Discovered by Axel Souchet and Vishal Chauhan from the MSRC
+	Vulnerabilities & Mitigations team.
+	* libtiff/tif_pixarlog.c: fix out-of-bounds write vulnerabilities
+	in heap allocated buffers. Reported as MSVR 35094. Discovered by
+	Axel Souchet and Vishal Chauhan from the MSRC Vulnerabilities &
+	Mitigations team.
+	* libtiff/tif_write.c: fix issue in error code path of TIFFFlushData1()
+	that didn't reset the tif_rawcc and tif_rawcp members. I'm not
+	completely sure if that could happen in practice outside of the odd
+	behaviour of t2p_seekproc() of tiff2pdf). The report points that a
+	better fix could be to check the return value of TIFFFlushData1() in
+	places where it isn't done currently, but it seems this patch is enough. 
+	Reported as MSVR 35095. Discovered by Axel Souchet & Vishal Chauhan &
+	Suha Can from the MSRC Vulnerabilities & Mitigations team.
+
 2016-09-20  Bob Friesenhahn  <bfriesen@simple.dallas.tx.us>
 
 	* html/man/index.html: Comment out links to documentation for
 	abandoned utilities.
 
diff --git a/libtiff/tif_pixarlog.c b/libtiff/tif_pixarlog.c
index 1fb8f3b84..d1246c3df 100644
--- a/libtiff/tif_pixarlog.c
+++ b/libtiff/tif_pixarlog.c
@@ -981,21 +981,18 @@ horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
 		g1 = (int32) CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
 		b1 = (int32) CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
 		a1 = (int32) CLAMP(ip[3]); wp[3] = (uint16)((a1-a2) & mask); a2 = a1;
 	    }
 	} else {
-	    ip += n - 1;	/* point to last one */
-	    wp += n - 1;	/* point to last one */
-	    n -= stride;
-	    while (n > 0) {
-		REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
-				wp[stride] -= wp[0];
-				wp[stride] &= mask;
-				wp--; ip--)
-		n -= stride;
-	    }
-	    REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
+        REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp++; ip++)
+        n -= stride;
+        while (n > 0) {
+            REPEAT(stride,
+                wp[0] = (uint16)(((int32)CLAMP(ip[0])-(int32)CLAMP(ip[-stride])) & mask);
+                wp++; ip++)
+            n -= stride;
+        }
 	}
     }
 }
 
 static void
@@ -1034,21 +1031,18 @@ horizontalDifference16(unsigned short *ip, int n, int stride,
 		g1 = CLAMP(ip[1]); wp[1] = (uint16)((g1-g2) & mask); g2 = g1;
 		b1 = CLAMP(ip[2]); wp[2] = (uint16)((b1-b2) & mask); b2 = b1;
 		a1 = CLAMP(ip[3]); wp[3] = (uint16)((a1-a2) & mask); a2 = a1;
 	    }
 	} else {
-	    ip += n - 1;	/* point to last one */
-	    wp += n - 1;	/* point to last one */
+        REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
 	    n -= stride;
 	    while (n > 0) {
-		REPEAT(stride, wp[0] = CLAMP(ip[0]);
-				wp[stride] -= wp[0];
-				wp[stride] &= mask;
-				wp--; ip--)
-		n -= stride;
-	    }
-	    REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
+            REPEAT(stride,
+                wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
+                wp++; ip++)
+            n -= stride;
+        }
 	}
     }
 }
 
 
@@ -1087,22 +1081,19 @@ horizontalDifference8(unsigned char *ip, int n, int stride,
 		a1 = CLAMP(ip[7]); wp[7] = (uint16)((a1-a2) & mask); a2 = a1;
 		wp += 4;
 		ip += 4;
 	    }
 	} else {
-	    wp += n + stride - 1;	/* point to last one */
-	    ip += n + stride - 1;	/* point to last one */
-	    n -= stride;
-	    while (n > 0) {
-		REPEAT(stride, wp[0] = CLAMP(ip[0]);
-				wp[stride] -= wp[0];
-				wp[stride] &= mask;
-				wp--; ip--)
-		n -= stride;
-	    }
-	    REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
-	}
+        REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
+        n -= stride;
+        while (n > 0) {
+            REPEAT(stride,
+                wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
+                wp++; ip++)
+            n -= stride;
+        }
+    }
     }
 }
 
 /*
  * Encode a chunk of pixels.
diff --git a/libtiff/tif_write.c b/libtiff/tif_write.c
index f9a3fc0db..d8fa8023e 100644
--- a/libtiff/tif_write.c
+++ b/libtiff/tif_write.c
@@ -796,11 +796,18 @@ TIFFFlushData1(TIFF* tif)
 			TIFFReverseBits((uint8*)tif->tif_rawdata,
 			    tif->tif_rawcc);
 		if (!TIFFAppendToStrip(tif,
 		    isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
 		    tif->tif_rawdata, tif->tif_rawcc))
+        {
+            /* We update those variables even in case of error since there's */
+            /* code that doesn't really check the return code of this */
+            /* function */
+            tif->tif_rawcc = 0;
+            tif->tif_rawcp = tif->tif_rawdata;
 			return (0);
+        }
 		tif->tif_rawcc = 0;
 		tif->tif_rawcp = tif->tif_rawdata;
 	}
 	return (1);
 }
diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
index dcd5a7ef5..f8df6b522 100644
--- a/tools/tiff2pdf.c
+++ b/tools/tiff2pdf.c
@@ -284,11 +284,11 @@ tsize_t t2p_readwrite_pdf_image(T2P*, TIFF*, TIFF*);
 tsize_t t2p_readwrite_pdf_image_tile(T2P*, TIFF*, TIFF*, ttile_t);
 #ifdef OJPEG_SUPPORT
 int t2p_process_ojpeg_tables(T2P*, TIFF*);
 #endif
 #ifdef JPEG_SUPPORT
-int t2p_process_jpeg_strip(unsigned char*, tsize_t*, unsigned char*, tsize_t*, tstrip_t, uint32);
+int t2p_process_jpeg_strip(unsigned char*, tsize_t*, unsigned char*, tsize_t, tsize_t*, tstrip_t, uint32);
 #endif
 void t2p_tile_collapse_left(tdata_t, tsize_t, uint32, uint32, uint32);
 void t2p_write_advance_directory(T2P*, TIFF*);
 tsize_t t2p_sample_planar_separate_to_contig(T2P*, unsigned char*, unsigned char*, tsize_t);
 tsize_t t2p_sample_realize_palette(T2P*, unsigned char*);
@@ -2406,11 +2406,12 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){
 			for(i=0;i<stripcount;i++){
 				striplength=TIFFReadRawStrip(input, i, (tdata_t) stripbuffer, -1);
 				if(!t2p_process_jpeg_strip(
 					stripbuffer, 
 					&striplength, 
-					buffer, 
+					buffer,
+                    t2p->tiff_datasize,
 					&bufferoffset, 
 					i, 
 					t2p->tiff_length)){
 						TIFFError(TIFF2PDF_MODULE, 
 				"Can't process JPEG data in input file %s", 
@@ -3437,10 +3438,11 @@ int t2p_process_ojpeg_tables(T2P* t2p, TIFF* input){
 #ifdef JPEG_SUPPORT
 int t2p_process_jpeg_strip(
 	unsigned char* strip, 
 	tsize_t* striplength, 
 	unsigned char* buffer, 
+    tsize_t buffersize,
 	tsize_t* bufferoffset, 
 	tstrip_t no, 
 	uint32 height){
 
 	tsize_t i=0;
@@ -3471,25 +3473,33 @@ int t2p_process_jpeg_strip(
 			if (datalen < 2 || datalen >= (*striplength - i))
 				return(0);
 		}
 		switch( strip[i] ){
 			case 0xd8:	/* SOI - start of image */
+                if( *bufferoffset + 2 > buffersize )
+                    return(0);
 				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), 2);
 				*bufferoffset+=2;
 				break;
 			case 0xc0:	/* SOF0 */
 			case 0xc1:	/* SOF1 */
 			case 0xc3:	/* SOF3 */
 			case 0xc9:	/* SOF9 */
 			case 0xca:	/* SOF10 */
 				if(no==0){
+                    if( *bufferoffset + datalen + 2 + 6 > buffersize )
+                        return(0);
 					_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2);
+                    if( *bufferoffset + 9 >= buffersize )
+                        return(0);
 					ncomp = buffer[*bufferoffset+9];
 					if (ncomp < 1 || ncomp > 4)
 						return(0);
 					v_samp=1;
 					h_samp=1;
+                    if( *bufferoffset + 11 + 3*(ncomp-1) >= buffersize )
+                        return(0);
 					for(j=0;j<ncomp;j++){
 						uint16 samp = buffer[*bufferoffset+11+(3*j)];
 						if( (samp>>4) > h_samp) 
 							h_samp = (samp>>4);
 						if( (samp & 0x0f) > v_samp) 
@@ -3517,24 +3527,32 @@ int t2p_process_jpeg_strip(
 					buffer[(*bufferoffset)++]= ri & 0xff;
 				}
 				break;
 			case 0xc4: /* DHT */
 			case 0xdb: /* DQT */
+                if( *bufferoffset + datalen + 2 > buffersize )
+                    return(0);
 				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2);
 				*bufferoffset+=datalen+2;
 				break;
 			case 0xda: /* SOS */
 				if(no==0){
+                    if( *bufferoffset + datalen + 2 > buffersize )
+                        return(0);
 					_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2);
 					*bufferoffset+=datalen+2;
 				} else {
+                    if( *bufferoffset + 2 > buffersize )
+                        return(0);
 					buffer[(*bufferoffset)++]=0xff;
 					buffer[(*bufferoffset)++]=
                                             (unsigned char)(0xd0 | ((no-1)%8));
 				}
 				i += datalen + 1;
 				/* copy remainder of strip */
+                if( *bufferoffset + *striplength - i > buffersize )
+                    return(0);
 				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i]), *striplength - i);
 				*bufferoffset+= *striplength - i;
 				return(1);
 			default:
 				/* ignore any other marker */
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index ebc4abaf5..76855665a 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -5756,11 +5756,12 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image,
 static int
 loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned char **read_ptr)
   {
   uint32   i;
   float    xres = 0.0, yres = 0.0;
-  uint16   nstrips = 0, ntiles = 0, planar = 0;
+  uint32   nstrips = 0, ntiles = 0;
+  uint16   planar = 0;
   uint16   bps = 0, spp = 0, res_unit = 0;
   uint16   orientation = 0;
   uint16   input_compression = 0, input_photometric = 0;
   uint16   subsampling_horiz, subsampling_vert;
   uint32   width = 0, length = 0;
@@ -6064,15 +6065,27 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
  
   read_buff = *read_ptr;
   /* +3 : add a few guard bytes since reverseSamples16bits() can read a bit */
   /* outside buffer */
   if (!read_buff)
+  {
+    if( buffsize > 0xFFFFFFFFU - 3 )
+    {
+        TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
+        return (-1);
+    }
     read_buff = (unsigned char *)_TIFFmalloc(buffsize+3);
+  }
   else
     {
     if (prev_readsize < buffsize)
+    {
+      if( buffsize > 0xFFFFFFFFU - 3 )
       {
+          TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
+          return (-1);
+      }
       new_buff = _TIFFrealloc(read_buff, buffsize+3);
       if (!new_buff)
         {
 	free (read_buff);
         read_buff = (unsigned char *)_TIFFmalloc(buffsize+3);
@@ -8910,10 +8923,15 @@ reverseSamplesBytes (uint16 spp, uint16 bps, uint32 width,
     TIFFError("reverseSamplesBytes","Invalid input or output buffer");
     return (1);
     }
 
   bytes_per_pixel  = ((bps * spp) + 7) / 8;
+  if( bytes_per_pixel > sizeof(swapbuff) )
+  {
+    TIFFError("reverseSamplesBytes","bytes_per_pixel too large");
+    return (1);
+  }
   switch (bps / 8)
      {
      case 8:  /* Use memcpy for multiple bytes per sample data */
      case 4:
      case 3:

Vulnerable code: 	    ip += n - 1;	/* point to last one */
	    wp += n - 1;	/* point to last one */
	    n -= stride;
	    while (n > 0) {
		REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
				wp[stride] -= wp[0];
				wp[stride] &= mask;
				wp--; ip--)
		n -= stride;
	    }
	    REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
	    ip += n - 1;	/* point to last one */
	    wp += n - 1;	/* point to last one */
		REPEAT(stride, wp[0] = CLAMP(ip[0]);
				wp[stride] -= wp[0];
				wp[stride] &= mask;
				wp--; ip--)
		n -= stride;
	    }
	    REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
	    wp += n + stride - 1;	/* point to last one */
	    ip += n + stride - 1;	/* point to last one */
	    n -= stride;
	    while (n > 0) {
		REPEAT(stride, wp[0] = CLAMP(ip[0]);
				wp[stride] -= wp[0];
				wp[stride] &= mask;
				wp--; ip--)
		n -= stride;
	    }
	    REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
	}
int t2p_process_jpeg_strip(unsigned char*, tsize_t*, unsigned char*, tsize_t*, tstrip_t, uint32);
					buffer, 
  uint16   nstrips = 0, ntiles = 0, planar = 0;
Fixed code: 2016-09-23 Even Rouault <even.rouault at spatialys.com>

	* tools/tiffcrop.c: fix various out-of-bounds write vulnerabilities
	in heap or stack allocated buffers. Reported as MSVR 35093,
	MSVR 35096 and MSVR 35097. Discovered by Axel Souchet and Vishal
	Chauhan from the MSRC Vulnerabilities & Mitigations team.
	* tools/tiff2pdf.c: fix out-of-bounds write vulnerabilities in
	heap allocate buffer in t2p_process_jpeg_strip(). Reported as MSVR
	35098. Discovered by Axel Souchet and Vishal Chauhan from the MSRC
	Vulnerabilities & Mitigations team.
	* libtiff/tif_pixarlog.c: fix out-of-bounds write vulnerabilities
	in heap allocated buffers. Reported as MSVR 35094. Discovered by
	Axel Souchet and Vishal Chauhan from the MSRC Vulnerabilities &
	Mitigations team.
	* libtiff/tif_write.c: fix issue in error code path of TIFFFlushData1()
	that didn't reset the tif_rawcc and tif_rawcp members. I'm not
	completely sure if that could happen in practice outside of the odd
	behaviour of t2p_seekproc() of tiff2pdf). The report points that a
	better fix could be to check the return value of TIFFFlushData1() in
	places where it isn't done currently, but it seems this patch is enough. 
	Reported as MSVR 35095. Discovered by Axel Souchet & Vishal Chauhan &
	Suha Can from the MSRC Vulnerabilities & Mitigations team.

        REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp++; ip++)
        n -= stride;
        while (n > 0) {
            REPEAT(stride,
                wp[0] = (uint16)(((int32)CLAMP(ip[0])-(int32)CLAMP(ip[-stride])) & mask);
                wp++; ip++)
            n -= stride;
        }
        REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
            REPEAT(stride,
                wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
                wp++; ip++)
            n -= stride;
        }
        REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
        n -= stride;
        while (n > 0) {
            REPEAT(stride,
                wp[0] = (uint16)((CLAMP(ip[0])-CLAMP(ip[-stride])) & mask);
                wp++; ip++)
            n -= stride;
        }
    }
        {
            /* We update those variables even in case of error since there's */
            /* code that doesn't really check the return code of this */
            /* function */
            tif->tif_rawcc = 0;
            tif->tif_rawcp = tif->tif_rawdata;
        }
int t2p_process_jpeg_strip(unsigned char*, tsize_t*, unsigned char*, tsize_t, tsize_t*, tstrip_t, uint32);
					buffer,
                    t2p->tiff_datasize,
    tsize_t buffersize,
                if( *bufferoffset + 2 > buffersize )
                    return(0);
                    if( *bufferoffset + datalen + 2 + 6 > buffersize )
                        return(0);
                    if( *bufferoffset + 9 >= buffersize )
                        return(0);
                    if( *bufferoffset + 11 + 3*(ncomp-1) >= buffersize )
                        return(0);
                if( *bufferoffset + datalen + 2 > buffersize )
                    return(0);
                    if( *bufferoffset + datalen + 2 > buffersize )
                        return(0);
                    if( *bufferoffset + 2 > buffersize )
                        return(0);
                if( *bufferoffset + *striplength - i > buffersize )
                    return(0);
  uint32   nstrips = 0, ntiles = 0;
  uint16   planar = 0;
  {
    if( buffsize > 0xFFFFFFFFU - 3 )
    {
        TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
        return (-1);
    }
  }
    {
      if( buffsize > 0xFFFFFFFFU - 3 )
          TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
          return (-1);
      }
  if( bytes_per_pixel > sizeof(swapbuff) )
  {
    TIFFError("reverseSamplesBytes","bytes_per_pixel too large");
    return (1);
  }