forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStreams.qll
More file actions
203 lines (176 loc) · 5.84 KB
/
FileStreams.qll
File metadata and controls
203 lines (176 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* A module for modeling classes and functions related to file streams in the C++ Standard Library.
*
* A file stream is a template instantiation of `std::basic_fstream`.
* This module identifies such instantiations as `FileStream`s. It also models various features of
* file streams, including:
* - Calls to various `fstream` functions (`open()`,`close()`, `seekp()` etc.).
* - Calls to `fstream` insertion and extraction operators (`operator<<`, `operator>>`).
* - Sources of file stream objects `FileStreamSource`
*/
import cpp
private import semmle.code.cpp.dataflow.DataFlow
private import semmle.code.cpp.dataflow.TaintTracking
private import codingstandards.cpp.Operator
/**
* A `basic_fstream` like `std::fstream`
*/
class FileStream extends ClassTemplateInstantiation {
FileStream() { this.getTemplate().hasQualifiedName("std", "basic_fstream") }
}
/**
* A `basic_istream` like `std::istream`
*/
class IStream extends Type {
IStream() {
this.(Class).getQualifiedName().matches("std::basic\\_istream%")
or
this.getUnspecifiedType() instanceof IStream
or
this.(Class).getABaseClass() instanceof IStream
or
this.(ReferenceType).getBaseType() instanceof IStream
}
}
/**
* A `basic_ostream` like `std::ostream`
*/
class OStream extends Type {
OStream() {
this.(Class).getQualifiedName().matches("std::basic\\_ostream%")
or
this.getUnspecifiedType() instanceof OStream
or
this.(Class).getABaseClass() instanceof OStream
or
this.(ReferenceType).getBaseType() instanceof OStream
}
}
/**
* A call to a `FileStream` function.
*/
abstract class FileStreamFunctionCall extends FunctionCall {
//returns the affected fstream
abstract Expr getFStream();
}
predicate sameStreamSource(FileStreamFunctionCall a, FileStreamFunctionCall b) {
exists(FileStreamSource c |
c.getAUse() = a.getFStream() and
c.getAUse() = b.getFStream()
)
}
/**
* Insertion `operator<<` and Extraction `operator>>` operators.
*/
class InsertionOperatorCall extends FileStreamFunctionCall {
InsertionOperatorCall() { this.getTarget() instanceof StreamInsertionOperator }
override Expr getFStream() {
result = this.getQualifier()
or
result = this.getArgument(0) and this.getNumberOfArguments() = 2
}
}
class ExtractionOperatorCall extends FileStreamFunctionCall {
ExtractionOperatorCall() { this.getTarget() instanceof StreamExtractionOperator }
override Expr getFStream() {
result = this.getQualifier()
or
result = this.getArgument(0) and this.getNumberOfArguments() = 2
}
}
/**
* A call to `open` functions.
*/
class OpenFunctionCall extends FileStreamFunctionCall {
OpenFunctionCall() { getTarget().hasQualifiedName("std", "basic_fstream", "open") }
override Expr getFStream() { result = getQualifier() }
}
/**
* A call to `close` functions.
*/
class CloseFunctionCall extends FileStreamFunctionCall {
CloseFunctionCall() {
this.getTarget().hasQualifiedName("std", "basic_fstream", "close") or
this.getTarget().hasGlobalName("fclose")
}
override Expr getFStream() {
result = getQualifier()
or
result = this.getArgument(0) and this.getNumberOfArguments() = 1
}
}
/**
* A call to `std:basic_istream:seekg`, `std:basic_istream:seekg`,
* `fflush`,`fseek`,`fsetpos`,`rewind` functions.
*/
class FileStreamPositioningCall extends FileStreamFunctionCall {
FileStreamPositioningCall() {
this.getTarget().hasQualifiedName("std", "basic_istream", "seekg") or
this.getTarget().hasQualifiedName("std", "basic_ostream", "seekp")
}
override Expr getFStream() { result = getQualifier() }
}
/**
* A call to `istream` or `ostream` functions.
*/
class IOStreamFunctionCall extends FileStreamFunctionCall {
IOStreamFunctionCall() {
this.getTarget().getDeclaringType() instanceof IStream
or
this.getTarget().getDeclaringType() instanceof OStream
}
override Expr getFStream() {
result = getQualifier()
or
result = getArgument(0) and getNumberOfArguments() = 2
}
}
/**
* A source of a `FileStream` object.
*/
abstract class FileStreamSource extends Element {
/** A use of this source stream. */
abstract Expr getAUse();
}
/**
* A `FileStream` created by a `ConstructorCall`.
*/
class FileStreamConstructorCall extends FileStreamSource, Expr {
FileStreamConstructorCall() {
this.(ConstructorCall).getTarget().getDeclaringType().getABaseClass*() instanceof FileStream
}
override Expr getAUse() {
FileStreamConstructorCallUseFlow::flow(DataFlow::exprNode(this), DataFlow::exprNode(result))
}
}
/**
* A `FileStream` defined externally, and which therefore cannot be tracked as a source by taint tracking.
*/
class FileStreamExternGlobal extends FileStreamSource, GlobalOrNamespaceVariable {
FileStreamExternGlobal() { this.getType().stripType() instanceof FileStream }
override Expr getAUse() {
// Defined externally, so assume any access is a use.
result = getAnAccess()
}
}
/**
* A global taint tracking configuration to track `FileStream` uses in the program.
*/
private module FileStreamConstructorCallUseConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source.asExpr() instanceof FileStreamConstructorCall }
predicate isSink(DataFlow::Node sink) {
sink.asExpr().getType().stripType() instanceof FileStream
}
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
// By default we do not get flow from ConstructorFieldInit expressions to accesses
// of the field in other member functions, so we add it explicitly here.
exists(ConstructorFieldInit cfi, Field f |
cfi.getTarget() = f and
f.getType().stripType() instanceof FileStream and
node1.asExpr() = cfi.getExpr() and
node2.asExpr() = f.getAnAccess()
)
}
}
private module FileStreamConstructorCallUseFlow =
TaintTracking::Global<FileStreamConstructorCallUseConfig>;