Skip to content

Commit 770bc61

Browse files
ppennaCopilot
andcommitted
virtiofs: restore microVM handles opened with access mode 3
A microVM guest that opened a virtio-fs file with access mode 3 and held it open could save the VM's state, but its restore failed with "saved handle has invalid access mode", on read-write and read-only shares alike (microsoft/nvx#2125). Linux's open(2) reserves access mode 3 to check for read and write permission and return a descriptor that can neither read nor write, and the guest's FUSE client sends that access mode to FUSE_OPEN, which accepts it. The restore disagreed with FUSE_OPEN about such a handle twice. reopen_flags rejected access mode 3 outright, and validate_microvm_state took every access mode other than O_RDONLY for one that writes, so a read-only share or child, or a node outside the writable paths, could not restore the handle even without that rejection. FUSE_OPEN gives the handle there, since check_open_readonly refuses only the opens that opens_for_writing names: those for O_WRONLY or O_RDWR, or with O_TRUNC. reopen_flags now keeps every access mode, and the validation takes a handle for one that writes as opens_for_writing takes the open that reopens it, which drops O_TRUNC, so only O_WRONLY and O_RDWR need a writable node. A restored handle keeps access mode 3, so it still neither reads nor writes. The format of the saved state is unchanged. a_handle_that_neither_reads_nor_writes_survives_save_and_restore opens a file with access mode 3 on a read-write share, on a read-only share, and outside the writable paths of a read-write share, saves and restores the state, and expects the restored handle to serve its node and to fail reads and writes with EBADF. At feature/microvm/dev 1633b37, the restore fails with "saved handle has invalid access mode" on Windows 11 (NTFS) and Azure Linux 3 (kernel 6.18, tmpfs). Without that check alone, it fails with "read-only microVM profile cannot restore a writable handle" on the read-only share, and with "microVM access policy cannot restore a writable handle outside its writable paths" outside the writable paths. With the fix, the test passes on both hosts, and so do the other tests of the virtiofs package. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 350018c commit 770bc61

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
//! Tests for microVM filesystem persistence and limits.
55
66
use super::policy_tests::create;
7+
use super::policy_tests::error;
78
use super::policy_tests::lookup;
89
use super::policy_tests::mkdir;
910
use super::policy_tests::name;
1011
use super::policy_tests::open;
12+
use super::policy_tests::owned;
1113
use super::policy_tests::read_node;
1214
use super::policy_tests::release;
1315
use super::policy_tests::request;
16+
use super::policy_tests::write;
1417
use super::profile::MICROVM_ATTACHMENT_ID;
1518
use super::profile::MICROVM_FUSE_MAJOR;
1619
use super::profile::MICROVM_FUSE_MAX_WRITE;
@@ -41,6 +44,7 @@ use fuse::SessionInfoState;
4144
use fuse::SessionState;
4245
use fuse::protocol::FUSE_ASYNC_READ;
4346
use fuse::protocol::FUSE_DIRECT_IO_ALLOW_MMAP_FLAG2;
47+
use fuse::protocol::FUSE_GETATTR_FH;
4448
use fuse::protocol::FUSE_INIT;
4549
use fuse::protocol::FUSE_INIT_EXT;
4650
use fuse::protocol::FUSE_ROOT_ID;
@@ -1200,6 +1204,65 @@ fn readonly_profile_rejects_a_writable_saved_handle() {
12001204
assert!(validate_microvm_state(&state, &profile).is_err());
12011205
}
12021206

