Skip to content

Commit b37617f

Browse files
ppennaCopilot
andcommitted
virtiofs: refuse a write through a handle not open for writing
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>
1 parent 8e3306e commit b37617f

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎vm/devices/virtio/virtiofs/src/lib.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ impl Fuse for VirtioFs {
464464
return Err(lx::Error::E2BIG);
465465
}
466466
let file = self.get_file(request.node_id(), arg.fh)?;
467+
// Like Linux's write(2), refuse a handle that the guest did not open
468+
// for writing, before refusing a file that it may not write.
469+
if !matches!(
470+
file.open_flags() as i32 & lx::O_ACCESS_MASK,
471+
lx::O_WRONLY | lx::O_RDWR
472+
) {
473+
return Err(lx::Error::EBADF);
474+
}
467475
self.check_writable(file.inode())?;
468476
file.write(data, arg.offset, request.uid())
469477
}

‎vm/devices/virtio/virtiofs/src/microvm/policy_tests.rs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,43 @@ impl Share {
263263
}
264264
}
265265

266+
#[test]
267+
fn writes_need_a_handle_open_for_writing() {
268+
let share = Share::new();
269+
let fs = share.fs(&[], &[], &["out"]);
270+
let out = lookup(&fs, "out").unwrap();
271+
let existing = lookup(&fs, "out/existing").unwrap();
272+
273+
// Like Linux's write(2), a write through a handle that the guest did not
274+
// open for writing fails with EBADF, whether FUSE_OPEN or FUSE_CREATE
275+
// opened it, even with O_TRUNC, for which a Windows host needs write
276+
// access.
277+
for flags in [lx::O_RDONLY, lx::O_RDONLY | lx::O_TRUNC] {
278+
let fh = open(&fs, existing, flags).unwrap();
279+
assert_eq!(error(write(&fs, existing, fh, b"guest")), lx::Error::EBADF);
280+
release(&fs, existing, fh);
281+
}
282+
let flags = lx::O_RDONLY | lx::O_CREAT | lx::O_TRUNC;
283+
let (file, fh) = create_with_flags(&fs, out, "created", flags).unwrap();
284+
assert_eq!(error(write(&fs, file, fh, b"guest")), lx::Error::EBADF);
285+
release(&fs, file, fh);
286+
287+
// That error comes before the policy's EROFS outside the writable paths.
288+
let log = lookup(&fs, "build.log").unwrap();
289+
let fh = open(&fs, log, lx::O_RDONLY).unwrap();
290+
assert_eq!(error(write(&fs, log, fh, b"guest")), lx::Error::EBADF);
291+
release(&fs, log, fh);
292+
293+
let fh = open(&fs, existing, lx::O_WRONLY).unwrap();
294+
assert_eq!(write(&fs, existing, fh, b"guest").unwrap(), 5);
295+
release(&fs, existing, fh);
296+
297+
assert_eq!(share.read("out/existing"), "guest");
298+
assert_eq!(share.read("out/created"), "");
299+
assert_eq!(share.read("build.log"), "log");
300+
share.assert_protected_paths_unchanged();
301+
}
302+
266303
#[test]
267304
fn writes_succeed_only_under_writable_paths() {
268305
let share = Share::new();

0 commit comments

Comments
 (0)