Skip to content

Commit 043083f

Browse files
ppennaCopilot
andcommitted
virtiofs: count only shown entries toward a microVM listing's bound
A microVM directory listing that already holds 4,096 entries, `.` and `..` included, failed with E2BIG when the host listed another entry that the access policy hides, although the guest never sees that entry (microsoft/nvx#2093). The same directory listed all 4,096 entries when the host listed the hidden entries first, so whether the first FUSE_READDIR or FUSE_READDIRPLUS of a handle failed depended on the host's enumeration order, which the guest cannot control. build_microvm_directory_snapshot checked the 4,096-entry bound at the top of its read_dir callback, before the name check and the policy filter, so a hidden entry counted toward the bound as if it were listed. The check now runs where an entry is about to join the listing, after the filter, beside the check of the 64 KiB bound on names, which already ran there. Only the entries that the guest can list count toward either bound, and a 4,097th of them still fails the listing with E2BIG. A test at the real bound needs a directory of more than 4,096 entries, which takes seconds to create on Windows, past the 1 s after which OpenVMM's CI profile calls a test slow and close to the 10 s after which it ends one. build_microvm_directory_snapshot therefore lists through build_microvm_directory_snapshot_within, which takes the bound as a parameter, so that a test can pass a smaller one. microvm_listings_count_only_the_entries_that_the_policy_shows hides each of three files in turn, so that one of them is the last that the host lists, and expects a bound of four entries to hold `.`, `..`, and the two shown files, and a bound of three to fail with E2BIG. With the check back at the top of the callback, it fails with E2BIG for the file that the host lists last: `c` on NTFS, which lists names in order, and `a` on the tmpfs of the Linux host, which lists the newest first. At feature/microvm/dev d44a113, a one-off test, not committed, that fills a handle's listing with 4,094 shown files and hides one more file fails the first FUSE_READDIR and FUSE_READDIRPLUS of the root with E2BIG when the hidden file is the last that the host lists, and lists all 4,096 entries when it is the first, on Windows 11 Enterprise (build 26300, NTFS) and on Azure Linux 3 (kernel 6.18, tmpfs). With the fix, it lists all 4,096 entries in both cases on both hosts, and a listing of 4,095 shown files still fails with E2BIG; the other tests of the virtiofs package pass there too. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 44747bf commit 043083f

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,24 @@ impl VirtioFsFile {
297297
}
298298
}
299299

