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

240d3a
From 7c6f59619616a01e242401cf4c8e06428539a035 Mon Sep 17 00:00:00 2001
240d3a
From: Luis Diaz Mas <piponazo@gmail.com>
240d3a
Date: Sat, 16 Dec 2017 16:05:08 +0100
240d3a
Subject: Fix arithmetic operation overflow
240d3a
240d3a
240d3a
diff --git a/src/jp2image.cpp b/src/jp2image.cpp
240d3a
index 09d023e2..a308bfd9 100644
240d3a
--- a/src/jp2image.cpp
240d3a
+++ b/src/jp2image.cpp
240d3a
@@ -41,6 +41,7 @@ EXIV2_RCSID("@(#) $Id$")
240d3a
 #include "error.hpp"
240d3a
 #include "futils.hpp"
240d3a
 #include "types.hpp"
240d3a
+#include "safe_op.hpp"
240d3a
240d3a
 // + standard includes
240d3a
 #include <string>
240d3a
@@ -269,15 +270,16 @@ namespace Exiv2
240d3a
                             std::cout << "Exiv2::Jp2Image::readMetadata: "
240d3a
                                      << "Color data found" << std::endl;
240d3a
 #endif
240d3a
+
240d3a
                             const long pad = 3 ; // 3 padding bytes 2 0 0
240d3a
-                            DataBuf data(subBox.length+8);
240d3a
+                            DataBuf data(Safe::add(subBox.length, static_cast<uint32_t>(8)));
240d3a
                             io_->read(data.pData_,data.size_);
240d3a
                             const long    iccLength = getULong(data.pData_+pad, bigEndian);
240d3a
                             // subtracting pad from data.size_ is safe:
240d3a
                             // size_ is at least 8 and pad = 3
240d3a
                             if (iccLength > data.size_ - pad) {
240d3a
                                 throw Error(58);
240d3a
-			    }
240d3a
+                            }
240d3a
                             DataBuf icc(iccLength);
240d3a
                             ::memcpy(icc.pData_,data.pData_+pad,icc.size_);
240d3a
 #ifdef DEBUG
