Skip to content

Commit 1495ac8

Browse files
ppennaCopilot
andcommitted
virtiofs: record the host open flags of a microVM create handle
A microVM snapshot that held a handle that FUSE_CREATE returned could fail to restore (microsoft/nvx#2129): the save succeeded, but the restore refused the handle with "saved handle has unsupported open flags". FUSE_OPEN records the options that util::open_flags derives from the guest's flags, without __FMODE_EXEC, O_LARGEFILE, and O_NOFOLLOW, which only the guest uses. FUSE_CREATE opened the file with the options that util::create_flags derives in the same way, but recorded the guest's flags as they came. A save stores the recorded flags, and a restore checks them with reopen_flags, which refuses __FMODE_EXEC and spells O_LARGEFILE as x86's 0x8000. On an x86-64 host, a create handle whose flags held __FMODE_EXEC, which only a guest that drives the queue itself sends with FUSE_CREATE, thus could not restore, and on an AArch64 host, where O_LARGEFILE is 0x20000 and 0x8000 is O_NOFOLLOW, no create handle of a 64-bit Linux guest could, since its kernel adds O_LARGEFILE to every open. Both date from 6315c16 (virtiofs: persist microVM FUSE namespaces, handles, and directory cookies). create now records the options, as open does, so a save stores no guest-only flag, and VirtioFsFile says that the flags that it records are such options. reopen_flags accepts lx::O_LARGEFILE, the host architecture's value, in place of x86's, because a snapshot from a release that recorded the guest's flags holds O_LARGEFILE in each of its create handles; the restore drops it, as it drops O_NOFOLLOW. A create handle of such a snapshot that holds __FMODE_EXEC still fails to restore. No saved-state format or version changes. a_create_handle_saves_and_restores_without_guest_only_flags, in the microVM's persistence tests, creates a file on a read-write share with O_RDWR | O_CREAT and the three guest-only flags, saves, restores, checks that the handle saved O_RDWR | O_CREAT, and writes and reads the file through the restored handle; then again with the saved handle holding O_LARGEFILE and O_NOFOLLOW too, as such a release saved it. Without the fix, at 4aeb405 as at feature/microvm/dev 1633b37, the restore fails on Windows 11 and on Linux (x86-64) with "saved handle has unsupported open flags 0x28062". With the fix, the test and the package's other tests pass on both. The second case fails without the change to reopen_flags only where O_LARGEFILE is not 0x8000, as on AArch64, which no host here runs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4aeb405 commit 1495ac8

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pub(crate) struct DirectorySnapshot {
2828
pub struct VirtioFsFile {
2929
pub(crate) file: RwLock<LxFile>,
3030
pub(crate) inode: Arc<VirtioFsInode>,
31+
/// The host open options of `file`, which a microVM snapshot saves and a
32+
/// restore reopens it with, so never the guest-only flags that
33+
/// `util::create_flags` consumes.
3134
open_flags: u32,
3235
pub(crate) directory_snapshot: RwLock<DirectorySnapshot>,
3336
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ impl Fuse for VirtioFs {
309309
// on the inode number (if this is a non-exclusive create), so make sure to associate the
310310
// file with the returned inode.
311311
let (new_inode, node_id) = inode_reservation.insert(new_inode)?;
312-
let file = VirtioFsFile::new(file, new_inode, arg.flags);
312+
// As `open` does, record the host options rather than the guest's
313+
// flags, so that a microVM snapshot saves no guest-only flag.
314+
let file = VirtioFsFile::new(file, new_inode, options as u32);
313315
let fh = file_reservation.insert(file)?;
314316
Ok(CreateOut {
315317
entry: fuse_entry_out::new(

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,53 @@ fn reopen_flags_preserve_safe_status_bits_and_reject_effects() {
12711271
assert!(reopen_flags(0x80000000).is_err());
12721272
}
12731273

1274+
/// A handle that `FUSE_CREATE` returns records the flags that the server
1275+
/// opened the file with, as one that `FUSE_OPEN` returns does, without
1276+
/// `__FMODE_EXEC`, `O_LARGEFILE`, and `O_NOFOLLOW`, which only the guest uses,
1277+
/// so a snapshot restores the handle, still open for writing. A snapshot from
1278+
/// a release that recorded the guest's flags as they came restores it too: a
1279+
/// Linux guest adds `O_LARGEFILE`, whose value depends on the architecture, to
1280+
/// every create, and may add `O_NOFOLLOW`, but never `__FMODE_EXEC`.
1281+
#[test]
1282+
fn a_create_handle_saves_and_restores_without_guest_only_flags() {
1283+
let guest_only = lx::__FMODE_EXEC | lx::O_LARGEFILE | lx::O_NOFOLLOW;
1284+
for earlier_release in [false, true] {
1285+
let temporary_directory = tempdir().unwrap();
1286+
let root_path = temporary_directory.path();
1287+
let profile = MicroVmVirtioFsProfile::from_attachment(
1288+
MICROVM_ATTACHMENT_ID.to_owned(),
1289+
microvm_root_identity(root_path).unwrap(),
1290+
false,
1291+
Vec::new(),
1292+
)
1293+
.unwrap();
1294+
let source = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
1295+
let (node_id, fh) = create_with_flags(
1296+
&source,
1297+
FUSE_ROOT_ID,
1298+
"file",
1299+
lx::O_RDWR | lx::O_CREAT | guest_only,
1300+
)
1301+
.unwrap();
1302+
let mut state = source
1303+
.save_microvm_state(&profile, SessionState::default())
1304+
.unwrap();
1305+
let saved_flags = state.handles[0].open_flags;
1306+
if earlier_release {
1307+
state.handles[0].open_flags |= (lx::O_LARGEFILE | lx::O_NOFOLLOW) as u32;
1308+
}
1309+
1310+
let destination = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
1311+
let session = Session::new(destination.clone());
1312+
destination
1313+
.restore_microvm_state(&profile, state, &session)
1314+
.unwrap();
1315+
assert_eq!(saved_flags, (lx::O_RDWR | lx::O_CREAT) as u32);
1316+
assert_eq!(write(&destination, node_id, fh, b"guest").unwrap(), 5);
1317+
assert_eq!(read_node(&destination, node_id), b"guest");
1318+
}
1319+
}
1320+
12741321
#[test]
12751322
fn readonly_profile_rejects_a_writable_saved_handle() {
12761323
let temporary_directory = tempdir().unwrap();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ pub(crate) fn reopen_flags(saved: u32) -> anyhow::Result<u32> {
586586
const O_NONBLOCK: u32 = 0x800;
587587
const O_DSYNC: u32 = 0x1000;
588588
const O_SYNC: u32 = 0x101000;
589-
const O_LARGEFILE: u32 = 0x8000;
590589
const O_CLOEXEC: u32 = 0x80000;
591590
const O_TMPFILE: u32 = 0x410000;
592591

@@ -607,7 +606,9 @@ pub(crate) fn reopen_flags(saved: u32) -> anyhow::Result<u32> {
607606
| O_NONBLOCK
608607
| O_DSYNC
609608
| O_SYNC
610-
| O_LARGEFILE
609+
// A release that recorded a created file's guest flags saved the
610+
// guest's `O_LARGEFILE`, whose value depends on the architecture.
611+
| lx::O_LARGEFILE as u32
611612
| O_CLOEXEC;
612613
anyhow::ensure!(
613614
saved & !allowed == 0,

0 commit comments

Comments
 (0)