1207+
/// Linux's access mode 3 opens a file for neither reading nor writing, so
1208+
/// `FUSE_OPEN` gives such a handle of a file that the guest may not write too:
1209+
/// on a read-only share, and outside the writable paths of a read-write share.
1210+
/// A restore must reopen that handle with the same access, as it reopens the
1211+
/// handles of the other access modes.
1212+
#[test]
1213+
fn a_handle_that_neither_reads_nor_writes_survives_save_and_restore() {
1214+
let cases: [(bool, &[&str]); 3] = [(false, &[]), (true, &[]), (false, &["out"])];
1215+
for (read_only, writable_paths) in cases {
1216+
let case = format!("read_only={read_only}, writable_paths={writable_paths:?}");
1217+
let temporary_directory = tempdir().unwrap();
1218+
let root_path = temporary_directory.path();
1219+
std::fs::create_dir(root_path.join("out")).unwrap();
1220+
std::fs::write(root_path.join("file"), b"data").unwrap();
1221+
let profile = MicroVmVirtioFsProfile::from_attachment_with_policy(
1222+
MICROVM_ATTACHMENT_ID.to_owned(),
1223+
microvm_root_identity(root_path).unwrap(),
1224+
read_only,
1225+
Vec::new(),
1226+
Vec::new(),
1227+
owned(writable_paths),
1228+
)
1229+
.unwrap();
1230+
let source = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
1231+
let node_id = lookup(&source, "file").unwrap();
1232+
let fh = open(&source, node_id, lx::O_NOACCESS).unwrap();
1233+
let state = source
1234+
.save_microvm_state(&profile, SessionState::default())
1235+
.unwrap();
1236+
1237+
let destination = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
1238+
let session = Session::new(destination.clone());
1239+
if let Err(error) = destination.restore_microvm_state(&profile, state, &session) {
1240+
panic!("{case}: {error:#}");
1241+
}
1242+
destination
1243+
.get_attr(&request(node_id), FUSE_GETATTR_FH, fh)
1244+
.unwrap();
1245+
let mut read = fuse_read_in::new_zeroed();
1246+
read.fh = fh;
1247+
read.size = 64;
1248+
assert_eq!(
1249+
error(destination.read(&request(node_id), &read)),
1250+
lx::Error::EBADF,
1251+
"{case}"
1252+
);
1253+
assert_eq!(
1254+
error(write(&destination, node_id, fh, b"guest")),
1255+
lx::Error::EBADF,
1256+
"{case}"
1257+
);
1258+
assert_eq!(
1259+
std::fs::read(root_path.join("file")).unwrap(),
1260+
b"data",
1261+
"{case}"
1262+
);
1263+
}
1264+
}
1265+
12031266
#[test]
12041267
fn saved_parent_alias_is_rejected_before_reopen() {
12051268
let temporary_directory = tempdir().unwrap();

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use super::saved_state::SavedState;
3131
use crate::FuseNegotiation;
3232
use crate::file::VirtioFsFile;
3333
use crate::inode::VirtioFsVolume;
34+
use crate::opens_for_writing;
3435
use anyhow::Context;
3536
use fuse::SessionInfoState;
3637
use fuse::SessionState;
@@ -526,7 +527,9 @@ pub(crate) fn validate_microvm_state(
526527
"saved handle has an unsupported object kind"
527528
);
528529
let reopen_flags = reopen_flags(handle.open_flags)?;
529-
let writable_handle = reopen_flags & lx::O_ACCESS_MASK as u32 != lx::O_RDONLY as u32;
530+
// As for `FUSE_OPEN`, only an open for writing needs a writable node:
531+
// access mode 3 opens the file for neither reading nor writing.
532+
let writable_handle = opens_for_writing(reopen_flags as i32);
530533
if readonly {
531534
anyhow::ensure!(
532535
!writable_handle,
@@ -610,10 +613,6 @@ pub(crate) fn reopen_flags(saved: u32) -> anyhow::Result<u32> {
610613
"saved handle has unsupported open flags {:#x}",
611614
saved
612615
);
613-
anyhow::ensure!(
614-
saved & lx::O_ACCESS_MASK as u32 != lx::O_NOACCESS as u32,
615-
"saved handle has invalid access mode"
616-
);
617616
Ok(saved & !(lx::O_CREAT as u32 | lx::O_EXCL as u32 | lx::O_TRUNC as u32))
618617
}
619618

0 commit comments

Comments
 (0)