Blame SOURCES/0015-CVE-2019-14973-Fix-integer-overflow-in-_TIFFCheckMal.patch

ccba1e
From 00aeede6bdba3cb74943932b24accc7ba61d2cb0 Mon Sep 17 00:00:00 2001
243c74
From: Even Rouault <even.rouault@spatialys.com>
243c74
Date: Sat, 10 Aug 2019 18:25:03 +0200
ccba1e
Subject: [PATCH] (CVE-2019-14973) Fix integer overflow in _TIFFCheckMalloc()
ccba1e
 and other implementation-defined behaviour (CVE-2019-14973)
243c74
243c74
_TIFFCheckMalloc()/_TIFFCheckRealloc() used a unsafe way to detect overflow
243c74
in the multiplication of nmemb and elem_size (which are of type tmsize_t, thus
243c74
signed), which was especially easily triggered on 32-bit builds (with recent
243c74
enough compilers that assume that signed multiplication cannot overflow, since
243c74
this is undefined behaviour by the C standard). The original issue which lead to
243c74
this fix was trigged from tif_fax3.c
243c74
243c74
There were also unsafe (implementation defied), and broken in practice on 64bit
243c74
builds, ways of checking that a uint64 fits of a (signed) tmsize_t by doing
243c74
(uint64)(tmsize_t)uint64_var != uint64_var comparisons. Those have no known
243c74
at that time exploits, but are better to fix in a more bullet-proof way.
243c74
Or similarly use of (int64)uint64_var <= 0.
ccba1e
ccba1e
(cherry picked from commit 1b5e3b6a23827c33acf19ad50ce5ce78f12b3773)
243c74
---
243c74
 libtiff/tif_aux.c      | 49 +++++++++++++++++++++++++++++++++++++-----
243c74
 libtiff/tif_getimage.c |  6 ++----
243c74
 libtiff/tif_luv.c      |  8 +------
243c74
 libtiff/tif_pixarlog.c |  7 +-----
243c74
 libtiff/tif_read.c     | 38 +++++++++-----------------------
243c74
 libtiff/tif_strip.c    | 35 ++++--------------------------
243c74
 libtiff/tif_tile.c     | 27 +++--------------------
243c74
 libtiff/tiffiop.h      |  7 +++++-
243c74
 8 files changed, 71 insertions(+), 106 deletions(-)
243c74
243c74
diff --git a/libtiff/tif_aux.c b/libtiff/tif_aux.c
ccba1e
index 10b8d00c..38a98b67 100644
243c74
--- a/libtiff/tif_aux.c
243c74
+++ b/libtiff/tif_aux.c
243c74
@@ -59,18 +59,57 @@ _TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
243c74
 	return bytes;
243c74
 }
243c74
 
243c74
+tmsize_t
243c74
+_TIFFMultiplySSize(TIFF* tif, tmsize_t first, tmsize_t second, const char* where)
243c74
+{
243c74
+    if( first <= 0 || second <= 0 )
243c74
+    {
243c74
+        if( tif != NULL && where != NULL )
243c74
+        {
243c74
+            TIFFErrorExt(tif->tif_clientdata, where,
243c74
+                        "Invalid argument to _TIFFMultiplySSize() in %s", where);
243c74
+        }
243c74
+        return 0;
243c74
+    }
243c74
+
243c74
+    if( first > TIFF_TMSIZE_T_MAX / second )
243c74
+    {
243c74
+        if( tif != NULL && where != NULL )
243c74
+        {
243c74
+            TIFFErrorExt(tif->tif_clientdata, where,
243c74
+                        "Integer overflow in %s", where);
243c74
+        }
243c74
+        return 0;
243c74
+    }
243c74
+    return first * second;
243c74
+}
243c74
+
243c74
+tmsize_t _TIFFCastUInt64ToSSize(TIFF* tif, uint64 val, const char* module)
243c74
+{
243c74
+    if( val > (uint64)TIFF_TMSIZE_T_MAX )
243c74
+    {
243c74
+        if( tif != NULL && module != NULL )
243c74
+        {
243c74
+            TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
+        }
243c74
+        return 0;
243c74
+    }
243c74
+    return (tmsize_t)val;
243c74
+}
243c74
+
243c74
 void*
243c74
 _TIFFCheckRealloc(TIFF* tif, void* buffer,
243c74
 		  tmsize_t nmemb, tmsize_t elem_size, const char* what)
