cleanup: Remove all uses of SIZEOF_VLA.

This is step 1 towards removing VLAs altogether.
This commit is contained in:
iphydf 2024-01-24 11:40:56 +00:00
parent 662c2140f3
commit 5c093c4888
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
20 changed files with 135 additions and 107 deletions

View File

@ -266,13 +266,14 @@ static void kill_tcp_con(struct sec_TCP_con *con)
static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP_con *con, const uint8_t *data,
uint16_t length)
{
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
const uint16_t packet_size = sizeof(uint16_t) + length + CRYPTO_MAC_SIZE;
VLA(uint8_t, packet, packet_size);
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
memcpy(packet, &c_length, sizeof(uint16_t));
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
return -1;
}
@ -282,7 +283,7 @@ static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP
localhost.ip = get_loopback();
localhost.port = 0;
ck_assert_msg(net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &localhost) == SIZEOF_VLA(packet),
ck_assert_msg(net_send(con->ns, logger, con->sock, packet, packet_size, &localhost) == packet_size,
"Failed to send a packet.");
return 0;
}

View File

@ -22,13 +22,12 @@ struct test_data {
static void set_random(Tox *m, const Random *rng, bool (*setter)(Tox *, const uint8_t *, size_t, Tox_Err_Set_Info *), size_t length)
{
VLA(uint8_t, text, length);
uint32_t i;
for (i = 0; i < length; ++i) {
for (uint32_t i = 0; i < length; ++i) {
text[i] = random_u08(rng);
}
setter(m, text, SIZEOF_VLA(text), nullptr);
setter(m, text, length, nullptr);
}
static void alloc_string(uint8_t **to, size_t length)

View File

@ -1 +1 @@
c028527fe5eecde7daa6ac9dacc47bd4bb765463e8e4b5af3881789797bf81a8 /usr/local/bin/tox-bootstrapd
8a2cc2e20ac0688b42c6e6211cb1bc5797c276ed52a2322d8b786f8ad562d535 /usr/local/bin/tox-bootstrapd

View File

@ -126,11 +126,6 @@ SUPPRESSIONS += 19.2
#
# Reason: We believe it should be used when #define is used in block scope.
SUPPRESSIONS += 20.5
# The # and ## preprocessor operators should not be used.
#
# TODO(iphydf): Remove suppression when VLAs are gone. This is only used in
# the SIZEOF_VLA macro.
SUPPRESSIONS += 20.10
# #define and #undef shall not be used on a reserved identifier or reserved macro name.
#
# Reason: Needed for feature test macros like _DEFAULT_SOURCE.

View File

@ -806,8 +806,9 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
header.flags |= RTP_KEY_FRAME;
}
VLA(uint8_t, rdata, length + RTP_HEADER_SIZE + 1);
memset(rdata, 0, SIZEOF_VLA(rdata));
const uint16_t rdata_size = length + RTP_HEADER_SIZE + 1;
VLA(uint8_t, rdata, rdata_size);
memset(rdata, 0, rdata_size);
rdata[0] = session->payload_type; // packet id == payload_type
if (MAX_CRYPTO_DATA_SIZE > (length + RTP_HEADER_SIZE + 1)) {
@ -818,10 +819,10 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
rtp_header_pack(rdata + 1, &header);
memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length);
if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, SIZEOF_VLA(rdata))) {
if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, rdata_size)) {
char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %u)! net error: %s",
(unsigned)SIZEOF_VLA(rdata), netstrerror);
rdata_size, netstrerror);
net_kill_strerror(netstrerror);
}
} else {

View File

@ -850,12 +850,14 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc
goto RETURN;
}
VLA(uint8_t, dest, sample_count + sizeof(sampling_rate)); /* This is more than enough always */
/* This is more than enough always */
const uint16_t dest_size = sample_count + sizeof(sampling_rate);
VLA(uint8_t, dest, dest_size);
sampling_rate = net_htonl(sampling_rate);
memcpy(dest, &sampling_rate, sizeof(sampling_rate));
const int vrc = opus_encode(call->audio->encoder, pcm, sample_count,
dest + sizeof(sampling_rate), SIZEOF_VLA(dest) - sizeof(sampling_rate));
dest + sizeof(sampling_rate), dest_size - sizeof(sampling_rate));
if (vrc < 0) {
LOGGER_WARNING(av->m->log, "Failed to encode frame %s", opus_strerror(vrc));

View File

@ -1487,13 +1487,14 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
memcpy(plain + 1 + nodes_length, sendback_data, length);
const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
VLA(uint8_t, data, 1 + nodes_length + length + crypto_size);
const uint32_t data_size = 1 + nodes_length + length + crypto_size;
VLA(uint8_t, data, data_size);
const int len = dht_create_packet(dht->mem, dht->rng,
dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
plain, 1 + nodes_length + length, data, SIZEOF_VLA(data));
plain, 1 + nodes_length + length, data, data_size);
if (len != SIZEOF_VLA(data)) {
if (len != data_size) {
return -1;
}
@ -1576,7 +1577,8 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
return false;
}
VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t));
const uint32_t plain_size = 1 + data_size + sizeof(uint64_t);
VLA(uint8_t, plain, plain_size);
const uint8_t *shared_key = dht_get_shared_key_sent(dht, packet + 1);
const int len = decrypt_data_symmetric(
shared_key,
@ -1585,7 +1587,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE,
plain);
if ((unsigned int)len != SIZEOF_VLA(plain)) {
if ((uint32_t)len != plain_size) {
return false;
}

View File

@ -1223,7 +1223,8 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
return false;
}
VLA(uint8_t, packet, 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length);
const uint16_t packet_size = 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length;
VLA(uint8_t, packet, packet_size);
packet[0] = filenumber;
file_type = net_htonl(file_type);
memcpy(packet + 1, &file_type, sizeof(file_type));
@ -1234,7 +1235,7 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
}
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, SIZEOF_VLA(packet), false);
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, packet_size, false);
}
/** @brief Send a file send request.
@ -1301,7 +1302,8 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
return false;
}
VLA(uint8_t, packet, 3 + data_length);
const uint16_t packet_size = 3 + data_length;
VLA(uint8_t, packet, packet_size);
packet[0] = inbound ? 1 : 0;
packet[1] = filenumber;
@ -1311,7 +1313,7 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
memcpy(packet + 3, data, data_length);
}
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, SIZEOF_VLA(packet), false);
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, packet_size, false);
}
/** @brief Send a file control request.
@ -1501,7 +1503,8 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
return -1;
}
VLA(uint8_t, packet, 2 + length);
const uint16_t packet_size = 2 + length;
VLA(uint8_t, packet, packet_size);
packet[0] = PACKET_ID_FILE_DATA;
packet[1] = filenumber;
@ -1510,7 +1513,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
}
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), packet, SIZEOF_VLA(packet), true);
m->friendlist[friendnumber].friendcon_id), packet, packet_size, true);
}
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)

View File

@ -394,10 +394,11 @@ int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id,
return 0;
}
VLA(uint8_t, packet, 1 + length);
const uint16_t packet_size = 1 + length;
VLA(uint8_t, packet, packet_size);
packet[0] = con_id + NUM_RESERVED_PORTS;
memcpy(packet + 1, data, length);
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
}
/**
@ -412,11 +413,12 @@ int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint
return -1;
}
VLA(uint8_t, packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
const uint16_t packet_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + length;
VLA(uint8_t, packet, packet_size);
packet[0] = TCP_PACKET_OOB_SEND;
memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
}
@ -536,10 +538,11 @@ int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, ui
*/
int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length)
{
VLA(uint8_t, packet, 1 + length);
const uint16_t packet_size = 1 + length;
VLA(uint8_t, packet, packet_size);
packet[0] = TCP_PACKET_ONION_REQUEST;
memcpy(packet + 1, data, length);
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
}
void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object)

