Visitar URL original
C++: Refactor ExecTainted.ql to only report results after string concatenation by rdmarsh2 · Pull Request #6184 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20260829154351/https://github.com/github/codeql/pull/6184
Skip to content

C++: Refactor ExecTainted.ql to only report results after string concatenation - #6184

Merged
jbj merged 29 commits into
mainfrom
rdmarsh2/improve-exec-tainted
Sep 29, 2021
Merged

C++: Refactor ExecTainted.ql to only report results after string concatenation#6184
jbj merged 29 commits into
mainfrom
rdmarsh2/improve-exec-tainted

Conversation

@rdmarsh2

Copy link
Copy Markdown
Contributor

This makes ExecTainted report results only when the tainted value does not become the start of the string which is eventually run as a shell command. The theory is that those cases are likely to be deliberate, and part of the expected threat model of the program (e.g. $CC in make). This lines up better with the results I considered fixable true positives in LGTM testing.

Opened as a draft because I have more work to do on testing, and it's in need of a rebase to clean up old commits and give before/after results for the newly added tests

@github-actions github-actions Bot added the C++ label Jun 30, 2021

@geoffw0 geoffw0 left a comment

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.

This looks promising but a few more changes are needed. After that I'd really like to see how results on LGTM will be affected.

Comment thread cpp/ql/src/Security/CWE/CWE-078/ExecTainted.ql Outdated
Comment thread cpp/ql/src/Security/CWE/CWE-078/ExecTainted.ql
Comment thread cpp/ql/src/Security/CWE/CWE-078/ExecTainted.ql
call.getStaticCallTarget() = op and
op.hasQualifiedName("std", "operator+") and
op.getType().(UserType).hasQualifiedName("std", "basic_string") and
call.getArgument(1) = rse.getArgumentOperand().getAnyDef() and // left operand

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.

Suggested change
call.getArgument(1) = rse.getArgumentOperand().getAnyDef() and // left operand
call.getArgument(1) = rse.getArgumentOperand().getAnyDef() and // right operand

Comment on lines +57 to +62
op.hasQualifiedName("std", "operator+") and
op.getType().(UserType).hasQualifiedName("std", "basic_string") and

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.

Whenever I see code like this I feel like the decision to make all of the models private was a mistake. IMO, I'd much rather want to write op instanceof StdStringPlus rather than this somewhat ugly (but necessary) conjunction.

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.

I agree, but that's something to take up elsewhere.

@rdmarsh2
rdmarsh2 dismissed a stale review via df4dc25 July 19, 2021 23:07
@rdmarsh2
rdmarsh2 force-pushed the rdmarsh2/improve-exec-tainted branch from 61df136 to df4dc25 Compare July 19, 2021 23:07
@rdmarsh2

Copy link
Copy Markdown
Contributor Author

rebased and responded to PR comments. New LGTM run: https://lgtm.com/query/1936393694543588922/

@geoffw0 geoffw0 left a comment

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.

QL and results otherwise LGTM.

I note the query now produces much fewer (and different) results to before. They appear to be high quality results, which is what we want.

Comment thread cpp/ql/src/semmle/code/cpp/commons/Printf.qll Outdated
@rdmarsh2
rdmarsh2 marked this pull request as ready for review July 22, 2021 00:04
@rdmarsh2
rdmarsh2 requested a review from a team as a code owner July 22, 2021 00:04
@rdmarsh2

Copy link
Copy Markdown
Contributor Author

I've added that QLDoc, raised the precision to high, and added a change note. Let me know if you want a bigger LGTM run or a medium precision.

@geoffw0

geoffw0 commented Jul 22, 2021 •

Copy link
Copy Markdown
Contributor

I'm happy with results and upgrading the precision. A CPP-Differences might be good, to confirm performance.

"This argument to an OS command is derived from $@ and then passed to " + callChain, taintSource,
"user input (" + taintCause + ")"
taintCause = sourceNode.getNode().(FlowSource).getSourceType() and
conf1.hasFlowPath(sourceNode, concatSink) and // TODO: can we link these better?

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.

Re: the TODO. I don't really see how. Did you have anything in mind?

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.

I was concerned about losing call contexts when we switch data flow configurations. I didn't see any false positives caused by it, and I don't think the dataflow API exposes call contexts on PathNode. If we get flow labels in the future, they'd be a cleaner solution to this problem.

}
}

class ExecTaintConfiguration extends TaintTracking::Configuration {

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.

Shouldn't this TaintTracking::Configuration be TaintTracking2::Configuration?

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, for performance. Unfortunately that breaks the combined paths I'd set up - I might be able to make it work with an IPA type but I'm not confident about it.

@rdmarsh2
rdmarsh2 force-pushed the rdmarsh2/improve-exec-tainted branch from 004aa29 to a7662a5 Compare July 22, 2021 19:38
@rdmarsh2

Copy link
Copy Markdown
Contributor Author

Started a CPP-Differences run: internal link

@rdmarsh2

Copy link
Copy Markdown
Contributor Author

rerunning the CPP-Differences: internal link

@geoffw0

geoffw0 commented Jul 29, 2021

Copy link
Copy Markdown
Contributor

Changes LGTM. Is performance OK?

@rdmarsh2

Copy link
Copy Markdown
Contributor Author

The last rerun here has slowdowns of 17% to 60%, all attributed to ExecTainted.ql. I think there's reevaluation going on, either of the IR, dataflow, or both.

@MathiasVP

MathiasVP commented Jul 30, 2021 •

Copy link
Copy Markdown
Contributor

The last rerun here has slowdowns of 17% to 60%, all attributed to ExecTainted.ql. I think there's reevaluation going on, either of the IR, dataflow, or both.

Looking at the profile viewer, the IR is definitely being re-evaluated, yeah. I would guess that a fix similar to #6347 would solve this.

@rdmarsh2

rdmarsh2 commented Aug 2, 2021

Copy link
Copy Markdown
Contributor Author

I would guess that a fix similar to #6347 would solve this.

So would I, but I haven't managed to find the right import order...

class ExecTaintConfiguration extends TaintTracking2::Configuration {
ExecTaintConfiguration() { this = "ExecTaintConfiguration" }

override predicate isSource(DataFlow::Node source) { interestingConcatenation(_, source) }

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.

Would it be better for performance if we implemented this isSource like this:

override predicate isSource(DataFlow::Node source) {
  exists(DataFlow::Node prevSink, TaintToConcatenationConfiguration conf |
    conf.hasFlow(_, prevSink) and
    interestingConcatenation(prevSink, source)
  )
}

so that we only compute flow from the sources that actually end up having flow from a FlowSource?

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.

Good call.

@rdmarsh2
rdmarsh2 force-pushed the rdmarsh2/improve-exec-tainted branch from e95d850 to def947d Compare August 23, 2021 16:59
@rdmarsh2
rdmarsh2 force-pushed the rdmarsh2/improve-exec-tainted branch from def947d to f50e4c5 Compare September 9, 2021 01:34
@rdmarsh2

rdmarsh2 commented Sep 9, 2021

Copy link
Copy Markdown
Contributor Author

Rebased to pick up #5032 and add forwarding for the new PathGraph::subpaths predicate, and started a new CPP-Differences

Robert Marsh added 5 commits September 15, 2021 10:55
This makes ExecTainted report results only when the tainted value does
not become the start of the string which is eventually run as a shell
command. The theory is that those cases are likely to be deliberate, and
part of the expected threat model of the program (e.g. $CC in make).
This lines up better with the results I considered fixable true
positives in LGTM testing
@rdmarsh2
rdmarsh2 force-pushed the rdmarsh2/improve-exec-tainted branch from 27fdf20 to c85cc14 Compare September 15, 2021 21:18
@geoffw0

geoffw0 commented Sep 17, 2021

Copy link
Copy Markdown
Contributor

Code changes LGTM.

CPP Differences LGTM. All differences appear to be in the BAD cases for CWE78_OS_Command_Injection, which is exactly what we want.

Checks are failing. I think these are due to the (now unnecessary) import of import semmle.code.cpp.security.FunctionWithWrappers in System.qll, bringing in the PrintableElement class from PrintAST.qll.

Performance - do we trust CPP-Differences for this or should I check locally?

@MathiasVP

Copy link
Copy Markdown
Contributor

Performance - do we trust CPP-Differences for this or should I check locally?

If you're referring to https://jenkins.internal.semmle.com/job/Changes/job/CPP-Differences/2244/ then indeed there is/was a problem. I think this is what Robert wrote about on Slack here:

ExecTainted... turns out semmle.code.cpp.security.CommandExecution adds to models that the IR uses and actually changes the semantics. I'll move it into the main models library tomorrow.

I think this has been fixed by a3e1f54, but we should run a fresh CPP-Differences to confirm this.

Robert Marsh added 3 commits September 17, 2021 12:16
This avoids a performance issue in DataFlowImpl::localFlowStep when the
DataFlow::Configuration subclasses in DefaultTaintTracking are active
in the same query as other Configuration
subclasses.
ResolveCall.qll is kept internal for the moment.
@geoffw0

geoffw0 commented Sep 22, 2021

Copy link
Copy Markdown
Contributor

Recent changes LGTM. ResolveCall looks like something we could begin to use more widely?

@MathiasVP

Copy link
Copy Markdown
Contributor

Recent changes LGTM. ResolveCall looks like something we could begin to use more widely?

Indeed. We've previously received a couple of requests to support function pointer resolution outside the context of global dataflow. Now we can actually point people to a public pretty interface instead of internal dataflow files that manipulate the IR directly 🎉.

@jbj

jbj commented Sep 28, 2021

Copy link
Copy Markdown
Contributor

All checks are passing! Is this PR ready for final review? If so, @rdmarsh2, please share links to

  • A DCA run to demonstrate that performance and results are alright on our set of test projects.
  • A test run of the query (with its supporting libraries if possible) on our sample of ~100 projects on lgtm.com. See Adding a new query.md in our team repo.

@rdmarsh2

Copy link
Copy Markdown
Contributor Author

Yes, it is.

DCA run: [https://github.com/github/codeql-dca-main/issues/508](internal link)
LGTM run: https://lgtm.com/query/4837668389671482937/

@rdmarsh2

Copy link
Copy Markdown
Contributor Author

I was concerned about losing call contexts when we switch data flow configurations. I didn't see any false positives caused by it, and I don't think the dataflow API exposes call contexts on PathNode. If we get flow labels in the future, they'd be a cleaner solution to this problem.

The result on WohlSoft/Moondust-Project in the latest LGTM run is a false positive caused by this...

@jbj

jbj commented Sep 29, 2021

Copy link
Copy Markdown
Contributor

I'll also point out (and this is totally not a matter of vanity 😇) that the result on jbj/magicrescue is a FP. I see you've added test7, which demonstrates the same kind of FP.

@jbj
jbj merged commit 45cf634 into main Sep 29, 2021
@jbj
jbj deleted the rdmarsh2/improve-exec-tainted branch September 29, 2021 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants