Skip to content

Commit 899dcd6

Browse files
committed
virtiofs: stop microVM READDIRPLUS from aliasing the listed node
A microVM READDIRPLUS looks each name of its directory listing other than . and .. up, and a lookup that reaches the object of a node returns that node, with one more lookup reference and with the name's path as the node's path. A listed name can reach the object of the very node whose handle the guest lists (microsoft/nvx#1634): a bind mount of a directory on one of its own subdirectories does, and so does the node's directory once the host moves it into a new directory at the node's path, since a handle that the guest then opens on the node lists that new directory. READDIRPLUS then handed the guest the listed node as an entry below itself, which the guest cannot hold, and moved the node to the name's path, at which it looked the rest of the listing up: in the moved directory, which lacks the listed names, so READDIRPLUS dropped them as gone, or through the bind mount, so that the node, even the share's root, stayed at a path that names another directory once the host unmounts the bind mount. write_microvm_entry_plus now checks, after the lookup and the check that the name still reaches the listed object, whether the lookup reached the object of the handle's node, by the dedup key and file type by which the inode map would alias it. Such an entry hands the guest no node, which FUSE allows for a READDIRPLUS entry, as for . and .., so it takes no lookup reference and leaves the node as it was, and it lists the name as READDIR does, by the listed number and type. The issue proposes to forget the entry's reference and stop the listing, with EIO if the entry is the first of a read; but the lookup has moved the node by then, and since each later read starts at the entry, the guest could never list the rest of the directory. Tests, in virtiofs' microVM tests: a READDIRPLUS of the directory that the host made at a node's path, which lists the node's moved directory before a file, lists both, the moved directory without a node, and leaves the node's path, aliases, and lookup count as they were; and, on Linux, a READDIRPLUS of a share whose root is bind mounted on one of its own subdirectories, in user and mount namespaces of the test's own, lists the mount point without a node, by its READDIR number and type, and leaves the root at its path. Without the fix, on top of the microsoft/nvx#1605 fix, the first lists no file on Windows and on Linux (Azure Linux 3, kernel 6.18, tmpfs and ext4), and on Windows it hands the moved directory the listed node and moves the node to listed\moved with two lookups; the second hands the mount point the root's node and moves the root to loop. With the fix, both and the package's other tests pass there. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c0efd6c commit 899dcd6

2 files changed

Lines changed: 169 additions & 1 deletion

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ impl VirtioFsFile {
192192
///
193193
/// The entry's lookup takes the lookup reference that the entry hands to
194194
/// the guest, so the reference is held only if the entry is written: an
195-
/// entry that does not fit, or that fails, holds none.
195+
/// entry that does not fit, or that fails, holds none. An entry whose name
196+
/// reaches the object of this handle's own node hands the guest no node,
197+
/// so it holds none either.
196198
fn write_microvm_entry_plus(
197199
&self,
198200
fs: &VirtioFs,
@@ -215,6 +217,24 @@ impl VirtioFsFile {
215217
if child.device_nr() == self.inode.device_nr() && attr.ino != entry.guest_inode_id {
216218
self.check_still_listed(entry, host_listing)?;
217219
}
220+
// A name can reach the object of this handle's own node: a bind mount
221+
// of the directory on one of its own subdirectories does, and so does
222+
// the node's directory once the host moves it into a new directory at
223+
// the node's path, which a handle opened on the node then lists.
224+
// Inserting the object would make the name's path the node's path, at
225+
// which this read looks up the directory's other names, and hand the
226+
// guest a directory below itself, which the guest cannot hold. So the
227+
// entry hands out no node, as `.` and `..` do, and lists the name as
228+
// READDIR does.
229+
if child.dedup_key() == self.inode.dedup_key()
230+
&& crate::InodeMap::reuses(&self.inode, &child)
231+
{
232+
return Ok(buffer.dir_entry_plus(
233+
name,
234+
entry.next_cookie,
235+
fuse_entry_out::new_dot(entry.guest_inode_id, entry.kind << 12),
236+
));
237+
}
218238
let fuse_entry = fs.insert_lookup(child, attr)?;
219239
let node_id = fuse_entry.nodeid;
220240
if !buffer.dir_entry_plus(name, entry.next_cookie, fuse_entry) {

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,154 @@ fn readdirplus_lists_an_entry_that_the_host_lists_by_another_number() {
839839
assert_eq!(entries["point"].0, point.attr.ino);
840840
}
841841

842+
/// Returns the names of the entries of `reply`, a READDIRPLUS reply, sorted,
843+
/// and the node ID of the entry named `name`.
844+
fn names_and_node_id_of(reply: &[u8], name: &str) -> (Vec<String>, u64) {
845+
let entries = decode_dir_entries(reply);
846+
let node_id = entries
847+
.iter()
848+
.find(|entry| entry.name == name)
849+
.map(|entry| entry.node_id)
850+
.unwrap();
851+
let mut names: Vec<_> = entries.into_iter().map(|entry| entry.name).collect();
852+
names.sort();
853+
(names, node_id)
854+
}
855+
856+
/// A READDIRPLUS entry hands the guest the node of the object that its name
857+
/// reaches, and a lookup that reaches the object of a node makes the name's
858+
/// path the node's path. A name that a handle lists can reach the object of
859+
/// the handle's own node: once the host moves the node's directory into a new
860+
/// directory at the node's path, as here, a handle that the guest opens on
861+
/// the node lists that new directory, which holds the moved one, and a bind
862+
/// mount of a directory on one of its own subdirectories reaches the directory
863+
/// too. The guest cannot hold a directory below itself, and READDIRPLUS looks
864+
/// the handle's other names up at the node's path, which must therefore stay
865+
/// the path of the directory that the handle lists. So READDIRPLUS must list
866+
/// the entry without a node, as READDIR lists it, and leave the node as it
867+
/// was.
868+
#[test]
869+
fn readdirplus_lists_an_entry_that_reaches_the_listed_node_without_a_node() {
870+
let temporary_directory = tempdir().unwrap();
871+
let root_path = temporary_directory.path();
872+
std::fs::create_dir(root_path.join("listed")).unwrap();
873+
let fs = VirtioFs::new_microvm(root_path, profile(root_path)).unwrap();
874+
let root = fs.get_inode(FUSE_ROOT_ID).unwrap();
875+
let node_id = fs
876+
.lookup_helper(&root, lx::LxStr::from_bytes(b"listed"))
877+
.unwrap()
878+
.nodeid;
879+
std::fs::rename(root_path.join("listed"), root_path.join("moved")).unwrap();
880+
std::fs::create_dir(root_path.join("listed")).unwrap();
881+
std::fs::rename(
882+
root_path.join("moved"),
883+
root_path.join("listed").join("moved"),
884+
)
885+
.unwrap();
886+
std::fs::write(root_path.join("listed").join("file"), "file").unwrap();
887+
let listed = fs.get_inode(node_id).unwrap();
888+
let handle = fs
889+
.insert_file(Arc::clone(&listed).open(lx::O_RDONLY).unwrap())
890+
.unwrap();
891+
let directory = fs.get_file(node_id, handle).unwrap();
892+
// List the directory without looking its entries up, and order its
893+
// snapshot as a host may, with the moved directory before the file.
894+
directory.read_dir(&fs, 0, 4096, false).unwrap();
895+
let mut entries = directory.directory_entries();
896+
entries.sort_by_key(|entry| entry.name == b"file");
897+
for (cookie, entry) in (1..).zip(&mut entries) {
898+
entry.next_cookie = cookie;
899+
}
900+
directory
901+
.restore_directory_snapshot(&fs.inner.directory_snapshot_budget.read(), true, entries)
902+
.unwrap();
903+
904+
let reply = directory.read_dir(&fs, 0, 4096, true).unwrap();
905+
let (names, moved) = names_and_node_id_of(&reply, "moved");
906+
assert_eq!(names, [".", "..", "file", "moved"]);
907+
assert_eq!(moved, 0);
908+
assert_eq!(listed.clone_path(), PathBuf::from("listed"));
909+
assert_eq!(listed.aliases(), [PathBuf::from("listed")]);
910+
assert_eq!(listed.lookup_count(), 1);
911+
}
912+
913+
/// A bind mount of a directory on one of its own subdirectories, which an
914+
/// unprivileged process can make in user and mount namespaces of its own,
915+
/// reaches the directory, which the host lists by the number of the
916+
/// subdirectory that the mount covers. READDIRPLUS must list it without a
917+
/// node, as READDIR lists it, and leave the directory's node, here the share's
918+
/// root, at its path; where the host allows no such namespaces, the test does
919+
/// nothing.
920+
#[cfg(target_os = "linux")]
921+
#[test]
922+
fn readdirplus_lists_a_bind_mount_of_the_listed_directory_without_a_node() {
923+
use std::os::unix::fs::MetadataExt;
924+
use std::process::Command;
925+
926+
const ROOT: &str = "VIRTIOFS_TEST_BIND_MOUNT_ROOT";
927+
let namespaces = ["--user", "--map-root-user", "--mount"];
928+
let Some(root_path) = std::env::var_os(ROOT) else {
929+
// Only a single-threaded process can enter a new user namespace, so
930+
// the test runs again in a child process in the namespaces, in a
931+
// directory that this process removes once the child is done.
932+
let temporary_directory = tempdir().unwrap();
933+
let root_path = temporary_directory.path();
934+
let bind_mount_works = Command::new("unshare")
935+
.args(namespaces)
936+
.args(["mount", "--bind"])
937+
.args([root_path, root_path])
938+
.status()
939+
.is_ok_and(|status| status.success());
940+
if !bind_mount_works {
941+
eprintln!("skipping: no bind mount in user and mount namespaces");
942+
return;
943+
}
944+
let status = Command::new("unshare")
945+
.args(namespaces)
946+
.arg(std::env::current_exe().unwrap())
947+
.args([
948+
"--exact",
949+
"microvm::fs_tests::readdirplus_lists_a_bind_mount_of_the_listed_directory_without_a_node",
950+
"--no-capture",
951+
])
952+
.env(ROOT, root_path)
953+
.status()
954+
.unwrap();
955+
assert!(status.success(), "{status}");
956+
return;
957+
};
958+
let root_path = Path::new(&root_path);
959+
std::fs::create_dir(root_path.join("loop")).unwrap();
960+
let status = Command::new("mount")
961+
.arg("--bind")
962+
.arg(root_path)
963+
.arg(root_path.join("loop"))
964+
.status()
965+
.unwrap();
966+
assert!(status.success(), "{status}");
967+
let identity = |path: &Path| {
968+
let metadata = std::fs::symlink_metadata(path).unwrap();
969+
(metadata.dev(), metadata.ino())
970+
};
971+
assert_eq!(identity(&root_path.join("loop")), identity(root_path));
972+
973+
let (fs, directory) = microvm_with_an_open_root(root_path, &["file"]);
974+
let reply = directory.read_dir(&fs, 0, 4096, true).unwrap();
975+
let (names, node_id) = names_and_node_id_of(&reply, "loop");
976+
assert_eq!(names, [".", "..", "file", "loop"]);
977+
assert_eq!(node_id, 0);
978+
let root = fs.get_inode(FUSE_ROOT_ID).unwrap();
979+
assert_eq!(root.clone_path(), PathBuf::new());
980+
assert_eq!(root.aliases(), [PathBuf::new()]);
981+
// The guest may read the pages of one listing with either request, so
982+
// READDIRPLUS lists the mount point by the number and type by which
983+
// READDIR lists it.
984+
assert_eq!(
985+
crate::tests::read_directory(&fs, &directory, 0, 4096, true)["loop"],
986+
crate::tests::read_directory(&fs, &directory, 0, 4096, false)["loop"]
987+
);
988+
}
989+
842990
#[test]
843991
fn saved_negotiation_must_match_the_exact_microvm_policy() {
844992
let temporary_directory = tempdir().unwrap();

0 commit comments

Comments
 (0)