From 23b0c9cdedae01b09055a0476823e225680d3930 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Sun, 24 Jan 2016 05:55:25 +0300 Subject: [PATCH] fix: replace memset with sodium_memzero for sensitive data --- toxcore/TCP_client.c | 4 ++-- toxcore/TCP_server.c | 8 ++++---- toxcore/friend_requests.c | 2 +- toxcore/group.c | 4 ++-- toxcore/net_crypto.c | 4 ++-- toxcore/onion_client.c | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index e2110ad8..752deecf 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -251,7 +251,7 @@ static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data memcpy(TCP_conn->recv_nonce, plain + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES); encrypt_precompute(plain, TCP_conn->temp_secret_key, TCP_conn->shared_key); - memset(TCP_conn->temp_secret_key, 0, crypto_box_SECRETKEYBYTES); + sodium_memzero(TCP_conn->temp_secret_key, crypto_box_SECRETKEYBYTES); return 0; } @@ -962,6 +962,6 @@ void kill_TCP_connection(TCP_Client_Connection *TCP_connection) wipe_priority_list(TCP_connection); kill_sock(TCP_connection->sock); - memset(TCP_connection, 0, sizeof(TCP_Client_Connection)); + sodium_memzero(TCP_connection, sizeof(TCP_Client_Connection)); free(TCP_connection); } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 79f59acd..d4944aed 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -169,7 +169,7 @@ static int del_accepted(TCP_Server *TCP_server, int index) if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index)) return -1; - memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection)); + sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection)); --TCP_server->num_accepted_connections; if (TCP_server->num_accepted_connections == 0) @@ -447,7 +447,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const static void kill_TCP_connection(TCP_Secure_Connection *con) { kill_sock(con->sock); - memset(con, 0, sizeof(TCP_Secure_Connection)); + sodium_memzero(con, sizeof(TCP_Secure_Connection)); } static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number); @@ -868,7 +868,7 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection return -1; } - memset(con, 0, sizeof(TCP_Secure_Connection)); + sodium_memzero(con, sizeof(TCP_Secure_Connection)); if (handle_TCP_packet(TCP_server, index, data, length) == -1) { kill_accepted(TCP_server, index); @@ -1056,7 +1056,7 @@ static int do_incoming(TCP_Server *TCP_server, uint32_t i) kill_TCP_connection(conn_new); memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection)); - memset(conn_old, 0, sizeof(TCP_Secure_Connection)); + sodium_memzero(conn_old, sizeof(TCP_Secure_Connection)); ++TCP_server->unconfirmed_connection_queue_index; return index_new; diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index dae1694a..7d4fecd1 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -94,7 +94,7 @@ int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) for (i = 0; i < MAX_RECEIVED_STORED; ++i) { if (id_equal(fr->received_requests[i], real_pk)) { - memset(fr->received_requests[i], 0, crypto_box_PUBLICKEYBYTES); + sodium_memzero(fr->received_requests[i], crypto_box_PUBLICKEYBYTES); return 0; } } diff --git a/toxcore/group.c b/toxcore/group.c index 1d733479..9313a24b 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -106,7 +106,7 @@ static int wipe_group_chat(Group_Chats *g_c, int groupnumber) return -1; uint32_t i; - memset(&(g_c->chats[groupnumber]), 0 , sizeof(Group_c)); + sodium_memzero(&(g_c->chats[groupnumber]), sizeof(Group_c)); for (i = g_c->num_chats; i != 0; --i) { if (g_c->chats[i - 1].status != GROUPCHAT_STATUS_NONE) @@ -2011,7 +2011,7 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 uint16_t top_distance = message_number - g->group[peer_index].top_lossy_number; if (top_distance >= MAX_LOSSY_COUNT) { - memset(g->group[peer_index].recv_lossy, 0, sizeof(g->group[peer_index].recv_lossy)); + sodium_memzero(g->group[peer_index].recv_lossy, sizeof(g->group[peer_index].recv_lossy)); g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1d6fa02e..c6729722 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1518,7 +1518,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) /* Keep mutex, only destroy it when connection is realloced out. */ pthread_mutex_t mutex = c->crypto_connections[crypt_connection_id].mutex; - memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection)); + sodium_memzero(&(c->crypto_connections[crypt_connection_id]), sizeof(Crypto_Connection)); c->crypto_connections[crypt_connection_id].mutex = mutex; for (i = c->crypto_connections_length; i != 0; --i) { @@ -2709,6 +2709,6 @@ void kill_net_crypto(Net_Crypto *c) networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL); networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_HS, NULL, NULL); networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_DATA, NULL, NULL); - memset(c, 0, sizeof(Net_Crypto)); + sodium_memzero(c, sizeof(Net_Crypto)); free(c); } diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 84e74c60..b879f393 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -1060,7 +1060,7 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num) //if (onion_c->friends_list[friend_num].know_dht_public_key) // DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, 0); - memset(&(onion_c->friends_list[friend_num]), 0, sizeof(Onion_Friend)); + sodium_memzero(&(onion_c->friends_list[friend_num]), sizeof(Onion_Friend)); unsigned int i; for (i = onion_c->num_friends; i != 0; --i) { @@ -1523,7 +1523,7 @@ void kill_onion_client(Onion_Client *onion_c) oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, NULL, NULL); cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, NULL, NULL); set_onion_packet_tcp_connection_callback(onion_c->c->tcp_c, NULL, NULL); - memset(onion_c, 0, sizeof(Onion_Client)); + sodium_memzero(onion_c, sizeof(Onion_Client)); free(onion_c); }