Blame SOURCES/0063-curl-7.29.0-CVE-2018-1000120.patch

6311d4
From 5452fdc5ae93f3571074c591fdf28cdf630796a0 Mon Sep 17 00:00:00 2001
6311d4
From: Daniel Stenberg <daniel@haxx.se>
6311d4
Date: Tue, 12 Sep 2017 09:29:01 +0200
6311d4
Subject: [PATCH 1/3] FTP: URL decode path for dir listing in nocwd mode
6311d4
6311d4
Reported-by: Zenju on github
6311d4
6311d4
Test 244 added to verify
6311d4
Fixes #1974
6311d4
Closes #1976
6311d4
6311d4
Upstream-commit: ecf21c551fa3426579463abe34b623111b8d487c
6311d4
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
6311d4
---
6311d4
 lib/ftp.c              | 93 +++++++++++++++++++++++---------------------------
6311d4
 tests/data/Makefile.am |  3 +-
6311d4
 tests/data/test244     | 54 +++++++++++++++++++++++++++++
6311d4
 3 files changed, 99 insertions(+), 51 deletions(-)
6311d4
 create mode 100644 tests/data/test244
6311d4
6311d4
diff --git a/lib/ftp.c b/lib/ftp.c
6311d4
index bcba6bb..fb3a716 100644
6311d4
--- a/lib/ftp.c
6311d4
+++ b/lib/ftp.c
6311d4
@@ -1003,7 +1003,7 @@ static CURLcode ftp_state_use_port(struct connectdata *conn,
6311d4
     char *port_start = NULL;
6311d4
     char *port_sep = NULL;
6311d4
 
6311d4
-    addr = calloc(addrlen+1, 1);
6311d4
+    addr = calloc(addrlen + 1, 1);
6311d4
     if(!addr)
6311d4
       return CURLE_OUT_OF_MEMORY;
6311d4
 
6311d4
@@ -1041,7 +1041,7 @@ static CURLcode ftp_state_use_port(struct connectdata *conn,
6311d4
     /* parse the port */
6311d4
     if(ip_end != NULL) {
6311d4
       if((port_start = strchr(ip_end, ':')) != NULL) {
6311d4
-        port_min = curlx_ultous(strtoul(port_start+1, NULL, 10));
6311d4
+        port_min = curlx_ultous(strtoul(port_start + 1, NULL, 10));
6311d4
         if((port_sep = strchr(port_start, '-')) != NULL) {
6311d4
           port_max = curlx_ultous(strtoul(port_sep + 1, NULL, 10));
6311d4
         }
6311d4
@@ -1469,25 +1469,22 @@ static CURLcode ftp_state_post_listtype(struct connectdata *conn)
6311d4
      then just do LIST (in that case: nothing to do here)
6311d4
   */
6311d4
   char *cmd,*lstArg,*slashPos;
6311d4
+  const char *inpath = data->state.path;
6311d4
 
6311d4
   lstArg = NULL;
6311d4
   if((data->set.ftp_filemethod == FTPFILE_NOCWD) &&
6311d4
-     data->state.path &&
6311d4
-     data->state.path[0] &&
6311d4
-     strchr(data->state.path,'/')) {
6311d4
-
6311d4
-    lstArg = strdup(data->state.path);
6311d4
-    if(!lstArg)
6311d4
-      return CURLE_OUT_OF_MEMORY;
6311d4
+     inpath && inpath[0] && strchr(inpath, '/')) {
6311d4
+    size_t n = strlen(inpath);
6311d4
 
6311d4
     /* Check if path does not end with /, as then we cut off the file part */
6311d4
-    if(lstArg[strlen(lstArg) - 1] != '/')  {
6311d4
-
6311d4
+    if(inpath[n - 1] != '/') {
6311d4
       /* chop off the file part if format is dir/dir/file */
6311d4
-      slashPos = strrchr(lstArg,'/');
6311d4
-      if(slashPos)
6311d4
-        *(slashPos+1) = '\0';
6311d4
+      slashPos = strrchr(inpath, '/');
6311d4
+      n = slashPos - inpath;
6311d4
     }
6311d4
+    result = Curl_urldecode(data, inpath, n, &lstArg, NULL, FALSE);
6311d4
+    if(result)
6311d4
+      return result;
6311d4
   }
6311d4
 
6311d4
   cmd = aprintf( "%s%s%s",
230636
@@ -3328,12 +3325,10 @@ static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
6311d4
   }
6311d4
 
6311d4
   /* get the "raw" path */
6311d4
-  path = curl_easy_unescape(data, path_to_use, 0, NULL);
6311d4
-  if(!path) {
6311d4
+  result = Curl_urldecode(data, path_to_use, 0, &path, NULL, FALSE);
6311d4
+  if(result) {
6311d4
     /* out of memory, but we can limp along anyway (and should try to
6311d4
      * since we may already be in the out of memory cleanup path) */
6311d4
-    if(!result)
6311d4
-      result = CURLE_OUT_OF_MEMORY;
6311d4
     ftpc->ctl_valid = FALSE; /* mark control connection as bad */
6311d4
     conn->bits.close = TRUE; /* mark for connection closure */
6311d4
     ftpc->prevpath = NULL; /* no path remembering */
230636
@@ -3644,7 +3639,7 @@ static CURLcode ftp_range(struct connectdata *conn)
6311d4
     }
6311d4
     else {
6311d4
       /* X-Y */
6311d4
-      data->req.maxdownload = (to-from)+1; /* include last byte */
6311d4
+      data->req.maxdownload = (to - from) + 1; /* include last byte */
6311d4
       data->state.resume_from = from;
6311d4
       DEBUGF(infof(conn->data, "FTP RANGE from %" FORMAT_OFF_T
6311d4
                    " getting %" FORMAT_OFF_T " bytes\n",
230636
@@ -4333,20 +4328,22 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
6311d4
     }
6311d4
     slash_pos=strrchr(cur_pos, '/');
6311d4
     if(slash_pos || !*cur_pos) {
6311d4
+      CURLcode result;
6311d4
       ftpc->dirs = calloc(1, sizeof(ftpc->dirs[0]));
6311d4
       if(!ftpc->dirs)
6311d4
         return CURLE_OUT_OF_MEMORY;
6311d4
 
6311d4
-      ftpc->dirs[0] = curl_easy_unescape(conn->data, slash_pos ? cur_pos : "/",
6311d4
-                                         slash_pos ?
6311d4
-                                         curlx_sztosi(slash_pos-cur_pos) : 1,
6311d4
-                                         NULL);
6311d4
-      if(!ftpc->dirs[0]) {
6311d4
+      result = Curl_urldecode(conn->data, slash_pos ? cur_pos : "/",
6311d4
+                              slash_pos ?
6311d4
+                              curlx_sztosi(slash_pos-cur_pos) : 1,
6311d4
+                              &ftpc->dirs[0], NULL,
6311d4
+                              FALSE);
6311d4
+      if(result) {
6311d4
         freedirs(ftpc);
6311d4
-        return CURLE_OUT_OF_MEMORY;
6311d4
+        return result;
6311d4
       }
6311d4
       ftpc->dirdepth = 1; /* we consider it to be a single dir */
6311d4
-      filename = slash_pos ? slash_pos+1 : cur_pos; /* rest is file name */
6311d4
+      filename = slash_pos ? slash_pos + 1 : cur_pos; /* rest is file name */
6311d4
     }
6311d4
     else
6311d4
       filename = cur_pos;  /* this is a file name only */
230636
@@ -4378,18 +4375,15 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
6311d4
           /* we skip empty path components, like "x//y" since the FTP command
6311d4
              CWD requires a parameter and a non-existent parameter a) doesn't
6311d4
              work on many servers and b) has no effect on the others. */
6311d4
-          int len = curlx_sztosi(slash_pos - cur_pos + absolute_dir);
6311d4
-          ftpc->dirs[ftpc->dirdepth] =
6311d4
-            curl_easy_unescape(conn->data, cur_pos - absolute_dir, len, NULL);
6311d4
-          if(!ftpc->dirs[ftpc->dirdepth]) { /* run out of memory ... */
6311d4
-            failf(data, "no memory");
6311d4
-            freedirs(ftpc);
6311d4
-            return CURLE_OUT_OF_MEMORY;
6311d4
-          }
6311d4
-          if(isBadFtpString(ftpc->dirs[ftpc->dirdepth])) {
6311d4
+          size_t len = slash_pos - cur_pos + absolute_dir;
6311d4
+          CURLcode result =
6311d4
+            Curl_urldecode(conn->data, cur_pos - absolute_dir, len,
6311d4
+                           &ftpc->dirs[ftpc->dirdepth], NULL,
6311d4
+                           TRUE);
6311d4
+          if(result) {
6311d4
             free(ftpc->dirs[ftpc->dirdepth]);
6311d4
             freedirs(ftpc);
6311d4
-            return CURLE_URL_MALFORMAT;
6311d4
+            return result;
6311d4
           }
6311d4
         }
6311d4
         else {
230636
@@ -4416,15 +4410,12 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
6311d4
   } /* switch */
6311d4
 
6311d4
   if(filename && *filename) {
6311d4
-    ftpc->file = curl_easy_unescape(conn->data, filename, 0, NULL);
6311d4
-    if(NULL == ftpc->file) {
6311d4
-      freedirs(ftpc);
6311d4
-      failf(data, "no memory");
6311d4
-      return CURLE_OUT_OF_MEMORY;
6311d4
-    }
6311d4
-    if(isBadFtpString(ftpc->file)) {
6311d4
+    CURLcode result =
6311d4
+      Curl_urldecode(conn->data, filename, 0,  &ftpc->file, NULL, TRUE);
6311d4
+
6311d4
+    if(result) {
6311d4
       freedirs(ftpc);
6311d4
-      return CURLE_URL_MALFORMAT;
6311d4
+      return result;
6311d4
     }
6311d4
   }
6311d4
   else
230636
@@ -4442,15 +4433,17 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
6311d4
   if(ftpc->prevpath) {
6311d4
     /* prevpath is "raw" so we convert the input path before we compare the
6311d4
        strings */
6311d4
-    int dlen;
6311d4
-    char *path = curl_easy_unescape(conn->data, data->state.path, 0, &dlen);
6311d4
-    if(!path) {
6311d4
+    size_t dlen;
6311d4
+    char *path;
6311d4
+    CURLcode result =
6311d4
+      Curl_urldecode(conn->data, data->state.path, 0, &path, &dlen, FALSE);
6311d4
+    if(result) {
6311d4
       freedirs(ftpc);
6311d4
-      return CURLE_OUT_OF_MEMORY;
6311d4
+      return result;
6311d4
     }
6311d4
 
6311d4
-    dlen -= ftpc->file?curlx_uztosi(strlen(ftpc->file)):0;
6311d4
-    if((dlen == curlx_uztosi(strlen(ftpc->prevpath))) &&
6311d4
+    dlen -= ftpc->file?strlen(ftpc->file):0;
6311d4
+    if((dlen == strlen(ftpc->prevpath)) &&
6311d4
        strnequal(path, ftpc->prevpath, dlen)) {
6311d4
       infof(data, "Request has same path as previous transfer\n");
6311d4
       ftpc->cwddone = TRUE;
6311d4
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
6311d4
index 56cb286..e7955ee 100644
6311d4
--- a/tests/data/Makefile.am
6311d4
+++ b/tests/data/Makefile.am
6311d4
@@ -28,7 +28,8 @@ test200 test201 test202 test203 test204 test205 test206 test207 test208	\
6311d4
 test209 test210 test211 test212 test213 test214 test215 test216 test217	\
6311d4
 test218 test220 test221 test222 test223 test224 test225 test226 test227	\
6311d4
 test228 test229 test231 test233 test234 test235 test236 test237 test238	\
6311d4
-test239 test240 test241 test242 test243 test245 test246 test247 test248	\
6311d4
+test239 test240 test241 test242 test243	\
6311d4
+test244 test245 test246 test247 test248	\
6311d4
 test249 test250 test251 test252 test253 test254 test255 test256 test257	\
6311d4
 test258 test259 test260 test261 test262 test263 test264 test265 test266	\
6311d4
 test267 test268 test269 test270 test271 test272 test273 test274 test275	\
6311d4
diff --git a/tests/data/test244 b/tests/data/test244
6311d4
new file mode 100644
6311d4
index 0000000..8ce4b63
6311d4
--- /dev/null
6311d4
+++ b/tests/data/test244
6311d4
@@ -0,0 +1,54 @@
6311d4
+<testcase>
6311d4
+<info>
6311d4
+<keywords>
6311d4
+FTP
6311d4
+PASV
6311d4
+CWD
6311d4
+--ftp-method
6311d4
+nocwd
6311d4
+</keywords>
6311d4
+</info>
6311d4
+#
6311d4
+# Server-side
6311d4
+<reply>
6311d4
+<data mode="text">
6311d4
+total 20
6311d4
+drwxr-xr-x   8 98       98           512 Oct 22 13:06 .
6311d4
+drwxr-xr-x   8 98       98           512 Oct 22 13:06 ..
6311d4
+drwxr-xr-x   2 98       98           512 May  2  1996 .NeXT
6311d4
+-r--r--r--   1 0        1             35 Jul 16  1996 README
6311d4
+lrwxrwxrwx   1 0        1              7 Dec  9  1999 bin -> usr/bin
6311d4
+dr-xr-xr-x   2 0        1            512 Oct  1  1997 dev
6311d4
+drwxrwxrwx   2 98       98           512 May 29 16:04 download.html
6311d4
+dr-xr-xr-x   2 0        1            512 Nov 30  1995 etc
6311d4
+drwxrwxrwx   2 98       1            512 Oct 30 14:33 pub
6311d4
+dr-xr-xr-x   5 0        1            512 Oct  1  1997 usr
6311d4
+</data>
6311d4
+</reply>
6311d4
+
6311d4
+# Client-side
6311d4
+<client>
6311d4
+<server>
6311d4
+ftp
6311d4
+</server>
6311d4
+ <name>
6311d4
+FTP dir listing with nocwd and URL encoded path
6311d4
+ </name>
6311d4
+ <command>
6311d4
+--ftp-method nocwd ftp://%HOSTIP:%FTPPORT/fir%23t/th%69rd/244/
6311d4
+</command>
6311d4
+</client>
6311d4
+
6311d4
+# Verify data after the test has been "shot"
6311d4
+<verify>
6311d4
+<protocol>
6311d4
+USER anonymous
6311d4
+PASS ftp@example.com
6311d4
+PWD
6311d4
+EPSV
6311d4
+TYPE A
6311d4
+LIST fir#t/third/244/
6311d4
+QUIT
6311d4
+</protocol>
6311d4
+</verify>
6311d4
+</testcase>
6311d4
-- 
6311d4
2.14.3
6311d4
6311d4
6311d4
From 295fc8b0dc5c94a1cbf6688bfba768128b13cde6 Mon Sep 17 00:00:00 2001
6311d4
From: Daniel Stenberg <daniel@haxx.se>
6311d4
Date: Wed, 2 Nov 2016 07:22:27 +0100
6311d4
Subject: [PATCH 2/3] ftp_done: don't clobber the passed in error code
6311d4
6311d4
Coverity CID 1374359 pointed out the unused result value.
6311d4
6311d4
Upstream-commit: f81a8364618caf99b4691ffd494a9b2d4c9fb1f6
6311d4
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
6311d4
---
6311d4
 lib/ftp.c | 9 +++++----
6311d4
 1 file changed, 5 insertions(+), 4 deletions(-)
6311d4
6311d4
diff --git a/lib/ftp.c b/lib/ftp.c
6311d4
index 9da5a24..0259a14 100644
6311d4
--- a/lib/ftp.c
6311d4
+++ b/lib/ftp.c
230636
@@ -3324,11 +3324,12 @@ static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
6311d4
     ftpc->known_filesize = -1;
6311d4
   }
6311d4
 
6311d4
-  /* get the "raw" path */
6311d4
-  result = Curl_urldecode(data, path_to_use, 0, &path, NULL, FALSE);
6311d4
+  if(!result)
6311d4
+    /* get the "raw" path */
6311d4
+    result = Curl_urldecode(data, path_to_use, 0, &path, NULL, FALSE);
6311d4
   if(result) {
6311d4
-    /* out of memory, but we can limp along anyway (and should try to
6311d4
-     * since we may already be in the out of memory cleanup path) */
6311d4
+    /* We can limp along anyway (and should try to since we may already be in
6311d4
+     * the error path) */
6311d4
     ftpc->ctl_valid = FALSE; /* mark control connection as bad */
6311d4
     conn->bits.close = TRUE; /* mark for connection closure */
6311d4
     ftpc->prevpath = NULL; /* no path remembering */
6311d4
-- 
6311d4
2.14.4
6311d4
6311d4
6311d4
From 9534442aae1da4e6cf2ce815e47dbcd82695c3d4 Mon Sep 17 00:00:00 2001
6311d4
From: Daniel Stenberg <daniel@haxx.se>
6311d4
Date: Wed, 31 Jan 2018 08:40:11 +0100
6311d4
Subject: [PATCH 3/3] FTP: reject path components with control codes
6311d4
6311d4
Refuse to operate when given path components featuring byte values lower
6311d4
than 32.
6311d4
6311d4
Previously, inserting a %00 sequence early in the directory part when
6311d4
using the 'singlecwd' ftp method could make curl write a zero byte
6311d4
outside of the allocated buffer.
6311d4
6311d4
Test case 340 verifies.
6311d4
6311d4
CVE-2018-1000120
6311d4
Reported-by: Duy Phan Thanh
6311d4
Bug: https://curl.haxx.se/docs/adv_2018-9cd6.html
6311d4
6311d4
Upstream-commit: 535432c0adb62fe167ec09621500470b6fa4eb0f
6311d4
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
6311d4
---
6311d4
 lib/ftp.c              |  8 ++++----
6311d4
 tests/data/Makefile.am |  1 +
6311d4
 tests/data/test340     | 40 ++++++++++++++++++++++++++++++++++++++++
6311d4
 3 files changed, 45 insertions(+), 4 deletions(-)
6311d4
 create mode 100644 tests/data/test340
6311d4
6311d4
diff --git a/lib/ftp.c b/lib/ftp.c
6311d4
index fb3a716..268efdd 100644
6311d4
--- a/lib/ftp.c
6311d4
+++ b/lib/ftp.c
6311d4
@@ -1482,7 +1482,7 @@ static CURLcode ftp_state_post_listtype(struct connectdata *conn)
6311d4
       slashPos = strrchr(inpath, '/');
6311d4
       n = slashPos - inpath;
6311d4
     }
6311d4
-    result = Curl_urldecode(data, inpath, n, &lstArg, NULL, FALSE);
6311d4
+    result = Curl_urldecode(data, inpath, n, &lstArg, NULL, TRUE);
6311d4
     if(result)
6311d4
       return result;
6311d4
   }
230636
@@ -3326,7 +3326,7 @@ static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
6311d4
 
6311d4
   if(!result)
6311d4
     /* get the "raw" path */
6311d4
-    result = Curl_urldecode(data, path_to_use, 0, &path, NULL, FALSE);
6311d4
+    result = Curl_urldecode(data, path_to_use, 0, &path, NULL, TRUE);
6311d4
   if(result) {
6311d4
     /* We can limp along anyway (and should try to since we may already be in
6311d4
      * the error path) */
230636
@@ -4338,7 +4338,7 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
6311d4
                               slash_pos ?
6311d4
                               curlx_sztosi(slash_pos-cur_pos) : 1,
6311d4
                               &ftpc->dirs[0], NULL,
6311d4
-                              FALSE);
6311d4
+                              TRUE);
6311d4
       if(result) {
6311d4
         freedirs(ftpc);
6311d4
         return result;
230636
@@ -4437,7 +4437,7 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
6311d4
     size_t dlen;
6311d4
     char *path;
6311d4
     CURLcode result =
6311d4
-      Curl_urldecode(conn->data, data->state.path, 0, &path, &dlen, FALSE);
6311d4
+      Curl_urldecode(conn->data, data->state.path, 0, &path, &dlen, TRUE);
6311d4
     if(result) {
6311d4
       freedirs(ftpc);
6311d4
       return result;
6311d4
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
6311d4
index e7955ee..910db5b 100644
6311d4
--- a/tests/data/Makefile.am
6311d4
+++ b/tests/data/Makefile.am
6311d4
@@ -39,6 +39,7 @@ test294 test295 test296 test297 test298 test299 test300 test301 test302	\
6311d4
 test303 test304 test305 test306 test307 test308 test309 test310 test311	\
6311d4
 test312 test313 test317 test318 \
6311d4
 test320 test321 test322 test323 test324 test350 test351	\
6311d4
+test340	\
6311d4
 test352 test353 test354 test400 test401 test402 test403 test404 test405	\
6311d4
 test406 test407 test408 test409 test500 test501 test502 test503 test504	\
6311d4
 test505 test506 test507 test508 test510 test511 test512 test513 test514	\
6311d4
diff --git a/tests/data/test340 b/tests/data/test340
6311d4
new file mode 100644
6311d4
index 0000000..d834d76
6311d4
--- /dev/null
6311d4
+++ b/tests/data/test340
6311d4
@@ -0,0 +1,40 @@
6311d4
+<testcase>
6311d4
+<info>
6311d4
+<keywords>
6311d4
+FTP
6311d4
+PASV
6311d4
+CWD
6311d4
+--ftp-method
6311d4
+singlecwd
6311d4
+</keywords>
6311d4
+</info>
6311d4
+#
6311d4
+# Server-side
6311d4
+<reply>
6311d4
+</reply>
6311d4
+
6311d4
+# Client-side
6311d4
+<client>
6311d4
+<server>
6311d4
+ftp
6311d4
+</server>
6311d4
+ <name>
6311d4
+FTP using %00 in path with singlecwd
6311d4
+ </name>
6311d4
+ <command>
6311d4
+--ftp-method singlecwd ftp://%HOSTIP:%FTPPORT/%00first/second/third/340
6311d4
+</command>
6311d4
+</client>
6311d4
+
6311d4
+# Verify data after the test has been "shot"
6311d4
+<verify>
6311d4
+<protocol>
6311d4
+USER anonymous
6311d4
+PASS ftp@example.com
6311d4
+PWD
6311d4
+</protocol>
6311d4
+<errorcode>
6311d4
+3
6311d4
+</errorcode>
6311d4
+</verify>
6311d4
+</testcase>
6311d4
-- 
6311d4
2.14.3
6311d4