Blame SOURCES/exiv2-CVE-2017-17725.patch

cc1899
diff --git a/src/actions.cpp b/src/actions.cpp
cc1899
index 35c7965..cb905f6 100644
cc1899
--- a/src/actions.cpp
cc1899
+++ b/src/actions.cpp
cc1899
@@ -59,6 +59,7 @@ EXIV2_RCSID("@(#) $Id: actions.cpp 4719 2017-03-08 20:42:28Z robinwmills $")
cc1899
 #include <ctime>
cc1899
 #include <cmath>
cc1899
 #include <cassert>
cc1899
+#include <stdexcept>
cc1899
 #include <sys/types.h>                  // for stat()
cc1899
 #include <sys/stat.h>                   // for stat()
cc1899
 #ifdef EXV_HAVE_UNISTD_H
cc1899
@@ -236,33 +237,43 @@ namespace Action {
cc1899
     }
cc1899
cc1899
     int Print::run(const std::string& path)
cc1899
-    try {
cc1899
-        path_ = path;
cc1899
-        int rc = 0;
cc1899
-        Exiv2::PrintStructureOption option = Exiv2::kpsNone ;
cc1899
-        switch (Params::instance().printMode_) {
cc1899
-            case Params::pmSummary:   rc = printSummary();     break;
cc1899
-            case Params::pmList:      rc = printList();        break;
cc1899
-            case Params::pmComment:   rc = printComment();     break;
cc1899
-            case Params::pmPreview:   rc = printPreviewList(); break;
cc1899
-            case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic)     ; break;
cc1899
-            case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive) ; break;
cc1899
-
cc1899
-            case Params::pmXMP:
cc1899
-                 option = option == Exiv2::kpsNone ? Exiv2::kpsXMP        : option;  // drop
cc1899
-            case Params::pmIccProfile:{
cc1899
-                 option = option == Exiv2::kpsNone ? Exiv2::kpsIccProfile : option;
cc1899
-                 _setmode(_fileno(stdout),O_BINARY);
cc1899
-                 rc = printStructure(std::cout,option);
cc1899
-            } break;
cc1899
+    {
cc1899
+        try {
cc1899
+            path_ = path;
cc1899
+            int rc = 0;
cc1899
+            Exiv2::PrintStructureOption option = Exiv2::kpsNone ;
cc1899
+            switch (Params::instance().printMode_) {
cc1899
+                case Params::pmSummary:   rc = printSummary();     break;
cc1899
+                case Params::pmList:      rc = printList();        break;
cc1899
+                case Params::pmComment:   rc = printComment();     break;
cc1899
+                case Params::pmPreview:   rc = printPreviewList(); break;
cc1899
+                case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic)     ; break;
cc1899
+                case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive) ; break;
cc1899
+
cc1899
+                case Params::pmXMP:
cc1899
+                    if (option == Exiv2::kpsNone)
cc1899
+                        option = Exiv2::kpsXMP;
cc1899
+                    // drop
cc1899
+                case Params::pmIccProfile:
cc1899
+                    if (option == Exiv2::kpsNone)
cc1899
+                        option = Exiv2::kpsIccProfile;
cc1899
+                    _setmode(_fileno(stdout),O_BINARY);
cc1899
+                    rc = printStructure(std::cout,option);
cc1899
+                    break;
cc1899
+            }
cc1899
+            return rc;
cc1899
+        }
cc1899
+        catch(const Exiv2::AnyError& e) {
cc1899
+            std::cerr << "Exiv2 exception in print action for file "
cc1899
+                      << path << ":\n" << e << "\n";
cc1899
+            return 1;
cc1899
+        }
cc1899
+        catch(const std::overflow_error& e) {
cc1899
+            std::cerr << "std::overflow_error exception in print action for file "
cc1899
+                      << path << ":\n" << e.what() << "\n";
cc1899
+            return 1;
cc1899
         }
cc1899
-        return rc;
cc1899
     }
cc1899
-    catch(const Exiv2::AnyError& e) {
cc1899
-        std::cerr << "Exiv2 exception in print action for file "
cc1899
-                  << path << ":\n" << e << "\n";
cc1899
-        return 1;
cc1899
-    } // Print::run
cc1899
cc1899
     int Print::printStructure(std::ostream& out, Exiv2::PrintStructureOption option)
