Blame SOURCES/0011-CVE-2018-18557-JBIG-fix-potential-out-of-bounds-writ.patch

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