Actions
Security #9211
closed
PA
JL
ftp: preliminary replies are retained without a memcap
Security #9211:
ftp: preliminary replies are retained without a memcap
Affected Versions:
Label:
CVE:
Git IDs:
645893625b68f03ba30a54bb8d8eb5944ef6524a
a7bb9a922712d061b07d86a6f2276f47e9f86bff
ab0c4e0dbc66a9a73100ca4ddb54e99ff471e3d9
afa823702dd717f7e2d8272a94f0e7e6d2f4337f
Description
Original report
## Summary
With the packaged FTP settings, `ftp.memcap` is commented out and `ftp_config_memcap` stays 0, so `FTPCheckMemcap` accepts every allocation. A reply whose first three characters are `1` and two digits, followed by whitespace, is a positive preliminary reply. `FTPParseResponse` appends that line to the open transaction and does not set `done`. `max-tx` only limits how many transactions are created, not how many lines an open transaction keeps. Each short `150 \r\n` line is six bytes on the wire and about 21 times that in RSS, because the parser stores a fixed-size response object per line. On Suricata 8.0.7, 16711680 bytes of those lines across 17 flows moved the process from about 35 MiB to 376.2 MiB under a 384 MiB cap, and the next flow was OOM-killed (exit 137). A completed FTP login on the same cap stayed up. Unpack the attachment and run `./verify.sh`.
## Affected Versions
Confirmed on Suricata 8.0.7 RELEASE, the image `jasonish/suricata:latest` at test time. FTP parsing is enabled in that image. `app-layer.protocols.ftp.memcap` is commented, so the C initial value 0 is what runs. `FTP memcap` is not logged, and the `ftp.memcap` counter stays 0. The image sets `stream.reassembly.depth` to 1 MiB. Crossing that depth marks the direction disrupted and the transaction is then freed, so one flow cannot hold more than 1 MiB of reply bytes. It can still hold every line below that depth.
## Preconditions
The sensor must be parsing FTP with the packaged default, not with `ftp.memcap` set to a positive value. The attacker needs a TCP port 21 session on a link the sensor sees. No Suricata account is required. Each flow has to stay under the 1 MiB reassembly depth and stay open. A reply that is not a preliminary reply completes the transaction and the list can be freed.
## Security Impact
The worker keeps the reply list until the cgroup killer stops the process. In this run `--memory` and `--memory-swap` were both 384 MiB. Startup RSS was 35.29 MiB. After a completed login the process was still running at 35.26 MiB with `OOMKilled` false. After 17694720 bytes of `150 \r\n` (18 flows, 983040 bytes each) `OOMKilled` was true and the exit code was 137. The last sample while the process was still alive was 376.2 MiB, with `ftp.memuse` at 208899795 and `ftp.memcap` at 0. Capture drops were 0 and `stream_depth_reached` was 0, so the lines were stored, not discarded by the depth cleanup.
This is a detection-process crash. It does not show code execution or a logic bypass. The cgroup kept the kill inside the container. The same reply bytes replayed from a pcap also kill the process when the cap is 384 MiB or 1024 MiB. A completed-session pcap does not.
## Vulnerable Code
`src/app-layer-ftp.c`. `ftp_config_memcap` is initialized to 0. `SCFTPGetConfigValues` writes it only when `app-layer.protocols.ftp.memcap` is present, which the packaged yaml does not set.
```c
static int FTPCheckMemcap(uint64_t size)
{
if (ftp_config_memcap == 0 || size + SC_ATOMIC_GET(ftp_memuse) <= ftp_config_memcap)
return 1;
```
A preliminary reply skips completion:
```c
static inline bool FTPIsPPR(const uint8_t *input, uint32_t input_len)
{
return input_len >= 4 && isdigit(input[0]) && input[0] == '1' &&
isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
}
```
```c
TAILQ_INSERT_TAIL(&tx->response_list, wrapper, next);
/* ... */
if (FTPIsPPR(line.buf, line.len)) {
continue;
}
tx->done = true;
```
`FTPTransactionCreate` applies `ftp_config_maxtx` (default 1024) only when allocating a new transaction. Lines added to the transaction that `USER` already opened never hit that check.
`rust/src/ftp/response.rs` allocates a `FTPResponseLine` plus a separate buffer for the three-digit code on every line. `total_size` is `size_of::<FTPResponseLine>()` plus the string lengths (75 bytes accounted for `150 \r\n`). RSS is higher because each of those heap blocks has allocator overhead. 16711680 payload bytes produced about 358 MiB of RSS, 21 times the bytes on the wire. `ftp.memuse` for the same bytes was 208899795, 12.5 times the wire size.
## Call Chain
Packet on the sniffed bridge -> `StreamTcpReassembleAppLayer` -> `FTPParseResponse` (`src/app-layer-ftp.c:680`) -> `SCFTPParseResponseLine` (`rust/src/ftp/response.rs:80`) -> `FTPResponseWrapperAlloc` (`src/app-layer-ftp.c:168`) -> `TAILQ_INSERT_TAIL` (`src/app-layer-ftp.c:780`) -> `FTPIsPPR` (`src/app-layer-ftp.c:665`) true -> `continue` without `tx->done = true` (`src/app-layer-ftp.c:790`). `FTPGetAlstateProgress` stays `FTP_STATE_IN_PROGRESS` while `done` is false (`src/app-layer-ftp.c:959`), so `AppLayerParserTransactionsCleanup` does not free the list. `FTPCheckMemcap` (`src/app-layer-ftp.c:113`) returns success while `ftp_config_memcap` is 0.
## Payload
One FTP control flow, kept under the 1 MiB reassembly depth and left established:
1. Server: `220 ftp ready\r\n` (completes the banner transaction).
2. Client: `USER ATTACK 983040\r\n` (opens a transaction and asks for a bounded reply).
3. Server: `150 \r\n` repeated until 983040 bytes, then silence. The TCP session stays open.
`150 \r\n` is `1`, `5`, `0`, space, CR, LF. `FTPIsPPR` is true. The live run opened 18 such flows. Seventeen of them were fully stored (16711680 bytes) and the eighteenth crossed the 384 MiB cap.
A completed session, used as the control, is `220`, `USER control`, `331`, `PASS anonymous`, `230`, `SYST`, `215`, `QUIT`, `221`. Every reply sets `done`.
## Reproduction
Image `jasonish/suricata:latest` (8.0.7 RELEASE). The FTP peer is `python:3.11-slim` because it is only a TCP listener. The host connects to `127.0.0.1:18221`, published to `ftpoom-app:21` on bridge `br-ftpoom` (network `suri-ftpoom`, `172.31.21.0/24`). `ftpoom-suri` uses host networking to sniff that bridge, with `--memory 384m` and `--memory-swap 384m`. `--set af-packet.1.cluster-id=21` sets cluster id 21 on the `default` af-packet entry, which is the one used for a bridge that is not `eth0`. `-k none` is required because bridge checksum offload would otherwise drop the packets. `--runmode single` keeps startup RSS near 35 MiB so the cap is not already exhausted. The image yaml is not edited, and `ftp.memcap` is not set to 0 on the command line.
```
./verify.sh
```
```
control_completed=true
control_running=true
control_oom_killed=false
control_stats=35.26MiB / 384MiB
baseline_stats=35.29MiB / 384MiB
attack_flows=18
attack_bytes=17694720
attack_last_stats=376.2MiB / 384MiB
attack_oom_killed=true
attack_exit_code=137
VERDICT: PASS
```
## Fix Recommendation
Give FTP a finite memcap when `app-layer.protocols.ftp.memcap` is absent. The commented 64 MiB in the packaged yaml is the documented ceiling. `FTPCheckMemcap` must not treat 0 as unlimited. Also cap how many response lines one transaction retains. A preliminary reply can stay incomplete, but `response_list` still needs a bound that `max-tx` does not provide. When the cap is hit, stop accepting further reply lines on that flow and surface the existing memcap counter, instead of allocating until the process is killed.
## Credits
Binbin Xu of Tencent YUNDING LAB CodeBuddy Security
Actions