Skip to content

openvmm: virtiofs: FUSE_FSYNCDIR on aggregate synthetic root handle fails with EBADF #1624

Open

Description

@tangruize

Summary

On an aggregate virtiofs mount (microVM or direct aggregate), FUSE_OPENDIR on FUSE_ROOT_ID (1) returns the sentinel handle SYNTHETIC_ROOT_FH (u64::MAX) without inserting an entry into self.inner.files. While get_attr, get_statx, read_dir, read_dir_plus, and release_dir all recognize SYNTHETIC_ROOT_FH, VirtioFs::fsync_dir ignores request and looks up fh directly in self.get_file(fh), causing FUSE_FSYNCDIR (fsync() / fdatasync() on an open directory descriptor for the aggregate mount point) to fail with EBADF. Found by testing.

Root Cause

In vm/devices/virtio/virtiofs/src/lib.rs#L471-L503 (as well as get_attr and get_statx), open_dir hands out SYNTHETIC_ROOT_FH for the synthetic aggregate root, and directory handlers check self.is_synthetic_root_handle(request.node_id(), fh) before consulting the file-handle map. However, VirtioFs::fsync_dir omits that check and unconditionally calls self.get_file(fh):

    fn fsync_dir(&self, _request: &Request, fh: u64, flags: u32) -> lx::Result<()> {
        let file = self.get_file(fh)?;
        let data_only = flags & FUSE_FSYNC_FDATASYNC != 0;
        file.fsync_dir(data_only)
    }

Because SYNTHETIC_ROOT_FH is never inserted into self.inner.files, self.get_file(fh) logs "Request for unknown file" and returns Err(lx::Error::EBADF).

Consequence

When guest software opens an aggregate mount-point directory (open("/mnt", O_RDONLY | O_DIRECTORY)) and invokes fsync(fd) or fdatasync(fd) (as archive extractors, package managers, backup tools, and atomic file-replacement helpers routinely do on parent/mount directories), the Linux FUSE client sends FUSE_FSYNCDIR with the handle returned by FUSE_OPENDIR and receives EBADF despite holding a valid open directory descriptor.

Reproduction

  1. Construct an aggregate VirtioFs via VirtioFs::new_microvm_aggregate (or VirtioFs::new_aggregate).
  2. Call Fuse::open_dir(&fs, &request(FUSE_ROOT_ID), lx::O_RDONLY as u32) to obtain fh (SYNTHETIC_ROOT_FH).
  3. Call Fuse::fsync_dir(&fs, &request(FUSE_ROOT_ID), fh, 0).
  4. Observe Err(lx::Error::EBADF) instead of Ok(()).

Related: #1627, #1617, #1632, #1599.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    In VirtioFs::fsync_dir, inspect request.node_id() and return Ok(()) immediately when self.is_synthetic_root_handle(request.node_id(), fh) holds (matching VirtioFs::release_dir), since the synthetic aggregate root is a read-only in-memory directory with no host backing descriptor to synchronize.

    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/lib.rs b/vm/devices/virtio/virtiofs/src/lib.rs
    index 71780f585..f248bd607 100644
    --- a/vm/devices/virtio/virtiofs/src/lib.rs
    +++ b/vm/devices/virtio/virtiofs/src/lib.rs
    @@ -560,7 +560,10 @@ fn fsync(&self, _request: &Request, fh: u64, flags: u32) -> lx::Result<()> {
             file.fsync(data_only)
         }
     
    -    fn fsync_dir(&self, _request: &Request, fh: u64, flags: u32) -> lx::Result<()> {
    +    fn fsync_dir(&self, request: &Request, fh: u64, flags: u32) -> lx::Result<()> {
    +        if self.is_synthetic_root_handle(request.node_id(), fh) {
    +            return Ok(());
    +        }
             let file = self.get_file(fh)?;
             let data_only = flags & FUSE_FSYNC_FDATASYNC != 0;
             file.fsync_dir(data_only)
    diff --git a/vm/devices/virtio/virtiofs/tests/synthetic_root_fsyncdir.rs b/vm/devices/virtio/virtiofs/tests/synthetic_root_fsyncdir.rs
    new file mode 100644
    index 000000000..35617f25e
    --- /dev/null
    +++ b/vm/devices/virtio/virtiofs/tests/synthetic_root_fsyncdir.rs
    @@ -0,0 +1,105 @@
    +// Copyright (c) Microsoft Corporation.
    +// Licensed under the MIT License.
    +
    +//! Tests that `FUSE_FSYNCDIR` on an aggregate mount's synthetic root handle
    +//! succeeds rather than failing with `EBADF`, while unknown handles remain
    +//! rejected with `EBADF`.
    +
    +use fuse::Fuse;
    +use fuse::protocol::FUSE_FSYNC_FDATASYNC;
    +use fuse::protocol::FUSE_LOOKUP;
    +use fuse::protocol::FUSE_ROOT_ID;
    +use fuse::protocol::fuse_in_header;
    +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: 1,
    +        padding: 0,
    +    };
    +    fuse::Request::new(header.as_bytes()).unwrap()
    +}
    +
    +fn read_in(fh: u64, offset: u64, size: u32) -> fuse::protocol::fuse_read_in {
    +    let mut arg = fuse::protocol::fuse_read_in::new_zeroed();
    +    arg.fh = fh;
    +    arg.offset = offset;
    +    arg.size = size;
    +    arg
    +}
    +
    +/// Opening an aggregate mount's root directory returns `SYNTHETIC_ROOT_FH`,
    +/// which represents an in-memory read-only directory with no host backing
    +/// handle. Calling `fsync_dir` (and `fdatasync`) on that handle must succeed,
    +/// whereas an unallocated handle must still return `EBADF`.
    +#[test]
    +fn fsyncdir_of_the_synthetic_root_handle_is_not_ebadf() {
    +    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();
    +    let fh = Fuse::open_dir(&fs, &request(FUSE_ROOT_ID), lx::O_RDONLY as u32)
    +        .unwrap()
    +        .fh;
    +
    +    let attr = Fuse::get_attr(&fs, &request(FUSE_ROOT_ID), 1, fh).unwrap();
    +    assert_eq!(attr.attr.mode, lx::S_IFDIR | 0o500);
    +    assert!(
    +        !Fuse::read_dir(&fs, &request(FUSE_ROOT_ID), &read_in(fh, 0, 4096))
    +            .unwrap()
    +            .is_empty()
    +    );
    +
    +    match Fuse::fsync_dir(&fs, &request(FUSE_ROOT_ID), fh, 0) {
    +        Ok(()) => {}
    +        Err(error) => panic!(
    +            "fsync_dir of the synthetic root handle {fh:#x} failed with {error:?}; \
    +             expected Ok(())"
    +        ),
    +    }
    +    assert_eq!(
    +        Fuse::fsync_dir(&fs, &request(FUSE_ROOT_ID), fh, FUSE_FSYNC_FDATASYNC),
    +        Ok(())
    +    );
    +    assert_eq!(
    +        Fuse::fsync_dir(&fs, &request(FUSE_ROOT_ID), 12345, 0),
    +        Err(lx::Error::EBADF)
    +    );
    +}
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