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

21ff78
From 2683f6c21aefc760d2f7e56dac6b4383841886d6 Mon Sep 17 00:00:00 2001
21ff78
From: Even Rouault <even.rouault@spatialys.com>
21ff78
Date: Sun, 14 Oct 2018 16:38:29 +0200
21ff78
Subject: [PATCH 2/2] JBIG: fix potential out-of-bounds write in JBIGDecode()
21ff78
21ff78
JBIGDecode doesn't check if the user provided buffer is large enough
21ff78
to store the JBIG decoded image, which can potentially cause out-of-bounds
21ff78
write in the buffer.
21ff78
This issue was reported and analyzed by Thomas Dullien.
21ff78
21ff78
Also fixes a (harmless) potential use of uninitialized memory when
21ff78
tif->tif_rawsize > tif->tif_rawcc
21ff78
21ff78
And in case libtiff is compiled with CHUNKY_STRIP_READ_SUPPORT, make sure
21ff78
that whole strip data is provided to JBIGDecode()
21ff78
---
21ff78
 libtiff/tif_jbig.c | 32 ++++++++++++++++++++++++++------
21ff78
 libtiff/tif_read.c |  6 ++++++
21ff78
 2 files changed, 32 insertions(+), 6 deletions(-)
21ff78
21ff78
diff --git a/libtiff/tif_jbig.c b/libtiff/tif_jbig.c
21ff78
index 7a14dd9..8136c77 100644
21ff78
--- a/libtiff/tif_jbig.c
21ff78
+++ b/libtiff/tif_jbig.c
21ff78
@@ -53,17 +53,18 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
21ff78
 	struct jbg_dec_state decoder;
21ff78
 	int decodeStatus = 0;
21ff78
 	unsigned char* pImage = NULL;
21ff78
-	(void) size, (void) s;
21ff78
+	unsigned long decodedSize;
21ff78
+	(void) s;
21ff78
 
21ff78
 	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
21ff78
 	{
21ff78
-		TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
21ff78
+		TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
21ff78
 	}
21ff78
 
21ff78
 	jbg_dec_init(&decoder);
21ff78
 
21ff78
 #if defined(HAVE_JBG_NEWLEN)
21ff78
-	jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
21ff78
+	jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
21ff78
 	/*
21ff78
 	 * I do not check the return status of jbg_newlen because even if this
21ff78
 	 * function fails it does not necessarily mean that decoding the image
21ff78
@@ -76,8 +77,8 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
21ff78
 	 */
21ff78
 #endif /* HAVE_JBG_NEWLEN */
21ff78
 
21ff78
-	decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
21ff78
-				  (size_t)tif->tif_rawdatasize, NULL);
21ff78
+	decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawcp,
21ff78
+				  (size_t)tif->tif_rawcc, NULL);
21ff78
 	if (JBG_EOK != decodeStatus)
21ff78
 	{
21ff78
 		/*
21ff78
@@ -98,9 +99,28 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
21ff78
 		return 0;
21ff78
 	}
21ff78
 
21ff78
+	decodedSize = jbg_dec_getsize(&decoder);
21ff78
+	if( (tmsize_t)decodedSize < size )
21ff78
+	{
21ff78
+	    TIFFWarningExt(tif->tif_clientdata, "JBIG",
21ff78
+	                   "Only decoded %lu bytes, whereas %lu requested",
21ff78
+	                   decodedSize, (unsigned long)size);
21ff78
+	}
21ff78
+	else if( (tmsize_t)decodedSize > size )
21ff78
+	{
21ff78
+	    TIFFErrorExt(tif->tif_clientdata, "JBIG",
21ff78
+	                 "Decoded %lu bytes, whereas %lu were requested",
21ff78
+	                 decodedSize, (unsigned long)size);
21ff78
+	    jbg_dec_free(&decoder);
21ff78
+	    return 0;
21ff78
+	}
21ff78
 	pImage = jbg_dec_getimage(&decoder, 0);
21ff78
-	_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
21ff78
+	_TIFFmemcpy(buffer, pImage, decodedSize);
21ff78
 	jbg_dec_free(&decoder);
21ff78
+
21ff78
+        tif->tif_rawcp += tif->tif_rawcc;
21ff78
+        tif->tif_rawcc = 0;
21ff78
+
21ff78
 	return 1;
21ff78
 }
21ff78
 
21ff78
diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
21ff78
index 2ba985a..04100f4 100644
21ff78
--- a/libtiff/tif_read.c
21ff78
+++ b/libtiff/tif_read.c
21ff78
@@ -348,6 +348,12 @@ TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
21ff78
             return 0;
21ff78
         whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
21ff78
                 || isMapped(tif);
21ff78
+        if( td->td_compression == COMPRESSION_JBIG )
21ff78
+        {
21ff78
+            /* Ideally plugins should have a way to declare they don't support
21ff78
+             * chunk strip */
21ff78
+            whole_strip = 1;
21ff78
+        }
21ff78
 #else
21ff78
         whole_strip = 1;
21ff78
 #endif
21ff78
-- 
21ff78
2.17.2
21ff78