GreedyBear icon indicating copy to clipboard operation
GreedyBear copied to clipboard

A single payload can exceed MAX_QUARANTINE_SIZE_GB

Open ABHIGYA0205 opened this issue 1 month ago • 8 comments

Description

PayloadExtractionJob.run() checks whether current quarantine usage has reached MAX_QUARANTINE_SIZE_GB before starting each download. If usage is below the limit, it starts the download without checking whether the payload will fit in the remaining space. The download path reads the response with response.content and then saves the payload, so one file can exceed the configured limit.

The setting is described as the maximum disk usage in docker/env_file_template. The check is at payload_extraction.py; the response is read and saved at lines 265–282.

Reproduction

I ran a temporary Django test against my local checkout with:

  • MAX_QUARANTINE_SIZE_GB set to 1 byte
  • _quarantine_usage_bytes() mocked to return 0
  • metadata for a 2-byte payload
  • a mocked 2-byte download response

PayloadExtractionJob.run() stored a payload record with size=2 and a non-empty payload file. The test passed because it asserted this behavior. The test used mocked HTTP and mocked starting usage; it was not a live T-Pot integration test.

Expected behavior

A payload should not cause quarantine usage to exceed the configured limit. Consider checking the available space before downloading and enforcing a byte limit while streaming the response. If a payload must be deferred, its metadata should remain available for later handling.

Impact and scope

This can exceed the configured disk quota, and reading the full response may cause substantial worker memory use for a sufficiently large file. I have not verified that an outside actor can cause a real T-Pot deployment to supply such a file, or tested an actual out-of-memory event; this report is not claiming a confirmed remote DoS.

This is a different edge case from #1532, which concerned retaining metadata for payloads skipped after the quota was reached; that behavior was addressed by #1570. Open PR #1764 also touches the extraction code.

ABHIGYA0205 avatar Oct 27 '26 20:10 ABHIGYA0205

Hi, I'd like to pick this up.

The bug is in PayloadExtractionJob.run(). It checks self._quarantine_usage_bytes() against max_size_bytes before the loop, but it doesn't check the size of the incoming payload in _download_and_store(). Since response.content loads the whole file into RAM and then writes it to disk, a single large payload will bypass the quota check, fill the disk, and potentially cause an OOM crash.

I see that PR #1764 is currently open and touches this exact extraction code. Is it okay if I start working on this now, or should I wait for #1764 to merge first to avoid conflicts?

My plan is to update _download_and_store() to:

  1. Calculate remaining_space = max_size_bytes - self._quarantine_usage_bytes().
  2. Check the Content-Length header against remaining_space before reading the content.
  3. If the payload exceeds the remaining space, log a warning and return False so the file is safely skipped without blowing the quota.
  4. (Stretch) Stream the body with a hard byte limit instead of relying solely on Content-Length, in case the header is missing or lying.

I recently had PR #1680 merged here and would love to tackle this next. Let me know!

zcosmicw avatar Oct 27 '26 21:10 zcosmicw

Hey @ABHIGYA0205 ! Thanks, good catch! The severity of this is quite low, so I would wait until issue #1511 is closed / PR #1764 is merged before assigning this issue.

regulartim avatar Oct 28 '26 09:10 regulartim

Hi @regulartim, as issue #1551 is closed. So this is good to go I think. Anyone taking this? @ABHIGYA0205, @zcosmicw?

Prasad8830 avatar Nov 27 '26 20:11 Prasad8830

Is this still free? I'd like to work on it if nobody else is.

pantha704 avatar Nov 28 '26 16:11 pantha704

hey @regulartim i would like to work on this , will raise the pr after assigned.

ABHIGYA0205 avatar Nov 28 '26 17:11 ABHIGYA0205

Hey all, since I just assigned @ABHIGYA0205 to another issue, @Prasad8830 can take this.

regulartim avatar Nov 30 '26 15:11 regulartim

Hey all, since I just assigned @ABHIGYA0205 to another issue, @Prasad8830 can take this.

Sure @regulartim

Prasad8830 avatar Dec 03 '26 09:12 Prasad8830

@regulartim, the fix I have in mind is to check whether the next payload actually fits in the space that's left, not just whether the quarantine is already full. In _extract_window I'd work out the remaining bytes and pass them into _download_and_store. If the listing's size or the Content-Length header already says the file is too big, it gets skipped before downloading. Otherwise the file is downloaded with stream=True and read in chunks into a temporary file, and the download stops as soon as it goes over the limit. That also covers a missing or wrong header, and it means we no longer load the whole file into memory through response.content.

A few things I wanted to check before writing it - i. for a payload that's too big, I was planning to store it as metadata only, the same way #1570 handles the quota case, so it isn't lost with the window. Is that fine, or should it just be skipped? ii. after one oversized file, I'd keep going with the rest of the batch since smaller files may still fit. Or would you prefer it stops like it does when the quota is full? iii. while streaming, the file would go into a SpooledTemporaryFile, so a big one spills to /tmp before being saved to quarantine. Is that ok, or would you rather it write a partial file straight into the quarantine dir and rename it at the end?

Prasad8830 avatar Dec 03 '26 18:12 Prasad8830