Skip to content

Commit 71d87be

Browse files
author
Robert Marsh
committed
C++: add flow through partial loads in DTT
1 parent 1472101 commit 71d87be

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

cpp/ql/src/semmle/code/cpp/ir/dataflow/DefaultTaintTracking.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ private predicate instructionTaintStep(Instruction i1, Instruction i2) {
147147
// Flow through pointer dereference
148148
i2.(LoadInstruction).getSourceAddress() = i1
149149
or
150+
i2.(LoadInstruction).getSourceValueOperand().getAnyDef() = i1
151+
or
150152
i2.(UnaryInstruction).getUnary() = i1
151153
or
152154
i2.(ChiInstruction).getPartial() = i1 and
@@ -311,6 +313,14 @@ predicate tainted(Expr source, Element tainted) {
311313
)
312314
}
313315

316+
predicate tainted_instruction(Function sourceFunc,Instruction source, Function sinkFunc, Instruction sink) {
317+
sourceFunc = source.getEnclosingFunction() and
318+
sinkFunc = sink.getEnclosingFunction() and
319+
exists(DefaultTaintTrackingCfg cfg |
320+
cfg.hasFlow(DataFlow::instructionNode(source), DataFlow::instructionNode(sink))
321+
)
322+
}
323+
314324
predicate taintedIncludingGlobalVars(Expr source, Element tainted, string globalVar) {
315325
tainted(source, tainted) and
316326
globalVar = ""

cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/defaulttainttracking.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ void test_dynamic_cast() {
7777
reinterpret_cast<D2*>(b2)->f(getenv("VAR"));
7878

7979
dynamic_cast<D3*>(b2)->f(getenv("VAR")); // tainted [FALSE POSITIVE]
80+
}
81+
82+
void flow_to_outparam(char ** ret, char *arg) {
83+
*ret = arg;
84+
}
85+
86+
void test_outparams() {
87+
char *p2 = nullptr;
88+
flow_to_outparam(&p2, getenv("VAR"));
89+
sink(p2); // tainted
8090
}

cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/tainted.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@
8989
| defaulttainttracking.cpp:79:30:79:35 | call to getenv | defaulttainttracking.cpp:79:30:79:35 | call to getenv |
9090
| defaulttainttracking.cpp:79:30:79:35 | call to getenv | defaulttainttracking.cpp:79:30:79:42 | (const char *)... |
9191
| defaulttainttracking.cpp:79:30:79:35 | call to getenv | test_diff.cpp:1:11:1:20 | p#0 |
92+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:9:11:9:20 | p#0 |
93+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:82:42:82:44 | arg |
94+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:83:12:83:14 | arg |
95+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:88:27:88:32 | call to getenv |
96+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:89:10:89:11 | (const char *)... |
97+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:89:10:89:11 | p2 |
98+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | test_diff.cpp:1:11:1:20 | p#0 |
9299
| test_diff.cpp:92:10:92:13 | argv | defaulttainttracking.cpp:9:11:9:20 | p#0 |
93100
| test_diff.cpp:92:10:92:13 | argv | test_diff.cpp:1:11:1:20 | p#0 |
94101
| test_diff.cpp:92:10:92:13 | argv | test_diff.cpp:92:10:92:13 | argv |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
| defaulttainttracking.cpp:38:25:38:30 | call to getenv | defaulttainttracking.cpp:39:36:39:61 | (const char *)... | AST only |
1010
| defaulttainttracking.cpp:38:25:38:30 | call to getenv | defaulttainttracking.cpp:39:51:39:61 | env_pointer | AST only |
1111
| defaulttainttracking.cpp:64:10:64:15 | call to getenv | defaulttainttracking.cpp:52:24:52:24 | p | IR only |
12+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:9:11:9:20 | p#0 | IR only |
13+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:82:31:82:33 | ret | AST only |
14+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:83:5:83:8 | * ... | AST only |
15+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:83:6:83:8 | ret | AST only |
16+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:89:10:89:11 | (const char *)... | IR only |
17+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | defaulttainttracking.cpp:89:10:89:11 | p2 | IR only |
18+
| defaulttainttracking.cpp:88:27:88:32 | call to getenv | test_diff.cpp:1:11:1:20 | p#0 | IR only |
1219
| test_diff.cpp:104:12:104:15 | argv | test_diff.cpp:104:11:104:20 | (...) | IR only |
1320
| test_diff.cpp:108:10:108:13 | argv | test_diff.cpp:36:24:36:24 | p | AST only |
1421
| test_diff.cpp:111:10:111:13 | argv | defaulttainttracking.cpp:9:11:9:20 | p#0 | AST only |

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,13 @@ void throughStmtExpr(int source1, int clean1) {
460460
});
461461
sink(local); // tainted
462462
}
463+
464+
void intOutparamSource(int *p) {
465+
*p = source();
466+
}
467+
468+
void viaOutparam() {
469+
int x = 0;
470+
intOutparamSource(&x);
471+
sink(x); // tainted [FALSE NEGATIVE]
472+
}

0 commit comments

Comments
 (0)