cc1899
     {
cc1899
diff --git a/src/jp2image.cpp b/src/jp2image.cpp
cc1899
index ac31257..4c072d7 100644
cc1899
--- a/src/jp2image.cpp
cc1899
+++ b/src/jp2image.cpp
cc1899
@@ -41,6 +41,7 @@ EXIV2_RCSID("@(#) $Id: jp2image.cpp 4759 2017-04-23 10:58:54Z robinwmills $")
cc1899
 #include "error.hpp"
cc1899
 #include "futils.hpp"
cc1899
 #include "types.hpp"
cc1899
+#include "safe_op.hpp"
cc1899
cc1899
 // + standard includes
cc1899
 #include <string>
cc1899
@@ -269,8 +270,9 @@ namespace Exiv2
cc1899
                             std::cout << "Exiv2::Jp2Image::readMetadata: "
cc1899
                                      << "Color data found" << std::endl;
cc1899
 #endif
cc1899
+
cc1899
                             long pad = 3 ; // 3 padding bytes 2 0 0
cc1899
-                            DataBuf data(subBox.length+8);
cc1899
+                            DataBuf data(Safe::add(subBox.length, static_cast<uint32_t>(8)));
cc1899
                             io_->read(data.pData_,data.size_);
cc1899
                             long    iccLength = getULong(data.pData_+pad, bigEndian);
cc1899
                             DataBuf icc(iccLength);
cc1899
diff --git a/src/safe_op.hpp b/src/safe_op.hpp
cc1899
new file mode 100644
cc1899
index 0000000..014b7f3
cc1899
--- /dev/null
cc1899
+++ b/src/safe_op.hpp
cc1899
@@ -0,0 +1,310 @@
cc1899
+// ********************************************************* -*- C++ -*-
cc1899
+/*
cc1899
+ * Copyright (C) 2004-2017 Exiv2 maintainers
cc1899
+ *
cc1899
+ * This program is part of the Exiv2 distribution.
cc1899
+ *
cc1899
+ * This program is free software; you can redistribute it and/or
cc1899
+ * modify it under the terms of the GNU General Public License
cc1899
+ * as published by the Free Software Foundation; either version 2
cc1899
+ * of the License, or (at your option) any later version.
cc1899
+ *
cc1899
+ * This program is distributed in the hope that it will be useful,
cc1899
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
cc1899
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cc1899
+ * GNU General Public License for more details.
cc1899
+ *
cc1899
+ * You should have received a copy of the GNU General Public License
cc1899
+ * along with this program; if not, write to the Free Software
cc1899
+ * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
cc1899
+ */
cc1899
+/*!
cc1899
+  @file    safe_op.hpp
cc1899
+  @brief   Overflow checks for integers
cc1899
+  @author  Dan Čermák (D4N)
cc1899
+           dan.cermak@cgc-instruments.com
cc1899
+  @date    14-Dec-17, D4N: created
cc1899
+ */
cc1899
+
cc1899
+#ifndef SAFE_OP_HPP_
cc1899
+#define SAFE_OP_HPP_
cc1899
+
cc1899
+#include <limits>
cc1899
+#include <stdexcept>
cc1899
+
cc1899
+#ifdef _MSC_VER
cc1899
+#include <Intsafe.h>
cc1899
+#endif
cc1899
+
cc1899
+/*!
cc1899
+ * @brief Arithmetic operations with overflow checks
cc1899
+ */
cc1899
+namespace Safe
cc1899
+{
cc1899
+    /*!
cc1899
+     * @brief Helper structs for providing integer overflow checks.
cc1899
+     *
cc1899
+     * This namespace contains the internal helper structs fallback_add_overflow
cc1899
+     * and builtin_add_overflow. Both have a public static member function add
cc1899
+     * with the following interface:
cc1899
+     *
cc1899
+     * bool add(T summand_1, T summand_2, T& result)
cc1899
+     *
cc1899
+     * where T is the type over which the struct is templated.
cc1899
+     *
cc1899
+     * The function performs a check whether the addition summand_1 + summand_2
cc1899
+     * can be performed without an overflow. If the operation would overflow,
cc1899
+     * true is returned and the addition is not performed if it would result in
cc1899
+     * undefined behavior. If no overflow occurs, the sum is saved in result and
cc1899
+     * false is returned.
cc1899
+     *
cc1899
+     * fallback_add_overflow implements a portable but slower overflow check.
cc1899
+     * builtin_add_overflow uses compiler builtins (when available) and should
cc1899
+     * be considerably faster. As builtins are not available for all types,
cc1899
+     * builtin_add_overflow falls back to fallback_add_overflow when no builtin
cc1899
+     * is available.
cc1899
+     */
cc1899
+    namespace Internal
cc1899
+    {
cc1899
+        /*!
cc1899
+         * @brief Helper struct to determine whether a type is signed or unsigned
cc1899
+
cc1899
+         * This struct is a backport of std::is_signed from C++11. It has a public
cc1899
+         * enum with the property VALUE which is true when the type is signed or
cc1899
+         * false if it is unsigned.
cc1899
+         */
cc1899
+        template <typename T>
cc1899
+        struct is_signed
cc1899
+        {
cc1899
+            enum
cc1899
+            {
cc1899
+                VALUE = T(-1) < T(0)
cc1899
+            };
cc1899
+        };
cc1899
+
cc1899
+        /*!
cc1899
+         * @brief Helper struct for SFINAE, from C++11
cc1899
+
cc1899
+         * This struct has a public typedef called type typedef'd to T if B is
cc1899
+         * true. Otherwise there is no typedef.
cc1899
+         */
cc1899
+        template <bool B, class T = void>
cc1899
+        struct enable_if
cc1899
+        {
cc1899
+        };
cc1899
+
cc1899
+        /*!
cc1899
+         * @brief Specialization of enable_if for the case B == true
cc1899
+         */
cc1899
+        template <class T>
cc1899
+        struct enable_if<true, T>
cc1899
+        {
cc1899
+            typedef T type;
cc1899
+        };
cc1899
+
cc1899
+        /*!
cc1899
+         * @brief Fallback overflow checker, specialized via SFINAE
cc1899
+         *
cc1899
+         * This struct implements a 'fallback' addition with an overflow check,
cc1899
+         * i.e. it does not rely on compiler intrinsics.  It is specialized via
cc1899
+         * SFINAE for signed and unsigned integer types and provides a public
cc1899
+         * static member function add.
cc1899
+         */
cc1899
+        template <typename T, typename = void>
cc1899
+        struct fallback_add_overflow;
cc1899
+
cc1899
+        /*!
cc1899
+         * @brief Overload of fallback_add_overflow for signed integers
cc1899
+         */
cc1899
+        template <typename T>
cc1899
+        struct fallback_add_overflow<T, typename enable_if<is_signed<T>::VALUE>::type>
cc1899
+        {
cc1899
+            /*!
cc1899
+             * @brief Adds the two summands only if no overflow occurs
cc1899
+             *
cc1899
+             * This function performs a check if summand_1 + summand_2 would
cc1899
+             * overflow and returns true in that case. If no overflow occurs,
cc1899
+             * the sum is saved in result and false is returned.
cc1899
+             *
cc1899
+             * @return true on overflow, false on no overflow
cc1899
+             *
cc1899
+             * The check for an overflow is performed before the addition to
cc1899
+             * ensure that no undefined behavior occurs. The value in result is
cc1899
+             * only valid when the function returns false.
cc1899
+             *
cc1899
+             * Further information:
cc1899
+             * https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
cc1899
+             */
cc1899
+            static bool add(T summand_1, T summand_2, T& result)
cc1899
+            {
cc1899
+                if (((summand_2 >= 0) && (summand_1 > std::numeric_limits<T>::max() - summand_2)) ||
cc1899
+                    ((summand_2 < 0) && (summand_1 < std::numeric_limits<T>::min() - summand_2))) {
cc1899
+                    return true;
cc1899
+                } else {
cc1899
+                    result = summand_1 + summand_2;
cc1899
+                    return false;
cc1899
+                }
cc1899
+            }
cc1899
+        };
cc1899
+
cc1899
+        /*!
cc1899
+         * @brief Overload of fallback_add_overflow for unsigned integers
cc1899
+         */
cc1899
+        template <typename T>
cc1899
+        struct fallback_add_overflow<T, typename enable_if<!is_signed<T>::VALUE>::type>
cc1899
+        {
cc1899
+            /*!
cc1899
+             * @brief Adds the two summands only if no overflow occurs
cc1899
+             *
cc1899
+             * This function performs a check if summand_1 + summand_2 would
cc1899
+             * overflow and returns true in that case. If no overflow occurs,
cc1899
+             * the sum is saved in result and false is returned.
cc1899
+             *
cc1899
+             * @return true on overflow, false on no overflow
cc1899
+             *
cc1899
+             * Further information:
cc1899
+             * https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap
cc1899
+             */
cc1899
+            static bool add(T summand_1, T summand_2, T& result)
cc1899
+            {
cc1899
+                if (summand_1 > std::numeric_limits<T>::max() - summand_2) {
cc1899
+                    return true;
cc1899
+                } else {
cc1899
+                    result = summand_1 + summand_2;
cc1899
+                    return false;
cc1899
+                }
cc1899
+            }
cc1899
+        };
cc1899
+
cc1899
+        /*!
cc1899
+         * @brief Overflow checker using compiler intrinsics
cc1899
+         *
cc1899
+         * This struct provides an add function with the same interface &
cc1899
+         * behavior as fallback_add_overload::add but it relies on compiler
cc1899
+         * intrinsics instead. This version should be considerably faster than
cc1899
+         * the fallback version as it can fully utilize available CPU
cc1899
+         * instructions & the compiler's diagnostic.
cc1899
+         *
cc1899
+         * However, as some compilers don't provide intrinsics for certain
cc1899
+         * types, the default implementation of add is the version from falback.
cc1899
+         *
cc1899
+         * The struct is explicitly specialized for each type via #ifdefs for
cc1899
+         * each compiler.
cc1899
+         */
cc1899
+        template <typename T>
cc1899
+        struct builtin_add_overflow
cc1899
+        {
cc1899
+            /*!
cc1899
+             * @brief Add summand_1 and summand_2 and check for overflows.
cc1899
+             *
cc1899
+             * This is the default add() function that uses
cc1899
+             * fallback_add_overflow<T>::add(). All specializations must have
cc1899
+             * exactly the same interface and behave the same way.
cc1899
+             */
cc1899
+            static inline bool add(T summand_1, T summand_2, T& result)
cc1899
+            {
cc1899
+                return fallback_add_overflow<T>::add(summand_1, summand_2, result);
cc1899
+            }
cc1899
+        };
cc1899
+
cc1899
+#if defined(__GNUC__) || defined(__clang__)
cc1899
+#if __GNUC__ >= 5
cc1899
+
cc1899
+/*!
cc1899
+ * This macro pastes a specialization of builtin_add_overflow using gcc's &
cc1899
+ * clang's __builtin_(s/u)add(l)(l)_overlow()
cc1899
+ *
cc1899
+ * The add function is implemented by forwarding the parameters to the intrinsic
cc1899
+ * and returning its value.
cc1899
+ *
cc1899
+ * The intrinsics are documented here:
cc1899
+ * https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html#Integer-Overflow-Builtins
cc1899
+ */
cc1899
+#define SPECIALIZE_builtin_add_overflow(type, builtin_name)                  \
cc1899
+    template <>                                                              \
cc1899
+    struct builtin_add_overflow<type>                                        \
cc1899
+    {                                                                        \
cc1899
+        static inline bool add(type summand_1, type summand_2, type& result) \
cc1899
+        {                                                                    \
cc1899
+            return builtin_name(summand_1, summand_2, &result);              \
cc1899
+        }                                                                    \
cc1899
+    }
cc1899
+
cc1899
+        SPECIALIZE_builtin_add_overflow(int, __builtin_sadd_overflow);
cc1899
+        SPECIALIZE_builtin_add_overflow(long, __builtin_saddl_overflow);
cc1899
+        SPECIALIZE_builtin_add_overflow(long long, __builtin_saddll_overflow);
cc1899
+
cc1899
+        SPECIALIZE_builtin_add_overflow(unsigned int, __builtin_uadd_overflow);
cc1899
+        SPECIALIZE_builtin_add_overflow(unsigned long, __builtin_uaddl_overflow);
cc1899
+        SPECIALIZE_builtin_add_overflow(unsigned long long, __builtin_uaddll_overflow);
cc1899
+
cc1899
+#undef SPECIALIZE_builtin_add_overflow
cc1899
+#endif
cc1899
+
cc1899
+#elif defined(_MSC_VER)
cc1899
+
cc1899
+/*!
cc1899
+ * This macro pastes a specialization of builtin_add_overflow using MSVC's
cc1899
+ * U(Int/Long/LongLong)Add.
cc1899
+ *
cc1899
+ * The add function is implemented by forwarding the parameters to the
cc1899
+ * intrinsic. As MSVC's intrinsics return S_OK on success, this specialization
cc1899
+ * returns whether the intrinsics return value does not equal S_OK. This ensures
cc1899
+ * a uniform interface of the add function (false is returned when no overflow
cc1899
+ * occurs, true on overflow).
cc1899
+ *
cc1899
+ * The intrinsics are documented here:
cc1899
+ * https://msdn.microsoft.com/en-us/library/windows/desktop/ff516460(v=vs.85).aspx
cc1899
+ */
cc1899
+#define SPECIALIZE_builtin_add_overflow_WIN(type, builtin_name)              \
cc1899
+    template <>                                                              \
cc1899
+    struct builtin_add_overflow<type>                                        \
cc1899
+    {                                                                        \
cc1899
+        static inline bool add(type summand_1, type summand_2, type& result) \
cc1899
+        {                                                                    \
cc1899
+            return builtin_name(summand_1, summand_2, &result) != S_OK;      \
cc1899
+        }                                                                    \
cc1899
+    }
cc1899
+
cc1899
+        SPECIALIZE_builtin_add_overflow_WIN(unsigned int, UIntAdd);
cc1899
+        SPECIALIZE_builtin_add_overflow_WIN(unsigned long, ULongAdd);
cc1899
+        SPECIALIZE_builtin_add_overflow_WIN(unsigned long long, ULongLongAdd);
cc1899
+
cc1899
+#undef SPECIALIZE_builtin_add_overflow_WIN
cc1899
+
cc1899
+#endif
cc1899
+
cc1899
+    }  // namespace Internal
cc1899
+
cc1899
+    /*!
cc1899
+     * @brief Safe addition, throws an exception on overflow.
cc1899
+     *
cc1899
+     * This function returns the result of summand_1 and summand_2 only when the
cc1899
+     * operation would not overflow, otherwise an exception of type
cc1899
+     * std::overflow_error is thrown.
cc1899
+     *
cc1899
+     * @param[in] summand_1, summand_2  summands to be summed up
cc1899
+     * @return  the sum of summand_1 and summand_2
cc1899
+     * @throws  std::overflow_error if the addition would overflow
cc1899
+     *
cc1899
+     * This function utilizes compiler builtins when available and should have a
cc1899
+     * very small performance hit then. When builtins are unavailable, a more
cc1899
+     * extensive check is required.
cc1899
+     *
cc1899
+     * Builtins are available for the following configurations:
cc1899
+     * - GCC/Clang for signed and unsigned int, long and long long (not char & short)
cc1899
+     * - MSVC for unsigned int, long and long long
cc1899
+     */
cc1899
+    template <typename T>
cc1899
+    T add(T summand_1, T summand_2)
cc1899
+    {
cc1899
+        T res = 0;
cc1899
+        if (Internal::builtin_add_overflow<T>::add(summand_1, summand_2, res)) {
cc1899
+            throw std::overflow_error("Overflow in addition");
cc1899
+        }
cc1899
+        return res;
cc1899
+    }
cc1899
+
cc1899
+}  // namespace Safe
cc1899
+
cc1899
+#endif  // SAFE_OP_HPP_