Skip to content

Commit

Permalink
Integrate util/system's CInit into RNGState
Browse files Browse the repository at this point in the history
This guarantees that OpenSSL is initialized properly whenever randomness
is used, even when that randomness is invoked from global constructors.

Note that this patch uses Mutex directly, rather than CCriticalSection.
This is because the lock-detection code is not necessarily initialized
during global constructors.
  • Loading branch information
Fuzzbawls committed Apr 14, 2021
1 parent 5f20e62 commit 038a45a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 54 deletions.
43 changes: 43 additions & 0 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/conf.h>

[[noreturn]] static void RandFailure()
{
Expand Down Expand Up @@ -294,15 +295,46 @@ void GetRandBytes(unsigned char* buf, int num)
}
}

void LockingCallbackOpenSSL(int mode, int i, const char* file, int line);

namespace {

struct RNGState {
Mutex m_mutex;
unsigned char m_state[32] GUARDED_BY(m_mutex) = {0};
uint64_t m_counter GUARDED_BY(m_mutex) = 0;
std::unique_ptr<Mutex[]> m_mutex_openssl;

RNGState()
{
InitHardwareRand();

// Init OpenSSL library multithreading support
m_mutex_openssl.reset(new Mutex[CRYPTO_num_locks()]);
CRYPTO_set_locking_callback(LockingCallbackOpenSSL);

// OpenSSL can optionally load a config file which lists optional loadable modules and engines.
// We don't use them so we don't require the config. However some of our libs may call functions
// which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
// or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
// that the config appears to have been loaded and there are no modules/engines available.
OPENSSL_no_config();

#ifdef WIN32
// Seed OpenSSL PRNG with current contents of the screen
RAND_screen();
#endif

// Seed OpenSSL PRNG with performance counter
RandAddSeed();
}

~RNGState()
{
// Securely erase the memory used by the OpenSSL PRNG
RAND_cleanup();
// Shutdown OpenSSL library multithreading support
CRYPTO_set_locking_callback(nullptr);
}

/** Extract up to 32 bytes of entropy from the RNG state, mixing in new entropy from hasher. */
Expand Down Expand Up @@ -343,6 +375,17 @@ RNGState& GetRNGState()
}
}

void LockingCallbackOpenSSL(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
{
RNGState& rng = GetRNGState();

if (mode & CRYPTO_LOCK) {
rng.m_mutex_openssl[i].lock();
} else {
rng.m_mutex_openssl[i].unlock();
}
}

static void AddDataToRng(void* data, size_t len, RNGState& rng);

void RandAddSeedSleep()
Expand Down
54 changes: 0 additions & 54 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@

#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
#include <openssl/conf.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>

const char * const PIVX_CONF_FILENAME = "pivx.conf";
const char * const PIVX_PID_FILENAME = "pivx.pid";
Expand All @@ -101,57 +98,6 @@ ArgsManager gArgs;
bool fDaemon = false;
CTranslationInterface translationInterface;

/** Init OpenSSL library multithreading support */
static RecursiveMutex** ppmutexOpenSSL;
void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
{
if (mode & CRYPTO_LOCK) {
ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
} else {
LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
}
}

// Init
class CInit
{
public:
CInit()
{
// Init OpenSSL library multithreading support
ppmutexOpenSSL = (RecursiveMutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(RecursiveMutex*));
for (int i = 0; i < CRYPTO_num_locks(); i++)
ppmutexOpenSSL[i] = new RecursiveMutex();
CRYPTO_set_locking_callback(locking_callback);

// OpenSSL can optionally load a config file which lists optional loadable modules and engines.
// We don't use them so we don't require the config. However some of our libs may call functions
// which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
// or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
// that the config appears to have been loaded and there are no modules/engines available.
OPENSSL_no_config();

#ifdef WIN32
// Seed OpenSSL PRNG with current contents of the screen
RAND_screen();
#endif

// Seed OpenSSL PRNG with performance counter
RandAddSeed();
}
~CInit()
{
// Securely erase the memory used by the PRNG
RAND_cleanup();
// Shutdown OpenSSL library multithreading support
CRYPTO_set_locking_callback(NULL);
for (int i = 0; i < CRYPTO_num_locks(); i++)
delete ppmutexOpenSSL[i];
OPENSSL_free(ppmutexOpenSSL);
}
} instance_of_cinit;


/**
* Interpret a string argument as a boolean.
*
Expand Down

0 comments on commit 038a45a

Please sign in to comment.