CVE-2016-9536 (C, published 2016-11-22T19:59Z)
Description: value: tools/tiff2pdf.c in libtiff 4.0.6 has out-of-bounds write vulnerabilities in heap allocated buffers in t2p_process_jpeg_strip(). Reported as MSVR 35098, aka "t2p_process_jpeg_strip heap-buffer-overflow."
Severity: HIGH (CVSS v3 base score: 9.8)
Weakness: CWE-119 \u2014 Improper Restriction of Operations within the Bounds of a Memory Buffer
The product performs operations on a memory buffer, but it reads from or writes to a memory location outside the buffer's intended boundary. This may result in read or write operations on unexpected memory locations that could be linked to other variables, data structures, or internal program data.
Repository: https://github.com/vadz/libtiff
Files changed:
ChangeLog
libtiff/tif_pixarlog.c
libtiff/tif_write.c
tools/tiff2pdf.c
tools/tiffcrop.c
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.


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);
  }