Skip to content

[DO NOT REVIEW] virtiofs: keep node names when renaming one hard link onto another - #5215

Merged
Pedro Henrique Penna (ppenna) merged 5 commits into
feature/microvm/devfrom
copilot/nvx-issue-1620-rename-hardlinks
Nov 22, 2026
Merged

Pedro Henrique Penna (ppenna) merged 5 commits into
feature/microvm/devfrom
copilot/nvx-issue-1620-rename-hardlinks

Conversation

@ppenna

Copy link
Copy Markdown
Contributor

Refs microsoft/nvx#1620.

Stacked on #5214 (microsoft/nvx#1600), itself stacked on the other open virtio-fs fixes; only the top commit, 2a483a06f, belongs to this PR. NVX pins the head of this branch, so this is a prerequisite of the NVX change that pins it, and it lands by fast-forwarding feature/microvm/dev to that exact commit, not with the merge button, which squashes.

What and why

Each virtio-fs node keeps the volume-relative names, or aliases, through which the guest has reached its object, and hard links share one node, since InodeMap deduplicates nodes by host device and inode number. VirtioFs::rename tracks a rename that is not an exchange as a move: after the host rename, InodeMap::remove_alias_prefix(new_path) drops the names at and below the destination, which the host replaced, and InodeMap::rename_alias_prefix(old_path, new_path) then rewrites those at and below the source under the destination.

When the guest renames a hard link onto another link of the same object, such as a onto f, a Linux host succeeds without changing anything, as POSIX requires: renameat2 returns 0 and keeps both names (or fails with EEXIST under RENAME_NOREPLACE). The bookkeeping still dropped f and then rewrote a to f, so the node kept only f, although a still reached its object. Once the guest removed f, the node had no names left, so its path-based requests failed with ENOENT, and save_microvm_state, and so a microVM snapshot, failed with inode has no bounded reopenable aliases.

A Linux guest normally completes such a rename itself, since both of its names reach one node (vfs_rename returns 0 when source == target); it sends FUSE_RENAME only when its dentries lead to different nodes, such as after the host changed a name behind it, and a guest can send the request directly. OpenVMM does not trust the guest, so its state must stay consistent for any request; the regression test drives the FUSE server directly.

The fix: after a successful rename on a POSIX host, VirtioFs::rename checks whether the source still reaches the destination's object, with a new VirtioFsVolume::names_one_object that compares the host device and inode numbers of two names on a volume with stable file IDs, and then changes no names.

  • It decides from the host's result, rather than from the two names before the rename as the issue suggests: through overlayfs, two hard links in a lower layer report one device and inode number, yet the rename moves one onto the other. On an Azure Linux 3 host (kernel 6.18), both names reported dev=49 ino=966744 before the rename, and only f remained after it, as ino=966745; a check before the rename would have kept the vanished a.
  • Windows hosts keep the existing behavior: NTFS removes the source link instead (seen through this code path), which the move's bookkeeping already records, and a rename that changes only the case of a name renames the entry while the old spelling still reaches the object in a case-insensitive directory, so the check would keep the old spelling there.
  • The move's alias-limit preflight still runs before such a rename. The names that the rename keeps are the table as it was, which already fits the limits, so the preflight can only be stricter than needed.
  • The early return of [DO NOT REVIEW] virtiofs: keep node names when renaming a name onto itself #5206 for a rename of a name onto itself, and the microVM rename-flag check below this PR, are unchanged.

No saved state, protocol, ABI, or format, and no version of any of them, changes.

Tests

One unit test in virtiofs, microvm::exchange_tests::renaming_a_hard_link_onto_another_link_keeps_the_remaining_names, next to the test of a rename of a name onto itself, whose module doc now covers it. On a read-write microVM share holding directory/first and its hard links second and third, it checks that the host fails second onto third under RENAME_NOREPLACE (EEXIST on Linux, EINVAL on Windows, which rejects every flag), then renames second onto third, in one directory, and directory/first onto third, across two, and checks that:

  • the host keeps all three names on Linux, and only third on Windows;
  • the node keeps exactly the names that remain, with its path among them;
  • a snapshot taken right away records those names and restores them;
  • once the guest removes all of them but one, the node still reads the object through that one: on Linux, through the source directory/first.

Validation

