Skip to content

Commit fbac4ce

Browse files
committed
C++: Split StdStringCStr and allow reverse flow on data.
1 parent fbff44e commit fbac4ce

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class StdBasicString extends TemplateClass {
88
}
99

1010
/**
11-
* The `std::string` functions `c_str` and `data`.
11+
* The `std::string` function `c_str`.
1212
*/
1313
class StdStringCStr extends TaintFunction {
14-
StdStringCStr() { this.hasQualifiedName("std", "basic_string", ["c_str", "data"]) }
14+
StdStringCStr() { this.hasQualifiedName("std", "basic_string", "c_str") }
1515

1616
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
1717
// flow from string itself (qualifier) to return value
@@ -20,6 +20,24 @@ class StdStringCStr extends TaintFunction {
2020
}
2121
}
2222

23+
/**
24+
* The `std::string` function `data`.
25+
*/
26+
class StdStringData extends TaintFunction {
27+
StdStringData() { this.hasQualifiedName("std", "basic_string", "data") }
28+
29+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
30+
// flow from string itself (qualifier) to return value
31+
input.isQualifierObject() and
32+
output.isReturnValue()
33+
or
34+
// reverse flow from returned reference to the qualifier (for writes to
35+
// `data`)
36+
input.isReturnValueDeref() and
37+
output.isQualifierObject()
38+
}
39+
}
40+
2341
/**
2442
* The `std::string` function `operator+`.
2543
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@
727727
| string.cpp:348:2:348:14 | access to array [post update] | string.cpp:348:6:348:9 | call to data [inner post update] | |
728728
| string.cpp:348:2:348:34 | ... = ... | string.cpp:348:2:348:14 | access to array [post update] | |
729729
| string.cpp:348:6:348:9 | call to data | string.cpp:348:2:348:14 | access to array | TAINT |
730+
| string.cpp:348:6:348:9 | call to data [inner post update] | string.cpp:348:2:348:4 | ref arg str | TAINT |
730731
| string.cpp:348:13:348:13 | 1 | string.cpp:348:2:348:14 | access to array | TAINT |
731732
| string.cpp:348:18:348:32 | call to source | string.cpp:348:2:348:34 | ... = ... | |
732733
| string.cpp:350:7:350:9 | str | string.cpp:350:11:350:14 | call to data | TAINT |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,6 @@ void test_string_data_more()
346346
std::string str("123");
347347

348348
str.data()[1] = ns_char::source();
349-
sink(str); // tainted [NOT DETECTED]
350-
sink(str.data()); // tainted [NOT DETECTED]
349+
sink(str); // tainted
350+
sink(str.data()); // tainted
351351
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
| string.cpp:339:7:339:7 | a | string.cpp:335:9:335:23 | call to source |
9595
| string.cpp:340:7:340:7 | b | string.cpp:336:12:336:26 | call to source |
9696
| string.cpp:341:7:341:7 | c | string.cpp:335:9:335:23 | call to source |
97+
| string.cpp:349:7:349:9 | str | string.cpp:348:18:348:32 | call to source |
98+
| string.cpp:350:11:350:14 | call to data | string.cpp:348:18:348:32 | call to source |
9799
| structlikeclass.cpp:35:8:35:9 | s1 | structlikeclass.cpp:29:22:29:27 | call to source |
98100
| structlikeclass.cpp:36:8:36:9 | s2 | structlikeclass.cpp:30:24:30:29 | call to source |
99101
| 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
| string.cpp:339:7:339:7 | string.cpp:335:9:335:23 | AST only |
9292
| string.cpp:340:7:340:7 | string.cpp:336:12:336:26 | AST only |
9393
| string.cpp:341:7:341:7 | string.cpp:335:9:335:23 | AST only |
94+
| string.cpp:349:7:349:9 | string.cpp:348:18:348:32 | AST only |
95+
| string.cpp:350:11:350:14 | string.cpp:348:18:348:32 | AST only |
9496
| structlikeclass.cpp:35:8:35:9 | structlikeclass.cpp:29:22:29:27 | AST only |
9597
| structlikeclass.cpp:36:8:36:9 | structlikeclass.cpp:30:24:30:29 | AST only |
9698
| structlikeclass.cpp:37:8:37:9 | structlikeclass.cpp:29:22:29:27 | AST only |

0 commit comments

Comments
 (0)