Skip to content

Commit 766a74a

Browse files
committed
virtiofs: reach a node whose last name is gone only through its handles
A request about a node whose names the guest had all removed, by FUSE_UNLINK, FUSE_RMDIR, or a FUSE_RENAME over the last one, acted on whatever object had since appeared at the node's former path (microsoft/nvx#1623). Through such a file node, GETATTR and STATX reported that object's attributes under the node's inode number, OPEN opened it, SETATTR truncated it or changed its mode, and the extended attribute requests read and changed its attributes. Through such a directory node, OPENDIR listed that directory, and LOOKUP, CREATE, MKDIR, MKNOD, SYMLINK, LINK, UNLINK, RMDIR, and RENAME looked up, created, moved, and removed its entries. The object could be a read-only file whose host hard link inside a writable path the guest had renamed over the node's name: the node, writable through its former name, then truncated the file, which a request through the file's own node refuses with EROFS. VirtioFsInode::remove_alias_prefix keeps an inode's path when it removes the last alias, and every path-based operation of VirtioFsInode used that path, except VirtioFsInode::link, which microsoft/nvx#1608's fix made take its target's path from VirtioFsInode::object_path, which fails with ENOENT once the guest knows no name for the object. Every other operation of VirtioFsInode that reaches its object by its path now takes the path from object_path too: object_stat, get_attr, get_statx, set_attr, open, read_link, stat_fs, get_xattr, set_xattr, list_xattr, remove_xattr, and check_directory_handle; and so does child_path, through which lookup_child, create, mkdir, mknod, symlink, link, unlink, and rename reach a directory's entries. A request about such a node that names no handle thus fails with ENOENT, as one through an unlinked file's former name does, and as Linux refuses to look up, create, or list entries of a removed directory, while requests through the node's open handles, which VirtioFs::get_file serves without a path, still reach its object. object_path now checks that the path is one of the names that the guest knows, rather than that there is one. VirtioFsInode::add_alias, which records a FUSE_LINK's name after the host link, makes that name the path when the path no longer is one, as when a FUSE_UNLINK of the node's other name on another request queue records its removal first; object_path would otherwise use the removed name, or now fail. The microVM profile's confinement check, which validated the former name as well because operations used it, thus validates only the names, so the former name no longer decides whether a request about the node fails, or with which error. a_file_whose_names_are_gone_is_reached_only_through_its_handles and a_directory_whose_names_are_gone_reaches_no_entry remove the last name of a file's and a directory's node, on a share with and without the microVM profile, either by unlinking or removing it and creating another at its name, or by renaming another entry over it, and send every request about the node that names no handle. a_link_that_an_unlink_of_the_other_name_overtakes_keeps_the_new_name records the removal of a node's other name before a link's new name. Without the fix, at e3b20ae, microsoft/nvx#1609's fix, which stacks on microsoft/nvx#1608's, on Windows and on a Linux KVM host, every request of the first two tests but FUSE_LINK reaches the new object: READLINK and GETXATTR fail with its errors, EINVAL and ENODATA, and the others succeed, so the file node truncates the file renamed over its name, which the microVM share makes read-only; and the third test's node keeps the removed name. With the fix, each request fails with ENOENT and changes nothing, the node's handle still reads and stats its file, the linked node reaches its object through the new name, and the package's tests pass on both hosts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8ce4155 commit 766a74a

3 files changed

Lines changed: 260 additions & 27 deletions

