Buffer write overflow when forming dns over http request
A buffer overflow occurs in curl's DNS-over-HTTPS encoding function when a long hostname is packed into a fixed-size buffer, allowing a one-byte overwrite. An attacker who controls the hostname could trigger this overflow, potentially leading to memory corruption.
- Disclosed after 17 months
Summary:
If dns over http is used, the hostname to look up is packed into a buffer to send to the dns server using the doh_encode function from the doh.c source file. By default, curl uses a 512 byte buffer. For that length, the buffer may be overflowed with one byte, which is set to 1.
Note that this happens even with the fix in https://github.com/curl/curl/pull/4345 which Daniel made after I emailed about a similar bug in the curl/doh repository.
Steps To Reproduce:
Build curl with address sanitizer, and/or add an assert assert(*olen <=len) ; right before returning from doh_encode() in doh.c https://github.com/curl/curl/blob/65f5b958c95d538a9b205e2753a476d1a7c89179/lib/doh.c#L135
Then issue a curl request:
src/curl --doh-url https://irrelevant/ x....xxxxxxxxxxxxxxxxxxxxx.x....x.xxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx...xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.x.x.......xxxxxxxxxxxxxxxxxxxxxx...xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx......xxxxxx.....xx..........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxx..x......xxxxxxxx..xxxxxxxxxxxxxxxxxxx.x...xxxx.x.x.x...xxxxx
Supporting Material/References:
When adding the assert above, this is the output of the above command:
curl: ../../../lib/doh.c:144: DOHcode doh_encode(const char *, DNStype, unsigned char *, size_t, size_t *): Assertion `*olen <=len' failed.
Impact
If the attacker somehow can control the hostname eventually used by curl, and DOH is in use, the buffer overflow can happen.
For the common case where dnsprobe.dohbuffer is used, the overwrite may be immediately remedied by assignment to the length (see https://github.com/curl/curl/blob/65f5b958c95d538a9b205e2753a476d1a7c89179/lib/doh.c#L195 ) This relies on the compiler not rearranging the writes.
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
Impact
If an attacker can control the hostname used by curl and DNS-over-HTTPS is enabled, a one-byte buffer overflow can occur, potentially leading to memory corruption; however, in the common case the overwrite may be immediately remedied by a subsequent assignment, depending on compiler behaviour.
What the attacker needed
Curl built with DNS-over-HTTPS support; attacker controls the hostname; DNS-over-HTTPS is used
How it was found
- Build curl with address sanitizer or add an assert before returning from doh_encode.
- Issue a curl request with a very long hostname and the --doh-url option.
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 looks up a domain name using DNS-over-HTTPS, it packs the name into a small buffer. If the name is too long, it can write one byte past the end of the buffer, which is a memory safety bug. An attacker who can choose the domain name might exploit this to crash 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.