View File

@ -151,18 +151,19 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
}
}
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
const uint16_t packet_size = sizeof(uint16_t) + length + CRYPTO_MAC_SIZE;
VLA(uint8_t, packet, packet_size);
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
memcpy(packet, &c_length, sizeof(uint16_t));
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
return -1;
}
if (priority) {
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port) : 0;
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port) : 0;
if (len <= 0) {
len = 0;
@ -170,14 +171,14 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
increment_nonce(con->sent_nonce);
if ((unsigned int)len == SIZEOF_VLA(packet)) {
if ((unsigned int)len == packet_size) {
return 1;
}
return add_priority(con, packet, SIZEOF_VLA(packet), len) ? 1 : 0;
return add_priority(con, packet, packet_size, len) ? 1 : 0;
}
len = net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port);
len = net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port);
if (len <= 0) {
return 0;
@ -185,12 +186,12 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
increment_nonce(con->sent_nonce);
if ((unsigned int)len == SIZEOF_VLA(packet)) {
if ((unsigned int)len == packet_size) {
return 1;
}
memcpy(con->last_packet, packet, SIZEOF_VLA(packet));
con->last_packet_length = SIZEOF_VLA(packet);
memcpy(con->last_packet, packet, packet_size);
con->last_packet_length = packet_size;
con->last_packet_sent = len;
return 1;
}

View File

@ -532,12 +532,13 @@ static int handle_tcp_oob_send(TCP_Server *tcp_server, uint32_t con_id, const ui
const int other_index = get_tcp_connection_index(tcp_server, public_key);
if (other_index != -1) {
VLA(uint8_t, resp_packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
const uint16_t resp_packet_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + length;
VLA(uint8_t, resp_packet, resp_packet_size);
resp_packet[0] = TCP_PACKET_OOB_RECV;
memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
write_packet_tcp_secure_connection(tcp_server->logger, &tcp_server->accepted_connection_array[other_index].con,
resp_packet, SIZEOF_VLA(resp_packet), false);
resp_packet, resp_packet_size, false);
}
return 0;
@ -620,11 +621,12 @@ static int handle_onion_recv_1(void *object, const IP_Port *dest, const uint8_t
TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[index];
VLA(uint8_t, packet, 1 + length);
const uint16_t packet_size = 1 + length;
VLA(uint8_t, packet, packet_size);
memcpy(packet + 1, data, length);
packet[0] = TCP_PACKET_ONION_RESPONSE;
if (write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), false) != 1) {
if (write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, packet_size, false) != 1) {
return 1;
}
@ -660,11 +662,12 @@ static bool handle_forward_reply_tcp(void *object, const uint8_t *sendback_data,
return false;
}
VLA(uint8_t, packet, 1 + length);
const uint16_t packet_size = 1 + length;
VLA(uint8_t, packet, packet_size);
memcpy(packet + 1, data, length);
packet[0] = TCP_PACKET_FORWARDING;
return write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), false) == 1;
return write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, packet_size, false) == 1;
}
/**

View File

@ -26,7 +26,6 @@
#if !defined(DISABLE_VLA) && !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
// C99 VLAs.
#define ALLOC_VLA(type, name, size) type name[size]
#define SIZEOF_VLA sizeof
#else
// Emulation using alloca.
@ -47,9 +46,7 @@
#endif
#define ALLOC_VLA(type, name, size) \
const size_t name##_vla_size = (size) * sizeof(type); \
type *const name = (type *)alloca(name##_vla_size)
#define SIZEOF_VLA(name) name##_vla_size
type *const name = (type *)alloca((size) * sizeof(type))
#endif

View File

@ -886,18 +886,19 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
return -1;
}
VLA(uint8_t, packet, 1 + sizeof(nospam_num) + length);
const uint16_t packet_size = 1 + sizeof(nospam_num) + length;
VLA(uint8_t, packet, packet_size);
memcpy(packet + 1, &nospam_num, sizeof(nospam_num));
memcpy(packet + 1 + sizeof(nospam_num), data, length);
if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) {
packet[0] = PACKET_ID_FRIEND_REQUESTS;
return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, SIZEOF_VLA(packet),
return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, packet_size,
false) != -1 ? 1 : 0;
}
packet[0] = CRYPTO_PACKET_FRIEND_REQ;
const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet));
const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, packet_size);
if (num <= 0) {
return -1;

View File

@ -148,10 +148,10 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co
addto_receivedlist(fr, source_pubkey);
const uint32_t message_len = length - sizeof(fr->nospam);
const uint16_t message_len = length - sizeof(fr->nospam);
VLA(uint8_t, message, message_len + 1);
memcpy(message, data + sizeof(fr->nospam), message_len);
message[SIZEOF_VLA(message) - 1] = 0; /* Be sure the message is null terminated. */
message[message_len] = 0; /* Be sure the message is null terminated. TODO(iphydf): But why? */
fr->handle_friendrequest(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata);
return 0;

