[CVE-2017-9502] url: fix buffer overwrite with file protocol 79/134479/2 accepted/tizen/unified/20170706.193711 submit/tizen/20170620.102303 submit/tizen/20170630.012307 submit/tizen/20170703.231407 submit/tizen/20170705.224745
authorDaniel Stenberg <daniel@haxx.se>
Tue, 6 Jun 2017 22:21:04 +0000 (00:21 +0200)
committerseonah moon <seonah1.moon@samsung.com>
Mon, 19 Jun 2017 01:40:57 +0000 (01:40 +0000)
https://github.com/curl/curl/issues/1540

Change-Id: Ic0c511886a16d0655e416882ee9719d1ac120be6

lib/url.c

index bc5b841..3282566 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -4436,6 +4436,7 @@ static CURLcode parseurlandfillconn(struct Curl_easy *data,
 #endif
 
     protop = "file"; /* protocol string */
+    *prot_missing = !url_has_scheme;
   }
   else {
     /* clear path */
@@ -4599,14 +4600,30 @@ static CURLcode parseurlandfillconn(struct Curl_easy *data,
 
     size_t plen = strlen(path); /* new path, should be 1 byte longer than
                                    the original */
-    size_t urllen = strlen(data->change.url); /* original URL length */
-
     size_t prefixlen = strlen(conn->host.name);
 
-    if(!*prot_missing)
-      prefixlen += strlen(protop) + strlen("://");
+    if(!*prot_missing) {
+      size_t protolen = strlen(protop);
+
+      if(curl_strnequal(protop, data->change.url, protolen))
+        prefixlen += protolen;
+      else {
+        failf(data, "<url> malformed");
+        return CURLE_URL_MALFORMAT;
+      }
+
+      if(curl_strnequal("://", &data->change.url[protolen], 3))
+        prefixlen += 3;
+      /* only file: is allowed to omit one or both slashes */
+      else if(curl_strnequal("file:", data->change.url, 5))
+        prefixlen += 1 + (data->change.url[5] == '/');
+      else {
+        failf(data, "<url> malformed");
+        return CURLE_URL_MALFORMAT;
+      }
+    }
 
-    reurl = malloc(urllen + 2); /* 2 for zerobyte + slash */
+    reurl = malloc(prefixlen + plen + 1);
     if(!reurl)
       return CURLE_OUT_OF_MEMORY;