cargo nextest run --profile agent -p virtiofs at this head, 2a483a06f, and at it with lib.rs and inode.rs reverted to the parent, 1270a975b:

Host Without the fix With it
Windows 11, local, NTFS passes: the host removes the source link, which the names already follow 199 passed
nvx-kvm-test, Azure Linux 3.0 VM, kernel 6.18 fails: the node keeps only the destination (aliases of node 3, left: {"third"}, right: {"directory/first", "second", "third"}) 205 passed

At this head, on both hosts, cargo check, cargo clippy --all-targets, and cargo doc --no-deps for virtiofs are clean, with no warning but the LNK4199 linker message that every linked artifact gets on the Windows machine, and one ignored test is skipped, as at the base. cargo xtask fmt --fix changes nothing. The same results held when the commit was first written on feature/microvm/dev b51f9156c with #5206.

On nvx-kvm-test, NVX's microVM filesystem scenarios platform-filesystem, filesystem-snapshot, filesystem-shares, and filesystem-policy pass on KVM, with an Alpine guest and one processor, against OpenVMM built at this head. They rename, exchange, and link files through the share, but a Linux guest completes a rename between two links of one node itself, so they cannot reach the new path.

Each entry of a FUSE_READDIRPLUS reply other than . and .. hands the
guest a lookup reference, which the guest releases with FUSE_FORGET
only for an entry that it receives. A microVM READDIRPLUS that failed
partway through its directory snapshot returned the error alone and
discarded the entries that it had already looked up, so their
references leaked (microsoft/nvx#1611): the nodes stayed in the inode
map, with their names, until FUSE_DESTROY, counted against the share's
limits of 4,096 nodes, 16,384 names, and 1 MiB of names until lookups
and creates failed with ENOSPC or E2BIG, and went into every snapshot.
A listing fails partway at a listed name that no longer reaches the
listed object, as after a rename over it, at a name whose lookup fails,
as after an unlink, and at a name whose node the limits refuse, as once
the guest holds 4,096 nodes.

read_dir_microvm now follows the rule that read_dir_with and the
aggregate root already follow: after an error, it returns the entries
read so far, and reports the error only if there are none, so that the
guest's next read, which starts at the failing entry, gets it.
write_microvm_entry_plus looks up and writes one entry. Should the
write fail after the lookup, which the size check before the lookup
rules out, it releases the entry's reference, so that a failed entry
holds none.

Tests, in virtiofs' microVM filesystem tests: with room for one more
node, a READDIRPLUS of two files returns the one that it looked up, the
next read fails with ENOSPC, and once the guest forgets that file, the
listing goes on with the other; and a READDIRPLUS of three files whose
last listed one a new file replaced keeps no reference that the guest
did not receive, so that once the guest forgets every entry that it
received, only the root's node remains. Without the fix, at
feature/microvm/dev b51f915 on Windows and on Linux (Azure Linux 3.0,
nvx-kvm-test, with shares on tmpfs and ext4), the first fails with
ENOSPC on its first read and the second leaves three nodes; with it,
both and the package's other tests pass there.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A request through an open handle, such as FUSE_WRITE, FUSE_READ,
FUSE_READDIRPLUS, or FUSE_SETATTR with FATTR_FH, names both the handle
and the node that it concerns, but VirtioFs::get_file looked the handle
up alone (microsoft/nvx#1599). A guest could therefore use a handle with
any other node's ID, and keep using it after FUSE_FORGET dropped the
handle's node from the inode table. The table updates the paths and
aliases of the nodes that it holds when the guest renames or removes a
name, and they decide what a request through a handle may do: whether
it may write, which check_writable decides from every name that the
guest has used for the object, and where READDIRPLUS looks up the
entries that it lists. The inode of a forgotten node, which only its
handles keep, learns no name again. So a guest that opened a file
through a writable host hard link, forgot the node, and looked the file
up again at its read-only name, which gives it a new node, still wrote
and truncated the read-only file through the handle, with either node's
ID; and READDIRPLUS through the handle of a forgotten directory that the
guest had moved looked its entries up at the directory's former path.

get_file now takes the request's node ID, which the nine requests
through a handle pass: GETATTR and STATX with FUSE_GETATTR_FH, SETATTR
with FATTR_FH, READ, WRITE, READDIR, READDIRPLUS, FSYNC, and FSYNCDIR.
The node must be in the inode table, or the request fails with EINVAL,
as other requests about an unknown node do, and the handle must have
been opened on that node, or the request fails with EBADF, as
virtiofsd's passthrough server answers. Unlike get_inode, get_file does
not revalidate the node's paths, so a handle still reaches its file
once the file is unlinked, as POSIX requires, and reads and writes walk
no path. RELEASE and RELEASEDIR still accept the handle of a forgotten
node: closing a handle uses neither the node nor its names, and
refusing it would leak the handle. A Linux guest sends none of the
refused requests: it names each handle's own node, and it forgets a
node only once the RELEASE of the node's last handle has completed,
since the RELEASE holds the inode, whose eviction sends FUSE_FORGET.

a_handle_serves_only_its_node_while_the_guest_holds_it sends the nine
requests through a file's and a directory's handle on a microVM share:
with the handles' own nodes, even once the file is unlinked, each
succeeds; with another node, each fails with EBADF; once the guest
forgets the handles' nodes, each fails with EINVAL; and both handles
can still be released.
a_handle_of_a_forgotten_node_cannot_write_a_read_only_file opens a
read-only file through a host hard link in a writable path, forgets the
node, looks the file up at its read-only name, and checks that the
handle can neither write nor truncate the file with either node's ID.
Without the fix, at feature/microvm/dev b51f915 on Windows and on
Linux (Azure Linux 3.0, nvx-kvm-test), every one of the nine requests
succeeds with another node and after FUSE_FORGET, and the handle
overwrites the read-only file; with it, both tests and the package's
other tests pass there. The tests that read a directory through
get_file now pass the handle's node.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A microVM guest could make device nodes in a writable share
(microsoft/nvx#1612). On a Linux host, VirtioFs::mknod passed the
guest's file type and device number to mknodat, and VirtioFs::rename
passed the guest's flags to renameat2. Since Linux 5.8, a caller
without CAP_MKNOD may make a whiteout, character device 0:0, with
mknodat(S_IFCHR, 0) or with renameat2(RENAME_WHITEOUT), which leaves
one at the old name. Even an unprivileged OpenVMM therefore made one
whenever the guest asked, which host overlay mounts and tools take for
a deleted file, and an OpenVMM with CAP_MKNOD made any character or
block device that the guest asked for, which reaches a host device
wherever the share's filesystem is not mounted nodev. A whiteout rename
also left an untracked node at the old name while HostFs moved that
name's aliases to the new one. Windows hosts already refused both, as a
microVM volume there makes only regular files and rejects every rename
flag.

A microVM share now checks both requests after its access policy, and
before its resource limits and the host:

- mknod makes only regular files, which a type of 0 also makes, FIFOs,
  and sockets. A character or block device fails with EPERM, as Linux
  fails a device that it denies, and any other type fails as mknod(2)
  fails it, a directory with EPERM and the rest with EINVAL, so that
  every host reports the same errors;
- a rename passes the host no flag, RENAME_NOREPLACE, or
  RENAME_EXCHANGE, and fails with EINVAL, as Linux fails a flag that a
  filesystem does not support, for RENAME_WHITEOUT, any other flag, or
  those two together.

The standard machine's virtio-fs still passes both to the host, and no
saved state changes. The Guide's virtio-fs page lists the refused
device nodes and rename flags, and its security model says why.

Tests, in virtiofs' new microVM device node tests: mknod of character
devices 0:0 and 1:3 and of block device 7:0 fails with EPERM, at a new
name and at an existing one, whose EEXIST the host never reports, a
directory with EPERM, and a symbolic link or an invalid type with
EINVAL, leaving the share unchanged, while a regular file, and on Linux
a FIFO, a socket, and a node of type 0, are still made; a rename with
RENAME_WHITEOUT, alone or with RENAME_NOREPLACE, with RENAME_NOREPLACE
and RENAME_EXCHANGE together, or with an unknown flag fails with EINVAL
and changes neither name, even a rename of a name onto itself, while
RENAME_NOREPLACE still reaches a Linux host. Without the fix, at
feature/microvm/dev b51f915, on Linux (Azure Linux 3.0, kernel 6.18,
unprivileged, on tmpfs; nvx-kvm-test), the first test fails where
mknod makes character device 0:0 and the second where the
RENAME_WHITEOUT rename succeeds; on Windows, only the directory's
EINVAL fails. With it, both pass, with the package's other tests, on
Windows and on Linux.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
with_fs_identity must run its operation without supplementary groups
other than the GID of its identity, and fail with EPERM when it cannot
drop them. In a user namespace that leaves some of the thread's groups
unmapped, getgroups reports each of them as the kernel's overflow GID
(/proc/sys/kernel/overflowgid, 65534 by default). When the target GID
was itself the overflow GID, as for a guest caller in the nogroup group
under --mount-owner caller, SwitchedCredentials::switch took those
groups for the target GID, skipped setgroups, and ran the operation
with the thread's original host groups, which it could use to read,
write, or keep S_ISGID on files of those groups (microsoft/nvx#1600).

A supplementary group that reads as the overflow GID now counts as
another group unless the thread's user namespace maps every group ID,
as the initial namespace does: a namespace maps only IDs that its
parent maps, so such a namespace leaves no group unmapped.
/proc/self/gid_map shows whether it does; a map that cannot be read
counts as partial, and an overflow GID that cannot be read may be any
GID. In a namespace with a partial map, a switch to the overflow GID
therefore drops the supplementary groups too, or fails with EPERM
before running the operation where it cannot, as when the namespace
denies setgroups. That includes a thread whose only group is the one
that the namespace maps as the overflow GID, since getgroups cannot
tell it apart from an unmapped one. Where the namespace maps every
group, the comparison stays exact, so an unprivileged thread whose only
group is the overflow GID still needs no privilege there.

overflow_gid_groups_fail_closed_without_privilege runs itself again in
a user namespace that maps only the overflow GID, to the test's GID,
and denies setgroups, and switches to that GID there. At b51f915, on
a Linux host (nvx-kvm-test, Azure Linux 3.0, kernel 6.18), the switch
returned Ok and chmod kept S_ISGID on a file of another of the test's
groups; with the fix, it fails with EPERM. Another test checks which
ID maps map every ID. The other lxutil tests pass there and on Windows,
and so do virtiofs' tests on that host.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A guest rename of a hard link onto another link of the same object
succeeds on a Linux host without changing anything, as POSIX requires,
but HostFs dropped the source name from the object's node
(microsoft/nvx#1620). Once the guest removed the other link, the node
had no names left although the source still reached its object, so
requests on the node failed with ENOENT, and a microVM save failed
with "inode has no bounded reopenable aliases".

VirtioFs::rename tracks a rename that is not an exchange as a move: it
drops the names at and below the destination, which the host replaced,
and then rewrites those at and below the source under the destination.
When both are links of one object, renameat2 replaces and moves
nothing, but the node keeps only the destination's name.

After a successful rename on a POSIX host, HostFs now checks whether
the source still reaches the destination's object, and then changes no
names. It decides from the host's result, not from the two names
before the rename: through overlayfs, two hard links in a lower layer
report one inode number, but the rename still moves one onto the
other. A Windows host does move the name: NTFS removes the source
link, which the move's bookkeeping already records, and a rename that
changes only the case of a name leaves the old spelling reaching the
object, so the check stays off there. The rename still passes the
move's alias-limit preflight, and the names that it keeps already fit
the limits.

renaming_a_hard_link_onto_another_link_keeps_the_remaining_names, in
virtiofs' microVM rename tests, renames a hard link onto another link
of its object in one directory and across two, after checking that the
host still fails such a rename under RENAME_NOREPLACE, with EEXIST on
Linux and EINVAL on Windows. On Linux, the node keeps all three names,
a snapshot taken right away saves and restores them, and the node
still reads the object through the source once the guest removes the
other links. On Windows, the host keeps only the destination, and so
does the node. Without the fix, on top of the fix for
microsoft/nvx#1610 at feature/microvm/dev b51f915, it fails on Linux
(nvx-kvm-test, Azure Linux 3, kernel 6.18), where the node keeps only
the destination, and passes on Windows; with it, it and the package's
other tests pass on both.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added Guide release-nvx Targets a branch under feature/microvm/. unsafe Related to unsafe code labels Nov 22, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 22, 2026
OpenVMM's virtio-fs server records the names through which the guest
reaches each node, and hard links share one node. It tracked every
rename that is not an exchange as a move: it dropped the names at and
below the destination, then rewrote those at and below the source
under it (#1620). A Linux host renames nothing when the
source is another hard link of the destination's object, as POSIX
requires, so the node kept only the destination's name, although the
source still reached its object. Once the guest removed the
destination, the node had no names left, so its requests failed with
ENOENT, and saving the microVM failed with "inode has no bounded
reopenable aliases".

Pin microsoft/openvmm 2a483a06f31fcea8736efa59e2e9c600a0529d52, the
head of microsoft/openvmm#5215, one commit on 1270a975b, the head of
microsoft/openvmm#5214:

- virtiofs: keep node names when renaming one hard link onto another.
  After a successful rename on a POSIX host, VirtioFs::rename changes
  no names when the source still reaches the destination's object, as
  their host device and inode numbers show. A Windows host removes the
  source link instead, which the names already followed. Its test
  fails without the change on Linux (nvx-kvm-test) and passes on
  Windows, and with it, NVX's microVM filesystem scenarios pass on
  KVM.

No saved state, protocol, ABI, or format, and no version of any of
them, changes. The protocol vectors and CPU profiles that nvx copies
are unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 22, 2026
Map position 349, the commit of microsoft/openvmm#5215, which NVX now
pins at 2a483a06f, 223 commits past the mapped tip:

- 349, virtiofs: keep node names when renaming one hard link onto
  another. After a successful rename on a POSIX host,
  VirtioFs::rename changes no names when the source still reaches the
  destination's object, as VirtioFsVolume::names_one_object tells from
  the two names' host device and inode numbers. It tracked every
  rename that is not an exchange as a move, so a rename between two
  hard links of one object, which a Linux host carries out without
  changing anything, left the node only the destination's name, after
  which removing the destination left it none and saving a microVM
  failed. A Windows host removes the source link, as the move records.

Position 349 is a PR13 fixup. It fixes the name bookkeeping that
PR13's position 048 adds, and its test, after position 340's in the
microVM exchange tests that position 166 adds, needs nothing that a
later position adds but position 340's RENAME_NOREPLACE, so it folds
into position 048 after position 340, and its test into position 166,
as the fold sentence after the DAG and the commit mapping say; the
Scope, the DAG, the dependency graph, the PR summary, and the commit
mapping record it with PR13. The alias preflight of PR18's position
081 still models the move, which can only be stricter than the names
that the rename keeps, so it is no PR18 fixup. Upstream main,
bf353460f, lacks the defect, since it lacks the names of position 048,
and replaying the commit there conflicts in lib.rs and inode.rs, next
to the code of positions 048, 160, and 332, and in the microVM's
exchange_tests.rs, which upstream main lacks, so it follows PR13
upstream.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ppenna
Pedro Henrique Penna (ppenna) merged commit 2a483a0 into feature/microvm/dev Nov 22, 2026
102 of 105 checks passed
syeedali pushed a commit to syeedali/nvx that referenced this pull request Nov 23, 2026
OpenVMM's virtio-fs server answered a FUSE_READDIR or FUSE_READDIRPLUS
of an aggregate's synthetic root whose buffer could not hold the next
entry with an empty reply, which the guest takes for the end of the
directory (microsoft#1617). A read from the start with a buffer
too small for `.` therefore listed an empty root, and a listing whose
buffer held the entries before a child with a longer name ended before
that child, without an error. The reads of every other directory fail
with EINVAL, as getdents64 does.

Pin microsoft/openvmm 189dc519ffcf30c5b9cfea4805e5b917c1556b1b, one
commit on 2a483a06f, the head of microsoft/openvmm#5215:

- virtiofs: fail a synthetic root read too small for its next entry.
  The read fails with EINVAL when the first entry that it reaches does
  not fit. A read that fits some entries still returns them, and a
  read at the end of the root still returns an empty reply. Its test
  fails without the change on Windows and on Linux (nvx-mshv-test).

NVX's Linux guest reads directories with buffers of at least a page,
which holds any entry, so the microVM behaves as it did. No saved
state, protocol, ABI, or format, and no version of any of them,
changes. The protocol vectors and CPU profiles that nvx copies are
unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Guide release-nvx Targets a branch under feature/microvm/. unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant