-
-
Notifications
You must be signed in to change notification settings - Fork 337
Fix memory safety vulnerabilities in high-level and VFD code #6140
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
7b22833
e450bd6
2b42b81
d3f9058
187b77a
a5298fd
31edfe0
14b3a69
1ce73d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1889,13 +1889,17 @@ H5LTtext_to_dtype(const char *text, H5LT_lang_t lang_type) | |
|
|
||
| input_len = strlen(text); | ||
| myinput = strdup(text); | ||
| if (!myinput) | ||
| goto out; | ||
|
|
||
| if ((type_id = H5LTyyparse()) < 0) { | ||
| free(myinput); | ||
| myinput = NULL; | ||
| goto out; | ||
| } | ||
|
|
||
| free(myinput); | ||
| myinput = NULL; | ||
| input_len = 0; | ||
|
|
||
| return type_id; | ||
|
|
@@ -1942,6 +1946,9 @@ realloc_and_append(bool _no_user_buf, size_t *len, char *buf, const char *str_to | |
| buf = tmp_realloc; | ||
| } | ||
|
|
||
| if (!buf) | ||
|
||
| goto out; | ||
|
|
||
| if (str_to_add) { | ||
| /* find the size of the buffer to add */ | ||
| size_str_to_add = strlen(str_to_add); | ||
|
|
@@ -2171,6 +2178,50 @@ H5LTdtype_to_text(hid_t dtype, char *str, H5LT_lang_t lang_type, size_t *len) | |
| return FAIL; | ||
| } | ||
|
|
||
| /*------------------------------------------------------------------------- | ||
| * Function: H5LT_append_dtype_super_text | ||
| * | ||
| * Purpose: Helper function to get super type text and append it to dt_str. | ||
| * This encapsulates the common pattern of: allocate buffer, | ||
| * convert dtype to text, append to string, free buffer. | ||
| * | ||
| * Return: Success: updated dt_str pointer, Failure: NULL | ||
| * | ||
| *------------------------------------------------------------------------- | ||
| */ | ||
| static char * | ||
| H5LT_append_dtype_super_text(hid_t super, char *dt_str, H5LT_lang_t lang, size_t *slen, bool no_user_buf) | ||
| { | ||
| size_t super_len; | ||
| char *stmp = NULL; | ||
|
|
||
| /* Get required buffer size for super type text */ | ||
| if (H5LTdtype_to_text(super, NULL, lang, &super_len) < 0) | ||
| return NULL; | ||
|
|
||
| /* Allocate buffer for super type text */ | ||
| stmp = (char *)calloc(super_len, sizeof(char)); | ||
| if (!stmp) | ||
| return NULL; | ||
|
|
||
| /* Convert super type to text */ | ||
| if (H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) { | ||
| free(stmp); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Append super type text to dt_str */ | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) { | ||
| free(stmp); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Clean up */ | ||
| free(stmp); | ||
|
|
||
| return dt_str; | ||
| } | ||
|
|
||
| /*------------------------------------------------------------------------- | ||
| * Function: H5LT_dtype_to_text | ||
| * | ||
|
|
@@ -2533,8 +2584,7 @@ H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, bo | |
| tag = H5Tget_tag(dtype); | ||
| if (tag) { | ||
| snprintf(tmp_str, TMP_LEN, "OPQ_TAG \"%s\";\n", tag); | ||
| if (tag) | ||
| H5free_memory(tag); | ||
| H5free_memory(tag); | ||
| tag = NULL; | ||
| } | ||
| else | ||
|
|
@@ -2553,33 +2603,19 @@ H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, bo | |
| break; | ||
| } | ||
| case H5T_ENUM: { | ||
| hid_t super; | ||
| size_t super_len; | ||
| char *stmp = NULL; | ||
| hid_t super; | ||
|
|
||
| /* Print lead-in */ | ||
| snprintf(dt_str, *slen, "H5T_ENUM {\n"); | ||
| indent += COL; | ||
| if (!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) | ||
| goto out; | ||
|
|
||
| /* Get super type and append its text representation */ | ||
| if ((super = H5Tget_super(dtype)) < 0) | ||
| goto out; | ||
| if (H5LTdtype_to_text(super, NULL, lang, &super_len) < 0) | ||
| goto out; | ||
| stmp = (char *)calloc(super_len, sizeof(char)); | ||
| if (H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) { | ||
| free(stmp); | ||
| goto out; | ||
| } | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) { | ||
| free(stmp); | ||
| if (!(dt_str = H5LT_append_dtype_super_text(super, dt_str, lang, slen, no_user_buf))) | ||
| goto out; | ||
| } | ||
|
|
||
| if (stmp) | ||
| free(stmp); | ||
| stmp = NULL; | ||
|
|
||
| snprintf(tmp_str, TMP_LEN, ";\n"); | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) | ||
|
|
@@ -2600,33 +2636,20 @@ H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, bo | |
| break; | ||
| } | ||
| case H5T_VLEN: { | ||
| hid_t super; | ||
| size_t super_len; | ||
| char *stmp = NULL; | ||
| hid_t super; | ||
|
|
||
| /* Print lead-in */ | ||
| snprintf(dt_str, *slen, "H5T_VLEN {\n"); | ||
| indent += COL; | ||
| if (!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) | ||
| goto out; | ||
|
|
||
| /* Get super type and append its text representation */ | ||
| if ((super = H5Tget_super(dtype)) < 0) | ||
| goto out; | ||
| if (H5LTdtype_to_text(super, NULL, lang, &super_len) < 0) | ||
| goto out; | ||
| stmp = (char *)calloc(super_len, sizeof(char)); | ||
| if (H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) { | ||
| free(stmp); | ||
| goto out; | ||
| } | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) { | ||
| free(stmp); | ||
| if (!(dt_str = H5LT_append_dtype_super_text(super, dt_str, lang, slen, no_user_buf))) | ||
| goto out; | ||
| } | ||
|
|
||
| if (stmp) | ||
| free(stmp); | ||
| stmp = NULL; | ||
| snprintf(tmp_str, TMP_LEN, "\n"); | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) | ||
| goto out; | ||
|
|
@@ -2644,8 +2667,6 @@ H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, bo | |
| } | ||
| case H5T_ARRAY: { | ||
| hid_t super; | ||
| size_t super_len; | ||
| char *stmp = NULL; | ||
| hsize_t dims[H5S_MAX_RANK]; | ||
| int ndims; | ||
|
|
||
|
|
@@ -2671,22 +2692,12 @@ H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, bo | |
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) | ||
| goto out; | ||
|
|
||
| /* Get super type and append its text representation */ | ||
| if ((super = H5Tget_super(dtype)) < 0) | ||
| goto out; | ||
| if (H5LTdtype_to_text(super, NULL, lang, &super_len) < 0) | ||
| goto out; | ||
| stmp = (char *)calloc(super_len, sizeof(char)); | ||
| if (H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) { | ||
| free(stmp); | ||
| if (!(dt_str = H5LT_append_dtype_super_text(super, dt_str, lang, slen, no_user_buf))) | ||
| goto out; | ||
| } | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) { | ||
| free(stmp); | ||
| goto out; | ||
| } | ||
| if (stmp) | ||
| free(stmp); | ||
| stmp = NULL; | ||
|
|
||
| snprintf(tmp_str, TMP_LEN, "\n"); | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) | ||
| goto out; | ||
|
|
@@ -2772,33 +2783,20 @@ H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, bo | |
| break; | ||
| } | ||
| case H5T_COMPLEX: { | ||
| hid_t super; | ||
| size_t super_len; | ||
| char *stmp = NULL; | ||
| hid_t super; | ||
|
|
||
| /* Print lead-in */ | ||
| snprintf(dt_str, *slen, "H5T_COMPLEX {\n"); | ||
| indent += COL; | ||
| if (!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) | ||
| goto out; | ||
|
|
||
| /* Get super type and append its text representation */ | ||
| if ((super = H5Tget_super(dtype)) < 0) | ||
| goto out; | ||
| if (H5LTdtype_to_text(super, NULL, lang, &super_len) < 0) | ||
| goto out; | ||
| stmp = (char *)calloc(super_len, sizeof(char)); | ||
| if (H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) { | ||
| free(stmp); | ||
| if (!(dt_str = H5LT_append_dtype_super_text(super, dt_str, lang, slen, no_user_buf))) | ||
| goto out; | ||
| } | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) { | ||
| free(stmp); | ||
| goto out; | ||
| } | ||
|
|
||
| if (stmp) | ||
| free(stmp); | ||
| stmp = NULL; | ||
| snprintf(tmp_str, TMP_LEN, "\n"); | ||
| if (!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) | ||
| goto out; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -393,8 +393,8 @@ H5FD_stdio_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr | |
|
|
||
| /* Use the value in the property list */ | ||
| if (H5Pget_file_locking(fapl_id, &unused, &file->ignore_disabled_file_locks) < 0) { | ||
| fclose(file->fp); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the issue with these close calls?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Snyk flags it for clarity on resource ownership. The concern is that it's not explicit which pointer "owns" the resource after the assignment. Once you've assigned file->fp = f, the FILE* is conceptually owned by the file structure, and using file->fp in cleanup makes this ownership clear. |
||
| free(file); | ||
| fclose(f); | ||
| H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTGET, | ||
| "unable to get use disabled file locks property", NULL); | ||
| } | ||
|
|
@@ -407,23 +407,23 @@ H5FD_stdio_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr | |
| file->fd = fileno(file->fp); | ||
| #endif /* H5_HAVE_WIN32_API */ | ||
| if (file->fd < 0) { | ||
| fclose(file->fp); | ||
| free(file); | ||
| fclose(f); | ||
| H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTOPENFILE, "unable to get file descriptor", NULL); | ||
| } /* end if */ | ||
|
|
||
| #ifdef H5_HAVE_WIN32_API | ||
| file->hFile = (HANDLE)_get_osfhandle(file->fd); | ||
| if (INVALID_HANDLE_VALUE == file->hFile) { | ||
| fclose(file->fp); | ||
| free(file); | ||
| fclose(f); | ||
| H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTOPENFILE, "unable to get Windows file handle", | ||
| NULL); | ||
| } /* end if */ | ||
|
|
||
| if (!GetFileInformationByHandle((HANDLE)file->hFile, &fileinfo)) { | ||
| fclose(file->fp); | ||
| free(file); | ||
| fclose(f); | ||
| H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTOPENFILE, | ||
| "unable to get Windows file descriptor information", NULL); | ||
| } /* end if */ | ||
|
|
@@ -433,8 +433,8 @@ H5FD_stdio_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr | |
| file->dwVolumeSerialNumber = fileinfo.dwVolumeSerialNumber; | ||
| #else /* H5_HAVE_WIN32_API */ | ||
| if (fstat(file->fd, &sb) < 0) { | ||
| fclose(file->fp); | ||
| free(file); | ||
| fclose(f); | ||
| H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_BADFILE, "unable to fstat file", NULL); | ||
| } /* end if */ | ||
| file->device = sb.st_dev; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.