Free of uninitialized pointer in doh_decode_rdata_name()
In curl's DNS-over-HTTPS name decoding, a pointer to a dynamic buffer is freed before that buffer is initialised when the remaining buffer length is zero or negative. A malicious or unusual DoH server response with empty resource data could trigger the free of an uninitialised pointer, potentially causing a crash or memory corruption.
- Disclosed after 4 months
doh_decode_rdata_name() (lib/doh.c) frees an uninitialized pointer under certain conditions.
If the remaining buffer length *remaining is <= 0, line 1033 is executed, free()-ing the uninitialized pointer thename.bufr (source below, from v.8.12.1; the bug is still present in master branch as of 3/11/2025):
1020:static CURLcode doh_decode_rdata_name(unsigned char **buf, size_t *remaining,
1021: char **dnsname)
1022:{
...
1026: struct dynbuf thename;
1027:
1028: DEBUGASSERT(buf && remaining && dnsname);
1029: if(!buf || !remaining || !dnsname)
1030: return CURLE_OUT_OF_MEMORY;
1031: rem = (int)*remaining;
1032: if(rem <= 0) {
1033: Curl_dyn_free(&thename);
1034: return CURLE_OUT_OF_MEMORY;
1035: }
1036: Curl_dyn_init(&thename, CURL_MAXLEN_host_name);
Curl_dyn_free() does
Curl_safefree(s->bufr);
but s->bufr isn't initialized when line 1033 calls it. The bug is that line 1036 should be at the beginning of the function.
To illustrate the issue using Visual Studio 2022:
-
Build cURL with debugging information;
-
Set the command arguments for the
curlproject to-v --ssl-no-revoke --doh-url https://cloudflare-dns.com/dns-query https://www.google.com
and set that project as the startup project.
- Set a BP on line 1032.
- Run curl.
- When the BP fires, use the debugger to doctor
remto0. - Step into line 1033 and examine
s-bufr. Notice that it's uninitialized (it probably contains0xcccccccccccccccc, which Visual Studio uses in debug builds to poison uninitialized memory, so that using it is likely to produce an exception). - Step the call to
Curl_safefree()and watch it throw an access violation.
It appears that rem can be == 0 if the DOH server returns no RRDATA . This seems legal under https://www.rfc-editor.org/rfc/rfc1034 s.3.6 (" A domain name identifies a node. Each node has a set of resource information, which may be empty").
I do not have a test brace to verify this hypothesis.
Impact
Possibly any impact that is rooted in a use-after-free 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 dynamic buffer is initialised after the early-return check, so the error path frees an uninitialised buffer pointer.
Impact
Possibly any impact that is rooted in a use-after-free bug; the author notes the trigger is a hypothesis and was not verified with a test case.
What the attacker needed
A DoH server returning empty resource data; curl built with DoH support
How it was found
- Build curl with debugging information and set the DoH URL and target URL as command arguments.
- Set a breakpoint on the line that reads the remaining length.
- Run curl and, when the breakpoint fires, change the remaining length to zero in the debugger.
- Step into the free call and observe that the buffer pointer is uninitialised.
- Step through the safe-free call and watch it raise an access violation.
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.
A program keeps a small helper object that manages a block of memory. Before using it, the program is supposed to set it up; here, in one early-exit path, it skips that setup and immediately asks the helper to release its memory. Releasing memory that was never set up is undefined behaviour and can crash the program or corrupt memory.
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.