diff options
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/circuitlist.c | 7 | ||||
-rw-r--r-- | src/or/connection.c | 9 | ||||
-rw-r--r-- | src/or/onion.c | 54 | ||||
-rw-r--r-- | src/or/relay.c | 9 |
4 files changed, 49 insertions, 30 deletions
diff --git a/src/or/circuitlist.c b/src/or/circuitlist.c index 67770ffd5..13dd97fb7 100644 --- a/src/or/circuitlist.c +++ b/src/or/circuitlist.c @@ -378,10 +378,12 @@ static void circuit_free(circuit_t *circ) { void *mem; + size_t memlen; tor_assert(circ); if (CIRCUIT_IS_ORIGIN(circ)) { origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ); mem = ocirc; + memlen = sizeof(origin_circuit_t); tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC); if (ocirc->build_state) { if (ocirc->build_state->chosen_exit) @@ -398,6 +400,7 @@ circuit_free(circuit_t *circ) } else { or_circuit_t *ocirc = TO_OR_CIRCUIT(circ); mem = ocirc; + memlen = sizeof(or_circuit_t); tor_assert(circ->magic == OR_CIRCUIT_MAGIC); if (ocirc->p_crypto) @@ -432,7 +435,7 @@ circuit_free(circuit_t *circ) * "active" checks will be violated. */ cell_queue_clear(&circ->n_conn_cells); - memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */ + memset(circ, 0xAA, memlen); /* poison memory */ tor_free(mem); } @@ -499,7 +502,7 @@ circuit_free_cpath_node(crypt_path_t *victim) if (victim->extend_info) extend_info_free(victim->extend_info); - victim->magic = 0xDEADBEEFu; + memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */ tor_free(victim); } diff --git a/src/or/connection.c b/src/or/connection.c index bb42ecf93..2dc0a8ae8 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -266,27 +266,33 @@ static void _connection_free(connection_t *conn) { void *mem; + size_t memlen; switch (conn->type) { case CONN_TYPE_OR: tor_assert(conn->magic == OR_CONNECTION_MAGIC); mem = TO_OR_CONN(conn); + memlen = sizeof(or_connection_t); break; case CONN_TYPE_AP: case CONN_TYPE_EXIT: tor_assert(conn->magic == EDGE_CONNECTION_MAGIC); mem = TO_EDGE_CONN(conn); + memlen = sizeof(edge_connection_t); break; case CONN_TYPE_DIR: tor_assert(conn->magic == DIR_CONNECTION_MAGIC); mem = TO_DIR_CONN(conn); + memlen = sizeof(dir_connection_t); break; case CONN_TYPE_CONTROL: tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC); mem = TO_CONTROL_CONN(conn); + memlen = sizeof(control_connection_t); break; default: tor_assert(conn->magic == BASE_CONNECTION_MAGIC); mem = conn; + memlen = sizeof(connection_t); break; } @@ -331,6 +337,7 @@ _connection_free(connection_t *conn) if (CONN_IS_EDGE(conn)) { edge_connection_t *edge_conn = TO_EDGE_CONN(conn); tor_free(edge_conn->chosen_exit_name); + memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t)); tor_free(edge_conn->socks_request); } if (conn->type == CONN_TYPE_CONTROL) { @@ -365,7 +372,7 @@ _connection_free(connection_t *conn) connection_or_remove_from_identity_map(TO_OR_CONN(conn)); } - memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */ + memset(conn, 0xAA, memlen); /* poison memory */ tor_free(mem); } diff --git a/src/or/onion.c b/src/or/onion.c index 281d011b9..abbadcbf0 100644 --- a/src/or/onion.c +++ b/src/or/onion.c @@ -166,7 +166,7 @@ onion_skin_create(crypto_pk_env_t *dest_router_key, crypto_dh_env_t **handshake_state_out, char *onion_skin_out) /* ONIONSKIN_CHALLENGE_LEN bytes */ { - char *challenge = NULL; + char challenge[DH_KEY_LEN]; crypto_dh_env_t *dh = NULL; int dhbytes, pkbytes; @@ -183,7 +183,6 @@ onion_skin_create(crypto_pk_env_t *dest_router_key, pkbytes = crypto_pk_keysize(dest_router_key); tor_assert(dhbytes == 128); tor_assert(pkbytes == 128); - challenge = tor_malloc_zero(DH_KEY_LEN); if (crypto_dh_get_public(dh, challenge, dhbytes)) goto err; @@ -211,12 +210,12 @@ onion_skin_create(crypto_pk_env_t *dest_router_key, PK_PKCS1_OAEP_PADDING, 1)<0) goto err; - tor_free(challenge); + memset(challenge, 0, sizeof(challenge)); *handshake_state_out = dh; return 0; err: - tor_free(challenge); + memset(challenge, 0, sizeof(challenge)); if (dh) crypto_dh_free(dh); return -1; } @@ -238,6 +237,7 @@ onion_skin_server_handshake(const char *onion_skin, /*ONIONSKIN_CHALLENGE_LEN*/ crypto_dh_env_t *dh = NULL; int len; char *key_material=NULL; + size_t key_material_len=0; int i; crypto_pk_env_t *k; @@ -277,9 +277,10 @@ onion_skin_server_handshake(const char *onion_skin, /*ONIONSKIN_CHALLENGE_LEN*/ puts(""); #endif - key_material = tor_malloc(DIGEST_LEN+key_out_len); + key_material_len = DIGEST_LEN+key_out_len; + key_material = tor_malloc(key_material_len); len = crypto_dh_compute_secret(dh, challenge, DH_KEY_LEN, - key_material, DIGEST_LEN+key_out_len); + key_material, key_material_len); if (len < 0) { log_info(LD_GENERAL, "crypto_dh_compute_secret failed."); goto err; @@ -300,11 +301,17 @@ onion_skin_server_handshake(const char *onion_skin, /*ONIONSKIN_CHALLENGE_LEN*/ puts(""); #endif + memset(challenge, 0, sizeof(challenge)); + memset(key_material, 0, key_material_len); tor_free(key_material); crypto_dh_free(dh); return 0; err: - tor_free(key_material); + memset(challenge, 0, sizeof(challenge)); + if (key_material) { + memset(key_material, 0, key_material_len); + tor_free(key_material); + } if (dh) crypto_dh_free(dh); return -1; @@ -327,6 +334,7 @@ onion_skin_client_handshake(crypto_dh_env_t *handshake_state, { int len; char *key_material=NULL; + size_t key_material_len; tor_assert(crypto_dh_get_bytes(handshake_state) == DH_KEY_LEN); #ifdef DEBUG_ONION_SKINS @@ -337,13 +345,14 @@ onion_skin_client_handshake(crypto_dh_env_t *handshake_state, puts(""); #endif - key_material = tor_malloc(20+key_out_len); + key_material_len = DIGEST_LEN + key_out_len; + key_material = tor_malloc(key_material_len); len = crypto_dh_compute_secret(handshake_state, handshake_reply, DH_KEY_LEN, - key_material, 20+key_out_len); + key_material, key_material_len); if (len < 0) goto err; - if (memcmp(key_material, handshake_reply+DH_KEY_LEN, 20)) { + if (memcmp(key_material, handshake_reply+DH_KEY_LEN, DIGEST_LEN)) { /* H(K) does *not* match. Something fishy. */ log_warn(LD_PROTOCOL,"Digest DOES NOT MATCH on onion handshake. " "Bug or attack."); @@ -351,7 +360,7 @@ onion_skin_client_handshake(crypto_dh_env_t *handshake_state, } /* use the rest of the key material for our shared keys, digests, etc */ - memcpy(key_out, key_material+20, key_out_len); + memcpy(key_out, key_material+DIGEST_LEN, key_out_len); #ifdef DEBUG_ONION_SKINS printf("Client: keys out:"); @@ -359,9 +368,11 @@ onion_skin_client_handshake(crypto_dh_env_t *handshake_state, puts(""); #endif + memset(key_material, 0, key_material_len); tor_free(key_material); return 0; err: + memset(key_material, 0, key_material_len); tor_free(key_material); return -1; } @@ -380,8 +391,9 @@ fast_server_handshake(const char *key_in, /* DIGEST_LEN bytes */ size_t key_out_len) { char tmp[DIGEST_LEN+DIGEST_LEN]; - char *out; + char *out = NULL; size_t out_len; + int r = -1; if (crypto_rand(handshake_reply_out, DIGEST_LEN)<0) return -1; @@ -391,15 +403,16 @@ fast_server_handshake(const char *key_in, /* DIGEST_LEN bytes */ out_len = key_out_len+DIGEST_LEN; out = tor_malloc(out_len); if (crypto_expand_key_material(tmp, sizeof(tmp), out, out_len)) { - tor_free(out); - return -1; + goto done; } memcpy(handshake_reply_out+DIGEST_LEN, out, DIGEST_LEN); memcpy(key_out, out+DIGEST_LEN, key_out_len); + r = 0; + done: memset(tmp, 0, sizeof(tmp)); memset(out, 0, out_len); tor_free(out); - return 0; + return r; } /** Implement the second half of the client side of the CREATE_FAST handshake. @@ -423,27 +436,28 @@ fast_client_handshake(const char *handshake_state, /* DIGEST_LEN bytes */ char tmp[DIGEST_LEN+DIGEST_LEN]; char *out; size_t out_len; + int r; memcpy(tmp, handshake_state, DIGEST_LEN); memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN); out_len = key_out_len+DIGEST_LEN; out = tor_malloc(out_len); if (crypto_expand_key_material(tmp, sizeof(tmp), out, out_len)) { - tor_free(out); - return -1; + goto done; } if (memcmp(out, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) { /* H(K) does *not* match. Something fishy. */ log_warn(LD_PROTOCOL,"Digest DOES NOT MATCH on fast handshake. " "Bug or attack."); - tor_free(out); - return -1; + goto done; } memcpy(key_out, out+DIGEST_LEN, key_out_len); + r = 0; + done: memset(tmp, 0, sizeof(tmp)); memset(out, 0, out_len); tor_free(out); - return 0; + return r; } /** Remove all circuits from the pending list. Called from tor_free_all. */ diff --git a/src/or/relay.c b/src/or/relay.c index 31f264b63..576d6e2cf 100644 --- a/src/or/relay.c +++ b/src/or/relay.c @@ -117,19 +117,14 @@ static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in, int encrypt_mode) { - char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */ int r; - - if (encrypt_mode) - r = crypto_cipher_encrypt(cipher, out, in, CELL_PAYLOAD_SIZE); - else - r = crypto_cipher_decrypt(cipher, out, in, CELL_PAYLOAD_SIZE); + (void)encrypt_mode; + r = crypto_cipher_crypt_inplace(cipher, in, CELL_PAYLOAD_SIZE); if (r) { log_warn(LD_BUG,"Error during relay encryption"); return -1; } - memcpy(in,out,CELL_PAYLOAD_SIZE); return 0; } |