Skip to content

Commit 81d6a88

Browse files
ppennaCopilot
andcommitted
virtiofs: never hand out u64::MAX, the synthetic root's handle
A handle map handed out u64::MAX as its last handle (microsoft/nvx#1627). An aggregate's synthetic root uses u64::MAX as its directory handle, SYNTHETIC_ROOT_FH, on the documented assumption that no real handle takes it, so a child's file or directory that got it shared its handle with the root: a RELEASE that names the root's node and handle, which a client sends for a file rather than a directory, closed the child's descriptor, since closing a handle does not check its node. A map that handed out u64::MAX also wrapped its next handle to 0, which a microVM save then recorded and every restore rejects as an invalid allocation state, so the VM could no longer be restored, for node IDs as for file handles. HandleMap::can_insert, which insert and reserve check, refused only a next handle of 0, the wrapped one. Live requests would take some 2^64 allocations to reach u64::MAX, but a microVM restore accepts any next handle or node ID above the saved ones, u64::MAX included, and the next OPEN, OPENDIR, or CREATE, or LOOKUP of a new object, then got u64::MAX. Refuse a next handle of u64::MAX too, so that a map whose next handle reaches it is full: the request that needs another handle or node fails with ENOSPC, as at any other exhaustion, and the map's next handle stays u64::MAX, which a save records and a restore accepts. No restored handle or node can hold u64::MAX, since a restore requires every saved ID below the next one, so the saved state and its schema are unchanged. handle_map_stops_after_max_handle now checks that a map hands out u64::MAX - 1 as its last handle and that a map that starts at u64::MAX, as a restored one can, is full; the reservation tests that started at the last handle start at u64::MAX - 1. In microvm::aggregate_fs_tests, children_never_get_the_synthetic_root_handle restores an aggregate whose next handle ID is u64::MAX and checks that OPEN, OPENDIR, and CREATE in a child fail with ENOSPC and leave the host unchanged, that the root's handle still lists the root, and that the state saves and restores again; in microvm::fs_tests, largest_restored_next_node_id_is_never_handed_out does the same for a LOOKUP with the largest next node ID. On 8225e2c, the head of #5224 (microsoft/nvx#1603), with the fix reverted, the HandleMap test gets u64::MAX, the aggregate test gets handle u64::MAX for the child's file, the node test gets node u64::MAX, and unused_reservations_release_on_drop reserves u64::MAX; a probe there also showed that the save that followed recorded a next handle ID of 0, which the restore rejected, and that a RELEASE of the root's handle closed the child's. With the fix, they and the rest of cargo nextest -p virtiofs pass on Windows and on a Linux KVM host. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8225e2c commit 81d6a88

4 files changed

Lines changed: 135 additions & 20 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use zerocopy::FromZeros;
2727
/// Reserved file handle `open_dir` returns for the synthetic aggregate root;
2828
/// `read_dir`/`read_dir_plus`/`fsync_dir`/`release_dir` recognize it and serve
2929
/// the root instead of the handle map. `u64::MAX` can't collide with a real
30-
/// handle, since `HandleMap` allocates from 1 upward.
30+
/// handle, since `HandleMap` allocates from 1 upward and never hands out
31+
/// `u64::MAX` (see [`HandleMap::can_insert`](crate::HandleMap::can_insert)).
3132
pub(crate) const SYNTHETIC_ROOT_FH: u64 = u64::MAX;
3233

3334
/// A single host folder exposed as a named child of the synthetic aggregate root.

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

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,16 @@ impl<T> HandleMap<T> {
11201120
Arc::ptr_eq(&self.generation, &reservation.generation)
11211121
}
11221122

1123+
/// Whether the map can hand out its next handle.
1124+
///
1125+
/// A map never hands out `u64::MAX`: it is the synthetic aggregate root's
1126+
/// directory handle ([`SYNTHETIC_ROOT_FH`]), and a map that handed it out
1127+
/// would wrap its next handle to 0, which a microVM restore rejects in a
1128+
/// saved state. A map whose next handle reaches `u64::MAX` is full instead.
11231129
pub fn can_insert(&self) -> bool {
1124-
self.next_handle != 0 && !self.values.contains_key(&self.next_handle)
1130+
self.next_handle != 0
1131+
&& self.next_handle != u64::MAX
1132+
&& !self.values.contains_key(&self.next_handle)
11251133
}
11261134

11271135
/// Retrieves a value from the map.
@@ -1817,8 +1825,8 @@ mod tests {
18171825
fn unused_reservations_release_on_drop() {
18181826
let root = tempfile::tempdir().unwrap();
18191827
let fs = VirtioFs::new(root.path(), None).unwrap();
1820-
fs.inner.files.write().next_handle = u64::MAX;
1821-
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX;
1828+
fs.inner.files.write().next_handle = u64::MAX - 1;
1829+
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX - 1;
18221830

18231831
let file = fs.reserve_file().unwrap();
18241832
let inode = fs.reserve_inode().unwrap();
@@ -1827,8 +1835,8 @@ mod tests {
18271835
drop(file);
18281836
drop(inode);
18291837

1830-
assert_eq!(fs.reserve_file().unwrap().reservation.handle, u64::MAX);
1831-
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX);
1838+
assert_eq!(fs.reserve_file().unwrap().reservation.handle, u64::MAX - 1);
1839+
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX - 1);
18321840
}
18331841

18341842
#[test]
@@ -1888,7 +1896,7 @@ mod tests {
18881896
let root = tempfile::tempdir().unwrap();
18891897
let fs = VirtioFs::new(root.path(), None).unwrap();
18901898
fs.inner.files.write().next_handle = 0;
1891-
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX;
1899+
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX - 1;
18921900

18931901
let result = fs.create(
18941902
&request(FUSE_ROOT_ID),
@@ -1898,16 +1906,16 @@ mod tests {
18981906

18991907
assert!(matches!(result, Err(lx::Error::ENOSPC)));
19001908
assert!(!root.path().join("file").exists());
1901-
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX);
1909+
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX - 1);
19021910
}
19031911

19041912
#[test]
19051913
fn create_releases_both_reservations_on_host_failure() {
19061914
let root = tempfile::tempdir().unwrap();
19071915
std::fs::write(root.path().join("file"), b"original").unwrap();
19081916
let fs = VirtioFs::new(root.path(), None).unwrap();
1909-
fs.inner.files.write().next_handle = u64::MAX;
1910-
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX;
1917+
fs.inner.files.write().next_handle = u64::MAX - 1;
1918+
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX - 1;
19111919

19121920
let result = fs.create(
19131921
&request(FUSE_ROOT_ID),
@@ -1924,8 +1932,8 @@ mod tests {
19241932
std::fs::read(root.path().join("file")).unwrap(),
19251933
b"original"
19261934
);
1927-
assert_eq!(fs.reserve_file().unwrap().reservation.handle, u64::MAX);
1928-
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX);
1935+
assert_eq!(fs.reserve_file().unwrap().reservation.handle, u64::MAX - 1);
1936+
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX - 1);
19291937
}
19301938