243c74
 {
243c74
 	void* cp = NULL;
243c74
-	tmsize_t bytes = nmemb * elem_size;
243c74
-
243c74
+        tmsize_t count = _TIFFMultiplySSize(tif, nmemb, elem_size, NULL);
243c74
 	/*
243c74
-	 * XXX: Check for integer overflow.
243c74
+	 * Check for integer overflow.
243c74
 	 */
243c74
-	if (nmemb && elem_size && bytes / elem_size == nmemb)
243c74
-		cp = _TIFFrealloc(buffer, bytes);
243c74
+	if (count != 0)
243c74
+	{
243c74
+		cp = _TIFFrealloc(buffer, count);
243c74
+	}
243c74
 
243c74
 	if (cp == NULL) {
243c74
 		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
243c74
diff --git a/libtiff/tif_getimage.c b/libtiff/tif_getimage.c
ccba1e
index fc554cca..ec09feaf 100644
243c74
--- a/libtiff/tif_getimage.c
243c74
+++ b/libtiff/tif_getimage.c
243c74
@@ -757,9 +757,8 @@ gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
243c74
 	uint32 leftmost_tw;
243c74
 
243c74
 	tilesize = TIFFTileSize(tif);  
243c74
-	bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
243c74
+	bufsize = _TIFFMultiplySSize(tif, alpha?4:3,tilesize, "gtTileSeparate");
243c74
 	if (bufsize == 0) {
243c74
-		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
243c74
 		return (0);
243c74
 	}
243c74
 
243c74
@@ -1021,9 +1020,8 @@ gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
243c74
         uint16 colorchannels;
243c74
 
243c74
 	stripsize = TIFFStripSize(tif);  
243c74
-	bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
243c74
+	bufsize = _TIFFMultiplySSize(tif,alpha?4:3,stripsize, "gtStripSeparate");
243c74
 	if (bufsize == 0) {
243c74
-		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
243c74
 		return (0);
243c74
 	}
243c74
 
243c74
diff --git a/libtiff/tif_luv.c b/libtiff/tif_luv.c
ccba1e
index 4b25244b..c4cb73a3 100644
243c74
--- a/libtiff/tif_luv.c
243c74
+++ b/libtiff/tif_luv.c
243c74
@@ -1264,16 +1264,10 @@ LogL16GuessDataFmt(TIFFDirectory *td)
243c74
 	return (SGILOGDATAFMT_UNKNOWN);
243c74
 }
243c74
 
243c74
-
243c74
-#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
243c74
-#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
243c74
-
243c74
 static tmsize_t
243c74
 multiply_ms(tmsize_t m1, tmsize_t m2)
243c74
 {
243c74
-        if( m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1 )
243c74
-            return 0;
243c74
-        return m1 * m2;
243c74
+        return _TIFFMultiplySSize(NULL, m1, m2, NULL);
243c74
 }
243c74
 
243c74
 static int
243c74
diff --git a/libtiff/tif_pixarlog.c b/libtiff/tif_pixarlog.c
ccba1e
index 979858da..8e9eaa1d 100644
243c74
--- a/libtiff/tif_pixarlog.c
243c74
+++ b/libtiff/tif_pixarlog.c
243c74
@@ -636,15 +636,10 @@ PixarLogGuessDataFmt(TIFFDirectory *td)
243c74
 	return guess;
243c74
 }
243c74
 
243c74
-#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
243c74
-#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
243c74
-
243c74
 static tmsize_t
243c74
 multiply_ms(tmsize_t m1, tmsize_t m2)
243c74
 {
243c74
-        if( m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1 )
243c74
-            return 0;
243c74
-        return m1 * m2;
243c74
+        return _TIFFMultiplySSize(NULL, m1, m2, NULL);
243c74
 }
243c74
 
243c74
 static tmsize_t
243c74
diff --git a/libtiff/tif_read.c b/libtiff/tif_read.c
ccba1e
index 04100f4d..9a0e6e95 100644
243c74
--- a/libtiff/tif_read.c
243c74
+++ b/libtiff/tif_read.c
243c74
@@ -31,9 +31,6 @@
243c74
 #include "tiffiop.h"
243c74
 #include <stdio.h>
243c74
 
243c74
-#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
243c74
-#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
243c74
-
243c74
 int TIFFFillStrip(TIFF* tif, uint32 strip);
243c74
 int TIFFFillTile(TIFF* tif, uint32 tile);
243c74
 static int TIFFStartStrip(TIFF* tif, uint32 strip);
243c74
@@ -51,6 +48,8 @@ TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* m
243c74
 #define THRESHOLD_MULTIPLIER 10
243c74
 #define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
243c74
 
243c74
+#define TIFF_INT64_MAX ((((int64)0x7FFFFFFF) << 32) | 0xFFFFFFFF)
243c74
+
243c74
 /* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset'
243c74
  * Returns 1 in case of success, 0 otherwise. */
