Skip to content

Commit 3a35a40

Browse files
Robert Marshgeoffw0
authored andcommitted
WIP: start on CWE-611 tests
1 parent 370dd05 commit 3a35a40

3 files changed

Lines changed: 91 additions & 14 deletions

File tree

cpp/ql/src/Security/CWE/CWE-611/XercesXXE.ql

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class AbstractDOMParser extends Class {
2323

2424
class DisableDefaultEntityResolution extends Function {
2525
DisableDefaultEntityResolution() {
26-
this.hasQualifiedName(_, "XercesDOMParser", "disableDefaultEntityResolution")
26+
this.hasQualifiedName(_, "AbstractOMParser", "setDisableDefaultEntityResolution")
2727
}
2828
}
2929

3030
class SetCreateEntityReferenceNodes extends Function {
3131
SetCreateEntityReferenceNodes() {
32-
this.hasQualifiedName(_, "XercesDOMParser", "setCreateEntityReferenceNodes")
32+
this.hasQualifiedName(_, "AbstractDOMParser", "setCreateEntityReferenceNodes")
3333
}
3434
}
3535

@@ -39,18 +39,36 @@ class CreateLSParser extends Function {
3939
}
4040
}
4141

42+
class SetSecurityManager extends Function {
43+
SetSecurityManager() {
44+
this.hasQualifiedName(_, "AbstractDOMParser", "setSecurityManager")
45+
}
46+
}
47+
48+
class SAXParser extends Class {
49+
SAXParser() { this.hasName("SAXParser") }
50+
}
51+
4252
class XercesXXEConfiguration extends DataFlow::Configuration {
4353
XercesXXEConfiguration() { this = "XercesXXEConfiguration" }
4454

45-
override predicate isSource(DataFlow::Node node) {
55+
override predicate isSource(DataFlow::Node node, string flowstate) {
4656
exists(CallInstruction call |
4757
node.asInstruction().(WriteSideEffectInstruction).getDestinationAddress() = call.getThisArgument() and
48-
call.getStaticCallTarget().(Constructor).getDeclaringType() instanceof XercesDOMParser
58+
call.getStaticCallTarget().(Constructor).getDeclaringType() instanceof XercesDOMParser and
59+
flowstate = "XercesDOM"
4960
)
5061
or
5162
exists(Call call |
5263
call.getTarget() instanceof CreateLSParser and
53-
call = node.asExpr()
64+
call = node.asExpr() and
65+
flowstate = "XercesDOM"
66+
)
67+
or
68+
exists(CallInstruction call |
69+
node.asInstruction().(WriteSideEffectInstruction).getDestinationAddress() = call.getThisArgument() and
70+
call.getStaticCallTarget().(Constructor).getDeclaringType() instanceof SAXParser and
71+
flowstate = "SAXParser"
5472
)
5573
}
5674

@@ -62,23 +80,40 @@ class XercesXXEConfiguration extends DataFlow::Configuration {
6280
)
6381
}
6482

65-
override predicate isBarrier(DataFlow::Node node) {
66-
exists(Call first, Call second |
83+
override predicate isAdditionalFlowStep(DataFlow::Node node1, string state1, DataFlow::Node node2, string state2) {
84+
exists(Call call |
85+
node1.asConvertedExpr() = call.getQualifier() and
86+
node2.asDefiningArgument() = call.getQualifier() and
87+
(
88+
call.getTarget() instanceof DisableDefaultEntityResolution and
89+
state1 = "XercesDOM" and
90+
state2 = "XercesDOM-DDER"
91+
or
92+
call.getTarget() instanceof SetCreateEntityReferenceNodes and
93+
state1 = "XercesDOM" and
94+
state2 = "XercesDOM-SCERN"
95+
)
96+
)
97+
}
98+
99+
override predicate isBarrier(DataFlow::Node node, string flowstate) {
100+
exists(Call call |
67101
(
68-
first.getTarget() instanceof DisableDefaultEntityResolution and
69-
second.getTarget() instanceof SetCreateEntityReferenceNodes
102+
flowstate = "XercesDOM-DDER" and
103+
call.getTarget() instanceof SetCreateEntityReferenceNodes
70104
or
71-
first.getTarget() instanceof SetCreateEntityReferenceNodes and
72-
second.getTarget() instanceof DisableDefaultEntityResolution
105+
flowstate = "XercesDOM-SCERN" and
106+
call.getTarget() instanceof DisableDefaultEntityResolution
73107
) and
74-
DataFlow::localExprFlow(first.getQualifier(), second.getQualifier()) and
75-
second.getQualifier() = node.asDefiningArgument()
108+
call.getQualifier() = node.asDefiningArgument()
76109
)
77110
or
78111
exists(Call setSecurityManager |
79112
// todo: security manager setup
80-
setSecurityManager.getQualifier() = node.asDefiningArgument()
113+
setSecurityManager.getQualifier() = node.asDefiningArgument() and
114+
setSecurityManager.getTarget() instanceof SetSecurityManager
81115
)
116+
//or
82117
}
83118
}
84119

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-611/XercesXXE.ql
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class AbstractDOMParser {
2+
public:
3+
AbstractDOMParser();
4+
void setDisableDefaultEntityResolution(bool);
5+
void setCreateEntityReferenceNodes(bool);
6+
void setSecurityManager();
7+
void parse();
8+
}
9+
10+
class XercesDOMParser: public AbstractDOMParser {
11+
public:
12+
XercesDOMParser();
13+
}
14+
15+
class LSParser: public AbstractDOMParser {
16+
17+
}
18+
19+
LSParser createLSParser();
20+
21+
void test1() {
22+
XercesDOMParser p = new XercesDOMParser();
23+
p.parse() // BAD
24+
}
25+
26+
void test2() {
27+
XercesDOMParser p = new XercesDOMParser();
28+
p.setDisableDefaultEntityResolution(true);
29+
p.parse() // GOOD
30+
}
31+
32+
void test3() {
33+
LSParser p = createLSParser();
34+
p.parse() // BAD
35+
}
36+
37+
void test2() {
38+
LSParser p = createLSParser();
39+
p.setDisableDefaultEntityResolution(true);
40+
p.parse() // GOOD
41+
}

0 commit comments

Comments
 (0)