Blame SOURCES/exiv2-CVE-2018-10958.patch

4a042c
diff --git a/include/exiv2/error.hpp b/include/exiv2/error.hpp
4a042c
index 24a70bf6..cc67725b 100644
4a042c
--- a/include/exiv2/error.hpp
4a042c
+++ b/include/exiv2/error.hpp
4a042c
@@ -192,6 +192,74 @@ namespace Exiv2 {
4a042c
         return os << error.what();
4a042c
     }
4a042c
4a042c
+    //! Complete list of all Exiv2 error codes
4a042c
+    enum ErrorCode {
4a042c
+        kerGeneralError = -1,
4a042c
+        kerSuccess = 0,
4a042c
+        kerErrorMessage,
4a042c
+        kerCallFailed,
4a042c
+        kerNotAnImage,
4a042c
+        kerInvalidDataset,
4a042c
+        kerInvalidRecord,
4a042c
+        kerInvalidKey,
4a042c
+        kerInvalidTag,
4a042c
+        kerValueNotSet,
4a042c
+        kerDataSourceOpenFailed,
4a042c
+        kerFileOpenFailed,
4a042c
+        kerFileContainsUnknownImageType,
4a042c
+        kerMemoryContainsUnknownImageType,
4a042c
+        kerUnsupportedImageType,
4a042c
+        kerFailedToReadImageData,
4a042c
+        kerNotAJpeg,
4a042c
+        kerFailedToMapFileForReadWrite,
4a042c
+        kerFileRenameFailed,
4a042c
+        kerTransferFailed,
4a042c
+        kerMemoryTransferFailed,
4a042c
+        kerInputDataReadFailed,
4a042c
+        kerImageWriteFailed,
4a042c
+        kerNoImageInInputData,
4a042c
+        kerInvalidIfdId,
4a042c
+        //! Entry::setValue: Value too large
4a042c
+        kerValueTooLarge,
4a042c
+        //! Entry::setDataArea: Value too large
4a042c
+        kerDataAreaValueTooLarge,
4a042c
+        kerOffsetOutOfRange,
4a042c
+        kerUnsupportedDataAreaOffsetType,
4a042c
+        kerInvalidCharset,
4a042c
+        kerUnsupportedDateFormat,
4a042c
+        kerUnsupportedTimeFormat,
4a042c
+        kerWritingImageFormatUnsupported,
4a042c
+        kerInvalidSettingForImage,
4a042c
+        kerNotACrwImage,
4a042c
+        kerFunctionNotSupported,
4a042c
+        kerNoNamespaceInfoForXmpPrefix,
4a042c
+        kerNoPrefixForNamespace,
4a042c
+        kerTooLargeJpegSegment,
4a042c
+        kerUnhandledXmpdatum,
4a042c
+        kerUnhandledXmpNode,
4a042c
+        kerXMPToolkitError,
4a042c
+        kerDecodeLangAltPropertyFailed,
4a042c
+        kerDecodeLangAltQualifierFailed,
4a042c
+        kerEncodeLangAltPropertyFailed,
4a042c
+        kerPropertyNameIdentificationFailed,
4a042c
+        kerSchemaNamespaceNotRegistered,
4a042c
+        kerNoNamespaceForPrefix,
4a042c
+        kerAliasesNotSupported,
4a042c
+        kerInvalidXmpText,
4a042c
+        kerTooManyTiffDirectoryEntries,
4a042c
+        kerMultipleTiffArrayElementTagsInDirectory,
4a042c
+        kerWrongTiffArrayElementTagType,
4a042c
+        kerInvalidKeyXmpValue,
4a042c
+        kerInvalidIccProfile,
4a042c
+        kerInvalidXMP,
4a042c
+        kerTiffDirectoryTooLarge,
4a042c
+        kerInvalidTypeValue,
4a042c
+        kerInvalidMalloc,
4a042c
+        kerCorruptedMetadata,
4a042c
+        kerArithmeticOverflow,
4a042c
+        kerMallocFailed,
4a042c
+    };
4a042c
+
4a042c
     /*!
4a042c
       @brief Simple error class used for exceptions. An output operator is
4a042c
              provided to print errors to a stream.
4a042c
4a042c
diff --git a/src/enforce.hpp b/src/enforce.hpp
4a042c
new file mode 100644
4a042c
index 00000000..b2d77eea
4a042c
--- /dev/null
4a042c
+++ b/src/enforce.hpp
4a042c
@@ -0,0 +1,96 @@
4a042c
+// ********************************************************* -*- C++ -*-
4a042c
+/*
4a042c
+ * Copyright (C) 2004-2018 Exiv2 maintainers
4a042c
+ *
4a042c
+ * This program is part of the Exiv2 distribution.
4a042c
+ *
4a042c
+ * This program is free software; you can redistribute it and/or
4a042c
+ * modify it under the terms of the GNU General Public License
4a042c
+ * as published by the Free Software Foundation; either version 2
4a042c
+ * of the License, or (at your option) any later version.
4a042c
+ *
4a042c
+ * This program is distributed in the hope that it will be useful,
4a042c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4a042c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4a042c
+ * GNU General Public License for more details.
4a042c
+ *
4a042c
+ * You should have received a copy of the GNU General Public License
4a042c
+ * along with this program; if not, write to the Free Software
4a042c
+ * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
4a042c
+ */
4a042c
+/*!
4a042c
+  @file    enforce.hpp
4a042c
+  @brief   Port of D's enforce() to C++ & Exiv2
4a042c
+  @author  Dan Čermák (D4N)
4a042c
+           dan.cermak@cgc-instruments.com
4a042c
+  @date    11-March-18, D4N: created
4a042c
+ */
4a042c
+
4a042c
+#include <string>
4a042c
+
4a042c
+#include "error.hpp"
4a042c
+
4a042c
+/*!
4a042c
+ * @brief Ensure that condition is true, otherwise throw an exception of the
4a042c
+ * type exception_t
4a042c
+ *
4a042c
+ * @tparam exception_t  Exception type that is thrown, must provide a
4a042c
+ * constructor that accepts a single argument to which arg1 is forwarded.
4a042c
+ *
4a042c
+ * @todo once we have C++>=11 use variadic templates and std::forward to remove
4a042c
+ * all overloads of enforce
4a042c
+ */
4a042c
+template <typename exception_t, typename T>
4a042c
+inline void enforce(bool condition, const T& arg1)
4a042c
+{
4a042c
+    if (!condition) {
4a042c
+        throw exception_t(arg1);
4a042c
+    }
4a042c
+}
4a042c
+
4a042c
+/*!
4a042c
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
4a042c
+ * the given error_code.
4a042c
+ */
4a042c
+inline void enforce(bool condition, Exiv2::ErrorCode err_code)
4a042c
+{
4a042c
+    if (!condition) {
4a042c
+        throw Exiv2::Error(err_code);
4a042c
+    }
4a042c
+}
4a042c
+
4a042c
+/*!
4a042c
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
4a042c
+ * the given error_code & arg1.
4a042c
+ */
4a042c
+template <typename T>
4a042c
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1)
4a042c
+{
4a042c
+    if (!condition) {
4a042c
+        throw Exiv2::Error(err_code, arg1);
4a042c
+    }
4a042c
+}
4a042c
+
4a042c
+/*!
4a042c
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
4a042c
+ * the given error_code, arg1 & arg2.
4a042c
+ */
4a042c
+template <typename T, typename U>
4a042c
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2)
4a042c
+{
4a042c
+    if (!condition) {
4a042c
+        throw Exiv2::Error(err_code, arg1, arg2);
4a042c
+    }
4a042c
+}
4a042c
+
4a042c
+/*!
4a042c
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
4a042c
+ * the given error_code, arg1, arg2 & arg3.
4a042c
+ */
4a042c
+template <typename T, typename U, typename V>
4a042c
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2, const V& arg3)
4a042c
+{
4a042c
+    if (!condition) {
4a042c
+        throw Exiv2::Error(err_code, arg1, arg2, arg3);
4a042c
+    }
4a042c
+}
4a042c
4a042c
diff --git a/src/pngchunk.cpp b/src/pngchunk.cpp
4a042c
index 4dcca4d..aae0f5f 100644
4a042c
--- a/src/pngchunk.cpp
4a042c
+++ b/src/pngchunk.cpp
4a042c
@@ -37,6 +37,7 @@ EXIV2_RCSID("@(#) $Id$")
4a042c
 #include "iptc.hpp"