19311939
#[test]
@@ -1938,7 +1946,7 @@ mod tests {
19381946
.lookup_child(lx::LxStr::from_bytes(b"file"))
19391947
.unwrap();
19401948
let (existing, node_id) = fs.insert_inode(inode).unwrap();
1941-
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX;
1949+
fs.inner.inodes.write().inodes_by_node_id.next_handle = u64::MAX - 1;
19421950

19431951
let reservation = fs.reserve_inode().unwrap();
19441952
let (inode, _) = root_inode
@@ -1948,7 +1956,7 @@ mod tests {
19481956

19491957
assert!(Arc::ptr_eq(&existing, &reused));
19501958
assert_eq!(reused_node_id, node_id);
1951-
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX);
1959+
assert_eq!(fs.reserve_inode().unwrap().reservation.handle, u64::MAX - 1);
19521960
}
19531961

19541962
/// Objects with one inode number on two devices are distinct, as `/proc`
@@ -2119,13 +2127,29 @@ mod tests {
21192127
assert_eq!(second_node_id, FUSE_ROOT_ID + 2);
21202128
}
21212129

2130+
/// A map hands out handles up to `u64::MAX - 1` and never `u64::MAX`, the
2131+
/// synthetic aggregate root's directory handle: a map that reaches it,
2132+
/// or starts there as a restored map can, is full.
21222133
#[test]
21232134
fn handle_map_stops_after_max_handle() {
2124-
let mut handles = HandleMap::starting_at(u64::MAX);
2135+
let mut handles = HandleMap::starting_at(u64::MAX - 1);
21252136

2126-
assert_eq!(handles.insert("last"), Some(u64::MAX));
2127-
assert_eq!(handles.get(u64::MAX), Some(&"last"));
2137+
assert_eq!(handles.insert("last"), Some(u64::MAX - 1));
2138+
assert_eq!(handles.get(u64::MAX - 1), Some(&"last"));
21282139
assert_eq!(handles.insert("overflow"), None);
2140+
assert_eq!(
2141+
handles.reserve().map(|reservation| reservation.handle),
2142+
None
2143+
);
2144+
assert_eq!(handles.get(u64::MAX), None);
2145+
2146+
let mut handles = HandleMap::starting_at(u64::MAX);
2147+
assert_eq!(
2148+
handles.reserve().map(|reservation| reservation.handle),
2149+
None
2150+
);
2151+
assert_eq!(handles.insert("reserved"), None);
2152+
assert_eq!(handles.get(u64::MAX), None);
21292153
}
21302154

21312155
#[test]
@@ -2164,12 +2188,12 @@ mod tests {
21642188

21652189
#[test]
21662190
fn handle_map_reuses_latest_released_reservation() {
2167-
let mut handles = HandleMap::starting_at(u64::MAX);
2191+
let mut handles = HandleMap::starting_at(u64::MAX - 1);
21682192

21692193
let reservation = handles.reserve().unwrap();
2170-
assert_eq!(reservation.handle, u64::MAX);
2194+
assert_eq!(reservation.handle, u64::MAX - 1);
21712195
handles.release_reservation(&reservation);
2172-
assert_eq!(handles.insert("last"), Some(u64::MAX));
2196+
assert_eq!(handles.insert("last"), Some(u64::MAX - 1));
21732197
}
21742198

21752199
/// On Linux, `/` reaches mounts such as `/proc`, `/sys`, and `/dev`:

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use super::state::encode_relative_path;
3131
use super::state::validate_microvm_state;
3232
use crate::MAX_GUEST_BUFFER_SIZE;
3333
use crate::VirtioFs;
34+
use crate::aggregate::SYNTHETIC_ROOT_FH;
3435
use crate::test_helpers::decode_dir_entries;
3536
use fuse::Fuse;
3637
use fuse::Session;
@@ -640,6 +641,56 @@ fn aggregate_state_rejects_changed_children() {
640641
assert!(error.to_string().contains("writable handle"), "{error:#}");
641642
}
642643

644+
/// No child's handle is the synthetic root's directory handle, `u64::MAX`. A
645+
/// restored state may hold that largest next handle ID, which no handle takes:
646+
/// the handle table is then full, so opening a child's file or directory, or
647+
/// creating a file, fails with `ENOSPC` and leaves the host unchanged, while
648+
/// the root's handle still lists the root, and the state saves with the same
649+
/// next handle ID, which a restore accepts.
650+
#[test]
651+
fn children_never_get_the_synthetic_root_handle() {
652+
let roots = Roots::new();
653+
let children = [
654+
Child::new("work", &roots.work, false),
655+
Child::new("tools", &roots.tools, true),
656+
];
657+
let profile = profile(&children);
658+
let source = aggregate(&children);
659+
let work = lookup(&source, "work").unwrap();
660+
let file = lookup(&source, "work/file").unwrap();
661+
let mut state = source
662+
.save_microvm_state(&profile, SessionState::default())
663+
.unwrap();
664+
state.next_handle_id = u64::MAX;
665+
let fs = aggregate(&children);
666+
let session = Session::new(fs.clone());
667+
fs.restore_microvm_state(&profile, state, &session).unwrap();
668+
669+
assert_eq!(open(&fs, file, lx::O_RDONLY), Err(lx::Error::ENOSPC));
670+
assert_eq!(
671+
fs.open_dir(&request(work), lx::O_RDONLY as u32)
672+
.map(|open| open.fh),
673+
Err(lx::Error::ENOSPC)
674+
);
675+
assert_eq!(create(&fs, work, "new"), Err(lx::Error::ENOSPC));
676+
assert_eq!(Roots::entries(&roots.work), ["file"]);
677+
let root = fs
678+
.open_dir(&request(FUSE_ROOT_ID), lx::O_RDONLY as u32)
679+
.unwrap();
680+
assert_eq!(root.fh, SYNTHETIC_ROOT_FH);
681+
assert_eq!(list(&fs, FUSE_ROOT_ID), ["tools", "work"]);
682+
683+
let state = fs
684+
.save_microvm_state(&profile, SessionState::default())
685+
.unwrap();
686+
assert_eq!(state.next_handle_id, u64::MAX);
687+
let restored = aggregate(&children);
688+
let session = Session::new(restored.clone());
689+
restored
690+
.restore_microvm_state(&profile, state, &session)
691+
.unwrap();
692+
}
693+
643694
#[test]
644695
fn a_hidden_child_root_exposes_only_its_allowed_paths() {
645696
let roots = Roots::new();

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,45 @@ fn largest_restored_lookup_count_saturates() {
124124
}
125125
}
126126

127+
/// A saved state may hold the largest next node ID, `u64::MAX`, which no node
128+
/// takes: the node table is then full, so a lookup of an object that has no
129+
/// node yet fails with `ENOSPC`, and the state saves with the same next node
130+
/// ID, which a restore accepts, rather than one wrapped to zero.
131+
#[test]
132+
fn largest_restored_next_node_id_is_never_handed_out() {
133+
let temporary_directory = tempdir().unwrap();
134+
let root_path = temporary_directory.path();
135+
std::fs::write(root_path.join("file"), b"data").unwrap();
136+
let profile = profile(root_path);
137+
let source = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
138+
let mut state = source
139+
.save_microvm_state(&profile, SessionState::default())
140+
.unwrap();
141+
state.next_node_id = u64::MAX;
142+
let destination = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
143+
let session = Session::new(destination.clone());
144+
destination
145+
.restore_microvm_state(&profile, state, &session)
146+
.unwrap();
147+
148+
assert_eq!(
149+
destination
150+
.lookup(&request(FUSE_ROOT_ID), name("file"))
151+
.map(|entry| entry.nodeid),
152+
Err(lx::Error::ENOSPC)
153+
);
154+
155+
let state = destination
156+
.save_microvm_state(&profile, SessionState::default())
157+
.unwrap();
158+
assert_eq!(state.next_node_id, u64::MAX);
159+
let restored = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
160+
let session = Session::new(restored.clone());
161+
restored
162+
.restore_microvm_state(&profile, state, &session)
163+
.unwrap();
164+
}
165+
127166
#[test]
128167
fn initialized_microvm_negotiation_restores() {
129168
let temporary_directory = tempdir().unwrap();

0 commit comments

Comments
 (0)