Security Vulnerability Report: Protocol Injection via Programmatic Options
libcurl fails to sanitise CRLF sequences in programmatic string options such as CURLOPT_USERNAME, CURLOPT_PASSWORD and CURLOPT_MAIL_FROM, so these are sent verbatim to text-based protocol servers. An attacker who can influence those option values could inject extra FTP, SMTP, POP3 or IMAP commands.
- Disclosed after 11 hours
Summary
Multiple text-based protocol handlers in libcurl (including FTP, SMTP, POP3, and IMAP) are vulnerable to protocol command injection. This occurs when an application sets credentials or other protocol-specific options programmatically (e.g., via CURLOPT_USERNAME, CURLOPT_PASSWORD, or CURLOPT_MAIL_FROM) using strings that contain CRLF (\r\n) sequences. These unsanitized inputs are passed directly to the protocol state machines and sent over the wire, allowing an attacker to inject arbitrary protocol commands.
Affected Protocols
- FTP: Injection via
USERandPASScommands. - SMTP: Injection via
MAIL FROM,RCPT TO, and SASL authentication tokens. - POP3: Injection via
USERandPASScommands. - IMAP: Injection via
LOGINcommand (improper quoting of CRLF).
Root Cause Analysis
The vulnerability stems from a lack of centralized sanitization for programmatic string options in lib/setopt.c and a corresponding lack of final-stage sanitization in the low-level Curl_pp_sendf function in lib/pingpong.c.
While curl implements robust sanitization for credentials provided via URLs (using REJECT_CTRL in Curl_urldecode) and .netrc files (using str_has_ctrl in override_login), these checks are bypassed when options are set programmatically.
Vulnerable Code Path (Example: FTP)
- Input: Application calls curl_easy_setopt(curl, CURLOPT_USERNAME, "user\r\nINJECTED_COMMAND").
- Storage: lib/setopt.c stores the string without validation.
- Transfer: lib/transfer.c copies the string to the internal connection structure.
- Protocol Execution: lib/ftp.c calls ftp_state_user():
C /* lib/ftp.c:3547 */ result = Curl_pp_sendf(data, &ftpc->pp, "USER %s", ftp->user); - Transmission: lib/pingpong.c's Curl_pp_sendf appends a final
\r\nand sends the data:USER user INJECTED_COMMAND \r\n
Proof of Concept
A simple C program demonstrates the injection by setting a malicious username:
#include <curl/curl.h>
int main(void) {
CURL *curl = curl_easy_init();
if(curl) {
/* Injecting a second USER command or any other protocol command */
curl_easy_setopt(curl, CURLOPT_URL, "ftp://127.0.0.1:2121/");
curl_easy_setopt(curl, CURLOPT_USERNAME, "user\r\nINJECTED_COMMAND");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}
Remediation
- Centralized Sanitization: Implement a str_has_ctrl check in lib/setopt.c for all string options that are used in text-based protocol commands, unless the protocol explicitly allows control characters (marked by
PROTOPT_USERPWDCTRL). - Protocol-Level Defense: Enhance Curl_pp_sendf to detect and reject (or safely escape) internal CRLF sequences in formatted commands.
- Consistent Validation: Ensure that
CURLOPT_USERNAME,CURLOPT_PASSWORD, and protocol-specific options likeCURLOPT_MAIL_FROMare validated against the same standards as URL-based credentials.
Impact
An attacker who can influence the strings passed to curl_easy_setopt in a libcurl-based application can:
- Bypass authentication by injecting subsequent commands.
- Perform unauthorized actions (e.g., deleting files via FTP, sending spoofed emails via SMTP).
- Exfiltrate data by redirecting protocol flows.
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 the programmatic option setters in lib/setopt.c store strings without the control-character check used for URL and .netrc credentials, and Curl_pp_sendf adds no final-stage rejection of embedded CRLF.
What the attacker needed
Application passes attacker-influenced strings to libcurl…; target uses a text-based protocol (FTP, SMTP, POP3, IMAP)
How it was found
- The application sets a protocol option such as the username to a string containing CRLF.
- libcurl stores the string without validation.
- The string is copied into the connection structure during transfer.
- The protocol state machine formats a command with the string.
- Curl_pp_sendf appends a final CRLF and sends the data, so the injected text becomes a separate command.
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.
Many internet protocols are line-based: each command is a line of text ending in a special pair of invisible characters (carriage return and line feed, together called CRLF). If a program puts user-supplied text into a command without removing those characters, the text can start a new line that the server treats as a second command. Here libcurl only checked for this when credentials came from a URL or a .netrc file, not when an application set them through its programming interface.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.