Skip to content

Commit f6ba4e0

Browse files
authored
Merge pull request github#6142 from artem-smotrakov/better-spring-exporters
Added sinks for RmiBasedExporter and HessianExporter
2 parents 469e709 + 0dfb869 commit f6ba4e0

File tree

10 files changed

+79
-21
lines changed

10 files changed

+79
-21
lines changed

java/ql/src/experimental/Security/CWE/CWE-502/UnsafeSpringExporterLib.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ import java
55
*/
66
predicate isRemoteInvocationSerializingExporter(RefType type) {
77
type.getASupertype*()
8-
.hasQualifiedName("org.springframework.remoting.rmi", "RemoteInvocationSerializingExporter")
8+
.hasQualifiedName("org.springframework.remoting.rmi",
9+
["RemoteInvocationSerializingExporter", "RmiBasedExporter"]) or
10+
type.getASupertype*().hasQualifiedName("org.springframework.remoting.caucho", "HessianExporter")
911
}

java/ql/src/experimental/Security/CWE/CWE-502/UnsafeSpringExporterQuery.inc.qhelp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55

66
<overview>
77
<p>
8-
The Spring Framework provides an abstract base class <code>RemoteInvocationSerializingExporter</code>
9-
for creating remote service exporters.
10-
A Spring exporter, which is based on this class, deserializes incoming data using <code>ObjectInputStream</code>.
8+
The Spring Framework provides several classes for creating remote service exporters.
9+
Under the hood, the exporters use various deserialization mechanisms
10+
such as <code>ObjectInputStream</code> or Hessian.
1111
Deserializing untrusted data is easily exploitable and in many cases allows an attacker
12-
to execute arbitrary code.
13-
</p>
14-
<p>
15-
The Spring Framework also provides <code>HttpInvokerServiceExporter</code>
16-
and <code>SimpleHttpInvokerServiceExporter</code> classes
17-
that extend <code>RemoteInvocationSerializingExporter</code>.
12+
to execute arbitrary code. If a remote attacker can reach endpoints created by the exporters,
13+
it results in remote code execution in the worst case.
1814
</p>
15+
1916
<p>
20-
These classes export specified beans as HTTP endpoints that deserialize data from an HTTP request
21-
using unsafe <code>ObjectInputStream</code>. If a remote attacker can reach such endpoints,
22-
it results in remote code execution in the worst case.
17+
Examples of unsafe exporters include: <code>HttpInvokerServiceExporter</code>,
18+
<code>SimpleHttpInvokerServiceExporter</code>, <code>RmiServiceExporter</code>,
19+
<code>HessianServiceExporter</code>.
2320
</p>
2421
<p>
2522
CVE-2016-1000027 has been assigned to this issue in the Spring Framework.
@@ -29,13 +26,11 @@ It is regarded as a design limitation, and can be mitigated but not fixed outrig
2926

3027
<recommendation>
3128
<p>
32-
Avoid using <code>HttpInvokerServiceExporter</code>, <code>SimpleHttpInvokerServiceExporter</code>
33-
and any other exporter that is based on <code>RemoteInvocationSerializingExporter</code>.
34-
Instead, use other message formats for API endpoints (for example, JSON),
29+
Avoid using unsafe service exporters. Instead, use other message formats for API endpoints (for example, JSON),
3530
but make sure that the underlying deserialization mechanism is properly configured
3631
so that deserialization attacks are not possible. If the vulnerable exporters can not be replaced,
3732
consider using global deserialization filters introduced in JEP 290.
3833
</p>
3934
</recommendation>
4035

41-
</qhelp>
36+
</qhelp>

java/ql/test/experimental/query-tests/security/CWE-502/SpringExporterUnsafeDeserialization.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@
22
import org.springframework.boot.autoconfigure.SpringBootApplication;
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.remoting.caucho.HessianServiceExporter;
56
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
67
import org.springframework.remoting.rmi.RemoteInvocationSerializingExporter;
8+
import org.springframework.remoting.rmi.RmiServiceExporter;
79

