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

240d3a
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
240d3a
index aecd621..cbbd859 100644
240d3a
--- a/src/CMakeLists.txt
240d3a
+++ b/src/CMakeLists.txt
240d3a
@@ -26,6 +26,7 @@ SET( LIBEXIV2_PRIVATE_HDR canonmn_int.hpp
240d3a
                           pngchunk_int.hpp
240d3a
                           rcsid_int.hpp
240d3a
                           rw2image_int.hpp
240d3a
+                          safe_op.hpp
240d3a
                           samsungmn_int.hpp
240d3a
                           sigmamn_int.hpp
240d3a
                           sonymn_int.hpp
240d3a
@@ -102,6 +103,7 @@ SET( LIBEXIV2_SRC         asfvideo.cpp
240d3a
                           futils.cpp
240d3a
                           fujimn.cpp
240d3a
                           gifimage.cpp
240d3a
+                          helper_functions.cpp
240d3a
                           http.cpp
240d3a
                           image.cpp
240d3a
                           ini.cpp
240d3a
diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp
240d3a
new file mode 100644
240d3a
index 0000000..623fbc1
240d3a
--- /dev/null
240d3a
+++ b/src/helper_functions.cpp
240d3a
@@ -0,0 +1,39 @@
240d3a
+// ********************************************************* -*- C++ -*-
240d3a
+/*
240d3a
+ * Copyright (C) 2004-2018 Exiv2 authors
240d3a
+ *
240d3a
+ * This program is part of the Exiv2 distribution.
240d3a
+ *
240d3a
+ * This program is free software; you can redistribute it and/or
240d3a
+ * modify it under the terms of the GNU General Public License
240d3a
+ * as published by the Free Software Foundation; either version 2
240d3a
+ * of the License, or (at your option) any later version.
240d3a
+ *
240d3a
+ * This program is distributed in the hope that it will be useful,
240d3a
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
240d3a
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
240d3a
+ * GNU General Public License for more details.
240d3a
+ *
240d3a
+ * You should have received a copy of the GNU General Public License
240d3a
+ * along with this program; if not, write to the Free Software
240d3a
+ * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
240d3a
+ */
240d3a
+/*!
240d3a
+  @file    helper_functions.cpp
240d3a
+  @brief   A collection of helper functions
240d3a
+  @author  Dan Čermák (D4N)
240d3a
+           dan.cermak@cgc-instruments.com
240d3a
+  @date    25-May-18, D4N: created
240d3a
+ */
240d3a
+
240d3a
+#include "helper_functions.hpp"
240d3a
+
240d3a
+#include <string.h>
240d3a
+
240d3a
+
240d3a
+std::string string_from_unterminated(const char* data, size_t data_length)
240d3a
+{
240d3a
+    const size_t StringLength = strnlen(data, data_length);
240d3a
+
240d3a
+    return std::string(data, StringLength);
240d3a
+}
240d3a
diff --git a/src/helper_functions.hpp b/src/helper_functions.hpp
240d3a
new file mode 100644
240d3a
index 0000000..d70cbc1
240d3a
--- /dev/null
240d3a
+++ b/src/helper_functions.hpp
240d3a
@@ -0,0 +1,50 @@
240d3a
+// ********************************************************* -*- C++ -*-
240d3a
+/*
240d3a
+ * Copyright (C) 2004-2018 Exiv2 authors
240d3a
+ *
240d3a
+ * This program is part of the Exiv2 distribution.
240d3a
+ *
240d3a
+ * This program is free software; you can redistribute it and/or
240d3a
+ * modify it under the terms of the GNU General Public License
240d3a
+ * as published by the Free Software Foundation; either version 2
240d3a
+ * of the License, or (at your option) any later version.
240d3a
+ *
240d3a
+ * This program is distributed in the hope that it will be useful,
240d3a
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
240d3a
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
240d3a
+ * GNU General Public License for more details.
240d3a
+ *
240d3a
+ * You should have received a copy of the GNU General Public License
240d3a
+ * along with this program; if not, write to the Free Software
240d3a
+ * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
240d3a
+ */
240d3a
+/*!
240d3a
+  @file    helper_functions.hpp
240d3a
+  @brief   A collection of helper functions
240d3a
+  @author  Dan Čermák (D4N)
240d3a
+           dan.cermak@cgc-instruments.com
240d3a
+  @date    25-May-18, D4N: created
240d3a
+ */
240d3a
+#ifndef HELPER_FUNCTIONS_HPP
240d3a
+#define HELPER_FUNCTIONS_HPP
240d3a
+
240d3a
+#include <string>
240d3a
+
240d3a
+/*!
240d3a
+  @brief Convert a (potentially not null terminated) array into a
240d3a
+  std::string.
240d3a
+
240d3a
+  Convert a C style string that may or may not be null terminated safely
240d3a
+  into a std::string. The string's termination is either set at the first \0
240d3a
+  or after data_length characters.
240d3a
+
240d3a
+  @param[in] data  A c-string from which the std::string shall be
240d3a
+      constructed. Does not need to be null terminated.
240d3a
+  @param[in] data_length  An upper bound for the string length (must be at most
240d3a
+      the allocated length of `buffer`). If no null terminator is found in data,
240d3a
+      then the resulting std::string will be null terminated at `data_length`.
240d3a
+
240d3a
+ */
240d3a
+std::string string_from_unterminated(const char* data, size_t data_length);
240d3a
+
240d3a
+#endif  // HELPER_FUNCTIONS_HPP
240d3a
diff --git a/src/pngchunk.cpp b/src/pngchunk.cpp
240d3a
index 29ffcfa..e4e3274 100644
240d3a
--- a/src/pngchunk.cpp
240d3a
+++ b/src/pngchunk.cpp
240d3a
@@ -38,6 +38,8 @@ EXIV2_RCSID("@(#) $Id$")
240d3a
 #include "image.hpp"
240d3a
 #include "error.hpp"
240d3a
 #include "enforce.hpp"
240d3a
+#include "helper_functions.hpp"
240d3a
+#include "safe_op.hpp"
240d3a
 
240d3a
 // + standard includes
240d3a
 #include <sstream>
240d3a
@@ -137,6 +139,8 @@ namespace Exiv2 {
240d3a
 
240d3a
         if(type == zTXt_Chunk)
240d3a
         {
240d3a
+            enforce(data.size_ >= Safe::add(keysize, 2), Exiv2::kerCorruptedMetadata);
240d3a
+
240d3a
             // Extract a deflate compressed Latin-1 text chunk
240d3a
 
240d3a
             // we get the compression method after the key
240d3a
@@ -153,11 +157,13 @@ namespace Exiv2 {
240d3a
             // compressed string after the compression technique spec
240d3a
             const byte* compressedText      = data.pData_ + keysize + 2;
240d3a
             unsigned int compressedTextSize = data.size_  - keysize - 2;
240d3a
+            enforce(compressedTextSize < data.size_, kerCorruptedMetadata);
240d3a
 
240d3a
             zlibUncompress(compressedText, compressedTextSize, arr);
240d3a
         }
240d3a
         else if(type == tEXt_Chunk)
240d3a
         {
240d3a
+            enforce(data.size_ >= Safe::add(keysize, 1), Exiv2::kerCorruptedMetadata);
240d3a
             // Extract a non-compressed Latin-1 text chunk
240d3a
 
240d3a
             // the text comes after the key, but isn't null terminated
240d3a
@@ -168,6 +174,7 @@ namespace Exiv2 {
240d3a
         }
240d3a
         else if(type == iTXt_Chunk)
240d3a
         {
240d3a
+            enforce(data.size_ >= Safe::add(keysize, 3), Exiv2::kerCorruptedMetadata);
240d3a
             const int nullSeparators = std::count(&data.pData_[keysize+3], &data.pData_[data.size_], '\0');
240d3a
 
240d3a
             enforce(nullSeparators >= 2, Exiv2::kerCorruptedMetadata);
240d3a
@@ -180,42 +187,46 @@ namespace Exiv2 {
240d3a
             const byte compressionMethod = data.pData_[keysize + 2];
240d3a
             enforce(compressionFlag == 0x00 || compressionFlag == 0x01, Exiv2::kerCorruptedMetadata);
240d3a
             enforce(compressionMethod == 0x00, Exiv2::kerCorruptedMetadata);
240d3a
+
240d3a
             // language description string after the compression technique spec
240d3a
-            std::string languageText((const char*)(data.pData_ + keysize + 3));
240d3a
-            unsigned int languageTextSize = static_cast<unsigned int>(languageText.size());
240d3a
+            const size_t languageTextMaxSize = data.size_ - keysize - 3;
240d3a
+            std::string languageText =
240d3a
+                string_from_unterminated((const char*)(data.pData_ + Safe::add(keysize, 3)), languageTextMaxSize);
240d3a
+            const unsigned int languageTextSize = static_cast<unsigned int>(languageText.size());
240d3a
+            enforce(data.size_ >= Safe::add(static_cast<unsigned int>(Safe::add(keysize, 4)), languageTextSize),
240d3a
+                    Exiv2::kerCorruptedMetadata);
240d3a
+
240d3a
             // translated keyword string after the language description
240d3a
-            std::string translatedKeyText((const char*)(data.pData_ + keysize + 3 + languageTextSize +1));
240d3a
-            unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
240d3a
+            std::string translatedKeyText =
240d3a
+                string_from_unterminated((const char*)(data.pData_ + keysize + 3 + languageTextSize + 1),
240d3a
+                                         data.size_ - (keysize + 3 + languageTextSize + 1));
240d3a
+            const unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
240d3a
 
240d3a
-            if ( compressionFlag == 0x00 )
240d3a
-            {
240d3a
-                // then it's an uncompressed iTXt chunk
240d3a
-#ifdef DEBUG
240d3a
-                std::cout << "Exiv2::PngChunk::parseTXTChunk: We found an uncompressed iTXt field\n";
240d3a
-#endif
240d3a
+            if ((compressionFlag == 0x00) || (compressionFlag == 0x01 && compressionMethod == 0x00)) {
240d3a
+                enforce(Safe::add(static_cast<unsigned int>(keysize + 3 + languageTextSize + 1),
240d3a
+                                  Safe::add(translatedKeyTextSize, 1u)) <= data.size_,
240d3a
+                        Exiv2::kerCorruptedMetadata);
240d3a
 
240d3a
-                // the text comes after the translated keyword, but isn't null terminated
240d3a
                 const byte* text = data.pData_ + keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1;
240d3a
-                long textsize    = data.size_ - (keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1);
240d3a
+                const long textsize = data.size_ - (keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1);
240d3a
 
240d3a
-                arr.alloc(textsize);
240d3a
-                arr = DataBuf(text, textsize);
240d3a
-            }
240d3a
-            else if ( compressionFlag == 0x01 && compressionMethod == 0x00 )
240d3a
-            {
240d3a
-                // then it's a zlib compressed iTXt chunk
240d3a
+                if (compressionFlag == 0x00) {
240d3a
+                    // then it's an uncompressed iTXt chunk
240d3a
 #ifdef DEBUG
240d3a
-                std::cout << "Exiv2::PngChunk::parseTXTChunk: We found a zlib compressed iTXt field\n";
240d3a
+                    std::cout << "Exiv2::PngChunk::parseTXTChunk: We found an uncompressed iTXt field\n";
240d3a
 #endif
240d3a
 
240d3a
-                // the compressed text comes after the translated keyword, but isn't null terminated
240d3a
-                const byte* compressedText = data.pData_ + keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1;
240d3a
-                long compressedTextSize    = data.size_ - (keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1);
240d3a
-
240d3a
-                zlibUncompress(compressedText, compressedTextSize, arr);
240d3a
-            }
240d3a
-            else
240d3a
-            {
240d3a
+                    arr.alloc(textsize);
240d3a
+                    arr = DataBuf(text, textsize);
240d3a
+                } else if (compressionFlag == 0x01 && compressionMethod == 0x00) {
240d3a
+                    // then it's a zlib compressed iTXt chunk
240d3a
+#ifdef DEBUG
240d3a
+                    std::cout << "Exiv2::PngChunk::parseTXTChunk: We found a zlib compressed iTXt field\n";
240d3a
+#endif
240d3a
+                    // the compressed text comes after the translated keyword, but isn't null terminated
240d3a
+                    zlibUncompress(text, textsize, arr);
240d3a
+                }
240d3a
+            } else {
240d3a
                 // then it isn't zlib compressed and we are sunk
240d3a
 #ifdef DEBUG
240d3a
                 std::cerr << "Exiv2::PngChunk::parseTXTChunk: Non-standard iTXt compression method.\n";