Skip to content

Commit 361ad6b

Browse files
committed
use abstract class for decompression flow steps
1 parent 656dc4e commit 361ad6b

4 files changed

Lines changed: 67 additions & 28 deletions

File tree

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import cpp
2+
import semmle.code.cpp.ir.dataflow.TaintTracking
23

34
/**
4-
* The Decompression Sink instances, extend this class to defind new decompression sinks.
5+
* The Decompression Sink instances, extend this class to define new decompression sinks.
56
*/
67
abstract class DecompressionFunction extends Function {
78
abstract int getArchiveParameterIndex();
89
}
10+
11+
/**
12+
* The Decompression Flow Steps, extend this class to define new decompression sinks.
13+
*/
14+
abstract class DecompressionFlowStep extends Function {
15+
abstract predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2);
16+
}

cpp/ql/src/experimental/query-tests/Security/CWE/CWE-409/DecompressionBombs.ql

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,7 @@ module DecompressionTaintConfig implements DataFlow::ConfigSig {
2929
}
3030

3131
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
32-
exists(FunctionCall fc | fc.getTarget() instanceof UnzOpenFunction |
33-
node1.asExpr() = fc.getArgument(0) and
34-
node2.asExpr() = fc
35-
)
36-
or
37-
exists(FunctionCall fc | fc.getTarget() instanceof Mz_zip_reader_entry |
38-
node1.asExpr() = fc.getArgument(0) and
39-
node2.asExpr() = fc.getArgument(1)
40-
)
41-
or
42-
exists(FunctionCall fc | fc.getTarget() instanceof Mz_zip_entry |
43-
node1.asExpr() = fc.getArgument(0) and
44-
node2.asExpr() = fc.getArgument(1)
45-
)
46-
or
47-
exists(FunctionCall fc |
48-
fc.getTarget() instanceof GzopenFunction or fc.getTarget() instanceof GzdopenFunction
49-
|
50-
node1.asExpr() = fc.getArgument(0) and
51-
node2.asExpr() = fc
52-
)
32+
any(DecompressionFlowStep f).isAdditionalFlowStep(node1, node2)
5333
}
5434
}
5535

cpp/ql/src/experimental/query-tests/Security/CWE/CWE-409/MiniZip.qll

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import semmle.code.cpp.security.FlowSources
88
import DecompressionBomb
99

1010
/**
11-
* The `mz_zip_entry` function is used in flow source.
11+
* The `mz_zip_entry` function is used in flow sink.
1212
* [docuemnt](https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_zip.md)
1313
*/
1414
class Mz_zip_entry extends DecompressionFunction {
@@ -17,6 +17,21 @@ class Mz_zip_entry extends DecompressionFunction {
1717
override int getArchiveParameterIndex() { result = 1 }
1818
}
1919

20+
/**
21+
* The `mz_zip_entry` function is used in flow steps.
22+
* [docuemnt](https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_zip.md)
23+
*/
24+
class Mz_zip_entry_flow_steps extends DecompressionFlowStep {
25+
Mz_zip_entry_flow_steps() { this.hasGlobalName("mz_zip_entry_read") }
26+
27+
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
28+
exists(FunctionCall fc | fc.getTarget() = this |
29+
node1.asExpr() = fc.getArgument(0) and
30+
node2.asExpr() = fc.getArgument(1)
31+
)
32+
}
33+
}
34+
2035
/**
2136
* The `mz_zip_reader_entry_*` and `mz_zip_reader_save_all` functions are used in flow source.
2237
* [docuemnt](https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_zip_rw.md)
@@ -32,9 +47,31 @@ class Mz_zip_reader_entry extends DecompressionFunction {
3247
override int getArchiveParameterIndex() { result = 1 }
3348
}
3449

50+
/**
51+
* The `mz_zip_reader_entry_*` and `mz_zip_reader_save_all` functions are used in flow steps.
52+
* [docuemnt](https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_zip_rw.md)
53+
*/
54+
class Mz_zip_reader_entry_flow_steps extends DecompressionFlowStep {
55+
Mz_zip_reader_entry_flow_steps() { this instanceof Mz_zip_reader_entry }
56+
57+
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
58+
exists(FunctionCall fc | fc.getTarget() = this |
59+
node1.asExpr() = fc.getArgument(0) and
60+
node2.asExpr() = fc.getArgument(1)
61+
)
62+
}
63+
}
64+
3565
/**
3666
* The `UnzOpen` function as a flow source.
3767
*/
38-
class UnzOpenFunction extends Function {
68+
class UnzOpenFunction extends DecompressionFlowStep {
3969
UnzOpenFunction() { this.hasGlobalName(["UnzOpen", "unzOpen64", "unzOpen2", "unzOpen2_64"]) }
70+
71+
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
72+
exists(FunctionCall fc | fc.getTarget() = this |
73+
node1.asExpr() = fc.getArgument(0) and
74+
node2.asExpr() = fc
75+
)
76+
}
4077
}

cpp/ql/src/experimental/query-tests/Security/CWE/CWE-409/ZlibGzopen.qll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,33 @@ class GzReadFunction extends DecompressionFunction {
4141
}
4242

4343
/**
44-
* The `gzdopen` function.
44+
* The `gzdopen` function is used in flow steps.
4545
*
4646
* `gzdopen(int fd, const char *mode)`
4747
*/
48-
class GzdopenFunction extends Function {
48+
class GzdopenFunction extends DecompressionFlowStep {
4949
GzdopenFunction() { this.hasGlobalName("gzdopen") }
50+
51+
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
52+
exists(FunctionCall fc | fc.getTarget() = this |
53+
node1.asExpr() = fc.getArgument(0) and
54+
node2.asExpr() = fc
55+
)
56+
}
5057
}
5158

5259
/**
53-
* The `gzopen` function.
60+
* The `gzopen` function is used in flow steps.
5461
*
5562
* `gzopen(const char *path, const char *mode)`
5663
*/
57-
class GzopenFunction extends Function {
64+
class GzopenFunction extends DecompressionFlowStep {
5865
GzopenFunction() { this.hasGlobalName("gzopen") }
66+
67+
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
68+
exists(FunctionCall fc | fc.getTarget() = this |
69+
node1.asExpr() = fc.getArgument(0) and
70+
node2.asExpr() = fc
71+
)
72+
}
5973
}

0 commit comments

Comments
 (0)