300+
/// Lists this handle's directory into the entries that the access policy
301+
/// shows, `.` and `..` included, in the host's order, and fails with
302+
/// `E2BIG` when they exceed a handle's bounds of
303+
/// [`MAX_DIRECTORY_ENTRIES_PER_HANDLE`] entries or
304+
/// [`MAX_DIRECTORY_SNAPSHOT_BYTES`] of names. The entries that the policy
305+
/// hides count toward neither bound, wherever the host lists them.
300306
fn build_microvm_directory_snapshot(&self) -> lx::Result<Vec<SavedDirectoryEntry>> {
307+
self.build_microvm_directory_snapshot_within(MAX_DIRECTORY_ENTRIES_PER_HANDLE)
308+
}
309+
310+
/// Lists this handle's directory as
311+
/// [`Self::build_microvm_directory_snapshot`] does, into at most
312+
/// `max_entries` entries. Tests pass fewer than a handle's bound, since a
313+
/// directory of that many entries takes seconds to create on Windows.
314+
pub(crate) fn build_microvm_directory_snapshot_within(
315+
&self,
316+
max_entries: usize,
317+
) -> lx::Result<Vec<SavedDirectoryEntry>> {
301318
let stat = self.object_stat()?;
302319
if stat.mode & lx::S_IFMT != lx::S_IFDIR {
303320
return Err(lx::Error::ENOTDIR);
@@ -307,9 +324,6 @@ impl VirtioFsFile {
307324
let mut total_bytes = 0usize;
308325
let mut entries = Vec::new();
309326
let mut add_entry = |entry: lx::DirEntry| -> lx::Result<bool> {
310-
if entries.len() == MAX_DIRECTORY_ENTRIES_PER_HANDLE {
311-
return Err(lx::Error::E2BIG);
312-
}
313327
let name = entry.name.as_bytes();
314328
if name.is_empty() || name.len() > MAX_DIRECTORY_ENTRY_NAME_BYTES {
315329
return Err(lx::Error::EINVAL);
@@ -322,6 +336,11 @@ impl VirtioFsFile {
322336
Err(error) => return Err(error),
323337
}
324338
}
339+
// Only here, so that the entries that the policy hides count
340+
// toward neither bound.
341+
if entries.len() == max_entries {
342+
return Err(lx::Error::E2BIG);
343+
}
325344
total_bytes = total_bytes
326345
.checked_add(name.len())
327346
.ok_or(lx::Error::E2BIG)?;

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,57 @@ fn microvm_listings_of_more_names_than_a_handle_holds_fail_with_e2big() {
13831383
assert_eq!(read_root(&fs, open_root(&fs), false), Err(lx::Error::E2BIG));
13841384
}
13851385

1386+
/// A handle's listing holds at most MAX_DIRECTORY_ENTRIES_PER_HANDLE entries,
1387+
/// `.` and `..` included, and a directory that shows more fails with E2BIG.
1388+
/// The entries that the access policy hides count toward no bound, so one
1389+
/// that the host lists after the shown entries that fill a listing does not
1390+
/// fail it with E2BIG (microsoft/nvx#2093). A directory of that many entries
1391+
/// takes seconds to create on Windows, so smaller bounds stand in for the
1392+
/// handle's.
1393+
#[test]
1394+
fn microvm_listings_count_only_the_entries_that_the_policy_shows() {
1395+
let temporary_directory = tempdir().unwrap();
1396+
let root_path = temporary_directory.path();
1397+
let files = ["a", "b", "c"];
1398+
for file in files {
1399+
std::fs::write(root_path.join(file), b"").unwrap();
1400+
}
1401+
// The host lists the files in an order of its own, so each is hidden in
1402+
// turn, and one of them is the last that the host lists.
1403+
for hidden in files {
1404+
let profile = MicroVmVirtioFsProfile::from_attachment(
1405+
MICROVM_ATTACHMENT_ID.to_owned(),
1406+
microvm_root_identity(root_path).unwrap(),
1407+
true,
1408+
vec![hidden.to_owned()],
1409+
)
1410+
.unwrap();
1411+
let fs = VirtioFs::new_microvm(root_path, profile).unwrap();
1412+
let directory = fs.get_file(FUSE_ROOT_ID, open_root(&fs)).unwrap();
1413+
let listed = directory
1414+
.build_microvm_directory_snapshot_within(4)
1415+
.map(|entries| {
1416+
let mut names: Vec<_> = entries
1417+
.into_iter()
1418+
.map(|entry| String::from_utf8(entry.name).unwrap())
1419+
.collect();
1420+
names.sort();
1421+
names
1422+
});
1423+
let expected: Vec<_> = [".", ".."]
1424+
.into_iter()
1425+
.chain(files.into_iter().filter(|&file| file != hidden))
1426+
.map(str::to_owned)
1427+
.collect();
1428+
assert_eq!(listed, Ok(expected), "hidden={hidden}");
1429+
assert_eq!(
1430+
directory.build_microvm_directory_snapshot_within(3).err(),
1431+
Some(lx::Error::E2BIG),
1432+
"hidden={hidden}"
1433+
);
1434+
}
1435+
}
1436+
13861437
#[test]
13871438
fn hard_link_aliases_restore_and_reject_missing_or_replaced_names() {
13881439
let temporary_directory = tempdir().unwrap();

0 commit comments

Comments
 (0)