Skip to content

Commit

Permalink
fix: Correct a few potential null derefs in bootstrap daemon.
Browse files Browse the repository at this point in the history
Found by PVS Studio.
  • Loading branch information
iphydf committed Feb 9, 2024
1 parent 7c44379 commit 9a36d44
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
38 changes: 36 additions & 2 deletions other/bootstrap_daemon/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions other/bootstrap_daemon/src/log_backend_syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9a36d44

Please sign in to comment.