use std::any::Any;
use std::ffi::CString;
use std::fs;
use std::fs::File;
use std::io;
use std::io::Write;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
use std::panic;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::Ordering;
use anyhow::anyhow;
use detcore_model::config::MountInfoRootRewrite;
use detcore_model::container_mounts::ContainerMountTable;
use detcore_model::container_mounts::Installation;
use detcore_model::container_mounts::assign_installed_rows;
use detcore_model::container_mounts::check_installed_rows;
use detcore_model::container_mounts::owned_mounts;
use hermit::Context;
use hermit::Error;
use hermit::FailureKind;
use hermit::HERMIT_DEADLINE_EXIT;
use hermit::HERMIT_LOG_CAP_EXIT;
use hermit::SerializableError;
use hermit::SkidOvershootError;
use nix::sched::CpuSet;
use nix::sched::sched_getaffinity;
use nix::sys::signal::SaFlags;
use nix::sys::signal::SigAction;
use nix::sys::signal::SigHandler;
use nix::sys::signal::SigSet;
use nix::sys::signal::Signal;
use nix::sys::signal::sigaction;
use nix::unistd::Pid;
use reverie::process::Container;
use reverie::process::ExitStatus;
use reverie::process::Mount;
use reverie::process::MountFlags;
use reverie::process::Namespace;
use reverie::process::RunError;
const GROUP_FILE: &str = "/etc/group";
const NSCD_DIR: &str = "/var/run/nscd";
const DETERMINISTIC_GROUP_ROOT: &[u8] = b"/tmpvol/.hermit/etc/group";
const DETERMINISTIC_NSCD_ROOT: &[u8] = b"/tmpvol/.hermit/run/nscd";
const DETERMINISTIC_TMP_ROOT: &[u8] = b"/tmpvol/.hermit/tmp";
#[derive(Debug)]
struct MountInfoRootSource {
source: Option<File>,
target: PathBuf,
deterministic_root: Option<Vec<u8>>,
rewrite_descendant_roots: bool,
raw_mountpoint_prefix: Option<Vec<u8>>,
deterministic_mountpoint_prefix: Option<Vec<u8>>,
}
fn open_path(path: &Path) -> io::Result<File> {
let path = CString::new(path.as_os_str().as_bytes())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "path contains NUL"))?;
let fd = unsafe { libc::open(path.as_ptr(), libc::O_PATH | libc::O_CLOEXEC) };
if fd < 0 {
return Err(io::Error::last_os_error());
}
Ok(unsafe { File::from_raw_fd(fd) })
}
fn mount_id_for_fd(fd: i32) -> io::Result<u64> {
let contents = fs::read(format!("/proc/thread-self/fdinfo/{fd}"))?;
detcore_model::procfs::parse_fdinfo_mount_id(&contents).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"fdinfo must contain exactly one decimal mnt_id field",
)
})
}
fn mountinfo_root_for_id(raw_mount_id: u64) -> io::Result<Vec<u8>> {
let contents = fs::read("/proc/thread-self/mountinfo")?;
let rows = detcore_model::procfs::parse_mountinfo(&contents).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData, "mountinfo has a malformed row")
})?;
rows.into_iter()
.find(|row| row.raw_mount_id == raw_mount_id)
.map(|row| row.root)
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"mountinfo omitted the proven mount ID",
)
})
}
fn mountinfo_escape_path(path: &Path) -> Vec<u8> {
let mut encoded = Vec::new();
for byte in path.as_os_str().as_bytes() {
match byte {
b' ' => encoded.extend_from_slice(b"\\040"),
b'\t' => encoded.extend_from_slice(b"\\011"),
b'\n' => encoded.extend_from_slice(b"\\012"),
b'\\' => encoded.extend_from_slice(b"\\134"),
byte => encoded.push(*byte),
}
}
encoded
}
struct MountRow {
id: u64,
device: Vec<u8>,
root: Vec<u8>,
mountpoint: Vec<u8>,
}
fn mount_rows() -> io::Result<Vec<MountRow>> {
let contents = fs::read("/proc/thread-self/mountinfo")?;
contents
.split(|byte| *byte == b'\n')
.filter(|line| !line.is_empty())
.map(|line| {
let fields = line.split(|byte| *byte == b' ').collect::<Vec<_>>();
let id = (fields.len() > 4)
.then(|| std::str::from_utf8(fields[0]).ok()?.parse().ok())
.flatten();
match id {
Some(id) => Ok(MountRow {
id,
device: fields[2].to_vec(),
root: fields[3].to_vec(),
mountpoint: fields[4].to_vec(),
}),
None => Err(io::Error::new(
io::ErrorKind::InvalidData,
"mountinfo has a malformed row",
)),
}
})
.collect()
}
fn namespace_rows() -> Result<Vec<detcore_model::container_mounts::ContainerMount>, Error> {
let contents = fs::read("/proc/thread-self/mountinfo")
.context("Failed to read the guest mount table for its container mount table")?;
detcore_model::container_mounts::parse_container_mounts(&contents)
.ok_or_else(|| anyhow!("the guest mount table has a malformed row"))
}
fn is_this_namespaces_proc(row: &detcore_model::container_mounts::ContainerMount) -> bool {
if row.fstype.as_slice() != b"proc" {
return false;
}
let Ok(proc_dir) = open_path(Path::new("/proc")) else {
return true;
};
if mount_id_for_fd(proc_dir.as_raw_fd()).ok() != Some(row.raw_mount_id) {
return true;
}
let me = unsafe { libc::getpid() };
fs::read_link("/proc/self").is_ok_and(|link| link.as_os_str() == me.to_string().as_str())
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum Backing {
Host(PathBuf),
Fresh,
}
#[derive(Debug, Default)]
struct InstallTimeTree {
layers: Vec<(PathBuf, Backing)>,
}
impl InstallTimeTree {
const MAX_LINKS: usize = 40;
fn backing_of(&self, path: &Path) -> Backing {
let layer = self
.layers
.iter()
.enumerate()
.filter_map(|(index, (mountpoint, backing))| {
let rest = path.strip_prefix(mountpoint).ok()?;
Some((mountpoint.components().count(), index, rest, backing))
})
.max_by_key(|(depth, index, _, _)| (*depth, *index));
match layer {
None => Backing::Host(path.to_path_buf()),
Some((_, _, rest, Backing::Host(source))) => {
Backing::Host(if rest.as_os_str().is_empty() {
source.clone()
} else {
source.join(rest)
})
}
Some((_, _, _, Backing::Fresh)) => Backing::Fresh,
}
}
fn resolve(&self, path: &Path) -> PathBuf {
let mut resolved = PathBuf::from("/");
let mut pending: std::collections::VecDeque<std::ffi::OsString> = path
.components()
.filter_map(|component| match component {
std::path::Component::Normal(name) => Some(name.to_os_string()),
std::path::Component::ParentDir => Some("..".into()),
_ => None,
})
.collect();
let mut links = 0;
while let Some(name) = pending.pop_front() {
if name == ".." {
resolved.pop();
continue;
}
let candidate = resolved.join(&name);
if let Backing::Host(host) = self.backing_of(&candidate)
&& links < Self::MAX_LINKS
&& fs::symlink_metadata(&host).is_ok_and(|meta| meta.file_type().is_symlink())
&& let Ok(target) = fs::read_link(&host)
{
links += 1;
if target.is_absolute() {
resolved = PathBuf::from("/");
}
let mut front: std::collections::VecDeque<std::ffi::OsString> = target
.components()
.filter_map(|component| match component {
std::path::Component::Normal(name) => Some(name.to_os_string()),
std::path::Component::ParentDir => Some("..".into()),
_ => None,
})
.collect();
front.extend(pending);
pending = front;
continue;
}
resolved = candidate;
}
resolved
}
fn install(&mut self, mount: &Mount) -> (Option<PathBuf>, Option<PathBuf>) {
if mount.clone().flags(MountFlags::MS_REMOUNT) == *mount {
return (None, None);
}
let mountpoint = self.resolve(mount.get_target());
let (backing, source) = match mount.get_source().filter(|_| is_bind_mount(mount)) {
Some(source) => {
let source = self.resolve(source);
let recursive = mount.clone().flags(MountFlags::MS_REC) == *mount;
(self.backing_of(&source), recursive.then_some(source))
}
None => (Backing::Fresh, None),
};
self.layers.push((mountpoint.clone(), backing));
(Some(mountpoint), source)
}
}
fn installations_of(plan: &[PlannedMount]) -> Vec<Installation> {
let mut tree = InstallTimeTree::default();
plan.iter()
.map(|planned| {
let (mountpoint, source) = tree.install(&planned.mount);
Installation {
ids: Vec::new(),
mountpoints: mountpoint
.iter()
.map(|path| mountinfo_escape_path(path))
.collect(),
sources: source
.iter()
.map(|path| mountinfo_escape_path(path))
.collect(),
backend: planned.backend,
}
})
.collect()
}
fn mountinfo_field_is_canonical(field: &[u8]) -> bool {
field.first() == Some(&b'/')
&& field
.iter()
.all(|byte| !byte.is_ascii_control() && *byte != b' ')
}
struct UserBind {
target: PathBuf,
named: Vec<Vec<u8>>,
resolved: Vec<Vec<u8>>,
}
impl UserBind {
fn new(target: &Path, staging: Option<&Path>) -> Option<Self> {
let named = |path: &Path| {
path.is_absolute()
&& !path
.components()
.any(|component| component == std::path::Component::ParentDir)
};
if !named(target) || staging.is_some_and(|staging| !named(staging)) {
return None;
}
let target: PathBuf = target.components().collect();
if !mountinfo_field_is_canonical(&mountinfo_escape_path(&target)) {
return None;
}
let staging: Option<PathBuf> = staging.map(|staging| staging.components().collect());
let mut named = vec![mountinfo_escape_path(&target)];
named.extend(staging.as_deref().map(mountinfo_escape_path));
let mut resolved = vec![mountinfo_escape_path(&canonical_mount_target(&target))];
resolved.extend(
staging
.as_deref()
.map(|staging| mountinfo_escape_path(&canonical_mount_target(staging))),
);
resolved.retain(|mountpoint| !named.contains(mountpoint));
Some(Self {
target,
named,
resolved,
})
}
fn rows<'rows>(
&self,
rows: &'rows [MountRow],
host_rows: &[(Vec<u8>, Vec<u8>, Vec<u8>)],
named: bool,
) -> impl Iterator<Item = &'rows MountRow> {
let mountpoints = if named {
self.named.clone()
} else {
let mut resolved = self.resolved.clone();
if let Ok(target) = fs::canonicalize(&self.target) {
resolved.push(mountinfo_escape_path(&target));
}
resolved
};
let host_rows = host_rows.to_vec();
rows.iter().filter(move |row| {
mountpoints.contains(&row.mountpoint)
&& row.root != b"/"
&& !host_rows.iter().any(|(mountpoint, root, device)| {
*mountpoint == row.mountpoint && *root == row.root && *device == row.device
})
})
}
}
fn warn_unrewritten_bind(target: &Path) {
crate::tracing::diagnostic_eprintln!(
"WARNING: /proc/self/mountinfo shows the host source path of the bind at {}: \
Hermit could not identify its rows (https://github.com/rrnewton/hermit/issues/4266).",
target.display()
);
}
impl MountInfoRootSource {
fn new(source: &Path, target: &Path, deterministic_root: &[u8]) -> Result<Self, Error> {
Ok(Self {
source: Some(open_path(source).with_context(|| {
format!(
"Failed to pin mountinfo provenance source {}",
source.display()
)
})?),
target: target.to_path_buf(),
deterministic_root: Some(deterministic_root.to_vec()),
rewrite_descendant_roots: false,
raw_mountpoint_prefix: None,
deterministic_mountpoint_prefix: None,
})
}
fn private_tmp(source: &Path) -> Result<Self, Error> {
let mut provenance = Self::new(source, Path::new("/tmp"), DETERMINISTIC_TMP_ROOT)?;
provenance.rewrite_descendant_roots = true;
provenance.raw_mountpoint_prefix = Some(mountinfo_escape_path(source));
provenance.deterministic_mountpoint_prefix = Some(b"/tmp".to_vec());
Ok(provenance)
}
fn translated_tmp_mountpoint(staging_path: &Path) -> Self {
Self {
source: None,
target: PathBuf::from("/tmp"),
deterministic_root: None,
rewrite_descendant_roots: false,
raw_mountpoint_prefix: Some(mountinfo_escape_path(staging_path)),
deterministic_mountpoint_prefix: Some(b"/tmp".to_vec()),
}
}
fn resolve(&self) -> Result<MountInfoRootRewrite, Error> {
let target = open_path(&self.target).with_context(|| {
format!(
"Failed to pin mountinfo provenance target {}",
self.target.display()
)
})?;
let target_metadata = target.metadata().with_context(|| {
format!(
"Failed to inspect pinned mountinfo provenance target {}",
self.target.display()
)
})?;
if let Some(source) = &self.source {
let source_metadata = source.metadata().with_context(|| {
format!(
"Failed to inspect pinned mountinfo provenance source for {}",
self.target.display()
)
})?;
if (source_metadata.dev(), source_metadata.ino())
!= (target_metadata.dev(), target_metadata.ino())
{
return Err(Error::msg(format!(
"Refusing mountinfo provenance for {}: the completed mount target does not match \
the pinned Hermit-owned source",
self.target.display()
)));
}
}
let raw_mount_id = mount_id_for_fd(target.as_raw_fd()).with_context(|| {
format!(
"Failed to identify the mount containing proven target {}",
self.target.display()
)
})?;
let observed_root = (self.rewrite_descendant_roots || self.deterministic_root.is_none())
.then(|| mountinfo_root_for_id(raw_mount_id))
.transpose()
.with_context(|| {
format!(
"Failed to identify the proven mount root for {}",
self.target.display()
)
})?;
let deterministic_root = self
.deterministic_root
.clone()
.or_else(|| observed_root.clone())
.expect("an explicit or observed root is required");
Ok(MountInfoRootRewrite {
raw_mount_id,
deterministic_root: deterministic_root.clone(),
raw_root_prefix: self.rewrite_descendant_roots.then(|| {
observed_root
.clone()
.expect("descendant rewriting captured the observed root")
}),
deterministic_root_prefix: self.rewrite_descendant_roots.then_some(deterministic_root),
raw_mountpoint_prefix: self.raw_mountpoint_prefix.clone(),
deterministic_mountpoint_prefix: self.deterministic_mountpoint_prefix.clone(),
})
}
}
pub(super) struct IdentityGuard {
_group_file: Option<tempfile::NamedTempFile>,
_nscd_dir: Option<tempfile::TempDir>,
mountinfo_roots: Vec<MountInfoRootSource>,
user_mount_targets: Vec<PathBuf>,
user_binds: Vec<UserBind>,
host_rows: Vec<(Vec<u8>, Vec<u8>, Vec<u8>)>,
plan: MountPlan,
}
#[derive(Clone, Debug)]
pub(super) struct PlannedMount {
pub(super) mount: Mount,
pub(super) backend: bool,
}
impl PlannedMount {
pub(super) fn hermit(mount: Mount) -> Self {
Self {
mount,
backend: false,
}
}
pub(super) fn backend(mount: Mount) -> Self {
Self {
mount,
backend: true,
}
}
}
#[derive(Debug, Default)]
enum MountPlan {
#[default]
Unrecorded,
Recorded(
Vec<PlannedMount>,
Vec<Installation>,
Option<Vec<detcore_model::container_mounts::ContainerMount>>,
),
ImageRoot,
}
impl IdentityGuard {
pub(super) fn empty() -> Self {
Self {
_group_file: None,
_nscd_dir: None,
mountinfo_roots: Vec::new(),
user_mount_targets: Vec::new(),
user_binds: Vec::new(),
host_rows: Vec::new(),
plan: MountPlan::Unrecorded,
}
}
pub(super) fn set_plan(
&mut self,
container: &mut Container,
plan: Vec<PlannedMount>,
) -> Result<(), Error> {
if plan.first().map(|planned| &planned.mount) != Some(&container_proc_mount()) {
anyhow::bail!("a container's mount plan must begin with its own /proc");
}
container.mounts(plan.iter().map(|planned| planned.mount.clone()));
let installations = installations_of(&plan);
self.plan = MountPlan::Recorded(plan, installations, None);
Ok(())
}
pub(super) fn add_backend_mounts(
&mut self,
container: &mut Container,
mounts: impl IntoIterator<Item = Mount>,
) -> Result<(), Error> {
let MountPlan::Recorded(plan, installations, None) = &mut self.plan else {
anyhow::bail!("a backend's own mounts need the container's mount plan");
};
for mount in mounts {
container.mount(mount.clone());
plan.push(PlannedMount::backend(mount));
}
*installations = installations_of(plan);
Ok(())
}
pub(super) fn set_image_root(&mut self) {
self.plan = MountPlan::ImageRoot;
}
pub(super) fn record_plan(&mut self) -> Result<(), Error> {
let MountPlan::Recorded(_, installations, recorded) = &mut self.plan else {
return Ok(());
};
if recorded.is_some() {
return Ok(());
}
let rows = namespace_rows()?;
if !assign_installed_rows(&rows, installations) {
anyhow::bail!(
"the container's mount table has no row for the plan's first mount, its /proc, \
so the plan's rows cannot be told from the host's"
);
}
check_installed_rows(&rows, installations, is_this_namespaces_proc)
.map_err(|refusal| anyhow!("refusing the container's mount table: {refusal}"))?;
*recorded = Some(rows);
Ok(())
}
#[cfg(feature = "native-exec")]
pub(super) fn forget_plan(&mut self) {
self.plan = MountPlan::Unrecorded;
}
pub(super) fn container_mount_view(&self) -> Result<ContainerMountView, Error> {
let namespace = hermit::current_mount_namespace()?;
let contents = fs::read("/proc/thread-self/mountinfo")
.context("Failed to read the guest mount table for its identity order")?;
let table = match &self.plan {
MountPlan::Unrecorded => None,
_ => {
let rows = detcore_model::container_mounts::parse_container_mounts(&contents)
.ok_or_else(|| anyhow!("the guest mount table has a malformed row"))?;
self.container_mount_table_of(rows)?
}
};
let order = hermit::mountinfo_identity_order_keeping_from_raw(
&contents,
&table
.as_ref()
.map(ContainerMountTable::shown_mount_ids)
.unwrap_or_default(),
)
.context("Failed to capture guest mountinfo identity order")?;
if hermit::current_mount_namespace()? != namespace {
anyhow::bail!("the guest mount table was read across a change of mount namespace");
}
let root_rewrites = rewrites_of_snapshot(self.mountinfo_root_rewrites()?, &order);
Ok(ContainerMountView {
table,
order,
namespace,
root_rewrites,
})
}
fn container_mount_table_of(
&self,
current: Vec<detcore_model::container_mounts::ContainerMount>,
) -> Result<Option<ContainerMountTable>, Error> {
let (rows, installed, backend) = match &self.plan {
MountPlan::Unrecorded => return Ok(None),
MountPlan::Recorded(_, _, None) => {
anyhow::bail!(
"the container mount table was asked for before the plan was recorded"
)
}
MountPlan::Recorded(_, installations, Some(rows)) => {
let (installed, backend) = owned_mounts(rows, installations);
(current, installed, backend)
}
MountPlan::ImageRoot => {
let installed = current.iter().map(|row| row.raw_mount_id).collect();
(current, installed, std::collections::BTreeSet::new())
}
};
let never = rows
.iter()
.filter(|row| row.is_ephemeral_host_seed() && !installed.contains(&row.raw_mount_id))
.map(|row| row.raw_mount_id)
.chain(backend)
.collect();
let base = hermit::base_mountpoint_mount_ids();
Ok(Some(ContainerMountTable::select(
&rows, &installed, &base, &never,
)))
}
pub(super) fn add_private_tmp(&mut self, source: &Path) -> Result<(), Error> {
self.mountinfo_roots
.push(MountInfoRootSource::private_tmp(source)?);
Ok(())
}
pub(super) fn add_user_bind(&mut self, target: &Path, staging: Option<&Path>) {
if self.user_binds.is_empty() {
match mount_rows() {
Ok(rows) => {
self.host_rows = rows
.into_iter()
.map(|row| (row.mountpoint, row.root, row.device))
.collect();
}
Err(_) => return warn_unrewritten_bind(target),
}
}
match UserBind::new(target, staging) {
Some(bind) => self.user_binds.push(bind),
None => warn_unrewritten_bind(target),
}
}
pub(super) fn add_translated_tmp_mountpoint(&mut self, staging_path: &Path) {
self.mountinfo_roots
.push(MountInfoRootSource::translated_tmp_mountpoint(staging_path));
}
pub(super) fn discard_mounts_shadowed_by(
&mut self,
mounts: &mut Vec<Mount>,
overriding_target: &Path,
) {
let resolution_changed = self
.user_mount_targets
.iter()
.any(|earlier| overriding_target.starts_with(earlier));
let resolved_override = if resolution_changed {
overriding_target.to_path_buf()
} else {
canonical_mount_target(overriding_target)
};
mounts.retain(|mount| {
!canonical_mount_target(mount.get_target()).starts_with(&resolved_override)
});
self.mountinfo_roots.retain(|source| {
!canonical_mount_target(&source.target).starts_with(&resolved_override)
});
self.user_mount_targets
.push(overriding_target.to_path_buf());
}
pub(super) fn mountinfo_root_rewrites(&self) -> Result<Vec<MountInfoRootRewrite>, Error> {
let mut rewrites = self
.mountinfo_roots
.iter()
.map(MountInfoRootSource::resolve)
.collect::<Result<Vec<_>, _>>()?;
if self.user_binds.is_empty() {
return Ok(rewrites);
}
let rows = mount_rows().context("Failed to read the guest mount table")?;
let mut found = std::collections::BTreeMap::<u64, Vec<u8>>::new();
for named in [true, false] {
for bind in &self.user_binds {
let root = mountinfo_escape_path(&bind.target);
for row in bind.rows(&rows, &self.host_rows, named) {
found.entry(row.id).or_insert_with(|| root.clone());
}
}
}
for bind in &self.user_binds {
let root = mountinfo_escape_path(&bind.target);
if !found.values().any(|found| *found == root) {
warn_unrewritten_bind(&bind.target);
}
}
for (raw_mount_id, deterministic_root) in found {
match rewrites
.iter_mut()
.find(|rewrite| rewrite.raw_mount_id == raw_mount_id)
{
Some(rewrite) if rewrite.deterministic_root.starts_with(b"/tmpvol/.hermit/") => {}
Some(rewrite) => rewrite.deterministic_root = deterministic_root,
None => rewrites.push(MountInfoRootRewrite {
raw_mount_id,
deterministic_root,
raw_root_prefix: None,
deterministic_root_prefix: None,
raw_mountpoint_prefix: None,
deterministic_mountpoint_prefix: None,
}),
}
}
Ok(rewrites)
}
pub(super) fn mount_provenance(&self) -> Result<hermit::MountProvenance, Error> {
let view = self.container_mount_view()?;
Ok(hermit::MountProvenance {
root_rewrites: view.root_rewrites,
container_mount_table: view.table,
mount_ids: Some(view.order),
})
}
}
pub(super) struct ContainerMountView {
pub(super) table: Option<ContainerMountTable>,
pub(super) order: Vec<u64>,
pub(super) namespace: detcore_model::config::MountNamespaceIdentity,
pub(super) root_rewrites: Vec<MountInfoRootRewrite>,
}
fn rewrites_of_snapshot(
rewrites: Vec<MountInfoRootRewrite>,
order: &[u64],
) -> Vec<MountInfoRootRewrite> {
rewrites
.into_iter()
.filter(|rewrite| order.contains(&rewrite.raw_mount_id))
.collect()
}
pub(super) fn is_bind_mount(mount: &Mount) -> bool {
mount.clone().flags(MountFlags::MS_BIND) == *mount
}
fn canonical_mount_target(path: &Path) -> PathBuf {
for ancestor in path.ancestors() {
if let Ok(mut canonical) = fs::canonicalize(ancestor)
&& let Ok(suffix) = path.strip_prefix(ancestor)
{
canonical.push(suffix);
return canonical;
}
}
path.to_path_buf()
}
#[cfg(test)]
pub(super) fn mount_target_is_shadowed(target: &Path, overriding_target: &Path) -> bool {
canonical_mount_target(target).starts_with(canonical_mount_target(overriding_target))
}
fn overflow_gid() -> String {
detcore::machine::MachineDescription::default()
.limits
.overflow_gid
.to_string()
}
fn frozen_group_file() -> Result<tempfile::NamedTempFile, Error> {
let overflow_gid = overflow_gid();
let mut contents = fs::read_to_string(GROUP_FILE)
.context("Failed to read the host group database for the guest")?;
let has_overflow_group = contents.lines().any(|line| {
line.split(':')
.nth(2)
.is_some_and(|gid| gid == overflow_gid)
});
if !has_overflow_group {
if !contents.ends_with('\n') {
contents.push('\n');
}
contents.push_str("nobody:x:");
contents.push_str(&overflow_gid);
contents.push_str(":\n");
}
let mut group_file = tempfile::NamedTempFile::new()
.context("Failed to create the frozen group database for the guest")?;
group_file
.write_all(contents.as_bytes())
.context("Failed to populate the frozen group database for the guest")?;
group_file
.as_file()
.set_permissions(fs::Permissions::from_mode(0o644))
.context("Failed to set permissions on the frozen guest group database")?;
Ok(group_file)
}
pub(super) fn identity_hardening_mounts() -> Result<(Vec<Mount>, IdentityGuard), Error> {
let group_file = frozen_group_file()?;
let mut mountinfo_roots = vec![MountInfoRootSource::new(
group_file.path(),
Path::new(GROUP_FILE),
DETERMINISTIC_GROUP_ROOT,
)?];
let mut mounts = vec![Mount::bind(group_file.path(), GROUP_FILE).readonly()];
let nscd_dir = if Path::new(NSCD_DIR).is_dir() {
let directory =
tempfile::TempDir::new().context("Failed to create the empty guest nscd directory")?;
let target = canonical_mount_target(Path::new(NSCD_DIR));
mountinfo_roots.push(MountInfoRootSource::new(
directory.path(),
&target,
DETERMINISTIC_NSCD_ROOT,
)?);
mounts.push(Mount::bind(directory.path(), target).readonly());
Some(directory)
} else {
None
};
Ok((
mounts,
IdentityGuard {
_group_file: Some(group_file),
_nscd_dir: nscd_dir,
mountinfo_roots,
user_mount_targets: Vec::new(),
user_binds: Vec::new(),
host_rows: Vec::new(),
plan: MountPlan::Unrecorded,
},
))
}
fn choose_affinity_core(allowed: &[usize]) -> Option<usize> {
(!allowed.is_empty()).then(|| allowed[rand::random_range(0..allowed.len())])
}
fn cpu_ids(affinity: &CpuSet) -> Vec<usize> {
(0..CpuSet::count())
.filter(|cpu| affinity.is_set(*cpu).unwrap_or(false))
.collect()
}
pub(super) fn apply_affinity(container: &mut Container, pin_threads: bool) {
if pin_threads {
let affinity = sched_getaffinity(Pid::from_raw(0))
.expect("failed to query the tracer's allowed CPU affinity mask");
let allowed = cpu_ids(&affinity);
let rand_core = choose_affinity_core(&allowed)
.expect("the tracer's allowed CPU affinity mask is empty");
tracing::info!("Pinning tracer and guest threads to core {}", rand_core);
container.affinity(rand_core);
}
}
pub fn default_container(pin_threads: bool) -> Container {
let mut container = namespace_container(pin_threads);
container.mount(container_proc_mount());
container
}
pub(super) fn namespace_container(pin_threads: bool) -> Container {
let mut container = Container::new();
container
.unshare(Namespace::PID)
.map_root()
.hostname("hermetic-container.local")
.domainname("local")
.mount(Mount::new("/").rprivate());
apply_affinity(&mut container, pin_threads);
container
}
pub(super) fn container_proc_mount() -> Mount {
Mount::proc().allow_readonly_fallback()
}
pub(super) fn image_container(
rootfs: &Path,
tmpfs: &Path,
pin_threads: bool,
private_tmp: bool,
) -> Result<(Container, IdentityGuard), Error> {
let mut container = Container::new();
container
.unshare(Namespace::PID)
.map_root()
.hostname("hermetic-container.local")
.domainname("local")
.mount(Mount::new("/").rprivate());
container.mount(Mount::bind(rootfs, rootfs).readonly());
container.mount(Mount::bind(tmpfs, rootfs.join("tmp")).rshared());
let proc_target = rootfs.join("proc");
container.mount(Mount::proc().allow_readonly_fallback().target(&proc_target));
let dev_root = rootfs.join("dev");
for node in crate::image::DEV_BIND_TARGETS {
container.mount(Mount::bind(
Path::new("/dev").join(node),
dev_root.join(node),
));
}
container.mount(
Mount::devpts(dev_root.join("pts"))
.data("newinstance,ptmxmode=0666")
.flags(MountFlags::MS_NOSUID | MountFlags::MS_NOEXEC),
);
container.chroot(rootfs);
apply_affinity(&mut container, pin_threads);
let mut identity_sources = IdentityGuard::empty();
if private_tmp {
identity_sources.add_private_tmp(tmpfs)?;
}
identity_sources.set_image_root();
Ok((container, identity_sources))
}
#[cfg(feature = "native-exec")]
pub(super) mod declared_inputs {
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::ffi::CString;
use std::fs::File;
use std::os::fd::AsFd;
use std::os::fd::AsRawFd;
use std::os::fd::BorrowedFd;
use std::os::fd::FromRawFd;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::FileExt;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use std::sync::Arc;
use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use reverie::process::Container;
use reverie::process::Mount;
use reverie::process::MountFlags;
use reverie::process::PinnedMountSource;
use sha2::Digest;
use sha2::Sha256;
use super::super::native_exec_inputs::CapturedIdentity;
use super::super::native_exec_inputs::GuestPath;
use super::super::native_exec_inputs::HeldDynamicInputs;
use super::super::native_exec_inputs::InputRole;
pub(crate) const STOCK_LOADER_PATH: &[u8] = b"/run/hermit/native/stock-loader";
const SEALS: i32 = libc::F_SEAL_SEAL
| libc::F_SEAL_SHRINK
| libc::F_SEAL_GROW
| libc::F_SEAL_WRITE
| libc::F_SEAL_FUTURE_WRITE
| 0x0020; const ROOT_ST_RELATIME: libc::c_ulong = 0x1000;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FilePlacement {
Additional,
InterpreterReplacement,
}
pub(crate) struct RuntimeFile {
path: GuestPath,
file: File,
identity: CapturedIdentity,
executable: bool,
placement: FilePlacement,
}
impl RuntimeFile {
pub(crate) fn new(
path: GuestPath,
descriptor: BorrowedFd<'_>,
executable: bool,
placement: FilePlacement,
) -> Result<Self> {
let fd = unsafe { libc::fcntl(descriptor.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 3) };
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
let file = unsafe { File::from_raw_fd(fd) };
let metadata = file.metadata()?;
if !metadata.is_file()
|| metadata.len() > 64 * 1024 * 1024
|| metadata.mode() & 0o6000 != 0
|| unsafe { libc::fcntl(fd, libc::F_GET_SEALS) } != SEALS
|| (metadata.mode() & 0o111 != 0) != executable
{
bail!("runtime root file is not an exact sealed object with the requested mode");
}
use std::os::unix::fs::FileExt;
let mut bytes = vec![0; usize::try_from(metadata.len())?];
file.read_exact_at(&mut bytes, 0)?;
Ok(Self {
path,
file,
identity: CapturedIdentity {
device: metadata.dev(),
inode: metadata.ino(),
size: metadata.len(),
digest: Sha256::digest(&bytes).into(),
},
executable,
placement,
})
}
pub(crate) fn try_clone(&self) -> Result<Self> {
Ok(Self {
path: self.path.clone(),
file: self.file.try_clone()?,
identity: self.identity,
executable: self.executable,
placement: self.placement,
})
}
pub(crate) fn path(&self) -> &GuestPath {
&self.path
}
pub(crate) fn identity(&self) -> CapturedIdentity {
self.identity
}
pub(crate) fn executable(&self) -> bool {
self.executable
}
pub(crate) fn placement(&self) -> FilePlacement {
self.placement
}
pub(crate) fn descriptor(&self) -> BorrowedFd<'_> {
use std::os::fd::AsFd;
self.file.as_fd()
}
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub(crate) enum Endpoint {
Rpc,
Provision,
Statistics,
}
impl Endpoint {
fn path(self) -> &'static str {
match self {
Self::Rpc => "/run/rpc.sock",
Self::Provision => "/run/provision.sock",
Self::Statistics => "/run/stats.sock",
}
}
}
#[derive(Default)]
pub(crate) struct DeclaredRootAdditions {
files: Vec<RuntimeFile>,
endpoints: Vec<(Endpoint, PinnedMountSource, u64, u64)>,
common_group: Option<Arc<RuntimeFile>>,
}
impl DeclaredRootAdditions {
pub(crate) fn try_clone(&self) -> Result<Self> {
Ok(Self {
files: self
.files
.iter()
.map(RuntimeFile::try_clone)
.collect::<Result<_>>()?,
endpoints: self.endpoints.clone(),
common_group: self.common_group.as_ref().map(Arc::clone),
})
}
pub(crate) fn capture_common_inputs(&mut self) -> Result<()> {
if self.common_group.is_some() {
bail!("common identity inputs already captured");
}
let group = super::frozen_group_file()?;
let sealed = super::super::native_exec_inputs::seal_bytes(
&std::fs::read(group.path())?,
InputRole::Data,
)?;
self.common_group = Some(Arc::new(RuntimeFile::new(
GuestPath::new(b"/etc/group".to_vec())?,
sealed.as_fd(),
false,
FilePlacement::Additional,
)?));
Ok(())
}
pub(crate) fn common_group(&self) -> Result<&RuntimeFile> {
self.common_group
.as_deref()
.context("common identity input is absent")
}
pub(crate) fn adopt_common_group(&mut self, descriptor: BorrowedFd<'_>) -> Result<()> {
if self.common_group.is_some() {
bail!("common identity inputs already captured");
}
self.common_group = Some(Arc::new(RuntimeFile::new(
GuestPath::new(b"/etc/group".to_vec())?,
descriptor,
false,
FilePlacement::Additional,
)?));
Ok(())
}
pub(crate) fn add_file(&mut self, file: RuntimeFile) -> Result<()> {
if file.path.as_bytes() == b"/etc/group"
|| self.files.len() == 256
|| self.files.iter().any(|old| old.path == file.path)
{
bail!("duplicate or excessive runtime root files");
}
self.files.push(file);
Ok(())
}
pub(crate) fn add_endpoint(&mut self, kind: Endpoint, source: &Path) -> Result<()> {
if self.endpoints.iter().any(|(old, _, _, _)| *old == kind) {
bail!("duplicate protected endpoint");
}
let metadata = std::fs::symlink_metadata(source)?;
use std::os::unix::fs::FileTypeExt;
if !metadata.file_type().is_socket() {
bail!("protected endpoint is not a socket");
}
let pin =
PinnedMountSource::open(source).map_err(|error| anyhow::anyhow!("{error}"))?;
self.endpoints
.push((kind, pin, metadata.dev(), metadata.ino()));
Ok(())
}
}
pub(crate) struct DeclaredInputRoot {
inputs: Arc<HeldDynamicInputs>,
additions: DeclaredRootAdditions,
stage: tempfile::TempDir,
stage_parent: Option<File>,
stage_name: CString,
stage_pin: PinnedMountSource,
parent_namespace: (u64, u64),
stage_identity: (u64, u64),
configured: bool,
materialized: bool,
}
fn reserved(path: &Path) -> bool {
["/proc", "/dev", "/run", "/tmp", "/tool"]
.iter()
.any(|reserved| path.starts_with(reserved))
|| path == Path::new("/etc/group")
}
impl DeclaredInputRoot {
pub(crate) fn prepare(
inputs: Arc<HeldDynamicInputs>,
mut additions: DeclaredRootAdditions,
staging_parent: &Path,
) -> Result<Self> {
inputs.recheck()?;
for file in inputs.files() {
if reserved(file.path.as_path()) || reserved(file.resolved_path.as_path()) {
bail!("declared input collides with common runtime-owned paths");
}
}
for alias in inputs.aliases() {
if reserved(alias.path.as_path()) {
bail!("declared alias shadows a runtime-owned path");
}
if inputs
.resolve_path(&alias.path)
.is_ok_and(|target| reserved(target.as_path()))
{
bail!("declared alias redirects into runtime-owned paths");
}
}
let group = additions
.common_group
.as_ref()
.context("common identity inputs were not captured before verify")?;
let checked = RuntimeFile::new(
group.path.clone(),
group.descriptor(),
group.executable,
group.placement,
)?;
if checked.identity != group.identity
|| group.path.as_bytes() != b"/etc/group"
|| group.executable
|| group.placement != FilePlacement::Additional
|| additions.files.len() == 256
{
bail!("retained common identity object differs or root file bound exceeded");
}
additions.files.push(group.try_clone()?);
let mut endpoints = BTreeSet::new();
for file in inputs.files() {
endpoints.insert(file.resolved_path.clone());
}
for file in &additions.files {
let resolved = inputs.resolve_path(&file.path)?;
match file.placement {
FilePlacement::InterpreterReplacement => {
if !file.executable
|| !inputs.is_interpreter(&file.path)
|| !endpoints.remove(&resolved)
{
bail!("bootstrap replacement does not name an enrolled PT_INTERP");
}
}
FilePlacement::Additional => {}
}
if !endpoints.insert(resolved) {
bail!("runtime file collides with declared input");
}
}
for (endpoint, _, _, _) in &additions.endpoints {
if !endpoints.insert(GuestPath::new(endpoint.path().as_bytes().to_vec())?) {
bail!("protected endpoint collides with a runtime file");
}
}
for path in &endpoints {
if endpoints.iter().any(|other| {
other.as_bytes().len() > path.as_bytes().len()
&& other.as_bytes().starts_with(path.as_bytes())
&& other.as_bytes()[path.as_bytes().len()] == b'/'
}) {
bail!("root file shadows another endpoint parent");
}
}
for replacement in additions
.files
.iter()
.filter(|file| file.placement == FilePlacement::InterpreterReplacement)
{
let resolved = inputs.resolve_path(&replacement.path)?;
let original = inputs
.files()
.iter()
.find(|file| file.resolved_path == resolved && file.role == InputRole::Loader)
.context("original interpreter object missing")?;
if !additions.files.iter().any(|file| {
file.path.as_bytes() == STOCK_LOADER_PATH
&& file.placement == FilePlacement::Additional
&& file.executable
&& file.identity == original.identity
}) {
bail!("bootstrap replacement lacks the exact independently held stock loader");
}
}
let namespace = std::fs::metadata("/proc/self/ns/mnt")?;
let parent_namespace = (namespace.dev(), namespace.ino());
if !staging_parent.is_absolute() {
bail!("declared root staging parent must be absolute");
}
let stage = tempfile::Builder::new()
.prefix("hermit-declared-root-")
.tempdir_in(staging_parent)?;
let stage_parent = File::open(
stage
.path()
.parent()
.context("root staging parent missing")?,
)?;
let stage_name = CString::new(stage.path().file_name().unwrap().as_bytes())?;
let stage_pin =
PinnedMountSource::open(stage.path()).map_err(|e| anyhow::anyhow!("{e}"))?;
let stage_meta = std::fs::metadata(stage.path())?;
let stage_identity = (stage_meta.dev(), stage_meta.ino());
Ok(Self {
inputs,
additions,
stage,
stage_parent: Some(stage_parent),
stage_name,
stage_pin,
parent_namespace,
stage_identity,
configured: false,
materialized: false,
})
}
pub(crate) fn runtime_files(&self) -> &[RuntimeFile] {
&self.additions.files
}
pub(crate) fn configure(&mut self, container: &mut Container) -> Result<()> {
if self.configured {
bail!("declared root configured twice");
}
container.mount(Mount::bind_pinned(&self.stage_pin, self.stage.path()));
container.mount(
Mount::tmpfs(self.stage.path())
.flags(MountFlags::MS_NOSUID | MountFlags::MS_NODEV)
.data("mode=0755,size=384m"),
);
container.mount(
Mount::tmpfs(self.stage.path().join("tmp"))
.touch_target()
.flags(MountFlags::MS_NOSUID | MountFlags::MS_NODEV | MountFlags::MS_NOEXEC)
.data("mode=1777,size=64m"),
);
for (kind, source, _, _) in &self.additions.endpoints {
container.mount(
Mount::bind_pinned(source, self.stage.path().join(&kind.path()[1..]))
.readonly()
.flags(MountFlags::MS_NOSUID | MountFlags::MS_NODEV | MountFlags::MS_NOEXEC)
.touch_target(),
);
}
self.configured = true;
Ok(())
}
pub(crate) unsafe fn materialize(&mut self) -> Result<PreparedDeclaredRoot> {
if !self.configured || self.materialized {
bail!("declared root setup order or reuse");
}
self.materialized = true; let namespace = std::fs::metadata("/proc/self/ns/mnt")?;
if unsafe { libc::getpid() } <= 0
|| unsafe { libc::syscall(libc::SYS_gettid) }
!= i64::from(unsafe { libc::getpid() })
{
bail!("common root materialization requires the actual current main task");
}
if (namespace.dev(), namespace.ino()) == self.parent_namespace {
bail!("declared root was not installed in a fresh child mount namespace");
}
let held_parent = self
.stage_parent
.as_ref()
.context("child staging descriptor retired")?;
let parent_path = CString::new(
self.stage
.path()
.parent()
.context("staging parent missing")?
.as_os_str()
.as_bytes(),
)?;
let parent_fd = unsafe {
libc::open(
parent_path.as_ptr(),
libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if parent_fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
let parent = unsafe { File::from_raw_fd(parent_fd) };
let held = held_parent.metadata()?;
let actual = parent.metadata()?;
if (held.dev(), held.ino()) != (actual.dev(), actual.ino()) {
bail!("common mounts covered or replaced the declared staging parent");
}
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
self.stage_name.as_ptr(),
libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
let root = unsafe { File::from_raw_fd(fd) };
let mut fs = std::mem::MaybeUninit::<libc::statfs>::uninit();
if unsafe { libc::fstatfs(fd, fs.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
let root_meta = root.metadata()?;
if unsafe { fs.assume_init() }.f_type != libc::TMPFS_MAGIC
|| (root_meta.dev(), root_meta.ino()) == self.stage_identity
{
bail!("declared root is not the new child tmpfs");
}
self.inputs.recheck()?;
for (kind, _, device, inode) in &self.additions.endpoints {
let target = root_relative(&root, kind.path().as_bytes(), libc::O_PATH)?;
let actual = target.metadata()?;
use std::os::unix::fs::FileTypeExt;
if actual.dev() != *device
|| actual.ino() != *inode
|| !actual.file_type().is_socket()
{
bail!("bound protected endpoint changed from its parent-held object");
}
}
let mut copies = Vec::new();
for file in self.inputs.files() {
if self.additions.files.iter().any(|replacement| {
replacement.placement == FilePlacement::InterpreterReplacement
&& self.inputs.resolve_path(&replacement.path).ok().as_ref()
== Some(&file.resolved_path)
}) {
continue;
}
copies.push(RootCopy {
path: file.resolved_path.clone(),
source: File::from(file.descriptor().try_clone_to_owned()?),
executable: file.role != InputRole::Data,
expected: file.identity,
});
}
for file in &self.additions.files {
copies.push(RootCopy {
path: self.inputs.resolve_path(&file.path)?,
source: file.file.try_clone()?,
executable: file.executable,
expected: file.identity,
});
}
let copies = copy_root_groups(&root, copies)?;
let mut backings = BTreeMap::new();
for copy in copies {
let path = copy.request.path.clone();
let backing = bind_root_copy(&root, copy)?;
if backings.insert(path, backing).is_some() {
bail!("duplicate readonly root placement");
}
}
let proc_root = super::open_path(Path::new("/proc"))?;
let mut proc_fs = std::mem::MaybeUninit::<libc::statfs>::uninit();
if unsafe { libc::fstatfs(proc_root.as_raw_fd(), proc_fs.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
if unsafe { proc_fs.assume_init() }.f_type != libc::PROC_SUPER_MAGIC {
bail!("common proc source is not procfs");
}
let proc_mount = observed_mount_id(proc_root.as_fd())?;
let namespace_fd = super::open_path(Path::new("/proc/self/ns/mnt"))?;
for node in ["null", "zero", "full", "random", "urandom"] {
let source = super::open_path(Path::new(&format!("/dev/{node}")))?;
bind_fd(
&root,
&GuestPath::new(format!("/dev/{node}").into_bytes())?,
source.as_fd(),
false,
None,
)?;
}
mkdir_parents(&root, b"/run/nscd/empty")?;
for alias in self.inputs.aliases() {
create_link(&root, &alias.path, &alias.target)?;
}
let identity = root.metadata()?;
let mount_id = observed_mount_id(root.as_fd())?;
self.stage_pin
.retire()
.map_err(|e| anyhow::anyhow!("{e}"))?;
for (_, source, _, _) in &self.additions.endpoints {
source.retire().map_err(|e| anyhow::anyhow!("{e}"))?;
}
self.stage_parent.take(); self.materialized = true;
Ok(PreparedDeclaredRoot {
inputs: self.inputs.clone(),
backings,
root,
proc_root,
namespace_fd,
device: identity.dev(),
inode: identity.ino(),
mount_id,
proc_mount,
setup_pid: unsafe { libc::getpid() },
namespace: (namespace.dev(), namespace.ino()),
guest_prepared: false,
})
}
}
struct RootCopy {
path: GuestPath,
source: File,
expected: CapturedIdentity,
executable: bool,
}
struct CopiedRootFile {
request: RootCopy,
identity: CapturedIdentity,
links: u64,
}
#[derive(Clone)]
pub(crate) struct ReadonlyRootBacking(Arc<ReadonlyRootBackingOwner>);
struct ReadonlyRootBackingOwner {
source: File,
expected: CapturedIdentity,
source_mode: u32,
backing: File,
identity: CapturedIdentity,
mount_id: u64,
flags: libc::c_ulong,
executable: bool,
links: u64,
root: File,
root_identity: (u64, u64),
root_mount: u64,
namespace: File,
namespace_identity: (u64, u64),
path: GuestPath,
}
impl ReadonlyRootBacking {
pub(crate) fn source_identity(&self) -> CapturedIdentity {
self.0.expected
}
pub(crate) fn identity(&self) -> CapturedIdentity {
self.0.identity
}
#[cfg(test)]
pub(crate) fn mount_id(&self) -> u64 {
self.0.mount_id
}
pub(crate) fn descriptor(&self) -> BorrowedFd<'_> {
self.0.backing.as_fd()
}
pub(crate) fn executable(&self) -> bool {
self.0.executable
}
pub(crate) fn verify(&self) -> Result<()> {
let owner = &self.0;
let root_meta = owner.root.metadata()?;
let namespace = owner.namespace.metadata()?;
let current_namespace = std::fs::metadata("/proc/thread-self/ns/mnt")?;
let mut fs = std::mem::MaybeUninit::<libc::statfs>::uninit();
if unsafe { libc::fstatfs(owner.root.as_raw_fd(), fs.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
if (root_meta.dev(), root_meta.ino()) != owner.root_identity
|| (namespace.dev(), namespace.ino()) != owner.namespace_identity
|| (current_namespace.dev(), current_namespace.ino()) != owner.namespace_identity
|| observed_mount_id(owner.root.as_fd())? != owner.root_mount
|| unsafe { fs.assume_init() }.f_type != libc::TMPFS_MAGIC
|| checked_file_identity(&owner.source, owner.executable, true)? != owner.expected
|| owner.source.metadata()?.mode() != owner.source_mode
{
bail!("retained root backing source or root changed");
}
let current = open_root_file(&owner.root, owner.path.as_bytes())?;
for file in [&owner.backing, ¤t] {
if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GETFL) } & libc::O_ACCMODE
!= libc::O_RDONLY
|| checked_file_identity(file, owner.executable, false)? != owner.identity
|| file.metadata()?.nlink() != owner.links
|| observed_mount_id(file.as_fd())? != owner.mount_id
|| raw_statvfs(file.as_raw_fd())?.f_flag != owner.flags
{
bail!("readonly root backing bytes, path or mount changed");
}
}
let required = libc::ST_RDONLY | libc::ST_NOSUID | libc::ST_NODEV;
if owner.flags & required != required
|| (owner.flags & libc::ST_NOEXEC != 0) == owner.executable
{
bail!("readonly root backing role policy differs");
}
Ok(())
}
}
#[cfg(test)]
impl ReadonlyRootBackingOwner {
fn try_clone_for_test(&self) -> Result<Self> {
Ok(Self {
source: self.source.try_clone()?,
expected: self.expected,
source_mode: self.source_mode,
backing: self.backing.try_clone()?,
identity: self.identity,
mount_id: self.mount_id,
flags: self.flags,
executable: self.executable,
links: self.links,
root: self.root.try_clone()?,
root_identity: self.root_identity,
root_mount: self.root_mount,
namespace: self.namespace.try_clone()?,
namespace_identity: self.namespace_identity,
path: self.path.clone(),
})
}
}
fn checked_file_identity(
file: &File,
executable: bool,
sealed: bool,
) -> Result<CapturedIdentity> {
let before = file.metadata()?;
let mode = if executable { 0o555 } else { 0o444 };
if !before.is_file()
|| before.len() > 64 * 1024 * 1024
|| before.mode() & 0o6000 != 0
|| (before.mode() & 0o111 != 0) != executable
|| (!sealed && before.mode() & 0o7777 != mode)
|| (sealed && unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GET_SEALS) } != SEALS)
{
bail!("root copy file kind, size, mode or source seals differ");
}
let mut hash = Sha256::new();
let mut bytes = [0u8; 64 * 1024];
let mut offset = 0;
while offset < before.len() {
let count = usize::try_from((before.len() - offset).min(bytes.len() as u64))?;
file.read_exact_at(&mut bytes[..count], offset)?;
hash.update(&bytes[..count]);
offset += count as u64; }
if file.read_at(&mut bytes[..1], before.len())? != 0 {
bail!("root copy file grew during readback");
}
let after = file.metadata()?;
if (
before.dev(),
before.ino(),
before.len(),
before.mode(),
before.nlink(),
) != (
after.dev(),
after.ino(),
after.len(),
after.mode(),
after.nlink(),
) || (sealed && unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GET_SEALS) } != SEALS)
{
bail!("root copy file metadata changed during readback");
}
Ok(CapturedIdentity {
device: after.dev(),
inode: after.ino(),
size: after.len(),
digest: hash.finalize().into(),
})
}
fn path_leaf(path: &[u8]) -> Result<CString> {
Ok(CString::new(
path.rsplit(|byte| *byte == b'/')
.next()
.context("missing root leaf")?,
)?)
}
fn open_root_file(root: &File, path: &[u8]) -> Result<File> {
open_root_file_with_flags(root, path, libc::O_RDONLY)
}
fn open_root_file_with_flags(root: &File, path: &[u8], flags: i32) -> Result<File> {
let mut parent = root.try_clone()?;
let parts: Vec<_> = path[1..].split(|byte| *byte == b'/').collect();
for part in &parts[..parts.len() - 1] {
let component = CString::new(*part)?;
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
component.as_ptr(),
libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
parent = unsafe { File::from_raw_fd(fd) };
}
let leaf = path_leaf(path)?;
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
leaf.as_ptr(),
flags | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
Ok(unsafe { File::from_raw_fd(fd) })
}
fn open_new_root_file(root: &File, path: &[u8], mode: libc::mode_t) -> Result<File> {
let parent = mkdir_parents(root, path)?;
let leaf = path_leaf(path)?;
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
leaf.as_ptr(),
libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW | libc::O_CLOEXEC,
mode,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
let file = unsafe { File::from_raw_fd(fd) };
if unsafe { libc::fchmod(fd, mode) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
Ok(file)
}
fn copy_root_groups(root: &File, requests: Vec<RootCopy>) -> Result<Vec<CopiedRootFile>> {
let mut groups: BTreeMap<(u64, u64), Vec<RootCopy>> = BTreeMap::new();
let mut names = BTreeSet::new();
for request in requests {
if !names.insert(request.path.clone())
|| checked_file_identity(&request.source, request.executable, true)?
!= request.expected
{
bail!("duplicate root path or changed sealed copy source");
}
let key = (request.expected.device, request.expected.inode);
let group = groups.entry(key).or_default();
if let Some(first) = group.first()
&& (first.expected != request.expected
|| first.executable != request.executable
|| first.source.metadata()?.mode() != request.source.metadata()?.mode())
{
bail!("same-source root placements disagree about bytes or role");
}
group.push(request);
}
let mut copies = Vec::new();
for group in groups.into_values() {
let first = &group[0];
let mode = if first.executable { 0o555 } else { 0o444 };
let writable = open_new_root_file(root, first.path.as_bytes(), mode)?;
let mut bytes = [0u8; 64 * 1024];
let mut offset = 0;
while offset < first.expected.size {
let count =
usize::try_from((first.expected.size - offset).min(bytes.len() as u64))?;
first.source.read_exact_at(&mut bytes[..count], offset)?;
writable.write_all_at(&bytes[..count], offset)?;
offset += count as u64;
}
let readback = open_root_file(root, first.path.as_bytes())?;
let before_close = checked_file_identity(&readback, first.executable, false)?;
if before_close.size != first.expected.size
|| before_close.digest != first.expected.digest
|| checked_file_identity(&first.source, first.executable, true)? != first.expected
{
bail!("root copy differs before writer retirement");
}
drop(writable); let after_close = checked_file_identity(&readback, first.executable, false)?;
if after_close != before_close {
bail!("root copy changed at writer retirement");
}
let primary_parent = mkdir_parents(root, first.path.as_bytes())?;
let primary_leaf = path_leaf(first.path.as_bytes())?;
for other in &group[1..] {
let parent = mkdir_parents(root, other.path.as_bytes())?;
let leaf = path_leaf(other.path.as_bytes())?;
if unsafe {
libc::linkat(
primary_parent.as_raw_fd(),
primary_leaf.as_ptr(),
parent.as_raw_fd(),
leaf.as_ptr(),
0,
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
}
let links = u64::try_from(group.len())?;
for request in group {
let file = open_root_file(root, request.path.as_bytes())?;
if checked_file_identity(&file, request.executable, false)? != after_close
|| file.metadata()?.nlink() != links
|| checked_file_identity(&request.source, request.executable, true)?
!= request.expected
{
bail!("root hardlink or sealed source changed before binding");
}
copies.push(CopiedRootFile {
request,
identity: after_close,
links,
});
}
}
Ok(copies)
}
fn bind_root_copy(root: &File, copy: CopiedRootFile) -> Result<ReadonlyRootBacking> {
let request = copy.request;
let parent = mkdir_parents(root, request.path.as_bytes())?;
let leaf = path_leaf(request.path.as_bytes())?;
let source = open_root_file(root, request.path.as_bytes())?;
if checked_file_identity(&source, request.executable, false)? != copy.identity
|| source.metadata()?.nlink() != copy.links
{
bail!("root copy changed before self bind");
}
let source_flags = raw_statvfs(source.as_raw_fd())?.f_flag;
if source_flags & libc::ST_RDONLY != 0 || source_flags & libc::ST_NOEXEC != 0 {
bail!("new root copy is not on the fresh writable executable tmpfs");
}
let from = CString::new(format!("/proc/self/fd/{}", source.as_raw_fd()))?;
let mut target = format!("/proc/self/fd/{}/", parent.as_raw_fd()).into_bytes();
target.extend_from_slice(leaf.as_bytes());
let target = CString::new(target)?;
if unsafe {
libc::mount(
from.as_ptr(),
target.as_ptr(),
std::ptr::null(),
libc::MS_BIND,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
let mut mount_flags =
libc::MS_BIND | libc::MS_REMOUNT | libc::MS_RDONLY | libc::MS_NOSUID | libc::MS_NODEV;
let mut expected_flags = source_flags | libc::ST_RDONLY | libc::ST_NOSUID | libc::ST_NODEV;
if !request.executable {
mount_flags |= libc::MS_NOEXEC;
expected_flags |= libc::ST_NOEXEC;
}
for (observed, flag) in [
(libc::ST_NOATIME, libc::MS_NOATIME),
(libc::ST_NODIRATIME, libc::MS_NODIRATIME),
(ROOT_ST_RELATIME, libc::MS_RELATIME),
] {
if source_flags & observed != 0 {
mount_flags |= flag;
}
}
if unsafe {
libc::mount(
std::ptr::null(),
target.as_ptr(),
std::ptr::null(),
mount_flags,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
let backing = open_root_file(root, request.path.as_bytes())?;
let metadata = root.metadata()?;
let namespace = File::open("/proc/thread-self/ns/mnt")?;
let namespace_metadata = namespace.metadata()?;
let owner = ReadonlyRootBacking(Arc::new(ReadonlyRootBackingOwner {
source_mode: request.source.metadata()?.mode(),
source: request.source,
expected: request.expected,
identity: copy.identity,
mount_id: observed_mount_id(backing.as_fd())?,
flags: expected_flags,
backing,
executable: request.executable,
links: copy.links,
root: root.try_clone()?,
root_identity: (metadata.dev(), metadata.ino()),
root_mount: observed_mount_id(root.as_fd())?,
namespace,
namespace_identity: (namespace_metadata.dev(), namespace_metadata.ino()),
path: request.path,
}));
owner.verify()?;
Ok(owner)
}
pub(crate) struct PreparedDeclaredRoot {
inputs: Arc<HeldDynamicInputs>,
backings: BTreeMap<GuestPath, ReadonlyRootBacking>,
root: File,
proc_root: File,
namespace_fd: File,
device: u64,
inode: u64,
mount_id: u64,
proc_mount: u64,
setup_pid: i32,
namespace: (u64, u64),
guest_prepared: bool,
}
impl PreparedDeclaredRoot {
pub(crate) fn inputs(&self) -> &Arc<HeldDynamicInputs> {
&self.inputs
}
pub(crate) fn backing(
&self,
path: &GuestPath,
expected: CapturedIdentity,
) -> Result<&ReadonlyRootBacking> {
let backing = self
.backings
.get(path)
.context("unregistered root backing path")?;
if backing.source_identity() != expected {
bail!("root backing does not belong to the retained source");
}
backing.verify()?;
Ok(backing)
}
pub(crate) fn descriptor(&self) -> BorrowedFd<'_> {
self.root.as_fd()
}
pub(crate) fn device(&self) -> u64 {
self.device
}
pub(crate) fn inode(&self) -> u64 {
self.inode
}
pub(crate) fn mount_id(&self) -> u64 {
self.mount_id
}
pub(crate) fn namespace(&self) -> (u64, u64) {
self.namespace
}
pub(crate) fn prepare_guest_setup(&mut self, cwd: &Path) -> Result<GuestDeclaredRootSetup> {
if self.guest_prepared {
bail!("guest root setup prepared twice");
}
self.guest_prepared = true;
let cwd_bytes = cwd.as_os_str().as_bytes();
let mut working_directory = [0u8; 4096];
if cwd_bytes != b"/" {
let path = GuestPath::new(cwd_bytes.to_vec())?;
let resolved = self.inputs.resolve_path(&path)?;
let mut directory = resolved.as_bytes().to_vec();
directory.extend_from_slice(b"/.cwd-endpoint");
mkdir_parents(&self.root, &directory)?;
}
working_directory[..cwd_bytes.len()].copy_from_slice(cwd_bytes);
Ok(GuestDeclaredRootSetup {
root: Some(self.root.try_clone()?),
proc_root: Some(self.proc_root.try_clone()?),
namespace_fd: Some(self.namespace_fd.try_clone()?),
device: self.device,
inode: self.inode,
mount_id: self.mount_id,
proc_mount: self.proc_mount,
setup_pid: self.setup_pid,
namespace: self.namespace,
working_directory,
attempted: false,
})
}
}
pub(crate) struct GuestDeclaredRootSetup {
root: Option<File>,
proc_root: Option<File>,
namespace_fd: Option<File>,
device: u64,
inode: u64,
mount_id: u64,
proc_mount: u64,
setup_pid: i32,
namespace: (u64, u64),
working_directory: [u8; 4096],
attempted: bool,
}
impl GuestDeclaredRootSetup {
pub(crate) unsafe fn bind_and_enter(
&mut self,
) -> std::io::Result<MaterializedDeclaredRoot> {
if self.attempted {
return Err(raw_refusal());
}
self.attempted = true;
let root = self.root.as_ref().ok_or_else(raw_refusal)?.as_raw_fd();
let proc_root = self.proc_root.as_ref().ok_or_else(raw_refusal)?.as_raw_fd();
let namespace_fd = self
.namespace_fd
.as_ref()
.ok_or_else(raw_refusal)?
.as_raw_fd();
let pid = unsafe { libc::getpid() };
if pid <= 0
|| pid == self.setup_pid
|| unsafe { libc::syscall(libc::SYS_gettid) } != i64::from(pid)
{
return Err(raw_refusal());
}
let namespace = raw_stat(namespace_fd)?;
if (namespace.st_dev, namespace.st_ino) != self.namespace {
return Err(raw_refusal());
}
let current_namespace = unsafe {
libc::openat(
proc_root,
c"self/ns/mnt".as_ptr(),
libc::O_RDONLY | libc::O_CLOEXEC,
)
};
if current_namespace < 0 {
return Err(std::io::Error::last_os_error());
}
let current_namespace = unsafe { File::from_raw_fd(current_namespace) };
let current = raw_stat(current_namespace.as_raw_fd())?;
if (current.st_dev, current.st_ino) != self.namespace {
return Err(raw_refusal());
}
drop(current_namespace);
let root_stat = raw_stat(root)?;
let root_view = raw_statvfs(root)?;
if (root_stat.st_dev, root_stat.st_ino) != (self.device, self.inode)
|| raw_mount_id(root)? != self.mount_id
|| root_view.f_flag & libc::ST_RDONLY != 0
|| raw_mount_id(proc_root)? != self.proc_mount
{
return Err(raw_refusal());
}
let mut proc_fs = std::mem::MaybeUninit::<libc::statfs>::uninit();
if unsafe { libc::fstatfs(proc_root, proc_fs.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error());
}
if unsafe { proc_fs.assume_init() }.f_type != libc::PROC_SUPER_MAGIC {
return Err(raw_refusal());
}
let mut number = RawPath::new();
number.number(pid as u32)?;
let mut self_target = [0u8; 32];
let length = unsafe {
libc::readlinkat(
proc_root,
c"self".as_ptr(),
self_target.as_mut_ptr().cast(),
self_target.len(),
)
};
if length < 0 {
return Err(std::io::Error::last_os_error());
}
if length as usize != number.len
|| self_target[..length as usize] != number.bytes[..number.len]
{
return Err(raw_refusal());
}
raw_mkdir(root, c"proc".as_ptr(), true)?;
raw_mkdir(root, number.prefixed(b"proc/")?.as_ptr(), false)?;
let mut task = RawPath::new();
task.push(b"proc/")?;
task.push(number.as_bytes())?;
task.push(b"/task")?;
raw_mkdir(root, task.as_ptr(), false)?;
task.push(b"/")?;
task.push(number.as_bytes())?;
raw_mkdir(root, task.as_ptr(), false)?;
for leaf in [
b"maps".as_slice(),
b"smaps",
b"status",
b"auxv",
b"comm",
b"fd",
] {
let mut source = RawPath::new();
source.push(number.as_bytes())?;
source.push(b"/")?;
source.push(leaf)?;
let mut target = source.prefixed(b"proc/")?;
raw_bind_proc(
root,
proc_root,
&source,
&target,
leaf == b"fd",
self.proc_mount,
)?;
target = RawPath::new();
target.push(task.as_bytes())?;
target.push(b"/")?;
target.push(leaf)?;
raw_bind_proc(
root,
proc_root,
&source,
&target,
leaf == b"fd",
self.proc_mount,
)?;
}
if unsafe { libc::symlinkat(number.as_ptr(), root, c"proc/self".as_ptr()) } != 0 {
return Err(std::io::Error::last_os_error());
}
let mut thread = RawPath::new();
thread.push(number.as_bytes())?;
thread.push(b"/task/")?;
thread.push(number.as_bytes())?;
if unsafe { libc::symlinkat(thread.as_ptr(), root, c"proc/thread-self".as_ptr()) } != 0
{
return Err(std::io::Error::last_os_error());
}
let root_path = RawPath::descriptor(root)?;
if unsafe {
libc::mount(
std::ptr::null(),
root_path.as_ptr(),
std::ptr::null(),
libc::MS_REMOUNT | libc::MS_RDONLY | libc::MS_NOSUID | libc::MS_NODEV,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error());
}
if raw_statvfs(root)?.f_flag & libc::ST_RDONLY == 0
|| raw_mount_id(root)? != self.mount_id
{
return Err(raw_refusal());
}
let actual_ns = unsafe {
libc::openat(
proc_root,
c"self/ns/mnt".as_ptr(),
libc::O_RDONLY | libc::O_CLOEXEC,
)
};
if actual_ns < 0 {
return Err(std::io::Error::last_os_error());
}
let actual_ns = unsafe { File::from_raw_fd(actual_ns) };
let actual = raw_stat(actual_ns.as_raw_fd())?;
if (actual.st_dev, actual.st_ino) != self.namespace {
return Err(raw_refusal());
}
drop(actual_ns);
if unsafe { libc::fchdir(root) } != 0
|| unsafe { libc::chroot(c".".as_ptr()) } != 0
|| unsafe { libc::chdir(self.working_directory.as_ptr().cast()) } != 0
{
return Err(std::io::Error::last_os_error());
}
self.proc_root.take();
self.namespace_fd.take();
Ok(MaterializedDeclaredRoot {
root: self.root.take().ok_or_else(raw_refusal)?,
device: self.device,
inode: self.inode,
mount_id: self.mount_id,
visible_pid: pid,
namespace: self.namespace,
})
}
}
pub(crate) struct MaterializedDeclaredRoot {
root: File,
device: u64,
inode: u64,
mount_id: u64,
visible_pid: i32,
namespace: (u64, u64),
}
impl MaterializedDeclaredRoot {
pub(crate) fn device(&self) -> u64 {
self.device
}
pub(crate) fn inode(&self) -> u64 {
self.inode
}
pub(crate) fn mount_id(&self) -> u64 {
self.mount_id
}
pub(crate) fn visible_pid(&self) -> i32 {
self.visible_pid
}
pub(crate) fn namespace(&self) -> (u64, u64) {
self.namespace
}
pub(crate) fn descriptor(&self) -> BorrowedFd<'_> {
self.root.as_fd()
}
}
fn raw_refusal() -> std::io::Error {
std::io::Error::from_raw_os_error(libc::EINVAL)
}
fn raw_stat(fd: i32) -> std::io::Result<libc::stat> {
let mut value = std::mem::MaybeUninit::<libc::stat>::uninit();
if unsafe { libc::fstat(fd, value.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(unsafe { value.assume_init() })
}
fn raw_statvfs(fd: i32) -> std::io::Result<libc::statvfs> {
let mut value = std::mem::MaybeUninit::<libc::statvfs>::uninit();
if unsafe { libc::fstatvfs(fd, value.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(unsafe { value.assume_init() })
}
fn raw_mount_id(fd: i32) -> std::io::Result<u64> {
let mut value = std::mem::MaybeUninit::<libc::statx>::zeroed();
if unsafe {
libc::statx(
fd,
c"".as_ptr(),
libc::AT_EMPTY_PATH | libc::AT_SYMLINK_NOFOLLOW,
libc::STATX_MNT_ID,
value.as_mut_ptr(),
)
} != 0
{
return Err(std::io::Error::last_os_error());
}
let value = unsafe { value.assume_init() };
if value.stx_mask & libc::STATX_MNT_ID == 0 || value.stx_mnt_id == 0 {
return Err(raw_refusal());
}
Ok(value.stx_mnt_id)
}
struct RawPath {
bytes: [u8; 128],
len: usize,
}
impl RawPath {
fn new() -> Self {
Self {
bytes: [0; 128],
len: 0,
}
}
fn as_ptr(&self) -> *const libc::c_char {
self.bytes.as_ptr().cast()
}
fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.len]
}
fn push(&mut self, bytes: &[u8]) -> std::io::Result<()> {
if self.len + bytes.len() >= self.bytes.len() || bytes.contains(&0) {
return Err(raw_refusal());
}
self.bytes[self.len..self.len + bytes.len()].copy_from_slice(bytes);
self.len += bytes.len();
self.bytes[self.len] = 0;
Ok(())
}
fn number(&mut self, mut value: u32) -> std::io::Result<()> {
let mut digits = [0u8; 10];
let mut start = digits.len();
loop {
start -= 1;
digits[start] = b'0' + (value % 10) as u8;
value /= 10;
if value == 0 {
break;
}
}
self.push(&digits[start..])
}
fn prefixed(&self, prefix: &[u8]) -> std::io::Result<Self> {
let mut result = Self::new();
result.push(prefix)?;
result.push(self.as_bytes())?;
Ok(result)
}
fn descriptor(fd: i32) -> std::io::Result<Self> {
if fd < 0 {
return Err(raw_refusal());
}
let mut result = Self::new();
result.push(b"/proc/self/fd/")?;
result.number(fd as u32)?;
Ok(result)
}
}
fn raw_mkdir(
root: i32,
path: *const libc::c_char,
allow_existing: bool,
) -> std::io::Result<()> {
if unsafe { libc::mkdirat(root, path, 0o755) } == 0 {
return Ok(());
}
let error = std::io::Error::last_os_error();
if !allow_existing || error.raw_os_error() != Some(libc::EEXIST) {
return Err(error);
}
let fd = unsafe {
libc::openat(
root,
path,
libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
drop(unsafe { File::from_raw_fd(fd) });
Ok(())
}
fn raw_bind_proc(
root: i32,
proc_root: i32,
source_path: &RawPath,
target_path: &RawPath,
directory: bool,
mount_id: u64,
) -> std::io::Result<()> {
let source = unsafe {
libc::openat(
proc_root,
source_path.as_ptr(),
libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if source < 0 {
return Err(std::io::Error::last_os_error());
}
let source = unsafe { File::from_raw_fd(source) };
let metadata = raw_stat(source.as_raw_fd())?;
if raw_mount_id(source.as_raw_fd())? != mount_id
|| (metadata.st_mode & libc::S_IFMT)
!= if directory {
libc::S_IFDIR
} else {
libc::S_IFREG
}
{
return Err(raw_refusal());
}
if directory {
raw_mkdir(root, target_path.as_ptr(), false)?;
} else {
let target = unsafe {
libc::openat(
root,
target_path.as_ptr(),
libc::O_CREAT
| libc::O_EXCL
| libc::O_WRONLY
| libc::O_NOFOLLOW
| libc::O_CLOEXEC,
0o444,
)
};
if target < 0 {
return Err(std::io::Error::last_os_error());
}
drop(unsafe { File::from_raw_fd(target) });
}
let from = RawPath::descriptor(source.as_raw_fd())?;
let mut to = RawPath::descriptor(root)?;
to.push(b"/")?;
to.push(target_path.as_bytes())?;
if unsafe {
libc::mount(
from.as_ptr(),
to.as_ptr(),
std::ptr::null(),
libc::MS_BIND,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error());
}
let source_flags = raw_statvfs(source.as_raw_fd())?.f_flag;
let mut flags = libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_RDONLY
| libc::MS_NOSUID
| libc::MS_NODEV
| libc::MS_NOEXEC;
for (probe, flag) in [
(libc::ST_NOATIME, libc::MS_NOATIME),
(libc::ST_NODIRATIME, libc::MS_NODIRATIME),
] {
if source_flags & probe != 0 {
flags |= flag;
}
}
if unsafe {
libc::mount(
std::ptr::null(),
to.as_ptr(),
std::ptr::null(),
flags,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error());
}
let target = unsafe {
libc::openat(
root,
target_path.as_ptr(),
libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if target < 0 {
return Err(std::io::Error::last_os_error());
}
let target = unsafe { File::from_raw_fd(target) };
let actual = raw_stat(target.as_raw_fd())?;
let required = libc::ST_RDONLY | libc::ST_NOSUID | libc::ST_NODEV | libc::ST_NOEXEC;
if (actual.st_dev, actual.st_ino) != (metadata.st_dev, metadata.st_ino)
|| raw_statvfs(target.as_raw_fd())?.f_flag & required != required
{
return Err(raw_refusal());
}
Ok(())
}
fn observed_mount_id(descriptor: BorrowedFd<'_>) -> Result<u64> {
let mut observed = std::mem::MaybeUninit::<libc::statx>::zeroed();
if unsafe {
libc::statx(
descriptor.as_raw_fd(),
c"".as_ptr(),
libc::AT_EMPTY_PATH | libc::AT_SYMLINK_NOFOLLOW,
libc::STATX_MNT_ID,
observed.as_mut_ptr(),
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
let observed = unsafe { observed.assume_init() };
if observed.stx_mask & libc::STATX_MNT_ID == 0 || observed.stx_mnt_id == 0 {
bail!("kernel did not report an actual mount identity");
}
Ok(observed.stx_mnt_id)
}
fn root_relative(root: &File, path: &[u8], flags: i32) -> Result<File> {
let parent = mkdir_parents(root, path)?;
let name = CString::new(path.rsplit(|byte| *byte == b'/').next().unwrap())?;
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
name.as_ptr(),
flags | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
Ok(unsafe { File::from_raw_fd(fd) })
}
fn mkdir_parents(root: &File, path: &[u8]) -> Result<File> {
let mut parent = root.try_clone()?;
let parts: Vec<_> = path[1..].split(|byte| *byte == b'/').collect();
for part in &parts[..parts.len() - 1] {
let name = CString::new(*part)?;
if unsafe { libc::mkdirat(parent.as_raw_fd(), name.as_ptr(), 0o755) } != 0
&& std::io::Error::last_os_error().raw_os_error() != Some(libc::EEXIST)
{
return Err(std::io::Error::last_os_error().into());
}
let next = unsafe {
libc::openat(
parent.as_raw_fd(),
name.as_ptr(),
libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if next < 0 {
return Err(std::io::Error::last_os_error().into());
}
parent = unsafe { File::from_raw_fd(next) };
}
Ok(parent)
}
fn create_link(root: &File, path: &GuestPath, target: &[u8]) -> Result<()> {
let parent = mkdir_parents(root, path.as_bytes())?;
let name = CString::new(path.as_bytes().rsplit(|byte| *byte == b'/').next().unwrap())?;
let target = CString::new(target)?;
if unsafe { libc::symlinkat(target.as_ptr(), parent.as_raw_fd(), name.as_ptr()) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
Ok(())
}
fn bind_fd(
root: &File,
path: &GuestPath,
source: BorrowedFd<'_>,
executable: bool,
expected: Option<CapturedIdentity>,
) -> Result<()> {
let metadata = File::from(source.try_clone_to_owned()?).metadata()?;
let parent = mkdir_parents(root, path.as_bytes())?;
let name = CString::new(path.as_bytes().rsplit(|byte| *byte == b'/').next().unwrap())?;
if metadata.is_dir() {
if unsafe { libc::mkdirat(parent.as_raw_fd(), name.as_ptr(), 0o555) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
} else {
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
name.as_ptr(),
libc::O_CREAT
| libc::O_EXCL
| libc::O_WRONLY
| libc::O_CLOEXEC
| libc::O_NOFOLLOW,
0o444,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
drop(unsafe { File::from_raw_fd(fd) });
}
let from = CString::new(format!("/proc/self/fd/{}", source.as_raw_fd()))?;
let mut target = format!("/proc/self/fd/{}/", parent.as_raw_fd()).into_bytes();
target.extend_from_slice(name.as_bytes());
let target = CString::new(target)?;
if unsafe {
libc::mount(
from.as_ptr(),
target.as_ptr(),
std::ptr::null(),
libc::MS_BIND,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
let mut source_view = std::mem::MaybeUninit::<libc::statvfs>::uninit();
if unsafe { libc::fstatvfs(source.as_raw_fd(), source_view.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
let source_flags = unsafe { source_view.assume_init() }.f_flag;
use std::os::unix::fs::FileTypeExt;
let mut flags = libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_RDONLY
| libc::MS_NOSUID
| if metadata.file_type().is_char_device() {
0
} else {
libc::MS_NODEV
}
| if executable { 0 } else { libc::MS_NOEXEC };
for (probe, flag) in [
(libc::ST_NOSUID, libc::MS_NOSUID),
(libc::ST_NODEV, libc::MS_NODEV),
(libc::ST_NOEXEC, libc::MS_NOEXEC),
(libc::ST_NOATIME, libc::MS_NOATIME),
(libc::ST_NODIRATIME, libc::MS_NODIRATIME),
] {
if source_flags & probe != 0 {
flags |= flag;
}
}
if executable && flags & libc::MS_NOEXEC != 0 {
bail!("executable held source is on a noexec mount");
}
if unsafe {
libc::mount(
std::ptr::null(),
target.as_ptr(),
std::ptr::null(),
flags,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
let fd = unsafe {
libc::openat(
parent.as_raw_fd(),
name.as_ptr(),
libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
let actual = unsafe { File::from_raw_fd(fd) };
let actual_meta = actual.metadata()?;
let mut actual_view = std::mem::MaybeUninit::<libc::statvfs>::uninit();
if unsafe { libc::fstatvfs(actual.as_raw_fd(), actual_view.as_mut_ptr()) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
let actual_flags = unsafe { actual_view.assume_init() }.f_flag;
if actual_flags & libc::ST_RDONLY == 0
|| (actual_flags & libc::ST_NOEXEC != 0) == executable
{
bail!("bound root object mount policy differs");
}
if actual_meta.dev() != metadata.dev() || actual_meta.ino() != metadata.ino() {
bail!("bound root object identity changed");
}
if let Some(expected) = expected
&& (actual_meta.dev() != expected.device
|| actual_meta.ino() != expected.inode
|| actual_meta.len() != expected.size
|| unsafe { libc::fcntl(source.as_raw_fd(), libc::F_GET_SEALS) } != SEALS)
{
bail!("bound sealed object changed");
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::super::super::native_exec_inputs::OpenedGuestRoot;
use super::*;
fn requested_copy(path: &[u8], source: &File, executable: bool) -> RootCopy {
let source_identity = checked_file_identity(source, executable, true).unwrap();
RootCopy {
path: GuestPath::new(path.to_vec()).unwrap(),
source: source.try_clone().unwrap(),
expected: source_identity,
executable,
}
}
#[test]
fn readonly_root_copy_checks_real_seals_bytes_and_conflicting_roles() {
let source = super::super::super::native_exec_inputs::seal_bytes(
b"captured copy",
InputRole::Data,
)
.unwrap();
let root_dir = tempfile::tempdir().unwrap();
let root = File::open(root_dir.path()).unwrap();
let request = requested_copy(b"/input", &source, false);
let expected = request.expected;
let copied = copy_root_groups(&root, vec![request]).unwrap();
let copy = open_root_file(&root, b"/input").unwrap();
let identity = checked_file_identity(©, false, false).unwrap();
assert_eq!(identity.size, expected.size);
assert_eq!(identity.digest, expected.digest);
assert_ne!(
(identity.device, identity.inode),
(expected.device, expected.inode)
);
assert_eq!(copied.len(), 1);
assert_eq!(
unsafe { libc::fcntl(source.as_raw_fd(), libc::F_GET_SEALS) },
SEALS
);
assert_eq!(
source.write_at(b"X", 0).unwrap_err().raw_os_error(),
Some(libc::EPERM)
);
let mut wrong = requested_copy(b"/other", &source, false);
wrong.expected.digest[0] ^= 1;
assert!(copy_root_groups(&root, vec![wrong]).is_err());
let mut wrong = requested_copy(b"/role", &source, false);
wrong.executable = true;
assert!(copy_root_groups(&root, vec![wrong]).is_err());
let ordinary = tempfile::tempfile().unwrap();
ordinary.write_all_at(b"captured copy", 0).unwrap();
assert!(checked_file_identity(&ordinary, false, true).is_err());
copy.write_at(b"X", 0).unwrap_err(); assert_eq!(unsafe { libc::fchmod(copy.as_raw_fd(), 0o644) }, 0);
let writable = File::options()
.write(true)
.open(root_dir.path().join("input"))
.unwrap();
writable.write_all_at(b"X", 0).unwrap();
assert_eq!(unsafe { libc::fchmod(copy.as_raw_fd(), 0o444) }, 0);
assert_ne!(
checked_file_identity(©, false, false).unwrap().digest,
expected.digest
);
}
#[test]
fn actual_readonly_root_backings_group_aliases_and_refuse_changed_owners() {
let inputs = data_inputs();
let source =
super::super::super::native_exec_inputs::seal_bytes(b"same bytes", InputRole::Data)
.unwrap();
let distinct =
super::super::super::native_exec_inputs::seal_bytes(b"same bytes", InputRole::Data)
.unwrap();
let code = super::super::super::native_exec_inputs::seal_bytes(
b"executable bytes",
InputRole::Executable,
)
.unwrap();
let mut additions = DeclaredRootAdditions::default();
for (path, file) in [
(b"/a".as_slice(), &source),
(b"/b", &source),
(b"/c", &distinct),
] {
additions
.add_file(
RuntimeFile::new(
GuestPath::new(path.to_vec()).unwrap(),
file.as_fd(),
false,
FilePlacement::Additional,
)
.unwrap(),
)
.unwrap();
}
additions
.add_file(
RuntimeFile::new(
GuestPath::new(b"/exec".to_vec()).unwrap(),
code.as_fd(),
true,
FilePlacement::Additional,
)
.unwrap(),
)
.unwrap();
let expected = checked_file_identity(&source, false, true).unwrap();
let mut plan = root_plan(inputs, additions).unwrap();
let mut container = super::super::default_container(false);
plan.configure(&mut container).unwrap();
let result: Result<(), String> = container
.run(|| {
let mut work = || -> Result<()> {
let prepared = unsafe { plan.materialize()? };
let a_path = GuestPath::new(b"/a".to_vec())?;
let a = prepared.backing(&a_path, expected)?.clone();
let b = prepared.backing(&GuestPath::new(b"/b".to_vec())?, expected)?;
let c = prepared.backing(
&GuestPath::new(b"/c".to_vec())?,
checked_file_identity(&distinct, false, true)?,
)?;
assert_eq!(a.identity(), b.identity());
assert_ne!(a.identity().inode, c.identity().inode);
assert_eq!(a.identity().digest, c.identity().digest);
assert_ne!(a.mount_id(), b.mount_id());
for backing in [&a, b, c] {
backing.verify()?;
}
let alias = GuestPath::new(b"/alias".to_vec())?;
create_link(&prepared.root, &alias, b"a")?;
let alias_fd = unsafe {
libc::openat(
prepared.root.as_raw_fd(),
c"alias".as_ptr(),
libc::O_RDONLY | libc::O_CLOEXEC,
)
};
if alias_fd < 0 {
return Err(std::io::Error::last_os_error().into());
}
let alias_file = unsafe { File::from_raw_fd(alias_fd) };
assert_eq!(
checked_file_identity(&alias_file, false, false)?,
a.identity()
);
assert!(prepared.backing(&alias, expected).is_err());
assert!(open_root_file(&prepared.root, b"/alias").is_err());
let executable = prepared.backing(
&GuestPath::new(b"/exec".to_vec())?,
checked_file_identity(&code, true, true)?,
)?;
executable.verify()?;
assert!(executable.executable());
assert_eq!(
raw_statvfs(executable.descriptor().as_raw_fd())?.f_flag
& libc::ST_NOEXEC,
0
);
let flags = raw_statvfs(a.descriptor().as_raw_fd())?.f_flag;
assert_eq!(
flags
& (libc::ST_RDONLY
| libc::ST_NOSUID
| libc::ST_NODEV
| libc::ST_NOEXEC),
libc::ST_RDONLY | libc::ST_NOSUID | libc::ST_NODEV | libc::ST_NOEXEC
);
let mut wrong_source = expected;
wrong_source.inode ^= 1;
assert!(prepared.backing(&a_path, wrong_source).is_err());
assert!(
prepared
.backing(&GuestPath::new(b"/absent".to_vec())?, expected)
.is_err()
);
let readonly_write = a.0.backing.write_at(b"X", 0).unwrap_err();
assert_eq!(readonly_write.raw_os_error(), Some(libc::EBADF));
let mut wrong_mount = (*a.0).try_clone_for_test()?;
wrong_mount.mount_id ^= 1;
assert!(ReadonlyRootBacking(Arc::new(wrong_mount)).verify().is_err());
let mut wrong_namespace = (*a.0).try_clone_for_test()?;
wrong_namespace.namespace_identity.1 ^= 1;
assert!(
ReadonlyRootBacking(Arc::new(wrong_namespace))
.verify()
.is_err()
);
let parent = mkdir_parents(&prepared.root, b"/a")?;
let c_target =
CString::new(format!("/proc/self/fd/{}/c", parent.as_raw_fd()))?;
assert_eq!(
unsafe { libc::umount2(c_target.as_ptr(), libc::MNT_DETACH) },
0
);
assert_eq!(
unsafe { libc::unlinkat(parent.as_raw_fd(), c"c".as_ptr(), 0) },
0
);
let replacement = open_new_root_file(&prepared.root, b"/c", 0o444)?;
replacement.write_all_at(b"same bytes", 0)?;
drop(replacement);
assert!(c.verify().is_err()); a.verify()?; let target =
CString::new(format!("/proc/self/fd/{}/a", parent.as_raw_fd()))?;
let mut atime_flags = 0;
for (observed, flag) in [
(libc::ST_NOATIME, libc::MS_NOATIME),
(libc::ST_NODIRATIME, libc::MS_NODIRATIME),
(ROOT_ST_RELATIME, libc::MS_RELATIME),
] {
if flags & observed != 0 {
atime_flags |= flag;
}
}
let remount = |flags| -> Result<()> {
if unsafe {
libc::mount(
std::ptr::null(),
target.as_ptr(),
std::ptr::null(),
flags | atime_flags,
std::ptr::null(),
)
} != 0
{
return Err(std::io::Error::last_os_error().into());
}
Ok(())
};
remount(
libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_RDONLY
| libc::MS_NOSUID
| libc::MS_NODEV,
)?;
assert!(a.verify().is_err()); remount(
libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_RDONLY
| libc::MS_NOSUID
| libc::MS_NODEV
| libc::MS_NOEXEC,
)?;
a.verify()?;
remount(
libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_NOSUID
| libc::MS_NODEV
| libc::MS_NOEXEC,
)?;
assert!(a.verify().is_err());
let writable =
open_root_file_with_flags(&prepared.root, b"/a", libc::O_WRONLY)?;
writable.write_all_at(b"X", 0)?;
drop(writable);
remount(
libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_RDONLY
| libc::MS_NOSUID
| libc::MS_NODEV
| libc::MS_NOEXEC,
)?;
assert!(a.verify().is_err()); assert!(b.verify().is_err()); remount(
libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_NOSUID
| libc::MS_NODEV
| libc::MS_NOEXEC,
)?;
let writable =
open_root_file_with_flags(&prepared.root, b"/a", libc::O_WRONLY)?;
writable.set_len(1)?;
drop(writable);
remount(
libc::MS_BIND
| libc::MS_REMOUNT
| libc::MS_RDONLY
| libc::MS_NOSUID
| libc::MS_NODEV
| libc::MS_NOEXEC,
)?;
assert!(a.verify().is_err()); Ok(())
};
work().map_err(|error| format!("{error:#}"))
})
.unwrap();
result.unwrap();
}
fn root_plan(
inputs: Arc<HeldDynamicInputs>,
mut additions: DeclaredRootAdditions,
) -> Result<DeclaredInputRoot> {
if additions.common_group.is_none() {
additions.capture_common_inputs()?;
}
DeclaredInputRoot::prepare(inputs, additions, &std::env::temp_dir())
}
fn data_inputs() -> Arc<HeldDynamicInputs> {
let source = tempfile::tempdir().unwrap();
std::fs::write(source.path().join("input"), b"sealed input").unwrap();
let opened = OpenedGuestRoot::open(source.path()).unwrap();
Arc::new(
HeldDynamicInputs::capture_data_control(
&opened,
GuestPath::new(b"/input".to_vec()).unwrap(),
)
.unwrap(),
)
}
#[test]
fn verify_plans_retain_one_actual_common_group_owner() {
let inputs = data_inputs();
assert!(
DeclaredInputRoot::prepare(
inputs.clone(),
DeclaredRootAdditions::default(),
&std::env::temp_dir(),
)
.is_err()
);
let mut additions = DeclaredRootAdditions::default();
additions.capture_common_inputs().unwrap();
assert!(additions.capture_common_inputs().is_err());
let second_additions = additions.try_clone().unwrap();
assert!(Arc::ptr_eq(
additions.common_group.as_ref().unwrap(),
second_additions.common_group.as_ref().unwrap(),
));
let first =
DeclaredInputRoot::prepare(inputs.clone(), additions, &std::env::temp_dir())
.unwrap();
let second =
DeclaredInputRoot::prepare(inputs, second_additions, &std::env::temp_dir())
.unwrap();
let group = |plan: &DeclaredInputRoot| {
plan.runtime_files()
.iter()
.find(|file| file.path().as_bytes() == b"/etc/group")
.unwrap()
.identity()
};
assert_eq!(group(&first), group(&second));
assert_ne!(first.stage.path(), second.stage.path());
assert_eq!(
unsafe {
libc::fcntl(
first
.additions
.common_group
.as_ref()
.unwrap()
.descriptor()
.as_raw_fd(),
libc::F_GET_SEALS,
)
},
SEALS
);
}
#[test]
fn runtime_additions_require_real_seals_and_exact_executable_mode() {
let ordinary = tempfile::NamedTempFile::new().unwrap();
assert!(
RuntimeFile::new(
GuestPath::new(b"/tool/config".to_vec()).unwrap(),
ordinary.as_file().as_fd(),
false,
FilePlacement::Additional
)
.is_err()
);
let sealed = super::super::super::native_exec_inputs::seal_bytes(
b"runtime bytes",
InputRole::Data,
)
.unwrap();
assert!(
RuntimeFile::new(
GuestPath::new(b"/tool/config".to_vec()).unwrap(),
sealed.as_fd(),
true,
FilePlacement::Additional
)
.is_err()
);
let actual = RuntimeFile::new(
GuestPath::new(b"/tool/config".to_vec()).unwrap(),
sealed.as_fd(),
false,
FilePlacement::Additional,
)
.unwrap();
let cloned = actual.try_clone().unwrap();
assert_eq!(actual.identity(), cloned.identity());
assert_ne!(
actual.descriptor().as_raw_fd(),
cloned.descriptor().as_raw_fd()
);
}
#[test]
fn root_plan_refuses_duplicate_files_and_unenrolled_interpreter_replacement() {
let inputs = data_inputs();
let sealed = super::super::super::native_exec_inputs::seal_bytes(
b"bootstrap",
InputRole::Executable,
)
.unwrap();
let replacement = RuntimeFile::new(
GuestPath::new(b"/loader".to_vec()).unwrap(),
sealed.as_fd(),
true,
FilePlacement::InterpreterReplacement,
)
.unwrap();
let mut additions = DeclaredRootAdditions::default();
additions.add_file(replacement).unwrap();
assert!(root_plan(inputs.clone(), additions).is_err());
let mut additions = DeclaredRootAdditions::default();
additions
.add_file(
RuntimeFile::new(
GuestPath::new(b"/input".to_vec()).unwrap(),
sealed.as_fd(),
true,
FilePlacement::Additional,
)
.unwrap(),
)
.unwrap();
assert!(root_plan(inputs, additions).is_err());
}
#[test]
fn root_setup_refuses_parent_materialization_and_duplicate_configuration() {
fn requires_send_sync<T: Send + Sync>() {}
requires_send_sync::<GuestDeclaredRootSetup>();
let mut plan = root_plan(data_inputs(), DeclaredRootAdditions::default()).unwrap();
let mut container = super::super::default_container(false);
assert!(unsafe { plan.materialize() }.is_err());
plan.configure(&mut container).unwrap();
assert!(plan.configure(&mut container).is_err());
assert!(unsafe { plan.materialize() }.is_err());
assert!(unsafe { plan.materialize() }.is_err());
}
#[test]
fn actual_native_endpoints_survive_declared_root_mount_namespace() {
use std::os::unix::fs::FileTypeExt;
use std::os::unix::net::UnixListener;
use super::super::super::native_exec_run::NativeEndpoints;
fn socket_view(path: &Path) -> (u64, u64, u32, libc::c_ulong) {
let path = CString::new(path.as_os_str().as_bytes()).unwrap();
let raw = unsafe {
libc::open(
path.as_ptr(),
libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
assert!(
raw >= 0,
"socket open failed: {}",
std::io::Error::last_os_error()
);
let fd = unsafe { File::from_raw_fd(raw) };
let metadata = fd.metadata().unwrap();
assert!(metadata.file_type().is_socket());
let mut view = std::mem::MaybeUninit::<libc::statvfs>::uninit();
assert_eq!(
unsafe { libc::fstatvfs(fd.as_raw_fd(), view.as_mut_ptr()) },
0
);
(
metadata.dev(),
metadata.ino(),
metadata.mode() & libc::S_IFMT,
unsafe { view.assume_init() }.f_flag,
)
}
let backing = tempfile::Builder::new()
.prefix(&format!("native-endpoint-{}", "x".repeat(120)))
.tempdir()
.unwrap();
let endpoints = NativeEndpoints::bind(backing.path()).unwrap();
let socket_directory = std::fs::read_dir(backing.path())
.unwrap()
.map(|entry| entry.unwrap().path())
.find(|path| {
path.file_name()
.unwrap()
.as_bytes()
.starts_with(b"native-sockets-")
})
.unwrap();
let rpc = socket_directory.join("rpc.sock");
let unix_address: libc::sockaddr_un = unsafe { std::mem::zeroed() };
assert!(rpc.as_os_str().as_bytes().len() >= unix_address.sun_path.len());
let short_stats = reverie_inguest::initial_image_stats::initial_image_stats_endpoint(
Path::new("/run/rpc.sock"),
)
.unwrap();
let stats = socket_directory.join(short_stats.file_name().unwrap());
let sources = [
(Endpoint::Rpc, rpc.clone()),
(Endpoint::Statistics, stats),
(Endpoint::Provision, socket_directory.join("provision.sock")),
];
let mut additions = DeclaredRootAdditions::default();
additions.capture_common_inputs().unwrap();
endpoints.add_to(&mut additions).unwrap();
assert_eq!(additions.endpoints.len(), 3);
let expected = sources.map(|(kind, source)| {
let identity = socket_view(&source);
let (_, _, device, inode) = additions
.endpoints
.iter()
.find(|(candidate, _, _, _)| *candidate == kind)
.unwrap();
assert_eq!((identity.0, identity.1), (*device, *inode));
(kind, identity)
});
let inputs = data_inputs();
let mut plan = DeclaredInputRoot::prepare(
inputs.clone(),
additions.try_clone().unwrap(),
backing.path(),
)
.unwrap();
let namespace = std::fs::metadata("/proc/self/ns/mnt").unwrap();
let parent_namespace = (namespace.dev(), namespace.ino());
let mut container = super::super::default_container(false);
plan.configure(&mut container).unwrap();
container
.run(|| {
let namespace = std::fs::metadata("/proc/self/ns/mnt").unwrap();
assert_ne!((namespace.dev(), namespace.ino()), parent_namespace);
for (kind, source) in expected {
let actual = socket_view(&plan.stage.path().join(&kind.path()[1..]));
assert_eq!(
(actual.0, actual.1, actual.2),
(source.0, source.1, source.2)
);
assert_eq!(
actual.3,
source.3
| libc::ST_RDONLY
| libc::ST_NOSUID
| libc::ST_NODEV
| libc::ST_NOEXEC,
);
}
for (_, pin, _, _) in &plan.additions.endpoints {
pin.retire().unwrap();
}
plan.stage_pin.retire().unwrap();
})
.unwrap();
let mut replaced =
DeclaredInputRoot::prepare(inputs, additions, backing.path()).unwrap();
std::fs::rename(&rpc, socket_directory.join("held-rpc.sock")).unwrap();
let directory = File::open(&socket_directory).unwrap();
let short_rpc = format!("/proc/self/fd/{}/rpc.sock", directory.as_raw_fd());
let _replacement = UnixListener::bind(&short_rpc).unwrap();
let mut container = super::super::default_container(false);
replaced.configure(&mut container).unwrap();
match container.run(|| -> () { panic!("replacement endpoint reached workload") }) {
Err(reverie::process::RunError::Spawn(error)) => {
assert_eq!(error.context(), reverie::process::Context::Mount);
assert_eq!(
std::io::Error::from(error).raw_os_error(),
Some(libc::ESTALE)
);
}
other => panic!("expected exact endpoint replacement refusal, received {other:?}"),
}
drop(endpoints);
}
#[test]
fn actual_common_container_materializes_readonly_data_and_same_task_proc() {
let inputs = data_inputs();
let mut plan = root_plan(inputs.clone(), DeclaredRootAdditions::default()).unwrap();
let mut container = super::super::default_container(false);
plan.configure(&mut container).unwrap();
let result: Result<(i32, i32, [u8; 8]), String> = container
.run(|| {
let mut work = || -> Result<_> {
let mut prepared = unsafe { plan.materialize()? };
assert_ne!(prepared.mount_id(), 0);
let setup_pid = unsafe { libc::getpid() };
let mut guest = prepared.prepare_guest_setup(Path::new("/work"))?;
assert!(prepared.prepare_guest_setup(Path::new("/work")).is_err());
let mut pair = [-1; 2];
if unsafe { libc::pipe2(pair.as_mut_ptr(), libc::O_CLOEXEC) } != 0 {
return Err(std::io::Error::last_os_error().into());
}
let read = unsafe { File::from_raw_fd(pair[0]) };
let write = unsafe { File::from_raw_fd(pair[1]) };
let pid = unsafe { libc::fork() };
if pid < 0 {
return Err(std::io::Error::last_os_error().into());
}
if pid == 0 {
let mut check = || -> std::io::Result<()> {
let receipt = unsafe { guest.bind_and_enter()? };
if receipt.visible_pid() != unsafe { libc::getpid() }
|| receipt.visible_pid() == setup_pid
|| receipt.namespace() != prepared.namespace()
|| receipt.device() != prepared.device()
|| receipt.inode() != prepared.inode()
|| receipt.mount_id() != prepared.mount_id()
{
return Err(raw_refusal());
}
assert_eq!(
unsafe { guest.bind_and_enter() }
.err()
.unwrap()
.raw_os_error(),
Some(libc::EINVAL)
);
let mut actual_cwd = [0u8; 32];
if unsafe {
libc::getcwd(actual_cwd.as_mut_ptr().cast(), actual_cwd.len())
}
.is_null()
|| &actual_cwd[..6] != b"/work\0"
{
return Err(raw_refusal());
}
let input = unsafe {
libc::open(c"/input".as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC)
};
if input < 0 {
return Err(std::io::Error::last_os_error());
}
let input = unsafe { File::from_raw_fd(input) };
let mut bytes = [0; 32];
let n = unsafe {
libc::read(
input.as_raw_fd(),
bytes.as_mut_ptr().cast(),
bytes.len(),
)
};
if n != 12 || &bytes[..12] != b"sealed input" {
return Err(raw_refusal());
}
let denied = unsafe {
libc::open(c"/input".as_ptr(), libc::O_WRONLY | libc::O_CLOEXEC)
};
if denied >= 0 {
unsafe {
libc::close(denied);
}
return Err(raw_refusal());
}
if std::io::Error::last_os_error().raw_os_error()
!= Some(libc::EROFS)
{
return Err(raw_refusal());
}
let scratch = unsafe {
libc::open(
c"/tmp/scratch".as_ptr(),
libc::O_WRONLY
| libc::O_CREAT
| libc::O_EXCL
| libc::O_CLOEXEC,
0o600,
)
};
if scratch < 0 {
return Err(std::io::Error::last_os_error());
}
let scratch = unsafe { File::from_raw_fd(scratch) };
if unsafe {
libc::write(scratch.as_raw_fd(), b"scratch".as_ptr().cast(), 7)
} != 7
{
return Err(raw_refusal());
}
let status = unsafe {
libc::open(
c"/proc/self/status".as_ptr(),
libc::O_RDONLY | libc::O_CLOEXEC,
)
};
if status < 0 {
return Err(std::io::Error::last_os_error());
}
let status = unsafe { File::from_raw_fd(status) };
let mut text = [0u8; 4096];
let n = unsafe {
libc::read(
status.as_raw_fd(),
text.as_mut_ptr().cast(),
text.len(),
)
};
if n <= 0 {
return Err(raw_refusal());
}
let mut expected = RawPath::new();
expected.push(b"Pid:\t")?;
expected.number(receipt.visible_pid() as u32)?;
expected.push(b"\n")?;
if !text[..n as usize]
.windows(expected.len)
.any(|row| row == expected.as_bytes())
{
return Err(raw_refusal());
}
for alias in [c"/proc/self/status", c"/proc/thread-self/status"] {
let fd = unsafe {
libc::open(alias.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC)
};
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
drop(unsafe { File::from_raw_fd(fd) });
}
let missing = unsafe {
libc::open(
c"/usr/bin/unlisted".as_ptr(),
libc::O_RDONLY | libc::O_CLOEXEC,
)
};
if missing >= 0 {
unsafe {
libc::close(missing);
}
return Err(raw_refusal());
}
if std::io::Error::last_os_error().raw_os_error()
!= Some(libc::ENOENT)
{
return Err(raw_refusal());
}
let mut packet = [0u8; 8];
packet[..4].copy_from_slice(&receipt.visible_pid().to_ne_bytes());
packet[4..].copy_from_slice(b"PASS");
if unsafe {
libc::write(
write.as_raw_fd(),
packet.as_ptr().cast(),
packet.len(),
)
} != packet.len() as isize
{
return Err(raw_refusal());
}
Ok(())
};
let code = if check().is_ok() { 0 } else { 96 };
unsafe {
libc::_exit(code);
}
}
drop(write);
let mut status = 0;
if unsafe { libc::waitpid(pid, &mut status, 0) } != pid || status != 0 {
bail!("actual guest root child {pid} failed: wait={status:#x}");
}
let mut packet = [0u8; 8];
use std::io::Read;
(&read).read_exact(&mut packet)?;
Ok((setup_pid, pid, packet))
};
work().map_err(|error| format!("{error:#}"))
})
.unwrap();
let (setup_pid, pid, packet) = result.unwrap();
assert_eq!(setup_pid, 1);
assert!(pid > setup_pid);
assert_eq!(i32::from_ne_bytes(packet[..4].try_into().unwrap()), pid);
assert_eq!(&packet[4..], b"PASS");
inputs.recheck().unwrap();
}
}
}
pub(super) fn deterministic_container() -> Result<(Container, IdentityGuard), Error> {
let mut container = default_container(true);
let (mounts, identity_guard) = identity_hardening_mounts()?;
container.mounts(mounts);
Ok((container, identity_guard))
}
fn arm_parent_death_signal() -> Result<(), Error> {
if unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) } == -1 {
return Err(std::io::Error::last_os_error()).context(
"Failed to arm the container init parent-death signal \
(prctl(PR_SET_PDEATHSIG, SIGKILL)); refusing to start a guest that \
an expiring deadline could not then kill",
);
}
Ok(())
}
const CONTAINER_INIT_STOP_SIGNALS: [Signal; 3] = [Signal::SIGTERM, Signal::SIGINT, Signal::SIGHUP];
extern "C" fn on_container_init_stop_signal(signal: libc::c_int) {
unsafe { libc::_exit(128 + signal) }
}
fn install_container_init_stop_handlers() -> Result<(), Error> {
let action = SigAction::new(
SigHandler::Handler(on_container_init_stop_signal),
SaFlags::empty(),
SigSet::empty(),
);
let mut unblock = SigSet::empty();
for signal in CONTAINER_INIT_STOP_SIGNALS {
unsafe { sigaction(signal, &action) }
.with_context(|| format!("Failed to install the container init {signal} handler"))?;
unblock.add(signal);
}
unblock
.thread_unblock()
.context("Failed to unblock the container init stop signals")?;
Ok(())
}
static STOP_SIGNAL_BRIDGE_HOLDER: AtomicI32 = AtomicI32::new(0);
extern "C" fn bridge_container_init_stop_signal(signal: libc::c_int) {
if unsafe { libc::getpid() } != STOP_SIGNAL_BRIDGE_HOLDER.load(Ordering::Relaxed) {
on_container_init_stop_signal(signal);
}
unsafe {
let mut default: libc::sigaction = std::mem::zeroed();
default.sa_sigaction = libc::SIG_DFL;
libc::sigaction(signal, &default, std::ptr::null_mut());
let mut unblock: libc::sigset_t = std::mem::zeroed();
libc::sigemptyset(&mut unblock);
libc::sigaddset(&mut unblock, signal);
libc::sigprocmask(libc::SIG_UNBLOCK, &unblock, std::ptr::null_mut());
libc::raise(signal);
}
}
fn current_signal_action(signal: Signal) -> Result<libc::sigaction, Error> {
let mut current: libc::sigaction = unsafe { std::mem::zeroed() };
if unsafe { libc::sigaction(signal as libc::c_int, std::ptr::null(), &mut current) } == -1 {
return Err(std::io::Error::last_os_error())
.with_context(|| format!("Failed to read the {signal} disposition"));
}
Ok(current)
}
#[must_use = "the bridge is removed when this guard is dropped"]
pub(super) struct ContainerInitStopSignalBridge {
bridged: [bool; CONTAINER_INIT_STOP_SIGNALS.len()],
handler: libc::sighandler_t,
}
impl ContainerInitStopSignalBridge {
pub(super) fn install() -> Result<Self, Error> {
let handler: extern "C" fn(libc::c_int) = bridge_container_init_stop_signal;
let mut guard = Self {
bridged: [false; CONTAINER_INIT_STOP_SIGNALS.len()],
handler: handler as libc::sighandler_t,
};
let holder = unsafe { libc::getpid() };
if holder == 1 {
return Ok(guard);
}
STOP_SIGNAL_BRIDGE_HOLDER.store(holder, Ordering::Relaxed);
let mut bridge: libc::sigaction = unsafe { std::mem::zeroed() };
bridge.sa_sigaction = guard.handler;
bridge.sa_flags = libc::SA_RESTART;
unsafe {
libc::sigemptyset(&mut bridge.sa_mask);
for signal in CONTAINER_INIT_STOP_SIGNALS {
libc::sigaddset(&mut bridge.sa_mask, signal as libc::c_int);
}
}
for (index, signal) in CONTAINER_INIT_STOP_SIGNALS.into_iter().enumerate() {
if current_signal_action(signal)?.sa_sigaction != libc::SIG_DFL {
continue;
}
if unsafe { libc::sigaction(signal as libc::c_int, &bridge, std::ptr::null_mut()) }
== -1
{
return Err(std::io::Error::last_os_error()).with_context(|| {
format!("Failed to install the container init {signal} bridge")
});
}
guard.bridged[index] = true;
}
Ok(guard)
}
}
impl Drop for ContainerInitStopSignalBridge {
fn drop(&mut self) {
let default = SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty());
for (index, signal) in CONTAINER_INIT_STOP_SIGNALS.into_iter().enumerate() {
if !self.bridged[index] {
continue;
}
let still_bridged = current_signal_action(signal)
.is_ok_and(|current| current.sa_sigaction == self.handler);
if still_bridged {
let _ = unsafe { sigaction(signal, &default) };
}
}
}
}
pub(super) fn stall_container_init_before_arming_if_asked() {
if std::env::var_os(CONTAINER_INIT_ARMING_STALL_ENV).as_deref()
!= Some(std::ffi::OsStr::new("1"))
{
return;
}
let mut name = [0u8; 16];
unsafe {
libc::prctl(libc::PR_GET_NAME, name.as_mut_ptr());
libc::prctl(libc::PR_SET_NAME, c"unarmed-init".as_ptr());
}
wait_for_parent_exit(CONTAINER_INIT_ARMING_STALL);
unsafe { libc::prctl(libc::PR_SET_NAME, name.as_ptr()) };
}
static PARENT_PIDFD: OnceLock<Option<(OwnedFd, u64, u64)>> = OnceLock::new();
pub(super) fn open_parent_pidfd_for_container_inits() {
PARENT_PIDFD.get_or_init(|| {
let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, libc::getpid(), 0) };
if fd < 0 {
return None;
}
let fd = unsafe { OwnedFd::from_raw_fd(fd as libc::c_int) };
let (dev, ino) = fd_identity(fd.as_raw_fd())?;
Some((fd, dev, ino))
});
}
fn fd_identity(fd: libc::c_int) -> Option<(u64, u64)> {
let mut stat: libc::stat = unsafe { std::mem::zeroed() };
(unsafe { libc::fstat(fd, &mut stat) } == 0).then_some((stat.st_dev, stat.st_ino))
}
fn inherited_parent_pidfd() -> Option<libc::c_int> {
let (fd, dev, ino) = PARENT_PIDFD.get()?.as_ref()?;
let fd = fd.as_raw_fd();
(fd_identity(fd) == Some((*dev, *ino))).then_some(fd)
}
fn wait_for_parent_exit(timeout: std::time::Duration) -> bool {
let Some(fd) = inherited_parent_pidfd() else {
let pause = libc::timespec {
tv_sec: timeout.as_secs() as libc::time_t,
tv_nsec: timeout.subsec_nanos() as libc::c_long,
};
unsafe { libc::nanosleep(&pause, std::ptr::null_mut()) };
return false;
};
let mut ready = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
let millis = libc::c_int::try_from(timeout.as_millis()).unwrap_or(libc::c_int::MAX);
(unsafe { libc::poll(&mut ready, 1, millis) } == 1) && ready.revents & libc::POLLIN != 0
}
pub(super) fn exit_if_parent_already_gone() {
if wait_for_parent_exit(std::time::Duration::ZERO) {
unsafe { libc::_exit(128 + libc::SIGKILL) }
}
}
pub(super) const CONTAINER_INIT_ARMING_STALL_ENV: &str =
"HERMIT_INTERNAL_STALL_CONTAINER_INIT_BEFORE_ARMING";
pub(super) const CONTAINER_INIT_ARMING_STALL: std::time::Duration =
std::time::Duration::from_secs(60);
static EXIT_AFTER_PUBLICATION: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
#[cfg(feature = "dbt")]
pub(super) fn exit_init_after_publication() {
EXIT_AFTER_PUBLICATION.store(true, Ordering::Release);
}
pub(super) fn exits_init_after_publication() -> bool {
EXIT_AFTER_PUBLICATION.load(Ordering::Acquire)
}
pub(super) fn arm_container_init_guards() -> Result<(), SerializableError> {
arm_parent_death_signal().map_err(SerializableError::from)?;
exit_if_parent_already_gone();
super::tracing::exit_if_log_cap_already_crossed();
install_container_init_stop_handlers().map_err(SerializableError::from)?;
Ok(())
}
pub(super) fn arm_namespace_only_guest() -> Result<(), reverie::Errno> {
if unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) } == -1 {
return Err(reverie::Errno::last());
}
exit_if_parent_already_gone();
super::tracing::exit_if_log_cap_already_crossed();
Ok(())
}
const I386_KILL: i32 = 37;
const AUDIT_ARCH_I386: u32 = 0x4000_0003;
pub(super) fn group_kill_filter() -> reverie::process::seccomp::Filter {
use reverie::process::seccomp::Action;
use reverie::process::seccomp::TargetArch;
reverie::process::seccomp::FilterBuilder::new()
.default_action(Action::Allow)
.syscall(reverie::syscalls::Sysno::kill, Action::Notify)
.alternate_arch(TargetArch::x86, Action::Notify)
.build()
}
fn is_kill(nr: i32, arch: u32) -> bool {
if arch == AUDIT_ARCH_I386 {
nr == I386_KILL
} else {
nr == libc::SYS_kill as i32
}
}
pub(super) fn refuses_group_kill(
args: [u64; 6],
caller_group: Option<i32>,
hermit_group: i32,
) -> bool {
args[0] as i32 == 0
&& (1..=libc::SIGRTMAX()).contains(&(args[1] as i32))
&& caller_group.is_none_or(|group| group == hermit_group)
}
fn process_group(pid: u32) -> Option<i32> {
let stat = fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
stat.rsplit_once(')')?
.1
.split_whitespace()
.nth(2)?
.parse()
.ok()
}
pub(super) fn install_group_kill_filter(
filter: &reverie::process::seccomp::Filter,
socket: i32,
) -> Result<(), reverie::Errno> {
let listener = match filter.load_and_listen() {
Ok(listener) => listener,
Err(errno) => {
let raw = errno.into_raw().to_le_bytes();
let record = [GROUP_KILL_FILTER_FAILED, raw[0], raw[1], raw[2], raw[3]];
unsafe { libc::send(socket, record.as_ptr().cast(), record.len(), 0) };
return Err(errno);
}
};
let fd = listener.as_raw_fd();
let mut byte = 0u8;
let mut iov = libc::iovec {
iov_base: (&mut byte as *mut u8).cast(),
iov_len: 1,
};
let mut control = [0u64; 4];
let mut message: libc::msghdr = unsafe { std::mem::zeroed() };
message.msg_iov = &mut iov;
message.msg_iovlen = 1;
message.msg_control = control.as_mut_ptr().cast();
message.msg_controllen =
unsafe { libc::CMSG_SPACE(std::mem::size_of::<i32>() as u32) } as usize;
let sent = unsafe {
let header = libc::CMSG_FIRSTHDR(&message);
(*header).cmsg_level = libc::SOL_SOCKET;
(*header).cmsg_type = libc::SCM_RIGHTS;
(*header).cmsg_len = libc::CMSG_LEN(std::mem::size_of::<i32>() as u32) as usize;
std::ptr::write(libc::CMSG_DATA(header).cast::<i32>(), fd);
libc::sendmsg(socket, &message, 0)
};
if sent != 1 {
return Err(reverie::Errno::last());
}
drop(listener);
Ok(())
}
const GROUP_KILL_FILTER_FAILED: u8 = b'F';
pub(super) fn group_kill_filter_failure(socket: &OwnedFd) -> Option<i32> {
let mut record = [0u8; 8];
let received = unsafe {
libc::recv(
socket.as_raw_fd(),
record.as_mut_ptr().cast(),
record.len(),
libc::MSG_DONTWAIT,
)
};
(received == 5 && record[0] == GROUP_KILL_FILTER_FAILED)
.then(|| i32::from_le_bytes([record[1], record[2], record[3], record[4]]))
}
pub(super) fn receive_group_kill_listener(socket: &OwnedFd) -> std::io::Result<OwnedFd> {
let mut byte = 0u8;
let mut iov = libc::iovec {
iov_base: (&mut byte as *mut u8).cast(),
iov_len: 1,
};
let mut control = [0u64; 4];
let mut message: libc::msghdr = unsafe { std::mem::zeroed() };
message.msg_iov = &mut iov;
message.msg_iovlen = 1;
message.msg_control = control.as_mut_ptr().cast();
message.msg_controllen = std::mem::size_of_val(&control);
let received = unsafe {
libc::recvmsg(
socket.as_raw_fd(),
&mut message,
libc::MSG_DONTWAIT | libc::MSG_CMSG_CLOEXEC,
)
};
if received != 1 {
return Err(if received < 0 {
std::io::Error::last_os_error()
} else {
std::io::Error::other("the guest sent no seccomp listener")
});
}
unsafe {
let header = libc::CMSG_FIRSTHDR(&message);
if header.is_null()
|| (*header).cmsg_level != libc::SOL_SOCKET
|| (*header).cmsg_type != libc::SCM_RIGHTS
{
return Err(std::io::Error::other(
"the guest's message carried no seccomp listener",
));
}
Ok(OwnedFd::from_raw_fd(std::ptr::read(
libc::CMSG_DATA(header).cast::<i32>(),
)))
}
}
pub(super) fn police_group_kills(listener: OwnedFd, stop: OwnedFd) {
use reverie::process::seccomp::SECCOMP_IOCTL_NOTIF_ID_VALID;
use reverie::process::seccomp::SECCOMP_IOCTL_NOTIF_RECV;
use reverie::process::seccomp::SECCOMP_IOCTL_NOTIF_SEND;
use reverie::process::seccomp::SECCOMP_USER_NOTIF_FLAG_CONTINUE;
use reverie::process::seccomp::seccomp_notif;
use reverie::process::seccomp::seccomp_notif_resp;
let hermit_group = unsafe { libc::getpgrp() };
let fd = listener.as_raw_fd();
loop {
let mut ready = [
libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
},
libc::pollfd {
fd: stop.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
},
];
if unsafe { libc::poll(ready.as_mut_ptr(), 2, -1) } < 0 {
let error = std::io::Error::last_os_error();
if error.raw_os_error() == Some(libc::EINTR) {
continue;
}
tracing::warn!(
"hermit stopped answering the --namespace-only guest's kill calls: polling \
their seccomp listener failed ({error}); each one now fails with ENOSYS \
(https://github.com/rrnewton/hermit/issues/4065)"
);
return;
}
if ready[1].revents != 0 || ready[0].revents & (libc::POLLHUP | libc::POLLERR) != 0 {
return;
}
if ready[0].revents & libc::POLLIN == 0 {
continue;
}
let mut notification: seccomp_notif = unsafe { std::mem::zeroed() };
if unsafe { libc::ioctl(fd, SECCOMP_IOCTL_NOTIF_RECV as _, &mut notification) } < 0 {
continue;
}
let args = notification.data.args;
let refuse = is_kill(notification.data.nr, notification.data.arch)
&& refuses_group_kill(args, process_group(notification.pid), hermit_group);
if unsafe { libc::ioctl(fd, SECCOMP_IOCTL_NOTIF_ID_VALID as _, ¬ification.id) } < 0 {
continue;
}
let response = if refuse {
tracing::warn!(
"hermit refused the guest's kill(0, {}) from pid {} under --namespace-only \
with EPERM: its process group is Hermit's, so the signal would reach Hermit and \
the processes that started it, where Linux would deliver it \
(https://github.com/rrnewton/hermit/issues/4065)",
args[1] as i32,
notification.pid,
);
seccomp_notif_resp {
id: notification.id,
val: 0,
error: -libc::EPERM,
flags: 0,
}
} else {
seccomp_notif_resp {
id: notification.id,
val: 0,
error: 0,
flags: SECCOMP_USER_NOTIF_FLAG_CONTINUE,
}
};
unsafe { libc::ioctl(fd, SECCOMP_IOCTL_NOTIF_SEND as _, &response) };
}
}
static PANIC_LOCATION: OnceLock<Mutex<Option<String>>> = OnceLock::new();
fn panic_location() -> &'static Mutex<Option<String>> {
PANIC_LOCATION.get_or_init(|| Mutex::new(None))
}
fn install_panic_location_hook() {
static INSTALLED: OnceLock<()> = OnceLock::new();
INSTALLED.get_or_init(|| {
let previous = panic::take_hook();
panic::set_hook(Box::new(move |info| {
if let Some(location) = info.location() {
let recorded = format!(
"{}:{}:{}",
location.file(),
location.line(),
location.column()
);
if let Ok(mut slot) = panic_location().lock() {
*slot = Some(recorded);
}
}
previous(info);
}));
});
}
fn take_panic_location() -> String {
panic_location()
.lock()
.ok()
.and_then(|mut slot| slot.take())
.unwrap_or_else(|| "<unknown location>".to_string())
}
fn panic_message(payload: &(dyn Any + Send)) -> String {
if let Some(message) = payload.downcast_ref::<&'static str>() {
(*message).to_string()
} else if let Some(message) = payload.downcast_ref::<String>() {
message.clone()
} else {
"<panic payload was not a string>".to_string()
}
}
fn inject_test_fault(site: &str) {
if std::env::var("HERMIT_TEST_CONTAINER_CHILD_FAULT_SITE").is_ok_and(|want| want != site) {
return;
}
match std::env::var("HERMIT_TEST_CONTAINER_CHILD_FAULT")
.ok()
.as_deref()
{
Some("panic") => {
panic!("deliberate container-child panic for fault-injection testing at site {site}")
}
Some("segv") => {
unsafe { std::ptr::null_mut::<u8>().write_volatile(1) };
}
Some("block") => {
eprintln!("hermit: test fault: container child blocked at site {site}");
loop {
std::thread::park();
}
}
_ => {}
}
}
#[cfg(test)]
fn catch_child_panic<F, T>(f: &mut F) -> Result<T, SerializableError>
where
F: FnMut() -> Result<T, Error>,
{
install_panic_location_hook();
match panic::catch_unwind(panic::AssertUnwindSafe(|| {
inject_test_fault("with_container");
f()
})) {
Ok(result) => result.map_err(SerializableError::from),
Err(payload) => {
let location = take_panic_location();
Err(SerializableError::from(anyhow!(
"panic in container child at {location}: {}",
panic_message(&*payload)
))
.into_panic())
}
}
}
pub(super) fn catch_child_panic_at<F, T>(
site: &'static str,
mut f: F,
) -> Result<T, SerializableError>
where
F: FnMut() -> Result<T, Error>,
{
install_panic_location_hook();
match panic::catch_unwind(panic::AssertUnwindSafe(|| {
inject_test_fault(site);
f()
})) {
Ok(result) => result.map_err(SerializableError::from),
Err(payload) => Err(SerializableError::from(anyhow!(
"panic in container child at {}: {}",
take_panic_location(),
panic_message(&*payload)
))
.into_panic()),
}
}
#[derive(Debug)]
pub struct ContainerChildExit(pub ExitStatus);
impl std::fmt::Display for ContainerChildExit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Sandbox container exited unexpectedly: {:?}", self.0)
}
}
impl std::error::Error for ContainerChildExit {}
#[derive(Debug)]
pub struct PolicyRefusal;
impl std::fmt::Display for PolicyRefusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Hermit refused the run: a fail-closed policy stopped it before completion. \
This is not a hermit failure; the refusal reason is above."
)
}
}
impl std::error::Error for PolicyRefusal {}
#[derive(Debug)]
pub struct RunTimeoutMarker;
impl std::fmt::Display for RunTimeoutMarker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "the --timeout bound fired")
}
}
impl std::error::Error for RunTimeoutMarker {}
#[derive(Debug)]
pub struct LogCapExceeded;
impl std::fmt::Display for LogCapExceeded {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"hermit's log output exceeded --max-log-bytes and the run was aborted; \
lower the log verbosity or raise --max-log-bytes"
)
}
}
impl std::error::Error for LogCapExceeded {}
#[derive(Debug)]
pub struct SignalDeath(pub i32);
impl std::fmt::Display for SignalDeath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Hermit's container was terminated by signal {}. This is not a hermit \
failure and not a refusal: hermit did not choose to stop this run.",
self.0
)
}
}
impl std::error::Error for SignalDeath {}
#[derive(Debug)]
pub struct ContainerChildPanic(pub Error);
impl std::fmt::Display for ContainerChildPanic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for ContainerChildPanic {}
pub fn classify_container_result<T>(
ran: Result<Result<T, SerializableError>, RunError>,
) -> Result<T, Error> {
match ran {
Ok(Ok(value)) => Ok(value),
Ok(Err(reported)) => {
let kind = reported.kind();
let error = Error::from(reported);
Err(match kind {
FailureKind::Panic => Error::new(ContainerChildPanic(error)),
FailureKind::PolicyRefusal => error.context(PolicyRefusal),
FailureKind::SkidOvershoot { count } => error
.context(SkidOvershootError::new(count))
.context(PolicyRefusal),
FailureKind::RunTimeout => error.context(RunTimeoutMarker),
FailureKind::BackendRunFailure => {
Error::new(hermit::BackendRunFailure).context(format!("{error:#}"))
}
FailureKind::Error => error,
})
}
Err(RunError::ExitStatus(status))
if status.code() == Some(detcore_model::HERMIT_POLICY_REFUSAL_EXIT) =>
{
Err(Error::new(PolicyRefusal))
}
Err(RunError::ExitStatus(status)) if status.code() == Some(HERMIT_DEADLINE_EXIT) => {
Err(Error::new(RunTimeoutMarker))
}
Err(RunError::ExitStatus(status)) if status.code() == Some(HERMIT_LOG_CAP_EXIT) => {
super::tracing::bound_log_cap_exit();
Err(Error::new(LogCapExceeded))
}
Err(RunError::ExitStatus(status))
if status
.code()
.and_then(detcore_model::signal_from_exit_status)
.is_some() =>
{
let signal = status
.code()
.and_then(detcore_model::signal_from_exit_status)
.expect("guard just matched");
Err(Error::new(SignalDeath(signal)))
}
Err(RunError::ExitStatus(status)) => Err(Error::new(ContainerChildExit(status))),
Err(error @ RunError::Spawn(_)) => {
Err(Error::new(error).context("Sandbox container failed to spawn"))
}
}
}
#[cfg(test)]
#[path = "../../../tests/common/readonly_proc.rs"]
mod readonly_proc;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rewrites_of_snapshot_drop_a_mount_that_left_the_namespace() {
let rewrite = |raw_mount_id| MountInfoRootRewrite {
raw_mount_id,
deterministic_root: b"/".to_vec(),
raw_root_prefix: None,
deterministic_root_prefix: None,
raw_mountpoint_prefix: None,
deterministic_mountpoint_prefix: None,
};
let kept = rewrites_of_snapshot(vec![rewrite(30), rewrite(40), rewrite(10)], &[10, 30, 1]);
assert_eq!(
kept.iter()
.map(|rewrite| rewrite.raw_mount_id)
.collect::<Vec<_>>(),
[30, 10]
);
}
#[test]
fn installations_record_their_install_time_mountpoints() {
let scratch = tempfile::tempdir().unwrap();
let root = fs::canonicalize(scratch.path()).unwrap();
for dir in ["a", "c", "item", "foo", "bar", "opt"] {
fs::create_dir(root.join(dir)).unwrap();
}
std::os::unix::fs::symlink(root.join("foo"), root.join("a/x")).unwrap();
std::os::unix::fs::symlink(root.join("bar"), root.join("c/x")).unwrap();
let plan = [
PlannedMount::hermit(container_proc_mount()),
PlannedMount::hermit(Mount::bind(root.join("a"), root.join("opt")).recursive()),
PlannedMount::hermit(Mount::bind(root.join("item"), root.join("opt/x"))),
PlannedMount::hermit(Mount::bind(root.join("c"), root.join("opt"))),
PlannedMount::backend(
Mount::new(root.join("opt"))
.flags(MountFlags::MS_BIND | MountFlags::MS_REMOUNT | MountFlags::MS_RDONLY),
),
];
let installations = installations_of(&plan);
let escaped = |path: &Path| vec![mountinfo_escape_path(path)];
assert_eq!(installations[0].mountpoints, escaped(Path::new("/proc")));
assert_eq!(installations[1].mountpoints, escaped(&root.join("opt")));
assert_eq!(installations[1].sources, escaped(&root.join("a")));
assert_eq!(installations[2].mountpoints, escaped(&root.join("foo")));
assert!(
installations[2].sources.is_empty(),
"a plain bind copies nothing"
);
assert_eq!(installations[3].mountpoints, escaped(&root.join("opt")));
assert!(
installations[4].mountpoints.is_empty(),
"a remount makes no row"
);
assert!(installations[4].backend);
let mut tree = InstallTimeTree::default();
for planned in &plan[..4] {
tree.install(&planned.mount);
}
assert_eq!(tree.resolve(&root.join("opt/x")), root.join("bar"));
}
const STOP_SIGNAL_BUDGET: std::time::Duration = std::time::Duration::from_secs(5);
fn in_isolated_process<T>(body: impl FnMut() -> Result<T, String>) -> T
where
T: serde::Serialize + serde::de::DeserializeOwned,
{
Container::new()
.run(body)
.expect("the isolated test process should run to completion")
.unwrap_or_else(|error| panic!("{error}"))
}
fn set_default_stop_dispositions() -> Result<(), String> {
let default = SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty());
for signal in CONTAINER_INIT_STOP_SIGNALS {
unsafe { sigaction(signal, &default) }.map_err(|error| format!("{signal}: {error}"))?;
}
Ok(())
}
fn enter_new_pid_namespace() -> Result<(), String> {
if unsafe { libc::unshare(libc::CLONE_NEWUSER | libc::CLONE_NEWPID) } == -1 {
return Err(format!("unshare: {}", io::Error::last_os_error()));
}
Ok(())
}
fn fork_waiting_child(prepare: fn() -> bool) -> Result<libc::pid_t, String> {
let mut fds = [0; 2];
if unsafe { libc::pipe(fds.as_mut_ptr()) } == -1 {
return Err(format!("pipe: {}", io::Error::last_os_error()));
}
match unsafe { libc::fork() } {
-1 => Err(format!("fork: {}", io::Error::last_os_error())),
0 => unsafe {
libc::close(fds[0]);
if !prepare() {
libc::_exit(99);
}
libc::write(fds[1], [1u8].as_ptr().cast(), 1);
libc::close(fds[1]);
loop {
libc::pause();
}
},
child => {
let mut ready = 0u8;
let read = unsafe {
libc::close(fds[1]);
let read = libc::read(fds[0], (&mut ready as *mut u8).cast(), 1);
libc::close(fds[0]);
read
};
if read != 1 {
return Err(format!(
"child {child} exited without reporting that it was ready, so its \
preparation failed"
));
}
Ok(child)
}
}
}
fn signal_and_reap(child: libc::pid_t, signal: Signal) -> Result<Option<i32>, String> {
if unsafe { libc::kill(child, signal as libc::c_int) } == -1 {
return Err(format!(
"kill({child}, {signal}): {}",
io::Error::last_os_error()
));
}
let deadline = std::time::Instant::now() + STOP_SIGNAL_BUDGET;
loop {
let mut status = 0;
match unsafe { libc::waitpid(child, &mut status, libc::WNOHANG) } {
reaped if reaped == child => return Ok(Some(status)),
-1 => return Err(format!("waitpid({child}): {}", io::Error::last_os_error())),
_ => {}
}
if std::time::Instant::now() >= deadline {
unsafe {
libc::kill(child, libc::SIGKILL);
libc::waitpid(child, &mut status, 0);
}
return Ok(None);
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
fn describe_wait_status(status: Option<i32>) -> String {
match status {
None => format!("still running {STOP_SIGNAL_BUDGET:?} after the signal"),
Some(status) if libc::WIFEXITED(status) => {
format!("exited {}", libc::WEXITSTATUS(status))
}
Some(status) if libc::WIFSIGNALED(status) => {
format!("killed by signal {}", libc::WTERMSIG(status))
}
Some(status) => format!("wait status {status:#x}"),
}
}
#[test]
fn a_namespace_init_honours_stop_signals_sent_before_it_arms() {
for signal in CONTAINER_INIT_STOP_SIGNALS {
let status = in_isolated_process(|| {
set_default_stop_dispositions()?;
let _bridge = ContainerInitStopSignalBridge::install()
.map_err(|error| format!("{error:#}"))?;
enter_new_pid_namespace()?;
let init = fork_waiting_child(|| true)?;
signal_and_reap(init, signal)
});
assert!(
status.is_some_and(|status| libc::WIFEXITED(status)
&& libc::WEXITSTATUS(status) == 128 + signal as i32),
"{signal}: a namespace init that had not armed its own handlers should \
exit {}, as `on_container_init_stop_signal` does; it {}",
128 + signal as i32,
describe_wait_status(status)
);
}
}
#[test]
fn the_process_holding_the_bridge_still_dies_from_stop_signals() {
fn hold_the_bridge() -> bool {
if set_default_stop_dispositions().is_err() {
return false;
}
match ContainerInitStopSignalBridge::install() {
Ok(bridge) => {
std::mem::forget(bridge);
true
}
Err(_) => false,
}
}
for signal in CONTAINER_INIT_STOP_SIGNALS {
let status = in_isolated_process(|| {
let holder = fork_waiting_child(hold_the_bridge)?;
signal_and_reap(holder, signal)
});
assert!(
status
.is_some_and(|status| libc::WIFSIGNALED(status)
&& libc::WTERMSIG(status) == signal as i32),
"{signal}: the process holding the bridge should be killed by {signal}; it {}",
describe_wait_status(status)
);
}
}
#[test]
fn the_bridge_leaves_ignored_and_handled_signals_alone_and_restores_the_default() {
extern "C" fn someone_elses_handler(_signal: libc::c_int) {}
let dispositions = in_isolated_process(|| {
let read = || -> Result<Vec<libc::sighandler_t>, String> {
CONTAINER_INIT_STOP_SIGNALS
.into_iter()
.map(|signal| {
current_signal_action(signal)
.map(|action| action.sa_sigaction)
.map_err(|error| format!("{error:#}"))
})
.collect()
};
let actions = [
SigHandler::SigDfl,
SigHandler::SigIgn,
SigHandler::Handler(someone_elses_handler),
];
for (signal, handler) in CONTAINER_INIT_STOP_SIGNALS.into_iter().zip(actions) {
let action = SigAction::new(handler, SaFlags::empty(), SigSet::empty());
unsafe { sigaction(signal, &action) }
.map_err(|error| format!("{signal}: {error}"))?;
}
let before = read()?;
let bridge =
ContainerInitStopSignalBridge::install().map_err(|error| format!("{error:#}"))?;
let during = read()?;
let installed = bridge.handler;
drop(bridge);
Ok((before, during, read()?, installed))
});
let (before, during, after, installed) = dispositions;
let other = before[2];
assert_eq!(
before[..2],
[libc::SIG_DFL, libc::SIG_IGN],
"SIGTERM starts at SIG_DFL and SIGINT at SIG_IGN"
);
assert!(
![libc::SIG_DFL, libc::SIG_IGN, installed].contains(&other),
"SIGHUP starts at a handler that is not the bridge"
);
assert_eq!(
during,
vec![installed, libc::SIG_IGN, other],
"only SIGTERM, the signal at SIG_DFL, is bridged while the guard is held"
);
assert_eq!(
after, before,
"dropping the guard restores every disposition it found"
);
}
#[test]
fn the_bridge_is_not_installed_by_a_process_that_is_pid_1() {
fn install_as_pid_1() -> bool {
if unsafe { libc::getpid() } != 1 || set_default_stop_dispositions().is_err() {
return false;
}
let Ok(bridge) = ContainerInitStopSignalBridge::install() else {
return false;
};
let untouched = CONTAINER_INIT_STOP_SIGNALS.into_iter().all(|signal| {
current_signal_action(signal)
.is_ok_and(|action| action.sa_sigaction == libc::SIG_DFL)
});
drop(bridge);
untouched
}
in_isolated_process(|| {
enter_new_pid_namespace()?;
let init = fork_waiting_child(install_as_pid_1)?;
signal_and_reap(init, Signal::SIGKILL).map(drop)
});
}
fn namespace_proc_contents() -> Result<(String, String), String> {
Ok((
fs::read_to_string("/proc/mounts").map_err(|error| error.to_string())?,
fs::read_to_string("/proc/self/status").map_err(|error| error.to_string())?,
))
}
#[test]
fn default_container_uses_readonly_proc_fallback() {
let (mounts, status) = Container::new()
.run(|| {
readonly_proc::deny_writable_mounts().map_err(|error| error.to_string())?;
default_container(false)
.run(namespace_proc_contents)
.map_err(|error| format!("{error:?}"))?
})
.unwrap()
.expect("default container should retry its denied proc mount read-only");
readonly_proc::assert_readonly_proc(&mounts, &status, 1);
}
#[test]
fn image_container_uses_readonly_proc_fallback() {
let rootfs = tempfile::tempdir().unwrap();
let tmpfs = tempfile::tempdir().unwrap();
for directory in ["proc", "tmp", "dev/pts"] {
fs::create_dir_all(rootfs.path().join(directory)).unwrap();
}
for node in crate::image::DEV_BIND_TARGETS {
File::create(rootfs.path().join("dev").join(node)).unwrap();
}
let (mounts, status) = Container::new()
.run(|| {
readonly_proc::deny_writable_mounts().map_err(|error| error.to_string())?;
let (mut container, _identity) =
image_container(rootfs.path(), tmpfs.path(), false, true)
.map_err(|error| error.to_string())?;
container
.run(namespace_proc_contents)
.map_err(|error| format!("{error:?}"))?
})
.unwrap()
.expect("image container should retry its denied proc mount read-only");
readonly_proc::assert_readonly_proc(&mounts, &status, 1);
}
#[test]
fn image_container_accepts_a_rootfs_on_a_nosuid_nodev_filesystem() {
fn root_mount_flags() -> Result<(bool, bool, bool), String> {
let mut stat = std::mem::MaybeUninit::<libc::statvfs>::uninit();
let rc = unsafe { libc::statvfs(c"/".as_ptr(), stat.as_mut_ptr()) };
if rc != 0 {
return Err(format!("statvfs /: {}", io::Error::last_os_error()));
}
let flags = unsafe { stat.assume_init() }.f_flag;
Ok((
flags & libc::ST_RDONLY != 0,
flags & libc::ST_NOSUID != 0,
flags & libc::ST_NODEV != 0,
))
}
let base = tempfile::tempdir().unwrap();
let base_path = base.path().to_owned();
let flags = Container::new()
.map_root()
.mount(Mount::tmpfs(&base_path).flags(MountFlags::MS_NOSUID | MountFlags::MS_NODEV))
.run(move || -> Result<(bool, bool, bool), String> {
let rootfs = base_path.join("rootfs");
let tmpfs = base_path.join("scratch");
for directory in ["proc", "tmp", "dev/pts"] {
fs::create_dir_all(rootfs.join(directory)).map_err(|e| e.to_string())?;
}
for node in crate::image::DEV_BIND_TARGETS {
File::create(rootfs.join("dev").join(node)).map_err(|e| e.to_string())?;
}
fs::create_dir(&tmpfs).map_err(|e| e.to_string())?;
let (mut container, _identity) = image_container(&rootfs, &tmpfs, false, true)
.map_err(|error| error.to_string())?;
container
.run(root_mount_flags)
.map_err(|error| format!("{error:?}"))?
})
.expect("run the outer namespace holding the nosuid,nodev filesystem")
.expect("an image root on a nosuid,nodev filesystem must mount read-only");
assert_eq!(
flags,
(true, true, true),
"image root (read-only, nosuid, nodev) flags inside the container"
);
}
#[test]
fn a_refusal_reported_through_the_error_channel_is_still_a_refusal() {
let refusal = SerializableError::from(anyhow::Error::new(
detcore::UnsupportedSyscallError(reverie::syscalls::Sysno::kexec_load),
));
assert_eq!(
refusal.kind(),
FailureKind::PolicyRefusal,
"an UnsupportedSyscallError must be classified at the boundary, which is the \
last place its TYPE still exists -- past it everything is strings"
);
let classified = classify_container_result::<()>(Ok(Err(refusal)))
.expect_err("a refusal is not a success");
assert!(
classified.downcast_ref::<PolicyRefusal>().is_some(),
"record mode must report the same class as run mode for the same policy"
);
assert!(
classified.downcast_ref::<ContainerChildPanic>().is_none(),
"a refusal is not a panic"
);
assert!(
format!("{classified:#}").contains("kexec_load"),
"the refused syscall must survive into the reported chain: {classified:#}"
);
let ordinary = SerializableError::from(anyhow::anyhow!("something broke"));
assert_eq!(ordinary.kind(), FailureKind::Error);
let classified = classify_container_result::<()>(Ok(Err(ordinary)))
.expect_err("an error is not a success");
assert!(classified.downcast_ref::<PolicyRefusal>().is_none());
}
#[test]
fn skid_overshoot_count_survives_the_container_boundary() {
let reported = SerializableError::from(Error::new(SkidOvershootError::new(3)));
assert_eq!(reported.kind(), FailureKind::SkidOvershoot { count: 3 });
let classified = classify_container_result::<()>(Ok(Err(reported)))
.expect_err("a skid overshoot is not a success");
assert!(classified.downcast_ref::<PolicyRefusal>().is_some());
assert_eq!(
classified
.downcast_ref::<SkidOvershootError>()
.expect("the parent must recover the typed cause")
.count(),
3
);
}
#[test]
fn a_container_child_exiting_in_the_signal_band_is_not_an_internal_failure() {
use reverie::process::ExitStatus;
let signal_death = classify_container_result::<()>(Err(RunError::ExitStatus(
ExitStatus::Exited(detcore_model::HERMIT_SIGINT_DEATH_EXIT),
)))
.expect_err("a signal death is not a success");
assert!(
signal_death.downcast_ref::<SignalDeath>().is_some(),
"Exited(130) must classify as a signal death, not as an unchosen child \
exit -- LiteInst and the exit-code path both key on this distinction"
);
assert!(
signal_death.downcast_ref::<ContainerChildExit>().is_none(),
"a signal death must not also read as an unaccounted container exit"
);
let refusal = classify_container_result::<()>(Err(RunError::ExitStatus(
ExitStatus::Exited(detcore_model::HERMIT_POLICY_REFUSAL_EXIT),
)))
.expect_err("a refusal is not a success");
assert!(refusal.downcast_ref::<PolicyRefusal>().is_some());
assert!(
refusal.downcast_ref::<SignalDeath>().is_none(),
"122 must not parse as a signal death; the const-assert pins them disjoint"
);
let unchosen =
classify_container_result::<()>(Err(RunError::ExitStatus(ExitStatus::Exited(7))))
.expect_err("an unaccounted exit is not a success");
assert!(unchosen.downcast_ref::<ContainerChildExit>().is_some());
assert!(unchosen.downcast_ref::<SignalDeath>().is_none());
}
#[test]
fn child_panic_becomes_an_error_naming_message_and_location() {
let mut f = || -> Result<(), Error> { panic!("divergence detail that must survive") };
let reported =
catch_child_panic(&mut f).expect_err("a panic must not be reported as success");
assert_eq!(
reported.kind(),
FailureKind::Panic,
"a caught panic must be tagged as one at the catch site"
);
let error = Error::from(reported);
let rendered = format!("{error:#}");
assert!(
rendered.contains("divergence detail that must survive"),
"panic message was lost: {rendered}"
);
assert!(
rendered.contains("container.rs:"),
"panic location was lost, which is the half catch_unwind does not give us: {rendered}"
);
}
#[test]
fn ordinary_results_pass_through_unchanged() {
let mut ok = || -> Result<u32, Error> { Ok(7) };
assert_eq!(catch_child_panic(&mut ok).unwrap(), 7);
let mut err = || -> Result<u32, Error> { Err(anyhow!("a plain error, not a panic")) };
let reported = catch_child_panic(&mut err).unwrap_err();
assert_eq!(
reported.kind(),
FailureKind::Error,
"an ordinary reported error must NOT be tagged as a panic"
);
let rendered = format!("{:#}", Error::from(reported));
assert!(
rendered.contains("a plain error, not a panic"),
"{rendered}"
);
assert!(
!rendered.contains("panic in container child"),
"an ordinary error must not be relabelled as a panic: {rendered}"
);
}
#[test]
fn identity_hardening_freezes_group_with_overflow_entry() {
let (mounts, guard) =
identity_hardening_mounts().expect("identity hardening mounts should be constructible");
assert!(
!mounts.is_empty(),
"expected at least the frozen /etc/group mount"
);
let group_file = guard
._group_file
.as_ref()
.expect("identity hardening must produce a frozen group file");
let contents = fs::read_to_string(group_file.path())
.expect("frozen group database should be readable");
let overflow_gid = overflow_gid();
assert_eq!(overflow_gid, "65534");
assert!(
contents
.lines()
.any(|line| line.split(':').nth(2) == Some(overflow_gid.as_str())),
"frozen group database must resolve overflow gid {overflow_gid}:\n{contents}"
);
}
#[test]
fn mountinfo_provenance_uses_object_identity_not_temp_name_or_target() {
let actual = tempfile::NamedTempFile::new().expect("create actual temporary source");
let target = actual.path().to_path_buf();
let matching =
MountInfoRootSource::new(actual.path(), &target, b"/tmpvol/.hermit/matching").unwrap();
let guard = IdentityGuard {
_group_file: Some(actual),
_nscd_dir: None,
mountinfo_roots: vec![matching],
user_mount_targets: Vec::new(),
user_binds: Vec::new(),
host_rows: Vec::new(),
plan: MountPlan::Unrecorded,
};
let rewrites = guard.mountinfo_root_rewrites().unwrap();
assert_eq!(rewrites.len(), 1);
assert_eq!(rewrites[0].deterministic_root, b"/tmpvol/.hermit/matching");
assert_eq!(rewrites[0].raw_root_prefix, None);
assert_eq!(rewrites[0].raw_mountpoint_prefix, None);
assert_ne!(rewrites[0].raw_mount_id, 0);
}
#[test]
fn private_tmp_provenance_marks_descendant_roots_for_rewriting() {
let private_tmp = tempfile::TempDir::new().expect("create private tmp");
let provenance = MountInfoRootSource::private_tmp(private_tmp.path()).unwrap();
assert!(provenance.rewrite_descendant_roots);
assert_eq!(
provenance.raw_mountpoint_prefix.as_deref(),
Some(mountinfo_escape_path(private_tmp.path()).as_slice())
);
assert_eq!(
provenance.deterministic_mountpoint_prefix.as_deref(),
Some(b"/tmp".as_slice())
);
}
#[test]
fn image_container_registers_only_an_automatic_private_tmp() {
let rootfs = tempfile::TempDir::new().expect("create image rootfs");
let private_tmp = tempfile::TempDir::new().expect("create private tmp");
let (_, generated) = image_container(rootfs.path(), private_tmp.path(), false, true)
.expect("construct image container with generated tmp");
assert_eq!(generated.mountinfo_roots.len(), 1);
let (_, supplied) = image_container(rootfs.path(), private_tmp.path(), false, false)
.expect("construct image container with user tmp");
assert!(supplied.mountinfo_roots.is_empty());
}
#[test]
fn fdinfo_mount_id_parser_refuses_malformed_duplicates_and_missing_fields() {
assert_eq!(
detcore_model::procfs::parse_fdinfo_mount_id(b"pos:\t0\nmnt_id:\t37\n").unwrap(),
37
);
for malformed in [
"pos:\t0\n",
"mnt_id:\tbad\nmnt_id:\t37\n",
"mnt_id:\t37\nmnt_id:\t38\n",
"mnt_id:\t37 trailing\n",
"mnt_id:\t18446744073709551616\n",
] {
assert!(
detcore_model::procfs::parse_fdinfo_mount_id(malformed.as_bytes()).is_none(),
"accepted malformed fdinfo: {malformed:?}"
);
}
}
#[test]
fn mountinfo_provenance_refuses_a_same_target_lookalike() {
let actual = tempfile::NamedTempFile::new().expect("create actual temporary source");
let lookalike = tempfile::NamedTempFile::new().expect("create lookalike temporary source");
let target = actual.path().to_path_buf();
let same_target_wrong_source =
MountInfoRootSource::new(lookalike.path(), &target, b"/tmpvol/.hermit/lookalike")
.unwrap();
let guard = IdentityGuard {
_group_file: Some(actual),
_nscd_dir: None,
mountinfo_roots: vec![same_target_wrong_source],
user_mount_targets: Vec::new(),
user_binds: Vec::new(),
host_rows: Vec::new(),
plan: MountPlan::Unrecorded,
};
let error = guard.mountinfo_root_rewrites().unwrap_err().to_string();
assert!(error.contains("does not match the pinned Hermit-owned source"));
}
#[test]
fn explicit_tmp_mount_discards_private_tmp_provenance_before_resolution() {
let private_tmp = tempfile::TempDir::new().expect("create private tmp");
let mut guard = IdentityGuard::empty();
guard.add_private_tmp(private_tmp.path()).unwrap();
assert_eq!(guard.mountinfo_roots.len(), 1);
let mut mounts = Vec::new();
guard.discard_mounts_shadowed_by(&mut mounts, Path::new("/tmp"));
assert!(guard.mountinfo_roots.is_empty());
}
#[test]
fn ordered_user_mounts_do_not_reuse_stale_host_resolution() {
let var_run_is_run = fs::canonicalize("/var/run").ok() == fs::canonicalize("/run").ok();
if !var_run_is_run {
return;
}
let mut guard = IdentityGuard::empty();
guard.mountinfo_roots.push(MountInfoRootSource {
source: None,
target: PathBuf::from("/run/nscd"),
deterministic_root: Some(DETERMINISTIC_NSCD_ROOT.to_vec()),
rewrite_descendant_roots: false,
raw_mountpoint_prefix: None,
deterministic_mountpoint_prefix: None,
});
let mut mounts = vec![Mount::bind("/tmp", "/run/nscd")];
guard.discard_mounts_shadowed_by(&mut mounts, Path::new("/var"));
guard.discard_mounts_shadowed_by(&mut mounts, Path::new("/var/run/nscd"));
assert_eq!(mounts.len(), 1);
assert_eq!(guard.mountinfo_roots.len(), 1);
for direct_target in ["/run", "/var/run/nscd"] {
let mut guard = IdentityGuard::empty();
guard.mountinfo_roots.push(MountInfoRootSource {
source: None,
target: PathBuf::from("/run/nscd"),
deterministic_root: Some(DETERMINISTIC_NSCD_ROOT.to_vec()),
rewrite_descendant_roots: false,
raw_mountpoint_prefix: None,
deterministic_mountpoint_prefix: None,
});
let mut mounts = vec![Mount::bind("/tmp", "/run/nscd")];
guard.discard_mounts_shadowed_by(&mut mounts, Path::new(direct_target));
assert!(mounts.is_empty(), "{direct_target} must shadow /run/nscd");
assert!(guard.mountinfo_roots.is_empty());
}
}
#[test]
fn shadow_detection_follows_the_host_var_run_target() {
let var_run_is_run = fs::canonicalize("/var/run").ok() == fs::canonicalize("/run").ok();
assert_eq!(
mount_target_is_shadowed(Path::new("/var/run/nscd"), Path::new("/var")),
!var_run_is_run
);
assert_eq!(
mount_target_is_shadowed(Path::new("/var/run/nscd"), Path::new("/run")),
var_run_is_run
);
assert!(!mount_target_is_shadowed(
Path::new("/var/run/nscd"),
Path::new("/var/lib")
));
}
#[test]
fn affinity_core_is_an_actual_member_of_a_sparse_allowed_mask() {
let mut affinity = CpuSet::new();
for cpu in [7, 31, 211] {
affinity.set(cpu).expect("valid sparse CPU id");
}
let allowed = cpu_ids(&affinity);
assert_eq!(allowed, [7, 31, 211]);
for _ in 0..128 {
let selected = choose_affinity_core(&allowed).expect("non-empty mask");
assert!(allowed.contains(&selected), "selected CPU {selected}");
}
assert_eq!(choose_affinity_core(&[]), None);
}
type ArmOutcome = (i32, bool);
fn arm_after_the_parent_crossed(arm: fn() -> bool) -> ArmOutcome {
use std::io::Read as _;
in_isolated_process(move || {
let budget = crate::tracing::LogBudget::new(100).map_err(|e| e.to_string())?;
crate::tracing::register_invocation_log_budget(Some(budget.clone()));
if unsafe { libc::prctl(libc::PR_SET_CHILD_SUBREAPER, 1) } == -1 {
return Err(format!("subreaper: {}", io::Error::last_os_error()));
}
let mut fds = [0; 2];
if unsafe { libc::pipe(fds.as_mut_ptr()) } == -1 {
return Err(format!("pipe: {}", io::Error::last_os_error()));
}
let (read_fd, marker_fd) = (fds[0], fds[1]);
let p = unsafe { libc::fork() };
if p == -1 {
return Err(format!("fork P: {}", io::Error::last_os_error()));
}
if p == 0 {
unsafe {
libc::close(read_fd);
let parent = libc::getpid();
let c = libc::fork();
if c == -1 {
libc::_exit(70);
}
if c == 0 {
let mut waits = 0;
while libc::getppid() == parent {
waits += 1;
if waits > 10_000 {
libc::_exit(71);
}
libc::usleep(1000);
}
if !arm() {
libc::_exit(72);
}
libc::write(marker_fd, b"G".as_ptr().cast(), 1);
libc::_exit(0);
}
libc::close(marker_fd);
let null = libc::open(c"/dev/null".as_ptr(), libc::O_WRONLY);
let mut writer = crate::tracing::CappedWriter::file(
std::fs::File::from_raw_fd(null),
0,
Some(budget.clone()),
);
let _ = writer.write_all(&[b'a'; 60]);
let _ = writer.write_all(&[b'b'; 60]);
libc::_exit(73);
}
}
unsafe { libc::close(marker_fd) };
let mut status = 0;
if unsafe { libc::waitpid(p, &mut status, 0) } != p {
return Err(format!("waitpid P: {}", io::Error::last_os_error()));
}
if !libc::WIFEXITED(status) || libc::WEXITSTATUS(status) != hermit::HERMIT_LOG_CAP_EXIT
{
return Err(format!(
"P did not cross the cap and exit 123: status {status:#x}"
));
}
let mut c_status = 0;
if unsafe { libc::waitpid(-1, &mut c_status, 0) } <= 0 {
return Err(format!("waitpid C: {}", io::Error::last_os_error()));
}
let mut marker = Vec::new();
unsafe { std::fs::File::from_raw_fd(read_fd) }
.read_to_end(&mut marker)
.map_err(|e| e.to_string())?;
Ok((c_status, !marker.is_empty()))
})
}
fn assert_stopped_by_the_crossing(name: &str, (status, marker): ArmOutcome) {
assert!(
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == hermit::HERMIT_LOG_CAP_EXIT,
"{name}: armed after its parent crossed --max-log-bytes and exited 123, so its death \
signal can never arrive; it must exit 123 itself, but its wait status was \
{status:#x} (exit code {}){}",
libc::WEXITSTATUS(status),
if marker {
", and it went on to the guest start"
} else {
""
},
);
assert!(
!marker,
"{name}: it exited 123 but wrote the guest-start marker first"
);
}
#[test]
fn log_cap_crossed_before_the_container_init_arms_stops_the_init() {
let outcome = arm_after_the_parent_crossed(|| arm_container_init_guards().is_ok());
assert_stopped_by_the_crossing("arm_container_init_guards", outcome);
}
#[test]
fn log_cap_crossed_before_the_namespace_only_guest_arms_stops_the_exec() {
let outcome = arm_after_the_parent_crossed(|| arm_namespace_only_guest().is_ok());
assert_stopped_by_the_crossing("arm_namespace_only_guest", outcome);
}
#[test]
fn only_a_group_kill_from_hermits_group_is_refused() {
let args = |pid: i64, sig: i64| [pid as u64, sig as u64, 0, 0, 0, 0];
let hermit = 40;
assert!(refuses_group_kill(
args(0, libc::SIGUSR1 as i64),
Some(hermit),
hermit
));
assert!(refuses_group_kill(
args(0, libc::SIGKILL as i64),
None,
hermit
));
assert!(!refuses_group_kill(args(0, 0), Some(hermit), hermit));
assert!(!refuses_group_kill(args(0, 100), Some(hermit), hermit));
assert!(refuses_group_kill(args(0, 34), Some(hermit), hermit));
assert!(refuses_group_kill(args(0, 64), Some(hermit), hermit));
assert!(!refuses_group_kill(
args(0, libc::SIGUSR1 as i64),
Some(7),
hermit
));
assert!(!refuses_group_kill(
args(-1, libc::SIGUSR1 as i64),
Some(hermit),
hermit
));
assert!(!refuses_group_kill(
args(5, libc::SIGTERM as i64),
Some(hermit),
hermit
));
assert!(refuses_group_kill(
args(1 << 32, libc::SIGUSR1 as i64),
Some(hermit),
hermit
));
assert!(is_kill(libc::SYS_kill as i32, 0xc000_003e));
assert!(is_kill(I386_KILL, AUDIT_ARCH_I386));
assert!(!is_kill(I386_KILL, 0xc000_003e));
assert!(!is_kill(libc::SYS_getpid as i32, 0xc000_003e));
}
}