Skip to content

Commit f8fed26

Browse files
committed
C++: Exclude results that are used as file names.
1 parent 1d58218 commit f8fed26

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

cpp/ql/src/Security/CWE/CWE-311/CleartextFileWrite.ql

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,37 @@ import cpp
1515
import semmle.code.cpp.security.SensitiveExprs
1616
import semmle.code.cpp.security.FileWrite
1717
import semmle.code.cpp.dataflow.DataFlow
18+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
19+
20+
/**
21+
* An operation on a filename.
22+
*/
23+
predicate filenameOperation(FunctionCall op, Expr path) {
24+
exists(string name | name = op.getTarget().getName() |
25+
name =
26+
[
27+
"remove", "unlink", "rmdir", "rename", "fopen", "open", "freopen", "_open", "_wopen",
28+
"_wfopen", "_fsopen", "_wfsopen", "chmod", "chown", "stat", "lstat", "fstat", "access", "_access", "_waccess", "_access_s", "_waccess_s"
29+
] and
30+
path = op.getArgument(0)
31+
or
32+
name = ["fopen_s", "wfopen_s", "rename"] and
33+
path = op.getArgument(1)
34+
)
35+
}
36+
37+
predicate isFileName(GVN gvn)
38+
{
39+
exists(FunctionCall op, Expr path |
40+
filenameOperation(op, path) and
41+
gvn = globalValueNumber(path)
42+
)
43+
}
1844

1945
from FileWrite w, SensitiveExpr source, Expr dest
2046
where
2147
DataFlow::localFlow(DataFlow::exprNode(source), DataFlow::exprNode(w.getASource())) and
22-
dest = w.getDest()
48+
dest = w.getDest() and
49+
not isFileName(globalValueNumber(source)) // file names are not passwords
2350
select w, "This write into file '" + dest.toString() + "' may contain unencrypted data from $@",
2451
source, "this source."

cpp/ql/test/query-tests/Security/CWE/CWE-311/semmle/tests/CleartextFileWrite.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
| test2.cpp:57:2:57:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:57:39:57:49 | call to getPassword | this source. |
77
| test2.cpp:65:3:65:9 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:62:18:62:25 | password | this source. |
88
| test2.cpp:79:2:79:8 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:79:36:79:43 | password | this source. |
9-
| test2.cpp:84:4:84:10 | call to fprintf | This write into file 'log' may contain unencrypted data from $@ | test2.cpp:84:50:84:63 | passwd_config2 | this source. |
109
| test.cpp:45:3:45:7 | call to fputs | This write into file 'file' may contain unencrypted data from $@ | test.cpp:45:9:45:19 | thePassword | this source. |
1110
| test.cpp:70:35:70:35 | call to operator<< | This write into file 'mystream' may contain unencrypted data from $@ | test.cpp:70:38:70:48 | thePassword | this source. |
1211
| test.cpp:73:37:73:41 | call to write | This write into file 'mystream' may contain unencrypted data from $@ | test.cpp:73:43:73:53 | thePassword | this source. |

cpp/ql/test/query-tests/Security/CWE/CWE-311/semmle/tests/test2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void tests(FILE *log, myStruct &s)
8181
{
8282
if (fopen(s.passwd_config2, "rt") == 0)
8383
{
84-
fprintf(log, "could not open file '%s'.\n", s.passwd_config2); // GOOD [FALSE POSITIVE]
84+
fprintf(log, "could not open file '%s'.\n", s.passwd_config2); // GOOD
8585
}
8686
}
8787

0 commit comments

Comments
 (0)