Security hardening (#6302)

Addresses CodeQL issues (Critical and High).
This commit is contained in:
Scot Breitenfeld
2026-03-18 22:32:03 -05:00
committed by GitHub
parent 39d7b764d3
commit 05758a6168
25 changed files with 239 additions and 109 deletions
+2 -2
View File
@@ -1037,8 +1037,8 @@ h5diff(const char *fname1, const char *fname2, const char *objname1, const char
MPI_Abort(MPI_COMM_WORLD, 0);
} /* end if */
strcpy(filenames[0], fname1);
strcpy(filenames[1], fname2);
snprintf(filenames[0], MAX_FILENAME, "%s", fname1);
snprintf(filenames[1], MAX_FILENAME, "%s", fname2);
/* Alert the worker tasks that there's going to be work. */
for (int i = 1; i < g_nTasks; i++)
+6 -5
View File
@@ -396,14 +396,15 @@ xml_dump_all_cb(hid_t group, const char *name, const H5L_info2_t *linfo, void H5
char *t_targbuf = xml_escape_the_name(targbuf);
char *t_obj_path = xml_escape_the_name(obj_path);
char *t_link_path;
int res;
/* +2 accounts for '/' separator and NUL terminator */
size_t t_link_path_len = strlen(prefix) + linfo->u.val_size + 2;
int res;
t_link_path = (char *)malloc(strlen(prefix) + linfo->u.val_size + 1);
t_link_path = (char *)malloc(t_link_path_len);
if (targbuf[0] == '/')
strcpy(t_link_path, targbuf);
snprintf(t_link_path, t_link_path_len, "%s", targbuf);
else {
strcpy(t_link_path, prefix);
strcat(strcat(t_link_path, "/"), targbuf);
snprintf(t_link_path, t_link_path_len, "%s/%s", prefix, targbuf);
} /* end else */
/* Create OBJ-XIDs for the parent and object */
+14 -7
View File
@@ -131,7 +131,11 @@ main(int argc, char *argv[])
case 1: /* counting input files */
if (opt->fcount < 29) {
(void)strcpy(opt->infiles[opt->fcount].datafile, argv[i]);
if (snprintf(opt->infiles[opt->fcount].datafile, MAX_PATH_NAME_LENGTH, "%s", argv[i]) >=
MAX_PATH_NAME_LENGTH) {
(void)fprintf(rawerrorstream, err10, argv[i]);
goto err;
}
in = &(opt->infiles[opt->fcount].in);
opt->infiles[opt->fcount].config = 0;
setDefaultValues(in, opt->fcount);
@@ -148,7 +152,11 @@ main(int argc, char *argv[])
break;
case 3: /* get configfile name */
(void)strcpy(opt->infiles[opt->fcount - 1].configfile, argv[i]);
if (snprintf(opt->infiles[opt->fcount - 1].configfile, MAX_PATH_NAME_LENGTH, "%s", argv[i]) >=
MAX_PATH_NAME_LENGTH) {
(void)fprintf(rawerrorstream, err10, argv[i]);
goto err;
}
opt->infiles[opt->fcount - 1].config = 1;
break;
@@ -160,7 +168,7 @@ main(int argc, char *argv[])
(void)fprintf(rawerrorstream, err10, argv[i]);
goto err;
}
(void)strcpy(opt->outfile, argv[i]);
snprintf(opt->outfile, MAX_PATH_NAME_LENGTH, "%s", argv[i]);
outfile_named = true;
break;
@@ -2524,7 +2532,7 @@ parsePathInfo(struct path_info *path, char *temp)
(void)fprintf(rawerrorstream, "%s", err1);
return (-1);
}
strcpy(path->group[i++], token);
snprintf(path->group[i++], MAX_PATH_NAME_LENGTH, "%s", token);
while (1) {
token = strtok(NULL, delimiter);
@@ -2534,7 +2542,7 @@ parsePathInfo(struct path_info *path, char *temp)
(void)fprintf(rawerrorstream, "%s", err1);
return (-1);
}
strcpy(path->group[i++], token);
snprintf(path->group[i++], MAX_PATH_NAME_LENGTH, "%s", token);
}
path->count = i;
return (0);
@@ -3973,8 +3981,7 @@ getExternalFilename(struct Input *in, FILE *strm)
temp_len = strlen(temp);
in->externFilename = (char *)malloc((temp_len + 1) * sizeof(char));
(void)strcpy(in->externFilename, temp);
in->externFilename[temp_len] = '\0';
snprintf(in->externFilename, temp_len + 1, "%s", temp);
return (0);
}
+3 -3
View File
@@ -401,9 +401,9 @@ struct infilesformat {
};
struct Options {
struct infilesformat infiles[30]; /* structure to hold the list of input file names. Limited to 30*/
char outfile[256]; /* output file name */
int fcount; /* number of input files */
struct infilesformat infiles[30]; /* structure to hold the list of input file names. Limited to 30*/
char outfile[MAX_PATH_NAME_LENGTH]; /* output file name */
int fcount; /* number of input files */
};
static char keytable[NUM_KEYS][30] = {"PATH",
+6 -5
View File
@@ -2285,14 +2285,15 @@ list_obj(const char *name, const H5O_info2_t *oinfo, const char *first_seen, voi
/* Modification time */
if (oinfo->mtime > 0) {
char buf[256];
struct tm *tm;
struct tm tm_buf;
struct tm *tm_result;
if (simple_output_g)
tm = gmtime(&(oinfo->mtime));
tm_result = HDgmtime_r(&(oinfo->mtime), &tm_buf);
else
tm = localtime(&(oinfo->mtime));
if (tm) {
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", tm);
tm_result = HDlocaltime_r(&(oinfo->mtime), &tm_buf);
if (tm_result) {
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", &tm_buf);
h5tools_str_reset(&buffer);
h5tools_str_append(&buffer, " %-10s %s\n", "Modified:", buf);
h5tools_render_element(rawoutstream, info, &ctx, &buffer, &curr_pos,
+13 -11
View File
@@ -429,10 +429,12 @@ pio_create_filename(iotype iot, const char *base_name, char *fullname, size_t si
fullname[size - 1] = '\0';
}
if ((strlen(fullname) + strlen(base_name) + 1) < size) {
{
/* Append the base_name with a slash first. Multiple slashes are
* handled below. */
h5_stat_t buf;
size_t cur_len;
int nchars;
memset(&buf, 0, sizeof(h5_stat_t));
if (HDstat(fullname, &buf) < 0)
@@ -440,15 +442,13 @@ pio_create_filename(iotype iot, const char *base_name, char *fullname, size_t si
if (HDmkdir(fullname, (mode_t)0755) < 0 && errno != EEXIST) {
/* We couldn't make the "/tmp/${USER,LOGIN}" subdirectory.
* Default to PREFIX's original prefix value. */
strcpy(fullname, prefix);
snprintf(fullname, size, "%s", prefix);
}
strcat(fullname, "/");
strcat(fullname, base_name);
}
else {
/* Buffer is too small */
return NULL;
cur_len = strlen(fullname);
nchars = snprintf(fullname + cur_len, size - cur_len, "/%s", base_name);
if (nchars < 0 || (size_t)nchars >= size - cur_len)
return NULL;
}
}
else if (strlen(base_name) >= size) {
@@ -456,15 +456,17 @@ pio_create_filename(iotype iot, const char *base_name, char *fullname, size_t si
return NULL;
}
else {
strcpy(fullname, base_name);
snprintf(fullname, size, "%s", base_name);
}
/* Append a suffix */
if (suffix) {
if (strlen(fullname) + strlen(suffix) >= size)
size_t cur_len = strlen(fullname);
if (cur_len + strlen(suffix) >= size)
return NULL;
strcat(fullname, suffix);
snprintf(fullname + cur_len, size - cur_len, "%s", suffix);
}
/* Remove any double slashes in the filename */
+13 -11
View File
@@ -338,10 +338,12 @@ sio_create_filename(iotype iot, const char *base_name, char *fullname, size_t si
fullname[size - 1] = '\0';
}
if ((strlen(fullname) + strlen(base_name) + 1) < size) {
{
/* Append the base_name with a slash first. Multiple slashes are
* handled below. */
h5_stat_t buf;
size_t cur_len;
int nchars;
memset(&buf, 0, sizeof(h5_stat_t));
if (HDstat(fullname, &buf) < 0)
@@ -349,15 +351,13 @@ sio_create_filename(iotype iot, const char *base_name, char *fullname, size_t si
if (HDmkdir(fullname, 0755) < 0 && errno != EEXIST) {
/* We couldn't make the "/tmp/${USER,LOGIN}" subdirectory.
* Default to PREFIX's original prefix value. */
strcpy(fullname, prefix);
snprintf(fullname, size, "%s", prefix);
}
strcat(fullname, "/");
strcat(fullname, base_name);
}
else {
/* Buffer is too small */
return NULL;
cur_len = strlen(fullname);
nchars = snprintf(fullname + cur_len, size - cur_len, "/%s", base_name);
if (nchars < 0 || (size_t)nchars >= size - cur_len)
return NULL;
}
}
else if (strlen(base_name) >= size) {
@@ -365,15 +365,17 @@ sio_create_filename(iotype iot, const char *base_name, char *fullname, size_t si
return NULL;
}
else {
strcpy(fullname, base_name);
snprintf(fullname, size, "%s", base_name);
}
/* Append a suffix */
if (suffix) {
if (strlen(fullname) + strlen(suffix) >= size)
size_t cur_len = strlen(fullname);
if (cur_len + strlen(suffix) >= size)
return NULL;
strcat(fullname, suffix);
snprintf(fullname + cur_len, size - cur_len, "%s", suffix);
}
/* Remove any double slashes in the filename */
+2 -2
View File
@@ -680,7 +680,7 @@ parse_command_line(int argc, const char *const *argv, pack_opt_t *options)
else {
char msgType[10];
strcpy(msgType, msgPtr + 1);
snprintf(msgType, sizeof(msgType), "%s", msgPtr + 1);
msgPtr[0] = '\0';
ssize = atoi(H5_optarg);
if (!strncmp(msgType, "dspace", 6))
@@ -725,7 +725,7 @@ parse_command_line(int argc, const char *const *argv, pack_opt_t *options)
case 'S': {
char strategy[MAX_NC_NAME];
strcpy(strategy, H5_optarg);
snprintf(strategy, MAX_NC_NAME, "%s", H5_optarg);
if (!strcmp(strategy, "FSM_AGGR"))
options->fs_strategy = H5F_FSPACE_STRATEGY_FSM_AGGR;
else if (!strcmp(strategy, "PAGE"))
+2 -2
View File
@@ -94,7 +94,7 @@ parse_filter(const char *str, unsigned *n_objs, filter_info_t *filt, pack_opt_t
else
sobj[k + 1] = '\0';
strcpy(obj_list[n].obj, sobj);
snprintf(obj_list[n].obj, MAX_NC_NAME, "%s", sobj);
memset(sobj, 0, sizeof(sobj));
n++;
k = -1;
@@ -530,7 +530,7 @@ parse_layout(const char *str, unsigned *n_objs, pack_info_t *pack, /* info about
sobj[k] = '\0';
else
sobj[k + 1] = '\0';
strcpy(obj_list[n].obj, sobj);
snprintf(obj_list[n].obj, MAX_NC_NAME, "%s", sobj);
memset(sobj, 0, sizeof(sobj));
n++;
k = -1;