Skip to content

Commit e7da53d

Browse files
authored
Merge pull request github#17715 from MathiasVP/fopen-taint
C++: Add taint through `fopen`
2 parents 3ef49f3 + acac3a0 commit e7da53d

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added taint flow model for `fopen` and related functions.

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

Lines changed: 19 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,22 @@ 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+
(
53+
this.hasGlobalOrStdName(["fopen", "freopen"]) or
54+
this.hasGlobalName(["_wfopen", "_fsopen", "_wfsopen"])
55+
) and
56+
input.isParameterDeref(0) and
57+
output.isReturnValueDeref()
58+
or
59+
// The out parameter is a pointer to a `FILE*`.
60+
this.hasGlobalOrStdName("fopen_s") and
61+
input.isParameterDeref(1) and
62+
output.isParameterDeref(0, 2)
63+
or
64+
this.hasGlobalName(["_open", "_wopen"]) and
65+
input.isParameterDeref(0) and
66+
output.isReturnValue()
67+
}
5068
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6584,6 +6584,16 @@ WARNING: module 'TaintTracking' has been deprecated and may be removed in future
65846584
| taint.cpp:767:21:767:24 | ref arg path | taint.cpp:768:8:768:11 | path | |
65856585
| taint.cpp:768:8:768:11 | path | taint.cpp:768:7:768:11 | * ... | |
65866586
| taint.cpp:778:37:778:42 | call to source | taint.cpp:779:7:779:9 | obj | |
6587+
| taint.cpp:785:23:785:28 | source | taint.cpp:785:23:785:28 | source | |
6588+
| taint.cpp:785:23:785:28 | source | taint.cpp:786:18:786:23 | source | |
6589+
| taint.cpp:785:23:785:28 | source | taint.cpp:790:15:790:20 | source | |
6590+
| 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 |
6592+
| taint.cpp:789:8:789:9 | f2 | taint.cpp:790:11:790:12 | f2 | |
6593+
| taint.cpp:789:8:789:9 | f2 | taint.cpp:791:7:791:8 | f2 | |
6594+
| taint.cpp:790:10:790:12 | ref arg & ... | taint.cpp:790:11:790:12 | f2 [inner post update] | |
6595+
| taint.cpp:790:10:790:12 | ref arg & ... | taint.cpp:791:7:791:8 | f2 | |
6596+
| taint.cpp:790:11:790:12 | f2 | taint.cpp:790:10:790:12 | & ... | |
65876597
| vector.cpp:16:43:16:49 | source1 | vector.cpp:17:26:17:32 | source1 | |
65886598
| vector.cpp:16:43:16:49 | source1 | vector.cpp:31:38:31:44 | source1 | |
65896599
| vector.cpp:17:21:17:33 | call to vector | vector.cpp:19:14:19:14 | v | |

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,16 @@ TaintInheritingContentObject source(bool);
777777
void test_TaintInheritingContent() {
778778
TaintInheritingContentObject obj = source(true);
779779
sink(obj.flowFromObject); // $ ir MISSING: ast
780+
}
781+
782+
FILE* fopen(const char*, const char*);
783+
int fopen_s(FILE** pFile, const char *filename, const char *mode);
784+
785+
void fopen_test(char* source) {
786+
FILE* f = fopen(source, "r");
787+
sink(f); // $ ast,ir
788+
789+
FILE* f2;
790+
fopen_s(&f2, source, "r");
791+
sink(f2); // $ ast,ir
780792
}

0 commit comments

Comments
 (0)