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

340e69
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
340e69
index aecd621..cbbd859 100644
340e69
--- a/src/CMakeLists.txt
340e69
+++ b/src/CMakeLists.txt
340e69
@@ -26,6 +26,7 @@ SET( LIBEXIV2_PRIVATE_HDR canonmn_int.hpp
340e69
                           pngchunk_int.hpp
340e69
                           rcsid_int.hpp
340e69
                           rw2image_int.hpp
340e69
+                          safe_op.hpp
340e69
                           samsungmn_int.hpp
340e69
                           sigmamn_int.hpp
340e69
                           sonymn_int.hpp
340e69
@@ -102,6 +103,7 @@ SET( LIBEXIV2_SRC         asfvideo.cpp
340e69
                           futils.cpp
340e69
                           fujimn.cpp
340e69
                           gifimage.cpp
340e69
+                          helper_functions.cpp
340e69
                           http.cpp
340e69
                           image.cpp
340e69
                           ini.cpp
340e69
diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp
340e69
new file mode 100644
340e69
index 0000000..623fbc1
340e69
--- /dev/null
340e69
+++ b/src/helper_functions.cpp
340e69
@@ -0,0 +1,39 @@
340e69
+// ********************************************************* -*- C++ -*-
340e69
+/*
340e69
+ * Copyright (C) 2004-2018 Exiv2 authors
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    helper_functions.cpp
340e69
+  @brief   A collection of helper functions
340e69
+  @author  Dan Čermák (D4N)
340e69
+           dan.cermak@cgc-instruments.com
340e69
+  @date    25-May-18, D4N: created
340e69
+ */
340e69
+
340e69
+#include "helper_functions.hpp"
340e69
+
340e69
+#include <string.h>
340e69
+
340e69
+
340e69
+std::string string_from_unterminated(const char* data, size_t data_length)
340e69
+{
340e69
+    const size_t StringLength = strnlen(data, data_length);
340e69
+
340e69
+    return std::string(data, StringLength);
340e69
+}
340e69
diff --git a/src/helper_functions.hpp b/src/helper_functions.hpp
340e69
new file mode 100644
340e69
index 0000000..d70cbc1
340e69
--- /dev/null
340e69
+++ b/src/helper_functions.hpp
340e69
@@ -0,0 +1,50 @@
340e69
+// ********************************************************* -*- C++ -*-
340e69
+/*
340e69
+ * Copyright (C) 2004-2018 Exiv2 authors
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    helper_functions.hpp
340e69
+  @brief   A collection of helper functions
340e69
+  @author  Dan Čermák (D4N)
340e69
+           dan.cermak@cgc-instruments.com
340e69
+  @date    25-May-18, D4N: created
340e69
+ */
340e69
+#ifndef HELPER_FUNCTIONS_HPP
340e69
+#define HELPER_FUNCTIONS_HPP
340e69
+
340e69
+#include <string>
340e69
+
340e69
+/*!
340e69
+  @brief Convert a (potentially not null terminated) array into a
340e69
+  std::string.
340e69
+
340e69
+  Convert a C style string that may or may not be null terminated safely
340e69
+  into a std::string. The string's termination is either set at the first \0
340e69
+  or after data_length characters.
340e69
+
340e69
+  @param[in] data  A c-string from which the std::string shall be
340e69
+      constructed. Does not need to be null terminated.
340e69
+  @param[in] data_length  An upper bound for the string length (must be at most
340e69
+      the allocated length of `buffer`). If no null terminator is found in data,
340e69
+      then the resulting std::string will be null terminated at `data_length`.
340e69
+
340e69
+ */
340e69
+std::string string_from_unterminated(const char* data, size_t data_length);
340e69
+
340e69
+#endif  // HELPER_FUNCTIONS_HPP
340e69
diff --git a/src/pngchunk.cpp b/src/pngchunk.cpp
340e69
index 29ffcfa..e4e3274 100644
340e69
--- a/src/pngchunk.cpp
340e69
+++ b/src/pngchunk.cpp
340e69
@@ -38,6 +38,8 @@ EXIV2_RCSID("@(#) $Id$")
340e69
 #include "image.hpp"
340e69
 #include "error.hpp"
340e69
 #include "enforce.hpp"
340e69
+#include "helper_functions.hpp"
340e69
+#include "safe_op.hpp"
340e69
 
340e69
 // + standard includes
340e69
 #include <sstream>
340e69
@@ -137,6 +139,8 @@ namespace Exiv2 {
340e69
 
340e69
         if(type == zTXt_Chunk)
340e69
         {
340e69
+            enforce(data.size_ >= Safe::add(keysize, 2), Exiv2::kerCorruptedMetadata);
340e69
+
340e69
             // Extract a deflate compressed Latin-1 text chunk
340e69
 
340e69
             // we get the compression method after the key
340e69
@@ -153,11 +157,13 @@ namespace Exiv2 {
340e69
             // compressed string after the compression technique spec
340e69
             const byte* compressedText      = data.pData_ + keysize + 2;
340e69
             unsigned int compressedTextSize = data.size_  - keysize - 2;
340e69
+            enforce(compressedTextSize < data.size_, kerCorruptedMetadata);
340e69
 
340e69
             zlibUncompress(compressedText, compressedTextSize, arr);
340e69
         }
340e69
         else if(type == tEXt_Chunk)
340e69
         {
340e69
+            enforce(data.size_ >= Safe::add(keysize, 1), Exiv2::kerCorruptedMetadata);
340e69
             // Extract a non-compressed Latin-1 text chunk
340e69
 
340e69
             // the text comes after the key, but isn't null terminated
340e69
@@ -168,6 +174,7 @@ namespace Exiv2 {
340e69
         }
340e69
         else if(type == iTXt_Chunk)
340e69
         {
340e69
+            enforce(data.size_ >= Safe::add(keysize, 3), Exiv2::kerCorruptedMetadata);
340e69
             const int nullSeparators = std::count(&data.pData_[keysize+3], &data.pData_[data.size_], '\0');
340e69
 
340e69
             enforce(nullSeparators >= 2, Exiv2::kerCorruptedMetadata);
340e69
@@ -180,42 +187,46 @@ namespace Exiv2 {
340e69
             const byte compressionMethod = data.pData_[keysize + 2];
340e69
             enforce(compressionFlag == 0x00 || compressionFlag == 0x01, Exiv2::kerCorruptedMetadata);
340e69
             enforce(compressionMethod == 0x00, Exiv2::kerCorruptedMetadata);
340e69
+
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
+            const size_t languageTextMaxSize = data.size_ - keysize - 3;
340e69
+            std::string languageText =
340e69
+                string_from_unterminated((const char*)(data.pData_ + Safe::add(keysize, 3)), languageTextMaxSize);
340e69
+            const unsigned int languageTextSize = static_cast<unsigned int>(languageText.size());
340e69
+            enforce(data.size_ >= Safe::add(static_cast<unsigned int>(Safe::add(keysize, 4)), languageTextSize),
340e69
+                    Exiv2::kerCorruptedMetadata);
340e69
+
340e69
             // translated keyword string after the language description
340e69
-            std::string translatedKeyText((const char*)(data.pData_ + keysize + 3 + languageTextSize +1));
340e69
-            unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
340e69
+            std::string translatedKeyText =
340e69
+                string_from_unterminated((const char*)(data.pData_ + keysize + 3 + languageTextSize + 1),
340e69
+                                         data.size_ - (keysize + 3 + languageTextSize + 1));
340e69
+            const unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
340e69
 
340e69
-            if ( compressionFlag == 0x00 )
340e69
-            {
340e69
-                // then it's an uncompressed iTXt chunk
340e69
-#ifdef DEBUG
340e69
-                std::cout << "Exiv2::PngChunk::parseTXTChunk: We found an uncompressed iTXt field\n";
340e69
-#endif
340e69
+            if ((compressionFlag == 0x00) || (compressionFlag == 0x01 && compressionMethod == 0x00)) {
340e69
+                enforce(Safe::add(static_cast<unsigned int>(keysize + 3 + languageTextSize + 1),
340e69
+                                  Safe::add(translatedKeyTextSize, 1u)) <= data.size_,
340e69
+                        Exiv2::kerCorruptedMetadata);
340e69
 
340e69
-                // the text comes after the translated keyword, but isn't null terminated
340e69
                 const byte* text = data.pData_ + keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1;
340e69
-                long textsize    = data.size_ - (keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1);
340e69
+                const long textsize = data.size_ - (keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1);
340e69
 
340e69
-                arr.alloc(textsize);
340e69
-                arr = DataBuf(text, textsize);
340e69
-            }
340e69
-            else if ( compressionFlag == 0x01 && compressionMethod == 0x00 )
340e69
-            {
340e69
-                // then it's a zlib compressed iTXt chunk
340e69
+                if (compressionFlag == 0x00) {
340e69
+                    // then it's an uncompressed iTXt chunk
340e69
 #ifdef DEBUG
340e69
-                std::cout << "Exiv2::PngChunk::parseTXTChunk: We found a zlib compressed iTXt field\n";
340e69
+                    std::cout << "Exiv2::PngChunk::parseTXTChunk: We found an uncompressed iTXt field\n";
340e69
 #endif
340e69
 
340e69
-                // the compressed text comes after the translated keyword, but isn't null terminated
340e69
-                const byte* compressedText = data.pData_ + keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1;
340e69
-                long compressedTextSize    = data.size_ - (keysize + 3 + languageTextSize + 1 + translatedKeyTextSize + 1);
340e69
-
340e69
-                zlibUncompress(compressedText, compressedTextSize, arr);
340e69
-            }
340e69
-            else
340e69
-            {
340e69
+                    arr.alloc(textsize);
340e69
+                    arr = DataBuf(text, textsize);
340e69
+                } else if (compressionFlag == 0x01 && compressionMethod == 0x00) {
340e69
+                    // then it's a zlib compressed iTXt chunk
340e69
+#ifdef DEBUG
340e69
+                    std::cout << "Exiv2::PngChunk::parseTXTChunk: We found a zlib compressed iTXt field\n";
340e69
+#endif
340e69
+                    // the compressed text comes after the translated keyword, but isn't null terminated
340e69
+                    zlibUncompress(text, textsize, arr);
340e69
+                }
340e69
+            } else {
340e69
                 // then it isn't zlib compressed and we are sunk
340e69
 #ifdef DEBUG
340e69
                 std::cerr << "Exiv2::PngChunk::parseTXTChunk: Non-standard iTXt compression method.\n";