Race condition with CURL_LOCK_DATA_CONNECT can cause connections to be used at the same time
A race condition in libcurl's connection reuse logic allows two threads with separate easy handles to share the same connection pointer simultaneously, leading to use-after-free and double-free errors. The reporter is unsure of the practical security impact because the issue is inconsistent to trigger.
- Disclosed after 14 months
Summary:
We've seen race conditions when using CURL_LOCK_DATA_CONNECT in libcurl where sometimes two different threads using two different easy handles ends up sharing the same connection pointer at the same time. This causes UAFs and double frees when both threads are freeing items on the same connection pointer.
Steps To Reproduce:
I added curl.cpp which stresses CURL_LOCK_DATA_CONNECT and should eventually trigger an ASAN error with curl compiled using clang's address sanitizers. It's not consistent how it fails since it's a threading issue. I've found that it's more consistent after adding a random sleep after the unlock here https://github.com/curl/curl/blob/master/lib/url.c#L1372.
A colleague suggested that a potential fix could be to remove the CONN_INUSE check from this condition because the connection isn't actually marked as inuse until a different set of lock and unlocks. It does appear to stop the crashes but we're unsure on how ideal that fix is.
Supporting Material/References:
curl.cpp - Repro code asan-output.txt - Asan results with some added logging Notably three threads with different easy handles decide to reuse the 0x61b000fbd688 connection at the same time.
[thread: 7bdde700] multi_done(data = 0x623000206108, conn = 0x61b000fbd688)
[thread: 825eb700] url.c:1370 chosen connection=0x61b000fbd688
[thread: 825eb700] reuse_conn(old_conn=0x61b000979188, conn=0x61b000fbd688)
[thread: 7bdde700] url.c:1370 chosen connection=0x61b000fbd688
[thread: 7bdde700][thread: 8a7ff700] Cur reuse_conn(old_conn=0x61b0012e1888, conn=0x61b000fbl.c:1370 chosen connecd688)
[thread: 877f9700] url_attach_connnection(data = 0x6230002c4d08, conn = 0x61b000f1a388)
tion=0x61b000fbd688
[thread: 877f9700] reuse_conn(old_conn=0x61b000a94988, conn=0x61b000fbd688)
[thread: 825eb700] Curl_attach_connnection(data = 0x62300030e508, conn = 0x61b000fbd688)
[thread: 7bdde700] Curl_attach_connnection(data = 0x6230000e3908, conn = 0x61b000fbd688)
[thread: 877f9700] Curl_attach_connnection(data = 0x62300021cd08, conn = 0x61b000fbd688)
Impact
Not sure how much of a security impact or exploitable this is in practice since it's pretty inconsistent on when it's hit.
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
Likely a missing or incorrectly ordered lock around the connection-in-use check, so a connection is selected for reuse before it is marked as in use.
Impact
The race could cause use-after-free and double-free memory corruption, but the reporter is unsure how exploitable this is in practice because it is inconsistent to trigger.
What the attacker needed
Multi-threaded libcurl use; CURL_LOCK_DATA_CONNECT sharing; timing-dependent execution
The fix
A suggested fix is to remove the CONN_INUSE check from the condition, though the reporter is unsure how ideal that is.
How it was found
- Compile curl with clang's address sanitiser.
- Run the provided stress test that exercises CURL_LOCK_DATA_CONNECT with multiple threads and easy handles.
- Observe that the crash is inconsistent but becomes more reliable after adding a random sleep after the unlock in url.c.
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 a program uses multiple threads to make network requests, it must carefully coordinate access to shared resources like open connections. Here, a timing flaw lets two threads believe they own the same connection at once, so one thread can free it while the other is still using it, causing memory corruption. The reporter is not sure how reliably an attacker could exploit this.
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.