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

covscan: fix miscellaneaous errors #2003

Merged
merged 7 commits into from
Nov 1, 2018

Conversation

jeromemarchand
Copy link
Contributor

This fixes a few errors discovered by a coverity scan.

Copy link
Collaborator

@yonghong-song yonghong-song left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. A few comments.

src/cc/bcc_elf.c Outdated
@@ -373,6 +373,9 @@ static int verify_checksum(const char *file, unsigned int crc) {
void *buf;
unsigned int actual;

if (file == NULL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is actually not needed.

DONE:
  free(bindir);
  if (res && check_crc && !verify_checksum(res, crc))
    return NULL;
  return res;

The file is guaranteed to be non NULL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Commit b84714a probably made the coverity warning disappeared already.

@@ -99,7 +99,7 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
while (true) {
buf[0] = '\0';
// From fs/proc/task_mmu.c:show_map_vma
if (fscanf(procmap, "%lx-%lx %s %llx %s %lu%[^\n]", &begin, &end, perm,
if (fscanf(procmap, "%lx-%lx %4s %llx %7s %lu%[^\n]", &begin, &end, perm,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should %7s to %5s since it MAJOR:MINOR format where MAJOR/MINOR are two byte width?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 7 because the buffer dev is of size 8. But a 6 bytes buffer would be enough. We don't do anything with it after the call to fscanf() anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then "7" is okay. thanks for explanation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to be sure, %Ns means reading at most N characters, right? Dev number could definitely be shorter.

Copy link
Member

@palmtenor palmtenor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in general, thank you!

@@ -435,8 +438,10 @@ static char *find_debug_via_debuglink(Elf *e, const char *binpath,

DONE:
free(bindir);
if (res && check_crc && !verify_checksum(res, crc))
if (res && check_crc && !verify_checksum(res, crc)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I just looked at Man page for dirname and it says

These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. Alternatively, they may return a pointer to some part of path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required.

So maybe the free(bindir) part is also not really correct per say?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked glibc code, For the path without '/' (e.g., a.out), a static memory inside dirname is returned.
It looks the best way is to declare an array, e.g., bindir[PATH_MAX] as the input to the bindir function.

This code will not execute in typical case. It will be executed if a separate debug file is specified, which is probably we won't hit the issue often.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The result of strdup(binpath) should be preserved in a tmp pointer and that pointer freed instead of bindir.

@@ -99,7 +99,7 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
while (true) {
buf[0] = '\0';
// From fs/proc/task_mmu.c:show_map_vma
if (fscanf(procmap, "%lx-%lx %s %llx %s %lu%[^\n]", &begin, &end, perm,
if (fscanf(procmap, "%lx-%lx %4s %llx %7s %lu%[^\n]", &begin, &end, perm,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to be sure, %Ns means reading at most N characters, right? Dev number could definitely be shorter.

@yonghong-song
Copy link
Collaborator

@jeromemarchand any update on this one?

@jeromemarchand
Copy link
Contributor Author

Did that last update addressed all of your concerns? I dropped the unneeded patch, reduced the dev buffer to 6 bytes and fixed the free()/dirname() issue.

@yonghong-song
Copy link
Collaborator

Sorry, I did not notice you made the change. The change actually looks good. Will test it now.

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

2 similar comments
@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@yonghong-song yonghong-song merged commit 415bd4e into iovisor:master Nov 1, 2018
@jeromemarchand jeromemarchand deleted the covscan2 branch August 5, 2019 16:02
CrackerCat pushed a commit to CrackerCat/bcc that referenced this pull request Jul 31, 2024
* Coverity #def53: COPY_PASTE_ERROR

* Coverity #def18: DC.STREAM_BUFFER. Double-check max length of dev

* Coverity #def44: MISSING_BREAK. This looks like it should be here

* Coverity #def67: STRING_NULL: potential OOB read if 0 bytes read.

* Coverity #def66: FORWARD_NULL: potential null ptr deref

* Coverity #def17: RESOURCE_LEAK: missing free()

* Dont free the result of dirname

dirname() may return pointers to statically allocated memory. Don't
free the pointer it returns.
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.

4 participants