openvmm: virtiofs: microVM aggregate file child accepts bare relative paths and symlinks in identity validation but fails in open_microvm_volume

Open
#1,618 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
63/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start in vm/devices/virtio/virtiofs/src/microvm/fs.rs at open_microvm_volume to see how root_path.parent() and file_name() are split before mount_options.new_volume(directory), then compare with microvm_object_identity and validate_object_path in vm/devices/virtio/virtiofs/src/microvm/profile.rs, which canonicalize first. Reproduce both cases (bare seed.txt and a symlink to a regular file) in a temp directory and run the virtiofs tests. Done means identity validation and volume opening agree on one policy for relative paths and symlinks, with a regression test covering both.

Written by the indexing model from the issue text.

Description

area: openvmm

Summary

For a microVM aggregate single-file child (MicroVmAggregateChild::new_file), microvm_file_identity and MicroVmAggregateChild::validate_root_path canonicalize root_path before checking that it names a regular file, whereas open_microvm_volume splits the uncanonicalized root_path via root_path.parent() and root_path.file_name(). Because of this mismatch, both (a) a bare relative filename such as Path::new("seed.txt") and (b) a symlink pointing to a regular file pass microvm_file_identity and validate_root_path, yet fail mid-attachment inside open_microvm_volume. Found by testing.

Root Cause

In vm/devices/virtio/virtiofs/src/microvm/profile.rs#L667-L690 (called by microvm_file_identity and via validate_root_path / validate_object_path), microvm_object_identity calls std::fs::canonicalize(path)? first, resolving relative paths against the current working directory and following symlinks to their final regular-file target. However, when VirtioFs::new_microvm_aggregate opens the child volume in open_microvm_volume, it decomposes the caller's raw root_path without normalizing or canonicalizing it:

        let (Some(directory), Some(name)) = (root_path.parent(), root_path.file_name()) else {
            anyhow::bail!(
                "microVM virtio-fs file {} has no host directory",
                root_path.display()
            );
        };
        let exposed_file = PathBuf::from(name);
...
    let volume = mount_options.new_volume(directory)?;

This creates two concrete failures for inputs accepted by microvm_file_identity and child.validate_root_path(root_path):

  1. Bare relative filenames (Path::new("seed.txt")): In Rust's std::path::Path, Path::new("seed.txt").parent() returns Some(Path::new("")) rather than None or Some(Path::new(".")). open_microvm_volume passes "" to mount_options.new_volume(""), which fails to open the host directory with ENOENT instead of opening "." (unlike directory shares, where Path::new("work") is passed directly or "./seed.txt" yields parent() == Some(Path::new("."))).
  2. Symlinks to regular files: microvm_file_identity and validate_root_path canonicalize the symlink and succeed on the target regular file, whereas open_microvm_volume opens the symlink's parent directory and sets exposed_file to the symlink's basename. VirtioFsInode::new then calls volume.lstat(&path) (O_NOFOLLOW), observing S_IFLNK instead of S_IFREG, and fails in VirtioFsVolume::ensure_object_allowed with EACCES (and validate_opened_root also requires lx::S_IFREG).

Consequence

Callers that compute microvm_file_identity and pass MicroVmAggregateChild::validate_root_path for either a bare relative filename in the working directory ("seed.txt", whereas "./seed.txt" works) or a host symlink to a regular file (whereas directory shares follow a symlink at root_path when opening the root directory) hit unexpected ENOENT or EACCES failures during VirtioFs::new_microvm_aggregate.

Reproduction

  1. Bare relative path: In a temporary working directory containing seed.txt, build a MicroVmAggregateChild::new_file("seed".to_owned(), microvm_file_identity("seed.txt")?, true) and call VirtioFs::new_microvm_aggregate(&[Path::new("seed.txt")], profile). Observe validate_root_path succeed and new_microvm_aggregate fail with ENOENT on new_volume("").
  2. Symlink path: Create a regular file real.txt and a symlink link.txt -> real.txt, compute microvm_file_identity(&link) (which succeeds), and call VirtioFs::new_microvm_aggregate(&[&link], profile). Observe validate_root_path succeed and open_microvm_volume fail with EACCES when VirtioFsInode::new lstats link.txt.

Suggested Fix

First, in open_microvm_volume, substitute Path::new(".") whenever directory.as_os_str().is_empty() so bare relative filenames open the current working directory consistently with "./file". Second, align profile validation and volume opening on a single policy for symlink root_paths on file children: either reject symlinks up front in microvm_file_identity / validate_object_path (inspecting std::fs::symlink_metadata(path) before canonicalizing) if file-child host paths must directly name regular files, or canonicalize root_path in open_microvm_volume before splitting (parent, file_name) so a symlink root_path opens the target regular file's parent directory and exposes the target's basename (matching how directory shares resolve a symlink at the share root).

Related: #1614.

Dominant language
Rust
Stars
848
Forks
63
Avg merge
13h 37m
Merged PRs (30d)
571

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/nvx

All issues in microsoft/nvx

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.