From 79ef301adf18a7a4d22bd6f70c38cac4c39e83ab Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 5 Jan 2025 09:54:42 +0000 Subject: [PATCH] cleanup: Reduce stack frame sizes to below 4096 bytes. This is currently only detectable in tests, because the main code uses VLAs where stack frame size is unknown at compile time. --- auto_tests/TCP_test.c | 17 +++++++++++++---- auto_tests/save_compatibility_test.c | 25 ++++++++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index b59fa9469b..15c73ccc81 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -165,7 +165,9 @@ static void test_basic(void) } // Receiving the second response and verifying its validity - uint8_t packet_resp[4096]; + const size_t max_packet_size = 4096; + uint8_t *packet_resp = (uint8_t *)malloc(max_packet_size); + ck_assert(packet_resp != nullptr); int recv_data_len = net_recv(ns, logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, &localhost); ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Failed to receive server response to request. %d", recv_data_len); @@ -173,7 +175,8 @@ static void test_basic(void) ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size for request response."); - uint8_t packet_resp_plain[4096]; + uint8_t *packet_resp_plain = (uint8_t *)malloc(max_packet_size); + ck_assert(packet_resp_plain != nullptr); ret = decrypt_data_symmetric(mem, f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response."); increment_nonce(f_nonce_r); @@ -183,6 +186,9 @@ static void test_basic(void) ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection."); ck_assert_msg(pk_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key."); + free(packet_resp_plain); + free(packet_resp); + // Closing connections. kill_sock(ns, sock); kill_tcp_server(tcp_s); @@ -337,7 +343,8 @@ static void test_some(void) do_tcp_server_delay(tcp_s, mono_time, 50); // Testing response from connection 1 - uint8_t data[2048]; + const size_t max_packet_size = 4096; + uint8_t *data = (uint8_t *)malloc(max_packet_size); int len = read_packet_sec_tcp(logger, con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); @@ -351,7 +358,7 @@ static void test_some(void) ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); ck_assert_msg(pk_equal(data + 2, con1->public_key), "Key in response packet wrong."); - uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? + const uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet)); write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet)); @@ -406,6 +413,8 @@ static void test_some(void) ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]); ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); + free(data); + // Kill off the connections kill_tcp_server(tcp_s); kill_tcp_con(con1); diff --git a/auto_tests/save_compatibility_test.c b/auto_tests/save_compatibility_test.c index bd7157ca20..6267f669b1 100644 --- a/auto_tests/save_compatibility_test.c +++ b/auto_tests/save_compatibility_test.c @@ -145,7 +145,10 @@ static bool is_little_endian(void) // cppcheck-suppress constParameter int main(int argc, char *argv[]) { - char base_path[4096] = {0}; + const size_t base_path_size = 4096; + char *base_path = (char *)malloc(base_path_size); + ck_assert(base_path != nullptr); + memset(base_path, 0, 4096); if (argc <= 1) { const char *srcdir = getenv("srcdir"); @@ -154,21 +157,29 @@ int main(int argc, char *argv[]) srcdir = "."; } - snprintf(base_path, sizeof(base_path), "%s", srcdir); + snprintf(base_path, base_path_size, "%s", srcdir); } else { - snprintf(base_path, sizeof(base_path), "%s", argv[1]); + snprintf(base_path, base_path_size, "%s", argv[1]); base_path[strrchr(base_path, '/') - base_path] = '\0'; } if (is_little_endian()) { - char save_path[4096 + sizeof(LOADED_SAVE_FILE_LITTLE)]; - snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE); + const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_LITTLE); + char *save_path = (char *)malloc(save_path_size); + ck_assert(save_path != nullptr); + snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE); test_save_compatibility(save_path); + free(save_path); } else { - char save_path[4096 + sizeof(LOADED_SAVE_FILE_BIG)]; - snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_BIG); + const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_BIG); + char *save_path = (char *)malloc(save_path_size); + ck_assert(save_path != nullptr); + snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_BIG); test_save_compatibility(save_path); + free(save_path); } + free(base_path); + return 0; }