Skip to content

C++: Recognize any non-overflowing arithmetic expression as a barrier for cpp/uncontrolled-arithmetic #6120

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

Merged
merged 3 commits into from
Jun 23, 2021

Conversation

MathiasVP
Copy link
Contributor

This should be one of the final low-hanging fruits for this query that's motivated by real-world FPs.

(Part of https://github.com/github/codeql-c-team/issues/272.)

@MathiasVP MathiasVP added the C++ label Jun 21, 2021
@MathiasVP MathiasVP requested a review from a team as a code owner June 21, 2021 12:11
@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Jun 21, 2021
@geoffw0
Copy link
Contributor

geoffw0 commented Jun 21, 2021

This is similar to something we did on another query recently, right?

@MathiasVP
Copy link
Contributor Author

This is similar to something we did on another query recently, right?

Yes. We did this in #5903. Once this PR is merged I'll pull the bounded predicate into a shared file and use it in both cpp/uncontrolled-allocation-size, cpp/uncontrolled-arithmetic and cpp/tainted-arithmetic.

@geoffw0
Copy link
Contributor

geoffw0 commented Jun 21, 2021

Once this PR is merged I'll pull the bounded predicate into a shared file and use it in both cpp/uncontrolled-allocation-size, cpp/uncontrolled-arithmetic and cpp/tainted-arithmetic.

Sounds good.

Can you point me to some LGTM projects for which this query has results?

@MathiasVP
Copy link
Contributor Author

Can you point me to some LGTM projects for which this query has results?

Our list of usual suspect has some results: https://lgtm.com/query/6937375284332555889/.

In terms of where this PR makes a difference, you can see an instance of this on the python-pillow/Pillow project if you compare the query without this change to the query after this change.

The other projects in that run are similar cases which sadly need a more syntactic barrier which I haven't added yet.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

A few comments.

Can we get rid of any of the boundedDiv cases in bounded now?

e instanceof UnaryArithmeticOperation or
e instanceof BinaryArithmeticOperation
) and
not convertedExprMightOverflow(e)
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm understanding correctly, this can in certain cases fall short of the specification "greatly reduces the range of possible values" - to give an extreme example the following is now a barrier:

x = x + 0; // this can't overflow

However this is pretty silly, in practice we expect the overwhelming majority of arithmetic either can cause an overflow (x = x + 1), or greatly narrows the range of values (x = x / RAND_MAX) ... or is working on an already constrained value (hopefully safely).

If my above understanding is accurate, this is a somewhat heuristic approach, but I think I'm happy with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. This whole predicate uses a bunch of heuristics.

@MathiasVP
Copy link
Contributor Author

Can we get rid of any of the boundedDiv cases in bounded now?

Unfortunately not. SimpleRangeAnalysis only handles division and right shifts when one of the operations is a constant. Whereas boundedDiv removes them regardless of whether or not they are constants.

@MathiasVP MathiasVP force-pushed the not-overflow-is-barrier-in-cwe-190 branch from bb6a2d3 to a611e76 Compare June 23, 2021 08:28
@MathiasVP
Copy link
Contributor Author

@geoffw0 I believe all your concerns have been addressed.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

Yep, thanks for the fixes, merging...

@geoffw0 geoffw0 merged commit 298f70f into github:main Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants