-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Add a cpp/invalid-pointer-deref
query to experimental
#10438
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d981f89
C++: Add flow states to the product dataflow library.
MathiasVP b8a5aa5
C++: Fix a couple of range analysis issues:
MathiasVP 4482669
C++: Add a new 'InvalidPointerDeref' query to experimental.
MathiasVP 031f20a
C++: Respond to review comments.
MathiasVP dc00643
C++: More QLDoc.
MathiasVP 78535dc
C++: Autoformat.
MathiasVP d1cf688
C++: Fix test function naming.
MathiasVP 3e6576b
C++: Add example of missing result.
MathiasVP 0207607
C++: Add more comments.
MathiasVP 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
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
26 changes: 26 additions & 0 deletions
26
cpp/ql/src/experimental/Security/CWE/CWE-193/InvalidPointerDeref.cpp
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,26 @@ | ||
void *malloc(unsigned); | ||
unsigned get_size(); | ||
void write_data(const unsigned char*, const unsigned char*); | ||
|
||
int main(int argc, char* argv[]) { | ||
unsigned size = get_size(); | ||
|
||
{ | ||
unsigned char *begin = (unsigned char*)malloc(size); | ||
if(!begin) return -1; | ||
|
||
unsigned char* end = begin + size; | ||
write_data(begin, end); | ||
*end = '\0'; // BAD: Out-of-bounds write | ||
} | ||
|
||
{ | ||
unsigned char *begin = (unsigned char*)malloc(size); | ||
if(!begin) return -1; | ||
|
||
unsigned char* end = begin + size; | ||
write_data(begin, end); | ||
*(end - 1) = '\0'; // GOOD: writing to the last byte | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
cpp/ql/src/experimental/Security/CWE/CWE-193/InvalidPointerDeref.qhelp
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,31 @@ | ||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
<overview> | ||
<p>The program performs an out-of-bounds read or write operation. In addition to causing program instability, techniques exist which may allow an attacker to use this vulnerability to execute arbitrary code.</p> | ||
|
||
</overview> | ||
<recommendation> | ||
|
||
<p>Ensure that pointer dereferences are properly guarded to ensure that they cannot be used to read or write past the end of the allocation.</p> | ||
|
||
</recommendation> | ||
<example> | ||
<p>The first example allocates a buffer of size <code>size</code> and creates a local variable that stores the location that is one byte past the end of the allocation. | ||
This local variable is then dereferenced which results in an out-of-bounds write. | ||
The second example subtracts one from the <code>end</code> variable before dereferencing it. This subtraction ensures that the write correctly updates the final byte of the allocation.</p> | ||
<sample src="InvalidPointerDeref.cpp" /> | ||
|
||
</example> | ||
<references> | ||
|
||
<li>CERT C Coding Standard: | ||
<a href="https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts">ARR30-C. Do not form or use out-of-bounds pointers or array subscripts</a>.</li> | ||
<li> | ||
OWASP: | ||
<a href="https://owasp.org/www-community/vulnerabilities/Buffer_Overflow">Buffer Overflow</a>. | ||
</li> | ||
|
||
</references> | ||
</qhelp> |
Oops, something went wrong.
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.