NIST deprecates RSA-2048 from 2030. Find every one of them before attackers do.
🦀
🦀 crates.io
Not in CISA KEV
MEDIUM severity

zlib-rs stack overflow during decompression with malicious inputGHSA-j3px-q95c-9683

MEDIUM

GHSA-j3px-q95c-9683 is a medium-severity (CVSS 5.3) remote code execution vulnerability in zlib-rs. A fix is available for zlib-rs — see the affected versions and patch details below.

Also known asRUSTSEC-2024-0401
Published
Updated
Affected
3 pkgs
Patched
3 / 3
Exploits
None indexed
Exploitation data as of Dec 4, 2024 · OSV.dev, FIRST.org (EPSS)

Real-World Exposure

3 pkgs affected
🦀zlib-rs🦀libz-rs-sys🦀libz-rs-sys-cdylib

Real-time download stats are indexed for npm and PyPI packages. This vulnerability affects crates.io packages — download data is not available via public APIs for these ecosystems.

Description

A denial of service vulnerability was found in zlib-rs, triggered by specially constructed input. This input causes a stack overflow, resulting in the process using zlib-rs to crash.

Impact

Due to the way LLVM handles the zlib-rs codebase, tail calls were not guaranteed. This caused certain input patterns to result in a large number of stack frames being required, quickly resulting in a stack overflow. These are unlikely to occur in practice, but a dedicated attacker can construct malicious input files.

After stack overflows were found by @inahga with a fuzzer, we dove into the assembly, and found some cases where the stack grew

.LBB109_326:
    mov rdi, rbx
    call zlib_rs::inflate::State::type_do
    jmp .LBB109_311

.LBB109_311:
    lea rsp, [rbp - 40]
    pop rbx
    pop r12
    pop r13
    pop r14
    pop r15
    pop rbp
    .cfi_def_cfa rsp, 8
    ret

LLVM wants to centralize the cleanup before the return (many other blocks jump to LBB109_311), thereby invalidating a tail call to type_do. We were not able to get rid of this call without introducing one elsewhere: we just don't currently have the power to tell LLVM what we want it to do.

So, we switch back to loop+match waiting for changes to rust to make a more efficient implementation possible. Performance-wise, the damage is relatively minimal: we're just slower in cases where we already were slower than C. We are faster in cases where the relevant code is barely touched (in these cases the logic quickly moves into a hot inner loop and just spends most of its time there).

Patches

Version 0.4.0 patches the problem and is no longer vulnerable.

Workarounds

Users of zlib-rs should upgrade to the latest version. Users could alternatively run zlib-rs in a separate process to prevent a stack overflow crashing the entire program. In some situations a signal handler can be used to catch a stack overflow happening.

Affected Packages

3 total 3 fixed
EcosystemPackageVulnerable rangeFix
🦀crates.iozlib-rsall versions0.4.0cargo update -p zlib-rs --precise 0.4.0
🦀crates.iolibz-rs-sysall versions0.4.0cargo update -p libz-rs-sys --precise 0.4.0
🦀crates.iolibz-rs-sys-cdyliball versions0.4.0cargo update -p libz-rs-sys-cdylib --precise 0.4.0

Detection & mitigation playbook

Open-source dependency
  1. Detect

    Scan your dependency tree (package-lock.json, pnpm-lock.yaml, requirements.txt, go.sum, etc.) for zlib-rs, including transitive dependencies — a direct dependency you never call can still pull in a vulnerable version.

  2. Fix

    Update zlib-rs to 0.4.0 or later, then make sure no transitive (indirect) dependency still pins the vulnerable range — O3 confirms GHSA-j3px-q95c-9683 is resolved across your whole dependency graph.

  3. Workarounds

    Constrain what reaches the vulnerable code: limit the size and shape of untrusted input, isolate the affected component in a sandboxed or least-privileged process, and enable the platform's memory-safety mitigations (ASLR, stack protector, hardened allocator) so an out-of-bounds access is more likely to fail closed than to be exploitable.

Frequently Asked Questions

A denial of service vulnerability was found in zlib-rs, triggered by specially constructed input. This input causes a stack overflow, resulting in the process using zlib-rs to crash. ### Impact Due to the way LLVM handles the zlib-rs codebase, tail calls were not guaranteed. This caused certain input patterns to result in a large number of stack frames being required, quickly resulting in a stack overflow. These are unlikely to occur in practice, but a dedicated attacker can construct malicious input files. After stack overflows were found by @inahga with a fuzzer, we dove into the assembly
O3 Security · Impact-Aware SCA

Is GHSA-j3px-q95c-9683 in your dependencies?

Find it across crates.io, including transitive dependencies.

GHSA-j3px-q95c-9683: zlib-rs DoS — Fixed in 0.4.0