Skip to content
Memory corruption Disclosed report Advanced

16 instances where return value of OpenSSL i2d_RSAPublicKey is discarded -- might lead to use of uninitialized memory

Several Tor functions call OpenSSL's i2d_RSAPublicKey without checking whether it succeeded, so when the call fails due to memory exhaustion the output buffers are left uninitialised. An attacker who can cause memory pressure could make Tor use uninitialised memory in digests, fingerprints and service identifiers.

guido Follow

Originally disclosed on HackerOne

  • Fixed in 11 days
  • Paid in 10 days
  • Disclosed after 18 months

The following functions rely on OpenSSL i2d_RSAPublicKey and do not heed its return value.

int crypto_pk_get_all_digests()
    tor-0.2.7.6/src/common/tortls.c:775
        if i2d_RSAPublicKey fails, cert->pkey_digests is all zeroes (cert was allocated with tor_malloc_zero)
int crypto_pk_get_digest()
    tor-0.2.7.6/src/or/connection_or.c:1686
        if i2d_RSAPublicKey fails, digest_rcvd_out is left uninitialized XXX
        (digest_rcvd_out is a stack-based buffer in connection_tls_finish_handshake and is never initialized)
    tor-0.2.7.6/src/or/hibernate.c:552
        if i2d_RSAPublicKey fails, digest is left uninitialized XXX
    tor-0.2.7.6/src/or/rendcommon.c:482
        if i2d_RSAPublicKey fails, service_id is left uninitialized XXX
    tor-0.2.7.6/src/or/rendservice.c:2318
        if i2d_RSAPublicKey fails, key_digest is left uninitialized XXX
    tor-0.2.7.6/src/or/router.c:201
        if i2d_RSAPublicKey fails, server_identitykey_digest is left uninitialized XXX
    tor-0.2.7.6/src/or/router.c:859
        if i2d_RSAPublicKey fails, v3_digest is all zeroes (it was initialized with memset) XXX
    tor-0.2.7.6/src/or/router.c:1019
        if i2d_RSAPublicKey fails, digest is left uninitialized XXX
    tor-0.2.7.6/src/or/routerkeys.c:1070
        if i2d_RSAPublicKey fails, signed_data is left uninitialized XXX
    tor-0.2.7.6/src/or/routerparse.c:5075
        if i2d_RSAPublicKey fails, public_key_hash is left uninitialized XXX
int crypto_pk_get_fingerprint()
    tor-0.2.7.6/src/or/control.c:1737
        if i2d_RSAPublicKey fails, answer is left uninitialized XXX
    tor-0.2.7.6/src/or/dirvote.c:1922
        if i2d_RSAPublicKey fails, fingerprint is left uninitialized XXX
    tor-0.2.7.6/src/or/dirvote.c:1923
        if i2d_RSAPublicKey fails, signing_key_fingerprint is left uninitialized XXX
    tor-0.2.7.6/src/or/dirvote.c:1946
        if i2d_RSAPublicKey fails, signing_key_fingerprint is left uninitialized (or retains its previous data) XXX
int rend_get_service_id()
    tor-0.2.7.6/src/or/directory.c:2138
        if i2d_RSAPublicKey fails, service_id is left uninitialized XXX
    tor-0.2.7.6/src/or/rendservice.c:3250
        if i2d_RSAPublicKey fails, service_id is left uninitialized XXX

i2d_RSA_PublicKey does a memory allocation internally and a shortage of allocatable memory will cause it to fail. Like I said in the other report, a repeatedly triggered memory leak by the attacker, or utilizing some way to allocate a large amount of memory, or a "natural" memory shortage on the system caused by another process using large amounts of memory could trigger this.

Here is a proof of concept:

C
#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>

#define tor_assert(x) {if (!(x)) { abort(); }}

struct crypto_pk_t
{
  int refs; /**< reference count, so we don't have to copy keys */
  RSA *key; /**< The key itself */
};
typedef struct crypto_pk_t crypto_pk_t;
int
crypto_digest(char *digest, const char *m, size_t len)
{
  tor_assert(m);
  tor_assert(digest);
  return (SHA1((const unsigned char*)m,len,(unsigned char*)digest) == NULL);
}
int
crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
{
  unsigned char *buf = NULL;
  int len;

  len = i2d_RSAPublicKey((RSA*)pk->key, &buf);
  if (len < 0 || buf == NULL)
    return -1;
  if (crypto_digest(digest_out, (char*)buf, len) < 0) {
    OPENSSL_free(buf);
    return -1;
  }
  OPENSSL_free(buf);
  return 0;
}

#define DIGEST_LEN 20
int main(void)
{
    char _digest[DIGEST_LEN];
    char digest[] = "uninitialized mem..";

    if ( sizeof(_digest) != sizeof(digest) )
    {
        return 0;
    }
    void* ptr = (void*)1;
    int count = 0;
    crypto_pk_t* pk = malloc(sizeof(crypto_pk_t));

    pk->key = RSA_generate_key(1024, 3, 0, 0);

    while ( ptr != 0 )
    {
        ptr = malloc(100);
        count ++;
    }

    printf("allocated 100 * %d bytes\n", count);
    printf("crypto_pk_digest returns: %d\n", crypto_pk_get_digest(pk, digest));

    printf("digest is: %s\n", digest);
    return 0;
}

Before you run this, you might want to run this command:

ulimit -Sv 500000

so that process memory is limited to 500MB (so your whole system doesn't start lagging).

It should output something like this:

allocated 100 * 4477420 bytes
crypto_pk_digest returns: -1
digest is: uninitialized mem..

In other words, digest is not touched by crypto_pk_digest() and retains it original value.

Guido

The original report as published on HackerOne, by guido. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

Likely the return value of i2d_RSAPublicKey is ignored, so a failed serialisation leaves caller-supplied buffers uninitialised or stale.

Impact

If memory is exhausted, Tor could compute digests and identifiers from uninitialised memory, which may leak stale data or cause incorrect behaviour; the report does not demonstrate a concrete exploit beyond the uninitialised buffer.

What the attacker needed

Attacker can cause memory exhaustion

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.

Some code asks a crypto library to convert a public key into bytes, then hashes those bytes. The library can fail if it cannot get memory, but the code never checks for failure, so it hashes whatever was already in the buffer. That leftover data is unpredictable and could cause wrong values to be used.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by guido

View all

49 more disclosed reports, best first.

More from Tor

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.