Blame SOURCES/CVE-2017-18234.patch

a3db16
From 70567edd9fc8753cc176de02b1d96f504a14e82c Mon Sep 17 00:00:00 2001
a3db16
From: =?UTF-8?q?Hubert=20Figui=C3=A8re?= <hub@figuiere.net>
a3db16
Date: Sun, 26 Mar 2017 01:10:11 -0400
a3db16
Subject: [PATCH 2/5] Bug 100397 - Fix crash on malformed JPEG file
a3db16
a3db16
---
a3db16
 source/XMPFiles/FormatSupport/TIFF_MemoryReader.cpp | 10 +++++++---
a3db16
 source/XMPFiles/FormatSupport/TIFF_Support.hpp      | 13 ++++++++++++-
a3db16
 source/common/EndianUtils.hpp                       |  9 +++++++++
a3db16
 3 files changed, 28 insertions(+), 4 deletions(-)
a3db16
a3db16
diff --git a/source/XMPFiles/FormatSupport/TIFF_MemoryReader.cpp b/source/XMPFiles/FormatSupport/TIFF_MemoryReader.cpp
a3db16
index 316cea0..1446cb4 100644
a3db16
--- a/source/XMPFiles/FormatSupport/TIFF_MemoryReader.cpp
a3db16
+++ b/source/XMPFiles/FormatSupport/TIFF_MemoryReader.cpp
a3db16
@@ -65,7 +65,7 @@ void TIFF_MemoryReader::SortIFD ( TweakedIFDInfo* thisIFD )
a3db16
 		} else if ( thisTag == prevTag ) {
a3db16
 
a3db16
 			// Duplicate tag, keep the 2nd copy, move the tail of the array up, prevTag is unchanged.
a3db16
-			memcpy ( &ifdEntries[i-1], &ifdEntries[i], 12*(tagCount-i) );	// AUDIT: Safe, moving tail forward, i >= 1.
a3db16
+			memmove ( &ifdEntries[i-1], &ifdEntries[i], 12*(tagCount-i) ); // may overlap -- Hub
a3db16
 			--tagCount;
a3db16
 			--i; // ! Don't move forward in the array, we've moved the unseen part up.
a3db16
 
a3db16
@@ -81,7 +81,7 @@ void TIFF_MemoryReader::SortIFD ( TweakedIFDInfo* thisIFD )
a3db16
 
a3db16
 				// Out of order duplicate, move it to position j, move the tail of the array up.
a3db16
 				ifdEntries[j] = ifdEntries[i];
a3db16
-				memcpy ( &ifdEntries[i], &ifdEntries[i+1], 12*(tagCount-(i+1)) );	// AUDIT: Safe, moving tail forward, i >= 1.
a3db16
+				memmove ( &ifdEntries[i], &ifdEntries[i+1], 12*(tagCount-(i+1)) );	// may overlap -- Hub
a3db16
 				--tagCount;
a3db16
 				--i; // ! Don't move forward in the array, we've moved the unseen part up.
a3db16
 
a3db16
@@ -212,7 +212,11 @@ bool TIFF_MemoryReader::GetTag ( XMP_Uns8 ifd, XMP_Uns16 id, TagInfo* info ) con
a3db16
 		info->dataLen = thisTag->bytes;
a3db16
 
a3db16
 		info->dataPtr = this->GetDataPtr ( thisTag );
a3db16
-
a3db16
+		// Here we know that if it is NULL, it is wrong. -- Hub
a3db16
+		// GetDataPtr will return NULL in case of overflow.
a3db16
+		if (info->dataPtr == NULL) {
a3db16
+			return false;
a3db16
+		}
a3db16
 	}
a3db16
 
a3db16
 	return true;
a3db16
diff --git a/source/XMPFiles/FormatSupport/TIFF_Support.hpp b/source/XMPFiles/FormatSupport/TIFF_Support.hpp
a3db16
index 9af76c4..95badba 100644
a3db16
--- a/source/XMPFiles/FormatSupport/TIFF_Support.hpp
a3db16
+++ b/source/XMPFiles/FormatSupport/TIFF_Support.hpp
a3db16
@@ -723,7 +723,18 @@ private:
a3db16
 	const TweakedIFDEntry* FindTagInIFD ( XMP_Uns8 ifd, XMP_Uns16 id ) const;
a3db16
 
a3db16
 	const inline void* GetDataPtr ( const TweakedIFDEntry* tifdEntry ) const
a3db16
-		{ if ( tifdEntry->bytes <= 4 ) return &tifdEntry->dataOrPos; else return (this->tiffStream + tifdEntry->dataOrPos); };
a3db16
+		{ if ( GetUns32AsIs(&tifdEntry->bytes) <= 4 ) {
a3db16
+			return &tifdEntry->dataOrPos;
a3db16
+		  } else {
a3db16
+			XMP_Uns32 pos = GetUns32AsIs(&tifdEntry->dataOrPos);
a3db16
+			if (pos + GetUns32AsIs(&tifdEntry->bytes) > this->tiffLength) {
a3db16
+				// Invalid file.
a3db16
+				// The data is past the length of the TIFF.
a3db16
+				return NULL;
a3db16
+			}
a3db16
+			return (this->tiffStream + pos);
a3db16
+		  }
a3db16
+		}
a3db16
 
a3db16
 	static inline void NotAppropriate() { XMP_Throw ( "Not appropriate for TIFF_Reader", kXMPErr_InternalFailure ); };
a3db16
 
a3db16
diff --git a/source/common/EndianUtils.hpp b/source/common/EndianUtils.hpp
a3db16
index 59e2e32..0e2e2fe 100644
a3db16
--- a/source/common/EndianUtils.hpp
a3db16
+++ b/source/common/EndianUtils.hpp
a3db16
@@ -148,6 +148,15 @@ GetUns32LE ( const void * addr )
a3db16
 
a3db16
 // -------------------------------------------------------------------------------------------------
a3db16
 
a3db16
+static inline XMP_Uns32
a3db16
+GetUns32AsIs ( const void * addr )
a3db16
+{
a3db16
+	XMP_Uns32 value = *((XMP_Uns32*)addr);
a3db16
+	return value;   // Use this to avoid SPARC failure to handle unaligned loads and stores.
a3db16
+}
a3db16
+
a3db16
+// -------------------------------------------------------------------------------------------------
a3db16
+
a3db16
 static inline XMP_Uns64
a3db16
 GetUns64BE ( const void * addr )
a3db16
 {
a3db16
-- 
a3db16
2.17.2
a3db16