|
| 1 | +/** |
| 2 | + * @name Unsafe deserialization with RMI. |
| 3 | + * @description TBD |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id java/unsafe-deserialization-rmi |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-502 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import semmle.code.java.frameworks.Rmi |
| 14 | + |
| 15 | +private class ObjectInputStream extends RefType { |
| 16 | + ObjectInputStream() { hasQualifiedName("java.io", "ObjectInputStream") } |
| 17 | +} |
| 18 | + |
| 19 | +private class BindMethod extends Method { |
| 20 | + BindMethod() { |
| 21 | + getDeclaringType().hasQualifiedName("java.rmi", "Naming") and |
| 22 | + hasName(["bind", "rebind"]) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +private Method getVulnerableMethod(Type type) { |
| 27 | + type.(RefType).getASupertype*() instanceof TypeRemote and |
| 28 | + exists(Method m, Type parameterType | |
| 29 | + m.getDeclaringType() = type and parameterType = m.getAParamType() |
| 30 | + | |
| 31 | + not parameterType instanceof PrimitiveType and |
| 32 | + not parameterType instanceof TypeString and |
| 33 | + not parameterType instanceof ObjectInputStream and |
| 34 | + result = m |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +private class UnsafeRmiBinding extends MethodAccess { |
| 39 | + Method vulnerableMethod; |
| 40 | + |
| 41 | + UnsafeRmiBinding() { |
| 42 | + this.getMethod() instanceof BindMethod and |
| 43 | + vulnerableMethod = getVulnerableMethod(this.getArgument(1).getType()) |
| 44 | + } |
| 45 | + |
| 46 | + Method getVulnerableMethod() { result = vulnerableMethod } |
| 47 | +} |
| 48 | + |
| 49 | +// TODO: Cover Registry.bind() and rebind() -- test these sinks first |
| 50 | + |
| 51 | +from UnsafeRmiBinding call |
| 52 | +select call, "Unsafe deserialization with RMI in '" + call.getVulnerableMethod() + "' method" |
0 commit comments