From 239e5dfb7e80b50cab860a915da98593509a8137 Mon Sep 17 00:00:00 2001 From: miodragpop Date: Tue, 3 Nov 2020 20:01:06 +0100 Subject: [PATCH 1/4] Replace OPENSSL_cleanse --- src/support/cleanse.cpp | 28 +++++++++++++++++++++++++--- src/support/cleanse.h | 2 ++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/support/cleanse.cpp b/src/support/cleanse.cpp index db39ac9c3..7c58c94fe 100644 --- a/src/support/cleanse.cpp +++ b/src/support/cleanse.cpp @@ -3,11 +3,33 @@ // Distributed under the GPLv3 software license, see the accompanying // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html -#include "cleanse.h" +#include -#include +#include + +#if defined(_MSC_VER) +#include // For SecureZeroMemory. +#endif void memory_cleanse(void *ptr, size_t len) { - OPENSSL_cleanse(ptr, len); +#if defined(_MSC_VER) + /* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */ + SecureZeroMemory(ptr, len); +#else + std::memset(ptr, 0, len); + + /* Memory barrier that scares the compiler away from optimizing out the memset. + * + * Quoting Adam Langley in commit ad1907fe73334d6c696c8539646c21b11178f20f + * in BoringSSL (ISC License): + * As best as we can tell, this is sufficient to break any optimisations that + * might try to eliminate "superfluous" memsets. + * This method is used in memzero_explicit() the Linux kernel, too. Its advantage is that it + * is pretty efficient because the compiler can still implement the memset() efficiently, + * just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by + * Yang et al. (USENIX Security 2017) for more background. + */ + __asm__ __volatile__("" : : "r"(ptr) : "memory"); +#endif } diff --git a/src/support/cleanse.h b/src/support/cleanse.h index f7bfda283..de5682d6a 100644 --- a/src/support/cleanse.h +++ b/src/support/cleanse.h @@ -8,6 +8,8 @@ #include +/** Secure overwrite a buffer (possibly containing secret data) with zero-bytes. The write + * operation will not be optimized out by the compiler. */ void memory_cleanse(void *ptr, size_t len); #endif // BITCOIN_SUPPORT_CLEANSE_H From a226334d664b8504589c8084e39c529d1da7f41a Mon Sep 17 00:00:00 2001 From: miodragpop Date: Tue, 3 Nov 2020 21:26:25 +0100 Subject: [PATCH 2/4] more hidden OpenSSL stuff replaced --- src/wallet/crypter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp index 558f1621e..2635dff29 100644 --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -28,8 +28,8 @@ #include #include #include -#include -#include +#include +#include using namespace libzcash; From 7e3c7ff502af22caff029ddf82b524db2c586a2d Mon Sep 17 00:00:00 2001 From: miodragpop Date: Tue, 3 Nov 2020 22:19:17 +0100 Subject: [PATCH 3/4] more OpenSSL leftovers fixed (thanks to buggy 'make clean' for missing this) --- configure.ac | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index aab16b9be..3c0529926 100644 --- a/configure.ac +++ b/configure.ac @@ -665,7 +665,7 @@ if test x$use_pkgconfig = xyes; then [PKG_CHECK_MODULES], [ PKG_CHECK_MODULES([SSL], [wolfssl],, [AC_MSG_ERROR(WolfSSL not found.)]) - #PKG_CHECK_MODULES([CRYPTO], [libcrypto],,[AC_MSG_ERROR(libcrypto not found.)]) + PKG_CHECK_MODULES([CRYPTO], [wolfssl],,[AC_MSG_ERROR(libcrypto not found.)]) if test x$build_bitcoin_utils$build_bitcoind$bitcoin_enable_qt$use_tests != xnononono; then PKG_CHECK_MODULES([EVENT], [libevent],, [AC_MSG_ERROR(libevent not found.)]) if test x$TARGET_OS != xwindows; then @@ -689,7 +689,7 @@ else echo 'BUG: configure does not yet check for the following dependencies if pkg-config is not on the system: libcrypto++, gmp' AC_CHECK_HEADER([wolfssl/openssl/crypto.h],,AC_MSG_ERROR(libcrypto headers missing)) - AC_CHECK_LIB([crypto], [main],CRYPTO_LIBS=-lcrypto, AC_MSG_ERROR(libcrypto missing)) + AC_CHECK_LIB([crypto], [main],CRYPTO_LIBS=-lwolfssl, AC_MSG_ERROR(libcrypto missing)) AC_CHECK_HEADER([wolfssl/ssl.h],, AC_MSG_ERROR(libssl headers missing),) AC_CHECK_LIB([wolfssl], [main],SSL_LIBS=-lwolfssl, AC_MSG_ERROR(libwolfssl missing)) From f8461ed1ae6f600fe30e51908c3c71d61a43763e Mon Sep 17 00:00:00 2001 From: miodragpop Date: Tue, 3 Nov 2020 23:25:45 +0100 Subject: [PATCH 4/4] -lcrypto => -lwolfssl --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 3c0529926..1eb494dde 100644 --- a/configure.ac +++ b/configure.ac @@ -764,7 +764,7 @@ AX_CHECK_COMPILE_FLAG([-fwrapv],[CXXFLAGS="$CXXFLAGS -fwrapv"]) AX_CHECK_COMPILE_FLAG([-fno-strict-aliasing],[CXXFLAGS="$CXXFLAGS -fno-strict-aliasing"]) AX_CHECK_COMPILE_FLAG([-Wno-builtin-declaration-mismatch],[CXXFLAGS="$CXXFLAGS -Wno-builtin-declaration-mismatch"],,[[$CXXFLAG_WERROR]]) -LIBZCASH_LIBS="-lgmp -lgmpxx $BOOST_SYSTEM_LIB -lcrypto -lsodium $RUST_LIBS" +LIBZCASH_LIBS="-lgmp -lgmpxx $BOOST_SYSTEM_LIB -lwolfssl -lsodium $RUST_LIBS" AC_MSG_CHECKING([whether to build komodod]) AM_CONDITIONAL([BUILD_BITCOIND], [test x$build_bitcoind = xyes])