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

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