File tree

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

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl VirtioFsInode {
321321

322322
pub(crate) fn object_stat(&self) -> lx::Result<lx::Stat> {
323323
self.validate_confined_object()?;
324-
self.volume.lstat(&*self.get_path())
324+
self.volume.lstat(&*self.object_path()?)
325325
}
326326

327327
/// Maps the raw host inode number of an entry of this directory, as an
@@ -407,13 +407,13 @@ impl VirtioFsInode {
407407
/// Retrieves the attributes of this inode.
408408
pub fn get_attr(&self) -> lx::Result<fuse_attr> {
409409
self.validate_confined_object()?;
410-
let stat = self.volume.lstat(&*self.get_path())?;
410+
let stat = self.volume.lstat(&*self.object_path()?)?;
411411
Ok(self.attr_from_stat(&stat))
412412
}
413413

414414
/// Validates directory policy against the object being enumerated.
415415
pub(crate) fn check_directory_handle(&self, file: &lxutil::LxFile) -> lx::Result<()> {
416-
let path = self.get_path();
416+
let path = self.object_path()?;
417417
let stat = self.volume.lstat(&*path)?;
418418
// The share root is captured by the volume, not resolved by a mutable pathname.
419419
if path.as_os_str().is_empty() {
@@ -433,7 +433,7 @@ impl VirtioFsInode {
433433
/// Retrieves the extended attributes of this inode.
434434
pub fn get_statx(&self) -> lx::Result<fuse_statx> {
435435
self.validate_confined_object()?;
436-
let statx = self.volume.statx(&*self.get_path())?;
436+
let statx = self.volume.statx(&*self.object_path()?)?;
437437
Ok(self.statx_from(&statx))
438438
}
439439

@@ -445,14 +445,14 @@ impl VirtioFsInode {
445445
// Because FUSE_HANDLE_KILLPRIV is set, set-user-ID and set-group-ID must be cleared
446446
// depending on the attributes being set. Lxutil takes care of that on Windows (and Linux
447447
// does it naturally).
448-
let stat = self.volume.set_attr_stat(&*self.get_path(), attr)?;
448+
let stat = self.volume.set_attr_stat(&*self.object_path()?, attr)?;
449449
Ok(self.attr_from_stat(&stat))
450450
}
451451

452452
/// Opens the inode, creating a file object.
453453
pub fn open(self: Arc<VirtioFsInode>, options: i32) -> lx::Result<VirtioFsFile> {
454454
self.validate_confined_object()?;
455-
let file = self.volume.open(&*self.get_path(), options, None)?;
455+
let file = self.volume.open(&*self.object_path()?, options, None)?;
456456
crate::microvm::inode::reject_opened_symlink(&self.volume, &file)?;
457457
Ok(VirtioFsFile::new(file, self, options as u32))
458458
}
@@ -566,7 +566,7 @@ impl VirtioFsInode {
566566
/// Reads the target of the symbolic link, if this inode is a symbolic link.
567567
pub fn read_link(&self) -> lx::Result<LxString> {
568568
self.validate_confined_object()?;
569-
self.volume.read_link(&*self.get_path())
569+
self.volume.read_link(&*self.object_path()?)
570570
}
571571

572572
/// Removes a file or directory child of this inode.
@@ -596,7 +596,7 @@ impl VirtioFsInode {
596596
/// Gets the attributes of the file system that the inode resides on.
597597
pub fn stat_fs(&self) -> lx::Result<fuse_kstatfs> {
598598
self.validate_confined_object()?;
599-
let stat_fs = self.volume.stat_fs(&*self.get_path())?;
599+
let stat_fs = self.volume.stat_fs(&*self.object_path()?)?;
600600
Ok(fuse_kstatfs::new(
601601
stat_fs.block_count,
602602
stat_fs.free_block_count,
@@ -612,26 +612,26 @@ impl VirtioFsInode {
612612
/// Gets the value or the size of an extended attribute on this inode.
613613
pub fn get_xattr(&self, name: &LxStr, value: Option<&mut [u8]>) -> lx::Result<usize> {
614614
self.validate_confined_object()?;
615-
self.volume.get_xattr(&*self.get_path(), name, value)
615+
self.volume.get_xattr(&*self.object_path()?, name, value)
616616
}
617617

618618
/// Sets an extended attribute on this inode.
619619
pub fn set_xattr(&self, name: &LxStr, value: &[u8], flags: u32) -> lx::Result<()> {
620620
self.validate_confined_object()?;
621621
self.volume
622-
.set_xattr(&*self.get_path(), name, value, flags as i32)
622+
.set_xattr(&*self.object_path()?, name, value, flags as i32)
623623
}
624624

625625
/// Lists the extended attributes on this inode.
626626
pub fn list_xattr(&self, list: Option<&mut [u8]>) -> lx::Result<usize> {
627627
self.validate_confined_object()?;
628-
self.volume.list_xattr(&*self.get_path(), list)
628+
self.volume.list_xattr(&*self.object_path()?, list)
629629
}
630630

631631
/// Removes an extended attribute from this inode.
632632
pub fn remove_xattr(&self, name: &LxStr) -> lx::Result<()> {
633633
self.validate_confined_object()?;
634-
self.volume.remove_xattr(&*self.get_path(), name)
634+
self.volume.remove_xattr(&*self.object_path()?, name)
635635
}
636636

637637
/// Gets a clone of the stored path.
@@ -645,8 +645,17 @@ impl VirtioFsInode {
645645
}
646646

647647
/// Adds an alias after a successful hard link.
648+
///
649+
/// A request on another queue may have removed the primary path's name
650+
/// since the link reached the object through it; the new name then
651+
/// becomes the primary path (see [`Self::object_path`]).
648652
pub(crate) fn add_alias(&self, path: PathBuf) {
649-
self.aliases.write().insert(path);
653+
let mut aliases = self.aliases.write();
654+
aliases.insert(path.clone());
655+
let mut primary = self.path.write();
656+
if !aliases.contains(&*primary) {
657+
*primary = path;
658+
}
650659
}
651660

652661
/// Removes all aliases at or below an unlinked directory path.
@@ -716,6 +725,10 @@ impl VirtioFsInode {
716725
}
717726

718727
/// Appends a child name to this inode's path.
728+
///
729+
/// Like Linux, which refuses to look up or create an entry in a removed
730+
/// directory, this fails with `ENOENT` once the guest has removed every
731+
/// name of the directory (see [`Self::object_path`]).
719732
pub(crate) fn child_path(&self, name: &LxStr) -> lx::Result<PathBuf> {
720733
if !lx::s_isdir(self.file_type) {
721734
return Err(lx::Error::ENOTDIR);
@@ -731,7 +744,7 @@ impl VirtioFsInode {
731744
return Err(lx::Error::EINVAL);
732745
}
733746

734-
let mut path = self.clone_path();
747+
let mut path = self.object_path()?.clone();
735748
path.push_lx(LxStr::from_bytes(name))?;
736749
crate::microvm::inode::validate_child_path(&self.volume, &path)?;
737750
Ok(path)
@@ -743,19 +756,23 @@ impl VirtioFsInode {
743756
}
744757

745758
/// Locks and returns the path through which an operation reaches this
746-
/// inode's object, while the path still names the object.
759+
/// inode's object, or an entry of it (see [`Self::child_path`]), while the
760+
/// path still names the object.
747761
///
748762
/// Renames and removals keep the path one of the names that the guest
749763
/// knows for the object, until the guest removes the last one, through an
750764
/// unlink, an rmdir, or a rename over it. The inode then keeps that former
751765
/// name, where the guest or the host may since have put another object. So
752-
/// once the guest knows no name for the object, this fails with `ENOENT`,
753-
/// as an operation through the former name of an unlinked file does.
766+
/// once the path is not one of those names, this fails with `ENOENT`, as
767+
/// an operation through the former name of an unlinked file does, and only
768+
/// the object's open handles still reach it.
754769
pub(crate) fn object_path(&self) -> lx::Result<parking_lot::RwLockReadGuard<'_, PathBuf>> {
755-
if self.aliases.read().is_empty() {
770+
let aliases = self.aliases.read();
771+
let path = self.get_path();
772+
if !aliases.contains(&*path) {
756773
return Err(lx::Error::ENOENT);
757774
}
758-
Ok(self.get_path())
775+
Ok(path)
759776
}
760777
}
761778

@@ -831,6 +848,27 @@ mod tests {
831848
}
832849
}
833850

851+
/// `LINK` records its new name after the host link, so an `UNLINK` of the
852+
/// node's last other name on another queue can record its removal first.
853+
/// The node must then reach its object through the new name.
854+
#[test]
855+
fn a_link_that_an_unlink_of_the_other_name_overtakes_keeps_the_new_name() {
856+
let tmp = tempfile::tempdir().unwrap();
857+
std::fs::write(tmp.path().join("old"), b"data").unwrap();
858+
let volume =
859+
Arc::new(VirtioFsVolume::new(LxVolume::new(tmp.path()).unwrap(), 0, false).unwrap());
860+
let (root, _) = VirtioFsInode::new(volume, PathBuf::new()).unwrap();
861+
let (file, _) = root.lookup_child("old".as_ref()).unwrap();
862+
863+
root.link("new".as_ref(), &file).unwrap();
864+
root.unlink("old".as_ref(), 0).unwrap();
865+
file.remove_alias_prefix(Path::new("old"));
866+
file.add_alias(PathBuf::from("new"));
867+
868+
assert_eq!(&*file.object_path().unwrap(), Path::new("new"));
869+
assert_eq!(file.get_attr().unwrap().size, 4);
870+
}
871+
834872
#[test]
835873
fn directory_handle_checks_fail_closed_without_stable_ids() {
836874
let tmp = tempfile::tempdir().unwrap();

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,12 @@ impl VirtioFsInode {
187187
if !self.volume.strict_paths() {
188188
return Ok(());
189189
}
190-
// Operations use the primary path, which outlives the aliases when the
191-
// object is unlinked, so it must be validated as well.
192-
let mut paths = self.aliases();
193-
let primary = self.clone_path();
194-
if !paths.contains(&primary) {
195-
paths.push(primary);
196-
}
197-
for path in paths {
190+
// An operation reaches the object through its primary path, and only
191+
// while that is one of the names that the guest knows for the object
192+
// (see `VirtioFsInode::object_path`), so only those names need the
193+
// check: not the former name that the primary path keeps once the
194+
// guest has removed them all, where another object may since be.
195+
for path in self.aliases() {
198196
validate_relative_path(&path, true)?;
199197
self.volume.ensure_path_allowed(&path)?;
200198
let component_count = path.components().count();

0 commit comments

Comments
 (0)