curl / Docs / Security Problems / FTP path trickery leads to NIL byte out of bounds write

FTP path trickery leads to NIL byte out of bounds write

Project curl Security Advisory, March 14th 2018 - Permalink

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.

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

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 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 distros@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!