← All stories

Debian thought removing an OpenSSL line Valgrind flagged was harmless. It made generated keys guessable.

Debian silenced a Valgrind warning and made every OpenSSL key guessable for 20 months

Removing one line that fed uninitialised memory into OpenSSL's random pool looked like harmless cleanup. An identical-looking line was the one that fed in the real entropy, and without it the generator was seeded by little more than the process ID.

Debian· May 13, 2008
Impact
Key material generated by OpenSSL on Debian systems from version 0.9.8c-1 (uploaded 2006-09-17) was predictable, including SSH, OpenVPN, DNSSEC and X.509 keys; all DSA keys used on affected systems had to be treated as compromised.
Stack
openssl · openssh · debian

On 13 May 2008, the Debian Security Team published advisory DSA-1571. Its first paragraph was blunt: Luciano Bello had discovered that the random number generator in Debian's openssl package is predictable, caused by "an incorrect Debian-specific change to the openssl package." The consequence, in the advisory's words: "cryptographic key material may be guessable."

This was not a bug in OpenSSL itself. It affected Debian and, as the advisory notes, systems based on Debian. Other systems could still be affected indirectly if weak keys generated on Debian were imported into them. And it had been shipping for a long time: the first vulnerable package version, 0.9.8c-1, was uploaded to Debian unstable on 2006-09-17 and had propagated into testing and the stable release, etch.

The affected keys were the ones that hold infrastructure together: SSH keys, OpenVPN keys, DNSSEC keys, and key material for X.509 certificates, plus session keys for SSL/TLS connections. The advisory told administrators to regenerate all such material from scratch.

What happened

The change that caused this was made with good intentions. The Debian community's wiki page written to support users through the cleanup, SSLkeys, explains that it was an attempt to remove Valgrind warnings about the use of uninitialised memory inside the OpenSSL libraries. Valgrind is a memory checker that C developers run over their programs, and noisy warnings from a library make it harder to find real bugs in your own code. Making OpenSSL quiet under Valgrind was, as the wiki says, "a good thing to do."

The wiki says two identical lines, MD_Update(&m, buf, j);, were commented out of OpenSSL's md_rand.c in 2006. It also says the change had been discussed with the OpenSSL team and that no objections were raised at the time, and it points to Debian bug #363516 and the openssl-dev mailing list for that discussion. The wiki's own judgement is that the main culprit seems to be a misunderstanding about which list to ask on, followed by misleading answers.

The packages then shipped for roughly twenty months, from the September 2006 upload to the May 2008 advisory. The bug produced no errors and no crashes. Keys were generated, certificates were signed, SSH logins worked. Nothing visible was wrong.

What the engineers believed

The belief was that those lines were only feeding uninitialised memory into the random pool, and that removing them would not affect security. The wiki describes it as an "overzealous, well-intentioned elimination of code that was believed to have no impact on security."

For one of the two lines, that belief was correct. That's what made it so easy to apply to both.

What was actually going on

How OpenSSL's generator worked

A cryptographic random number generator of this kind keeps an internal state called the entropy pool. Two operations matter:

  • Add. Unpredictable input, such as bytes from /dev/urandom and other sources, is mixed into the pool by running it through a hash function. In OpenSSL's code this is ssleay_rand_add, and the mixing call is MD_Update.
  • Extract. When a program asks for random bytes, the generator hashes the pool state to produce output and updates the pool. In OpenSSL this is ssleay_rand_bytes.

The output can only be as unpredictable as what went into the pool. A perfect hash function fed predictable input produces predictable output that happens to look random.

Two identical lines, two opposite jobs

The wiki's technical section explains the difference between the two removed calls (source).

The second call was in ssleay_rand_bytes, the extract path. There, buf is the output buffer, the memory the caller passed in to receive random bytes. OpenSSL hashed whatever was already in that buffer into the pool before overwriting it. That memory is often uninitialised, which is exactly what Valgrind flags. The wiki says this use was already marked as a bad idea when OpenSSL was built with -DPURIFY, and that removing it should not meaningfully alter the entropy in the pool: OpenSSL does not depend on uninitialised memory for correct operation. Removing this call was fine.

