Skip to content

[DO NOT REVIEW] virtiofs, lxutil: refuse writes through handles not open for writing - #5226

Merged
Pedro Henrique Penna (ppenna) merged 2 commits into
feature/microvm/devfrom
copilot/nvx-issue-1609-rdonly-trunc
Nov 22, 2026
Merged

Pedro Henrique Penna (ppenna) merged 2 commits into
feature/microvm/devfrom
copilot/nvx-issue-1609-rdonly-trunc

Conversation

@ppenna

@ppenna Pedro Henrique Penna (ppenna) commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Refs microsoft/nvx#1609.

Its two commits, a1cf71a35 and e3b20aef8, sit directly on feature/microvm/dev at 78ddb8044, where #5225 (microsoft/nvx#1627), on which this PR was stacked, landed. NVX pins the head of this branch in microsoft/nvx#1969, so this is a prerequisite of that NVX change, and it lands by fast-forwarding feature/microvm/dev to that exact commit, not with the merge button, which squashes.

What and why

On a Windows host, lxutil's LxVolume::open asked for FILE_WRITE_DATA whenever the open flags held O_TRUNC, since Windows truncates only through a handle with write access, and the LxFile then kept that handle, and recorded that access, for its lifetime. So the file that an O_RDONLY | O_TRUNC or O_NOACCESS | O_TRUNC open returned could be written with pwrite and truncated with set_attr, unlike a Linux descriptor with the same flags, which fails such a write with EBADF and such a truncation with EINVAL; and O_APPEND | O_TRUNC got FILE_WRITE_DATA back, so its writes went to their offsets rather than to the end of the file. A virtio-fs guest could thus keep writing a file through a handle that it had opened read-only (FUSE_OPEN or FUSE_CREATE with O_RDONLY | O_TRUNC, then FUSE_WRITE), and a microVM restore, which reopens the handle without O_TRUNC, silently took that access away. virtio-9p, whose writes call LxFile::pwrite directly, had the same hole.

Separately, on every host, VirtioFs::write checked whether the guest may write the handle's file, which fails with EROFS on a read-only share or outside a microVM's writable paths, and then left it to the host to refuse a handle that the guest had not opened for writing. So a FUSE_WRITE through an O_RDONLY handle of a file that the guest may not write failed with EROFS, where Linux's write(2) fails with EBADF first.

The fix, in two commits:

  • lxutil: keep O_TRUNC from making a Windows handle writable: open_flags_to_access_mask derives the access of the open file from its access mode and O_APPEND alone. LxVolume::open truncates an existing file through the file's handle when that access includes FILE_WRITE_DATA, and otherwise through a handle that util::reopen_file opens with FILE_WRITE_DATA for the truncation alone and then closes. The open still needs write access to truncate, as on Linux, and fails with EACCES without it. Linux hosts are unchanged.
  • virtiofs: refuse a write through a handle not open for writing: write fails with EBADF before it checks whether the guest may write the file, unless the access mode of the guest's open flags, which the handle records and a microVM snapshot saves, is O_WRONLY or O_RDWR. A guest kernel writes only through handles that it opened for writing, so only a guest that drives the queue itself can see the change.

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

Kept from the issue's suggested fix: the virtio-fs check of the handle's access mode before the policy check, and the shape of its test. Added: the lxutil fix of the root cause, without which a Windows host would still let a guest truncate a file through a read-only handle with FUSE_SETATTR and FATTR_FH, would still drop the handle's write access on restore, and would still let O_APPEND | O_TRUNC writes overwrite the file, and virtio-9p would keep the hole.

Tests

  • lxutil, tests::truncating_open_keeps_the_access_mode: opens an existing file with O_RDONLY | O_TRUNC and with O_NOACCESS | O_TRUNC, and creates one with O_RDONLY | O_CREAT | O_TRUNC, and checks that each open leaves the file empty but that a pwrite through it fails with EBADF and a truncate with EINVAL.
  • lxutil, tests::truncating_append_open_stays_append_only: two writes at offset 0 through O_WRONLY | O_APPEND, with and without O_TRUNC, both append.
  • virtiofs, microvm::policy_tests::writes_need_a_handle_open_for_writing: in a microVM share whose only writable path is out, writes through handles of out/existing that FUSE_OPEN opened O_RDONLY and O_RDONLY | O_TRUNC, of a new file that FUSE_CREATE opened O_RDONLY | O_CREAT | O_TRUNC, and of build.log that FUSE_OPEN opened O_RDONLY, each fail with EBADF and change nothing, while a write through an O_WRONLY handle succeeds.

The lxutil tests follow Linux semantics, so they pass on Linux with and without the fix and check that Windows matches.

Validation

cargo nextest run --profile agent on the packages, at this head, and with the two fixes reverted to the base, 78ddb8044, keeping the new tests:

Host Without the fixes (at 78ddb8044) At this head
Windows 11, local all three new tests fail: the pwrite through the O_RDONLY | O_TRUNC file returns Ok(5); the O_APPEND | O_TRUNC writes leave cd, not abcd; and the virtio-fs write through the O_RDONLY | O_TRUNC handle succeeds lxutil 72, virtiofs 224, and plan9 16 pass (2, 3, and 0 ignored tests skipped)
nvx-kvm-test, Azure Linux 3.0 VM, kernel 6.18 the virtio-fs test fails at the write through build.log: left: EROFS, right: EBADF; the two lxutil tests pass, since the host kernel gives a descriptor these semantics lxutil 52, virtiofs 223, and plan9 17 pass (3, 1, and 0 skipped)

The lxutil commit alone passes the lxutil and virtiofs tests on both hosts (295 on Windows, 274 on Linux). At this head, on both hosts, cargo check, cargo clippy --all-targets, and cargo doc --no-deps for lxutil and virtiofs are clean, with no warning but the LNK4199 linker message that every linked artifact gets on the Windows machine. cargo xtask fmt --fix changes nothing. The two commits are unchanged, as git range-diff shows, from their earlier versions on b1c57d55c and b51f9156c, where the same tests failed the same way without the fixes and passed with them.

@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

