CVE-2016-5314: Out-of-bounds Write (CWE-787)
value: Buffer overflow in the PixarLogDecode function in tif_pixarlog.c in LibTIFF 4.0.6 and earlier allows remote attackers to cause a denial of service (application crash) or possibly have unspecified other impact via a crafted TIFF image, as demonstrated by overwriting the vgetparent function pointer with rgb2ycbcr.
CWE description: The product writes data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 8.8
Published: 2018-03-12T02:29Z
Repository: https://github.com/vadz/libtiff
Language: Other
Files changed: ChangeLog, libtiff/tif_pixarlog.c
Commit date: 2016-06-28 15:12:19 +0000
Commit message: * libtiff/tif_pixarlog.c: fix potential buffer write overrun in
PixarLogDecode() on corrupted/unexpected images (reported by Mathias Svensson)

Diff: diff --git a/ChangeLog b/ChangeLog
index 1b0e5996c..dee18813f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
+2016-06-28  Even Rouault <even.rouault at spatialys.com>
+
+	* libtiff/tif_pixarlog.c: fix potential buffer write overrun in
+	PixarLogDecode() on corrupted/unexpected images (reported by Mathias Svensson)
+
 2016-06-15  Bob Friesenhahn  <bfriesen@simple.dallas.tx.us>
 
 	* libtiff/libtiff.def: Added _TIFFMultiply32 and _TIFFMultiply64
 	to libtiff.def
 
diff --git a/libtiff/tif_pixarlog.c b/libtiff/tif_pixarlog.c
index 06e8af390..cc486c547 100644
--- a/libtiff/tif_pixarlog.c
+++ b/libtiff/tif_pixarlog.c
@@ -457,10 +457,11 @@ horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
  * file using PixarLog compression/decompression.
  */
 typedef	struct {
 	TIFFPredictorState	predict;
 	z_stream		stream;
+	tmsize_t		tbuf_size; /* only set/used on reading for now */
 	uint16			*tbuf; 
 	uint16			stride;
 	int			state;
 	int			user_datafmt;
 	int			quality;
@@ -692,10 +693,11 @@ PixarLogSetupDecode(TIFF* tif)
 	if (tbuf_size == 0)
 		return (0);   /* TODO: this is an error return without error report through TIFFErrorExt */
 	sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
 	if (sp->tbuf == NULL)
 		return (0);
+	sp->tbuf_size = tbuf_size;
 	if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
 		sp->user_datafmt = PixarLogGuessDataFmt(td);
 	if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
 		TIFFErrorExt(tif->tif_clientdata, module,
 			"PixarLog compression can't handle bits depth/data format combination (depth: %d)", 
@@ -781,10 +783,16 @@ PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
 	if (sp->stream.avail_out != nsamples * sizeof(uint16))
 	{
 		TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
 		return (0);
 	}
+	/* Check that we will not fill more than what was allocated */
+	if (sp->stream.avail_out > sp->tbuf_size)
+	{
+		TIFFErrorExt(tif->tif_clientdata, module, "sp->stream.avail_out > sp->tbuf_size");
+		return (0);
+	}
 	do {
 		int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
 		if (state == Z_STREAM_END) {
 			break;			/* XXX */
 		}

Fixed code: 2016-06-28  Even Rouault <even.rouault at spatialys.com>

	* libtiff/tif_pixarlog.c: fix potential buffer write overrun in
	PixarLogDecode() on corrupted/unexpected images (reported by Mathias Svensson)

	tmsize_t		tbuf_size; /* only set/used on reading for now */
	sp->tbuf_size = tbuf_size;
	/* Check that we will not fill more than what was allocated */
	if (sp->stream.avail_out > sp->tbuf_size)
	{
		TIFFErrorExt(tif->tif_clientdata, module, "sp->stream.avail_out > sp->tbuf_size");
		return (0);
	}