243c74
 static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
243c74
@@ -735,23 +734,8 @@ TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
243c74
 		return ((tmsize_t)(-1));
243c74
 	}
243c74
 	bytecount = td->td_stripbytecount[strip];
243c74
-	if ((int64)bytecount <= 0) {
243c74
-#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
243c74
-		TIFFErrorExt(tif->tif_clientdata, module,
243c74
-			     "%I64u: Invalid strip byte count, strip %lu",
243c74
-			     (unsigned __int64) bytecount,
243c74
-			     (unsigned long) strip);
243c74
-#else
243c74
-		TIFFErrorExt(tif->tif_clientdata, module,
243c74
-			     "%llu: Invalid strip byte count, strip %lu",
243c74
-			     (unsigned long long) bytecount,
243c74
-			     (unsigned long) strip);
243c74
-#endif
243c74
-		return ((tmsize_t)(-1));
243c74
-	}
243c74
-	bytecountm = (tmsize_t)bytecount;
243c74
-	if ((uint64)bytecountm!=bytecount) {
243c74
-		TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
243c74
+        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount, module);
243c74
+	if (bytecountm == 0) {
243c74
 		return ((tmsize_t)(-1));
243c74
 	}
243c74
 	if (size != (tmsize_t)(-1) && size < bytecountm)
243c74
@@ -775,7 +759,7 @@ TIFFFillStrip(TIFF* tif, uint32 strip)
243c74
 	if ((tif->tif_flags&TIFF_NOREADRAW)==0)
243c74
 	{
243c74
 		uint64 bytecount = td->td_stripbytecount[strip];
243c74
-		if ((int64)bytecount <= 0) {
243c74
+		if( bytecount == 0 || bytecount > (uint64)TIFF_INT64_MAX ) {
243c74
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
243c74
 			TIFFErrorExt(tif->tif_clientdata, module,
243c74
 				"Invalid strip byte count %I64u, strip %lu",
243c74
@@ -802,7 +786,7 @@ TIFFFillStrip(TIFF* tif, uint32 strip)
243c74
 			    (bytecount - 4096) / 10 > (uint64)stripsize  )
243c74
 			{
243c74
 				uint64 newbytecount = (uint64)stripsize * 10 + 4096;
243c74
-				if( (int64)newbytecount >= 0 )
243c74
+				if( newbytecount == 0 || newbytecount > (uint64)TIFF_INT64_MAX )
243c74
 				{
243c74
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
243c74
 					TIFFWarningExt(tif->tif_clientdata, module,
243c74
@@ -1197,10 +1181,8 @@ TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
243c74
 	bytecount64 = td->td_stripbytecount[tile];
243c74
 	if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
243c74
 		bytecount64 = (uint64)size;
243c74
-	bytecountm = (tmsize_t)bytecount64;
243c74
-	if ((uint64)bytecountm!=bytecount64)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
+	bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
243c74
+        if( bytecountm == 0 ) {
243c74
 		return ((tmsize_t)(-1));
243c74
 	}
243c74
 	return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
243c74
@@ -1222,7 +1204,7 @@ TIFFFillTile(TIFF* tif, uint32 tile)
243c74
 	if ((tif->tif_flags&TIFF_NOREADRAW)==0)
243c74
 	{
243c74
 		uint64 bytecount = td->td_stripbytecount[tile];
243c74
-		if ((int64)bytecount <= 0) {
243c74
+		if( bytecount == 0 || bytecount > (uint64)TIFF_INT64_MAX ) {
243c74
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
243c74
 			TIFFErrorExt(tif->tif_clientdata, module,
243c74
 				"%I64u: Invalid tile byte count, tile %lu",
243c74
@@ -1249,7 +1231,7 @@ TIFFFillTile(TIFF* tif, uint32 tile)
243c74
 			    (bytecount - 4096) / 10 > (uint64)stripsize  )
243c74
 			{
243c74
 				uint64 newbytecount = (uint64)stripsize * 10 + 4096;
243c74
-				if( (int64)newbytecount >= 0 )
243c74
+				if( newbytecount == 0 || newbytecount > (uint64)TIFF_INT64_MAX )
243c74
 				{
243c74
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
243c74
 					TIFFWarningExt(tif->tif_clientdata, module,
243c74
diff --git a/libtiff/tif_strip.c b/libtiff/tif_strip.c
ccba1e
index 6e9f2ef6..321ad6b9 100644
243c74
--- a/libtiff/tif_strip.c
243c74
+++ b/libtiff/tif_strip.c
243c74
@@ -131,15 +131,8 @@ TIFFVStripSize(TIFF* tif, uint32 nrows)
243c74
 {
243c74
 	static const char module[] = "TIFFVStripSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFVStripSize64(tif,nrows);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+        return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /*
243c74
@@ -213,15 +206,8 @@ TIFFStripSize(TIFF* tif)
243c74
 {
243c74
 	static const char module[] = "TIFFStripSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFStripSize64(tif);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+	return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /*
243c74
@@ -332,14 +318,8 @@ TIFFScanlineSize(TIFF* tif)
243c74
 {
243c74
 	static const char module[] = "TIFFScanlineSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFScanlineSize64(tif);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m) {
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+	return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /*
243c74
@@ -368,15 +348,8 @@ TIFFRasterScanlineSize(TIFF* tif)
243c74
 {
243c74
 	static const char module[] = "TIFFRasterScanlineSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFRasterScanlineSize64(tif);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+	return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /* vim: set ts=8 sts=8 sw=8 noet: */
243c74
diff --git a/libtiff/tif_tile.c b/libtiff/tif_tile.c
ccba1e
index 388e168a..7d057509 100644
243c74
--- a/libtiff/tif_tile.c
243c74
+++ b/libtiff/tif_tile.c
243c74
@@ -183,15 +183,8 @@ TIFFTileRowSize(TIFF* tif)
243c74
 {
243c74
 	static const char module[] = "TIFFTileRowSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFTileRowSize64(tif);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+	return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /*
243c74
@@ -250,15 +243,8 @@ TIFFVTileSize(TIFF* tif, uint32 nrows)
243c74
 {
243c74
 	static const char module[] = "TIFFVTileSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFVTileSize64(tif,nrows);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+	return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /*
243c74
@@ -274,15 +260,8 @@ TIFFTileSize(TIFF* tif)
243c74
 {
243c74
 	static const char module[] = "TIFFTileSize";
243c74
 	uint64 m;
243c74
-	tmsize_t n;
243c74
 	m=TIFFTileSize64(tif);
243c74
-	n=(tmsize_t)m;
243c74
-	if ((uint64)n!=m)
243c74
-	{
243c74
-		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
243c74
-		n=0;
243c74
-	}
243c74
-	return(n);
243c74
+	return _TIFFCastUInt64ToSSize(tif, m, module);
243c74
 }
243c74
 
243c74
 /*
243c74
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
ccba1e
index 08e5dc44..d4b86314 100644
243c74
--- a/libtiff/tiffiop.h
243c74
+++ b/libtiff/tiffiop.h
243c74
@@ -79,6 +79,9 @@ extern int snprintf(char* str, size_t size, const char* format, ...);
243c74
 #define	FALSE	0
243c74
 #endif
243c74
 
243c74
+#define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
243c74
+#define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
243c74
+
243c74
 typedef struct client_info {
243c74
     struct client_info *next;
243c74
     void *data;
243c74
@@ -260,7 +263,7 @@ struct tiff {
243c74
 #define TIFFhowmany8_64(x) (((x)&0x07)?((uint64)(x)>>3)+1:(uint64)(x)>>3)
243c74
 #define TIFFroundup_64(x, y) (TIFFhowmany_64(x,y)*(y))
243c74
 
243c74
-/* Safe multiply which returns zero if there is an integer overflow */
243c74
+/* Safe multiply which returns zero if there is an *unsigned* integer overflow. This macro is not safe for *signed* integer types */
243c74
 #define TIFFSafeMultiply(t,v,m) ((((t)(m) != (t)0) && (((t)(((v)*(m))/(m))) == (t)(v))) ? (t)((v)*(m)) : (t)0)
243c74
 
243c74
 #define TIFFmax(A,B) ((A)>(B)?(A):(B))
243c74
@@ -366,6 +369,8 @@ extern TIFFErrorHandlerExt _TIFFerrorHandlerExt;
243c74
 
243c74
 extern uint32 _TIFFMultiply32(TIFF*, uint32, uint32, const char*);
243c74
 extern uint64 _TIFFMultiply64(TIFF*, uint64, uint64, const char*);
243c74
+extern tmsize_t _TIFFMultiplySSize(TIFF*, tmsize_t, tmsize_t, const char*);
243c74
+extern tmsize_t _TIFFCastUInt64ToSSize(TIFF*, uint64, const char*);
243c74
 extern void* _TIFFCheckMalloc(TIFF*, tmsize_t, tmsize_t, const char*);
243c74
 extern void* _TIFFCheckRealloc(TIFF*, void*, tmsize_t, tmsize_t, const char*);
243c74