|
| 1 | +import java.io.ObjectInputFilter; |
1 | 2 | import java.io.ObjectInputStream;
|
2 | 3 | import java.rmi.Naming;
|
3 | 4 | import java.rmi.Remote;
|
4 | 5 | import java.rmi.RemoteException;
|
5 | 6 | import java.rmi.registry.LocateRegistry;
|
6 | 7 | import java.rmi.registry.Registry;
|
| 8 | +import java.rmi.server.UnicastRemoteObject; |
7 | 9 |
|
8 | 10 | public class UnsafeDeserializationRmi {
|
9 | 11 |
|
10 |
| - // BAD (bind a remote object that has a vulnerable method that takes Object) |
| 12 | + // BAD (bind a remote object that has a vulnerable method) |
11 | 13 | public static void testRegistryBindWithObjectParameter() throws Exception {
|
12 | 14 | Registry registry = LocateRegistry.createRegistry(1099);
|
13 |
| - registry.bind("test", new RemoteObjectWithObject()); |
14 |
| - registry.rebind("test", new RemoteObjectWithObject()); |
| 15 | + registry.bind("unsafe", new UnsafeRemoteObjectImpl()); |
| 16 | + registry.rebind("unsafe", new UnsafeRemoteObjectImpl()); |
| 17 | + registry.rebind("unsafe", UnicastRemoteObject.exportObject(new UnsafeRemoteObjectImpl())); |
15 | 18 | }
|
16 | 19 |
|
17 | 20 | // GOOD (bind a remote object that has methods that takes safe parameters)
|
18 | 21 | public static void testRegistryBindWithIntParameter() throws Exception {
|
19 | 22 | Registry registry = LocateRegistry.createRegistry(1099);
|
20 |
| - registry.bind("test", new SafeRemoteObject()); |
21 |
| - registry.rebind("test", new SafeRemoteObject()); |
| 23 | + registry.bind("safe", new SafeRemoteObjectImpl()); |
| 24 | + registry.rebind("safe", new SafeRemoteObjectImpl()); |
22 | 25 | }
|
23 | 26 |
|
24 |
| - // BAD (bind a remote object that has a vulnerable method that takes Object) |
| 27 | + // BAD (bind a remote object that has a vulnerable method) |
25 | 28 | public static void testNamingBindWithObjectParameter() throws Exception {
|
26 |
| - Naming.bind("test", new RemoteObjectWithObject()); |
27 |
| - Naming.rebind("test", new RemoteObjectWithObject()); |
| 29 | + Naming.bind("unsafe", new UnsafeRemoteObjectImpl()); |
| 30 | + Naming.rebind("unsafe", new UnsafeRemoteObjectImpl()); |
28 | 31 | }
|
29 | 32 |
|
30 | 33 | // GOOD (bind a remote object that has methods that takes safe parameters)
|
31 | 34 | public static void testNamingBindWithIntParameter() throws Exception {
|
32 |
| - Naming.bind("test", new SafeRemoteObject()); |
33 |
| - Naming.rebind("test", new SafeRemoteObject()); |
| 35 | + Naming.bind("safe", new SafeRemoteObjectImpl()); |
| 36 | + Naming.rebind("safe", new SafeRemoteObjectImpl()); |
| 37 | + } |
| 38 | + |
| 39 | + // GOOD (bind a remote object with a deserialization filter) |
| 40 | + public static void testRegistryBindWithDeserializationFilter() throws Exception { |
| 41 | + Registry registry = LocateRegistry.createRegistry(1099); |
| 42 | + ObjectInputFilter filter = info -> { |
| 43 | + if (info.serialClass().getCanonicalName().startsWith("com.safe.package.")) { |
| 44 | + return ObjectInputFilter.Status.ALLOWED; |
| 45 | + } |
| 46 | + return ObjectInputFilter.Status.REJECTED; |
| 47 | + }; |
| 48 | + registry.rebind("safe", UnicastRemoteObject.exportObject(new UnsafeRemoteObjectImpl(), 12345, filter)); |
34 | 49 | }
|
35 | 50 | }
|
36 | 51 |
|
37 |
| -interface RemoteObjectWithObjectInterface extends Remote { |
| 52 | +interface UnsafeRemoteObject extends Remote { |
38 | 53 | void take(Object obj) throws RemoteException;
|
39 | 54 | }
|
40 | 55 |
|
41 |
| -class RemoteObjectWithObject implements RemoteObjectWithObjectInterface { |
| 56 | +class UnsafeRemoteObjectImpl implements UnsafeRemoteObject { |
42 | 57 | public void take(Object obj) throws RemoteException {}
|
43 | 58 | }
|
44 | 59 |
|
45 |
| -interface SafeRemoteObjectInterface extends Remote { |
| 60 | +interface SafeRemoteObject extends Remote { |
46 | 61 | void take(int n) throws RemoteException;
|
47 | 62 | void take(double n) throws RemoteException;
|
48 | 63 | void take(String s) throws RemoteException;
|
49 | 64 | void take(ObjectInputStream ois) throws RemoteException;
|
50 | 65 | }
|
51 | 66 |
|
52 |
| -class SafeRemoteObject implements SafeRemoteObjectInterface { |
| 67 | +class SafeRemoteObjectImpl implements SafeRemoteObject { |
53 | 68 | public void take(int n) throws RemoteException {}
|
54 | 69 | public void take(double n) throws RemoteException {}
|
55 | 70 | public void take(String s) throws RemoteException {}
|
56 | 71 | public void take(ObjectInputStream ois) throws RemoteException {}
|
57 |
| - public void safeMethod(Object object) {} // this method is not declared in SafeRemoteObjectInterface |
| 72 | + public void safeMethod(Object object) {} // this method is not declared in SafeRemoteObject |
58 | 73 | }
|
0 commit comments