Skip to content

Commit 23b0c9c

Browse files
author
Roman Proskuryakov
committed
fix: replace memset with sodium_memzero for sensitive data
1 parent 7d66c70 commit 23b0c9c

File tree

6 files changed

+13
-13
lines changed

6 files changed

+13
-13
lines changed

toxcore/TCP_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data
251251

252252
memcpy(TCP_conn->recv_nonce, plain + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES);
253253
encrypt_precompute(plain, TCP_conn->temp_secret_key, TCP_conn->shared_key);
254-
memset(TCP_conn->temp_secret_key, 0, crypto_box_SECRETKEYBYTES);
254+
sodium_memzero(TCP_conn->temp_secret_key, crypto_box_SECRETKEYBYTES);
255255
return 0;
256256
}
257257

@@ -962,6 +962,6 @@ void kill_TCP_connection(TCP_Client_Connection *TCP_connection)
962962

963963
wipe_priority_list(TCP_connection);
964964
kill_sock(TCP_connection->sock);
965-
memset(TCP_connection, 0, sizeof(TCP_Client_Connection));
965+
sodium_memzero(TCP_connection, sizeof(TCP_Client_Connection));
966966
free(TCP_connection);
967967
}

toxcore/TCP_server.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static int del_accepted(TCP_Server *TCP_server, int index)
169169
if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index))
170170
return -1;
171171

172-
memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection));
172+
sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection));
173173
--TCP_server->num_accepted_connections;
174174

175175
if (TCP_server->num_accepted_connections == 0)
@@ -447,7 +447,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
447447
static void kill_TCP_connection(TCP_Secure_Connection *con)
448448
{
449449
kill_sock(con->sock);
450-
memset(con, 0, sizeof(TCP_Secure_Connection));
450+
sodium_memzero(con, sizeof(TCP_Secure_Connection));
451451
}
452452

453453
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
868868
return -1;
869869
}
870870

871-
memset(con, 0, sizeof(TCP_Secure_Connection));
871+
sodium_memzero(con, sizeof(TCP_Secure_Connection));
872872

873873
if (handle_TCP_packet(TCP_server, index, data, length) == -1) {
874874
kill_accepted(TCP_server, index);
@@ -1056,7 +1056,7 @@ static int do_incoming(TCP_Server *TCP_server, uint32_t i)
10561056
kill_TCP_connection(conn_new);
10571057

10581058
memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection));
1059-
memset(conn_old, 0, sizeof(TCP_Secure_Connection));
1059+
sodium_memzero(conn_old, sizeof(TCP_Secure_Connection));
10601060
++TCP_server->unconfirmed_connection_queue_index;
10611061

10621062
return index_new;

toxcore/friend_requests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk)
9494

9595
for (i = 0; i < MAX_RECEIVED_STORED; ++i) {
9696
if (id_equal(fr->received_requests[i], real_pk)) {
97-
memset(fr->received_requests[i], 0, crypto_box_PUBLICKEYBYTES);
97+
sodium_memzero(fr->received_requests[i], crypto_box_PUBLICKEYBYTES);
9898
return 0;
9999
}
100100
}

toxcore/group.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int wipe_group_chat(Group_Chats *g_c, int groupnumber)
106106
return -1;
107107

108108
uint32_t i;
109-
memset(&(g_c->chats[groupnumber]), 0 , sizeof(Group_c));
109+
sodium_memzero(&(g_c->chats[groupnumber]), sizeof(Group_c));
110110

111111
for (i = g_c->num_chats; i != 0; --i) {
112112
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
20112011
uint16_t top_distance = message_number - g->group[peer_index].top_lossy_number;
20122012

20132013
if (top_distance >= MAX_LOSSY_COUNT) {
2014-
memset(g->group[peer_index].recv_lossy, 0, sizeof(g->group[peer_index].recv_lossy));
2014+
sodium_memzero(g->group[peer_index].recv_lossy, sizeof(g->group[peer_index].recv_lossy));
20152015
g->group[peer_index].top_lossy_number = message_number;
20162016
g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1;
20172017
g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1;

toxcore/net_crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id)
15181518

15191519
/* Keep mutex, only destroy it when connection is realloced out. */
15201520
pthread_mutex_t mutex = c->crypto_connections[crypt_connection_id].mutex;
1521-
memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection));
1521+
sodium_memzero(&(c->crypto_connections[crypt_connection_id]), sizeof(Crypto_Connection));
15221522
c->crypto_connections[crypt_connection_id].mutex = mutex;
15231523

15241524
for (i = c->crypto_connections_length; i != 0; --i) {
@@ -2709,6 +2709,6 @@ void kill_net_crypto(Net_Crypto *c)
27092709
networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL);
27102710
networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_HS, NULL, NULL);
27112711
networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_DATA, NULL, NULL);
2712-
memset(c, 0, sizeof(Net_Crypto));
2712+
sodium_memzero(c, sizeof(Net_Crypto));
27132713
free(c);
27142714
}

toxcore/onion_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num)
10601060
//if (onion_c->friends_list[friend_num].know_dht_public_key)
10611061
// DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, 0);
10621062

1063-
memset(&(onion_c->friends_list[friend_num]), 0, sizeof(Onion_Friend));
1063+
sodium_memzero(&(onion_c->friends_list[friend_num]), sizeof(Onion_Friend));
10641064
unsigned int i;
10651065

10661066
for (i = onion_c->num_friends; i != 0; --i) {
@@ -1523,7 +1523,7 @@ void kill_onion_client(Onion_Client *onion_c)
15231523
oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, NULL, NULL);
15241524
cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, NULL, NULL);
15251525
set_onion_packet_tcp_connection_callback(onion_c->c->tcp_c, NULL, NULL);
1526-
memset(onion_c, 0, sizeof(Onion_Client));
1526+
sodium_memzero(onion_c, sizeof(Onion_Client));
15271527
free(onion_c);
15281528
}
15291529

0 commit comments

Comments
 (0)