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

Fix issues reported by Coverity - v1 #5260

Closed
wants to merge 2 commits into from
Closed

Conversation

satta
Copy link
Contributor

@satta satta commented Aug 3, 2020

Link to redmine ticket: https://redmine.openinfosecfoundation.org/issues/3855

Describe changes:

  • Initialize structs u8da and u32da. This is basically just to please static checkers, values set at the beginning will always be overwritten or not used afterwards.
  • Rework error handling in some MQTT keyword parsing code to unify the path to the error case.
  • Use the more idiomatic SCStrdup() instead of using standard library functions.

Please note that I currently do not have access to the Coverity service, so this was a best effort to address the issue the checker is raising.

This is meant to provide a single path to the error case.
This might help make things more clear for static
checkers.
@satta satta requested a review from a team as a code owner August 3, 2020 08:49
de->username = de->password = de->will = MQTT_DONT_CARE;
de->will_retain = de->clean_session = MQTT_DONT_CARE;

char copy[strlen(rawstr)+1];
strlcpy(copy, rawstr, sizeof(copy));
copy = SCStrdup(rawstr);
Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand the goal here. Why is the stack copy not working?

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 is working. I wanted to move the code path to use goto error everywhere to make sure that Coverity sees the complete lifetime of de. However, the compiler won't allow us to goto into a scope with a variably sized declaration (copy). So I chose to manage the memory manually here.

int ret = 0;
int ov[MAX_SUBSTRINGS];

ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
if (ret < 1) {
SCLogError(SC_ERR_PCRE_MATCH, "invalid flag definition: %s", rawstr);
return NULL;
goto error;
Copy link
Member

Choose a reason for hiding this comment

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

I feel this use of the error label is adding unnecessary complexity. Its not going to do anything there, but now we need to follow this jump logic to realize this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just to address Coverity's complaint about us checking de for NULL in the previous code -- it thinks that it might be NULL but it has been dereferenced before. Apparently it didn't catch that in that case we already returned from the function before in that case.

Copy link
Member

Choose a reason for hiding this comment

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

I think it tells you there is an issue: by checking it can be NULL, it assumes the programmer "knows" that it can be. But in all the paths leading to the NULL check its already dereferenced. So it alerts you that you may have a bug before the null check. Of course it were smarter it would see that de was already checked for 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.

Got it. So you agree we should not try to work around that and remove the check in these few cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #5261

}

de = SCCalloc(1, sizeof(DetectMQTTConnectFlagsData));
if (unlikely(de == NULL))
return NULL;
goto error;
Copy link
Member

Choose a reason for hiding this comment

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

returning NULL here was fine. I think the issue coverity flagged is that after this we don't have to check de != NULL anymore

Copy link
Contributor Author

@satta satta Aug 3, 2020

Choose a reason for hiding this comment

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

True. That check was requested in a previous review though -- I would be happy to remove it in these 3 cases as I agree it it only complicates things. OK?

@satta satta closed this Aug 4, 2020
@satta satta deleted the coverity-3855-v1 branch August 4, 2020 16:58
victorjulien added a commit to victorjulien/suricata that referenced this pull request Apr 11, 2022
victorjulien added a commit to victorjulien/suricata that referenced this pull request Apr 11, 2022
jasonish pushed a commit to jasonish/suricata that referenced this pull request Apr 18, 2022
Bug: OISF#5260.
(cherry picked from commit 93d5bce)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants