Skip to content

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Nov 22, 2018

Add a little bit of dataflow to the OverflowStatic.ql query, just enough to catch the cases described in https://jira.semmle.com/browse/CPP-299 (I was wrong when I said BadlyBoundedWrite.ql should catch these examples - because they are reads, and reads are plainly out of the scope of that query).

In the long run (for 1.20 or 1.21?) I'd like to go a lot further:

  • splitting OverflowStatic.ql into three parts, one for each of the already distinct modes evident in the QL, so that they can be individually turned on/off/tagged/worked on.
  • the wrongBufferSize part is largely a weaker version of the BadlyBoundedWrite.ql query except that it looks at reads in addition to writes. I think a better design would be to create a new BadlyBoundedRead.ql query such that BadlyBoundedRead + BadlyBoundedWrite cover everything we already cover (and more).
  • similarly for the other two parts (overflowOffsetInLoop and outOfBounds), either merge them into an existing query or make whatever improvements seem appropriate.

If there's agreement I'll create a JIRA issue for this plan.

@geoffw0 geoffw0 added the C++ label Nov 22, 2018
@geoffw0 geoffw0 requested a review from a team as a code owner November 22, 2018 11:01
@sj
Copy link
Collaborator

sj commented Nov 22, 2018

@geoffw0: fully agree with your plan, could you please send me a ping as soon as you've opened a Jira issue?

@sj
Copy link
Collaborator

sj commented Nov 22, 2018

I've added some more pointers for further discussion in CPP-299. It'd be great to hear @geoffw0's thoughts on that issue before this PR gets merged.

}
int statedSizeValue() {
exists(Expr statedSizeSrc |
DataFlow::localFlowStep*(DataFlow::exprNode(statedSizeSrc), DataFlow::exprNode(statedSizeExpr())) and
Copy link
Contributor

Choose a reason for hiding this comment

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

You can write localFlow instead of localFlowStep*.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

error = call.statedSize() and
statedSize = call.statedSizeValue() and
statedSize > bufsize and
error = call.statedSizeExpr() and
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that you've introduced data flow, we need to decide what to do when multiple values may flow to error. With this PR we'll start seeing false positives in code like

char buf10[10], buf20[20];
if (b) { size = 10; } else { size = 20; }
if (b) {
  memcpy(buf10, src, size); // false positive
} else {
  memcpy(buf20, src, size);
}

You can be more conservative, and still address CPP-299, by replacing call.statedSizeValue() with min(call.statedSizeValue()). That won't address https://github.com/MosheWagner/VulnC/blob/f787c51fbe09d2fb88403d3b01bca7709fd7ed9e/src/bad_read.c#L158-L169, but I would prefer to err on the side of false negatives until we run a CPP-Differences job and see that such FPs are rare in practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, makes sense to be conservative at this time. Done.

@geoffw0
Copy link
Contributor Author

geoffw0 commented Nov 22, 2018

Jira issue created here: https://jira.semmle.com/browse/CPP-304

@geoffw0
Copy link
Contributor Author

geoffw0 commented Nov 22, 2018

Rebased to fix merge conflict (in change note).

@jbj jbj merged commit 4ad5923 into github:master Nov 23, 2018
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.

3 participants