diff options
author | Daniel Stenberg <daniel@haxx.se> | 2013-06-24 22:24:35 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2013-06-25 09:55:49 +0200 |
commit | 365c5ba39591fab2e60bf4f0e67d9dcf79ecc506 (patch) | |
tree | 6e02f6904f12fcd94b1036643bc5064c809e807b /lib/formdata.c | |
parent | cb1aa8b0e32068ec4bbbc42d41267420b46a36e7 (diff) | |
download | curl-365c5ba39591fab2e60bf4f0e67d9dcf79ecc506.tar.gz |
formpost: better random boundaries
When doing multi-part formposts, libcurl used a pseudo-random value that
was seeded with time(). This turns out to be bad for users who formpost
data that is provided with users who then can guess how the boundary
string will look like and then they can forge a different formpost part
and trick the receiver.
My advice to such implementors is (still even after this change) to not
rely on the boundary strings being cryptographically strong. Fix your
code and logic to not depend on them that much!
I moved the Curl_rand() function into the sslgen.c source file now to be
able to take advantage of the SSL library's random function if it
provides one. If not, try to use the RANDOM_FILE for seeding and as a
last resort keep the old logic, just modified to also add microseconds
which makes it harder to properly guess the exact seed.
The formboundary() function in formdata.c is now using 64 bit entropy
for the boundary and therefore the string of dashes was reduced by 4
letters and there are 16 hex digits following it. The total length is
thus still the same.
Bug: http://curl.haxx.se/bug/view.cgi?id=1251
Reported-by: "Floris"
Diffstat (limited to 'lib/formdata.c')
-rw-r--r-- | lib/formdata.c | 35 |
1 files changed, 8 insertions, 27 deletions
diff --git a/lib/formdata.c b/lib/formdata.c index 49c5d2943..decb84d9f 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -24,9 +24,6 @@ #include <curl/curl.h> -/* Length of the random boundary string. */ -#define BOUNDARY_LENGTH 40 - #if !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY) #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME) @@ -35,7 +32,7 @@ #include "urldata.h" /* for struct SessionHandle */ #include "formdata.h" -#include "curl_rand.h" +#include "sslgen.h" #include "strequal.h" #include "curl_memory.h" #include "sendf.h" @@ -56,7 +53,7 @@ static char *Curl_basename(char *path); #endif static size_t readfromfile(struct Form *form, char *buffer, size_t size); -static char *formboundary(void); +static char *formboundary(struct SessionHandle *data); /* What kind of Content-Type to use on un-specified files with unrecognized extensions. */ @@ -1101,7 +1098,7 @@ CURLcode Curl_getformdata(struct SessionHandle *data, if(!post) return result; /* no input => no output! */ - boundary = formboundary(); + boundary = formboundary(data); if(!boundary) return CURLE_OUT_OF_MEMORY; @@ -1157,7 +1154,7 @@ CURLcode Curl_getformdata(struct SessionHandle *data, the magic to include several files with the same field name */ Curl_safefree(fileboundary); - fileboundary = formboundary(); + fileboundary = formboundary(data); if(!fileboundary) { result = CURLE_OUT_OF_MEMORY; break; @@ -1464,28 +1461,12 @@ char *Curl_formpostheader(void *formp, size_t *len) * formboundary() creates a suitable boundary string and returns an allocated * one. */ -static char *formboundary(void) +static char *formboundary(struct SessionHandle *data) { - char *retstring; - size_t i; - - static const char table16[]="0123456789abcdef"; - - retstring = malloc(BOUNDARY_LENGTH+1); - - if(!retstring) - return NULL; /* failed */ - - strcpy(retstring, "----------------------------"); - - for(i=strlen(retstring); i<BOUNDARY_LENGTH; i++) - retstring[i] = table16[Curl_rand()%16]; - - /* 28 dashes and 12 hexadecimal digits makes 12^16 (184884258895036416) + /* 24 dashes and 16 hexadecimal digits makes 64 bit (18446744073709551615) combinations */ - retstring[BOUNDARY_LENGTH]=0; /* zero terminate */ - - return retstring; + return aprintf("------------------------%08x%08x", + Curl_rand(data), Curl_rand(data)); } #else /* CURL_DISABLE_HTTP */ |