View File

@ -1622,12 +1622,13 @@ static bool send_packet_group_peer(const Friend_Connections *fr_c, int friendcon
}
group_num = net_htons(group_num);
VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length);
const uint32_t packet_size = 1 + sizeof(uint16_t) + length;
VLA(uint8_t, packet, packet_size);
packet[0] = packet_id;
memcpy(packet + 1, &group_num, sizeof(uint16_t));
memcpy(packet + 1 + sizeof(uint16_t), data, length);
return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id), packet,
SIZEOF_VLA(packet), false) != -1;
return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id),
packet, packet_size, false) != -1;
}
/** @brief Send a group lossy packet to friendcon_id.
@ -1644,12 +1645,13 @@ static bool send_lossy_group_peer(const Friend_Connections *fr_c, int friendcon_
}
group_num = net_htons(group_num);
VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length);
const uint32_t packet_size = 1 + sizeof(uint16_t) + length;
VLA(uint8_t, packet, packet_size);
packet[0] = packet_id;
memcpy(packet + 1, &group_num, sizeof(uint16_t));
memcpy(packet + 1 + sizeof(uint16_t), data, length);
return send_lossy_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id),
packet, SIZEOF_VLA(packet)) != -1;
packet, packet_size) != -1;
}
/** @brief invite friendnumber to groupnumber.
@ -1782,7 +1784,8 @@ static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t fri
const bool member = g->status == GROUPCHAT_STATUS_CONNECTED;
VLA(uint8_t, response, member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE);
const uint32_t response_size = member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE;
VLA(uint8_t, response, response_size);
response[0] = member ? INVITE_MEMBER_ID : INVITE_ACCEPT_ID;
net_pack_u16(response + 1, groupnumber);
memcpy(response + 1 + sizeof(uint16_t), data, length);
@ -1791,7 +1794,7 @@ static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t fri
net_pack_u16(response + 1 + sizeof(uint16_t) + length, g->peer_number);
}
if (!send_conference_invite_packet(g_c->m, friendnumber, response, SIZEOF_VLA(response))) {
if (!send_conference_invite_packet(g_c->m, friendnumber, response, response_size)) {
return false;
}
@ -2457,11 +2460,12 @@ static unsigned int send_peers(const Group_Chats *g_c, const Group_c *g, int fri
}
if (g->title_len > 0) {
VLA(uint8_t, title_packet, 1 + g->title_len);
const uint32_t title_packet_size = 1 + g->title_len;
VLA(uint8_t, title_packet, title_packet_size);
title_packet[0] = PEER_TITLE_ID;
memcpy(title_packet + 1, g->title, g->title_len);
send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, title_packet,
SIZEOF_VLA(title_packet));
send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num,
title_packet, title_packet_size);
}
return sent;
@ -2696,7 +2700,8 @@ static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint
return -3;
}
VLA(uint8_t, packet, sizeof(uint16_t) + sizeof(uint32_t) + 1 + len);
const uint16_t packet_size = sizeof(uint16_t) + sizeof(uint32_t) + 1 + len;
VLA(uint8_t, packet, packet_size);
const uint16_t peer_num = net_htons(g->peer_number);
memcpy(packet, &peer_num, sizeof(peer_num));
@ -2715,7 +2720,7 @@ static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint
memcpy(packet + sizeof(uint16_t) + sizeof(uint32_t) + 1, data, len);
}
const unsigned int ret = send_message_all_connections(g_c, g, packet, SIZEOF_VLA(packet), -1);
const unsigned int ret = send_message_all_connections(g_c, g, packet, packet_size, -1);
if (ret == 0) {
return -4;
@ -2768,14 +2773,15 @@ int send_group_lossy_packet(const Group_Chats *g_c, uint32_t groupnumber, const
return -1;
}
VLA(uint8_t, packet, sizeof(uint16_t) * 2 + length);
const uint16_t packet_size = sizeof(uint16_t) * 2 + length;
VLA(uint8_t, packet, packet_size);
const uint16_t peer_number = net_htons(g->peer_number);
memcpy(packet, &peer_number, sizeof(uint16_t));
const uint16_t message_num = net_htons(g->lossy_message_number);
memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t));
memcpy(packet + sizeof(uint16_t) * 2, data, length);
if (send_lossy_all_connections(g_c, g, packet, SIZEOF_VLA(packet), -1) == 0) {
if (send_lossy_all_connections(g_c, g, packet, packet_size, -1) == 0) {
return -1;
}

View File

@ -1116,7 +1116,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_
increment_nonce(conn->sent_nonce);
pthread_mutex_unlock(conn->mutex);
return send_packet_to(c, crypt_connection_id, packet, SIZEOF_VLA(packet));
return send_packet_to(c, crypt_connection_id, packet, packet_size);
}
/** @brief Creates and sends a data packet with buffer_start and num to the peer using the fastest route.
@ -1136,13 +1136,14 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
num = net_htonl(num);
buffer_start = net_htonl(buffer_start);
const uint16_t padding_length = (MAX_CRYPTO_DATA_SIZE - length) % CRYPTO_MAX_PADDING;
VLA(uint8_t, packet, sizeof(uint32_t) + sizeof(uint32_t) + padding_length + length);
const uint16_t packet_size = sizeof(uint32_t) + sizeof(uint32_t) + padding_length + length;
VLA(uint8_t, packet, packet_size);
memcpy(packet, &buffer_start, sizeof(uint32_t));
memcpy(packet + sizeof(uint32_t), &num, sizeof(uint32_t));
memzero(packet + (sizeof(uint32_t) * 2), padding_length);
memcpy(packet + (sizeof(uint32_t) * 2) + padding_length, data, length);
return send_data_packet(c, crypt_connection_id, packet, SIZEOF_VLA(packet));
return send_data_packet(c, crypt_connection_id, packet, packet_size);
}
non_null()

View File

@ -189,7 +189,8 @@ int create_onion_packet(const Random *rng, uint8_t *packet, uint16_t max_packet_
return -1;
}
VLA(uint8_t, step1, SIZE_IPPORT + length);
const uint16_t step1_size = SIZE_IPPORT + length;
VLA(uint8_t, step1, step1_size);
ipport_pack(step1, dest);
memcpy(step1 + SIZE_IPPORT, data, length);
@ -197,21 +198,23 @@ int create_onion_packet(const Random *rng, uint8_t *packet, uint16_t max_packet_
uint8_t nonce[CRYPTO_NONCE_SIZE];
random_nonce(rng, nonce);
VLA(uint8_t, step2, SIZE_IPPORT + SEND_BASE + length);
const uint16_t step2_size = SIZE_IPPORT + SEND_BASE + length;
VLA(uint8_t, step2, step2_size);
ipport_pack(step2, &path->ip_port3);
memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE);
int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, SIZEOF_VLA(step1),
int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, step1_size,
step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) {
return -1;
}
VLA(uint8_t, step3, SIZE_IPPORT + SEND_BASE * 2 + length);
const uint16_t step3_size = SIZE_IPPORT + SEND_BASE * 2 + length;
VLA(uint8_t, step3, step3_size);
ipport_pack(step3, &path->ip_port2);
memcpy(step3 + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE);
len = encrypt_data_symmetric(path->shared_key2, nonce, step2, SIZEOF_VLA(step2),
len = encrypt_data_symmetric(path->shared_key2, nonce, step2, step2_size,
step3 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) {
@ -222,7 +225,7 @@ int create_onion_packet(const Random *rng, uint8_t *packet, uint16_t max_packet_
memcpy(packet + 1, nonce, CRYPTO_NONCE_SIZE);
memcpy(packet + 1 + CRYPTO_NONCE_SIZE, path->public_key1, CRYPTO_PUBLIC_KEY_SIZE);
len = encrypt_data_symmetric(path->shared_key1, nonce, step3, SIZEOF_VLA(step3),
len = encrypt_data_symmetric(path->shared_key1, nonce, step3, step3_size,
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE);
if (len != SIZE_IPPORT + SEND_BASE * 2 + length + CRYPTO_MAC_SIZE) {
@ -249,7 +252,8 @@ int create_onion_packet_tcp(const Random *rng, uint8_t *packet, uint16_t max_pac
return -1;
}
VLA(uint8_t, step1, SIZE_IPPORT + length);
const uint16_t step1_size = SIZE_IPPORT + length;
VLA(uint8_t, step1, step1_size);
ipport_pack(step1, dest);
memcpy(step1 + SIZE_IPPORT, data, length);
@ -257,11 +261,12 @@ int create_onion_packet_tcp(const Random *rng, uint8_t *packet, uint16_t max_pac
uint8_t nonce[CRYPTO_NONCE_SIZE];
random_nonce(rng, nonce);
VLA(uint8_t, step2, SIZE_IPPORT + SEND_BASE + length);
const uint16_t step2_size = SIZE_IPPORT + SEND_BASE + length;
VLA(uint8_t, step2, step2_size);
ipport_pack(step2, &path->ip_port3);
memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE);
int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, SIZEOF_VLA(step1),
int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, step1_size,
step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) {
@ -270,7 +275,7 @@ int create_onion_packet_tcp(const Random *rng, uint8_t *packet, uint16_t max_pac
ipport_pack(packet + CRYPTO_NONCE_SIZE, &path->ip_port2);
memcpy(packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE);
len = encrypt_data_symmetric(path->shared_key2, nonce, step2, SIZEOF_VLA(step2),
len = encrypt_data_symmetric(path->shared_key2, nonce, step2, step2_size,
packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) {

View File

@ -637,11 +637,12 @@ static int handle_data_request(void *object, const IP_Port *source, const uint8_
return 1;
}
VLA(uint8_t, data, length - (CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3));
const uint16_t data_size = length - (CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3);
VLA(uint8_t, data, data_size);
data[0] = NET_PACKET_ONION_DATA_RESPONSE;
memcpy(data + 1, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3));
if (send_onion_response(onion_a->log, onion_a->net, &onion_a->entries[index].ret_ip_port, data, SIZEOF_VLA(data),
if (send_onion_response(onion_a->log, onion_a->net, &onion_a->entries[index].ret_ip_port, data, data_size,
onion_a->entries[index].ret) == -1) {
return 1;
}

View File

@ -1066,7 +1066,8 @@ static int handle_announce_response_old(void *object, const IP_Port *source, con
return 1;
}
VLA(uint8_t, plain, 1 + ONION_PING_ID_SIZE + len_nodes);
const uint16_t plain_size = 1 + ONION_PING_ID_SIZE + len_nodes;
VLA(uint8_t, plain, plain_size);
int len;
const uint16_t nonce_start = 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH;
const uint16_t ciphertext_start = nonce_start + CRYPTO_NONCE_SIZE;
@ -1090,8 +1091,8 @@ static int handle_announce_response_old(void *object, const IP_Port *source, con
return 1;
}
if ((uint32_t)len != SIZEOF_VLA(plain)) {
LOGGER_WARNING(onion_c->logger, "decrypted size (%lu) is not the expected plain text size (%lu)", (unsigned long)len, (unsigned long)SIZEOF_VLA(plain));
if ((uint32_t)len != plain_size) {
LOGGER_WARNING(onion_c->logger, "decrypted size (%lu) is not the expected plain text size (%u)", (unsigned long)len, plain_size);
return 1;
}
@ -1141,21 +1142,23 @@ static int handle_data_response(void *object, const IP_Port *source, const uint8
return 1;
}
VLA(uint8_t, temp_plain, length - ONION_DATA_RESPONSE_MIN_SIZE);
const uint16_t temp_plain_size = length - ONION_DATA_RESPONSE_MIN_SIZE;
VLA(uint8_t, temp_plain, temp_plain_size);
int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, onion_c->temp_secret_key, packet + 1,
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE,
length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE), temp_plain);
if ((uint32_t)len != SIZEOF_VLA(temp_plain)) {
if ((uint32_t)len != temp_plain_size) {
return 1;
}
VLA(uint8_t, plain, SIZEOF_VLA(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE);
const uint16_t plain_size = temp_plain_size - DATA_IN_RESPONSE_MIN_SIZE;
VLA(uint8_t, plain, plain_size);
len = decrypt_data(temp_plain, nc_get_self_secret_key(onion_c->c),
packet + 1, temp_plain + CRYPTO_PUBLIC_KEY_SIZE,
SIZEOF_VLA(temp_plain) - CRYPTO_PUBLIC_KEY_SIZE, plain);
temp_plain_size - CRYPTO_PUBLIC_KEY_SIZE, plain);
if ((uint32_t)len != SIZEOF_VLA(plain)) {
if ((uint32_t)len != plain_size) {
return 1;
}
@ -1164,7 +1167,7 @@ static int handle_data_response(void *object, const IP_Port *source, const uint8
}
return onion_c->onion_data_handlers[plain[0]].function(onion_c->onion_data_handlers[plain[0]].object, temp_plain, plain,
SIZEOF_VLA(plain), userdata);
plain_size, userdata);
}
#define DHTPK_DATA_MIN_LENGTH (1 + sizeof(uint64_t) + CRYPTO_PUBLIC_KEY_SIZE)
@ -1307,13 +1310,14 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
uint8_t nonce[CRYPTO_NONCE_SIZE];
random_nonce(onion_c->rng, nonce);
VLA(uint8_t, packet, DATA_IN_RESPONSE_MIN_SIZE + length);
const uint16_t packet_size = DATA_IN_RESPONSE_MIN_SIZE + length;
VLA(uint8_t, packet, packet_size);
memcpy(packet, nc_get_self_public_key(onion_c->c), CRYPTO_PUBLIC_KEY_SIZE);
int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key,
nc_get_self_secret_key(onion_c->c), nonce, data,
length, packet + CRYPTO_PUBLIC_KEY_SIZE);
if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE != SIZEOF_VLA(packet)) {
if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE != packet_size) {
return -1;
}
@ -1329,7 +1333,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
uint8_t o_packet[ONION_MAX_PACKET_SIZE];
len = create_data_request(
onion_c->rng, o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_public_key,
node_list[good_nodes[i]].data_public_key, nonce, packet, SIZEOF_VLA(packet));
node_list[good_nodes[i]].data_public_key, nonce, packet, packet_size);
if (len == -1) {
continue;
@ -1364,21 +1368,22 @@ static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uin
uint8_t nonce[CRYPTO_NONCE_SIZE];
random_nonce(onion_c->rng, nonce);
VLA(uint8_t, temp, DATA_IN_RESPONSE_MIN_SIZE + CRYPTO_NONCE_SIZE + length);
const uint16_t temp_size = DATA_IN_RESPONSE_MIN_SIZE + CRYPTO_NONCE_SIZE + length;
VLA(uint8_t, temp, temp_size);
memcpy(temp, nc_get_self_public_key(onion_c->c), CRYPTO_PUBLIC_KEY_SIZE);
memcpy(temp + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE);
int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key,
nc_get_self_secret_key(onion_c->c), nonce, data,
length, temp + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE != SIZEOF_VLA(temp)) {
if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE != temp_size) {
return -1;
}
uint8_t packet_data[MAX_CRYPTO_REQUEST_SIZE];
len = create_request(
onion_c->rng, dht_get_self_public_key(onion_c->dht), dht_get_self_secret_key(onion_c->dht), packet_data,
onion_c->friends_list[friend_num].dht_public_key, temp, SIZEOF_VLA(temp), CRYPTO_PACKET_DHTPK);
onion_c->friends_list[friend_num].dht_public_key, temp, temp_size, CRYPTO_PACKET_DHTPK);
assert(len <= UINT16_MAX);
const Packet packet = {packet_data, (uint16_t)len};

View File

@ -24,20 +24,22 @@ static void create_timed_auth_to_hash(const Mono_Time *mono_time, uint16_t timeo
void generate_timed_auth(const Mono_Time *mono_time, uint16_t timeout, const uint8_t *key,
const uint8_t *data, uint16_t length, uint8_t *timed_auth)
{
VLA(uint8_t, to_hash, sizeof(uint64_t) + length);
const uint16_t to_hash_size = sizeof(uint64_t) + length;
VLA(uint8_t, to_hash, to_hash_size);
create_timed_auth_to_hash(mono_time, timeout, false, data, length, to_hash);
crypto_hmac(timed_auth, key, to_hash, SIZEOF_VLA(to_hash));
crypto_hmac(timed_auth, key, to_hash, to_hash_size);
}
bool check_timed_auth(const Mono_Time *mono_time, uint16_t timeout, const uint8_t *key, const uint8_t *data,
uint16_t length, const uint8_t *timed_auth)
{
VLA(uint8_t, to_hash, sizeof(uint64_t) + length);
const uint16_t to_hash_size = sizeof(uint64_t) + length;
VLA(uint8_t, to_hash, to_hash_size);
for (uint8_t i = 0; i < 2; ++i) {
create_timed_auth_to_hash(mono_time, timeout, i != 0, data, length, to_hash);
if (crypto_hmac_verify(timed_auth, key, to_hash, SIZEOF_VLA(to_hash))) {
if (crypto_hmac_verify(timed_auth, key, to_hash, to_hash_size)) {
return true;
}
}