[curl/f15] reject URLs containing bad data (CVE-2012-0036)
Kamil Dudka
kdudka at fedoraproject.org
Tue Jan 24 12:30:59 UTC 2012
commit 5fbe32796e265e70aa359d8d540c32e18e59eb9a
Author: Kamil Dudka <kdudka at redhat.com>
Date: Tue Jan 24 13:25:46 2012 +0100
reject URLs containing bad data (CVE-2012-0036)
0012-curl-7.21.3-75ca568.patch | 316 ++++++++++++++++++++++++++++++++++++++++
curl.spec | 8 +-
2 files changed, 323 insertions(+), 1 deletions(-)
---
diff --git a/0012-curl-7.21.3-75ca568.patch b/0012-curl-7.21.3-75ca568.patch
new file mode 100644
index 0000000..d928513
--- /dev/null
+++ b/0012-curl-7.21.3-75ca568.patch
@@ -0,0 +1,316 @@
+From d86e698610eceb600e83b164cfe94a1498570445 Mon Sep 17 00:00:00 2001
+From: Daniel Stenberg <daniel at 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
+
+Signed-off-by: Kamil Dudka <kdudka at redhat.com>
+---
+ lib/escape.c | 100 ++++++++++++++++++++++++++++++++++-----------------------
+ lib/escape.h | 10 ++++--
+ lib/imap.c | 7 +---
+ lib/pop3.c | 6 +---
+ lib/smtp.c | 7 ++--
+ 5 files changed, 72 insertions(+), 58 deletions(-)
+
+diff --git a/lib/escape.c b/lib/escape.c
+index 735e1d8..0dd5a1d 100644
+--- a/lib/escape.c
++++ b/lib/escape.c
+@@ -5,7 +5,7 @@
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+- * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel at haxx.se>, et al.
++ * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel at 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
+@@ -24,17 +24,14 @@
+ * allocated string or NULL if an error occurred. */
+
+ #include "setup.h"
+-#include <ctype.h>
++
+ #include <curl/curl.h>
+
+-#include <stdio.h>
+-#include <stdlib.h>
+-#include <string.h>
+ #include "curl_memory.h"
+-/* urldata.h and easyif.h are included for Curl_convert_... prototypes */
+ #include "urldata.h"
+-#include "easyif.h"
+ #include "warnless.h"
++#include "non-ascii.h"
++#include "escape.h"
+
+ #define _MPRINTF_REPLACE /* use our functions only */
+ #include <curl/mprintf.h>
+@@ -88,13 +85,10 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
+ 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;
+
+-#ifndef CURL_DOES_CONVERSIONS
+- /* avoid compiler warnings */
+- (void)handle;
+-#endif
+ ns = malloc(alloc);
+ if(!ns)
+ return NULL;
+@@ -103,10 +97,9 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
+ while(length--) {
+ in = *string;
+
+- if (Curl_isunreserved(in)) {
++ if(Curl_isunreserved(in))
+ /* just copy this */
+ ns[strindex++]=in;
+- }
+ else {
+ /* encode it */
+ newlen += 2; /* the size grows with two, since this'll become a %XX */
+@@ -122,15 +115,12 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
+ }
+ }
+
+-#ifdef CURL_DOES_CONVERSIONS
+-/* escape sequences are always in ASCII so convert them on non-ASCII hosts */
+- if(!handle ||
+- (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
++ res = Curl_convert_to_network(handle, &in, 1);
++ if(res) {
+ /* Curl_convert_to_network calls failf if unsuccessful */
+ free(ns);
+ return NULL;
+ }
+-#endif /* CURL_DOES_CONVERSIONS */
+
+ snprintf(&ns[strindex], 4, "%%%02X", in);
+
+@@ -143,26 +133,29 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
+ }
+
+ /*
+- * 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;
+
+-#ifndef CURL_DOES_CONVERSIONS
+- /* avoid compiler warnings */
+- (void)handle;
+-#endif
+- if( !ns )
+- return NULL;
++ if(!ns)
++ return CURLE_OUT_OF_MEMORY;
+
+ while(--alloc > 0) {
+ in = *string;
+@@ -178,19 +171,20 @@ char *curl_easy_unescape(CURL *handle, const char *string, int length,
+
+ in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
+
+-#ifdef CURL_DOES_CONVERSIONS
+-/* escape sequences are always in ASCII so convert them on non-ASCII hosts */
+- if(!handle ||
+- (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
++ res = Curl_convert_from_network(data, &in, 1);
++ if(res) {
+ /* Curl_convert_from_network calls failf if unsuccessful */
+ free(ns);
+- return NULL;
++ return res;
+ }
+-#endif /* CURL_DOES_CONVERSIONS */
+
+ string+=2;
+ alloc-=2;
+ }
++ if(reject_ctrl && (in < 0x20)) {
++ free(ns);
++ return CURLE_URL_MALFORMAT;
++ }
+
+ ns[strindex++] = in;
+ string++;
+@@ -200,7 +194,33 @@ char *curl_easy_unescape(CURL *handle, const char *string, int length,
+ 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 --git a/lib/escape.h b/lib/escape.h
+index 04b06a9..4c7f841 100644
+--- a/lib/escape.h
++++ b/lib/escape.h
+@@ -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 at haxx.se>, et al.
++ * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel at 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 --git a/lib/imap.c b/lib/imap.c
+index 9c39625..c04ad11 100644
+--- a/lib/imap.c
++++ b/lib/imap.c
+@@ -906,17 +906,12 @@ static CURLcode imap_parse_url_path(struct connectdata *conn)
+ 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 --git a/lib/pop3.c b/lib/pop3.c
+index 9f67443..c4699e7 100644
+--- a/lib/pop3.c
++++ b/lib/pop3.c
+@@ -852,11 +852,7 @@ static CURLcode pop3_parse_url_path(struct connectdata *conn)
+ 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 --git a/lib/smtp.c b/lib/smtp.c
+index 82621a4..c1ed098 100644
+--- a/lib/smtp.c
++++ b/lib/smtp.c
+@@ -1047,7 +1047,6 @@ static CURLcode smtp_connect(struct connectdata *conn,
+ struct SessionHandle *data=conn->data;
+ struct pingpong *pp=&smtpc->pp;
+ const char *path = conn->data->state.path;
+- int len;
+ char localhost[1024 + 1];
+
+ *done = FALSE; /* default to not done yet */
+@@ -1121,9 +1120,9 @@ static CURLcode smtp_connect(struct connectdata *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
+ */
+--
+1.7.1
+
diff --git a/curl.spec b/curl.spec
index 1f7add6..bfc360a 100644
--- a/curl.spec
+++ b/curl.spec
@@ -1,7 +1,7 @@
Summary: A utility for getting files from remote servers (FTP, HTTP, and others)
Name: curl
Version: 7.21.3
-Release: 12%{?dist}
+Release: 13%{?dist}
License: MIT
Group: Applications/Internet
Source: http://curl.haxx.se/download/%{name}-%{version}.tar.lzma
@@ -39,6 +39,9 @@ Patch9: 0009-curl-7.21.3-d6f319f.patch
# break busy loops in tests 502, 555, and 573
Patch11: 0011-curl-7.21.1-3445fa2.patch
+# reject URLs containing bad data (CVE-2012-0036)
+Patch12: 0012-curl-7.21.7-75ca568.patch
+
# patch making libcurl multilib ready
Patch101: 0101-curl-7.21.1-multilib.patch
@@ -262,6 +265,9 @@ rm -rf $RPM_BUILD_ROOT
%{_datadir}/aclocal/libcurl.m4
%changelog
+* Tue Jan 24 2012 Kamil Dudka <kdudka at redhat.com> 7.21.3-13
+- reject URLs containing bad data (CVE-2012-0036)
+
* Mon Sep 19 2011 Kamil Dudka <kdudka at redhat.com> 7.21.3-12
- curl-config now provides dummy --static-libs option (#733956)
- break busy loops in tests 502, 555, and 573
More information about the scm-commits
mailing list