810
@Configuration
911
public class SpringExporterUnsafeDeserialization {
1012

13+
@Bean(name = "/unsafeRmiServiceExporter")
14+
RmiServiceExporter unsafeRmiServiceExporter() {
15+
RmiServiceExporter exporter = new RmiServiceExporter();
16+
exporter.setServiceInterface(AccountService.class);
17+
exporter.setService(new AccountServiceImpl());
18+
exporter.setServiceName(AccountService.class.getSimpleName());
19+
exporter.setRegistryPort(1099);
20+
return exporter;
21+
}
22+
23+
@Bean(name = "/unsafeHessianServiceExporter")
24+
HessianServiceExporter unsafeHessianServiceExporter() {
25+
HessianServiceExporter exporter = new HessianServiceExporter();
26+
exporter.setService(new AccountServiceImpl());
27+
exporter.setServiceInterface(AccountService.class);
28+
return exporter;
29+
}
30+
1131
@Bean(name = "/unsafeHttpInvokerServiceExporter")
1232
HttpInvokerServiceExporter unsafeHttpInvokerServiceExporter() {
1333
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
| SpringExporterUnsafeDeserialization.java:12:32:12:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
2-
| SpringExporterUnsafeDeserialization.java:20:41:20:88 | unsafeCustomeRemoteInvocationSerializingExporter | Unsafe deserialization in a Spring exporter bean '/unsafeCustomeRemoteInvocationSerializingExporter' |
3-
| SpringExporterUnsafeDeserialization.java:36:32:36:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
4-
| SpringExporterUnsafeDeserialization.java:48:32:48:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
1+
| SpringExporterUnsafeDeserialization.java:14:24:14:47 | unsafeRmiServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeRmiServiceExporter' |
2+
| SpringExporterUnsafeDeserialization.java:24:28:24:55 | unsafeHessianServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHessianServiceExporter' |
3+
| SpringExporterUnsafeDeserialization.java:32:32:32:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
4+
| SpringExporterUnsafeDeserialization.java:40:41:40:88 | unsafeCustomeRemoteInvocationSerializingExporter | Unsafe deserialization in a Spring exporter bean '/unsafeCustomeRemoteInvocationSerializingExporter' |
5+
| SpringExporterUnsafeDeserialization.java:56:32:56:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
6+
| SpringExporterUnsafeDeserialization.java:68:32:68:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| beans.xml:10:5:13:12 | /unsafeBooking | Unsafe deserialization in a Spring exporter bean '/unsafeBooking' |
22
| beans.xml:15:5:18:12 | org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean 'org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter' |
3+
| beans.xml:20:5:24:12 | org.springframework.remoting.rmi.RmiServiceExporter | Unsafe deserialization in a Spring exporter bean 'org.springframework.remoting.rmi.RmiServiceExporter' |
4+
| beans.xml:26:5:29:12 | org.springframework.remoting.caucho.HessianServiceExporter | Unsafe deserialization in a Spring exporter bean 'org.springframework.remoting.caucho.HessianServiceExporter' |

java/ql/test/experimental/query-tests/security/CWE-502/beans.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,15 @@
1616
<property name="service" ref="anotherBookingService"/>
1717
<property name="serviceInterface" value="com.gypsyengineer.api.CabBookingService"/>
1818
</bean>
19+
20+
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
21+
<property name="service" ref="oneMoreBookingService"/>
22+
<property name="serviceInterface" value="com.gypsyengineer.api.CabBookingService"/>
23+
<property name="registryPort" value="1199"/>
24+
</bean>
25+
26+
<bean class="org.springframework.remoting.caucho.HessianServiceExporter">
27+
<property name="service" ref="oneMoreBookingService"/>
28+
<property name="serviceInterface" value="com.gypsyengineer.api.CabBookingService"/>
29+
</bean>
1930
</beans>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.springframework.remoting.caucho;
2+
3+
public class HessianExporter {
4+
5+
public void setService(Object service) {}
6+
7+
public void setServiceInterface(Class clazz) {}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.springframework.remoting.caucho;
2+
3+
public class HessianServiceExporter extends HessianExporter {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springframework.remoting.rmi;
2+
3+
public abstract class RmiBasedExporter {
4+
5+
public void setService(Object service) {}
6+
7+
public void setServiceInterface(Class clazz) {}
8+
9+
public void setServiceName(String name) {}
10+
11+
public void setRegistryPort(int port) {}
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.springframework.remoting.rmi;
2+
3+
public class RmiServiceExporter extends RmiBasedExporter {}

0 commit comments

Comments
 (0)