Weak randomness in WebCrypto keygen
The key generation code for WebCrypto secret keys called the entropy source without checking whether it succeeded, and the returned bytes were not guaranteed to be cryptographically strong. An attacker could potentially predict generated keys, breaking confidentiality and integrity.
Originally disclosed on HackerOne
- Triaged in under an hour
- Fixed in 24 days
- Disclosed after 54 days
https://github.com/nodejs/node/pull/35093 introduced a call to EntropySource() in SecretKeyGenTraits::DoKeyGen() in src/crypto/crypto_keygen.cc. There are two problems with this:
-
It does not check the return value, it assumes
EntropySource()always succeeds, but it can (and sometimes will) fail. -
The random data returned by
EntropySource()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).
Impact
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:
-
No random data gets written to the output buffer, i.e., the output is unmodified, or
-
Weak random data is written. It's theoretically possible for the output to be fully predictable because the CSPRNG starts from a predictable state.
The output buffer is allocated in SecretKeyGenTraits::DoKeyGen() using OPENSSL_malloc() (alias for CRYPTO_malloc()), which in turn calls malloc().
malloc() does not zero the buffer but its contents may be predicted or manipulated by an external attacker, e.g. by manipulating an arraybuffer, then forcing the GC to reclaim it.
Users can override the CSPRNG (and do) so there are probably more failure modes. A buggy CSPRNG could write out only zeroes, for example, comparable to (2).
I have a (trivial!) patch available. H1 gives this a really high CVSS score but I suppose that's appropriate when the worst case failure mode is a complete breakdown of confidentiality and integrity.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code ignored the return value of the entropy source and treated best-effort random data as suitable keying material, with the output buffer left uninitialised on failure.
Impact
On systems where the CSPRNG fails to initialise, generated keys could in theory be fully predictable, breaking confidentiality and integrity.
What the attacker needed
System with failing or weak CSPRNG; attacker can influence heap contents
The fix
The author had a trivial patch available, though the text does not describe its contents.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When software creates a secret key, it needs unpredictable random numbers. This code asked the operating system for randomness but never checked whether that request worked, and the randomness could be weak on some systems. If the numbers are predictable, someone could guess the key and read or alter protected data.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.