Potential invocation of qsort on uninitialized memory during cookie save
In curl's cookie-saving code, a temporary array of cookie pointers was sorted using the total cookie count rather than the number of entries actually filled, so qsort compared uninitialised memory. An attacker who could get a crafted cookie jar loaded could trigger out-of-bounds reads and possibly a crash, though the reporter describes the impact as fairly benign.
- Disclosed after 17 months
Summary:
If cookiejar is set, cookies are written to file at exit. That is done by the function cookie_output() in cookie.c. The cookies are sorted before being stored, using qsort on a temporary array. That temporary array is uninitialized (gotten from malloc at https://github.com/curl/curl/blob/7c596f5dea586c1ba99dfbe7f3ce1996d82f7de0/lib/cookie.c#L1534 ). This would not be a problem unless there also is a bug in the range given to qsort https://github.com/curl/curl/blob/7c596f5dea586c1ba99dfbe7f3ce1996d82f7de0/lib/cookie.c#L1550 which is numcookies. However, it should be j which is used for counting at https://github.com/curl/curl/blob/7c596f5dea586c1ba99dfbe7f3ce1996d82f7de0/lib/cookie.c#L1546.
The buffer passed to qsort is partially filled with cookie data, and the rest is uninitialized. When qsort sorts, it will dereference the supposed to be pointers to compare the elements and depending on the results jump around reading in memory.
Steps To Reproduce:
I found this through fuzzing and I do not want to make that public until the problems I find are fixed - in case you want it now already, just hit me up. I attached the most important part of the fuzzer.
It is not obvious how to reproduce without the fuzzer: (c->numcookies must be nonzero and co->domain must not be set on at least one of them for this bug to be triggered. Perhaps by loading an evil cookie file from disk.
To detect it, address and undefined sanitizers are not sufficient. That is likely because qsort is a library function, so it's not instrumented. Valgrind does not always catch it either. I found it by adding an assert on pointer alignment inside the cookie_sort_ct(), and eventually found which of the 60000 test cases I had caused it.
Suggested fix
diff --git a/lib/cookie.c b/lib/cookie.c
index 53ca40237..875569bb0 100644
--- a/lib/cookie.c
+++ b/lib/cookie.c
@@ -1547,7 +1547,7 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere)
}
}
- qsort(array, c->numcookies, sizeof(struct Cookie *), cookie_sort_ct);
+ qsort(array, j, sizeof(struct Cookie *), cookie_sort_ct);
for(i = 0; i < j; i++) {
char *format_ptr = get_netscape_format(array[i]);
Even better (defence in depth) would be to allocate array with calloc instead of malloc which would cause (near null) pointer dereference instead of "random" values.
Supporting Material/References:
Attached is
- the test case to feed as input to the fuzzer above.
- crash report from valgrind and assert()
Impact
This is read access, and if triggered it will perhaps cause a crash (segmentation fault), and the cookie jar is not written. So a fairly benign bug.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The qsort call used the total cookie count (numcookies) instead of the number of entries actually written into the temporary array (j), so uninitialised slots were sorted.
Impact
This is a read-only issue that may cause a crash (segmentation fault) and prevent the cookie jar from being written, which the reporter calls fairly benign.
What the attacker needed
Cookiejar option set; crafted cookie jar with a domainless cookie
The fix
The qsort call should use the actual filled count j instead of numcookies, and allocating the array with calloc would add defence in depth.
How it was found
- Load a cookie jar where the cookie count is nonzero and at least one cookie has no domain set.
- Let curl exit with cookiejar configured so cookie_output() runs.
- Observe qsort reading uninitialised array slots, detected via an alignment assert or valgrind.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When curl saves cookies to a file it first sorts them. The sorting routine was told there were more cookies than had actually been copied into the temporary list, so it compared leftover, uninitialised memory as if it were real cookie pointers. That can make the program read memory it should not and crash.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.