Skip to content

C++: Fix missing bounds in range analysis #10555

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 4 commits into from
Sep 23, 2022

Conversation

MathiasVP
Copy link
Contributor

This PR adds bounds for the following case:

void foo(char* p, unsigned index, char* end) {
  if(p + index < end) {
    use(*(p + index)); // <-- In here we now know that `p + index` is bounded by `end`
  }
}

This is motivated by CVE-2016-10160, and the first commit adds a reduced version of that vulnerability (and indeed after this PR we correctly identify this CVE).

The reason we didn't find the issue in the first commit is as follows:

Currently, on main, the range-analysis library didn't spot that the pointer-arithmetic expression p + index in the p + index > end guard is identical to the one being used for the store operation in p[index] = '\0', and so we didn't realize that there's an off-by-one error here.

In contrast, when we're storing the result of the pointer-arithmetic expression in a local variable q, we correctly catch this.

The second commit fixes this by generating a new SSAVariable representing pointer-arithmetic expressions appearing in guards, and using GVN to connect the p + index in the guard with the pointer-arithmetic expression computing the address of p[index].

(And for completeness: The last commit accepts the test changes.)

@MathiasVP MathiasVP requested a review from a team as a code owner September 23, 2022 11:22
@github-actions github-actions bot added the C++ label Sep 23, 2022
@MathiasVP MathiasVP requested a review from rdmarsh2 September 23, 2022 11:44
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.

2 participants