From 28d4d5fdb6a1229ade88e679df8133cc8d2bf255 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 26 Jul 2018 12:54:46 +0200 Subject: [PATCH] src: avoid possible race during NodeBIO initialization PR-URL: https://github.com/nodejs/node/pull/21984 Reviewed-By: Anatoli Papirovski Reviewed-By: James M Snell --- src/node_crypto.cc | 2 ++ src/node_crypto.h | 2 ++ src/node_crypto_bio.cc | 2 ++ src/node_crypto_bio.h | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index de2545f3859970..8fac840244c6f7 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5126,6 +5126,8 @@ void InitCryptoOnce() { ERR_load_ENGINE_strings(); ENGINE_load_builtin_engines(); #endif // !OPENSSL_NO_ENGINE + + NodeBIO::GetMethod(); } diff --git a/src/node_crypto.h b/src/node_crypto.h index ee069c9cf799b2..269bccbc03a1d7 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -97,6 +97,8 @@ extern int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx); extern void UseExtraCaCerts(const std::string& file); +void InitCryptoOnce(); + class SecureContext : public BaseObject { public: ~SecureContext() override { diff --git a/src/node_crypto_bio.cc b/src/node_crypto_bio.cc index ab68c0a08111f2..baa90204f2f97a 100644 --- a/src/node_crypto_bio.cc +++ b/src/node_crypto_bio.cc @@ -247,6 +247,8 @@ const BIO_METHOD* NodeBIO::GetMethod() { return &method; #else + // This is called from InitCryptoOnce() to avoid race conditions during + // initialization. static BIO_METHOD* method = nullptr; if (method == nullptr) { diff --git a/src/node_crypto_bio.h b/src/node_crypto_bio.h index fefd097b32a7ba..0c61f19d0189d2 100644 --- a/src/node_crypto_bio.h +++ b/src/node_crypto_bio.h @@ -164,6 +164,8 @@ class NodeBIO : public MemoryRetainer { int eof_return_ = -1; Buffer* read_head_ = nullptr; Buffer* write_head_ = nullptr; + + friend void node::crypto::InitCryptoOnce(); }; } // namespace crypto