Skip to content

Commit 1b51dd4

Browse files
Added an example with deserialization filter to UnsafeDeserializationRmi.qhelp
1 parent c837605 commit 1b51dd4

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public void bindRemoteObject(Registry registry, int port) throws Exception {
2+
ObjectInputFilter filter = info -> {
3+
if (info.serialClass().getCanonicalName().startsWith("com.safe.package.")) {
4+
return ObjectInputFilter.Status.ALLOWED;
5+
}
6+
return ObjectInputFilter.Status.REJECTED;
7+
};
8+
registry.bind("safer", UnicastRemoteObject.exportObject(new RemoteObjectImpl(), port, filter));
9+
}

java/ql/src/experimental/Security/CWE/CWE-502/RmiSafeRemoteObject.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public class Server {
2-
public static void main(String... args) throws Exception {
3-
Registry registry = LocateRegistry.createRegistry(1099);
2+
public void bindRemoteObject(Registry registry) throws Exception {
43
registry.bind("safe", new RemoteObjectImpl());
54
}
65
}

java/ql/src/experimental/Security/CWE/CWE-502/RmiUnsafeRemoteObject.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public class Server {
2-
public static void main(String... args) throws Exception {
3-
Registry registry = LocateRegistry.createRegistry(1099);
2+
public void bindRemoteObject(Registry registry) throws Exception {
43
registry.bind("unsafe", new RemoteObjectImpl());
54
}
65
}

java/ql/src/experimental/Security/CWE/CWE-502/UnsafeDeserializationRmi.qhelp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,44 @@ In the worst case, it results in remote code execution.
1515
<p>
1616
Use only strings and primitive types in parameters of remote objects.
1717
</p>
18+
</p>
19+
Set a filter for incoming serialized data by wrapping remote objects using either <code>UnicastRemoteObject.exportObject(Remote, int, ObjectInputFilter)</code>
20+
or <code>UnicastRemoteObject.exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory, ObjectInputFilter)</code> methods.
21+
Those methods accept an <code>ObjectInputFilter</code> that decides which classes are allowed for deserialization.
22+
The filter should allow deserializing only safe classes.
23+
</p>
1824
<p>
19-
Java RMI does not offer API for specifying classes which are only allowed for deserialization.
20-
However, it is possible to set a process-wide deserialization filter that was introduced in JEP 290.
21-
The filter can be set via system or security property <code>jdk.serialFilter</code>.
25+
It is also possible to set a process-wide deserialization filter.
26+
The filter can be set by with <code>ObjectInputFilter.Config.setSerialFilter(ObjectInputFilter)</code> method,
27+
or by setting system or security property <code>jdk.serialFilter</code>.
2228
Make sure that you use the latest Java versions that include JEP 290.
2329
</p>
2430
<p>
25-
Consider using other implementations of remote procedure calls. For example, HTTP API with JSON.
31+
If switching to the latest Java versions is not possible,
32+
consider using other implementations of remote procedure calls. For example, HTTP API with JSON.
2633
Make sure that the underlying deserialization mechanism is properly configured
2734
so that deserialization attacks are not possible.
2835
</p>
2936
</recommendation>
3037

3138
<example>
3239
<p>
33-
The following code registers a vulnerable remote object
34-
which has a method that accepts a complex object:
40+
The following code registers a remote object
41+
with a vulnerable method that accepts a complex object:
3542
</p>
3643
<sample src="RmiUnsafeRemoteObject.java" />
3744

3845
<p>
3946
The next example registers a safe remote object
40-
which has methods that use only primitive types and strings:
47+
whose methods use only primitive types and strings:
4148
</p>
4249
<sample src="RmiSafeRemoteObject.java" />
4350

51+
<p>
52+
The next example shows how to set a deserilization filter for a remote object:
53+
</p>
54+
<sample src="RmiRemoteObjectWithFilter.java" />
55+
4456
</example>
4557

4658
<references>

0 commit comments

Comments
 (0)