Skip to content

Commit

Permalink
feat(uhyvefilemap): improve guest path handling
Browse files Browse the repository at this point in the history
Until now, guest paths were not canonicalized.

This would mean that Uhyve would not be able to see that, for example,
"/root" and "/root/directory/.." are both locations referring to the
same directory. This requires a new dependency for now, although we
should remove it once Path::normalize_lexically is implemented and
part of Rust.

This also "removes" support for empty, mapped guest paths.

Fixes #815.
  • Loading branch information
n0toose committed Dec 17, 2024
1 parent 537e7cc commit c9cd2eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ sysinfo = { version = "0.33.0", default-features = false, features = ["system"]
vm-fdt = "0.3"
tempfile = "3.14.0"
uuid = { version = "1.11.0", features = ["fast-rng", "v4"]}
clean-path = "0.2.1"

[target.'cfg(target_os = "linux")'.dependencies]
kvm-bindings = "0.10"
Expand Down
67 changes: 37 additions & 30 deletions src/isolation/filemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use std::{
collections::HashMap,
ffi::{CString, OsString},
fs::canonicalize,
io::ErrorKind,
os::unix::ffi::OsStrExt,
path::{absolute, PathBuf},
};

use clean_path::clean;
use tempfile::TempDir;

use crate::isolation::tempdir::create_temp_dir;
Expand All @@ -27,16 +29,7 @@ impl UhyveFileMap {
.iter()
.map(String::as_str)
.map(Self::split_guest_and_host_path)
.map(|(guest_path, host_path)| {
(
guest_path,
canonicalize(&host_path).map_or(host_path, |host_path| {
absolute(host_path)
.expect("Path is empty or unable to get current directory.")
.into()
}),
)
})
.map(Result::unwrap)
.collect(),
tempdir: create_temp_dir(),
}
Expand All @@ -46,12 +39,19 @@ impl UhyveFileMap {
/// into a guest_path (String) and host_path (OsString) respectively.
///
/// * `mapping` - A mapping of the format `./host_path.txt:guest.txt`.
fn split_guest_and_host_path(mapping: &str) -> (String, OsString) {
let mut mappingiter = mapping.split(":");
let host_path = OsString::from(mappingiter.next().unwrap());
let guest_path = mappingiter.next().unwrap().to_owned();

(guest_path, host_path)
fn split_guest_and_host_path(mapping: &str) -> Result<(String, OsString), ErrorKind> {
let mut mappingiter: std::str::Split<'_, &str> = mapping.split(":");
let host_str = mappingiter.next().ok_or(ErrorKind::InvalidInput)?;
let guest_str = mappingiter.next().ok_or(ErrorKind::InvalidInput)?;

// TODO: Replace clean-path in favor of Path::normalize_lexically, which has not
// been implemented yet. See: https://github.com/rust-lang/libs-team/issues/396
let host_path = canonicalize(host_str)
.map_or_else(|_| clean(absolute(host_str).unwrap()), clean)
.into();
let guest_path: String = clean(guest_str).display().to_string();

Ok((guest_path, host_path))
}

/// Returns the host_path on the host filesystem given a requested guest_path, if it exists.
Expand All @@ -68,7 +68,9 @@ impl UhyveFileMap {
return None;
}

let requested_guest_pathbuf = PathBuf::from(guest_path);
let requested_guest_pathbuf = clean(PathBuf::from(guest_path));
// TODO: Replace clean-path in favor of Path::normalize_lexically, which has not
// been implemented yet. See: https://github.com/rust-lang/libs-team/issues/396
if let Some(parent_of_guest_path) = requested_guest_pathbuf.parent() {
debug!("The file is in a child directory, searching for a parent directory...");
for searched_parent_guest in parent_of_guest_path.ancestors() {
Expand Down Expand Up @@ -116,38 +118,43 @@ mod tests {

#[test]
fn test_split_guest_and_host_path() {
let host_guest_strings = vec![
let host_guest_strings = [
"./host_string.txt:guest_string.txt",
"/home/user/host_string.txt:guest_string.md.txt",
":guest_string.conf",
":",
"exists.txt:also_exists.txt:should_not_exist.txt",
"host_string.txt:this_does_exist.txt:should_not_exist.txt",
"host_string.txt:test/..//guest_string.txt",
];

// We will use `host_string.txt` for all tests checking canonicalization.
let mut fixture_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fixture_path.push("host_string.txt");

// Mind the inverted order.
let results = vec![
let results = [
(
String::from("guest_string.txt"),
OsString::from("./host_string.txt"),
fixture_path.clone().into(),
),
(
String::from("guest_string.md.txt"),
OsString::from("/home/user/host_string.txt"),
),
(String::from("guest_string.conf"), OsString::from("")),
(String::from(""), OsString::from("")),
(
String::from("also_exists.txt"),
OsString::from("exists.txt"),
String::from("this_does_exist.txt"),
fixture_path.clone().into(),
),
(String::from("guest_string.txt"), fixture_path.into()),
];

for (i, host_and_guest_string) in host_guest_strings
.into_iter()
.map(UhyveFileMap::split_guest_and_host_path)
.enumerate()
{
assert_eq!(host_and_guest_string, results[i]);
assert_eq!(
host_and_guest_string.expect("Result is an error!"),
results[i]
);
}
}

Expand Down Expand Up @@ -255,7 +262,7 @@ mod tests {
// Tests directory traversal leading to valid symbolic link with an
// empty guest_path_map.
host_path_map = fixture_path.clone();
guest_path_map = PathBuf::from("");
guest_path_map = PathBuf::from("/root");
uhyvefilemap_params = [format!(
"{}:{}",
host_path_map.to_str().unwrap(),
Expand All @@ -264,7 +271,7 @@ mod tests {

map = UhyveFileMap::new(&uhyvefilemap_params);

target_guest_path = PathBuf::from("this_symlink_leads_to_a_file");
target_guest_path = PathBuf::from("/root/this_symlink_leads_to_a_file");
target_host_path = fixture_path.clone();
target_host_path.push("this_folder_exists/file_in_folder.txt");
found_host_path = map.get_host_path(target_guest_path.to_str().unwrap());
Expand Down

0 comments on commit c9cd2eb

Please sign in to comment.