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

C++: Adds another redundant null check rule #3921

Merged
merged 9 commits into from Aug 27, 2020

Conversation

catenacyber
Copy link
Contributor

@catenacyber catenacyber commented Jul 7, 2020

CVE ID(s)

List the CVE ID(s) associated with this vulnerability. GitHub will automatically link CVE IDs to the GitHub Advisory Database.

Report

A potential NULL dereference (or redundant NULL check) may happen if a pointer is checked once against NULL in a function but not every time.

This is a new rule similar to RedundantNullCheckSimple, in addition to finding the NULL dereference from CVE-2020-10957 also finds the redundant null checks from :

This follows #3576 cc @jbj

I used Critical/MissingNullTest.ql as a start for finding the dereference (which does not use the Intermediate Representation)
Performance seems much better now, on a project like dovecot where I could find 2 redundant/missing null checks with this rule.

  • Are you planning to discuss this vulnerability submission publicly? (Blog Post, social networks, etc). We would love to have you spread the word about the good work you are doing

@catenacyber catenacyber requested review from hubwriter and a team as code owners Jul 7, 2020
@RasmusWL RasmusWL added the C++ label Jul 8, 2020
@RasmusWL RasmusWL changed the title Adds another redundant null check rule C++: Adds another redundant null check rule Jul 8, 2020
Copy link
Contributor

@jbj jbj left a comment

Thank you for the contribution. I look forward to seeing what results it has on a larger set of projects.

Please go through the steps in CONTRIBUTING.md -- in particular the steps where the query is moved to experimental and auto-formatted.

) and
//Simple test if the first access in this code path is dereferenced
not dereferenced(checked) and
blockDominates(checked.getEnclosingBlock(), unchecked.getEnclosingBlock())
Copy link
Contributor

@jbj jbj Jul 9, 2020

Choose a reason for hiding this comment

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

Suggested change
blockDominates(checked.getEnclosingBlock(), unchecked.getEnclosingBlock())
bbDominates(checked.getBasicBlock(), unchecked.getBasicBlock())

I'm guessing you're interested in BasicBlock (from the control-flow graph) rather than Block (from the tree of statements). A Block is a { ...; ...; } statement.

If so, you can delete the location-based blockDominates predicate.

Copy link
Contributor Author

@catenacyber catenacyber Jul 9, 2020

Choose a reason for hiding this comment

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

indeed, thanks

Copy link
Contributor Author

@catenacyber catenacyber Jul 10, 2020

Choose a reason for hiding this comment

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

In facts, it seems the basic block logic leads to false positives with condition like if (X && ptr == NULL)

Copy link
Contributor

@jbj jbj Aug 4, 2020

Choose a reason for hiding this comment

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

We'll have to investigate that if we ever want to promote this query out of experimental, but we can leave it as is for now.

@@ -0,0 +1,11 @@
void test(char *arg1, int *arg2) {
if (arg1[0] == 'A') {
if (arg2 != NULL) {
Copy link
Contributor

@jbj jbj Jul 9, 2020

Choose a reason for hiding this comment

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

Suggested change
if (arg2 != NULL) {
if (arg2 != NULL) { // redundant

Copy link
Contributor Author

@catenacyber catenacyber Jul 9, 2020

Choose a reason for hiding this comment

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

ok thanks

@jbj
Copy link
Contributor

jbj commented Jul 9, 2020

I'll be on holiday for the rest of July, so I'm assigning this PR over to @rdmarsh2.

@catenacyber
Copy link
Contributor Author

catenacyber commented Jul 9, 2020

I look forward to seeing what results it has on a larger set of projects.

Is there an easy way to test on all my Github repos ?

How can I get the ql file to be auto-formatted without Visual Code ?

@geoffw0
Copy link
Contributor

geoffw0 commented Jul 10, 2020

Is there an easy way to test on all my Github repos ?

If your projects are on LGTM and favourited you should be select multiple projects at the LGTM query console.

Does this link take you to an LGTM console set to run on 12 projects?

@catenacyber
Copy link
Contributor Author

catenacyber commented Jul 10, 2020

Does this link take you to an LGTM console set to run on 12 projects?

Yes :-D thank you very much for this pointer

@catenacyber
Copy link
Contributor Author

catenacyber commented Jul 10, 2020

So, running on my 12 projects found one more alert in ndpi
Not sure what to think of it :

There is indeed a NULL check here
https://github.com/ntop/nDPI/blob/dev/src/lib/ndpi_classify.c#L514
preceded by a call to a function using the pointer as an argument
https://github.com/ntop/nDPI/blob/dev/src/lib/ndpi_classify.c#L489
Function which does not check for this pointer NULLness explicitly
https://github.com/ntop/nDPI/blob/dev/src/lib/ndpi_classify.c#L263
And dereferences it
https://github.com/ntop/nDPI/blob/dev/src/lib/ndpi_classify.c#L298

I am not sure why dereferenced(unchecked) gets this case

@geoffw0
Copy link
Contributor

geoffw0 commented Jul 13, 2020

I am not sure why dereferenced(unchecked) gets this case

The definition of dereferenced appears to look inside function definitions to see if the corresponding parameter is dereferenced anywhere inside the function. Inside the call to ndpi_merge_splt_arrays the third parameter is dereferenced with pkt_len_twin[r].

@catenacyber
Copy link
Contributor Author

catenacyber commented Jul 13, 2020

The definition of dereferenced appears to look inside function definitions to see if the corresponding parameter is dereferenced anywhere inside the function

So, this is another true alert as far as I can tell :-)

@hubwriter
Copy link
Contributor

hubwriter commented Jul 21, 2020

Editorial check of the qhelp: 👍
LGTM

hubwriter
hubwriter previously approved these changes Jul 21, 2020
Copy link
Contributor

@jbj jbj left a comment

The query now complies with everything in https://github.com/github/codeql/blob/master/CONTRIBUTING.md except the auto-formatting requirement:

The queries and libraries must be autoformatted, for example using the "Format Document" command in CodeQL for Visual Studio Code.

@adityasharad adityasharad changed the base branch from master to main Aug 14, 2020
@catenacyber
Copy link
Contributor Author

catenacyber commented Aug 24, 2020

Thanks @jbj Did I get it right now ?

@jbj
Copy link
Contributor

jbj commented Aug 27, 2020

I've started the CI tests and will merge if they're clean.

jbj
jbj approved these changes Aug 27, 2020
@jbj jbj merged commit c507b33 into github:main Aug 27, 2020
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants