Skip to content

Commit 338e820

Browse files
committed
C++: Add a taint model for 'fopen' and accept test changes.
1 parent 201842d commit 338e820

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Fopen.qll

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import semmle.code.cpp.models.interfaces.Alias
77
import semmle.code.cpp.models.interfaces.SideEffect
88

99
/** The function `fopen` and friends. */
10-
private class Fopen extends Function, AliasFunction, SideEffectFunction {
10+
private class Fopen extends Function, AliasFunction, SideEffectFunction, TaintFunction {
1111
Fopen() {
1212
this.hasGlobalOrStdName(["fopen", "fopen_s", "freopen"])
1313
or
@@ -47,4 +47,19 @@ private class Fopen extends Function, AliasFunction, SideEffectFunction {
4747
i = 0 and
4848
buffer = true
4949
}
50+
51+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
52+
this.hasGlobalOrStdName(["fopen", "freopen", "_wfopen", "_fsopen", "_wfsopen"]) and
53+
input.isParameterDeref(0) and
54+
output.isReturnValueDeref()
55+
or
56+
// The out parameter is a pointer to a `FILE*`.
57+
this.hasGlobalOrStdName(["fopen_s"]) and
58+
input.isParameterDeref(1) and
59+
output.isParameterDeref(0, 2)
60+
or
61+
this.hasGlobalName(["_open", "_wopen"]) and
62+
input.isParameterDeref(0) and
63+
output.isReturnValue()
64+
}
5065
}

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6588,6 +6588,7 @@ WARNING: module 'TaintTracking' has been deprecated and may be removed in future
65886588
| taint.cpp:785:23:785:28 | source | taint.cpp:786:18:786:23 | source | |
65896589
| taint.cpp:785:23:785:28 | source | taint.cpp:790:15:790:20 | source | |
65906590
| taint.cpp:786:12:786:16 | call to fopen | taint.cpp:787:7:787:7 | f | |
6591+
| taint.cpp:786:18:786:23 | source | taint.cpp:786:12:786:16 | call to fopen | TAINT |
65916592
| taint.cpp:789:8:789:9 | f2 | taint.cpp:790:11:790:12 | f2 | |
65926593
| taint.cpp:789:8:789:9 | f2 | taint.cpp:791:7:791:8 | f2 | |
65936594
| taint.cpp:790:10:790:12 | ref arg & ... | taint.cpp:790:11:790:12 | f2 [inner post update] | |

cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,9 @@ int fopen_s(FILE** pFile, const char *filename, const char *mode);
784784

785785
void fopen_test(char* source) {
786786
FILE* f = fopen(source, "r");
787-
sink(f); // $ MISSING: ast,ir
787+
sink(f); // $ ast,ir
788788

789789
FILE* f2;
790790
fopen_s(&f2, source, "r");
791-
sink(f2); // $ ast MISSING: ir
791+
sink(f2); // $ ast,ir
792792
}

0 commit comments

Comments
 (0)