-
Notifications
You must be signed in to change notification settings - Fork 81
Fix buffer overflow, resource leaks, missing error checks and possible use of uninitialized variables #563
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: master
Are you sure you want to change the base?
Changes from all commits
30b528a
4dae491
f44d604
79cb46c
af6f868
72c0757
5d7113b
3f109ee
147a2e9
158dafb
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 |
|---|---|---|
|
|
@@ -106,6 +106,7 @@ generateEventList(char *base_dir) | |
| struct dirent *hwmonx; | ||
| int i,pathnum; | ||
| int retlen; | ||
| int err = PAPI_OK; | ||
|
|
||
| #define NUM_PATHS 2 | ||
| char paths[NUM_PATHS][PATH_MAX]={ | ||
|
|
@@ -133,6 +134,7 @@ generateEventList(char *base_dir) | |
| base_dir, hwmonx->d_name,paths[pathnum]); | ||
| if (retlen <= 0 || PATH_MAX <= retlen) { | ||
| SUBDBG("Path length is too long.\n"); | ||
| closedir(dir); | ||
| return PAPI_EINVAL; | ||
| } | ||
| SUBDBG("Trying to open %s\n",path); | ||
|
|
@@ -146,7 +148,8 @@ generateEventList(char *base_dir) | |
| retlen = snprintf(filename, PAPI_MAX_STR_LEN, "%s/name",path); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Module name too long.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
| fff=fopen(filename,"r"); | ||
| if (fff==NULL) { | ||
|
|
@@ -176,7 +179,8 @@ generateEventList(char *base_dir) | |
| path,i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Failed to construct location label.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
| fff=fopen(filename,"r"); | ||
| if (fff==NULL) { | ||
|
|
@@ -194,7 +198,8 @@ generateEventList(char *base_dir) | |
| path,i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Failed input temperature string.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
| fff=fopen(filename,"r"); | ||
| if (fff==NULL) continue; | ||
|
|
@@ -203,9 +208,8 @@ generateEventList(char *base_dir) | |
| retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:in%i_input", hwmonx->d_name, i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Unable to generate name %s:in%i_input\n", hwmonx->d_name, i); | ||
| closedir(dir); | ||
| closedir(d); | ||
| return ( PAPI_EINVAL ); | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| snprintf(units, PAPI_MIN_STR_LEN, "V"); | ||
|
|
@@ -214,10 +218,12 @@ generateEventList(char *base_dir) | |
| location); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("snprintf failed.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| if (insert_in_list(name,units,description,filename)!=PAPI_OK) { | ||
| err = PAPI_ECMP; | ||
| goto done_error; | ||
| } | ||
|
|
||
|
|
@@ -236,7 +242,8 @@ generateEventList(char *base_dir) | |
| path,i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Location label string failed.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
| fff=fopen(filename,"r"); | ||
| if (fff==NULL) { | ||
|
|
@@ -254,7 +261,8 @@ generateEventList(char *base_dir) | |
| path,i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Input temperature string failed.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
| fff=fopen(filename,"r"); | ||
| if (fff==NULL) continue; | ||
|
|
@@ -263,9 +271,8 @@ generateEventList(char *base_dir) | |
| retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:temp%i_input", hwmonx->d_name, i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Unable to generate name %s:temp%i_input\n", hwmonx->d_name, i); | ||
| closedir(d); | ||
| closedir(dir); | ||
| return ( PAPI_EINVAL ); | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| snprintf(units, PAPI_MIN_STR_LEN, "degrees C"); | ||
|
|
@@ -274,10 +281,12 @@ generateEventList(char *base_dir) | |
| location); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("snprintf failed.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| if (insert_in_list(name,units,description,filename)!=PAPI_OK) { | ||
| err = PAPI_ECMP; | ||
|
Contributor
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. Same as above. |
||
| goto done_error; | ||
| } | ||
|
|
||
|
|
@@ -295,7 +304,8 @@ generateEventList(char *base_dir) | |
| path,i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Failed to write fan label string.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
| fff=fopen(filename,"r"); | ||
| if (fff==NULL) { | ||
|
|
@@ -312,9 +322,8 @@ generateEventList(char *base_dir) | |
| retlen = snprintf(filename, PAPI_MAX_STR_LEN, "%s/fan%d_input", path,i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Unable to generate filename %s/fan%d_input\n", path,i); | ||
| closedir(d); | ||
| closedir(dir); | ||
| return ( PAPI_EINVAL ); | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| fff=fopen(filename,"r"); | ||
|
|
@@ -324,9 +333,8 @@ generateEventList(char *base_dir) | |
| retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:fan%i_input", hwmonx->d_name, i); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("Unable to generate name %s:fan%i_input\n", hwmonx->d_name, i); | ||
| closedir(d); | ||
| closedir(dir); | ||
| return ( PAPI_EINVAL ); | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| snprintf(units, PAPI_MIN_STR_LEN, "RPM"); | ||
|
|
@@ -335,10 +343,12 @@ generateEventList(char *base_dir) | |
| location); | ||
| if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { | ||
| SUBDBG("snprintf failed.\n"); | ||
| return PAPI_EINVAL; | ||
| err = PAPI_EINVAL; | ||
| goto done_error; | ||
| } | ||
|
|
||
| if (insert_in_list(name,units,description,filename)!=PAPI_OK) { | ||
| err = PAPI_ECMP; | ||
|
Contributor
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. Same as above. |
||
| goto done_error; | ||
| } | ||
|
|
||
|
|
@@ -356,7 +366,7 @@ generateEventList(char *base_dir) | |
| done_error: | ||
| closedir(d); | ||
| closedir(dir); | ||
| return PAPI_ECMP; | ||
| return err; | ||
| } | ||
|
|
||
| static long long | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -857,6 +857,9 @@ static int _pcp_init_component(int cidx) | |
| { | ||
| int retval = PAPI_OK; | ||
| char *reason = _papi_hwd[cidx]->cmp_info.disabled_reason; // For error messages. | ||
| char **allNames = NULL; | ||
| pmID *allPMID = NULL; | ||
| pmResult *allFetch = NULL; // result of pmFetch. | ||
| int rLen = PAPI_HUGE_STR_LEN; // Most I will print. | ||
| reason[rLen-1]=0; // last resort terminator. | ||
|
|
||
|
|
@@ -941,17 +944,24 @@ static int _pcp_init_component(int cidx) | |
| } | ||
|
|
||
| int i,j,k; | ||
| char **allNames=calloc(sEventCount, sizeof(char*)); // Make an array for all names. | ||
| allNames=calloc(sEventCount, sizeof(char*)); // Make an array for all names. | ||
| if (allNames == NULL) { | ||
| snprintf(reason, rLen, "memory alloc denied for allNames; " | ||
| "size=%i.\n", sEventCount); | ||
| int strErr=snprintf(reason, rLen, "Could not allocate %lu bytes of memory for allNames.", sEventCount*sizeof(allNames)); | ||
| if (strErr > rLen) HANDLE_STRING_ERROR; | ||
| retval = PAPI_ENOMEM; | ||
| goto fn_fail; | ||
| } | ||
| for (i=0; i<sEventCount; i++) { // .. | ||
| allNames[i] = pcp_event_info[i].name; // copy pointer into array. | ||
| } // end for each event. | ||
|
|
||
| pmID *allPMID=calloc(sEventCount, sizeof(pmID)); // Make an array for results. | ||
| allPMID=calloc(sEventCount, sizeof(pmID)); // Make an array for results. | ||
| if (allPMID == NULL) { // If we failed, | ||
| snprintf(reason, rLen, "memory alloc denied for allPMID; " | ||
| "size=%i.\n", sEventCount); | ||
| int strErr=snprintf(reason, rLen, "Could not allocate %lu bytes of memory for allPMID.", sEventCount*sizeof(pmID)); | ||
| free(allNames); | ||
| if (strErr > rLen) HANDLE_STRING_ERROR; | ||
| retval = PAPI_ENOMEM; | ||
| goto fn_fail; | ||
|
|
@@ -1003,7 +1013,6 @@ static int _pcp_init_component(int cidx) | |
|
|
||
| for (i=0; i<sEventCount; i++) pcp_event_info[i].pmid = allPMID[i]; // copy all the pmid over to array. | ||
|
|
||
| pmResult *allFetch = NULL; // result of pmFetch. | ||
| _time_gettimeofday(&t1, NULL); | ||
| ret = pcp_pmFetch(sEventCount, allPMID, &allFetch); // Fetch (read) all the events. | ||
| _time_gettimeofday(&t2, NULL); | ||
|
|
@@ -1217,10 +1226,6 @@ static int _pcp_init_component(int cidx) | |
| // *************************************** END DEBUG REPORT INFORMATION DISCOVERED ************************************** | ||
| //----------------------------------------------------------------------------------------------------------------------- | ||
|
|
||
| free(allNames); // Locals allocations not needed anymore. | ||
| free(allPMID); // .. the pmIDs we read. | ||
| pcp_pmFreeResult(allFetch); // .. release the results we fetched. | ||
|
|
||
| // For PCP, we can read any number of events at once | ||
| // in a single event set. Set vector elements for PAPI. | ||
|
|
||
|
|
@@ -1230,6 +1235,9 @@ static int _pcp_init_component(int cidx) | |
| _pcp_vector.cmp_info.CmpIdx = cidx; // export the component ID. | ||
|
|
||
| fn_exit: | ||
| free(allNames); // Locals allocations not needed anymore. | ||
| free(allPMID); // .. the pmIDs we read. | ||
| pcp_pmFreeResult(allFetch); // .. release the results we fetched. | ||
|
Contributor
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. From documentation: As a general rule, if the called function returns an error status, then no allocation is done, the pointer to the variable sized result is undefined, and free, pmFreeLabelSets, or pmFreeResult should not be called. Therefore, before calling |
||
| _papi_hwd[cidx]->cmp_info.disabled = retval; | ||
| return retval; | ||
| fn_fail: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,10 @@ static char * _local_strlcpy( char *dst, const char *src, size_t size ) | |
| static long long read_powercap_value( int index ) | ||
| { | ||
| int sz = pread(event_fds[index], read_buff, PAPI_MAX_STR_LEN, 0); | ||
| if (sz == -1) { | ||
| perror("Error in pread(): "); | ||
| return (long long) 0; | ||
| } | ||
| read_buff[sz] = '\0'; | ||
|
|
||
| return atoll(read_buff); | ||
|
|
@@ -207,6 +211,7 @@ static int _powercap_init_component( int cidx ) | |
| if (event_fd == -1) { break; } | ||
|
|
||
| int sz = pread(event_fd, read_buff, PAPI_MAX_STR_LEN, 0); | ||
| if (sz == -1) { HANDLE_STRING_ERROR; } | ||
|
Contributor
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. The formatting here for |
||
| read_buff[sz] = '\0'; | ||
| close(event_fd); | ||
|
|
||
|
|
@@ -254,6 +259,7 @@ static int _powercap_init_component( int cidx ) | |
|
|
||
| if(powercap_ntv_events[num_events].type == PKG_NAME) { | ||
| int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); | ||
| if (sz == -1) HANDLE_STRING_ERROR; | ||
| read_buff[sz] = '\0'; | ||
| strErr=snprintf(powercap_ntv_events[num_events].description, sizeof(powercap_ntv_events[num_events].description), "%s", read_buff); | ||
| powercap_ntv_events[num_events].description[sizeof(powercap_ntv_events[num_events].description)-1]=0; | ||
|
|
@@ -262,6 +268,7 @@ static int _powercap_init_component( int cidx ) | |
|
|
||
| if(powercap_ntv_events[num_events].type == PKG_MAX_ENERGY_RANGE) { | ||
| int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); | ||
| if (sz == -1) HANDLE_STRING_ERROR; | ||
| read_buff[sz] = '\0'; | ||
| max_pkg_energy_count = atoll(read_buff); | ||
| } | ||
|
|
@@ -302,6 +309,7 @@ static int _powercap_init_component( int cidx ) | |
|
|
||
| if(powercap_ntv_events[num_events].type == COMPONENT_NAME) { | ||
| int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); | ||
| if (sz == -1) HANDLE_STRING_ERROR; | ||
| read_buff[sz] = '\0'; | ||
| strErr=snprintf(powercap_ntv_events[num_events].description, sizeof(powercap_ntv_events[num_events].description), "%s", read_buff); | ||
| powercap_ntv_events[num_events].description[sizeof(powercap_ntv_events[num_events].description)-1]=0; | ||
|
|
@@ -310,6 +318,7 @@ static int _powercap_init_component( int cidx ) | |
|
|
||
| if(powercap_ntv_events[num_events].type == COMPONENT_MAX_ENERGY_RANGE) { | ||
| int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); | ||
| if (sz == -1) HANDLE_STRING_ERROR; | ||
| read_buff[sz] = '\0'; | ||
| max_component_energy_count = atoll(read_buff); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insert_in_listcan also returnPAPI_ENOMEM. Can you change this check such that you seterrequal toinsert_in_listand then if a failure does occur you can jump todone_error?