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