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

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