Skip to content

Commit 8e4bd1a

Browse files
committed
Add sink for ObjectInput.readObject to make test pass
1 parent 34fae32 commit 8e4bd1a

File tree

4 files changed

+78
-52
lines changed

4 files changed

+78
-52
lines changed

java/ql/lib/semmle/code/java/JDK.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ class TypeObjectOutputStream extends RefType {
211211
TypeObjectOutputStream() { this.hasQualifiedName("java.io", "ObjectOutputStream") }
212212
}
213213

214+
/** The type `java.io.ObjectInput`. */
215+
class TypeObjectInput extends RefType {
216+
TypeObjectInput() { this.hasQualifiedName("java.io", "ObjectInput") }
217+
}
218+
214219
/** The type `java.io.ObjectInputStream`. */
215220
class TypeObjectInputStream extends RefType {
216221
TypeObjectInputStream() { this.hasQualifiedName("java.io", "ObjectInputStream") }

java/ql/lib/semmle/code/java/security/UnsafeDeserializationQuery.qll

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ private import semmle.code.java.frameworks.google.Gson
2323
private import semmle.code.java.frameworks.apache.Lang
2424
private import semmle.code.java.Reflection
2525

26-
private class ObjectInputStreamReadObjectMethod extends Method {
27-
ObjectInputStreamReadObjectMethod() {
26+
private class ObjectInputReadObjectMethod extends Method {
27+
ObjectInputReadObjectMethod() {
28+
this.getDeclaringType().getASourceSupertype*() instanceof TypeObjectInput and
29+
this.hasName("readObject")
30+
}
31+
}
32+
33+
private class ObjectInputStreamReadUnsharedMethod extends Method {
34+
ObjectInputStreamReadUnsharedMethod() {
2835
this.getDeclaringType().getASourceSupertype*() instanceof TypeObjectInputStream and
29-
(this.hasName("readObject") or this.hasName("readUnshared"))
36+
this.hasName("readUnshared")
3037
}
3138
}
3239

@@ -147,7 +154,11 @@ private module SafeKryoFlow = DataFlow::Global<SafeKryoConfig>;
147154
*/
148155
predicate unsafeDeserialization(MethodCall ma, Expr sink) {
149156
exists(Method m | m = ma.getMethod() |
150-
m instanceof ObjectInputStreamReadObjectMethod and
157+
m instanceof ObjectInputReadObjectMethod and
158+
sink = ma.getQualifier() and
159+
not DataFlow::exprNode(sink).getTypeBound() instanceof SafeObjectInputStreamType
160+
or
161+
m instanceof ObjectInputStreamReadUnsharedMethod and
151162
sink = ma.getQualifier() and
152163
not DataFlow::exprNode(sink).getTypeBound() instanceof SafeObjectInputStreamType
153164
or

java/ql/test/query-tests/security/CWE-502/A.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public Object deserialize1a(Socket sock) throws java.io.IOException, ClassNotFou
2020
}
2121

2222
public Object deserialize2() throws java.io.IOException, ClassNotFoundException {
23-
ObjectInput objectInput = A.getTaintedObjectInput(); // $ MISSING: Source
24-
return objectInput.readObject(); // $ MISSING: Alert
23+
ObjectInput objectInput = A.getTaintedObjectInput(); // $ Source
24+
return objectInput.readObject(); // $ Alert
2525
}
2626

2727
public Object deserialize3() throws java.io.IOException, ClassNotFoundException {
28-
MyObjectInput objectInput = A.getTaintedMyObjectInput(); // $ MISSING: Source
29-
return objectInput.readObject(); // $ MISSING: Alert
28+
MyObjectInput objectInput = A.getTaintedMyObjectInput(); // $ Source
29+
return objectInput.readObject(); // $ Alert
3030
}
3131

3232
public Object deserialize4(Socket sock) throws java.io.IOException, ClassNotFoundException {

0 commit comments

Comments
 (0)