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