-
Notifications
You must be signed in to change notification settings - Fork 1.8k
CPP: Add query for detecteing incorrect error checking for scanf #14910
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
Conversation
QHelp previews: cpp/ql/src/Critical/IncorrectCheckScanf.qhelpIncorrect return-value check for a 'scanf'-like functionThis query finds calls of Functions in the RecommendationEnsure that all uses of ExampleThe following examples show different ways of guarding a {
int i, j;
// BAD: The result is only checked against zero
if (scanf("%d %d", &i, &j)) {
use(i);
use(j);
}
// BAD: The result is only checked against zero
if (scanf("%d %d", &i, &j) == 0) {
i = 0;
j = 0;
}
use(i);
use(j);
if (scanf("%d %d", &i, &j) == 2) {
// GOOD: the result is checked against 2
}
// GOOD: the result is compared directly
int r = scanf("%d %d", &i, &j);
if (r < 2) {
return;
}
if (r == 1) {
j = 0;
}
}
References
|
9016d42
to
3e9aeac
Compare
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.
This looks really good! I only have a couple of small nits, but otherwise I think this looks good to go 🚀
Co-authored-by: Mathias Vorreiter Pedersen <mathiasvp@github.com>
cpp/ql/src/change-notes/2023-12-04-incorrectly-checked-scanf.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Mathias Vorreiter Pedersen <mathiasvp@github.com>
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.
This LGTM! Let's wait for docs before merging this, though. I'll add the label now.
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.
Looks great, only really minor comments/suggestions, thank you!
Co-authored-by: Ben Ahmady <32935794+subatoi@users.noreply.github.com>
efce099
to
c883ce8
Compare
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.
All looks great, thank you!
This implements a new query for checking for a specific case of incorrect scanf use.
Specifically it looks for use of
*scanf
where the return value is checked against zero but not against-1
. The scanf functions return-1
when reaching end of input before any of the placeholders. Only rejecting zero means that-1
is treated as a success but the results aren't set.Known false positives:
/proc
or/sys
or parsing/etc/passwd
or simply data that is shipped with the program. Failure shouldn't be possible in these cases. However in these cases the user is still checking for an impossible error condition and ignoring the other.-1
.The general FP rate is a bit high considering securty bugs but I think the fact that many of the security FPs are correctness issues anyway makes it good enough.
The results from this query are excluded from missing-scanf-check to avoid double reporting. The total number may go down as this query reports on the sscanf but that query reports on the use of the output.