Skip to content

Commit 1f75c38

Browse files
committed
C++: Refactor dataflow examples to use DataFlow::ConfigSig
1 parent 4e75236 commit 1f75c38

File tree

4 files changed

+30
-38
lines changed

4 files changed

+30
-38
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import cpp
22
import semmle.code.cpp.dataflow.new.DataFlow
33

4-
class LiteralToGethostbynameConfiguration extends DataFlow::Configuration {
5-
LiteralToGethostbynameConfiguration() { this = "LiteralToGethostbynameConfiguration" }
4+
module LiteralToGethostbynameConfig implements DataFlow::ConfigSig {
5+
predicate isSource(DataFlow::Node source) { source.asIndirectExpr(1) instanceof StringLiteral }
66

7-
override predicate isSource(DataFlow::Node source) {
8-
source.asIndirectExpr(1) instanceof StringLiteral
9-
}
10-
11-
override predicate isSink(DataFlow::Node sink) {
7+
predicate isSink(DataFlow::Node sink) {
128
exists(FunctionCall fc |
139
sink.asIndirectExpr(1) = fc.getArgument(0) and
1410
fc.getTarget().hasName("gethostbyname")
1511
)
1612
}
1713
}
1814

19-
from
20-
StringLiteral sl, FunctionCall fc, LiteralToGethostbynameConfiguration cfg, DataFlow::Node source,
21-
DataFlow::Node sink
15+
module LiteralToGethostbynameFlow = DataFlow::Make<LiteralToGethostbynameConfig>;
16+
17+
from StringLiteral sl, FunctionCall fc, DataFlow::Node source, DataFlow::Node sink
2218
where
2319
source.asIndirectExpr(1) = sl and
2420
sink.asIndirectExpr(1) = fc.getArgument(0) and
25-
cfg.hasFlow(source, sink)
21+
LiteralToGethostbynameFlow::hasFlow(source, sink)
2622
select sl, fc

cpp/ql/test/examples/docs-examples/analyzing-data-flow-in-cpp/exercise4.ql

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ class GetenvSource extends DataFlow::Node {
55
GetenvSource() { this.asIndirectExpr(1).(FunctionCall).getTarget().hasGlobalName("getenv") }
66
}
77

8-
class GetenvToGethostbynameConfiguration extends DataFlow::Configuration {
9-
GetenvToGethostbynameConfiguration() { this = "GetenvToGethostbynameConfiguration" }
8+
module GetenvToGethostbynameConfig implements DataFlow::ConfigSig {
9+
predicate isSource(DataFlow::Node source) { source instanceof GetenvSource }
1010

11-
override predicate isSource(DataFlow::Node source) { source instanceof GetenvSource }
12-
13-
override predicate isSink(DataFlow::Node sink) {
11+
predicate isSink(DataFlow::Node sink) {
1412
exists(FunctionCall fc |
1513
sink.asIndirectExpr(1) = fc.getArgument(0) and
1614
fc.getTarget().hasName("gethostbyname")
1715
)
1816
}
1917
}
2018

21-
from
22-
Expr getenv, FunctionCall fc, GetenvToGethostbynameConfiguration cfg, DataFlow::Node source,
23-
DataFlow::Node sink
19+
module GetenvToGethostbynameFlow = DataFlow::Make<GetenvToGethostbynameConfig>;
20+
21+
from Expr getenv, FunctionCall fc, DataFlow::Node source, DataFlow::Node sink
2422
where
2523
source.asIndirectExpr(1) = getenv and
2624
sink.asIndirectExpr(1) = fc.getArgument(0) and
27-
cfg.hasFlow(source, sink)
25+
GetenvToGethostbynameFlow::hasFlow(source, sink)
2826
select getenv, fc
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import cpp
22
import semmle.code.cpp.dataflow.new.DataFlow
33

4-
class EnvironmentToFileConfiguration extends DataFlow::Configuration {
5-
EnvironmentToFileConfiguration() { this = "EnvironmentToFileConfiguration" }
6-
7-
override predicate isSource(DataFlow::Node source) {
4+
module EnvironmentToFileConfig implements DataFlow::ConfigSig {
5+
predicate isSource(DataFlow::Node source) {
86
exists(Function getenv |
97
source.asIndirectExpr(1).(FunctionCall).getTarget() = getenv and
108
getenv.hasGlobalName("getenv")
119
)
1210
}
1311

14-
override predicate isSink(DataFlow::Node sink) {
12+
predicate isSink(DataFlow::Node sink) {
1513
exists(FunctionCall fc |
1614
sink.asIndirectExpr(1) = fc.getArgument(0) and
1715
fc.getTarget().hasGlobalName("fopen")
1816
)
1917
}
2018
}
2119

22-
from
23-
Expr getenv, Expr fopen, EnvironmentToFileConfiguration config, DataFlow::Node source,
24-
DataFlow::Node sink
20+
module EnvironmentToFileFlow = DataFlow::Make<EnvironmentToFileConfig>;
21+
22+
from Expr getenv, Expr fopen, DataFlow::Node source, DataFlow::Node sink
2523
where
2624
source.asIndirectExpr(1) = getenv and
2725
sink.asIndirectExpr(1) = fopen and
28-
config.hasFlow(source, sink)
26+
EnvironmentToFileFlow::hasFlow(source, sink)
2927
select fopen, "This 'fopen' uses data from $@.", getenv, "call to 'getenv'"

cpp/ql/test/examples/docs-examples/analyzing-data-flow-in-cpp/index-flow-from-ntohl.ql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import cpp
22
import semmle.code.cpp.controlflow.Guards
33
import semmle.code.cpp.dataflow.new.TaintTracking
44

5-
class NetworkToBufferSizeConfiguration extends TaintTracking::Configuration {
6-
NetworkToBufferSizeConfiguration() { this = "NetworkToBufferSizeConfiguration" }
7-
8-
override predicate isSource(DataFlow::Node node) {
5+
module NetworkToBufferSizeConfig implements DataFlow::ConfigSig {
6+
predicate isSource(DataFlow::Node node) {
97
node.asExpr().(FunctionCall).getTarget().hasGlobalName("ntohl")
108
}
119

12-
override predicate isSink(DataFlow::Node node) {
10+
predicate isSink(DataFlow::Node node) {
1311
exists(ArrayExpr ae | node.asExpr() = ae.getArrayOffset())
1412
}
1513

16-
override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) {
14+
predicate isAdditionalFlowStep(DataFlow::Node pred, DataFlow::Node succ) {
1715
exists(Loop loop, LoopCounter lc |
1816
loop = lc.getALoop() and
1917
loop.getControllingExpr().(RelationalOperation).getGreaterOperand() = pred.asExpr()
@@ -22,7 +20,7 @@ class NetworkToBufferSizeConfiguration extends TaintTracking::Configuration {
2220
)
2321
}
2422

25-
override predicate isSanitizer(DataFlow::Node node) {
23+
predicate isBarrier(DataFlow::Node node) {
2624
exists(GuardCondition gc, Variable v |
2725
gc.getAChild*() = v.getAnAccess() and
2826
node.asExpr() = v.getAnAccess() and
@@ -32,7 +30,9 @@ class NetworkToBufferSizeConfiguration extends TaintTracking::Configuration {
3230
}
3331
}
3432

35-
from DataFlow::Node ntohl, DataFlow::Node offset, NetworkToBufferSizeConfiguration conf
36-
where conf.hasFlow(ntohl, offset)
33+
module NetworkToBufferSizeFlow = TaintTracking::Make<NetworkToBufferSizeConfig>;
34+
35+
from DataFlow::Node ntohl, DataFlow::Node offset
36+
where NetworkToBufferSizeFlow::hasFlow(ntohl, offset)
3737
select offset, "This array offset may be influenced by $@.", ntohl,
3838
"converted data from the network"

0 commit comments

Comments
 (0)