The first call was in ssleay_rand_add, the add path. There, buf is the input buffer: the entropy that a caller is deliberately handing to the generator. Without MD_Update there, the wiki says, "the pool will never actually get the entropy intended for it."

what the removed line was doingentropy bytesMD_Update into poolunguessable keysentropy bytescall removedseeded by PID onlythe line that looked like noise was the only way real randomness got in

The two calls are textually identical. Valgrind complained about one. Grep finds both. Whether removing a line is safe depends on what buf means at that call site, and nothing in the line itself tells you.

How weak "predictable" was

With real entropy no longer reaching the pool, what was left to vary between runs? According to the wiki, the broken version was seeded only by process ID. The output also depended on architecture, because of differences in endianness and sizeof(long), giving three families: little-endian 32-bit, little-endian 64-bit and big-endian 32-bit.

Process IDs on Linux then ran up to 32768, with PID 0 belonging to the kernel, so the wiki counts 32,767 possible random streams per architecture, or 98,301 in total across the three. An RSA or DSA key generated by ssh-keygen on an affected machine was therefore one of a short list of keys that anyone could compute in advance. The wiki notes that the Metasploit project had generated lists of these weak keys.

Why DSA keys were compromised even when they were generated elsewhere

The advisory goes further than "regenerate keys made on Debian." It says all DSA keys ever used on affected Debian systems for signing or authentication should be considered compromised, because DSA "relies on a secret random value used during signature generation."

This is standard DSA mathematics. Each DSA signature uses a fresh secret number, the nonce. The signature equation ties together the message hash, the private key and that nonce. If an attacker knows the nonce for a single signature, they can solve the equation for the private key. A DSA key generated on a perfectly healthy machine and then used once to log in from an affected Debian machine produced a signature with a guessable nonce, and that one signature was enough. The wiki makes the same point: simply using a strong DSA key from an affected machine may have compromised it.

The wiki also lists what was not affected: keys made by GnuPG, GNUTLS, and dm-crypt/LUKS, which do not generate keys with OpenSSL (source). The old stable release, sarge, was not affected either.

How they fixed it

The code fix was simple. Fixed packages were released as 0.9.8c-4etch3 for etch and 0.9.8g-9 for lenny and sid. The same update also fixed two other OpenSSL vulnerabilities that had been scheduled for a later point release.

The cleanup was not simple, because fixing the library does nothing for keys that already exist. Every key generated during those twenty months stayed exactly as weak as before. So the advisory came with tooling and instructions:

  • A detector for known weak key material, dowkd.pl, published by Debian security.
  • Blacklist packages listing the known weak keys, including openssh-blacklist, openssl-blacklist and openvpn-blacklist, shipped alongside updated OpenSSH packages that include the blacklist of known weak keys.
  • A key-rollover page with per-package instructions, which the advisory said would be continuously updated, and the SSLkeys wiki page, which walked through regenerating keys for many packages, from Apache certificates to Tor hidden-service keys.

The wiki's acknowledgements add a detail about the response timeline: its authors wished they had had a chance to prepare during the 5 days between the commit to pkg-openssl and the DSA being released.

What changed afterwards

The sources here are an advisory and a support page, not a retrospective, so they don't describe process changes inside Debian. What they do show is the shape of the cost. A one-line change took minutes. Undoing it required every administrator of every affected machine to find, revoke and reissue keys, and to treat any DSA key that had ever touched an affected machine as burned.

The rule

Warnings from tools like Valgrind are about the code, not about what the code is for. Before you remove a line from cryptographic or security-critical code to silence a tool, establish what that line contributes, at that call site, with that meaning of buf. Two identical lines can do opposite jobs, and in this case the line that looked like noise was the only way real randomness got into the generator.

Sources