Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion vm/devices/virtio/virtiofs/src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,16 @@ impl VirtioFs {
plus: bool,
mut lookup: impl FnMut(Arc<VirtioFsVolume>) -> lx::Result<fuse_entry_out>,
) -> lx::Result<Vec<u8>> {
if size as usize > crate::MAX_GUEST_BUFFER_SIZE {
return Err(lx::Error::E2BIG);
}
let Some(aggregate) = self.inner.aggregate() else {
return Ok(Vec::new());
};
let mut buffer = Vec::with_capacity(size as usize);
let mut buffer = Vec::new();
buffer
.try_reserve_exact(size as usize)
.map_err(|_| lx::Error::ENOMEM)?;
// `offset` is the cookie of the next entry to emit (0 at start of stream).
// Entry 0 => ".", 1 => "..", 2.. => children[index - 2].
let mut index = offset;
Expand Down
8 changes: 8 additions & 0 deletions vm/devices/virtio/virtiofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ impl Fuse for VirtioFs {
let new_path = new_inode.child_path(new_name)?;
self.check_writable_entry(&inode, &old_path)?;
self.check_writable_entry(&new_inode, &new_path)?;
// Renaming a name onto itself changes nothing on the host, which still
// fails it as it fails any other rename, such as with ENOENT for a
// missing name. Every node keeps its names: the bookkeeping of a move
// below would drop those at and below the name, since it first drops
// the destination's.
if old_path == new_path {
return inode.rename(name, &new_inode, new_name, flags);
}
let exchange = flags & RENAME_EXCHANGE != 0;
self.preflight_rename_aliases(inode.volume_id(), &old_path, &new_path, exchange)?;
inode.rename(name, &new_inode, new_name, flags)?;
Expand Down
48 changes: 48 additions & 0 deletions vm/devices/virtio/virtiofs/src/microvm/aggregate_fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use super::saved_state::SavedHandle;
use super::saved_state::SavedObjectIdentity;
use super::state::encode_relative_path;
use super::state::validate_microvm_state;
use crate::MAX_GUEST_BUFFER_SIZE;
use crate::VirtioFs;
use crate::test_helpers::decode_dir_entries;
use fuse::Fuse;
use fuse::Session;
use fuse::SessionState;
Expand Down Expand Up @@ -193,6 +195,52 @@ fn synthetic_root_lists_only_the_children_and_refuses_changes() {
assert_eq!(list(&fs, root), ["tools", "work"]);
}

/// A read of the synthetic root serves a buffer of up to
/// `MAX_GUEST_BUFFER_SIZE` bytes, the session's limit of 256 pages per
/// request, and fails a larger one with `E2BIG`, as a read of any other
/// directory does, rather than reserving whatever the guest asks for, up to
/// 4 GiB (microsoft/nvx#1598).
#[test]
fn synthetic_root_refuses_reads_beyond_the_guest_buffer_limit() {
let roots = Roots::new();
let fs = aggregate(&[
Child::new("work", &roots.work, false),
Child::new("tools", &roots.tools, true),
]);
let root = FUSE_ROOT_ID;
let fh = fs.open_dir(&request(root), lx::O_RDONLY as u32).unwrap().fh;
let read = |size: usize, plus: bool| {
let mut arg = fuse_read_in::new_zeroed();
arg.fh = fh;
arg.size = size.try_into().unwrap();
if plus {
fs.read_dir_plus(&request(root), &arg)
} else {
fs.read_dir(&request(root), &arg)
}
};

for plus in [false, true] {
for size in [MAX_GUEST_BUFFER_SIZE + 1, u32::MAX as usize] {
assert_eq!(
read(size, plus).map(|reply| reply.len()),
Err(lx::Error::E2BIG),
"{size} bytes, plus: {plus}"
);
}
}
// The largest buffer still lists the whole root.
assert_eq!(
read(MAX_GUEST_BUFFER_SIZE, false).unwrap(),
read(4096, false).unwrap()
);
let names = decode_dir_entries(&read(MAX_GUEST_BUFFER_SIZE, true).unwrap())
.into_iter()
.map(|entry| entry.name)
.collect::<Vec<_>>();
assert_eq!(names, [".", "..", "work", "tools"]);
}

#[test]
fn children_enforce_their_own_access_modes() {
let roots = Roots::new();
Expand Down
87 changes: 84 additions & 3 deletions vm/devices/virtio/virtiofs/src/microvm/exchange_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Tests for `RENAME_EXCHANGE` on microVM shares: the names that HostFs keeps
//! for each node, the alias limits, and snapshots taken right after an
//! exchange.
//! Tests for renames on microVM shares that do not move one name to another:
//! `RENAME_EXCHANGE`, which swaps two names, and a rename of a name onto
//! itself, which changes none. They check the names that HostFs keeps for each
//! node, the alias limits, and snapshots taken right after the rename.

use super::policy_tests::error;
use super::policy_tests::list;
Expand Down Expand Up @@ -34,6 +35,10 @@ use tempfile::TempDir;
use tempfile::tempdir;
use test_with_tracing::test;

/// Linux's `RENAME_NOREPLACE` flag for `renameat2`, which fails a rename onto
/// an existing name.
const RENAME_NOREPLACE: u32 = 1;

/// A read-write microVM share without an access policy.
struct Share {
_directory: TempDir,
Expand Down Expand Up @@ -318,3 +323,79 @@ fn exchange_checks_the_alias_limits_of_both_names() {
.is_none()
);
}

/// A rename of a name onto itself changes nothing on the host, so every node
/// at or below the name keeps all of its names, and a snapshot taken right
/// after it saves and restores them. A node whose object has another hard link
/// still reaches the object through the renamed name once the guest removes
/// the link. The host still fails such a rename as it fails any other.
#[test]
fn renaming_a_name_onto_itself_keeps_every_name() {
let share = Share::new(&[
("directory/file", "file"),
("directory/nested/leaf", "leaf"),
("first", "first"),
]);
std::fs::hard_link(share.path("first"), share.path("second")).unwrap();
let fs = share.fs();
let directory = lookup(&fs, "directory").unwrap();
let first = lookup(&fs, "first").unwrap();
assert_eq!(lookup(&fs, "second").unwrap(), first);
let nodes: [(u64, &[&str]); 5] = [
(directory, &["directory"]),
(lookup(&fs, "directory/file").unwrap(), &["directory/file"]),
(
lookup(&fs, "directory/nested").unwrap(),
&["directory/nested"],
),
(
lookup(&fs, "directory/nested/leaf").unwrap(),
&["directory/nested/leaf"],
),
(first, &["first", "second"]),
];

for (parent, entry) in [
(FUSE_ROOT_ID, "directory"),
(directory, "file"),
(FUSE_ROOT_ID, "first"),
] {
fs.rename(&request(parent), name(entry), parent, name(entry), 0)
.unwrap();
}
let rename_onto_itself = |entry, flags| {
error(fs.rename(
&request(FUSE_ROOT_ID),
name(entry),
FUSE_ROOT_ID,
name(entry),
flags,
))
};
assert_eq!(rename_onto_itself("missing", 0), lx::Error::ENOENT);
// Windows hosts reject every rename flag.
assert_eq!(
rename_onto_itself("first", RENAME_NOREPLACE),
if cfg!(windows) {
lx::Error::EINVAL
} else {
lx::Error::EEXIST
}
);
let check = |fs: &VirtioFs| {
for (node, names) in nodes {
assert_names(fs, node, names);
}
};
check(&fs);

let state = share.save(&fs);
for (node, names) in nodes {
assert_eq!(saved_names(&state, node), path_set(names));
}
check(&share.restore(state));

fs.unlink(&request(FUSE_ROOT_ID), name("second")).unwrap();
assert_names(&fs, first, &["first"]);
assert_eq!(read_node(&fs, first), b"first");
}
Loading