-
Notifications
You must be signed in to change notification settings - Fork 2k
CPP: Split PotentiallyDangerousFunction.ql #1315
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
11 commits
Select commit
Hold shift + click to select a range
1f80dea
CPP: Clone PotentiallyDangerousFunction query as DangerousUseOfGets.
geoffw0 88f363d
CPP: Update the ql, qhelp and example.
geoffw0 fc5e7e5
CPP: Test the new query.
geoffw0 9da2ead
CPP: Remove redundant tests.
geoffw0 2ae38a5
CPP: Adjust query tags.
geoffw0 34444ac
CPP: Update the query name and description.
geoffw0 dc44d68
CPP: Update CWE tag.
geoffw0 eb880c3
CPP: Reference URLs.
geoffw0 2e932b6
CPP: Change notes.
geoffw0 76d18b4
CPP: Repair getQualifiedName changes from elsewhere.
geoffw0 6c267f4
CPP: Fix qhelp.
geoffw0 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
15 changes: 15 additions & 0 deletions
15
cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.c
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,15 @@ | ||
| #define BUFFERSIZE (1024) | ||
|
|
||
| // BAD: using gets | ||
| void echo_bad() { | ||
| char buffer[BUFFERSIZE]; | ||
| gets(buffer); | ||
| printf("Input was: '%s'\n", buffer); | ||
| } | ||
|
|
||
| // GOOD: using fgets | ||
| void echo_good() { | ||
| char buffer[BUFFERSIZE]; | ||
| fgets(buffer, BUFFERSIZE, stdin); | ||
| printf("Input was: '%s'\n", buffer); | ||
| } |
42 changes: 42 additions & 0 deletions
42
cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.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,42 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p>This rule finds calls to the <code>gets</code> function, which is dangerous and | ||
| should not be used. See <strong>Related | ||
| rules</strong> below for rules that identify other dangerous functions.</p> | ||
|
|
||
| <p>The <code>gets</code> function is one of the vulnerabilities exploited by the Internet Worm of 1988, one of the first computer worms to spread through the Internet. The <code>gets</code> function provides no way to limit the amount of data that is read and stored, so without prior knowledge of the input it is impossible to use it safely with any size of buffer.</p> | ||
|
|
||
| </overview> | ||
| <recommendation> | ||
|
|
||
| <p>Replace calls to <code>gets</code> with <code>fgets</code>, specifying the maximum length to copy. This will prevent the buffer overflow.</p> | ||
|
|
||
| </recommendation> | ||
| <example> | ||
| <p>The following example gets a string from standard input in two ways:</p> | ||
| <sample src="DangerousUseOfGets" /> | ||
|
|
||
| <p>The first version uses <code>gets</code> and will overflow if the input | ||
| is longer than the buffer. The second version of the code | ||
| uses <code>fgets</code> and will not overflow, because the amount of data | ||
| written is limited by the length parameter.</p> | ||
| </example> | ||
| <section title="Related rules"> | ||
| <p>Other dangerous functions identified by CWE-676 ("Use of | ||
| Potentially Dangerous Function") include <code>strcpy</code> | ||
| and <code>strcat</code>. Use of these functions is highlighted by | ||
| rules for the following CWEs:</p> | ||
| <ul> | ||
| <li><a href="https://cwe.mitre.org/data/definitions/120.html">CWE-120 Classic Buffer Overflow</a>. | ||
| </li><li><a href="https://cwe.mitre.org/data/definitions/131.html">CWE-131 Incorrect Calculation of Buffer Size</a>. | ||
| </li></ul> | ||
|
|
||
| </section> | ||
| <references> | ||
| <li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Morris_worm">Morris worm</a>.</li> | ||
| <li>E. Spafford. <i>The Internet Worm Program: An Analysis</i>. Purdue Technical Report CSD-TR-823, <a href="http://www.textfiles.com/100/tr823.txt">(online)</a>, 1988.</li> | ||
| </references> | ||
| </qhelp> |
18 changes: 18 additions & 0 deletions
18
cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.ql
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,18 @@ | ||
| /** | ||
| * @name Use of dangerous function | ||
| * @description Use of a standard library function that does not guard against buffer overflow. | ||
| * @kind problem | ||
| * @problem.severity error | ||
| * @precision very-high | ||
| * @id cpp/dangerous-function-overflow | ||
| * @tags reliability | ||
| * security | ||
| * external/cwe/cwe-242 | ||
| */ | ||
| import cpp | ||
|
|
||
| from FunctionCall call, Function target | ||
| where | ||
| call.getTarget() = target and | ||
| target.hasGlobalName("gets") | ||
| select call, "gets does not guard against buffer overflow" |
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
File renamed without changes.
1 change: 1 addition & 0 deletions
1
cpp/ql/test/query-tests/Security/CWE/CWE-242/semmle/tests/DangerousFunctionOverflow.qlref
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 @@ | ||
| Security/CWE/CWE-676/DangerousFunctionOverflow.ql |
1 change: 0 additions & 1 deletion
1
cpp/ql/test/query-tests/Security/CWE/CWE-242/semmle/tests/PotentiallyDangerousFunction.qlref
This file was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
...curity/CWE/CWE-676/semmle/PotentiallyDangerousFunction/DangerousFunctionOverflow.expected
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,2 @@ | ||
| | test.c:42:2:42:5 | call to gets | gets does not guard against buffer overflow | | ||
| | test.c:43:6:43:9 | call to gets | gets does not guard against buffer overflow | |
1 change: 1 addition & 0 deletions
1
.../Security/CWE/CWE-676/semmle/PotentiallyDangerousFunction/DangerousFunctionOverflow.qlref
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 @@ | ||
| Security/CWE/CWE-676/DangerousFunctionOverflow.ql |
2 changes: 0 additions & 2 deletions
2
...ity/CWE/CWE-676/semmle/PotentiallyDangerousFunction/PotentiallyDangerousFunction.expected
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 |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| | test.c:31:22:31:27 | call to gmtime | Call to gmtime is potentially dangerous | | ||
| | test.c:42:2:42:5 | call to gets | gets does not guard against buffer overflow | | ||
| | test.c:43:6:43:9 | call to gets | gets does not guard against buffer overflow | | ||
| | test.c:48:19:48:27 | call to localtime | Call to localtime is potentially dangerous | | ||
| | test.c:49:22:49:26 | call to ctime | Call to ctime is potentially dangerous | | ||
| | test.c:50:23:50:29 | call to asctime | Call to asctime is potentially dangerous | |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 242 the correct CWE?
676?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this one should be 676 I think. Updated.