Skip to content

openvmm: virtiofs: aggregate synthetic root readdir performs unbounded infallible Vec::with_capacity(size) allocation #1598

Open

Description

@tangruize

Summary

When handling FUSE_READDIR or FUSE_READDIRPLUS on an aggregate mount's synthetic root directory, VirtioFs::read_synthetic_root_dir_with reserves the untrusted guest fuse_read_in.size (u32) directly with Vec::with_capacity(size as usize) without checking MAX_GUEST_BUFFER_SIZE or using fallible allocation. A malicious or buggy guest passing size = u32::MAX forces an immediate ~4 GiB heap reservation per request and aborts the host VMM / device process with SIGABRT if memory or cgroup/address-space limits cannot satisfy the allocation. Found by testing.

Root Cause

In vm/devices/virtio/virtiofs/src/lib.rs#L482-L496, VirtioFs::read_dir and VirtioFs::read_dir_plus forward the guest's raw arg.size: u32 directly to self.read_synthetic_root_dir(arg.offset, arg.size, ...). In vm/devices/virtio/virtiofs/src/aggregate.rs#L314-L325, VirtioFs::read_synthetic_root_dir_with allocates the reply buffer infallibly with no upper bound:

        let Some(aggregate) = self.inner.aggregate() else {
            return Ok(Vec::new());
        };
        let mut buffer = Vec::with_capacity(size as usize);

Every other guest-sized directory or data buffer path in virtiofs—guest_buffer, VirtioFsFile::read_dir_with, and VirtioFsFile::read_dir_microvm—rejects requests exceeding MAX_GUEST_BUFFER_SIZE (1 MiB) with lx::Error::E2BIG and allocates via Vec::try_reserve_exact mapped to lx::Error::ENOMEM.

Consequence

Any guest with access to an aggregate virtiofs mount (microVM or direct aggregate) can open FUSE_ROOT_ID (1) and issue FUSE_READDIR or FUSE_READDIRPLUS with size = u32::MAX (4_294_967_295). Under a memory-limited or cgroup-constrained VMM/device process, Rust's global allocator OOM handler aborts the entire host process (memory allocation of 4294967295 bytes failed, SIGABRT); even without an immediate OOM abort, concurrent FUSE requests can pin multi-gigabyte allocations per in-flight READDIR call and exhaust host memory.

Reproduction

  1. Construct an aggregate VirtioFs via VirtioFs::new_microvm_aggregate.
  2. Open the synthetic root directory via Fuse::open_dir(&fs, &request(FUSE_ROOT_ID), lx::O_RDONLY as u32) to obtain SYNTHETIC_ROOT_FH.
  3. Under a 512 MiB virtual address-space limit (ulimit -v 524288), call Fuse::read_dir(&fs, &request(FUSE_ROOT_ID), &fuse_read_in { fh, offset: 0, size: u32::MAX, .. }).
  4. Observe the process abort with SIGABRT (memory allocation of 4294967295 bytes failed) instead of returning Err(lx::Error::E2BIG).

