Skip to content

Commit 209a306

Browse files
authored
Merge pull request #2718 from jbj/DefaultTaintTracking-isUserInput
C++: Fix mapping of sources from Expr to Node
2 parents b2a87f6 + 9b651ea commit 209a306

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,19 @@ private predicate predictableInstruction(Instruction instr) {
2121
predictableInstruction(instr.(UnaryInstruction).getUnary())
2222
}
2323

24+
private DataFlow::Node getNodeForSource(Expr source) {
25+
isUserInput(source, _) and
26+
(
27+
result = DataFlow::exprNode(source)
28+
or
29+
result = DataFlow::definitionByReferenceNode(source)
30+
)
31+
}
32+
2433
private class DefaultTaintTrackingCfg extends DataFlow::Configuration {
2534
DefaultTaintTrackingCfg() { this = "DefaultTaintTrackingCfg" }
2635

27-
override predicate isSource(DataFlow::Node source) {
28-
exists(CallInstruction ci, WriteSideEffectInstruction wsei |
29-
userInputArgument(ci.getConvertedResultExpression(), wsei.getIndex()) and
30-
source.asInstruction() = wsei and
31-
wsei.getPrimaryInstruction() = ci
32-
)
33-
or
34-
userInputReturned(source.asExpr())
35-
or
36-
isUserInput(source.asExpr(), _)
37-
or
38-
source.asExpr() instanceof EnvironmentRead
39-
or
40-
source
41-
.asInstruction()
42-
.(LoadInstruction)
43-
.getSourceAddress()
44-
.(VariableAddressInstruction)
45-
.getASTVariable()
46-
.hasName("argv") and
47-
source.asInstruction().getEnclosingFunction().hasGlobalName("main")
48-
}
36+
override predicate isSource(DataFlow::Node source) { source = getNodeForSource(_) }
4937

5038
override predicate isSink(DataFlow::Node sink) { any() }
5139

@@ -59,7 +47,7 @@ private class DefaultTaintTrackingCfg extends DataFlow::Configuration {
5947
private class ToGlobalVarTaintTrackingCfg extends DataFlow::Configuration {
6048
ToGlobalVarTaintTrackingCfg() { this = "GlobalVarTaintTrackingCfg" }
6149

62-
override predicate isSource(DataFlow::Node source) { isUserInput(source.asExpr(), _) }
50+
override predicate isSource(DataFlow::Node source) { source = getNodeForSource(_) }
6351

6452
override predicate isSink(DataFlow::Node sink) {
6553
exists(GlobalOrNamespaceVariable gv | writesVariable(sink.asInstruction(), gv))
@@ -306,10 +294,7 @@ private Element adjustedSink(DataFlow::Node sink) {
306294

307295
predicate tainted(Expr source, Element tainted) {
308296
exists(DefaultTaintTrackingCfg cfg, DataFlow::Node sink |
309-
cfg.hasFlow(DataFlow::exprNode(source), sink)
310-
or
311-
cfg.hasFlow(DataFlow::definitionByReferenceNode(source), sink)
312-
|
297+
cfg.hasFlow(getNodeForSource(source), sink) and
313298
tainted = adjustedSink(sink)
314299
)
315300
}
@@ -322,7 +307,7 @@ predicate taintedIncludingGlobalVars(Expr source, Element tainted, string global
322307
ToGlobalVarTaintTrackingCfg toCfg, FromGlobalVarTaintTrackingCfg fromCfg, DataFlow::Node store,
323308
GlobalOrNamespaceVariable global, DataFlow::Node load, DataFlow::Node sink
324309
|
325-
toCfg.hasFlow(DataFlow::exprNode(source), store) and
310+
toCfg.hasFlow(getNodeForSource(source), store) and
326311
store
327312
.asInstruction()
328313
.(StoreInstruction)

0 commit comments

Comments
 (0)