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

Wcohen/coverity202411 #287

Merged
merged 4 commits into from
Nov 21, 2024

Conversation

wcohen
Copy link
Contributor

@wcohen wcohen commented Nov 20, 2024

Fixes for issues reported by coverity, redo of #266

Went through coverity scan of papi and produced patches to address some of the issue reported. PAPI with this PR's patches has been build and tests were run to check that things compiled and functions as expected. This incorporates the changes suggested in pull request #266.

Author Checklist

  • Description
    Why this PR exists. Reference all relevant information, including background, issues, test failures, etc
  • Commits
    Commits are self contained and only do one thing
    Commits have a header of the form: module: short description
    Commits have a body (whenever relevant) containing a detailed description of the addressed problem and its solution
  • Tests
    The PR needs to pass all the tests

@Treece-Burgess
Copy link
Contributor

Good morning @wcohen, on the old PR you stated that you incorporated the changes for src/components/infiniband/linux-infiniband.c. However, I do not see them in the new PR.

@wcohen wcohen force-pushed the wcohen/coverity202411 branch from 7063b54 to b976c71 Compare November 21, 2024 16:21
@wcohen
Copy link
Contributor Author

wcohen commented Nov 21, 2024

Your are correct. Shomehow the fixup for linux-infiniband.c wasn't in there. Just pushed a version with the fixup included to this pull request.

It is possible in add_ib_counter() that one of the strdup() operations
fails and the other succeeds.  In cases where the struct referencing
them is being free because of a failure one of the strdup() any
allocated memory should be freed also.  This was noted noted in a
coverity scan of PAPI:

 Error: RESOURCE_LEAK (CWE-772):
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/infiniband/linux-infiniband.c:332:5: alloc_fn: Storage is returned from allocation function "strdup".
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/infiniband/linux-infiniband.c:332:5: var_assign: Assigning: "new_cnt->ev_file_name" = storage returned from "strdup(file_name)".
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/infiniband/linux-infiniband.c:338:9: leaked_storage: Freeing "new_cnt" without freeing its pointer field "ev_file_name" leaks the storage that "ev_file_name" points to.
 #  336|       {
 #  337|           PAPIERROR("cannot allocate memory for counter internal fields");
 #  338|->         papi_free(new_cnt);
 #  339|           return (0);
 #  340|       }

 Error: RESOURCE_LEAK (CWE-772):
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/infiniband/linux-infiniband.c:331:5: alloc_fn: Storage is returned from allocation function "strdup".
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/infiniband/linux-infiniband.c:331:5: var_assign: Assigning: "new_cnt->ev_name" = storage returned from "strdup(name)".
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/infiniband/linux-infiniband.c:338:9: leaked_storage: Freeing "new_cnt" without freeing its pointer field "ev_name" leaks the storage that "ev_name" points to.
 #  336|       {
 #  337|           PAPIERROR("cannot allocate memory for counter internal fields");
 #  338|->         papi_free(new_cnt);
 #  339|           return (0);
 #  340|       }
…ized

Initializing variable type to a sane value (PAPI_MH_TYPE_EMPTY) to make
sure that the coverity static analysis does not generate the following
message:

 Error: UNINIT (CWE-457):
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/sysdetect/linux_cpu_utils.c:561:5: var_decl: Declaring variable "type" without initializer.
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/sysdetect/linux_cpu_utils.c:593:5: uninit_use: Using uninitialized value "type".
 #  591|       }
 #  592|
 #  593|->     *value = type;
 #  594|
 #  595|       return CPU_SUCCESS;
…read

Added code to properly free memory for the following situation that
Coverity pointed out:

 Error: RESOURCE_LEAK (CWE-772): [#def49] [important]
 papi-7.2.0b1-build/papi-7.2.0b1/src/threads.c:117:2: alloc_fn: Storage is returned from allocation function "malloc".
 papi-7.2.0b1-build/papi-7.2.0b1/src/threads.c:117:2: var_assign: Assigning: "thread->running_eventset" = storage returned from "malloc(8UL * (size_t)papi_num_components)".
 papi-7.2.0b1-build/papi-7.2.0b1/src/threads.c:134:4: leaked_storage: Freeing "thread" without freeing its pointer field "running_eventset" leaks the storage that "running_eventset" points to.
 #  132|   				papi_free( thread->context[i] );
 #  133|   			papi_free( thread->context );
 #  134|-> 			papi_free( thread );
 #  135|   			return ( NULL );
 #  136|
The local path_name variable in link_lmsensors_libraries() needs to
be PATH_MAX in size to avoid possible overruns and scribbling over
parts of the stack.

 Error: OVERRUN (CWE-119): [#def22] [important]
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/lmsensors/linux-lmsensors.c:398:7: overrun-buffer-arg: Overrunning array "path_name" of 1024 bytes by passing it to a function which accesses it at byte offset 4095 using argument "4096UL". [Note: The source code implementation of the function has been overridden by a builtin model.]
 #  396|      // Step 3: Try the explicit install default.
 #  397|      if (dl1 == NULL && lmsensors_root != NULL) {                          // if root given, try it.
 #  398|->       snprintf(path_name, PATH_MAX, "%s/lib64/libsensors.so", lmsensors_root);   // PAPI Root check.
 #  399|         dl1 = dlopen(path_name, RTLD_NOW | RTLD_GLOBAL);             // Try to open that path.
 #  400|      }

 Error: OVERRUN (CWE-119): [#def23] [important]
 papi-7.2.0b1-build/papi-7.2.0b1/src/components/lmsensors/linux-lmsensors.c:404:7: overrun-buffer-arg: Overrunning array "path_name" of 1024 bytes by passing it to a function which accesses it at byte offset 4095 using argument "4096UL". [Note: The source code implementation of the function has been overridden by a builtin model.]
 #  402|      // Step 4: Try another explicit install default.
 #  403|      if (dl1 == NULL && lmsensors_root != NULL) {                          // if root given, try it.
 #  404|->       snprintf(path_name, PATH_MAX, "%s/lib/libsensors.so", lmsensors_root);   // PAPI Root check.
 #  405|         dl1 = dlopen(path_name, RTLD_NOW | RTLD_GLOBAL);             // Try to open that path.
 #  406|      }
@wcohen wcohen force-pushed the wcohen/coverity202411 branch from b976c71 to ed919ac Compare November 21, 2024 18:49
@Treece-Burgess Treece-Burgess merged commit 86d1590 into icl-utk-edu:master Nov 21, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants