curl/curl-7.21.7-CVE-2012-0036.diff
2012-08-07 09:54:37 +00:00

223 lines
7.5 KiB
Diff

From 4fd13aca70443b2a206e2a32e876be45c2f9a918 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Fri, 23 Dec 2011 13:24:16 +0100
Subject: [PATCH] URL sanitize: reject URLs containing bad data
Protocols (IMAP, POP3 and SMTP) that use the path part of a URL in a
decoded manner now use the new Curl_urldecode() function to reject URLs
with embedded control codes (anything that is or decodes to a byte value
less than 32).
URLs containing such codes could easily otherwise be used to do harm and
allow users to do unintended actions with otherwise innocent tools and
applications. Like for example using a URL like
pop3://pop3.example.com/1%0d%0aDELE%201 when the app wants a URL to get
a mail and instead this would delete one.
This flaw is considered a security vulnerability: CVE-2012-0036
Security advisory at: http://curl.haxx.se/docs/adv_20120124.html
Reported by: Dan Fandrich
diff -Naurp curl-7.21.7/lib/escape.c curl-7.21.7.oden/lib/escape.c
--- curl-7.21.7/lib/escape.c 2011-05-23 17:08:12.000000000 +0000
+++ curl-7.21.7.oden/lib/escape.c 2012-04-13 08:07:48.000000000 +0000
@@ -34,6 +34,7 @@
#include "urldata.h"
#include "warnless.h"
#include "non-ascii.h"
+#include "escape.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
@@ -87,7 +88,7 @@ char *curl_easy_escape(CURL *handle, con
char *testing_ptr = NULL;
unsigned char in; /* we need to treat the characters unsigned */
size_t newlen = alloc;
- int strindex=0;
+ size_t strindex=0;
size_t length;
CURLcode res;
@@ -135,23 +136,29 @@ char *curl_easy_escape(CURL *handle, con
}
/*
- * Unescapes the given URL escaped string of given length. Returns a
- * pointer to a malloced string with length given in *olen.
- * If length == 0, the length is assumed to be strlen(string).
- * If olen == NULL, no output length is stored.
+ * Curl_urldecode() URL decodes the given string.
+ *
+ * Optionally detects control characters (byte codes lower than 32) in the
+ * data and rejects such data.
+ *
+ * Returns a pointer to a malloced string in *ostring with length given in
+ * *olen. If length == 0, the length is assumed to be strlen(string).
+ *
*/
-char *curl_easy_unescape(CURL *handle, const char *string, int length,
- int *olen)
+CURLcode Curl_urldecode(struct SessionHandle *data,
+ const char *string, size_t length,
+ char **ostring, size_t *olen,
+ bool reject_ctrl)
{
- int alloc = (length?length:(int)strlen(string))+1;
+ size_t alloc = (length?length:strlen(string))+1;
char *ns = malloc(alloc);
unsigned char in;
- int strindex=0;
+ size_t strindex=0;
unsigned long hex;
CURLcode res;
if(!ns)
- return NULL;
+ return CURLE_OUT_OF_MEMORY;
while(--alloc > 0) {
in = *string;
@@ -167,16 +174,20 @@ char *curl_easy_unescape(CURL *handle, c
in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
- res = Curl_convert_from_network(handle, &in, 1);
+ res = Curl_convert_from_network(data, &in, 1);
if(res) {
/* Curl_convert_from_network calls failf if unsuccessful */
free(ns);
- return NULL;
+ return res;
}
string+=2;
alloc-=2;
}
+ if(reject_ctrl && (in < 0x20)) {
+ free(ns);
+ return CURLE_URL_MALFORMAT;
+ }
ns[strindex++] = in;
string++;
@@ -186,7 +197,33 @@ char *curl_easy_unescape(CURL *handle, c
if(olen)
/* store output size */
*olen = strindex;
- return ns;
+
+ if(ostring)
+ /* store output string */
+ *ostring = ns;
+
+ return CURLE_OK;
+}
+
+/*
+ * Unescapes the given URL escaped string of given length. Returns a
+ * pointer to a malloced string with length given in *olen.
+ * If length == 0, the length is assumed to be strlen(string).
+ * If olen == NULL, no output length is stored.
+ */
+char *curl_easy_unescape(CURL *handle, const char *string, int length,
+ int *olen)
+{
+ char *str = NULL;
+ size_t inputlen = length;
+ size_t outputlen;
+ CURLcode res = Curl_urldecode(handle, string, inputlen, &str, &outputlen,
+ FALSE);
+ if(res)
+ return NULL;
+ if(olen)
+ *olen = curlx_uztosi(outputlen);
+ return str;
}
/* For operating systems/environments that use different malloc/free
diff -Naurp curl-7.21.7/lib/escape.h curl-7.21.7.oden/lib/escape.h
--- curl-7.21.7/lib/escape.h 2011-03-19 15:16:07.000000000 +0000
+++ curl-7.21.7.oden/lib/escape.h 2012-04-13 08:07:48.000000000 +0000
@@ -1,5 +1,5 @@
-#ifndef __ESCAPE_H
-#define __ESCAPE_H
+#ifndef HEADER_CURL_ESCAPE_H
+#define HEADER_CURL_ESCAPE_H
/***************************************************************************
* _ _ ____ _
@@ -8,7 +8,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -25,5 +25,9 @@
/* Escape and unescape URL encoding in strings. The functions return a new
* allocated string or NULL if an error occurred. */
+CURLcode Curl_urldecode(struct SessionHandle *data,
+ const char *string, size_t length,
+ char **ostring, size_t *olen,
+ bool reject_crlf);
#endif
diff -Naurp curl-7.21.7/lib/imap.c curl-7.21.7.oden/lib/imap.c
--- curl-7.21.7/lib/imap.c 2011-05-09 08:20:30.000000000 +0000
+++ curl-7.21.7.oden/lib/imap.c 2012-04-13 08:07:48.000000000 +0000
@@ -951,17 +951,12 @@ static CURLcode imap_parse_url_path(stru
struct imap_conn *imapc = &conn->proto.imapc;
struct SessionHandle *data = conn->data;
const char *path = data->state.path;
- int len;
if(!*path)
path = "INBOX";
/* url decode the path and use this mailbox */
- imapc->mailbox = curl_easy_unescape(data, path, 0, &len);
- if(!imapc->mailbox)
- return CURLE_OUT_OF_MEMORY;
-
- return CURLE_OK;
+ return Curl_urldecode(data, path, 0, &imapc->mailbox, NULL, TRUE);
}
/* call this when the DO phase has completed */
diff -Naurp curl-7.21.7/lib/pop3.c curl-7.21.7.oden/lib/pop3.c
--- curl-7.21.7/lib/pop3.c 2011-06-21 15:55:39.000000000 +0000
+++ curl-7.21.7.oden/lib/pop3.c 2012-04-13 08:07:48.000000000 +0000
@@ -900,11 +900,7 @@ static CURLcode pop3_parse_url_path(stru
const char *path = data->state.path;
/* url decode the path and use this mailbox */
- pop3c->mailbox = curl_easy_unescape(data, path, 0, NULL);
- if(!pop3c->mailbox)
- return CURLE_OUT_OF_MEMORY;
-
- return CURLE_OK;
+ return Curl_urldecode(data, path, 0, &pop3c->mailbox, NULL, TRUE);
}
/* call this when the DO phase has completed */
diff -Naurp curl-7.21.7/lib/smtp.c curl-7.21.7.oden/lib/smtp.c
--- curl-7.21.7/lib/smtp.c 2011-05-09 08:20:30.000000000 +0000
+++ curl-7.21.7.oden/lib/smtp.c 2012-04-13 08:07:48.000000000 +0000
@@ -1171,9 +1171,9 @@ static CURLcode smtp_connect(struct conn
}
/* url decode the path and use it as domain with EHLO */
- smtpc->domain = curl_easy_unescape(conn->data, path, 0, &len);
- if(!smtpc->domain)
- return CURLE_OUT_OF_MEMORY;
+ result = Curl_urldecode(conn->data, path, 0, &smtpc->domain, NULL, TRUE);
+ if(result)
+ return result;
/* When we connect, we start in the state where we await the server greeting
*/