Denial of Service by memory exhaustion in net/imap
The net-imap library reads server responses and, when a response contains a literal string, immediately allocates memory for the byte count the server declares. A malicious or compromised IMAP server could declare an enormous literal size, causing memory exhaustion and a crash or system instability.
- Disclosed after 23 hours
Summary by the program
There is a possibility for denial of service by memory exhaustion when net-imap reads server responses. At any time while the client is connected, a malicious server can send can send a "literal" byte count, which is automatically read by the client's receiver thread. The response reader immediately allocates memory for the number of bytes indicated by the server response.
This should not be an issue when securely connecting to trusted IMAP servers that are well-behaved. It can affect insecure connections and buggy, untrusted, or compromised servers (for example, connecting to a user supplied hostname).
https://github.com/ruby/net-imap/security/advisories/GHSA-j3g3-5qv5-52mj
The report
Summary
There is a possibility for denial of service by memory exhaustion when net-imap reads server responses. At any time while the client is connected, a malicious server can send can send a "literal" byte count, which is automatically read by the client's receiver thread. The response reader immediately allocates memory for the number of bytes indicated by the server response.
This should not be an issue when securely connecting to trusted IMAP servers that are well-behaved. It can affect insecure connections and buggy, untrusted, or compromised servers (for example, connecting to a user supplied hostname).
Details
The IMAP protocol allows "literal" strings to be sent in responses, prefixed with their size in curly braces (e.g. {1234567890}\r\n). When Net::IMAP receives a response containing a literal string, it calls IO#read with that size. When called with a size, IO#read immediately allocates memory to buffer the entire string before processing continues. The server does not need to send any more data. There is no limit on the size of literals that will be accepted.
Impact
Memory exhaustion leading to program crash or system instability
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 client trusted a server-supplied literal size and allocated a buffer for it without any upper limit.
Impact
A malicious, buggy or compromised server could exhaust the client's memory, leading to a program crash or system instability; trusted, well-behaved servers are not affected.
What the attacker needed
Client connects to an untrusted or compromised server; server sends a response with a huge literal size
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 used to fetch email from a mail server. When a server sends a message, it can first announce how many bytes are coming, and this library trusted that number and reserved that much memory straight away. A hostile server could announce a huge number, so the program runs out of memory and crashes.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.