Skip to content

openvmm: virtiofs: aggregate synthetic root returns empty EOF reply instead of EINVAL when buffer fits no entry #1617

Open

Description

@tangruize

Summary

When a guest issues FUSE_READDIR or FUSE_READDIRPLUS on an aggregate mount's synthetic root directory with a non-zero buffer size too small to hold the next directory entry, VirtioFs::read_synthetic_root_dir_with returns Ok(Vec::new()) instead of Err(lx::Error::EINVAL). Because FUSE defines an empty directory read reply as end-of-directory, the guest truncates the synthetic root's directory listing instead of receiving EINVAL. Found by testing.

Root Cause

In vm/devices/virtio/virtiofs/src/aggregate.rs#L314-L366, VirtioFs::read_synthetic_root_dir_with breaks out of the entry-serialization loop as soon as write_synthetic_dot or write_child_entry reports !fit, and unconditionally returns Ok(buffer) even when buffer is still empty:

            if !fit {
                break;
            }
            index = next;
        }
        Ok(buffer)

For example, a plain FUSE_READDIR entry for . requires fuse_dirent_align(size_of::<fuse_dirent>() + 1) = 32 bytes (fuse_direntplus requires 160 bytes), so any size in 1..=31 (1..=159 for FUSE_READDIRPLUS), or any continuation read whose size cannot hold the aligned entry of the next child share name, causes !fit on the first attempted entry and returns a 0-byte buffer. By contrast, real share directories reject undersized buffers that have remaining entries but fit none with EINVAL in both VirtioFsFile::read_dir_with and VirtioFsFile::read_dir_microvm.

Consequence

FUSE and getdents64 treat a 0-byte FUSE_READDIR / FUSE_READDIRPLUS reply as end-of-directory rather than a short-buffer error. A caller supplying a small buffer at offset 0 sees an empty root directory, and a caller whose continuation buffer fits . and .. (64 bytes) or short share names before hitting a longer share name receives a silent early EOF that hides the remaining shares instead of failing with EINVAL as Linux getdents64 and regular share directories do.

Reproduction

  1. Construct an aggregate VirtioFs via VirtioFs::new_microvm_aggregate with at least one child share (for example "work").
  2. Open the synthetic root directory via Fuse::open_dir(&fs, &request(FUSE_ROOT_ID), lx::O_RDONLY as u32).
  3. Call Fuse::read_dir at offset = 0 with size = 31 (or Fuse::read_dir_plus with size = 159), or first read . and .. with size = 64 (next_cookie = 2) and then call Fuse::read_dir at offset = 2 with size = 31.
  4. Observe Ok(vec![]) instead of Err(lx::Error::EINVAL).

