openvmm: virtiofs: microVM readdirplus continues listing when child entry aliases open directory node

Open
#1,634 1 comment 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
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start at vm/devices/virtio/virtiofs/src/lib.rs: Fuse::read_dir and read_dir_plus call file.read_dir without request.node_id(). Then read microvm/file.rs::read_dir_microvm and its lookup_helper loop, and trace InodeMap::insert plus VirtioFsInode::lookup in inode.rs for the path overwrite. Reproduce with the issue's dir/sub/after snapshot; done when a child aliasing the open directory is rejected and later names resolve under the original path.

Written by the indexing model from the issue text.

Description

area: openvmm

Summary

In microVM mode, VirtioFsFile::read_dir_microvm resolves each FUSE_READDIRPLUS entry by calling fs.lookup_helper(&self.inode, name) without checking whether the returned fuse_entry.nodeid equals the FUSE node ID of the open directory being enumerated (request.node_id()). If a child directory entry deduplicates onto self.inode's own node ID, InodeMap::insert rewrites self.inode.path from "dir" to "dir/child" in place, read_dir_microvm emits a self-referential child nodeid to the guest, and subsequent loop iterations resolve remaining snapshot names inside "dir/child" instead of "dir". Found by formal verification.

Root Cause

In vm/devices/virtio/virtiofs/src/lib.rs, Fuse::read_dir and Fuse::read_dir_plus do not pass request.node_id() to file.read_dir, and VirtioFsFile::read_dir_microvm accepts any fuse_entry returned by fs.lookup_helper(&self.inode, name) whose attr.ino matches the snapshot:

    fn read_dir_plus(&self, request: &Request, arg: &fuse_read_in) -> lx::Result<Vec<u8>> {
        if self.is_synthetic_root_handle(request.node_id(), arg.fh) {
            return self.read_synthetic_root_dir(arg.offset, arg.size, true);
        }
        let file = self.get_file(arg.fh)?;
        file.read_dir(self, arg.offset, arg.size, true)
    }
                    let fuse_entry = fs.lookup_helper(&self.inode, name)?;
                    if fuse_entry.attr.ino != entry.guest_inode_id {
                        return Err(lx::Error::EIO);
                    }
                    if !buffer.dir_entry_plus(name, entry.next_cookie, fuse_entry) {
                        return Err(lx::Error::EIO);
                    }

When lookup_helper resolves a child directory whose DedupKey matches self.inode (same file type S_IFDIR), InodeMap::insert takes the Entry::Occupied branch and calls VirtioFsInode::lookup, overwriting *self.inode.path.write() = new_path ("dir/child") and returning fuse_entry.nodeid == request.node_id(). Even the existing fuse_entry.attr.ino != entry.guest_inode_id check does not reject the entry when the snapshot already recorded that inode number.

Consequence

On local Linux filesystems (ext4, xfs, btrfs, tmpfs), an open directory file descriptor keeps its (st_dev, st_ino) allocated on the host until closed, preventing immediate (st_dev, st_ino) reuse by a newly created directory on the same device. However, synthetic, network, or FUSE-backed host shares that reuse or synthesize inode numbers, unlinked/retyped directory cases where the backing filesystem reuses (st_dev, st_ino), or nested subvolumes when DedupKey::Ino omits st_dev (for example a btrfs subvolume directory containing a nested subvolume, both having st_ino == 256) can hit this path. When triggered, read_dir_microvm returns a fuse_direntplus entry whose child nodeid equals its parent directory's nodeid (forming a directory cycle in the guest kernel's dcache) and continues iterating the snapshot while &self.inode now points at "dir/child", resolving later entries from the child directory rather than the open directory.

Reproduction

  1. Create a microVM VirtioFs share containing "dir/sub/after".
  2. Insert a directory VirtioFsInode at path "dir" with dir_node_id whose DedupKey matches "dir/sub" (modeling (dev, ino) reuse or collision), open it as a directory VirtioFsFile, and populate its directory snapshot with [".", "..", "sub", "after"].
  3. Call file.read_dir(&fs, dir_node_id, 0, 4096, true).
  4. Without the guard, lookup_helper on "sub" deduplicates onto dir_node_id, rewrites dir_inode's path from "dir" to "dir/sub", emits "sub" with nodeid == dir_node_id, and then resolves "after" against "dir/sub/after" even though "dir/after" does not exist.

Related: #1611, #1605, #1599.

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.