use std::ffi::OsStr;
use std::io;
use std::path::Path;
pub const REQUEST_ENV: &str = "HERMIT_E2E_EMPTY_WORKDIR";
pub const WORKDIR: &str = "/test";
pub fn requested_workdir(value: Option<&OsStr>) -> io::Result<Option<&'static Path>> {
match value {
None => Ok(None),
Some(value) if value == OsStr::new(WORKDIR) => Ok(Some(Path::new(WORKDIR))),
Some(value) => Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("{REQUEST_ENV} must be {WORKDIR}, got {value:?}"),
)),
}
}
pub fn with_isolated_workdir<F, T>(run: F) -> io::Result<T>
where
F: FnOnce() -> T + Send,
T: Send,
{
std::thread::scope(|scope| {
let thread = std::thread::Builder::new()
.name("hermit-test-workdir".into())
.spawn_scoped(scope, move || {
enter_namespace()?;
Ok(run())
})?;
match thread.join() {
Ok(result) => result,
Err(panic) => std::panic::resume_unwind(panic),
}
})
}
fn syscall_result(result: libc::c_int, operation: &str) -> io::Result<()> {
if result == 0 {
Ok(())
} else {
let error = io::Error::last_os_error();
Err(io::Error::new(
error.kind(),
format!("{operation}: {error}"),
))
}
}
fn enter_namespace() -> io::Result<()> {
syscall_result(
unsafe { libc::unshare(libc::CLONE_NEWNS) },
"unshare mount namespace",
)?;
syscall_result(
unsafe {
libc::mount(
std::ptr::null(),
c"/".as_ptr(),
std::ptr::null(),
libc::MS_REC | libc::MS_PRIVATE,
std::ptr::null(),
)
},
"make mount propagation private",
)?;
syscall_result(
unsafe {
libc::mount(
c"tmpfs".as_ptr(),
c"/test".as_ptr(),
c"tmpfs".as_ptr(),
libc::MS_NOSUID | libc::MS_NODEV,
c"mode=1777".as_ptr().cast(),
)
},
"mount per-run /test tmpfs",
)
}
#[cfg(test)]
mod tests {
use std::os::unix::ffi::OsStrExt;
use super::*;
#[test]
fn request_is_exact_and_fail_closed() {
assert_eq!(requested_workdir(None).unwrap(), None);
assert_eq!(
requested_workdir(Some(OsStr::new("/test"))).unwrap(),
Some(Path::new("/test"))
);
for value in [b"".as_slice(), b"/tmp", b"/test/", b"/test\0", b"/test\xff"] {
let error = requested_workdir(Some(OsStr::from_bytes(value))).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
assert!(
error
.to_string()
.contains("HERMIT_E2E_EMPTY_WORKDIR must be /test")
);
}
}
}