Skip to content
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

Memory Out-Of-Bounds Access Analysis #1094

Merged
merged 41 commits into from
Sep 28, 2023

Conversation

mrstanb
Copy link
Member

@mrstanb mrstanb commented Jun 27, 2023

This PR is for the addition of an analysis for the detection of memory out-of-bounds (OOB) accesses.

@mrstanb mrstanb changed the title [WIP] Mem oob analysis Memory OOB access analysis Jul 8, 2023
@mrstanb
Copy link
Member Author

mrstanb commented Jul 8, 2023

I did a rewrite of the majority of the logic that I had previously added for the memory OOB access checks. Still need to try and make it more precise.

Also, for some reason I can't get the BlobSize query for a pointer, which has dynamically allocated memory, to give me a meaningful result (i.e., not top). We should probably discuss this and see what's wrong there. I'm pretty sure I'm doing something wrong in my code.

@michael-schwarz Nonetheless, feel free to take a look at the PR and leave some feedback :)

@mrstanb mrstanb requested a review from sim642 August 20, 2023 17:15
@mrstanb
Copy link
Member Author

mrstanb commented Aug 20, 2023

Did yet another rewrite of the logic for this PR:

  • make test runs through successfully.
  • An open issue that's left is handling memory OOB accesses due to loops, such as the one in test case 77/03

Feel free to review

Copy link
Member

@michael-schwarz michael-schwarz left a comment

Choose a reason for hiding this comment

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

We discussed that this should happen at he address level, potentially using the mechanism by @jerhard from #967. Also, it should only warn for dereferences, and not already when constructing such invalid pointers.

Copy link
Member

@michael-schwarz michael-schwarz left a comment

Choose a reason for hiding this comment

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

Other than the small remarks, LGTM!

@mrstanb
Copy link
Member Author

mrstanb commented Sep 6, 2023

As a workaround, I would suggest disabling the info warnings category for this test with --disable warn.info, leaving a comment that this is not a longterm solution.

This is now added in 2b45499

@michael-schwarz
Copy link
Member

It seems like the current implementation gets confused by string constants, causing quite a few normally simple CWE tests to fail, because they contain code like this:

// PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval
#include <stdlib.h>
#include <stdio.h>

void myPrint(char *str) {
    printf("%s", str);
}

int main(int argc, char const *argv[]) {
    myPrint("starting test");
    // Some stuff
    myPrint("ending test");    
}
[Warning][Unknown] Pointer str has an empty points-to-set (tests/regression/77-mem-oob/04-oob-deref-after-ptr-arith.c:6:5-6:22)
[Warning][Behavior > Undefined > MemoryOutOfBoundsAccess][CWE-823] Size of pointer str not known. Memory out-of-bounds access might occur due to pointer arithmetic (tests/regression/77-mem-oob/04-oob-deref-after-ptr-arith.c:6:5-6:22)

This should be easy to fix as we have a special representation of such string constants in the address domain, for which we could just consider all accesses to be within bound.

@mrstanb
Copy link
Member Author

mrstanb commented Sep 13, 2023

It seems like the current implementation gets confused by string constants, causing quite a few normally simple CWE tests to fail, because they contain code like this:

// PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval
#include <stdlib.h>
#include <stdio.h>

void myPrint(char *str) {
    printf("%s", str);
}

int main(int argc, char const *argv[]) {
    myPrint("starting test");
    // Some stuff
    myPrint("ending test");    
}
[Warning][Unknown] Pointer str has an empty points-to-set (tests/regression/77-mem-oob/04-oob-deref-after-ptr-arith.c:6:5-6:22)
[Warning][Behavior > Undefined > MemoryOutOfBoundsAccess][CWE-823] Size of pointer str not known. Memory out-of-bounds access might occur due to pointer arithmetic (tests/regression/77-mem-oob/04-oob-deref-after-ptr-arith.c:6:5-6:22)

This should be easy to fix as we have a special representation of such string constants in the address domain, for which we could just consider all accesses to be within bound.

This should be now fixed by commit 3258cce in the staging_memsafety branch. If you think it makes sense, we can cherry-pick this, as well as other commits from the staging-memsafety branch, which have introduced some changes and fixes to memOutOfBounds.ml

@michael-schwarz
Copy link
Member

If you think it makes sense, we can cherry-pick this, as well as other commits from the staging-memsafety branch, which have introduced some changes and fixes to memOutOfBounds.ml

Probably a good idea, yes!

@mrstanb
Copy link
Member Author

mrstanb commented Sep 26, 2023

This should be now fixed by commit 3258cce in the staging_memsafety branch. If you think it makes sense, we can cherry-pick this, as well as other commits from the staging-memsafety branch, which have introduced some changes and fixes to memOutOfBounds.ml

We discussed that these changes will be taken after this PR's branch is merged into master, since cherry-picking them now would cause a lot of conflicts.

@mrstanb mrstanb marked this pull request as ready for review September 26, 2023 15:12
@mrstanb
Copy link
Member Author

mrstanb commented Sep 27, 2023

The CI job on MacOS is failing again, this time due to the 04/58 regr. test case. I'm strongly assuming that it's due to the issue, documented in #1151. Should we temporarily change some annotations of this test case as well?

@michael-schwarz
Copy link
Member

The CI job on MacOS is failing again, this time due to the 04/58 regr. test case.

Probably fixed as of 7f9ec9a. I'll merge master into this again and if the CI passes then, I'll merge the PR.

@mrstanb
Copy link
Member Author

mrstanb commented Sep 28, 2023

The CI job on MacOS is failing again, this time due to the 04/58 regr. test case.

Probably fixed as of 7f9ec9a. I'll merge master into this again and if the CI passes then, I'll merge the PR.

Awesome, thanks!

@michael-schwarz michael-schwarz merged commit 6701f5d into goblint:master Sep 28, 2023
10 checks passed
michael-schwarz added a commit that referenced this pull request Sep 28, 2023
@sim642 sim642 added this to the SV-COMP 2024 milestone Nov 15, 2023
sim642 added a commit to sim642/opam-repository that referenced this pull request Nov 24, 2023
nberth pushed a commit to nberth/opam-repository that referenced this pull request Jun 18, 2024
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.

None yet

3 participants