-
Notifications
You must be signed in to change notification settings - Fork 2k
CPP: Add query for CWE-377 Insecure Temporary File #6947
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
8e8a324
Add files via upload
ihsinme 3f3988c
Add files via upload
ihsinme 235a3ec
Update InsecureTemporaryFile.qhelp
ihsinme 432fc74
Apply suggestions from code review
ihsinme 2574aa8
Update InsecureTemporaryFile.ql
ihsinme c8a4a8b
Update InsecureTemporaryFile.ql
ihsinme cedc5fd
Update InsecureTemporaryFile.ql
ihsinme 55fe010
Update InsecureTemporaryFile.ql
ihsinme 289d587
Update InsecureTemporaryFile.ql
ihsinme 7514fe2
Update test.cpp
ihsinme a044824
Update InsecureTemporaryFile.expected
ihsinme 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
14 changes: 14 additions & 0 deletions
14
cpp/ql/src/experimental/Security/CWE/CWE-377/InsecureTemporaryFile.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,14 @@ | ||
| ... | ||
| fp = fopen("/tmp/name.tmp","w"); // BAD | ||
| ... | ||
| char filename = tmpnam(NULL); | ||
| fp = fopen(filename,"w"); // BAD | ||
| ... | ||
|
|
||
| strcat (filename, "/tmp/name.XXXXXX"); | ||
| fd = mkstemp(filename); | ||
| if ( fd < 0 ) { | ||
| return error; | ||
| } | ||
| fp = fdopen(fd,"w") // GOOD | ||
| ... |
23 changes: 23 additions & 0 deletions
23
cpp/ql/src/experimental/Security/CWE/CWE-377/InsecureTemporaryFile.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,23 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p>Working with a file, without checking its existence and its rights, as well as working with names that can be predicted, may not be safe. Requires the attention of developers.</p> | ||
|
|
||
| </overview> | ||
|
|
||
| <example> | ||
| <p>The following example demonstrates erroneous and corrected work with file.</p> | ||
| <sample src="InsecureTemporaryFile.cpp" /> | ||
|
|
||
| </example> | ||
| <references> | ||
|
|
||
| <li> | ||
| CERT C Coding Standard: | ||
| <a href="https://wiki.sei.cmu.edu/confluence/display/c/CON33-C.+Avoid+race+conditions+when+using+library+functions">CON33-C. Avoid race conditions when using library functions</a>. | ||
| </li> | ||
|
|
||
| </references> | ||
| </qhelp> |
112 changes: 112 additions & 0 deletions
112
cpp/ql/src/experimental/Security/CWE/CWE-377/InsecureTemporaryFile.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,112 @@ | ||
| /** | ||
| * @name Insecure generation of filenames. | ||
| * @description Using a predictable filename when creating a temporary file can lead to an attacker-controlled input. | ||
| * @kind problem | ||
| * @id cpp/insecure-generation-of-filename | ||
| * @problem.severity warning | ||
| * @precision medium | ||
| * @tags correctness | ||
| * security | ||
| * external/cwe/cwe-377 | ||
| */ | ||
|
|
||
| import cpp | ||
| import semmle.code.cpp.valuenumbering.GlobalValueNumbering | ||
|
|
||
| /** Holds for a function `f` that has an argument at index `apos` used to read the file. */ | ||
| predicate numberArgumentRead(Function f, int apos) { | ||
| f.hasGlobalOrStdName("fgets") and apos = 2 | ||
| or | ||
| f.hasGlobalOrStdName("fread") and apos = 3 | ||
| or | ||
| f.hasGlobalOrStdName("read") and apos = 0 | ||
| or | ||
| f.hasGlobalOrStdName("fscanf") and apos = 0 | ||
| } | ||
|
|
||
| /** Holds for a function `f` that has an argument at index `apos` used to write to file */ | ||
| predicate numberArgumentWrite(Function f, int apos) { | ||
| f.hasGlobalOrStdName("fprintf") and apos = 0 | ||
| or | ||
| f.hasGlobalOrStdName("fputs") and apos = 1 | ||
| or | ||
| f.hasGlobalOrStdName("write") and apos = 0 | ||
| or | ||
| f.hasGlobalOrStdName("fwrite") and apos = 3 | ||
| or | ||
| f.hasGlobalOrStdName("fflush") and apos = 0 | ||
| } | ||
|
|
||
| from FunctionCall fc, string msg | ||
| where | ||
| // search for functions for generating a name, without a guarantee of the absence of a file during the period of work with it. | ||
| ( | ||
| fc.getTarget().hasGlobalOrStdName("tmpnam") or | ||
| fc.getTarget().hasGlobalOrStdName("tmpnam_s") or | ||
| fc.getTarget().hasGlobalOrStdName("tmpnam_r") | ||
| ) and | ||
| not exists(FunctionCall fctmp | | ||
| ( | ||
| fctmp.getTarget().hasGlobalOrStdName("mktemp") or | ||
| fctmp.getTarget().hasGlobalOrStdName("mkstemp") or | ||
| fctmp.getTarget().hasGlobalOrStdName("mkstemps") or | ||
| fctmp.getTarget().hasGlobalOrStdName("mkdtemp") | ||
| ) and | ||
| ( | ||
| fc.getBasicBlock().getASuccessor*() = fctmp.getBasicBlock() or | ||
| fctmp.getBasicBlock().getASuccessor*() = fc.getBasicBlock() | ||
| ) | ||
| ) and | ||
| msg = | ||
| "Finding the name of a file that does not exist does not mean that it will not be exist at the next operation." | ||
| or | ||
| // finding places to work with a file without setting permissions, but with predictable names. | ||
| ( | ||
|
ihsinme marked this conversation as resolved.
|
||
| fc.getTarget().hasGlobalOrStdName("fopen") or | ||
| fc.getTarget().hasGlobalOrStdName("open") | ||
| ) and | ||
| fc.getNumberOfArguments() = 2 and | ||
| exists(FunctionCall fctmp, int i | | ||
| numberArgumentWrite(fctmp.getTarget(), i) and | ||
| globalValueNumber(fc) = globalValueNumber(fctmp.getArgument(i)) | ||
| ) and | ||
| not exists(FunctionCall fctmp, int i | | ||
| numberArgumentRead(fctmp.getTarget(), i) and | ||
| globalValueNumber(fc) = globalValueNumber(fctmp.getArgument(i)) | ||
| ) and | ||
| exists(FunctionCall fctmp | | ||
| ( | ||
| fctmp.getTarget().hasGlobalOrStdName("strcat") or | ||
| fctmp.getTarget().hasGlobalOrStdName("strcpy") | ||
| ) and | ||
| globalValueNumber(fc.getArgument(0)) = globalValueNumber(fctmp.getAnArgument()) | ||
| or | ||
| fctmp.getTarget().hasGlobalOrStdName("getenv") and | ||
| globalValueNumber(fc.getArgument(0)) = globalValueNumber(fctmp) | ||
| or | ||
| ( | ||
| fctmp.getTarget().hasGlobalOrStdName("asprintf") or | ||
| fctmp.getTarget().hasGlobalOrStdName("vasprintf") or | ||
| fctmp.getTarget().hasGlobalOrStdName("xasprintf") or | ||
| fctmp.getTarget().hasGlobalOrStdName("xvasprintf ") | ||
| ) and | ||
| exists(Variable vrtmp | | ||
| vrtmp = fc.getArgument(0).(VariableAccess).getTarget() and | ||
| vrtmp = fctmp.getArgument(0).(AddressOfExpr).getAddressable().(Variable) and | ||
| not vrtmp instanceof Field | ||
| ) | ||
| ) and | ||
| not exists(FunctionCall fctmp | | ||
| ( | ||
| fctmp.getTarget().hasGlobalOrStdName("umask") or | ||
| fctmp.getTarget().hasGlobalOrStdName("fchmod") or | ||
| fctmp.getTarget().hasGlobalOrStdName("chmod") | ||
| ) and | ||
| ( | ||
| fc.getBasicBlock().getASuccessor*() = fctmp.getBasicBlock() or | ||
| fctmp.getBasicBlock().getASuccessor*() = fc.getBasicBlock() | ||
| ) | ||
| ) and | ||
| msg = | ||
| "Creating a file for writing without evaluating its existence and setting permissions can be unsafe." | ||
| select fc, msg | ||
2 changes: 2 additions & 0 deletions
2
...experimental/query-tests/Security/CWE/CWE-377/semmle/tests/InsecureTemporaryFile.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.cpp:16:20:16:25 | call to tmpnam | Finding the name of a file that does not exist does not mean that it will not be exist at the next operation. | | ||
| | test.cpp:42:8:42:12 | call to fopen | Creating a file for writing without evaluating its existence and setting permissions can be unsafe. | |
1 change: 1 addition & 0 deletions
1
...st/experimental/query-tests/Security/CWE/CWE-377/semmle/tests/InsecureTemporaryFile.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 @@ | ||
| experimental/Security/CWE/CWE-377/InsecureTemporaryFile.ql |
68 changes: 68 additions & 0 deletions
68
cpp/ql/test/experimental/query-tests/Security/CWE/CWE-377/semmle/tests/test.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,68 @@ | ||
| typedef int FILE; | ||
| #define NULL (0) | ||
| FILE *fopen(char *filename, const char *mode); | ||
| FILE *fdopen(int handle, char *mode); | ||
| char * tmpnam(char * name); | ||
| int mkstemp(char * name); | ||
| char * strcat(char *str1, const char *str2); | ||
| int umask(int pmode); | ||
| int chmod(char * filename,int pmode); | ||
| int fprintf(FILE *fp,const char *fmt, ...); | ||
| int fclose(FILE *stream); | ||
|
|
||
| int funcTest1() | ||
| { | ||
| FILE *fp; | ||
| char *filename = tmpnam(NULL); // BAD | ||
| fp = fopen(filename,"w"); | ||
| fprintf(fp,"%s\n","data to file"); | ||
| fclose(fp); | ||
| return 0; | ||
| } | ||
|
|
||
| int funcTest2() | ||
| { | ||
| FILE *fp; | ||
| int fd; | ||
| char filename[80]; | ||
| strcat (filename, "/tmp/name.XXXXXX"); | ||
| fd = mkstemp(filename); | ||
| if ( fd < 0 ) { | ||
| return 1; | ||
| } | ||
| fp = fdopen(fd,"w"); // GOOD | ||
| return 0; | ||
| } | ||
|
|
||
| int funcTest3() | ||
| { | ||
| FILE *fp; | ||
| char filename[80]; | ||
| strcat(filename, "/tmp/tmp.name"); | ||
| fp = fopen(filename,"w"); // BAD | ||
| fprintf(fp,"%s\n","data to file"); | ||
| fclose(fp); | ||
| return 0; | ||
| } | ||
|
|
||
| int funcTest4() | ||
| { | ||
| FILE *fp; | ||
| char filename[80]; | ||
| umask(0022); | ||
| strcat(filename, "/tmp/tmp.name"); | ||
| fp = fopen(filename,"w"); // GOOD | ||
| chmod(filename,0666); | ||
| fprintf(fp,"%s\n","data to file"); | ||
| fclose(fp); | ||
| return 0; | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| funcTest1(); | ||
| funcTest2(); | ||
| funcTest3(); | ||
| funcTest4(); | ||
| 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.