-
Notifications
You must be signed in to change notification settings - Fork 84
Add out-of-bounds check for memset and memcpy
#1197
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
michael-schwarz
merged 23 commits into
goblint:master
from
mrstanb:memset-memcpy-size-check
Oct 1, 2023
+312
−87
Merged
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f0c73c5
Add support for 3rd param of memcpy
mrstanb a2c99ad
Merge branch 'master' into memset-memcpy-size-check
mrstanb 6e7c00e
Add out-of-bounds check for memset and memcpy
mrstanb 5ed769f
Add regr. test case for memset and memcpy out-of-bounds
mrstanb 12bee27
Add temporary fix for failing MacOS CI job
mrstanb 391c6ce
Change memset/memcpy count warning from must to may
mrstanb fae7256
Use ID.lt to compare dest size with count
mrstanb 655362e
Use join to combine all points-to elements sizes
mrstanb 2534945
Do not ingore offsets when calling BlobSize for memset/memcpy
mrstanb 033913b
Change name to get_size_of_dest which makes more sense
mrstanb 4ec9895
Add exception handling for ID operations
mrstanb 1e69b64
Fix wrong name use
mrstanb 49dcac9
Fix incompatible ikinds
mrstanb 87dadd2
Add more and more sophisticated memset and memcpy tests
mrstanb 9e9b5e3
Add memset/memcpy test case with arrays
mrstanb 5ca357d
Fix some bugs in memOutOfBounds and move memset/memcpy checks there
mrstanb 8bf3705
Remove unused function
mrstanb 3027910
Add further exception handling
mrstanb 62b20a2
Add further memset/memcpy test
mrstanb 5e683d4
Fix comment
mrstanb f03f81f
Remove some bot checks for ID arithmetic
mrstanb 3da9205
Use size_of_type_in_bytes where possible
mrstanb 89abb1a
Keep TODO about count of memset in base.ml
mrstanb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval --disable warn.info | ||
| // TODO: The "--disable warn.info" part is a temporary fix and needs to be removed once the MacOS CI job is fixed | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| typedef struct s { | ||
| int a; | ||
| char b; | ||
| } s; | ||
|
|
||
| int main(int argc, char const *argv[]) { | ||
| int *a = malloc(10 * sizeof(int)); //Size is 40 bytes, assuming a 4-byte int | ||
|
|
||
| memset(a, 0, 40); //NOWARN | ||
| memset(a, 0, 10 * sizeof(int)); //NOWARN | ||
| memset(a, 0, 41); //WARN | ||
| memset(a, 0, 40000000); //WARN | ||
|
|
||
| int d; | ||
|
|
||
| if (argc == 15) { | ||
| int c = 55; | ||
| a = &c; | ||
| memset(a, 0, argv[5]); //WARN | ||
| } else if (argv[2] == 2) { | ||
| a = &d; | ||
| } | ||
|
|
||
| memset(a, 0, 40); //WARN | ||
|
|
||
| int input; | ||
| scanf("%d", &input); | ||
| memset(a, 0, input); //WARN | ||
|
|
||
|
|
||
|
|
||
| int *b = malloc(15 * sizeof(int)); //Size is 60 bytes, assuming a 4-byte int | ||
| memset(b, 0, 60); //NOWARN | ||
| b += 1; | ||
| memset(b, 0, 60); //WARN | ||
|
|
||
|
|
||
|
|
||
| s *s_ptr = malloc(sizeof(s)); | ||
| memset(s_ptr, 0, sizeof(s)); //NOWARN | ||
| memset(s_ptr->a, 0, sizeof(s)); //WARN | ||
| memset(s_ptr->b, 0, sizeof(s)); //WARN | ||
|
|
||
| s_ptr = s_ptr->a; | ||
| memset(s_ptr, 0, sizeof(s)); //WARN | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval --disable warn.info | ||
| // TODO: The "--disable warn.info" part is a temporary fix and needs to be removed once the MacOS CI job is fixed | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| typedef struct s { | ||
| int a; | ||
| char b; | ||
| } s; | ||
|
|
||
| int main(int argc, char const *argv[]) { | ||
| int *a = malloc(10 * sizeof(int)); //Size is 40 bytes, assuming a 4-byte int | ||
| int *b = malloc(15 * sizeof(int)); //Size is 60 bytes, assuming a 4-byte int | ||
|
|
||
| memcpy(a, b, 40); //NOWARN | ||
| memcpy(a, b, 10 * sizeof(int)); //NOWARN | ||
| memcpy(a, b, 41); //WARN | ||
| memcpy(a, b, 40000000); //WARN | ||
| memcpy(a, b, 15 * sizeof(int)); //WARN | ||
|
|
||
| int d; | ||
|
|
||
| if (*argv == 42) { | ||
| a = &d; | ||
| } else if (*(argv + 5)) { | ||
| int random = rand(); | ||
| a = &random; | ||
| memcpy(a, b, 40); //WARN | ||
| } | ||
|
|
||
| memcpy(a, b, 40); //WARN | ||
| memcpy(a, b, sizeof(a)); //WARN | ||
|
|
||
| memcpy(b, a, 60); //NOWARN | ||
| b += 1; | ||
| memcpy(b, a, 60); //WARN | ||
|
|
||
|
|
||
| s *s_ptr = malloc(sizeof(s)); | ||
| memcpy(s_ptr, a, sizeof(s)); //NOWARN | ||
| memcpy(s_ptr->a, 0, sizeof(s)); //WARN | ||
| memcpy(s_ptr->b, 0, sizeof(s)); //WARN | ||
|
|
||
| memcpy(s_ptr, a, 40); //WARN | ||
| memcpy(s_ptr, a, 60); //WARN | ||
| memcpy(s_ptr, b, 40); //WARN | ||
| memcpy(s_ptr, b, 60); //WARN | ||
|
|
||
| s_ptr = s_ptr->b; | ||
| memcpy(s_ptr, a, sizeof(s)); //WARN | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval --disable warn.info | ||
| // TODO: The "--disable warn.info" part is a temporary fix and needs to be removed once the MacOS CI job is fixed | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| int main(int argc, char const *argv[]) { | ||
| int arr[42]; // Size should be 168 bytes (with 4 byte ints) | ||
| int *b = arr; | ||
|
|
||
|
|
||
| memset(b, 0, 168); //NOWARN | ||
| memset(b, 0, sizeof(arr)); //NOWARN | ||
| memset(b, 0, 169); //WARN | ||
| memset(b, 0, sizeof(arr) + 1); //WARN | ||
|
|
||
| int *c = malloc(sizeof(arr)); // Size should be 168 bytes (with 4 byte ints) | ||
| memcpy(b, c, 168); //NOWARN | ||
| memcpy(b, c, sizeof(arr)); //NOWARN | ||
| memcpy(b, c, 169); //WARN | ||
| memcpy(b, c, sizeof(arr) + 1); //WARN | ||
|
|
||
| int d; | ||
|
|
||
| if (*argv == 42) { | ||
| b = &d; | ||
| memset(b, 0, 168); //WARN | ||
| memcpy(b, c, 168); //WARN | ||
| } else if (*(argv + 5)) { | ||
| int random = rand(); | ||
| b = &random; | ||
| memset(b, 0, 168); //WARN | ||
| memcpy(b, c, 168); //WARN | ||
| } | ||
|
|
||
| memset(b, 0, sizeof(arr)); //WARN | ||
| memcpy(b, c, sizeof(arr)); //WARN | ||
| memset(b, 0, sizeof(int)); //NOWARN | ||
| memcpy(b, c, sizeof(int)); //NOWARN | ||
| memset(b, 0, sizeof(int) + 1); //WARN | ||
| memcpy(b, c, sizeof(int) + 1); //WARN | ||
|
|
||
| return 0; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.