On Windows, LxVolume::open asked for FILE_WRITE_DATA whenever the open
flags held O_TRUNC, since Windows truncates only through a handle with
write access, and the LxFile then kept that handle, and recorded that
access, for its lifetime. So the file that an O_RDONLY | O_TRUNC or
O_NOACCESS | O_TRUNC open returned could be written with pwrite and
truncated with set_attr, unlike a Linux descriptor with the same flags,
which fails such a write with EBADF and such a truncation with EINVAL;
and O_APPEND | O_TRUNC got FILE_WRITE_DATA back, so its writes went to
their offsets rather than to the end of the file. A virtio-fs guest
could thus keep writing a file through a handle that it had opened
read-only (microsoft/nvx#1609), and a microVM restore, which reopens
the handle without O_TRUNC, silently took that access away.

open_flags_to_access_mask now derives the access of the open file from
its access mode and O_APPEND alone. LxVolume::open truncates an
existing file through the file's handle when that access includes
FILE_WRITE_DATA, and otherwise through a handle reopened with
FILE_WRITE_DATA for the truncation alone, which it then closes. The
open still needs write access to truncate, as on Linux, and fails with
EACCES without it.

truncating_open_keeps_the_access_mode opens an existing file with
O_RDONLY | O_TRUNC and with O_NOACCESS | O_TRUNC, and creates one with
O_RDONLY | O_CREAT | O_TRUNC, and checks that each open leaves the file
empty but that a pwrite through it fails with EBADF and a truncate with
EINVAL. truncating_append_open_stays_append_only checks that two
writes at offset 0 through O_WRONLY | O_APPEND, with and without
O_TRUNC, both append. Without the fix, at feature/microvm/dev b51f915
on Windows, the pwrite through the O_RDONLY | O_TRUNC file succeeds,
and the writes through the O_WRONLY | O_APPEND | O_TRUNC one overwrite
each other; with it, both tests and the package's others pass there.
Both pass on Linux (Azure Linux 3.0, nvx-kvm-test) with and without
the fix, since the host kernel gives a descriptor these semantics.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
VirtioFs::write checked whether the guest may write the handle's file,
which fails with EROFS on a read-only share or outside a microVM's
writable paths, and then wrote through the handle, leaving it to the
host to refuse a handle that the guest had not opened for writing. So
a FUSE_WRITE through an O_RDONLY handle of a file that the guest may
not write failed with EROFS on every host, where Linux's write(2)
fails with EBADF first (microsoft/nvx#1609), and whether a write
through such a handle of a writable file failed depended on the host's
handle, which a Windows host opened with write access for
O_RDONLY | O_TRUNC until the previous commit.

write now fails with EBADF before it checks whether the guest may
write the file, unless the access mode of the guest's open flags,
which the handle records and a microVM snapshot saves, is O_WRONLY or
O_RDWR. A guest kernel writes only through handles that it opened for
writing, so only a guest that drives the queue itself can see the
change.

writes_need_a_handle_open_for_writing, in a microVM share whose only
writable path is out, writes through handles of out/existing that
FUSE_OPEN opened O_RDONLY and O_RDONLY | O_TRUNC, of a new file that
FUSE_CREATE opened O_RDONLY | O_CREAT | O_TRUNC, and of build.log that
FUSE_OPEN opened O_RDONLY, and checks that each write fails with EBADF
and changes nothing, while a write through an O_WRONLY handle
succeeds. Without the fix, at feature/microvm/dev b51f915, the write
through the O_RDONLY | O_TRUNC handle succeeds on Windows, and the
write through build.log fails with EROFS on Linux (Azure Linux 3.0,
nvx-kvm-test) and, once the previous commit refuses the first, on
Windows; with it, the test and the package's others pass on both.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ppenna
Pedro Henrique Penna (ppenna) force-pushed the copilot/nvx-issue-1609-rdonly-trunc branch from b37617f to e3b20ae Compare November 22, 2026 13:30
Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 22, 2026
Map positions 366 and 367, the commits of microsoft/openvmm#5226, which
NVX now pins at e3b20aef8, 241 commits past the mapped tip:

- 366, lxutil: keep O_TRUNC from making a Windows handle writable.
  LxVolume::open on Windows keeps the access of the file that it opens
  to its access mode and O_APPEND, and truncates for O_TRUNC through a
  handle reopened with write access for the truncation alone. It added
  FILE_WRITE_DATA to every O_TRUNC open, so a file opened with
  O_RDONLY | O_TRUNC could be written and truncated, and
  O_APPEND | O_TRUNC writes no longer appended.
- 367, virtiofs: refuse a write through a handle not open for writing.
  VirtioFs::write fails with EBADF, before it checks whether the guest
  may write the file, unless the guest opened the handle O_WRONLY or
  O_RDWR. It checked the file first, so a write through an O_RDONLY
  handle of a file that the guest may not write failed with EROFS.

Positions 366 and 367 are not yet assigned to a PR: they change
upstream's open_flags_to_access_mask and LxVolume::open, which no chain
commit changes, and upstream's write, between the get_file of position
346 and upstream's check_writable, and nothing that a mapped PR adds;
position 367 fixes no defect of position 160, whose policy only adds
another EROFS that upstream's order put first. Upstream main,
bf353460f, has both defects. Position 366 applies there, and lxutil
checks and passes its tests on the result on Windows, so it can go
upstream on its own; replaying position 367 there conflicts in lib.rs,
next to the get_file of position 346, and in the microVM's
policy_tests.rs, which upstream main lacks, so it cannot.

Record also that feature/microvm/dev now stands at position 365,
78ddb8044, where microsoft/openvmm#5225 landed positions 364 and 365.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ppenna
Pedro Henrique Penna (ppenna) merged commit e3b20ae into feature/microvm/dev Nov 22, 2026
102 of 105 checks passed
Pedro Henrique Penna (ppenna) added a commit to microsoft/nvx that referenced this pull request Nov 22, 2026
OpenVMM's virtio-fs server keeps a node's last name once the guest
removes every name of its object, by FUSE_UNLINK, FUSE_RMDIR, or a
FUSE_RENAME over it, and served every later request about the node
at that name but FUSE_LINK, which #1608's pin refuses,
so the requests read, listed, and changed whatever object had since
appeared there (#1623). Through a file's node, GETATTR
reported the other object's attributes under the node's inode number,
OPEN opened it, SETATTR truncated it, and the extended-attribute
requests changed it; through a directory's node, OPENDIR listed the
other directory, and LOOKUP, CREATE, UNLINK, RENAME, and the rest
reached its entries. On a microVM share, a guest that renamed a host
hard link of a read-only file over a writable file's name could
truncate the read-only file through the writable file's node.

Pin microsoft/openvmm eff41b13f03d5f72439b420279521793134c4036, one
commit on e3b20aef8, the head of feature/microvm/dev, where
microsoft/openvmm#5226 landed:

- virtiofs: reach a node whose last name is gone only through its
  handles. Every request about such a node that names no handle fails
  with ENOENT, as one through an unlinked file's former name does, and
  as Linux refuses to look up or create an entry in a removed
  directory; requests through the node's open handles still reach its
  object. A hard link that an unlink of the node's other name
  overtakes keeps the new name. Its tests fail without the change on
  Windows and on Linux (nvx-kvm-test).

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 368, the commit of microsoft/openvmm#5227, which NVX now
pins at eff41b13f, 242 commits past the mapped tip:

- 368, virtiofs: reach a node whose last name is gone only through
  its handles. Every request about a node whose names the guest has
  all removed, other than those through the node's open handles,
  fails with ENOENT, as one through an unlinked file's former name
  does, rather than reach whatever object has since appeared at the
  node's last name: VirtioFsInode's operations and its child_path take
  the path from position 352's object_path, which now checks that the
  path is one of the node's names. They used upstream's get_path, the
  name that position 048 keeps when it drops a node's last name, so
  GETATTR, OPEN, SETATTR, and the extended-attribute requests acted on
  the other object, a removed directory's node reached the entries of
  the directory created in its place, and a file's node whose name a
  read-only file had taken could truncate that file.

Position 368 is not yet assigned to a PR. It changes upstream's
operations in inode.rs, position 048's add_alias, position 083's
object_stat, position 352's object_path, and the names that PR27's
position 126 has the microVM's validate_confined_paths check, so
assigning it revisits PR13, PR18, and PR27, but it fixes no defect of
positions 048, 083, or 126: upstream main, bf353460f, has the defect,
since its nodes keep the name of their last lookup through removals.
It needs position 352's object_path. Replaying it on upstream main
conflicts in inode.rs and in the microVM's inode.rs and
policy_tests.rs, so it cannot go upstream on its own. The Scope also
records that feature/microvm/dev now stands at position 367,
e3b20aef8, where microsoft/openvmm#5226 landed positions 366 and 367.

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