4a042c
 #include "image.hpp"
4a042c
 #include "error.hpp"
4a042c
+#include "enforce.hpp"
4a042c
4a042c
 // + standard includes
4a042c
 #include <sstream>
4a042c
@@ -46,6 +47,7 @@ EXIV2_RCSID("@(#) $Id$")
4a042c
 #include <iostream>
4a042c
 #include <cassert>
4a042c
 #include <cstdio>
4a042c
+#include <algorithm>
4a042c
4a042c
 #include <zlib.h>     // To uncompress or compress text chunk
4a042c
4a042c
@@ -86,7 +88,7 @@ namespace Exiv2 {
4a042c
4a042c
 #ifdef DEBUG
4a042c
         std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk data: "
4a042c
-                  << std::string((const char*)arr.pData_, arr.size_) << "\n";
4a042c
+                  << std::string((const char*)arr.pData_, arr.size_) << std::endl;
4a042c
 #endif
4a042c
         parseChunkContent(pImage, key.pData_, key.size_, arr);
4a042c
4a042c
@@ -99,7 +101,7 @@ namespace Exiv2 {
4a042c
4a042c
 #ifdef DEBUG
4a042c
         std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk key: "
4a042c
-                  << std::string((const char*)key.pData_, key.size_) << "\n";
4a042c
+                  << std::string((const char*)key.pData_, key.size_) << std::endl;
4a042c
 #endif
4a042c
         return parseTXTChunk(data, key.size_, type);
4a042c
4a042c
@@ -164,12 +166,18 @@ namespace Exiv2 {
4a042c
         }
4a042c
         else if(type == iTXt_Chunk)
4a042c
         {
4a042c
+            const int nullSeparators = std::count(&data.pData_[keysize+3], &data.pData_[data.size_], '\0');
4a042c
+
4a042c
+            enforce(nullSeparators >= 2, Exiv2::kerCorruptedMetadata);
4a042c
+
4a042c
             // Extract a deflate compressed or uncompressed UTF-8 text chunk
4a042c
4a042c
             // we get the compression flag after the key
4a042c
-            const byte* compressionFlag   = data.pData_ + keysize + 1;
4a042c
+            const byte compressionFlag   = data.pData_[keysize + 1];
4a042c
             // we get the compression method after the compression flag
4a042c
-            const byte* compressionMethod = data.pData_ + keysize + 2;
4a042c
+            const byte compressionMethod = data.pData_[keysize + 2];
4a042c
+            enforce(compressionFlag == 0x00 || compressionFlag == 0x01, Exiv2::kerCorruptedMetadata);
4a042c
+            enforce(compressionMethod == 0x00, Exiv2::kerCorruptedMetadata);
4a042c
             // language description string after the compression technique spec
4a042c
             std::string languageText((const char*)(data.pData_ + keysize + 3));
4a042c
             unsigned int languageTextSize = static_cast<unsigned int>(languageText.size());
4a042c
@@ -177,7 +185,7 @@ namespace Exiv2 {
4a042c
             std::string translatedKeyText((const char*)(data.pData_ + keysize + 3 + languageTextSize +1));
4a042c
             unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
4a042c
4a042c
-            if ( compressionFlag[0] == 0x00 )
4a042c
+            if ( compressionFlag == 0x00 )
4a042c
             {
4a042c
                 // then it's an uncompressed iTXt chunk
4a042c
 #ifdef DEBUG
4a042c
@@ -191,7 +199,7 @@ namespace Exiv2 {
4a042c
                 arr.alloc(textsize);
4a042c
                 arr = DataBuf(text, textsize);
4a042c
             }
4a042c
-            else if ( compressionFlag[0] == 0x01 && compressionMethod[0] == 0x00 )
4a042c
+            else if ( compressionFlag == 0x01 && compressionMethod == 0x00 )
4a042c
             {
4a042c
                 // then it's a zlib compressed iTXt chunk
4a042c
 #ifdef DEBUG
4a042c
diff --git a/src/pngimage.cpp b/src/pngimage.cpp
4a042c
index ed7399a..991da6c 100644
4a042c
--- a/src/pngimage.cpp
4a042c
+++ b/src/pngimage.cpp
4a042c
@@ -375,7 +375,7 @@ namespace Exiv2 {
4a042c
     void PngImage::readMetadata()
4a042c
     {
4a042c
 #ifdef DEBUG
4a042c
-        std::cerr << "Exiv2::PngImage::readMetadata: Reading PNG file " << io_->path() << "\n";
4a042c
+        std::cerr << "Exiv2::PngImage::readMetadata: Reading PNG file " << io_->path() << std::endl;
4a042c
 #endif
4a042c
         if (io_->open() != 0)
4a042c
         {
4a042c
@@ -398,7 +398,7 @@ namespace Exiv2 {
4a042c
             // Read chunk header.
4a042c
4a042c
 #ifdef DEBUG
4a042c
-            std::cout << "Exiv2::PngImage::readMetadata: Position: " << io_->tell() << "\n";
4a042c
+            std::cout << "Exiv2::PngImage::readMetadata: Position: " << io_->tell() << std::endl;
4a042c
 #endif
4a042c
             std::memset(cheaderBuf.pData_, 0x0, cheaderBuf.size_);
4a042c
             long bufRead = io_->read(cheaderBuf.pData_, cheaderBuf.size_);
4a042c
@@ -432,14 +432,14 @@ namespace Exiv2 {
4a042c
                 {
4a042c
                     // Last chunk found: we stop parsing.
4a042c
 #ifdef DEBUG
4a042c
-                    std::cout << "Exiv2::PngImage::readMetadata: Found IEND chunk (length: " << dataOffset << ")\n";
4a042c
+                    std::cout << "Exiv2::PngImage::readMetadata: Found IEND chunk with length: " << dataOffset << std::endl;
4a042c
 #endif
4a042c
                     return;
4a042c
                 }
4a042c
                 else if (!memcmp(cheaderBuf.pData_ + 4, "IHDR", 4))
4a042c
                 {
4a042c
 #ifdef DEBUG
4a042c
-                    std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk (length: " << dataOffset << ")\n";
4a042c
+                    std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk with length: " << dataOffset << std::endl;
4a042c
 #endif
4a042c
                     if (cdataBuf.size_ >= 8) {
4a042c
                         PngChunk::decodeIHDRChunk(cdataBuf, &pixelWidth_, &pixelHeight_);
4a042c
@@ -448,21 +448,21 @@ namespace Exiv2 {
4a042c
                 else if (!memcmp(cheaderBuf.pData_ + 4, "tEXt", 4))
4a042c
                 {
4a042c
 #ifdef DEBUG
4a042c
-                    std::cout << "Exiv2::PngImage::readMetadata: Found tEXt chunk (length: " << dataOffset << ")\n";
4a042c
+                    std::cout << "Exiv2::PngImage::readMetadata: Found tEXt chunk with length: " << dataOffset << std::endl;
4a042c
 #endif
4a042c
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::tEXt_Chunk);
4a042c
                 }
4a042c
                 else if (!memcmp(cheaderBuf.pData_ + 4, "zTXt", 4))
4a042c
                 {
4a042c
 #ifdef DEBUG
4a042c
-                    std::cout << "Exiv2::PngImage::readMetadata: Found zTXt chunk (length: " << dataOffset << ")\n";
4a042c
+                    std::cout << "Exiv2::PngImage::readMetadata: Found zTXt chunk with length: " << dataOffset << std::endl;
4a042c
 #endif
4a042c
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::zTXt_Chunk);
4a042c
                 }
4a042c
                 else if (!memcmp(cheaderBuf.pData_ + 4, "iTXt", 4))
4a042c
                 {
4a042c
 #ifdef DEBUG
4a042c
-                    std::cout << "Exiv2::PngImage::readMetadata: Found iTXt chunk (length: " << dataOffset << ")\n";
4a042c
+                    std::cout << "Exiv2::PngImage::readMetadata: Found iTXt chunk with length: " << dataOffset << std::endl;
4a042c
 #endif
4a042c
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::iTXt_Chunk);
4a042c
                 }
4a042c
@@ -481,7 +481,7 @@ namespace Exiv2 {
4a042c
4a042c
             // Move to the next chunk: chunk data size + 4 CRC bytes.
4a042c
 #ifdef DEBUG
4a042c
-            std::cout << "Exiv2::PngImage::readMetadata: Seek to offset: " << dataOffset + 4 << "\n";
4a042c
+            std::cout << "Exiv2::PngImage::readMetadata: Seek to offset: " << dataOffset + 4 << std::endl;
4a042c
 #endif
4a042c
             io_->seek(dataOffset + 4 , BasicIo::cur);
4a042c
             if (io_->error() || io_->eof()) throw Error(14);
4a042c
@@ -511,8 +511,8 @@ namespace Exiv2 {
4a042c
         if (!outIo.isopen()) throw Error(21);
4a042c
4a042c
 #ifdef DEBUG
4a042c
-        std::cout << "Exiv2::PngImage::doWriteMetadata: Writing PNG file " << io_->path() << "\n";
4a042c
-        std::cout << "Exiv2::PngImage::doWriteMetadata: tmp file created " << outIo.path() << "\n";
4a042c
+        std::cout << "Exiv2::PngImage::doWriteMetadata: Writing PNG file " << io_->path() << std::endl;
4a042c
+        std::cout << "Exiv2::PngImage::doWriteMetadata: tmp file created " << outIo.path() << std::endl;
4a042c
 #endif
4a042c
4a042c
         // Ensure that this is the correct image type