SpiderLabs Blog

CVE-2014-0050: Exploit with Boundaries, Loops without Boundaries

Written by Oren Hafif | Feb 11, 2014 8:43:00 AM

In this article I will discuss CVE-2014-0050: Apache Commons FileUpload and Apache Tomcat Denial-of-Service in detail. The article reviews the vulnerability's technical aspects in depth and includes recommendations that can help administrators defend from future exploitation of this security issue.

How do we know about this vulnerability?

About five days ago, Mark Thomas, a Project Management Committee Member and Committer in the Apache Tomcat project, sent an email about the accidentally leaked vulnerability (CVE-2014-0050) which allows attackers to take down your Tomcat-deployed web sites and web services. "This issue was reported responsibly to the Apache Software Foundation via JPCERT but an error in addressing an e-mail led to the unintended early disclosure of this issue," Thomas said in his email.

An hour prior to sending the email, Thomas populated the Apache Commons FileUpload project SVN with code fixes. The most significant difference I detected was in the "MultipartStream" class code of the package:

http://svn.apache.org/viewvc/commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/MultipartStream.java?r1=1564788&r2=1565143&diff_format=h

The fixed code has an extra "if" condition (line number 330) that validates the length of the multipart boundary to be shorter than 4091 characters, raising an exception if that's not the case. The calculation is as follows:

boundary.length > bufSize – 1 – BOUNDARY_PREFIX.length = 4096 – 1 – 4 = 4091

Note: parts of the code were copied into the org.apache.tomcat.util.http.fileupload package in Apache Tomcat, causing it to be affected.

What is Multipart?

Multipart is often used in HTTP for the purpose of uploading files (more details can be found in RFC 2046). When your application needs to send a file to the server that has binary data, or data that might interfere with the structure of the HTTP message, it is possible to use a multipart content-type and setup a magic line to separate between values. This magic line is the boundary value and should be random enough that it doesn't appear in the actual content sent.

Note the complexity of the boundary value in the following example of a file upload request generated by Google Chrome. Such complexity reduces the chances of the boundary appearing in the binary blob of the file being uploaded, so the different parts of the message can be easily detected even by the human eye:

Creating the exploit

So let's get Apache Tomcat installed and try to send more than 4091 characters in the boundary field to the Apache Tomcat Manager application. Such a request might look like this:

I submitted the above request four times (one for each CPU) and this was the result:



The Tomcat process reached 100 percent CPU usage, and remained at levels higher than 95 percent CPU until I stopped the service.

Why is this happening?

When you are writing loops, check your boundaries. When you are writing endless loops, check your boundaries even more carefully.

While parsing the multipart message, the following "for" loop is used by the MultipartStream class:

The innocent-looking "for" loop above is an endless loop. It is "family related" to the famous "while(true)" loop. The developer's intention was to exit this loop either by raising an exception (line 1003) or by returning a value (line 1014), unfortunately when the boundary is longer than 4091 characters (as explained earlier) and the body is longer than 4096 characters (so it can potentially contain the boundary), neither would ever occur.

The Irony

I have to say a few words about the Irony. We can safely say that this bug is all about a field read from the request which the programmer failed to validate its permitted boundaries. However, the field's name is Boundary.

How to tell if you are vulnerable?

1) You are using Apache Tomcat 7.0-7.0.50 or 8.0-8.0.1 and your code utilizes Servlet 3.0 specifications (for example, "request.getPart" or "request.getParts" methods)

2) You are using Apache Commons FileUpload < 1.3.1

To be honest, these libraries are so commonly used that you might not even know that your site is vulnerable.

I wrote a small Ruby proof-of-concept exploit to help administrators try and recreate the Denial-of-Service state on QA and staging environments. This can help administrators and developers understand if a certain URL is vulnerable to the attack (but needs to be tested on all URLs).

The tool can also assist white-hat security professionals that are required to confirm the vulnerability throughout an engagement.

Download CVE-2014-0050

 

Defend yourself

Trustwave Spiderlabs research team recommends taking the following actions:

A) Once available, update your software to one of the following versions:

    1. Apache Commons FileUpload 1.3.1
    2. Apache Tomcat 7.0.51
    3. Apache Tomcat 8.0.2

B) You may choose to apply the appropriate patch:

  1. Apache Commons FileUpload: http://svn.apache.org/r1565143
  2. Apache Tomcat 8: http://svn.apache.org/r1565163
  3. Apache Tomcat 7: http://svn.apache.org/r1565169

 

ModSecurity Commercial Rule Set

The ModSecurity commercial rule set was updated and protects against the attack.

 

WebDefend

It is possible to add the following user defined rule in block mode to detect and mitigate the attack:

SecRule REQUEST_HEADERS:Content-Type "@rx .{4000}"

For more information on how to create user-defined rules, please refer to http://blog.spiderlabs.com/2013/04/restricting-adobe-cq-admin-logins-with-trustwave-wafs.html

Note: Due to the low chances of a false detection (false positive) and the simplicity of the exploit, we highly recommend setting this rule to BLOCK and disabling simulation mode for this rule if you are concerned about Denial-of-Service (and suspect that your site is using one of the affected technologies).