-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathJDK.qll
More file actions
486 lines (418 loc) · 13.7 KB
/
JDK.qll
File metadata and controls
486 lines (418 loc) · 13.7 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* Provides classes and predicates for working with standard classes and methods from the JDK.
*/
overlay[local?]
module;
import Member
import semmle.code.java.security.ExternalProcess
private import semmle.code.java.dataflow.FlowSteps
// --- Standard types ---
/** The class `java.lang.Object`. */
class TypeObject extends Class {
pragma[noinline]
TypeObject() { this.hasQualifiedName("java.lang", "Object") }
}
/** The interface `java.lang.Cloneable`. */
class TypeCloneable extends Interface {
TypeCloneable() { this.hasQualifiedName("java.lang", "Cloneable") }
}
/** The class `java.lang.ProcessBuilder`. */
class TypeProcessBuilder extends Class {
TypeProcessBuilder() { this.hasQualifiedName("java.lang", "ProcessBuilder") }
}
/** The class `java.lang.Runtime`. */
class TypeRuntime extends Class {
TypeRuntime() { this.hasQualifiedName("java.lang", "Runtime") }
}
/** The class `java.lang.String`. */
class TypeString extends Class {
TypeString() { this.hasQualifiedName("java.lang", "String") }
}
/** The `length()` method of the class `java.lang.String`. */
class StringLengthMethod extends Method {
StringLengthMethod() { this.hasName("length") and this.getDeclaringType() instanceof TypeString }
}
/** The `contains()` method of the class `java.lang.String`. */
class StringContainsMethod extends Method {
StringContainsMethod() {
this.hasName("contains") and this.getDeclaringType() instanceof TypeString
}
}
/** A call to the `java.lang.String.matches` method. */
class StringMatchesCall extends MethodCall, RegexMatch::Range {
StringMatchesCall() {
exists(Method m | m = this.getMethod() |
m.getDeclaringType() instanceof TypeString and
m.hasName("matches")
)
}
override Expr getRegex() { result = this.getArgument(0) }
override Expr getString() { result = this.getQualifier() }
override string getName() { result = "String.matches" }
}
/** A call to the `java.lang.String.replaceAll` method. */
class StringReplaceAllCall extends MethodCall {
StringReplaceAllCall() {
exists(Method m | m = this.getMethod() |
m.getDeclaringType() instanceof TypeString and
m.hasName("replaceAll")
)
}
}
/** A call to the `java.lang.String.replace` method. */
class StringReplaceCall extends MethodCall {
StringReplaceCall() {
exists(Method m | m = this.getMethod() |
m.getDeclaringType() instanceof TypeString and
m.hasName("replace")
)
}
}
/**
* The methods on the class `java.lang.String` that are used to perform partial matches with a specified substring or char.
*/
class StringPartialMatchMethod extends Method {
StringPartialMatchMethod() {
this.hasName([
"contains", "startsWith", "endsWith", "matches", "indexOf", "lastIndexOf", "regionMatches"
]) and
this.getDeclaringType() instanceof TypeString
}
/**
* Gets the index of the parameter that is being matched against.
*/
int getMatchParameterIndex() {
if this.hasName("regionMatches")
then this.getParameterType(result) instanceof TypeString
else result = 0
}
}
/** The class `java.lang.StringBuffer`. */
class TypeStringBuffer extends Class {
TypeStringBuffer() { this.hasQualifiedName("java.lang", "StringBuffer") }
}
/** The class `java.lang.StringBuilder`. */
class TypeStringBuilder extends Class {
TypeStringBuilder() { this.hasQualifiedName("java.lang", "StringBuilder") }
}
/** Class `java.lang.StringBuffer` or `java.lang.StringBuilder`. */
class StringBuildingType extends Class {
StringBuildingType() { this instanceof TypeStringBuffer or this instanceof TypeStringBuilder }
}
/** The class `java.lang.System`. */
class TypeSystem extends Class {
TypeSystem() { this.hasQualifiedName("java.lang", "System") }
}
/** The class `java.lang.Throwable`. */
class TypeThrowable extends Class {
TypeThrowable() { this.hasQualifiedName("java.lang", "Throwable") }
}
/** The class `java.lang.Exception`. */
class TypeException extends Class {
TypeException() { this.hasQualifiedName("java.lang", "Exception") }
}
/** The class `java.lang.Error`. */
class TypeError extends Class {
TypeError() { this.hasQualifiedName("java.lang", "Error") }
}
/** The class `java.lang.RuntimeException`. */
class TypeRuntimeException extends Class {
TypeRuntimeException() { this.hasQualifiedName("java.lang", "RuntimeException") }
}
/** The class `java.lang.ClassCastException`. */
class TypeClassCastException extends Class {
TypeClassCastException() { this.hasQualifiedName("java.lang", "ClassCastException") }
}
/** The class `java.lang.NullPointerException`. */
class TypeNullPointerException extends Class {
TypeNullPointerException() { this.hasQualifiedName("java.lang", "NullPointerException") }
}
/**
* The class `java.lang.Class`.
*
* This includes the generic source declaration, any parameterized instances and the raw type.
*/
class TypeClass extends Class {
TypeClass() { this.getSourceDeclaration().hasQualifiedName("java.lang", "Class") }
}
/**
* The class `java.lang.Constructor`.
*
* This includes the generic source declaration, any parameterized instances and the raw type.
*/
class TypeConstructor extends Class {
TypeConstructor() {
this.getSourceDeclaration().hasQualifiedName("java.lang.reflect", "Constructor")
}
}
/** The class `java.lang.Math`. */
class TypeMath extends Class {
TypeMath() { this.hasQualifiedName("java.lang", "Math") }
}
/** The class `java.lang.Number`. */
class TypeNumber extends RefType {
TypeNumber() { this.hasQualifiedName("java.lang", "Number") }
}
/** A (reflexive, transitive) subtype of `java.lang.Number`. */
class NumberType extends RefType {
pragma[nomagic]
NumberType() { this.getASupertype*() instanceof TypeNumber }
}
/** An immutable type. */
class ImmutableType extends Type {
ImmutableType() {
this instanceof PrimitiveType or
this instanceof NullType or
this instanceof VoidType or
this instanceof BoxedType or
this instanceof TypeString
}
}
// --- Java IO ---
/** The interface `java.io.Serializable`. */
class TypeSerializable extends Interface {
TypeSerializable() { this.hasQualifiedName("java.io", "Serializable") }
}
/** The interface `java.io.ObjectOutput`. */
class TypeObjectOutput extends Interface {
TypeObjectOutput() { this.hasQualifiedName("java.io", "ObjectOutput") }
}
/** The type `java.io.ObjectOutputStream`. */
class TypeObjectOutputStream extends RefType {
TypeObjectOutputStream() { this.hasQualifiedName("java.io", "ObjectOutputStream") }
}
/** The type `java.io.ObjectInput`. */
class TypeObjectInput extends RefType {
TypeObjectInput() { this.hasQualifiedName("java.io", "ObjectInput") }
}
/** The type `java.io.ObjectInputStream`. */
class TypeObjectInputStream extends RefType {
TypeObjectInputStream() { this.hasQualifiedName("java.io", "ObjectInputStream") }
}
/** The class `java.io.InputStream`. */
class TypeInputStream extends RefType {
TypeInputStream() { this.hasQualifiedName("java.io", "InputStream") }
}
/** The class `java.nio.file.Paths`. */
class TypePaths extends Class {
TypePaths() { this.hasQualifiedName("java.nio.file", "Paths") }
}
/** The type `java.nio.file.Path`. */
class TypePath extends RefType {
TypePath() { this.hasQualifiedName("java.nio.file", "Path") }
}
/** The class `java.nio.file.FileSystem`. */
class TypeFileSystem extends Class {
TypeFileSystem() { this.hasQualifiedName("java.nio.file", "FileSystem") }
}
/** The class `java.io.File`. */
class TypeFile extends Class {
TypeFile() { this.hasQualifiedName("java.io", "File") }
}
// --- Standard methods ---
/**
* Any method named `getenv` on class `java.lang.System`.
*/
class MethodSystemGetenv extends Method {
MethodSystemGetenv() {
this.hasName("getenv") and
this.getDeclaringType() instanceof TypeSystem
}
}
/**
* Any method named `getProperty` on class `java.lang.System`.
*/
class MethodSystemGetProperty extends ValuePreservingMethod {
MethodSystemGetProperty() {
this.hasName("getProperty") and
this.getDeclaringType() instanceof TypeSystem
}
override predicate returnsValue(int arg) { arg = 1 }
}
/**
* A call to a method named `getProperty` on class `java.lang.System`.
*/
class MethodCallSystemGetProperty extends MethodCall {
MethodCallSystemGetProperty() { this.getMethod() instanceof MethodSystemGetProperty }
/**
* Holds if this call has a compile-time constant first argument with the value `propertyName`.
* For example: `System.getProperty("user.dir")`.
*
* Note: Better to use `semmle.code.java.environment.SystemProperty#getSystemProperty` instead
* as that predicate covers ways of accessing the same information via various libraries.
*/
predicate hasCompileTimeConstantGetPropertyName(string propertyName) {
this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = propertyName
}
}
/**
* Any method named `exit` on class `java.lang.Runtime` or `java.lang.System`.
*/
class MethodExit extends Method {
MethodExit() {
this.hasName("exit") and
(
this.getDeclaringType() instanceof TypeRuntime or
this.getDeclaringType() instanceof TypeSystem
)
}
}
/**
* A method named `writeObject` on type `java.io.ObjectOutput`
* or `java.io.ObjectOutputStream`.
*/
class WriteObjectMethod extends Method {
WriteObjectMethod() {
this.hasName("writeObject") and
(
this.getDeclaringType() instanceof TypeObjectOutputStream or
this.getDeclaringType() instanceof TypeObjectOutput
)
}
}
/**
* A method that reads an object on type `java.io.ObjectInputStream`,
* including `readObject`, `readObjectOverride`, `readUnshared` and `resolveObject`.
*/
class ReadObjectMethod extends Method {
ReadObjectMethod() {
this.getDeclaringType() instanceof TypeObjectInputStream and
this.hasName(["readObject", "readObjectOverride", "readUnshared", "resolveObject"])
}
}
/** The method `Class.getName()`. */
class ClassNameMethod extends Method {
ClassNameMethod() {
this.hasName("getName") and
this.getDeclaringType() instanceof TypeClass
}
}
/** The method `Class.getSimpleName()`. */
class ClassSimpleNameMethod extends Method {
ClassSimpleNameMethod() {
this.hasName("getSimpleName") and
this.getDeclaringType() instanceof TypeClass
}
}
/** The method `Math.abs`. */
class MethodAbs extends Method {
MethodAbs() {
this.getDeclaringType() instanceof TypeMath and
this.getName() = "abs"
}
}
/** The method `Math.min`. */
class MethodMathMin extends Method {
MethodMathMin() {
this.getDeclaringType() instanceof TypeMath and
this.getName() = "min"
}
}
/** The method `Math.min`. */
class MethodMathMax extends Method {
MethodMathMax() {
this.getDeclaringType() instanceof TypeMath and
this.getName() = "max"
}
}
// --- Standard fields ---
/** The field `System.in`. */
class SystemIn extends Field {
SystemIn() {
this.hasName("in") and
this.getDeclaringType() instanceof TypeSystem
}
}
/** The field `System.out`. */
class SystemOut extends Field {
SystemOut() {
this.hasName("out") and
this.getDeclaringType() instanceof TypeSystem
}
}
/** The field `System.err`. */
class SystemErr extends Field {
SystemErr() {
this.hasName("err") and
this.getDeclaringType() instanceof TypeSystem
}
}
// --- User-defined methods with a particular meaning ---
/** A method with the same signature as `java.lang.Object.equals`. */
class EqualsMethod extends Method {
EqualsMethod() {
this.hasName("equals") and
this.getNumberOfParameters() = 1 and
this.getParameter(0).getType().(RefType).hasQualifiedName("java.lang", "Object")
}
/** Gets the single parameter of this method. */
Parameter getParameter() { result = this.getAParameter() }
}
/** A method with the same signature as `java.lang.Object.hashCode`. */
class HashCodeMethod extends Method {
HashCodeMethod() {
this.hasName("hashCode") and
this.hasNoParameters()
}
}
/** A method with the same signature as `java.lang.Object.clone`. */
class CloneMethod extends Method {
CloneMethod() {
this.hasName("clone") and
this.hasNoParameters()
}
}
/** A method with the same signature as `java.lang.Object.toString`. */
class ToStringMethod extends Method {
ToStringMethod() {
this.hasName("toString") and
this.hasNoParameters()
}
}
/**
* The public static `main` method, with a single formal parameter
* of type `String[]` and return type `void`.
*/
class MainMethod extends Method {
MainMethod() {
this.isPublic() and
this.isStatic() and
this.getReturnType().hasName("void") and
this.hasName("main") and
this.getNumberOfParameters() = 1 and
exists(Array a |
a = this.getAParameter().getType() and
a.getDimension() = 1 and
a.getElementType() instanceof TypeString
)
}
}
/** A premain method is an agent entry-point. */
class PreMainMethod extends Method {
PreMainMethod() {
this.isPublic() and
this.isStatic() and
this.getReturnType().hasName("void") and
this.getNumberOfParameters() < 3 and
this.getParameter(0).getType() instanceof TypeString and
(exists(this.getParameter(1)) implies this.getParameter(1).getType().hasName("Instrumentation"))
}
}
/** The `length` field of the array type. */
class ArrayLengthField extends Field {
ArrayLengthField() {
this.getDeclaringType() instanceof Array and
this.hasName("length")
}
}
/** A (reflexive, transitive) subtype of `java.lang.Throwable`. */
class ThrowableType extends RefType {
ThrowableType() { exists(TypeThrowable throwable | hasDescendant(throwable, this)) }
}
/** An unchecked exception. That is, a (reflexive, transitive) subtype of `java.lang.Error` or `java.lang.RuntimeException`. */
class UncheckedThrowableType extends RefType {
UncheckedThrowableType() {
exists(TypeError e | hasDescendant(e, this)) or
exists(TypeRuntimeException e | hasDescendant(e, this))
}
}