From 9a36d449ef7c92961215b57b3a36fd3159233903 Mon Sep 17 00:00:00 2001 From: iphydf Date: Fri, 9 Feb 2024 00:38:05 +0000 Subject: [PATCH] fix: Correct a few potential null derefs in bootstrap daemon. Found by PVS Studio. --- other/bootstrap_daemon/src/config.c | 38 ++++++++++++++++++- .../bootstrap_daemon/src/log_backend_syslog.c | 3 ++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index cfa96e55a5..cd03db6588 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -58,6 +58,10 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por // Similar procedure to the one of reading config file below *tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t)); + if (*tcp_relay_ports == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return; + } for (size_t i = 0; i < default_ports_count; ++i) { @@ -75,7 +79,14 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por // The loop above skips invalid ports, so we adjust the allocated memory size if ((*tcp_relay_port_count) > 0) { - *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); + uint16_t *tmp = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); + if (tmp == nullptr) { + free(*tcp_relay_ports); + *tcp_relay_ports = nullptr; + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + } else { + *tcp_relay_ports = tmp; + } } else { free(*tcp_relay_ports); *tcp_relay_ports = nullptr; @@ -98,6 +109,10 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por } *tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t)); + if (*tcp_relay_ports == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return; + } for (int i = 0; i < config_port_count; ++i) { config_setting_t *elem = config_setting_get_elem(ports_array, i); @@ -127,7 +142,14 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por // The loop above skips invalid ports, so we adjust the allocated memory size if ((*tcp_relay_port_count) > 0) { - *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); + uint16_t *tmp = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); + if (tmp == nullptr) { + free(*tcp_relay_ports); + *tcp_relay_ports = nullptr; + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + } else { + *tcp_relay_ports = tmp; + } } else { free(*tcp_relay_ports); *tcp_relay_ports = nullptr; @@ -177,6 +199,10 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const size_t pid_file_path_len = strlen(tmp_pid_file) + 1; *pid_file_path = (char *)malloc(pid_file_path_len); + if (*pid_file_path == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return false; + } memcpy(*pid_file_path, tmp_pid_file, pid_file_path_len); // Get keys file location @@ -190,6 +216,10 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const size_t keys_file_path_len = strlen(tmp_keys_file) + 1; *keys_file_path = (char *)malloc(keys_file_path_len); + if (*keys_file_path == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return false; + } memcpy(*keys_file_path, tmp_keys_file, keys_file_path_len); // Get IPv6 option @@ -304,6 +334,10 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string) const size_t len = strlen(hex_string) / 2; uint8_t *ret = (uint8_t *)malloc(len); + if (ret == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return nullptr; + } const char *pos = hex_string; diff --git a/other/bootstrap_daemon/src/log_backend_syslog.c b/other/bootstrap_daemon/src/log_backend_syslog.c index 7afb938551..c5fccefd56 100644 --- a/other/bootstrap_daemon/src/log_backend_syslog.c +++ b/other/bootstrap_daemon/src/log_backend_syslog.c @@ -61,6 +61,9 @@ void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) } char *buf = (char *)malloc(size + 1); + if (buf == nullptr) { + return; + } vsnprintf(buf, size + 1, format, args); syslog(log_backend_syslog_level(level), "%s", buf);