Skip to content

C++: Add more random sources in cpp/uncontrolled-arithmetic#6154

Merged
MathiasVP merged 9 commits intogithub:mainfrom
MathiasVP:more-random-sources-in-uncontrolled-arithmetic
Jul 12, 2021
Merged

C++: Add more random sources in cpp/uncontrolled-arithmetic#6154
MathiasVP merged 9 commits intogithub:mainfrom
MathiasVP:more-random-sources-in-uncontrolled-arithmetic

Conversation

@MathiasVP
Copy link
Copy Markdown
Contributor

This PR adds a couple more sources of randomness to cpp/uncontrolled-arithmetic.

I thought this would be a super trivial thing to do, but then I found Microsoft's rand_s function. That one behaves pretty much like rand (enough so that we actually want to include it in the query), but with the crucial difference that it returns its result by updating a pointer that it's been passed. This makes it difficult to model using the old TaintTrackingConfiguration library.

So as a bonus this PR also updates the query to use a TaintTracking::Configuration 🎉 .

Now that we have more sources in this query, I also noticed a couple new false positives on LGTM. See for instance the result on chakra-core/ChakraCore here. This PR fixes these false positives by recognizing all bitwise operations as barriers.
The idea is that, if a source of randomness is used in a bitwise operation, it's probably because the expression is used as a bit pattern, and thus overflow isn't a concern.

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

@MathiasVP MathiasVP added the C++ label Jun 24, 2021
@MathiasVP MathiasVP requested a review from a team as a code owner June 24, 2021 14:00
@MathiasVP MathiasVP added the depends on internal PR This PR should only be merged in sync with an internal Semmle PR label Jun 25, 2021
@MathiasVP
Copy link
Copy Markdown
Contributor Author

CPP Language Tests are failing due to changes in the internal repo. I've created an internal PR to fix it. That one should be merged with this one.

Copy link
Copy Markdown
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.

LGTM.

I wondered if it might be worth recognizing boost::random, the stuff in <random>, or common Mersenne Twister implementations (that were popular before all this stuff was standardized)? I'm leaning towards "no" however, because they're all more complex cases than rand and friends, and I'm not sure the payoff would be worth it.

this.getNumberOfParameters() = 1
}

override FunctionOutput getFunctionOutput() { result.isParameterDeref(0) }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The model of rand_s is nice and simple. 👍

exists(int n |
source.asDefiningArgument() = call.getArgument(n) and
rand.getFunctionOutput().isParameterDeref(n)
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's a shame we don't have a helper to convert Call + FunctionOutput-> Expr (to be clear, I'm not asking for one right now).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree. It would definitely be a good thing to have this. I guess we include such a predicate in semmle.code.cpp.models.interfaces.FunctionInputsAndOutputs.qll at some point.

Copy link
Copy Markdown
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.

All LGTM now.

@MathiasVP MathiasVP merged commit 6a11aa7 into github:main Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ depends on internal PR This PR should only be merged in sync with an internal Semmle PR documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants