Ecosyste.ms: Advisories

An open API service providing security vulnerability metadata for many open source software ecosystems.

Security Advisories: GSA_kwCzR0hTQS1jODI3LWhmdzYtcXd2bc4AA2jB

rustix's `rustix::fs::Dir` iterator with the `linux_raw` backend can cause memory explosion

Summary

When using rustix::fs::Dir using the linux_raw backend, it's possible for the iterator to "get stuck" when an IO error is encountered. Combined with a memory over-allocation issue in rustix::fs::Dir::read_more, this can cause quick and unbounded memory explosion (gigabytes in a few seconds if used on a hot path) and eventually lead to an OOM crash of the application.

Details

Discovery

The symptoms were initially discovered in https://github.com/imsnif/bandwhich/issues/284. That post has lots of details of our investigation. See this post and the Discord thread for details.

Diagnosis

This issue is caused by the combination of two independent bugs:

  1. Stuck iterator
  1. Memory over-allocation

Since <Dir as Iterator>::next calls Dir::read, which in turn calls Dir::read_more, this means an IO error encountered during reading a directory can lead to rapid and unbounded growth of memory use.

PoC

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // create a directory, get a FD to it, then unlink the directory but keep the FD
    std::fs::create_dir("tmp_dir")?;
    let dir_fd = rustix::fs::openat(
        rustix::fs::CWD,
        rustix::cstr!("tmp_dir"),
        rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC,
        rustix::fs::Mode::empty(),
    )?;
    std::fs::remove_dir("tmp_dir")?;

    // iterator gets stuck in infinite loop and memory explodes
    rustix::fs::Dir::read_from(dir_fd)?
        // the iterator keeps returning `Some(Err(_))`, but never halts by returning `None`
        // therefore if the implementation ignores the error (or otherwise continues
        // after seeing the error instead of breaking), the loop will not halt
        .filter_map(|dirent_maybe_error| dirent_maybe_error.ok())
        .for_each(|dirent| {
            // your happy path
            println!("{dirent:?}");
        });

    Ok(())
}

Impact

If a program tries to access a directory with its file descriptor after the file has been unlinked (or any other action that leaves the Dir iterator in the stuck state), and the implementation does not break after seeing an error, it can cause a memory explosion.

As an example, Linux's various virtual file systems (e.g. /proc, /sys) can contain directories that spontaneously pop in and out of existence. Attempting to iterate over them using rustix::fs::Dir directly or indirectly (e.g. with the procfs crate) can trigger this fault condition if the implementation decides to continue on errors.

An attacker knowledgeable about the implementation details of a vulnerable target can therefore try to trigger this fault condition via any one or a combination of several available APIs. If successful, the application host will quickly run out of memory, after which the application will likely be terminated by an OOM killer, leading to denial of service.

Permalink: https://github.com/advisories/GHSA-c827-hfw6-qwvm
JSON: https://advisories.ecosyste.ms/api/v1/advisories/GSA_kwCzR0hTQS1jODI3LWhmdzYtcXd2bc4AA2jB
Source: GitHub Advisory Database
Origin: Unspecified
Severity: Moderate
Classification: General
Published: about 1 year ago
Updated: 3 months ago


CVSS Score: 6.5
CVSS vector: CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:H

EPSS Percentage: 0.00043
EPSS Percentile: 0.09611

Identifiers: GHSA-c827-hfw6-qwvm, CVE-2024-43806
References: Repository: https://github.com/bytecodealliance/rustix
Blast Radius: 29.6

Affected Packages

cargo:rustix
Dependent packages: 159
Dependent repositories: 36,039
Downloads: 216,607,173 total
Affected Version Ranges: >= 0.38.0, < 0.38.19, >= 0.37.0, < 0.37.25, >= 0.36.0, < 0.36.16, >= 0.35.11, < 0.35.15
Fixed in: 0.38.19, 0.37.25, 0.36.16, 0.35.15
All affected versions: 0.35.11, 0.35.12, 0.35.13, 0.35.14, 0.36.0, 0.36.1, 0.36.2, 0.36.3, 0.36.4, 0.36.5, 0.36.6, 0.36.7, 0.36.8, 0.36.9, 0.36.10, 0.36.11, 0.36.12, 0.36.13, 0.36.14, 0.36.15, 0.37.0, 0.37.1, 0.37.2, 0.37.3, 0.37.4, 0.37.5, 0.37.6, 0.37.7, 0.37.8, 0.37.9, 0.37.10, 0.37.11, 0.37.12, 0.37.13, 0.37.14, 0.37.15, 0.37.16, 0.37.17, 0.37.18, 0.37.19, 0.37.20, 0.37.21, 0.37.22, 0.37.23, 0.37.24, 0.38.0, 0.38.1, 0.38.2, 0.38.3, 0.38.4, 0.38.5, 0.38.6, 0.38.7, 0.38.8, 0.38.9, 0.38.10, 0.38.11, 0.38.12, 0.38.13, 0.38.14, 0.38.15, 0.38.16, 0.38.17, 0.38.18
All unaffected versions: 0.0.0, 0.26.0, 0.26.1, 0.26.2, 0.27.0, 0.27.1, 0.28.0, 0.29.0, 0.29.1, 0.29.2, 0.30.0, 0.30.1, 0.31.0, 0.31.1, 0.31.2, 0.31.3, 0.32.0, 0.32.1, 0.33.0, 0.33.1, 0.33.2, 0.33.3, 0.33.4, 0.33.5, 0.33.6, 0.33.7, 0.34.0, 0.34.1, 0.34.2, 0.34.3, 0.34.4, 0.34.5, 0.34.6, 0.34.7, 0.34.8, 0.35.5, 0.35.6, 0.35.7, 0.35.8, 0.35.9, 0.35.10, 0.35.15, 0.35.16, 0.36.16, 0.36.17, 0.37.25, 0.37.26, 0.37.27, 0.38.19, 0.38.20, 0.38.21, 0.38.22, 0.38.23, 0.38.24, 0.38.25, 0.38.26, 0.38.27, 0.38.28, 0.38.29, 0.38.30, 0.38.31, 0.38.32, 0.38.33, 0.38.34, 0.38.35, 0.38.36, 0.38.37, 0.38.38, 0.38.39