-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathBufferAccess.qll
More file actions
351 lines (288 loc) · 10.1 KB
/
BufferAccess.qll
File metadata and controls
351 lines (288 loc) · 10.1 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import cpp
/**
* Returns the size of the pointed-to type, counting void types as size 1.
*/
int getPointedSize(Type t) {
result = t.getUnspecifiedType().(PointerType).getBaseType().getSize().maximum(1)
}
/**
* An operation that reads data from or writes data to a buffer.
*
* See the BufferWrite class for an explanation of how BufferAccess and
* BufferWrite differ.
*/
abstract class BufferAccess extends Expr {
BufferAccess() {
not this.isUnevaluated() and
//A buffer access must be reachable (not in dead code)
reachable(this)
}
abstract string getName();
/**
* Gets the expression that denotes the buffer, along with a textual label
* for it and an access type.
*
* accessType:
* - 1 = buffer range [0, getSize) is accessed entirely.
* - 2 = buffer range [0, getSize) may be accessed partially or entirely.
* - 3 = buffer is accessed at offset getSize - 1.
* - 4 = buffer is accessed with null terminator read protections
* (does not read past null terminator, regardless of access size)
*/
abstract Expr getBuffer(string bufferDesc, int accessType);
/**
* Gets the expression that represents the size of the buffer access. The
* actual size is typically the value of this expression multiplied by the
* result of `getSizeMult()`, in bytes.
*/
Expr getSizeExpr() { none() }
/**
* Gets a constant multiplier for the buffer access size given by
* `getSizeExpr`, in bytes.
*/
int getSizeMult() { none() }
/**
* Gets the buffer access size in bytes.
*/
int getSize() { result = this.getSizeExpr().getValue().toInt() * this.getSizeMult() }
}
/**
* Calls to memcpy and similar functions.
* memcpy(dest, src, num)
* wmemcpy(dest, src, num)
* memmove(dest, src, num)
* wmemmove(dest, src, num)
* mempcpy(dest, src, num)
* wmempcpy(dest, src, num)
* RtlCopyMemoryNonTemporal(dest, src, num)
*/
class MemcpyBA extends BufferAccess {
MemcpyBA() {
this.(FunctionCall).getTarget().getName() =
[
"memcpy", "wmemcpy", "memmove", "wmemmove", "mempcpy", "wmempcpy",
"RtlCopyMemoryNonTemporal"
]
}
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "destination buffer" and
accessType = 1
or
result = this.(FunctionCall).getArgument(1) and
bufferDesc = "source buffer" and
accessType = 1
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to bcopy.
* bcopy(src, dest, num)
*/
class BCopyBA extends BufferAccess {
BCopyBA() { this.(FunctionCall).getTarget().getName() = "bcopy" }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "source buffer" and
accessType = 1
or
result = this.(FunctionCall).getArgument(1) and
bufferDesc = "destination buffer" and
accessType = 1
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to strncpy.
* strncpy(dest, src, num)
*/
class StrncpyBA extends BufferAccess {
StrncpyBA() { this.(FunctionCall).getTarget().getName() = "strncpy" }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "destination buffer" and
accessType = 2
or
result = this.(FunctionCall).getArgument(1) and
bufferDesc = "source buffer" and
accessType = 4
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to memccpy.
* memccpy(dest, src, c, n)
*/
class MemccpyBA extends BufferAccess {
MemccpyBA() { this.(FunctionCall).getTarget().getName() = "memccpy" }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "destination buffer" and
accessType = 2
or
result = this.(FunctionCall).getArgument(1) and
bufferDesc = "source buffer" and
accessType = 2
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(3) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to memcmp and similar functions.
* memcmp(buffer1, buffer2, num)
* wmemcmp(buffer1, buffer2, num)
* _memicmp(buffer1, buffer2, count)
* _memicmp_l(buffer1, buffer2, count, locale)
*/
class MemcmpBA extends BufferAccess {
MemcmpBA() {
this.(FunctionCall).getTarget().getName() = ["memcmp", "wmemcmp", "_memicmp", "_memicmp_l"]
}
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "first buffer" and
accessType = 2
or
result = this.(FunctionCall).getArgument(1) and
bufferDesc = "second buffer" and
accessType = 2
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to swab and similar functions.
* swab(src, dest, num)
* _swab(src, dest, num)
*/
class SwabBA extends BufferAccess {
SwabBA() { this.(FunctionCall).getTarget().getName() = ["swab", "_swab"] }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "source buffer" and
accessType = 1
or
result = this.(FunctionCall).getArgument(1) and
bufferDesc = "destination buffer" and
accessType = 1
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to memset and similar functions.
* memset(dest, value, num)
* wmemset(dest, value, num)
*/
class MemsetBA extends BufferAccess {
MemsetBA() { this.(FunctionCall).getTarget().getName() = ["memset", "wmemset"] }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "destination buffer" and
accessType = 1
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to `RtlSecureZeroMemory`.
* RtlSecureZeroMemory(ptr, cnt)
*/
class ZeroMemoryBA extends BufferAccess {
ZeroMemoryBA() { this.(FunctionCall).getTarget().getName() = "RtlSecureZeroMemory" }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "destination buffer" and
accessType = 1
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(1) }
override int getSizeMult() { result = 1 }
}
/**
* Calls to memchr and similar functions.
* memchr(buffer, value, num)
* wmemchr(buffer, value, num)
*/
class MemchrBA extends BufferAccess {
MemchrBA() { this.(FunctionCall).getTarget().getName() = ["memchr", "wmemchr"] }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "source buffer" and
accessType = 2
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }
override int getSizeMult() {
result = getPointedSize(this.(FunctionCall).getTarget().getParameter(0).getType())
}
}
/**
* Calls to fread.
* fread(buffer, size, number, file)
*/
class FreadBA extends BufferAccess {
FreadBA() { this.(FunctionCall).getTarget().getName() = "fread" }
override string getName() { result = this.(FunctionCall).getTarget().getName() }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(FunctionCall).getArgument(0) and
bufferDesc = "destination buffer" and
accessType = 2
}
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(1) }
override int getSizeMult() { result = this.(FunctionCall).getArgument(2).getValue().toInt() }
}
/**
* A array access on a buffer:
* buffer[ix]
* but not:
* &buffer[ix]
*/
class ArrayExprBA extends BufferAccess, ArrayExpr {
ArrayExprBA() {
not exists(AddressOfExpr aoe | aoe.getAChild() = this) and
// exclude accesses in macro implementation of `strcmp`,
// which are carefully controlled but can look dangerous.
not exists(Macro m |
m.getName() = "strcmp" and
m.getAnInvocation().getAnExpandedElement() = this
)
}
override string getName() { result = "array indexing" }
override Expr getBuffer(string bufferDesc, int accessType) {
result = this.(ArrayExpr).getArrayBase() and
bufferDesc = "array" and
accessType = 3
}
override Expr getSizeExpr() { result = this.(ArrayExpr).getArrayOffset() }
override int getSize() {
// byte size of the buffer that would be required to support this
// access
result = (1 + this.getSizeExpr().getValue().toInt()) * this.getSizeMult()
}
override int getSizeMult() { result = this.(ArrayExpr).getType().getSize() }
}