Skip to content

Commit

Permalink
cleanup: Check that onion IP/Port packing worked.
Browse files Browse the repository at this point in the history
If it doesn't work for some reason, right now it would cause trouble
(e.g. buffer overrun).
  • Loading branch information
iphydf committed Jan 15, 2025
1 parent e092ecd commit 5cca245
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions toxcore/onion_announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ static int handle_announce_request_common(
uint8_t ping_id_data[CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT];
memcpy(ping_id_data, packet_public_key, CRYPTO_PUBLIC_KEY_SIZE);
const int packed_len = pack_ip_port(onion_a->log, &ping_id_data[CRYPTO_PUBLIC_KEY_SIZE], SIZE_IPPORT, source);
if (packed_len < 0) {
LOGGER_ERROR(onion_a->log, "failed to pack IP/Port");
mem_delete(onion_a->mem, plain);
return 1;
}
assert(packed_len <= SIZE_IPPORT);
memzero(&ping_id_data[CRYPTO_PUBLIC_KEY_SIZE + packed_len], SIZE_IPPORT - packed_len);
const uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE;
Expand Down
5 changes: 5 additions & 0 deletions toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *publ
memcpy(data, &num, sizeof(uint32_t));
memcpy(&data[sizeof(uint32_t)], public_key, CRYPTO_PUBLIC_KEY_SIZE);
const int packed_len = pack_ip_port(onion_c->logger, &data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE], SIZE_IPPORT, ip_port);
if (packed_len < 0) {
LOGGER_ERROR(onion_c->logger, "failed to pack IP/port");
return -1;
}
assert(packed_len <= SIZE_IPPORT);
memzero(&data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + packed_len], SIZE_IPPORT - packed_len);
memcpy(&data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT], &path_num, sizeof(uint32_t));
*sendback = ping_array_add(onion_c->announce_ping_array, onion_c->mono_time, onion_c->rng, data, sizeof(data));
Expand Down

0 comments on commit 5cca245

Please sign in to comment.