openvmm: virtiofs: renaming one hard link onto another hard link of the same file drops the source alias

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

Research direction

Start in vm/devices/virtio/virtiofs/src/lib.rs at VirtioFs::rename (around lines 532-546) and read how preflight_rename_aliases, remove_alias_prefix and rename_alias_prefix sequence with the host rename; then read the alias bookkeeping in vm/devices/virtio/virtiofs/src/inode.rs (lines 508-542) and the same assumption in vm/devices/virtio/virtiofs/src/microvm/limits.rs preflight_microvm_rename. Reproduce with the FUSE sequence in the issue (lookup f, link a, rename a->f with flags 0, unlink f, getattr) and confirm the node keeps aliases = {"a","f"}. Done means the same-inode rename returns Ok(()) without touching aliases while inode.rename still yields host-side EEXIST/ENOENT/permission errors, plus a regression test covering the follow-up unlink and save_microvm_state path.

Written by the indexing model from the issue text.

Description

area: openvmm

Summary

Under POSIX rename(2), renaming old_path (a) onto new_path (f) when both are distinct directory entries referring to the same existing file (hard links to the same inode) returns 0 and performs no action on the host filesystem, leaving both a and f intact on disk. VirtioFs::rename, however, unconditionally runs inodes.remove_alias_prefix(inode.volume_id(), &new_path) followed by inodes.rename_alias_prefix(inode.volume_id(), &old_path, &new_path), collapsing {a, f} into {f} and dropping a from the node's tracked aliases even though a still exists on the host. Found by testing.

Root Cause

In VirtioFs::rename, non-exchange renames always assume the host removed new_path and moved old_path onto new_path:

let exchange = flags & RENAME_EXCHANGE != 0;
self.preflight_rename_aliases(inode.volume_id(), &old_path, &new_path, exchange)?;
inode.rename(name, &new_inode, new_name, flags)?;
let mut inodes = self.inner.inodes.write();
if exchange {
    inodes.exchange_alias_prefixes(inode.volume_id(), &old_path, &new_path);
} else {
    inodes.remove_alias_prefix(inode.volume_id(), &new_path);
    inodes.rename_alias_prefix(inode.volume_id(), &old_path, &new_path);
}

When old_path ("a") and new_path ("f") are distinct hard links to the same host inode and flags has neither RENAME_EXCHANGE nor RENAME_NOREPLACE, the host renameat/renameat2 call returns 0 as a POSIX no-op with both directory entries preserved. Next, VirtioFsInode::remove_alias_prefix and VirtioFsInode::rename_alias_prefix remove "f" from self.aliases and rewrite "a" to "f" (also rewriting self.path to "f"), leaving the VirtioFsInode with aliases = {"f"} and path = "f" while "a" is forgotten. The same assumption appears in InodeMap::preflight_microvm_rename.

Consequence

  • Spurious ENOENT on a still-existing file: If the guest subsequently unlinks "f" (FUSE_UNLINK), remove_alias_prefix removes "f" and leaves aliases empty and self.path stuck at deleted "f", instead of falling back to the surviving hard link "a". Subsequent path-based requests on the held node (get_attr, get_statx, open, set_attr without FATTR_FH, xattr calls, and link) fail with ENOENT even though the file still exists on disk at "a".
  • MicroVM save failure after unlinking "f": Because aliases becomes empty after unlink("f") despite "a" remaining on disk, VirtioFs::save_microvm_state fails with "inode has no bounded reopenable aliases".
  • Incomplete alias set for policy and limit tracking: Dropping "a" from aliases removes "a" from VirtioFsInode::policy_permits_writes multi-link writability checks and undercounts tracked aliases against MAX_ALIASES and MAX_ALIAS_BYTES.

Reproduction

  1. Create a file f in a share root and attach VirtioFs.
  2. Issue FUSE_LOOKUP(FUSE_ROOT_ID, "f") to obtain node.
  3. Issue FUSE_LINK(FUSE_ROOT_ID, "a", node) so both a and f are hard links to the same host inode (node tracks aliases = {"a", "f"}).
  4. Issue FUSE_RENAME(FUSE_ROOT_ID, "a", FUSE_ROOT_ID, "f", 0): the host keeps both a and f on disk with the original inode number, but node drops "a" and keeps only aliases = {"f"}.
  5. Issue FUSE_UNLINK(FUSE_ROOT_ID, "f"): f is removed while a remains intact on disk.
  6. Issue FUSE_GETATTR or FUSE_OPEN on node (which the guest never forgot): it fails with ENOENT because node's primary path still points to "f" and "a" was lost from aliases.

Suggested Fix

For non-exchange renames (flags & RENAME_EXCHANGE == 0), check before updating InodeMap whether old_path == new_path or whether lstat on old_path and new_path before inode.rename succeeds and identifies the same host object ((stat.device_nr, stat.inode_nr) on volumes with stable file IDs). Still invoke inode.rename(name, &new_inode, new_name, flags)? so the host kernel enforces RENAME_NOREPLACE (EEXIST), missing-entry (ENOENT), and type/permission checks, and when the two paths named the same host object, return Ok(()) immediately without running preflight_rename_aliases, remove_alias_prefix, or rename_alias_prefix so all existing aliases remain tracked.

Related: #1610, #1602, #1623.

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.