Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct a few potential null derefs in bootstrap daemon. #2662

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 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 @@ -73,10 +77,8 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
++*tcp_relay_port_count;
}

// 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));
} else {
// No ports, so we free the array.
if (*tcp_relay_port_count == 0) {
free(*tcp_relay_ports);
*tcp_relay_ports = nullptr;
}
Expand All @@ -98,6 +100,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 @@ -125,10 +131,8 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
++*tcp_relay_port_count;
}

// 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));
} else {
// No ports, so we free the array.
if (*tcp_relay_port_count == 0) {
free(*tcp_relay_ports);
*tcp_relay_ports = nullptr;
}
Expand Down Expand Up @@ -177,6 +181,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 +198,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 +316,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
Loading