Skip to content

Commit 7cc877c

Browse files
committed
C++: Taint through RangeBasedForStmt (AST only)
1 parent 205dd1a commit 7cc877c

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

cpp/ql/src/semmle/code/cpp/dataflow/internal/TaintTrackingUtil.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ predicate localAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeT
6565
// tracking. The flow from expression `x` into `x++` etc. is handled in the
6666
// case above.
6767
exprTo = DataFlow::getAnAccessToAssignedVariable(exprFrom.(PostfixCrementOperation))
68+
or
69+
// In `for (char c : s) { ... c ... }`, this rule propagates taint from `s`
70+
// to `c`.
71+
exists(RangeBasedForStmt rbf |
72+
exprFrom = rbf.getRange() and
73+
// It's guaranteed up to at least C++20 that the range-based for loop
74+
// desugars to a variable with an initializer.
75+
exprTo = rbf.getVariable().getInitializer().getExpr()
76+
)
6877
)
6978
or
7079
// Taint can flow through modeled functions

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@
417417
| stl.cpp:239:15:239:15 | ref arg (__range) | stl.cpp:239:15:239:15 | (__range) | |
418418
| stl.cpp:239:15:239:15 | s | stl.cpp:239:15:239:15 | (__range) | |
419419
| stl.cpp:239:15:239:15 | s | stl.cpp:239:15:239:15 | (__range) | |
420+
| stl.cpp:239:15:239:15 | s | stl.cpp:239:15:239:15 | call to operator* | TAINT |
420421
| stl.cpp:243:33:243:33 | ref arg s | stl.cpp:243:50:243:50 | s | |
421422
| stl.cpp:243:33:243:33 | ref arg s | stl.cpp:247:16:247:16 | s | |
422423
| stl.cpp:243:35:243:39 | call to begin | stl.cpp:243:44:243:45 | it | |
@@ -438,6 +439,7 @@
438439
| stl.cpp:247:16:247:16 | ref arg (__range) | stl.cpp:247:16:247:16 | (__range) | |
439440
| stl.cpp:247:16:247:16 | s | stl.cpp:247:16:247:16 | (__range) | |
440441
| stl.cpp:247:16:247:16 | s | stl.cpp:247:16:247:16 | (__range) | |
442+
| stl.cpp:247:16:247:16 | s | stl.cpp:247:16:247:16 | call to operator* | TAINT |
441443
| stl.cpp:251:28:251:33 | call to source | stl.cpp:251:28:251:36 | call to basic_string | TAINT |
442444
| stl.cpp:251:28:251:36 | call to basic_string | stl.cpp:252:22:252:28 | const_s | |
443445
| stl.cpp:252:22:252:22 | call to begin | stl.cpp:252:22:252:22 | (__begin) | |
@@ -450,6 +452,7 @@
450452
| stl.cpp:252:22:252:22 | ref arg (__begin) | stl.cpp:252:22:252:22 | (__begin) | |
451453
| stl.cpp:252:22:252:28 | const_s | stl.cpp:252:22:252:22 | (__range) | |
452454
| stl.cpp:252:22:252:28 | const_s | stl.cpp:252:22:252:22 | (__range) | |
455+
| stl.cpp:252:22:252:28 | const_s | stl.cpp:252:22:252:22 | call to operator* | TAINT |
453456
| structlikeclass.cpp:5:7:5:7 | Unknown literal | structlikeclass.cpp:5:7:5:7 | constructor init of field v | TAINT |
454457
| structlikeclass.cpp:5:7:5:7 | Unknown literal | structlikeclass.cpp:5:7:5:7 | constructor init of field v | TAINT |
455458
| structlikeclass.cpp:5:7:5:7 | this | structlikeclass.cpp:5:7:5:7 | constructor init of field v [pre-this] | |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,19 @@ void sink(char) {}
237237
void test_range_based_for_loop() {
238238
std::string s(source());
239239
for(char c : s) {
240-
sink(c); // tainted [NOT DETECTED]
240+
sink(c); // tainted [NOT DETECTED by IR]
241241
}
242242

243243
for(std::string::iterator it = s.begin(); it != s.end(); ++it) {
244244
sink(*it); // tainted [NOT DETECTED]
245245
}
246246

247247
for(char& c : s) {
248-
sink(c); // tainted [NOT DETECTED]
248+
sink(c); // tainted [NOT DETECTED by IR]
249249
}
250250

251251
const std::string const_s(source());
252252
for(const char& c : const_s) {
253-
sink(c); // tainted [NOT DETECTED]
253+
sink(c); // tainted [NOT DETECTED by IR]
254254
}
255255
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
| stl.cpp:211:8:211:9 | s3 | stl.cpp:207:8:207:13 | call to source |
4848
| stl.cpp:230:8:230:9 | s1 | stl.cpp:226:32:226:37 | call to source |
4949
| stl.cpp:231:8:231:9 | s2 | stl.cpp:228:20:228:25 | call to source |
50+
| stl.cpp:240:8:240:8 | c | stl.cpp:238:16:238:21 | call to source |
51+
| stl.cpp:248:8:248:8 | c | stl.cpp:238:16:238:21 | call to source |
52+
| stl.cpp:253:8:253:8 | c | stl.cpp:251:28:251:33 | call to source |
5053
| structlikeclass.cpp:35:8:35:9 | s1 | structlikeclass.cpp:29:22:29:27 | call to source |
5154
| structlikeclass.cpp:36:8:36:9 | s2 | structlikeclass.cpp:30:24:30:29 | call to source |
5255
| structlikeclass.cpp:37:8:37:9 | s3 | structlikeclass.cpp:29:22:29:27 | call to source |

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
| stl.cpp:211:8:211:9 | stl.cpp:207:8:207:13 | AST only |
4545
| stl.cpp:230:8:230:9 | stl.cpp:226:32:226:37 | AST only |
4646
| stl.cpp:231:8:231:9 | stl.cpp:228:20:228:25 | AST only |
47+
| stl.cpp:240:8:240:8 | stl.cpp:238:16:238:21 | AST only |
48+
| stl.cpp:248:8:248:8 | stl.cpp:238:16:238:21 | AST only |
49+
| stl.cpp:253:8:253:8 | stl.cpp:251:28:251:33 | AST only |
4750
| structlikeclass.cpp:35:8:35:9 | structlikeclass.cpp:29:22:29:27 | AST only |
4851
| structlikeclass.cpp:36:8:36:9 | structlikeclass.cpp:30:24:30:29 | AST only |
4952
| structlikeclass.cpp:37:8:37:9 | structlikeclass.cpp:29:22:29:27 | AST only |

0 commit comments

Comments
 (0)