mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
crypto: fix weak randomness in WebCrypto keygen
Commit dae283d96f from August 2020 introduced a call to EntropySource()
in SecretKeyGenTraits::DoKeyGen() in src/crypto/crypto_keygen.cc. There
are two problems with that:
1. It does not check the return value, it assumes EntropySource() always
succeeds, but it can (and sometimes will) fail.
2. The random data returned byEntropySource() may not be
cryptographically strong and therefore not suitable as keying
material.
An example is a freshly booted system or a system without /dev/random or
getrandom(2).
EntropySource() calls out to openssl's RAND_poll() and RAND_bytes() in a
best-effort attempt to obtain random data. OpenSSL has a built-in CSPRNG
but that can fail to initialize, in which case it's possible either:
1. No random data gets written to the output buffer, i.e., the output is
unmodified, or
2. Weak random data is written. It's theoretically possible for the
output to be fully predictable because the CSPRNG starts from a
predictable state.
Replace EntropySource() and CheckEntropy() with new function CSPRNG()
that enforces checking of the return value. Abort on startup when the
entropy pool fails to initialize because that makes it too easy to
compromise the security of the process.
Refs: https://hackerone.com/bugs?report_id=1690000
Refs: https://github.com/nodejs/node/pull/35093
This commit is contained in:
1 parent
30ff0ff6c9
commit
5cc36c39d2
9 files changed
+56
-81
No files matched your search
+15
-10
@@ -913,11 +913,10 @@ std::unique_ptr<InitializationResult> InitializeOncePerProcess(
|
||||
// fipsinstall command. If the path to this file is incorrect no error
|
||||
// will be reported.
|
||||
//
|
||||
// For Node.js this will mean that EntropySource will be called by V8 as
|
||||
// part of its initialization process, and EntropySource will in turn call
|
||||
// CheckEntropy. CheckEntropy will call RAND_status which will now always
|
||||
// return 0, leading to an endless loop and the node process will appear to
|
||||
// hang/freeze.
|
||||
// For Node.js this will mean that CSPRNG() will be called by V8 as
|
||||
// part of its initialization process, and CSPRNG() will in turn call
|
||||
// call RAND_status which will now always return 0, leading to an endless
|
||||
// loop and the node process will appear to hang/freeze.
|
||||
|
||||
// Passing NULL as the config file will allow the default openssl.cnf file
|
||||
// to be loaded, but the default section in that file will not be used,
|
||||
@@ -975,11 +974,17 @@ std::unique_ptr<InitializationResult> InitializeOncePerProcess(
|
||||
return result;
|
||||
}
|
||||
|
||||
// Seed V8's PRNG from OpenSSL's pool. This is recommended by V8 and a
|
||||
// potentially better source of randomness than what V8 uses by default, but
|
||||
// it does not guarantee that pseudo-random values produced by V8 will be
|
||||
// cryptograhically secure.
|
||||
V8::SetEntropySource(crypto::EntropySource);
|
||||
// Ensure CSPRNG is properly seeded.
|
||||
CHECK(crypto::CSPRNG(nullptr, 0).is_ok());
|
||||
|
||||
V8::SetEntropySource([](unsigned char* buffer, size_t length) {
|
||||
// V8 falls back to very weak entropy when this function fails
|
||||
// and /dev/urandom isn't available. That wouldn't be so bad if
|
||||
// the entropy was only used for Math.random() but it's also used for
|
||||
// hash table and address space layout randomization. Better to abort.
|
||||
CHECK(crypto::CSPRNG(buffer, length).is_ok());
|
||||
return true;
|
||||
});
|
||||
#endif // HAVE_OPENSSL && !defined(OPENSSL_IS_BORINGSSL)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user