-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCVE-2013-7285-query.ql
More file actions
202 lines (189 loc) · 7.88 KB
/
Copy pathCVE-2013-7285-query.ql
File metadata and controls
202 lines (189 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* @name XStream deserialization without security framework - Internal method tracking
* @description Tracks internal XStream method calls that lack security validation in vulnerable versions.
* @problem.severity error
* @security-severity 9.8
* @precision high
* @tags security
* @kind path-problem
* @id java/xstream-unsafe-deserialization-internal
*/
import java
import semmle.code.java.dataflow.DataFlow
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.dataflow.TaintTracking
private import semmle.code.java.dataflow.ExternalFlow
class Source extends DataFlow::Node {
Source() {
// Internal method calls in XStream mapper chain
exists(MethodCall ma |
(
// CachingMapper.realClass method calls
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream.mapper", "CachingMapper") and
ma.getMethod().hasName("realClass")
or
// XStream internal setup methods
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma.getMethod().hasName(["setupAliases", "buildMapperDynamically", "buildMapper"])
or
// FileConverter method calls
ma.getMethod().getDeclaringType().hasName("FileConverter") and
ma.getMethod().hasName("flushCache")
or
// Mapper chain method calls
exists(Method m | m = ma.getMethod() |
m.getDeclaringType().getPackage().hasName("com.thoughtworks.xstream.mapper") and
m.hasName(["realClass", "mapperOfType", "lookupMapperOfType"])
)
or
// XStream deserialization entry points
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma.getMethod().hasName(["fromXML", "unmarshal"])
) and
this.asExpr() = ma
)
or
// Arguments to internal methods that handle class resolution
exists(MethodCall ma |
(
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream.mapper", "CachingMapper") and
ma.getMethod().hasName("realClass")
or
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma.getMethod().hasName(["setupAliases", "buildMapperDynamically"])
) and
this.asExpr() = ma.getAnArgument()
)
or
// Active threat model sources for completeness
this instanceof ActiveThreatModelSource
}
}
class Sink extends DataFlow::Node {
Sink() {
// Internal method calls that perform unsafe operations in vulnerable versions
exists(MethodCall ma |
(
// CachingMapper.realClass without security validation
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream.mapper", "CachingMapper") and
ma.getMethod().hasName("realClass") and
not hasSecurityValidation(ma)
or
// XStream setup methods that don't configure security
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma.getMethod().hasName(["setupAliases", "buildMapperDynamically", "buildMapper"]) and
not hasSecurityMapperInChain(ma)
or
// FileConverter methods without validation
ma.getMethod().getDeclaringType().hasName("FileConverter") and
ma.getMethod().hasName("flushCache")
) and
this.asExpr() = ma
)
}
}
predicate hasSecurityValidation(MethodCall ma) {
// Check if SecurityMapper is present in the compilation unit
exists(ImportType imp |
imp.getImportedType().hasQualifiedName("com.thoughtworks.xstream.mapper", "SecurityMapper") and
imp.getCompilationUnit() = ma.getEnclosingCallable().getDeclaringType().getCompilationUnit()
)
or
// Check if TypePermission classes are imported (indicating security framework usage)
exists(ImportType imp |
imp.getImportedType().getPackage().hasName("com.thoughtworks.xstream.security") and
imp.getCompilationUnit() = ma.getEnclosingCallable().getDeclaringType().getCompilationUnit()
)
or
// Check for SecurityMapper field in the class
exists(Field f |
f.getDeclaringType() = ma.getEnclosingCallable().getDeclaringType() and
f.hasName("securityMapper") and
f.getType().(RefType).hasQualifiedName("com.thoughtworks.xstream.mapper", "SecurityMapper")
)
}
predicate hasSecurityMapperInChain(MethodCall ma) {
// Check if SecurityMapper is configured in the mapper chain
exists(ClassInstanceExpr cie |
cie.getConstructedType().hasQualifiedName("com.thoughtworks.xstream.mapper", "SecurityMapper") and
cie.getEnclosingCallable() = ma.getEnclosingCallable()
)
or
// Check if security imports are present
hasSecurityValidation(ma)
or
// Check if setupSecurity method has been called
exists(MethodCall setupCall |
setupCall.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
setupCall.getMethod().hasName("setupSecurity") and
setupCall.getEnclosingCallable() = ma.getEnclosingCallable()
)
}
class Sanitizer extends DataFlow::Node {
Sanitizer() {
// SecurityMapper validation calls
exists(MethodCall ma |
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream.mapper", "SecurityMapper") and
ma.getMethod().hasName("realClass") and
this.asExpr() = ma
)
or
// TypePermission validation
exists(MethodCall ma |
ma.getMethod().getDeclaringType().getPackage().hasName("com.thoughtworks.xstream.security") and
(ma.getMethod().hasName("allows") or ma.getMethod().hasName("implies")) and
this.asExpr() = ma
)
or
// XStream security setup methods
exists(MethodCall ma |
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma.getMethod().hasName(["setupSecurity", "addPermission"]) and
this.asExpr() = ma
)
or
// SecurityMapper constructor (creating security validation layer)
exists(ClassInstanceExpr cie |
cie.getConstructedType().hasQualifiedName("com.thoughtworks.xstream.mapper", "SecurityMapper") and
this.asExpr() = cie
)
}
}
module MyPathConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof Source }
predicate isSink(DataFlow::Node sink) { sink instanceof Sink }
predicate isBarrier(DataFlow::Node sanitizer) { sanitizer instanceof Sanitizer }
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
// Flow through internal mapper chain calls
exists(MethodCall ma |
ma.getMethod().getDeclaringType().getPackage().hasName("com.thoughtworks.xstream.mapper") and
n1.asExpr() = ma.getAnArgument() and
n2.asExpr() = ma
)
or
// Flow through XStream internal setup methods
exists(MethodCall ma |
ma.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma.getMethod().hasName(["setupAliases", "buildMapperDynamically", "buildMapper"]) and
n1.asExpr() = ma.getAnArgument() and
n2.asExpr() = ma
)
or
// Flow from XStream deserialization to internal methods
exists(MethodCall ma1, MethodCall ma2 |
ma1.getMethod().getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") and
ma1.getMethod().hasName(["fromXML", "unmarshal"]) and
ma2.getMethod().getDeclaringType().getPackage().hasName("com.thoughtworks.xstream.mapper") and
DataFlow::localFlow(DataFlow::exprNode(ma1), DataFlow::exprNode(ma2.getQualifier())) and
n1.asExpr() = ma1 and
n2.asExpr() = ma2
)
}
}
module MyPathFlow = TaintTracking::Global<MyPathConfig>;
import MyPathFlow::PathGraph
from MyPathFlow::PathNode source, MyPathFlow::PathNode sink
where MyPathFlow::flowPath(source, sink)
select sink.getNode(), source, sink,
"Internal XStream method call without security validation allows unsafe class resolution.",
source.getNode(), "unsafe internal method call"