Discussion:
[SECURITY ADVISORY] curl: FTP path trickery leads to NIL byte out of bounds write
Daniel Stenberg
2018-03-14 06:55:04 UTC
Permalink
FTP path trickery leads to NIL byte out of bounds write
=======================================================

Project curl Security Advisory, March 14th 2018 -
[Permalink](https://curl.haxx.se/docs/adv_2018-9cd6.html)

VULNERABILITY
-------------

curl can be fooled into writing a zero byte out of bounds.

This bug can trigger when curl is told to work on an FTP URL, with the setting
to only issue a single CWD command (`--ftp-method singlecwd` or the libcurl
alternative `CURLOPT_FTP_FILEMETHOD`).

curl then URL-decodes the given path, calls strlen() on the result and deducts
the length of the file name part to find the end of the directory within the
buffer. It then writes a zero byte on that index, in a buffer allocated on the
heap.

If the directory part of the URL contains a "%00" sequence, the directory
length might end up shorter than the file name path, making the calculation
`size_t index = directory_len - filepart_len` end up with a huge index
variable for where the zero byte gets stored: `heap_buffer[index] = 0`. On
several architectures that huge index will wrap and work as a negative value,
thus overwriting memory *before* the intended heap buffer.

By using different file part lengths and putting %00 in different places in
the URL, an attacker that can control what paths a curl-using application uses
can write that zero byte on different indexes.

We are not aware of any exploit of this flaw.

INFO
----

This bug was introduced in December 2004 in [this
commit](https://github.com/curl/curl/commit/6e1e9caa32da0995).

The Common Vulnerabilities and Exposures (CVE) project has assigned the name
CVE-2018-1000120 to this issue.

CWE-122: Heap-based Buffer Overflow

AFFECTED VERSIONS
-----------------

- Affected versions: curl 7.12.3 to and including curl 7.58.0
- Not affected versions: curl < 7.12.3 and curl >= 7.59.0

libcurl is used by many applications, but not always advertised as such.

THE SOLUTION
------------

In curl version 7.59.0, curl rejects FTP URLs that contain any "control
characters". That is byte values below ascii 32.

A [patch for CVE-2018-1000120](https://curl.haxx.se/CVE-2018-1000120.patch) is available.

RECOMMENDATIONS
---------------

We suggest you take one of the following actions immediately, in order of
preference:

A - Upgrade curl to version 7.59.0

B - Apply the patch to your version and rebuild

C - Do not enable singlecwd mode for FTP transfers

TIME LINE
---------

It was reported to the curl project on January 29, 2018

We contacted ***@openwall on March 7, 2018.

curl 7.59.0 was released on March 14 2018, coordinated with the publication
of this advisory.

CREDITS
-------

Reported by Duy Phan Thanh. Patch by Daniel Stenberg.

Thanks a lot!
--
/ daniel.haxx.se
-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette: htt
Loading...