diff options
author | Caolán McNamara <caolanm@redhat.com> | 2017-05-15 11:17:57 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2017-05-16 13:42:18 +0200 |
commit | d881d77429371c3a04b758b151b091267c13d167 (patch) | |
tree | 4b0ecee5de2e62f80e498a29de24ee708e25377b | |
parent | 9304956306025194a671a90d4559746660a27010 (diff) |
ofz#1605 check multiply and shift
Change-Id: I6aad9ad23e7bf080b3b610223f92df7074530beb
Reviewed-on: https://gerrit.libreoffice.org/37642
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
-rw-r--r-- | include/o3tl/safeint.hxx | 89 | ||||
-rw-r--r-- | vcl/headless/svpbmp.cxx | 18 | ||||
-rw-r--r-- | vcl/source/gdi/salmisc.cxx | 19 | ||||
-rw-r--r-- | vcl/unx/generic/gdi/salbmp.cxx | 17 |
4 files changed, 139 insertions, 4 deletions
diff --git a/include/o3tl/safeint.hxx b/include/o3tl/safeint.hxx new file mode 100644 index 000000000000..ce144d22d9ea --- /dev/null +++ b/include/o3tl/safeint.hxx @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_SAFEINT_HXX +#define INCLUDED_O3TL_SAFEINT_HXX + +#include <limits> +#if defined(_MSC_VER) +#include <safeint.h> +#else +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif +#endif + +namespace o3tl +{ + +#if defined(_MSC_VER) + +template<typename T> inline bool checked_multiply(T a, T b, T& result) +{ + return !msl::utilities::SafeMultiply(a, b, result); +} + +#elif (defined __GNUC__ && __GNUC__ >= 5) || (__has_builtin(__builtin_mul_overflow)) + +template<typename T> inline bool checked_multiply(T a, T b, T& result) +{ + return __builtin_mul_overflow(a, b, &result); +} + +#else + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template<typename T> inline typename std::enable_if<std::is_signed<T>::value, bool>::type checked_multiply(T a, T b, T& result) +{ + if (a > 0) { /* a is positive */ + if (b > 0) { /* a and b are positive */ + if (a > (std::numeric_limits<T>::max() / b)) { + return true; /* Handle error */ + } + } else { /* a positive, b nonpositive */ + if (b < (std::numeric_limits<T>::min() / a)) { + return true; /* Handle error */ + } + } /* a positive, b nonpositive */ + } else { /* a is nonpositive */ + if (b > 0) { /* a is nonpositive, b is positive */ + if (a < (std::numeric_limits<T>::min() / b)) { + return true; /* Handle error */ + } + } else { /* a and b are nonpositive */ + if ( (a != 0) && (b < (std::numeric_limits<T>::max() / a))) { + return true; /* Handle error */ + } + } /* End if a and b are nonpositive */ + } /* End if a is nonpositive */ + + result = a * b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template<typename T> inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type checked_multiply(T a, T b, T& result) +{ + if (b && a > std::numeric_limits<T>::max() / b) { + return true;/* Handle error */ + } + + result = a * b; + + return false; +} + +#endif + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/headless/svpbmp.cxx b/vcl/headless/svpbmp.cxx index 202d63c0a84e..71c5e52586c6 100644 --- a/vcl/headless/svpbmp.cxx +++ b/vcl/headless/svpbmp.cxx @@ -27,7 +27,7 @@ #include <basegfx/vector/b2ivector.hxx> #include <basegfx/range/b2ibox.hxx> - +#include <o3tl/safeint.hxx> #include <vcl/salbtype.hxx> #include <vcl/bitmap.hxx> @@ -110,7 +110,21 @@ BitmapBuffer* ImplCreateDIB( pDIB->mnFormat |= ScanlineFormat::TopDown; pDIB->mnWidth = rSize.Width(); pDIB->mnHeight = rSize.Height(); - pDIB->mnScanlineSize = AlignedWidth4Bytes( pDIB->mnWidth * nBitCount ); + long nScanlineBase; + bool bFail = o3tl::checked_multiply<long>(pDIB->mnWidth, nBitCount, nScanlineBase); + if (bFail) + { + SAL_WARN("vcl.gdi", "checked multiply failed"); + delete pDIB; + return nullptr; + } + pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase); + if (pDIB->mnScanlineSize < nScanlineBase/8) + { + SAL_WARN("vcl.gdi", "scanline calculation wraparound"); + delete pDIB; + return nullptr; + } pDIB->mnBitCount = nBitCount; if( nColors ) diff --git a/vcl/source/gdi/salmisc.cxx b/vcl/source/gdi/salmisc.cxx index 84c7becceea4..0218c320e92b 100644 --- a/vcl/source/gdi/salmisc.cxx +++ b/vcl/source/gdi/salmisc.cxx @@ -20,6 +20,7 @@ #include <vcl/bitmapaccess.hxx> #include <vcl/salbtype.hxx> #include <bmpfast.hxx> +#include <o3tl/safeint.hxx> #include <osl/diagnose.h> #include <memory> @@ -330,7 +331,23 @@ BitmapBuffer* StretchAndConvert( pDstBuffer->mnFormat = nDstBitmapFormat; pDstBuffer->mnWidth = rTwoRect.mnDestWidth; pDstBuffer->mnHeight = rTwoRect.mnDestHeight; - pDstBuffer->mnScanlineSize = AlignedWidth4Bytes( pDstBuffer->mnBitCount * pDstBuffer->mnWidth ); + long nScanlineBase; + bool bFail = o3tl::checked_multiply<long>(pDstBuffer->mnBitCount, pDstBuffer->mnWidth, nScanlineBase); + if (bFail) + { + SAL_WARN("vcl.gdi", "checked multiply failed"); + pDstBuffer->mpBits = nullptr; + delete pDstBuffer; + return nullptr; + } + pDstBuffer->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase); + if (pDstBuffer->mnScanlineSize < nScanlineBase/8) + { + SAL_WARN("vcl.gdi", "scanline calculation wraparound"); + pDstBuffer->mpBits = nullptr; + delete pDstBuffer; + return nullptr; + } try { pDstBuffer->mpBits = new sal_uInt8[ pDstBuffer->mnScanlineSize * pDstBuffer->mnHeight ]; diff --git a/vcl/unx/generic/gdi/salbmp.cxx b/vcl/unx/generic/gdi/salbmp.cxx index 158a2f52d0fc..905232319123 100644 --- a/vcl/unx/generic/gdi/salbmp.cxx +++ b/vcl/unx/generic/gdi/salbmp.cxx @@ -41,6 +41,7 @@ #include <unx/salinst.h> #include <unx/x11/xlimits.hxx> +#include <o3tl/safeint.hxx> #include <opengl/salbmp.hxx> #include <vcl/opengl/OpenGLHelper.hxx> @@ -193,7 +194,21 @@ BitmapBuffer* X11SalBitmap::ImplCreateDIB( pDIB->mnWidth = rSize.Width(); pDIB->mnHeight = rSize.Height(); - pDIB->mnScanlineSize = AlignedWidth4Bytes( pDIB->mnWidth * nBitCount ); + long nScanlineBase; + bool bFail = o3tl::checked_multiply<long>(pDIB->mnWidth, nBitCount, nScanlineBase); + if (bFail) + { + SAL_WARN("vcl.gdi", "checked multiply failed"); + delete pDIB; + return nullptr; + } + pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase); + if (pDIB->mnScanlineSize < nScanlineBase/8) + { + SAL_WARN("vcl.gdi", "scanline calculation wraparound"); + delete pDIB; + return nullptr; + } pDIB->mnBitCount = nBitCount; if( nColors ) |