Related: #1598, #1632, #1624.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    Track whether read_synthetic_root_dir_with broke out of the loop because an entry did not fit (!fit) rather than because index reached the end of aggregate.registry.read().entries. Before returning Ok(buffer), return Err(lx::Error::EINVAL) if an entry failed to fit and buffer.is_empty(), matching VirtioFsFile::read_dir_with and VirtioFsFile::read_dir_microvm. Note that read_synthetic_root_dir_with is also modified at the top of the function when bounding guest buffer allocations against MAX_GUEST_BUFFER_SIZE.

    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..399c25a0c 100644
    --- a/vm/devices/virtio/virtiofs/src/aggregate.rs
    +++ b/vm/devices/virtio/virtiofs/src/aggregate.rs
    @@ -325,6 +325,7 @@ fn read_synthetic_root_dir_with(
             // `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;
    +        let mut full = false;
             while let Some(next) = index.checked_add(1) {
                 let fit = match index {
                     0 => self.write_synthetic_dot(&mut buffer, ".", next, plus),
    @@ -358,10 +359,14 @@ fn read_synthetic_root_dir_with(
                     }
                 };
                 if !fit {
    +                full = true;
                     break;
                 }
                 index = next;
             }
    +        if full && buffer.is_empty() {
    +            return Err(lx::Error::EINVAL);
    +        }
             Ok(buffer)
         }
     
    diff --git a/vm/devices/virtio/virtiofs/tests/synthetic_root_small_buffer.rs b/vm/devices/virtio/virtiofs/tests/synthetic_root_small_buffer.rs
    new file mode 100644
    index 000000000..16587080f
    --- /dev/null
    +++ b/vm/devices/virtio/virtiofs/tests/synthetic_root_small_buffer.rs
    @@ -0,0 +1,216 @@
    +// Copyright (c) Microsoft Corporation.
    +// Licensed under the MIT License.
    +
    +//! Tests that `read_dir` and `read_dir_plus` on an aggregate's synthetic root
    +//! reject buffers too small to fit the next directory entry with `EINVAL`
    +//! rather than returning an empty reply that signals end-of-directory.
    +
    +use fuse::Fuse;
    +use fuse::protocol::FUSE_LOOKUP;
    +use fuse::protocol::FUSE_ROOT_ID;
    +use fuse::protocol::fuse_dirent;
    +use fuse::protocol::fuse_dirent_align;
    +use fuse::protocol::fuse_direntplus;
    +use fuse::protocol::fuse_in_header;
    +use fuse::protocol::fuse_read_in;
    +use std::path::Path;
    +use std::path::PathBuf;
    +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::FromBytes;
    +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))
    +}
    +
    +fn decode_plain(reply: &[u8]) -> Vec<(String, u64)> {
    +    let mut entries = Vec::new();
    +    let mut reply = reply;
    +    while !reply.is_empty() {
    +        let (entry, _) = fuse_dirent::read_from_prefix(reply).unwrap();
    +        let name_start = size_of::<fuse_dirent>();
    +        let name_end = name_start + entry.namelen as usize;
    +        let name = String::from_utf8(reply[name_start..name_end].to_vec()).unwrap();
    +        entries.push((name, entry.off));
    +        reply = &reply[fuse_dirent_align(name_end)..];
    +    }
    +    entries
    +}
    +
    +fn microvm(root: &Path) -> VirtioFs {
    +    let profile = MicroVmVirtioFsProfile::from_attachment_with_policy(
    +        MICROVM_ATTACHMENT_ID.to_owned(),
    +        microvm_root_identity(root).unwrap(),
    +        false,
    +        Vec::new(),
    +        Vec::new(),
    +        Vec::new(),
    +    )
    +    .unwrap();
    +    VirtioFs::new_microvm(root, profile).unwrap()
    +}
    +
    +struct Aggregate {
    +    _directory: TempDir,
    +    work: PathBuf,
    +    fs: VirtioFs,
    +}
    +
    +impl Aggregate {
    +    fn new() -> Self {
    +        let directory = tempdir().unwrap();
    +        let work = directory.path().join("work");
    +        std::fs::create_dir(&work).unwrap();
    +        std::fs::write(work.join("file"), b"host").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,
    +            work,
    +            fs,
    +        }
    +    }
    +}
    +
    +/// A buffer size that fits a `fuse_dirent` header but not the aligned `.`
    +/// entry (32 bytes) is refused with `EINVAL`, matching share directories,
    +/// because an empty reply signals EOF to FUSE clients.
    +#[test]
    +fn synthetic_root_page_that_fits_no_entry_is_refused() {
    +    let aggregate = Aggregate::new();
    +    let fs = &aggregate.fs;
    +    let fh = open_dir(fs, FUSE_ROOT_ID);
    +    let size = (fuse_dirent_align(size_of::<fuse_dirent>() + 1) - 1) as u32;
    +
    +    let share = microvm(&aggregate.work);
    +    let share_root = open_dir(&share, FUSE_ROOT_ID);
    +    assert_eq!(
    +        read_dir(&share, FUSE_ROOT_ID, share_root, 0, size).unwrap_err(),
    +        lx::Error::EINVAL
    +    );
    +
    +    match read_dir(fs, FUSE_ROOT_ID, fh, 0, size) {
    +        Ok(page) => panic!(
    +            "read_dir of the synthetic root with size {size} returned a page of {} \
    +             bytes, which ends the guest's listing; expected EINVAL",
    +            page.len()
    +        ),
    +        Err(error) => assert_eq!(error, lx::Error::EINVAL),
    +    }
    +}
    +
    +/// After reading `.` and `..`, a continuation buffer too small to fit the next
    +/// child entry is refused with `EINVAL` rather than prematurely ending the
    +/// listing before the shares.
    +#[test]
    +fn synthetic_root_second_page_that_fits_no_entry_is_refused() {
    +    let aggregate = Aggregate::new();
    +    let fs = &aggregate.fs;
    +    let fh = open_dir(fs, FUSE_ROOT_ID);
    +
    +    let first = decode_plain(&read_dir(fs, FUSE_ROOT_ID, fh, 0, 64).unwrap());
    +    let names: Vec<_> = first.iter().map(|(name, _)| name.as_str()).collect();
    +    assert_eq!(names, [".", ".."]);
    +    let next = first.last().unwrap().1;
    +    assert_eq!(next, 2);
    +
    +    match read_dir(fs, FUSE_ROOT_ID, fh, next, 31) {
    +        Ok(page) => panic!(
    +            "the synthetic root's page at cookie {next} with size 31 returned {} bytes, \
    +             which ends the guest's listing before the shares; expected EINVAL",
    +            page.len()
    +        ),
    +        Err(error) => assert_eq!(error, lx::Error::EINVAL),
    +    }
    +
    +    let rest = decode_plain(&read_dir(fs, FUSE_ROOT_ID, fh, next, 4096).unwrap());
    +    let names: Vec<_> = rest.iter().map(|(name, _)| name.as_str()).collect();
    +    assert_eq!(names, ["work", "seed"]);
    +}
    +
    +/// A `read_dir_plus` buffer one byte short of the `fuse_direntplus` entry for
    +/// `.` is likewise refused with `EINVAL`.
    +#[test]
    +fn synthetic_root_plus_page_that_fits_no_entry_is_refused() {
    +    let aggregate = Aggregate::new();
    +    let fs = &aggregate.fs;
    +    let fh = open_dir(fs, FUSE_ROOT_ID);
    +    let size = (fuse_dirent_align(size_of::<fuse_direntplus>() + 1) - 1) as u32;
    +
    +    match read_dir_plus(fs, FUSE_ROOT_ID, fh, 0, size) {
    +        Ok(page) => panic!(
    +            "read_dir_plus of the synthetic root with size {size} returned a page of {} \
    +             bytes, which ends the guest's listing; expected EINVAL",
    +            page.len()
    +        ),
    +        Err(error) => assert_eq!(error, lx::Error::EINVAL),
    +    }
    +}
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