Skip to content

Commit 3c41ed5

Browse files
committed
CPP: Support taint to return value derefs instead.
1 parent f4aba14 commit 3c41ed5

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,9 @@ private predicate exprToExprStep_nocfg(Expr fromExpr, Expr toExpr) {
602602
exists(DataFlowFunction f, FunctionInput inModel, FunctionOutput outModel, int iIn |
603603
call.getTarget() = f and
604604
f.hasDataFlow(inModel, outModel) and
605-
fromExpr = call.getArgument(iIn) and
606-
(
607-
inModel.isParameter(iIn) and
608-
outModel.isReturnValue()
609-
or
610-
inModel.isParameterDeref(iIn) and
611-
outModel.isReturnValueDeref()
612-
)
605+
outModel.isReturnValue() and
606+
inModel.isParameter(iIn) and
607+
fromExpr = call.getArgument(iIn)
613608
)
614609
)
615610
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ predicate localAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeT
6969
or
7070
// Taint can flow through modeled functions
7171
exprToDefinitionByReferenceStep(nodeFrom.asExpr(), nodeTo.asDefiningArgument())
72+
or
73+
exprToExprStep(nodeFrom.asExpr(), nodeTo.asExpr())
7274
}
7375

7476
/**
@@ -118,6 +120,36 @@ private predicate noFlowFromChildExpr(Expr e) {
118120
e instanceof FieldAccess
119121
}
120122

123+
private predicate exprToExprStep(Expr exprIn, Expr exprOut) {
124+
exists(DataFlowFunction f, Call call, FunctionOutput outModel |
125+
call.getTarget() = f and
126+
exprOut = call and
127+
outModel.isReturnValueDeref() and
128+
exists(int argInIndex, FunctionInput inModel | f.hasDataFlow(inModel, outModel) |
129+
// Taint flows from a pointer to a dereference, which DataFlow does not handle
130+
// dest_ptr = strdup(tainted_ptr)
131+
inModel.isParameterDeref(argInIndex) and
132+
exprIn = call.getArgument(argInIndex)
133+
)
134+
)
135+
or
136+
exists(TaintFunction f, Call call, FunctionOutput outModel |
137+
call.getTarget() = f and
138+
exprOut = call and
139+
outModel.isReturnValueDeref() and
140+
exists(int argInIndex, FunctionInput inModel | f.hasTaintFlow(inModel, outModel) |
141+
inModel.isParameterDeref(argInIndex) and
142+
exprIn = call.getArgument(argInIndex)
143+
or
144+
inModel.isParameterDeref(argInIndex) and
145+
call.passesByReference(argInIndex, exprIn)
146+
or
147+
inModel.isParameter(argInIndex) and
148+
exprIn = call.getArgument(argInIndex)
149+
)
150+
)
151+
}
152+
121153
private predicate exprToDefinitionByReferenceStep(Expr exprIn, Expr argOut) {
122154
exists(DataFlowFunction f, Call call, FunctionOutput outModel, int argOutIndex |
123155
call.getTarget() = f and

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
| taint.cpp:170:10:170:15 | ref arg buffer | taint.cpp:171:8:171:13 | buffer | |
145145
| taint.cpp:170:10:170:15 | ref arg buffer | taint.cpp:172:10:172:15 | buffer | |
146146
| taint.cpp:170:10:170:15 | ref arg buffer | taint.cpp:173:8:173:13 | buffer | |
147-
| taint.cpp:170:18:170:26 | Hello, | taint.cpp:170:3:170:8 | call to strcpy | |
147+
| taint.cpp:170:18:170:26 | Hello, | taint.cpp:170:3:170:8 | call to strcpy | TAINT |
148148
| taint.cpp:170:18:170:26 | Hello, | taint.cpp:170:10:170:15 | ref arg buffer | TAINT |
149149
| taint.cpp:171:8:171:13 | ref arg buffer | taint.cpp:172:10:172:15 | buffer | |
150150
| taint.cpp:171:8:171:13 | ref arg buffer | taint.cpp:173:8:173:13 | buffer | |
@@ -164,9 +164,9 @@
164164
| taint.cpp:194:9:194:10 | ref arg & ... | taint.cpp:194:2:194:7 | call to memcpy | |
165165
| taint.cpp:194:9:194:10 | ref arg & ... | taint.cpp:195:7:195:7 | x | |
166166
| taint.cpp:194:10:194:10 | x | taint.cpp:194:9:194:10 | & ... | |
167-
| taint.cpp:194:13:194:18 | ref arg source | taint.cpp:194:2:194:7 | call to memcpy | |
168-
| taint.cpp:194:13:194:18 | source | taint.cpp:194:2:194:7 | call to memcpy | |
167+
| taint.cpp:194:13:194:18 | source | taint.cpp:194:2:194:7 | call to memcpy | TAINT |
169168
| taint.cpp:194:13:194:18 | source | taint.cpp:194:9:194:10 | ref arg & ... | TAINT |
169+
| taint.cpp:194:21:194:31 | sizeof(int) | taint.cpp:194:2:194:7 | call to memcpy | TAINT |
170170
| taint.cpp:194:21:194:31 | sizeof(int) | taint.cpp:194:9:194:10 | ref arg & ... | TAINT |
171171
| taint.cpp:207:6:207:11 | call to source | taint.cpp:207:2:207:13 | ... = ... | |
172172
| taint.cpp:207:6:207:11 | call to source | taint.cpp:210:7:210:7 | x | |
@@ -331,10 +331,10 @@
331331
| taint.cpp:365:24:365:29 | source | taint.cpp:371:14:371:19 | source | |
332332
| taint.cpp:369:6:369:11 | call to strdup | taint.cpp:369:2:369:19 | ... = ... | |
333333
| taint.cpp:369:6:369:11 | call to strdup | taint.cpp:372:7:372:7 | a | |
334-
| taint.cpp:369:13:369:18 | source | taint.cpp:369:6:369:11 | call to strdup | |
334+
| taint.cpp:369:13:369:18 | source | taint.cpp:369:6:369:11 | call to strdup | TAINT |
335335
| taint.cpp:370:6:370:11 | call to strdup | taint.cpp:370:2:370:27 | ... = ... | |
336336
| taint.cpp:370:6:370:11 | call to strdup | taint.cpp:373:7:373:7 | b | |
337-
| taint.cpp:370:13:370:26 | hello, world | taint.cpp:370:6:370:11 | call to strdup | |
337+
| taint.cpp:370:13:370:26 | hello, world | taint.cpp:370:6:370:11 | call to strdup | TAINT |
338338
| taint.cpp:371:6:371:12 | call to strndup | taint.cpp:371:2:371:25 | ... = ... | |
339339
| taint.cpp:371:6:371:12 | call to strndup | taint.cpp:374:7:374:7 | c | |
340340
| taint.cpp:377:23:377:28 | source | taint.cpp:381:30:381:35 | source | |
@@ -343,7 +343,7 @@
343343
| taint.cpp:385:27:385:32 | source | taint.cpp:389:13:389:18 | source | |
344344
| taint.cpp:389:6:389:11 | call to wcsdup | taint.cpp:389:2:389:19 | ... = ... | |
345345
| taint.cpp:389:6:389:11 | call to wcsdup | taint.cpp:391:7:391:7 | a | |
346-
| taint.cpp:389:13:389:18 | source | taint.cpp:389:6:389:11 | call to wcsdup | |
346+
| taint.cpp:389:13:389:18 | source | taint.cpp:389:6:389:11 | call to wcsdup | TAINT |
347347
| taint.cpp:390:6:390:11 | call to wcsdup | taint.cpp:390:2:390:28 | ... = ... | |
348348
| taint.cpp:390:6:390:11 | call to wcsdup | taint.cpp:392:7:392:7 | b | |
349-
| taint.cpp:390:13:390:27 | hello, world | taint.cpp:390:6:390:11 | call to wcsdup | |
349+
| taint.cpp:390:13:390:27 | hello, world | taint.cpp:390:6:390:11 | call to wcsdup | TAINT |

0 commit comments

Comments
 (0)