240d3a
diff --git a/src/safe_op.hpp b/src/safe_op.hpp
240d3a
new file mode 100644
240d3a
index 00000000..55d690e3
240d3a
--- /dev/null
240d3a
+++ b/src/safe_op.hpp
240d3a
@@ -0,0 +1,308 @@
240d3a
+// ********************************************************* -*- C++ -*-
240d3a
+/*
240d3a
+ * Copyright (C) 2004-2017 Exiv2 maintainers
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    safe_op.hpp
240d3a
+  @brief   Overflow checks for integers
240d3a
+  @author  Dan Čermák (D4N)
240d3a
+           dan.cermak@cgc-instruments.com
240d3a
+  @date    14-Dec-17, D4N: created
240d3a
+ */
240d3a
+
240d3a
+#ifndef SAFE_OP_HPP_
240d3a
+#define SAFE_OP_HPP_
240d3a
+
240d3a
+#include <limits>
240d3a
+#include <stdexcept>
240d3a
+
240d3a
+#ifdef _MSC_VER
240d3a
+#include <Intsafe.h>
240d3a
+#endif
240d3a
+
240d3a
+/*!
240d3a
+ * @brief Arithmetic operations with overflow checks
240d3a
+ */
240d3a
+namespace Safe
240d3a
+{
240d3a
+    /*!
240d3a
+     * @brief Helper structs for providing integer overflow checks.
240d3a
+     *
240d3a
+     * This namespace contains the internal helper structs fallback_add_overflow
240d3a
+     * and builtin_add_overflow. Both have a public static member function add
240d3a
+     * with the following interface:
240d3a
+     *
240d3a
+     * bool add(T summand_1, T summand_2, T& result)
240d3a
+     *
240d3a
+     * where T is the type over which the struct is templated.
240d3a
+     *
240d3a
+     * The function performs a check whether the addition summand_1 + summand_2
240d3a
+     * can be performed without an overflow. If the operation would overflow,
240d3a
+     * true is returned and the addition is not performed if it would result in
240d3a
+     * undefined behavior. If no overflow occurs, the sum is saved in result and
240d3a
+     * false is returned.
240d3a
+     *
240d3a
+     * fallback_add_overflow implements a portable but slower overflow check.
240d3a
+     * builtin_add_overflow uses compiler builtins (when available) and should
240d3a
+     * be considerably faster. As builtins are not available for all types,
240d3a
+     * builtin_add_overflow falls back to fallback_add_overflow when no builtin
240d3a
+     * is available.
240d3a
+     */
240d3a
+    namespace Internal
240d3a
+    {
240d3a
+        /*!
240d3a
+         * @brief Helper struct to determine whether a type is signed or unsigned
240d3a
+
240d3a
+         * This struct is a backport of std::is_signed from C++11. It has a public
240d3a
+         * enum with the property VALUE which is true when the type is signed or
240d3a
+         * false if it is unsigned.
240d3a
+         */
240d3a
+        template <typename T>
240d3a
+        struct is_signed
240d3a
+        {
240d3a
+            enum
240d3a
+            {
240d3a
+                VALUE = T(-1) < T(0)
240d3a
+            };
240d3a
+        };
240d3a
+
240d3a
+        /*!
240d3a
+         * @brief Helper struct for SFINAE, from C++11
240d3a
+
240d3a
+         * This struct has a public typedef called type typedef'd to T if B is
240d3a
+         * true. Otherwise there is no typedef.
240d3a
+         */
240d3a
+        template <bool B, class T = void>
240d3a
+        struct enable_if
240d3a
+        {
240d3a
+        };
240d3a
+
240d3a
+        /*!
240d3a
+         * @brief Specialization of enable_if for the case B == true
240d3a
+         */
240d3a
+        template <class T>
240d3a
+        struct enable_if<true, T>
240d3a
+        {
240d3a
+            typedef T type;
240d3a
+        };
240d3a
+
240d3a
+        /*!
240d3a
+         * @brief Fallback overflow checker, specialized via SFINAE
240d3a
+         *
240d3a
+         * This struct implements a 'fallback' addition with an overflow check,
240d3a
+         * i.e. it does not rely on compiler intrinsics.  It is specialized via
240d3a
+         * SFINAE for signed and unsigned integer types and provides a public
240d3a
+         * static member function add.
240d3a
+         */
240d3a
+        template <typename T, typename = void>
240d3a
+        struct fallback_add_overflow;
240d3a
+
240d3a
+        /*!
240d3a
+         * @brief Overload of fallback_add_overflow for signed integers
240d3a
+         */
240d3a
+        template <typename T>
240d3a
+        struct fallback_add_overflow<T, typename enable_if<is_signed<T>::VALUE>::type>
240d3a
+        {
240d3a
+            /*!
240d3a
+             * @brief Adds the two summands only if no overflow occurs
240d3a
+             *
240d3a
+             * This function performs a check if summand_1 + summand_2 would
240d3a
+             * overflow and returns true in that case. If no overflow occurs,
240d3a
+             * the sum is saved in result and false is returned.
240d3a
+             *
240d3a
+             * @return true on overflow, false on no overflow
240d3a
+             *
240d3a
+             * The check for an overflow is performed before the addition to
240d3a
+             * ensure that no undefined behavior occurs. The value in result is
240d3a
+             * only valid when the function returns false.
240d3a
+             *
240d3a
+             * Further information:
240d3a
+             * https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
240d3a
+             */
240d3a
+            static bool add(T summand_1, T summand_2, T& result)
240d3a
+            {
240d3a
+                if (((summand_2 >= 0) && (summand_1 > std::numeric_limits<T>::max() - summand_2)) ||
240d3a
+                    ((summand_2 < 0) && (summand_1 < std::numeric_limits<T>::min() - summand_2))) {
240d3a
+                    return true;
240d3a
+                } else {
240d3a
+                    result = summand_1 + summand_2;
240d3a
+                    return false;
240d3a
+                }
240d3a
+            }
240d3a
+        };
240d3a
+
240d3a
+        /*!
240d3a
+         * @brief Overload of fallback_add_overflow for unsigned integers
240d3a
+         */
240d3a
+        template <typename T>
240d3a
+        struct fallback_add_overflow<T, typename enable_if<!is_signed<T>::VALUE>::type>
240d3a
+        {
240d3a
+            /*!
240d3a
+             * @brief Adds the two summands only if no overflow occurs
240d3a
+             *
240d3a
+             * This function performs a check if summand_1 + summand_2 would
240d3a
+             * overflow and returns true in that case. If no overflow occurs,
240d3a
+             * the sum is saved in result and false is returned.
240d3a
+             *
240d3a
+             * @return true on overflow, false on no overflow
240d3a
+             *
240d3a
+             * Further information:
240d3a
+             * https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap
240d3a
+             */
240d3a
+            static bool add(T summand_1, T summand_2, T& result)
240d3a
+            {
240d3a
+                if (summand_1 > std::numeric_limits<T>::max() - summand_2) {
240d3a
+                    return true;
240d3a
+                } else {
240d3a
+                    result = summand_1 + summand_2;
240d3a
+                    return false;
240d3a
+                }
240d3a
+            }
240d3a
+        };
240d3a
+
240d3a
+        /*!
240d3a
+         * @brief Overflow checker using compiler intrinsics
240d3a
+         *
240d3a
+         * This struct provides an add function with the same interface &
240d3a
+         * behavior as fallback_add_overload::add but it relies on compiler
240d3a
+         * intrinsics instead. This version should be considerably faster than
240d3a
+         * the fallback version as it can fully utilize available CPU
240d3a
+         * instructions & the compiler's diagnostic.
240d3a
+         *
240d3a
+         * However, as some compilers don't provide intrinsics for certain
240d3a
+         * types, the default implementation of add is the version from falback.
240d3a
+         *
240d3a
+         * The struct is explicitly specialized for each type via #ifdefs for
240d3a
+         * each compiler.
240d3a
+         */
240d3a
+        template <typename T>
240d3a
+        struct builtin_add_overflow
240d3a
+        {
240d3a
+            /*!
240d3a
+             * @brief Add summand_1 and summand_2 and check for overflows.
240d3a
+             *
240d3a
+             * This is the default add() function that uses
240d3a
+             * fallback_add_overflow<T>::add(). All specializations must have
240d3a
+             * exactly the same interface and behave the same way.
240d3a
+             */
240d3a
+            static inline bool add(T summand_1, T summand_2, T& result)
240d3a
+            {
240d3a
+                return fallback_add_overflow<T>::add(summand_1, summand_2, result);
240d3a
+            }
240d3a
+        };
240d3a
+
240d3a
+#if defined(__GNUC__) || defined(__clang__)
240d3a
+
240d3a
+/*!
240d3a
+ * This macro pastes a specialization of builtin_add_overflow using gcc's &
240d3a
+ * clang's __builtin_(s/u)add(l)(l)_overlow()
240d3a
+ *
240d3a
+ * The add function is implemented by forwarding the parameters to the intrinsic
240d3a
+ * and returning its value.
240d3a
+ *
240d3a
+ * The intrinsics are documented here:
240d3a
+ * https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html#Integer-Overflow-Builtins
240d3a
+ */
240d3a
+#define SPECIALIZE_builtin_add_overflow(type, builtin_name)                  \
240d3a
+    template <>                                                              \
240d3a
+    struct builtin_add_overflow<type>                                        \
240d3a
+    {                                                                        \
240d3a
+        static inline bool add(type summand_1, type summand_2, type& result) \
240d3a
+        {                                                                    \
240d3a
+            return builtin_name(summand_1, summand_2, &result);              \
240d3a
+        }                                                                    \
240d3a
+    }
240d3a
+
240d3a
+        SPECIALIZE_builtin_add_overflow(int, __builtin_sadd_overflow);
240d3a
+        SPECIALIZE_builtin_add_overflow(long, __builtin_saddl_overflow);
240d3a
+        SPECIALIZE_builtin_add_overflow(long long, __builtin_saddll_overflow);
240d3a
+
240d3a
+        SPECIALIZE_builtin_add_overflow(unsigned int, __builtin_uadd_overflow);
240d3a
+        SPECIALIZE_builtin_add_overflow(unsigned long, __builtin_uaddl_overflow);
240d3a
+        SPECIALIZE_builtin_add_overflow(unsigned long long, __builtin_uaddll_overflow);
240d3a
+
240d3a
+#undef SPECIALIZE_builtin_add_overflow
240d3a
+
240d3a
+#elif defined(_MSC_VER)
240d3a
+
240d3a
+/*!
240d3a
+ * This macro pastes a specialization of builtin_add_overflow using MSVC's
240d3a
+ * U(Int/Long/LongLong)Add.
240d3a
+ *
240d3a
+ * The add function is implemented by forwarding the parameters to the
240d3a
+ * intrinsic. As MSVC's intrinsics return S_OK on success, this specialization
240d3a
+ * returns whether the intrinsics return value does not equal S_OK. This ensures
240d3a
+ * a uniform interface of the add function (false is returned when no overflow
240d3a
+ * occurs, true on overflow).
240d3a
+ *
240d3a
+ * The intrinsics are documented here:
240d3a
+ * https://msdn.microsoft.com/en-us/library/windows/desktop/ff516460(v=vs.85).aspx
240d3a
+ */
240d3a
+#define SPECIALIZE_builtin_add_overflow_WIN(type, builtin_name)              \
240d3a
+    template <>                                                              \
240d3a
+    struct builtin_add_overflow<type>                                        \
240d3a
+    {                                                                        \
240d3a
+        static inline bool add(type summand_1, type summand_2, type& result) \
240d3a
+        {                                                                    \
240d3a
+            return builtin_name(summand_1, summand_2, &result) != S_OK;      \
240d3a
+        }                                                                    \
240d3a
+    }
240d3a
+
240d3a
+        SPECIALIZE_builtin_add_overflow_WIN(unsigned int, UIntAdd);
240d3a
+        SPECIALIZE_builtin_add_overflow_WIN(unsigned long, ULongAdd);
240d3a
+        SPECIALIZE_builtin_add_overflow_WIN(unsigned long long, ULongLongAdd);
240d3a
+
240d3a
+#undef SPECIALIZE_builtin_add_overflow_WIN
240d3a
+
240d3a
+#endif
240d3a
+
240d3a
+    }  // namespace Internal
240d3a
+
240d3a
+    /*!
240d3a
+     * @brief Safe addition, throws an exception on overflow.
240d3a
+     *
240d3a
+     * This function returns the result of summand_1 and summand_2 only when the
240d3a
+     * operation would not overflow, otherwise an exception of type
240d3a
+     * std::overflow_error is thrown.
240d3a
+     *
240d3a
+     * @param[in] summand_1, summand_2  summands to be summed up
240d3a
+     * @return  the sum of summand_1 and summand_2
240d3a
+     * @throws  std::overflow_error if the addition would overflow
240d3a
+     *
240d3a
+     * This function utilizes compiler builtins when available and should have a
240d3a
+     * very small performance hit then. When builtins are unavailable, a more
240d3a
+     * extensive check is required.
240d3a
+     *
240d3a
+     * Builtins are available for the following configurations:
240d3a
+     * - GCC/Clang for signed and unsigned int, long and long long (not char & short)
240d3a
+     * - MSVC for unsigned int, long and long long
240d3a
+     */
240d3a
+    template <typename T>
240d3a
+    T add(T summand_1, T summand_2)
240d3a
+    {
240d3a
+        T res = 0;
240d3a
+        if (Internal::builtin_add_overflow<T>::add(summand_1, summand_2, res)) {
240d3a
+            throw std::overflow_error("Overflow in addition");
240d3a
+        }
240d3a
+        return res;
240d3a
+    }
240d3a
+
240d3a
+}  // namespace Safe
240d3a
+
240d3a
+#endif  // SAFE_OP_HPP_