Skip to content

Commit 3cfb0a2

Browse files
committed
C++: Fix Iterator.qll taint/data flows for operator+=.
1 parent 61b0d6a commit 3cfb0a2

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

cpp/ql/src/semmle/code/cpp/models/implementations/Iterator.qll

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,15 @@ private class IteratorAssignArithmeticOperator extends Operator, DataFlowFunctio
167167
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
168168
input.isParameter(0) and
169169
output.isReturnValue()
170-
or
171-
input.isParameterDeref(0) and output.isReturnValueDeref()
172170
}
173171

174172
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
173+
input.isParameterDeref(0) and output.isReturnValueDeref()
174+
or
175+
// reverse flow from returned reference to the object referenced by the first parameter
176+
input.isReturnValueDeref() and
177+
output.isParameterDeref(0)
178+
or
175179
input.isParameterDeref(1) and
176180
output.isParameterDeref(0)
177181
}
@@ -224,9 +228,7 @@ private class IteratorCrementMemberOperator extends MemberFunction, DataFlowFunc
224228
* A member `operator->` function for an iterator type.
225229
*/
226230
private class IteratorFieldMemberOperator extends Operator, TaintFunction {
227-
IteratorFieldMemberOperator() {
228-
this.getClassAndName("operator->") instanceof Iterator
229-
}
231+
IteratorFieldMemberOperator() { this.getClassAndName("operator->") instanceof Iterator }
230232

231233
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
232234
input.isQualifierObject() and
@@ -260,14 +262,18 @@ private class IteratorAssignArithmeticMemberOperator extends MemberFunction, Dat
260262
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
261263
input.isQualifierAddress() and
262264
output.isReturnValue()
263-
or
264-
input.isReturnValueDeref() and
265-
output.isQualifierObject()
266265
}
267266

268267
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
269268
input.isQualifierObject() and
270269
output.isReturnValueDeref()
270+
or
271+
// reverse flow from returned reference to the qualifier
272+
input.isReturnValueDeref() and
273+
output.isQualifierObject()
274+
or
275+
input.isParameterDeref(0) and
276+
output.isQualifierObject()
271277
}
272278
}
273279

@@ -276,9 +282,7 @@ private class IteratorAssignArithmeticMemberOperator extends MemberFunction, Dat
276282
*/
277283
private class IteratorArrayMemberOperator extends MemberFunction, TaintFunction,
278284
IteratorReferenceFunction {
279-
IteratorArrayMemberOperator() {
280-
this.getClassAndName("operator[]") instanceof Iterator
281-
}
285+
IteratorArrayMemberOperator() { this.getClassAndName("operator[]") instanceof Iterator }
282286

283287
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
284288
input.isQualifierObject() and

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3926,10 +3926,12 @@
39263926
| string.cpp:408:8:408:9 | i2 | string.cpp:409:10:409:11 | i7 | |
39273927
| string.cpp:409:10:409:11 | i7 | string.cpp:409:12:409:12 | call to operator+= | |
39283928
| string.cpp:409:12:409:12 | call to operator+= | string.cpp:409:8:409:8 | call to operator* | TAINT |
3929+
| string.cpp:409:14:409:14 | 1 | string.cpp:409:10:409:11 | ref arg i7 | TAINT |
39293930
| string.cpp:410:8:410:9 | i2 | string.cpp:410:3:410:9 | ... = ... | |
39303931
| string.cpp:410:8:410:9 | i2 | string.cpp:411:10:411:11 | i8 | |
39313932
| string.cpp:411:10:411:11 | i8 | string.cpp:411:12:411:12 | call to operator-= | |
39323933
| string.cpp:411:12:411:12 | call to operator-= | string.cpp:411:8:411:8 | call to operator* | TAINT |
3934+
| string.cpp:411:14:411:14 | 1 | string.cpp:411:10:411:11 | ref arg i8 | TAINT |
39333935
| string.cpp:413:8:413:9 | s2 | string.cpp:413:11:413:13 | call to end | TAINT |
39343936
| string.cpp:413:11:413:13 | call to end | string.cpp:413:3:413:15 | ... = ... | |
39353937
| string.cpp:413:11:413:13 | call to end | string.cpp:414:5:414:6 | i9 | |
@@ -7582,9 +7584,11 @@
75827584
| vector.cpp:528:3:528:4 | ref arg it | vector.cpp:529:9:529:10 | it | |
75837585
| vector.cpp:528:3:528:4 | ref arg it | vector.cpp:530:3:530:4 | it | |
75847586
| vector.cpp:528:3:528:4 | ref arg it | vector.cpp:531:9:531:10 | it | |
7587+
| vector.cpp:528:9:528:9 | 1 | vector.cpp:528:3:528:4 | ref arg it | TAINT |
75857588
| vector.cpp:529:9:529:10 | it | vector.cpp:529:8:529:8 | call to operator* | TAINT |
75867589
| vector.cpp:530:3:530:4 | it | vector.cpp:530:6:530:6 | call to operator+= | |
75877590
| vector.cpp:530:3:530:4 | ref arg it | vector.cpp:531:9:531:10 | it | |
7591+
| vector.cpp:530:9:530:14 | call to source | vector.cpp:530:3:530:4 | ref arg it | TAINT |
75887592
| vector.cpp:531:9:531:10 | it | vector.cpp:531:8:531:8 | call to operator* | TAINT |
75897593
| vector.cpp:532:8:532:9 | ref arg vs | vector.cpp:533:2:533:2 | vs | |
75907594
| vector.cpp:532:8:532:9 | vs | vector.cpp:532:10:532:10 | call to operator[] | TAINT |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void test_vector_iterator() {
528528
it += 1;
529529
sink(*it);
530530
it += source();
531-
sink(*it); // $ MISSING: ast,ir
531+
sink(*it); // $ ast,ir
532532
sink(vs[1]);
533533
}
534534
}

0 commit comments

Comments
 (0)