Related: #1596, #1617, #1632, #1624.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    At the start of VirtioFs::read_synthetic_root_dir_with (after checking self.inner.aggregate()), return Err(lx::Error::E2BIG) when size as usize > crate::MAX_GUEST_BUFFER_SIZE, and replace Vec::with_capacity(size as usize) with Vec::new() followed by buffer.try_reserve_exact(size as usize).map_err(|_| lx::Error::ENOMEM)?, matching VirtioFsFile::read_dir_with and VirtioFsFile::read_dir_microvm. Note that read_synthetic_root_dir_with is also modified at the end of its entry loop when fixing small-buffer EINVAL handling on the synthetic root.

    Patch against OpenVMM 9d64daad3cbe14fb0591081b73c6c9648e18d0fb. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/virtio/virtiofs/src/aggregate.rs b/vm/devices/virtio/virtiofs/src/aggregate.rs
    index 5b9694132..cf540b0b9 100644
    --- a/vm/devices/virtio/virtiofs/src/aggregate.rs
    +++ b/vm/devices/virtio/virtiofs/src/aggregate.rs
    @@ -321,7 +321,13 @@ fn read_synthetic_root_dir_with(
             let Some(aggregate) = self.inner.aggregate() else {
                 return Ok(Vec::new());
             };
    -        let mut buffer = Vec::with_capacity(size as usize);
    +        if size as usize > crate::MAX_GUEST_BUFFER_SIZE {
    +            return Err(lx::Error::E2BIG);
    +        }
    +        let mut buffer = Vec::new();
    +        buffer
    +            .try_reserve_exact(size as usize)
    +            .map_err(|_| lx::Error::ENOMEM)?;
             // `offset` is the cookie of the next entry to emit (0 at start of stream).
             // Entry 0 => ".", 1 => "..", 2.. => children[index - 2].
             let mut index = offset;
    diff --git a/vm/devices/virtio/virtiofs/tests/synthetic_root_readdir_size.rs b/vm/devices/virtio/virtiofs/tests/synthetic_root_readdir_size.rs
    new file mode 100644
    index 000000000..22f442757
    --- /dev/null
    +++ b/vm/devices/virtio/virtiofs/tests/synthetic_root_readdir_size.rs
    @@ -0,0 +1,154 @@
    +// Copyright (c) Microsoft Corporation.
    +// Licensed under the MIT License.
    +
    +//! Tests that `read_dir` and `read_dir_plus` on an aggregate's synthetic root
    +//! cap the guest-requested buffer size at `MAX_GUEST_BUFFER_SIZE` (`E2BIG`)
    +//! rather than performing an unbounded infallible allocation.
    +
    +use fuse::Fuse;
    +use fuse::protocol::FUSE_LOOKUP;
    +use fuse::protocol::FUSE_ROOT_ID;
    +use fuse::protocol::fuse_in_header;
    +use fuse::protocol::fuse_read_in;
    +use tempfile::TempDir;
    +use tempfile::tempdir;
    +use test_with_tracing::test;
    +use virtiofs::VirtioFs;
    +use virtiofs::profile::MICROVM_ATTACHMENT_ID;
    +use virtiofs::profile::MicroVmAggregateChild;
    +use virtiofs::profile::MicroVmVirtioFsProfile;
    +use virtiofs::profile::microvm_file_identity;
    +use virtiofs::profile::microvm_root_identity;
    +use zerocopy::FromZeros;
    +use zerocopy::IntoBytes;
    +
    +fn request(node_id: u64) -> fuse::Request {
    +    let header = fuse_in_header {
    +        len: size_of::<fuse_in_header>() as u32,
    +        opcode: FUSE_LOOKUP,
    +        unique: 1,
    +        nodeid: node_id,
    +        uid: 0,
    +        gid: 0,
    +        pid: 0,
    +        padding: 0,
    +    };
    +    fuse::Request::new(header.as_bytes()).unwrap()
    +}
    +
    +fn read_in(fh: u64, offset: u64, size: u32) -> fuse_read_in {
    +    let mut arg = fuse_read_in::new_zeroed();
    +    arg.fh = fh;
    +    arg.offset = offset;
    +    arg.size = size;
    +    arg
    +}
    +
    +fn open_dir(fs: &VirtioFs, node_id: u64) -> u64 {
    +    Fuse::open_dir(fs, &request(node_id), lx::O_RDONLY as u32)
    +        .unwrap()
    +        .fh
    +}
    +
    +fn read_dir(fs: &VirtioFs, node_id: u64, fh: u64, offset: u64, size: u32) -> lx::Result<Vec<u8>> {
    +    Fuse::read_dir(fs, &request(node_id), &read_in(fh, offset, size))
    +}
    +
    +fn read_dir_plus(
    +    fs: &VirtioFs,
    +    node_id: u64,
    +    fh: u64,
    +    offset: u64,
    +    size: u32,
    +) -> lx::Result<Vec<u8>> {
    +    Fuse::read_dir_plus(fs, &request(node_id), &read_in(fh, offset, size))
    +}
    +
    +struct Aggregate {
    +    _directory: TempDir,
    +    fs: VirtioFs,
    +}
    +
    +impl Aggregate {
    +    fn new() -> Self {
    +        let directory = tempdir().unwrap();
    +        let work = directory.path().join("work");
    +        std::fs::create_dir(&work).unwrap();
    +        let seed = directory.path().join("seed.txt");
    +        std::fs::write(&seed, b"seed").unwrap();
    +        let children = vec![
    +            MicroVmAggregateChild::new(
    +                "work".to_owned(),
    +                microvm_root_identity(&work).unwrap(),
    +                false,
    +                Vec::new(),
    +                Vec::new(),
    +                Vec::new(),
    +            )
    +            .unwrap(),
    +            MicroVmAggregateChild::new_file(
    +                "seed".to_owned(),
    +                microvm_file_identity(&seed).unwrap(),
    +                true,
    +            )
    +            .unwrap(),
    +        ];
    +        let profile =
    +            MicroVmVirtioFsProfile::from_aggregate(MICROVM_ATTACHMENT_ID.to_owned(), children)
    +                .unwrap();
    +        let fs = VirtioFs::new_microvm_aggregate(&[&work, &seed], profile).unwrap();
    +        Self {
    +            _directory: directory,
    +            fs,
    +        }
    +    }
    +}
    +
    +/// A buffer size above `MAX_GUEST_BUFFER_SIZE` (1 MiB) is refused with `E2BIG`
    +/// before allocating, matching regular share directory listings. Running the
    +/// `u32::MAX` check in a child process with a bounded address space (`ulimit -v`)
    +/// ensures an unbounded infallible allocation aborts only the child process.
    +#[test]
    +fn synthetic_root_huge_page_size_is_refused() {
    +    if std::env::var("SYNTHETIC_ROOT_READDIR_LIMITED_CHILD").is_ok() {
    +        let aggregate = Aggregate::new();
    +        let fs = &aggregate.fs;
    +        let fh = open_dir(fs, FUSE_ROOT_ID);
    +        assert_eq!(
    +            read_dir(fs, FUSE_ROOT_ID, fh, 0, u32::MAX).unwrap_err(),
    +            lx::Error::E2BIG
    +        );
    +        assert_eq!(
    +            read_dir_plus(fs, FUSE_ROOT_ID, fh, 0, u32::MAX).unwrap_err(),
    +            lx::Error::E2BIG
    +        );
    +        assert_eq!(
    +            read_dir(fs, FUSE_ROOT_ID, fh, 0, 1024 * 1024 + 1).unwrap_err(),
    +            lx::Error::E2BIG
    +        );
    +        assert_eq!(
    +            read_dir_plus(fs, FUSE_ROOT_ID, fh, 0, 1024 * 1024 + 1).unwrap_err(),
    +            lx::Error::E2BIG
    +        );
    +        std::process::exit(0);
    +    }
    +
    +    let exe = std::env::current_exe().unwrap();
    +    let test = "synthetic_root_huge_page_size_is_refused";
    +    // 512 MiB of address space: enough for the test binary, less than the
    +    // 4 GiB the guest requests.
    +    let status = std::process::Command::new("sh")
    +        .arg("-c")
    +        .arg("ulimit -v 524288; exec \"$1\" --exact \"$2\"")
    +        .arg("sh")
    +        .arg(exe.as_os_str())
    +        .arg(test)
    +        .env("SYNTHETIC_ROOT_READDIR_LIMITED_CHILD", "1")
    +        .status()
    +        .unwrap();
    +    assert!(
    +        status.success(),
    +        "child process running read_dir on the synthetic root with size u32::MAX \
    +         under a bounded address space ended with {status:?}; expected E2BIG"
    +    );
    +}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: openvmmInitial title-keyword triage suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions