Integer-underflow leads to heap over-read in TFTP implementation
A bounds check in libcurl's TFTP client can underflow when a malicious server negotiates a small block size, allowing an oversized filename to bypass the check. This causes curl to send heap memory beyond the allocated buffer to the server.
- Disclosed after 33 hours
libcurl on commit 3ee1d3b573e6ea36fb478dbd0d9913483b900928 contains a vulnerability in its TFTP implementation that can cause curl or a libcurl-user to send heap memory beyond the bounds of an allocated chunk to a malicious TFTP server.
The vulnerability lies in lib/tftp.c, in function tftp_send_first() on line 467:
if(strlen(filename) > (state->blksize - strlen(mode) - 4)) {
// [...]
}
curl_msnprintf((char *)state->spacket.data + 2,
state->blksize,
"%s%c%s%c", filename, '\0', mode, '\0');
sbytes = 4 + strlen(filename) + strlen(mode);
// [...]
senddata = sendto(state->sockfd, (void *)state->spacket.data,
(SEND_TYPE_ARG3)sbytes, 0,
CURL_SENDTO_ARG5(&remote_addr->curl_sa_addr),
(curl_socklen_t)remote_addr->addrlen);
If curl is invoked with --tftp-no-options or a libcurl-user sets CURLOPT_TFTP_NO_OPTIONS to 1, a malicious TFTP server can cause an integer underflow in line 467 that disables the bounds check for strlen(filename). If the mode stays the default "octet" and the server communicates a minimum block size of 8 then line 467 becomes
if(strlen(filename) > (8 /* state->blksize */ - 5 /* strlen(mode) */ - 4)) {
leading to an integer underflow such that the condition becomes false.
After that, sbytes can have any value depending on the length of filename and the following sendto() call may send out-of-bounds data.
Impact
I think it is pretty clear that this is at most a LOW severity vulnerabiliy. The preconditions to exploit the vulnerability are pretty strict and even then it only leads to a leak of memory contents.
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 an integer underflow in the bounds check when the block size is smaller than the combined length of the mode string and fixed overhead, making the comparison always false.
Impact
A malicious TFTP server could cause curl to leak heap memory contents, though the report rates this as at most LOW severity due to strict preconditions.
What the attacker needed
Curl built with TFTP support; TFTP no-options mode enabled; malicious TFTP server; server sets block size to 8
How it was found
- Invoke curl with --tftp-no-options or set CURLOPT_TFTP_NO_OPTIONS to 1.
- Connect to a malicious TFTP server that communicates a minimum block size of 8.
- Use a filename longer than the underflowed threshold.
- The bounds check is bypassed and sendto() sends out-of-bounds heap data.
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.
TFTP is a simple file transfer protocol. When curl connects to a TFTP server, it sends a request containing a filename and a mode. The code checks that the filename is not too long for the buffer, but a malicious server can make the buffer size very small, causing the check to fail and curl to send extra memory contents to the server.
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.