From: Lucian Petrut Date: Tue, 13 Oct 2020 10:26:10 +0000 (+0000) Subject: common,librbd: add portable ceph_memzero_s X-Git-Tag: v16.1.0~652^2~11 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6e1ec123c98a6aba464f9a7ddad1728a61344726;p=ceph.git common,librbd: add portable ceph_memzero_s We need a portable way of safely zero-ing out a buffer. We're adding ceph_memzero_s, which will use one of the following functions, dependning which one is available: memset_s, SecureZeroMemory and explicit_bzero. Signed-off-by: Lucian Petrut --- diff --git a/src/common/compat.cc b/src/common/compat.cc index f78822ef156..aacfcc524a4 100644 --- a/src/common/compat.cc +++ b/src/common/compat.cc @@ -232,6 +232,17 @@ char *ceph_strerror_r(int errnum, char *buf, size_t buflen) #endif } +int ceph_memzero_s(void *dest, size_t destsz, size_t count) { +#ifdef __STDC_LIB_EXT1__ + return memset_s(dest, destsz, 0, count); +#elif defined(_WIN32) + SecureZeroMemory(dest, count); +#else + explicit_bzero(dest, count); +#endif + return 0; +} + #ifdef _WIN32 #include diff --git a/src/include/compat.h b/src/include/compat.h index 748fafee983..a73e7fcac38 100644 --- a/src/include/compat.h +++ b/src/include/compat.h @@ -203,6 +203,8 @@ int pipe_cloexec(int pipefd[2], int flags); char *ceph_strerror_r(int errnum, char *buf, size_t buflen); unsigned get_page_size(); +int ceph_memzero_s(void *dest, size_t destsz, size_t count); + #ifdef __cplusplus } #endif diff --git a/src/librbd/crypto/openssl/DataCryptor.cc b/src/librbd/crypto/openssl/DataCryptor.cc index 4394b17855b..0ed1d84ed3b 100644 --- a/src/librbd/crypto/openssl/DataCryptor.cc +++ b/src/librbd/crypto/openssl/DataCryptor.cc @@ -5,6 +5,7 @@ #include #include #include "include/ceph_assert.h" +#include "include/compat.h" namespace librbd { namespace crypto { @@ -46,7 +47,8 @@ int DataCryptor::init(const char* cipher_name, const unsigned char* key, DataCryptor::~DataCryptor() { if (m_key != nullptr) { - explicit_bzero(m_key, EVP_CIPHER_key_length(m_cipher)); + ceph_memzero_s(m_key, EVP_CIPHER_key_length(m_cipher), + EVP_CIPHER_key_length(m_cipher)); delete m_key; m_key = nullptr; }