Repository navigation
[DO NOT REVIEW] virtiofs, lxutil: refuse writes through handles not open for writing - #5226
Merged
Pedro Henrique Penna (ppenna) merged 2 commits intoNov 22, 2026
Conversation
Pedro Henrique Penna (ppenna)
requested review from
a team and
Anatol Belski (weltling)
as code owners
November 22, 2026 07:47
|
This PR modifies files containing For more on why we check whole files, instead of just diffs, check out the Rustonomicon |
This was referenced Nov 22, 2026
Merged
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>
Pedro Henrique Penna (ppenna)
force-pushed
the
copilot/nvx-issue-1609-rdonly-trunc
branch
from
November 22, 2026 13:30
b37617f to
e3b20ae
Compare
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>
Pedro Henrique Penna (ppenna)
merged commit Nov 22, 2026
e3b20ae
into
feature/microvm/dev
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs microsoft/nvx#1609.
Its two commits,
a1cf71a35ande3b20aef8, sit directly onfeature/microvm/devat78ddb8044, 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-forwardingfeature/microvm/devto that exact commit, not with the merge button, which squashes.What and why
On a Windows host, lxutil's
LxVolume::openasked forFILE_WRITE_DATAwhenever the open flags heldO_TRUNC, since Windows truncates only through a handle with write access, and theLxFilethen kept that handle, and recorded that access, for its lifetime. So the file that anO_RDONLY | O_TRUNCorO_NOACCESS | O_TRUNCopen returned could be written withpwriteand truncated withset_attr, unlike a Linux descriptor with the same flags, which fails such a write withEBADFand such a truncation withEINVAL; andO_APPEND | O_TRUNCgotFILE_WRITE_DATAback, 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_OPENorFUSE_CREATEwithO_RDONLY | O_TRUNC, thenFUSE_WRITE), and a microVM restore, which reopens the handle withoutO_TRUNC, silently took that access away. virtio-9p, whose writes callLxFile::pwritedirectly, had the same hole.Separately, on every host,
VirtioFs::writechecked whether the guest may write the handle's file, which fails withEROFSon 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 aFUSE_WRITEthrough anO_RDONLYhandle of a file that the guest may not write failed withEROFS, where Linux'swrite(2)fails withEBADFfirst.The fix, in two commits:
lxutil: keep O_TRUNC from making a Windows handle writable:open_flags_to_access_maskderives the access of the open file from its access mode andO_APPENDalone.LxVolume::opentruncates an existing file through the file's handle when that access includesFILE_WRITE_DATA, and otherwise through a handle thatutil::reopen_fileopens withFILE_WRITE_DATAfor the truncation alone and then closes. The open still needs write access to truncate, as on Linux, and fails withEACCESwithout it. Linux hosts are unchanged.virtiofs: refuse a write through a handle not open for writing:writefails withEBADFbefore 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, isO_WRONLYorO_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_SETATTRandFATTR_FH, would still drop the handle's write access on restore, and would still letO_APPEND | O_TRUNCwrites overwrite the file, and virtio-9p would keep the hole.Tests
lxutil,tests::truncating_open_keeps_the_access_mode: opens an existing file withO_RDONLY | O_TRUNCand withO_NOACCESS | O_TRUNC, and creates one withO_RDONLY | O_CREAT | O_TRUNC, and checks that each open leaves the file empty but that apwritethrough it fails withEBADFand atruncatewithEINVAL.lxutil,tests::truncating_append_open_stays_append_only: two writes at offset 0 throughO_WRONLY | O_APPEND, with and withoutO_TRUNC, both append.virtiofs,microvm::policy_tests::writes_need_a_handle_open_for_writing: in a microVM share whose only writable path isout, writes through handles ofout/existingthatFUSE_OPENopenedO_RDONLYandO_RDONLY | O_TRUNC, of a new file thatFUSE_CREATEopenedO_RDONLY | O_CREAT | O_TRUNC, and ofbuild.logthatFUSE_OPENopenedO_RDONLY, each fail withEBADFand change nothing, while a write through anO_WRONLYhandle 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 agenton the packages, at this head, and with the two fixes reverted to the base,78ddb8044, keeping the new tests:78ddb8044)pwritethrough theO_RDONLY | O_TRUNCfile returnsOk(5); theO_APPEND | O_TRUNCwrites leavecd, notabcd; and the virtio-fs write through theO_RDONLY | O_TRUNChandle succeedslxutil72,virtiofs224, andplan916 pass (2, 3, and 0 ignored tests skipped)nvx-kvm-test, Azure Linux 3.0 VM, kernel 6.18build.log:left: EROFS,right: EBADF; the two lxutil tests pass, since the host kernel gives a descriptor these semanticslxutil52,virtiofs223, andplan917 pass (3, 1, and 0 skipped)The lxutil commit alone passes the
lxutilandvirtiofstests on both hosts (295 on Windows, 274 on Linux). At this head, on both hosts,cargo check,cargo clippy --all-targets, andcargo doc --no-depsforlxutilandvirtiofsare clean, with no warning but theLNK4199linker message that every linked artifact gets on the Windows machine.cargo xtask fmt --fixchanges nothing. The two commits are unchanged, asgit range-diffshows, from their earlier versions onb1c57d55candb51f9156c, where the same tests failed the same way without the fixes and passed with them.