From fd24762297ae008ac899e9e8051e7fd194800f83 Mon Sep 17 00:00:00 2001 From: ritt Date: Fri, 11 Feb 2022 19:08:31 +0100 Subject: [PATCH] Fixed (some, not all) compiler warnings about potential buffer overflows --- src/elconv.cxx | 10 +++--- src/elog.cxx | 10 +++--- src/elogd.cxx | 96 +++++++++++++++++++++++++++++--------------------- src/elogd.h | 2 +- 4 files changed, 66 insertions(+), 52 deletions(-) diff --git a/src/elconv.cxx b/src/elconv.cxx index ddf6f2b4..a41441f3 100644 --- a/src/elconv.cxx +++ b/src/elconv.cxx @@ -54,7 +54,7 @@ #define DIR_SEPARATOR '/' #define DIR_SEPARATOR_STR "/" -#define __USE_XOPEN /* needed for crypt() */ +#define __USE_XOPEN 1 /* needed for crypt() */ typedef int BOOL; @@ -230,7 +230,7 @@ INT ss_file_find(char *path, const char *pattern, char **plist) for (dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer)) { if (fnmatch1(pattern, dp->d_name) == 0) { *plist = (char *) realloc(*plist, (i + 1) * MAX_PATH_LENGTH); - strncpy(*plist + (i * MAX_PATH_LENGTH), dp->d_name, strlen(dp->d_name)); + strncpy(*plist + (i * MAX_PATH_LENGTH), dp->d_name, MAX_PATH_LENGTH); *(*plist + (i * MAX_PATH_LENGTH) + strlen(dp->d_name)) = '\0'; i++; seekdir(dir_pointer, telldir(dir_pointer)); @@ -278,7 +278,7 @@ INT el_search_message(char *tag, int *fh, BOOL walk, BOOL first) int lfh, i, n, d, min, max, size, offset, direction, status, did_walk; struct tm *tms, ltms; time_t lt, ltime, lact; - char str[256], file_name[256], dir[256]; + char str[256], file_name[1024], dir[256]; char *file_list, *tag_dir; did_walk = 0; @@ -583,7 +583,7 @@ INT el_submit(char attr_name[MAX_N_ATTR][NAME_LENGTH], { INT n, i, size, fh, status, index, offset, tail_size; struct tm *tms; - char file_name[256], afile_name[MAX_ATTACHMENTS][256], dir[256], + char file_name[1024], afile_name[MAX_ATTACHMENTS][256], dir[256], str[256], start_str[80], end_str[80], last[80], date[80], thread[80], attachment_all[64 * MAX_ATTACHMENTS]; time_t now; @@ -892,7 +892,7 @@ INT el_get_v1(char *tag, char *message, int *bufsize) void scan_messages() { int size, status, fh, message_id, i, n, n_messages; - char file_name[256], tag[256], str[256], last_file[256]; + char file_name[1024], tag[256], str[256], last_file[256]; char message[TEXT_SIZE + 1000]; char *ps, *pd, *file_list; THREAD *thread_list; diff --git a/src/elog.cxx b/src/elog.cxx index 2d126c2b..54a9dfd6 100755 --- a/src/elog.cxx +++ b/src/elog.cxx @@ -360,7 +360,7 @@ INT retrieve_elog(char *host, int port, char *subdir, int ssl, char *experiment, \********************************************************************/ { int i, n, first, index, sock; - char str[256], encrypted_passwd[256], *ph, *ps; + char str[1024], encrypted_passwd[256], *ph, *ps; #ifdef HAVE_SSL SSL *ssl_con = NULL; #endif @@ -525,7 +525,7 @@ INT retrieve_elog(char *host, int port, char *subdir, int ssl, char *experiment, if (strstr(response, "fail")) printf("Error: Invalid user name or password\n"); else { - strncpy(str, strstr(response, "Location:") + 10, sizeof(str)); + strncpy(str, strstr(response, "Location:") + 10, sizeof(str)-1); if (strchr(str, '?')) *strchr(str, '?') = 0; if (strchr(str, '\n')) @@ -958,7 +958,7 @@ INT submit_elog(char *host, int port, int ssl, char *subdir, char *experiment, else if (strstr(response, "fail")) printf("Error: Invalid user name or password\n"); else { - strncpy(str, strstr(response, "Location:") + 10, sizeof(str)); + strncpy(str, strstr(response, "Location:") + 10, sizeof(str)-1); if (strchr(str, '?')) *strchr(str, '?') = 0; if (strchr(str, '\n')) @@ -979,12 +979,12 @@ INT submit_elog(char *host, int port, int ssl, char *subdir, char *experiment, printf("Error: Missing or invalid password\n"); else if (strstr(response, "Error: Attribute")) { if (strstr(response, "not existing")) { - strncpy(str, strstr(response, "Error: Attribute") + 27, sizeof(str)); + strncpy(str, strstr(response, "Error: Attribute") + 27, sizeof(str)-1); if (strchr(str, '<')) *strchr(str, '<') = 0; printf("Error: Non existing attribute option \"%s\"\n", str); } else { - strncpy(str, strstr(response, "Error: Attribute") + 20, sizeof(str)); + strncpy(str, strstr(response, "Error: Attribute") + 20, sizeof(str)-1); if (strchr(str, '<')) *strchr(str, '<') = 0; printf("Error: Missing required attribute \"%s\"\n", str); diff --git a/src/elogd.cxx b/src/elogd.cxx index cdfe2955..ad522631 100755 --- a/src/elogd.cxx +++ b/src/elogd.cxx @@ -953,7 +953,7 @@ int my_shell(char *cmd, char *result, int size) { #ifdef OS_UNIX pid_t child_pid; int fh, status, wait_status; - char str[1024]; + char str[2048]; char tmp_filename[1024]; strlcpy(tmp_filename, "/tmp/elog_XXXXXX", sizeof(tmp_filename)); @@ -2503,7 +2503,7 @@ void split_url(const char *url, char *host, int *port, char *subdir, char *param if (strncmp(url, "https://", 8) == 0) p += 8; - strncpy(str, p, sizeof(str)); + strncpy(str, p, sizeof(str)-1); if (strchr(str, '/')) { if (subdir) strncpy(subdir, strchr(str, '/'), 256); @@ -3065,7 +3065,7 @@ int getcfg(const char *group, const char *param, char *value, int vsize) or [global] */ { - char str[256]; + char str[1024]; int status; /* if group is [global] and top group exists, read from there */ @@ -3715,9 +3715,10 @@ int ss_file_find(const char *path, const char *pattern, char **plist) for (dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer)) { if (fnmatch1(pattern, dp->d_name) == 0) { *plist = (char *) xrealloc(*plist, (i + 1) * MAX_PATH_LENGTH); - strncpy(*plist + (i * MAX_PATH_LENGTH), dp->d_name, strlen(dp->d_name)); + strncpy(*plist + (i * MAX_PATH_LENGTH), dp->d_name, MAX_PATH_LENGTH); *(*plist + (i * MAX_PATH_LENGTH) + strlen(dp->d_name)) = '\0'; i++; + seekdir(dir_pointer, telldir(dir_pointer)); } } closedir(dir_pointer); @@ -3754,6 +3755,7 @@ int ss_file_find(const char *path, const char *pattern, char **plist) xfree(lpfdata); return i; #endif + return 0; } /*------------------------------------------------------------------*/ @@ -4579,7 +4581,7 @@ int el_retrieve(LOGBOOK *lbs, int message_id, char *date, char attr_list[MAX_N_A int el_submit_attachment(LOGBOOK *lbs, const char *afilename, const char *buffer, int buffer_size, char *full_name) { - char file_name[MAX_PATH_LENGTH], ext_file_name[MAX_PATH_LENGTH + 100], str[MAX_PATH_LENGTH], + char file_name[MAX_PATH_LENGTH], ext_file_name[MAX_PATH_LENGTH + 100], str[MAX_PATH_LENGTH + 100], *p, subdir[MAX_PATH_LENGTH], path_name[MAX_PATH_LENGTH]; int fh; time_t now; @@ -6268,7 +6270,7 @@ const char void rsputs_elcode(LOGBOOK *lbs, BOOL email_notify, const char *str) { int i, j, k, l, m, elcode_disabled, elcode_disabled1, ordered_list, substituted, inside_table, smileys_enabled; - char *p, *pd, link[1000], link_text[1000], tmp[1000], attrib[1000], hattrib[1000], value[1000], + char *p, *pd, link[1000], link_text[1000], tmp[1000], attrib[1000], hattrib[1024], value[1000], subst[1000], base_url[256], param[256], *lstr, domain[256]; while (strlen_retbuf + (int) (2 * strlen(str) + 1000) >= return_buffer_size) { @@ -7350,7 +7352,7 @@ int scan_attributes(char *logbook) /* scan configuration file for attributes and fill attr_list, attr_options and attr_flags arrays */ { - char list[10000], str[NAME_LENGTH], str2[NAME_LENGTH], type[NAME_LENGTH], + char list[10000], str[NAME_LENGTH+32], str2[NAME_LENGTH], type[NAME_LENGTH], tmp_list[MAX_N_ATTR][NAME_LENGTH]; int i, j, n, m, n_options; @@ -7382,29 +7384,34 @@ int scan_attributes(char *logbook) for (i = 0; i < n; i++) { n_options = 0; - snprintf(str, sizeof(str), "Options %s", attr_list[i]); + strlcpy(str, "Options ", sizeof(str)); + strlcat(str, attr_list[i], sizeof(str)); if (getcfg(logbook, str, list, sizeof(list))) n_options = strbreak(list, attr_options[i], MAX_N_LIST, ",", FALSE); - snprintf(str, sizeof(str), "MOptions %s", attr_list[i]); + strlcpy(str, "MOptions ", sizeof(str)); + strlcat(str, attr_list[i], sizeof(str)); if (getcfg(logbook, str, list, sizeof(list))) { n_options = strbreak(list, attr_options[i], MAX_N_LIST, ",", FALSE); attr_flags[i] |= AF_MULTI; } - snprintf(str, sizeof(str), "ROptions %s", attr_list[i]); + strlcpy(str, "ROptions ", sizeof(str)); + strlcat(str, attr_list[i], sizeof(str)); if (getcfg(logbook, str, list, sizeof(list))) { n_options = strbreak(list, attr_options[i], MAX_N_LIST, ",", FALSE); attr_flags[i] |= AF_RADIO; } - snprintf(str, sizeof(str), "IOptions %s", attr_list[i]); + strlcpy(str, "IOptions ", sizeof(str)); + strlcat(str, attr_list[i], sizeof(str)); if (getcfg(logbook, str, list, sizeof(list))) { n_options = strbreak(list, attr_options[i], MAX_N_LIST, ",", FALSE); attr_flags[i] |= AF_ICON; } - snprintf(str2, sizeof(str2), "Sort Attribute Options %s", attr_list[i]); + strlcpy(str2, "Sort Attribute Options ", sizeof(str2)); + strlcat(str2, attr_list[i], sizeof(str2)); if (n_options && getcfg(logbook, str2, str, sizeof(str)) && atoi(str) == 1) { qsort(attr_options[i], n_options, NAME_LENGTH, ascii_compare2); } @@ -7456,7 +7463,8 @@ int scan_attributes(char *logbook) } for (i = 0; i < n; i++) { - snprintf(str, sizeof(str), "Type %s", attr_list[i]); + strlcpy(str, "Type ", sizeof(str)); + strlcat(str, attr_list[i], sizeof(str)); if (getcfg(logbook, str, type, sizeof(type))) { if (strieq(type, "date")) attr_flags[i] |= AF_DATE; @@ -9251,7 +9259,7 @@ void show_time_selector(int hour, int min, int sec, const char *index) { void attrib_from_param(int n_attr, char attrib[MAX_N_ATTR][NAME_LENGTH]) { int i, j, first, year, month, day, hour, min, sec; - char str[NAME_LENGTH], ua[NAME_LENGTH]; + char str[NAME_LENGTH+100], ua[NAME_LENGTH]; time_t ltime; struct tm ts; @@ -14682,7 +14690,7 @@ void show_logbook_new(LOGBOOK *lbs) { /*------------------------------------------------------------------*/ int show_download_page(LOGBOOK *lbs, char *path) { - char file_name[256], error_str[256]; + char file_name[1024], error_str[256]; int index, message_id, fh, i, size, delta; char message[TEXT_SIZE + 1000], *p, *buffer; @@ -18896,8 +18904,8 @@ void build_ref(char *ref, int size, const char *mode, const char *expand, const void show_page_filters(LOGBOOK *lbs, int n_msg, int page_n, BOOL mode_commands, char *mode) { int cur_exp, n, i, j, i1, i2, index, attr_index, size; - char ref[256], str[NAME_LENGTH], comment[NAME_LENGTH], list[MAX_N_LIST][NAME_LENGTH], - option[NAME_LENGTH], option_whole[NAME_LENGTH]; + char ref[256], str[NAME_LENGTH+100], comment[NAME_LENGTH], list[MAX_N_LIST][NAME_LENGTH], + option[NAME_LENGTH], option_whole[NAME_LENGTH+100], litem[NAME_LENGTH]; rsprintf("\n"); rsprintf("\n"); @@ -19130,7 +19138,8 @@ void show_page_filters(LOGBOOK *lbs, int n_msg, int page_n, BOOL mode_commands, loc("3 Days")); rsprintf("