Skip to content

Commit a897cae

Browse files
author
Robert Marsh
committed
C++: outbound dataflow via this indirections
1 parent 7dc30e3 commit a897cae

7 files changed

Lines changed: 23 additions & 14 deletions

File tree

cpp/ql/src/semmle/code/cpp/Parameter.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class Parameter extends LocalScopeVariable, @parameter {
165165
class ParameterIndex extends int {
166166
ParameterIndex() {
167167
exists(Parameter p | this = p.getIndex()) or
168-
exists(Call c | exists(c.getArgument(this))) // permit indexing varargs
168+
exists(Call c | exists(c.getArgument(this))) or // permit indexing varargs
169+
this = -1 // used for `this`
169170
}
170171
}

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ class ReturnValueNode extends ReturnNode {
7474
class ReturnIndirectionNode extends ReturnNode {
7575
override ReturnIndirectionInstruction primary;
7676

77-
override ReturnKind getKind() { result = TIndirectReturnKind(primary.getParameter().getIndex()) }
77+
override ReturnKind getKind() {
78+
result = TIndirectReturnKind(-1) and
79+
primary.isThisIndirection()
80+
or
81+
result = TIndirectReturnKind(primary.getParameter().getIndex())
82+
}
83+
7884
}
7985

8086
/** A data flow node that represents the output of a call. */

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ class ReturnIndirectionInstruction extends VariableInstruction {
541541
* function.
542542
*/
543543
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
544+
545+
final predicate isThisIndirection() { var instanceof IRThisVariable }
544546
}
545547

546548
class CopyInstruction extends Instruction {

cpp/ql/test/library-tests/dataflow/fields/A.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class A
129129
{
130130
B *b = new B();
131131
f7(b);
132-
sink(b->c); // $ast $f-:ir
132+
sink(b->c); // $ast,ir
133133
}
134134

135135
class D
@@ -149,7 +149,7 @@ class A
149149
{
150150
B *b = new B();
151151
D *d = new D(b, r());
152-
sink(d->b); // $ast=143:25 $ast=150:12 $f-:ir
152+
sink(d->b); // $ast,ir=143:25 $ast,ir=150:12
153153
sink(d->b->c); // $ast $f-:ir
154154
sink(b->c); // $ast,ir
155155
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ void class_field_test() {
8686
mc1.myMethod();
8787

8888
sink(mc1.a);
89-
sink(mc1.b); // tainted [NOT DETECTED with IR]
90-
sink(mc1.c); // tainted [NOT DETECTED with IR]
91-
sink(mc1.d); // tainted [NOT DETECTED with IR]
89+
sink(mc1.b); // tainted
90+
sink(mc1.c); // tainted
91+
sink(mc1.d); // tainted
9292
sink(mc2.a);
93-
sink(mc2.b); // tainted [NOT DETECTED with IR]
94-
sink(mc2.c); // tainted [NOT DETECTED with IR]
93+
sink(mc2.b); // tainted
94+
sink(mc2.c); // tainted
9595
sink(mc2.d);
9696
}
9797

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
| taint.cpp:41:7:41:13 | taint.cpp:35:12:35:17 | AST only |
1717
| taint.cpp:42:7:42:13 | taint.cpp:35:12:35:17 | AST only |
1818
| taint.cpp:43:7:43:13 | taint.cpp:37:22:37:27 | AST only |
19-
| taint.cpp:89:11:89:11 | taint.cpp:71:22:71:27 | AST only |
20-
| taint.cpp:90:11:90:11 | taint.cpp:72:7:72:12 | AST only |
21-
| taint.cpp:91:11:91:11 | taint.cpp:77:7:77:12 | AST only |
22-
| taint.cpp:93:11:93:11 | taint.cpp:71:22:71:27 | AST only |
23-
| taint.cpp:94:11:94:11 | taint.cpp:72:7:72:12 | AST only |
2419
| taint.cpp:109:7:109:13 | taint.cpp:105:12:105:17 | IR only |
2520
| taint.cpp:110:7:110:13 | taint.cpp:105:12:105:17 | IR only |
2621
| taint.cpp:111:7:111:13 | taint.cpp:106:12:106:17 | IR only |

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
| taint.cpp:8:8:8:13 | clean1 | taint.cpp:4:27:4:33 | source1 |
44
| taint.cpp:16:8:16:14 | source1 | taint.cpp:12:22:12:27 | call to source |
55
| taint.cpp:17:8:17:16 | ++ ... | taint.cpp:12:22:12:27 | call to source |
6+
| taint.cpp:89:11:89:11 | b | taint.cpp:71:22:71:27 | call to source |
7+
| taint.cpp:90:11:90:11 | c | taint.cpp:72:7:72:12 | call to source |
8+
| taint.cpp:91:11:91:11 | d | taint.cpp:77:7:77:12 | call to source |
9+
| taint.cpp:93:11:93:11 | b | taint.cpp:71:22:71:27 | call to source |
10+
| taint.cpp:94:11:94:11 | c | taint.cpp:72:7:72:12 | call to source |
611
| taint.cpp:109:7:109:13 | access to array | taint.cpp:105:12:105:17 | call to source |
712
| taint.cpp:110:7:110:13 | access to array | taint.cpp:105:12:105:17 | call to source |
813
| taint.cpp:111:7:111:13 | access to array | taint.cpp:106:12:106:17 | call to source |

0 commit comments

Comments
 (0)