Skip to content

Commit

Permalink
refactor: split isolation.rs into files
Browse files Browse the repository at this point in the history
This is a form of preparation for the introduction of additional
security-related measures, which may be best to implement in separate
files.
  • Loading branch information
n0toose committed Dec 1, 2024
1 parent cb1371f commit ad51f75
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/hypercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use uhyve_interface::{parameters::*, GuestPhysAddr, Hypercall, HypercallAddress,

use crate::{
consts::BOOT_PML4,
isolation::UhyveFileMap,
isolation::filemap::UhyveFileMap,
mem::{MemoryError, MmapMemory},
virt_to_phys,
vm::{UhyveVm, VirtualizationBackend},
Expand Down
23 changes: 2 additions & 21 deletions src/isolation.rs → src/isolation/filemap.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
use std::{
collections::HashMap,
ffi::{CString, OsString},
fs::{canonicalize, Permissions},
os::unix::{ffi::OsStrExt, fs::PermissionsExt},
fs::canonicalize,
os::unix::ffi::OsStrExt,
path::{absolute, PathBuf},
};

use tempfile::{Builder, TempDir};
use uuid::Uuid;

/// Creates a temporary directory.
pub fn create_temp_dir() -> TempDir {
let dir = Builder::new()
.permissions(Permissions::from_mode(0o700))
.prefix("uhyve-")
.suffix(&Uuid::new_v4().to_string())
.tempdir()
.ok()
.unwrap_or_else(|| panic!("The temporary directory could not be created."));

let dir_permissions = dir.path().metadata().unwrap().permissions();
assert!(!dir_permissions.readonly());

dir
}

/// Wrapper around a `HashMap` to map guest paths to arbitrary host paths.
#[derive(Debug, Clone)]
pub struct UhyveFileMap {
Expand Down
2 changes: 2 additions & 0 deletions src/isolation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod filemap;
pub mod tempdir;
19 changes: 19 additions & 0 deletions src/isolation/tempdir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::{fs::Permissions, os::unix::fs::PermissionsExt};

use tempfile::{Builder, TempDir};
use uuid::Uuid;
/// Creates a temporary directory.
pub fn create_temp_dir() -> TempDir {
let dir = Builder::new()
.permissions(Permissions::from_mode(0o700))
.prefix("uhyve-")
.suffix(&Uuid::new_v4().to_string())
.tempdir()
.ok()
.unwrap_or_else(|| panic!("The temporary directory could not be created."));

let dir_permissions = dir.path().metadata().unwrap().permissions();
assert!(!dir_permissions.readonly());

dir
}
2 changes: 1 addition & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
arch::{self, FrequencyDetectionFailed},
consts::*,
fdt::Fdt,
isolation::*,
isolation::{filemap::UhyveFileMap, tempdir::create_temp_dir},
mem::MmapMemory,
os::HypervisorError,
params::{self, Params},
Expand Down

0 comments on commit ad51f75

Please sign in to comment.