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

Aperta
#1,618 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
63/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Rust
Stelle
848
Fork
63
Merge medio
12h 32m
PR unite (30g)
602

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/nvx

Tutte le issue di microsoft/nvx

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.