use anyhow::{Result, bail};
use openmoat_core::boundary::{
Downgrade, EffectiveBoundary, Landlock, LaunchCheck, Network, NetworkMode, Requested,
ToolToken, VERSION, Weaker,
};
use openmoat_core::rule_id::RUN_BOUNDARY;
use openmoat_core::{EvalContext, ProjectTop, Verdict};
use super::isolation::{Asked, Lightweight, Tier, write_row};
use super::{platform, tool_shell};
use crate::cli::{Format, RunArgs};
use crate::exit::Code;
use crate::home::Home;
use crate::render;
use crate::sandbox::landlock::abi::{self, Kernel};
use crate::sandbox::own_login::Owner;
const TOOL: &str = "boundary";
const PROJECT_TOP_WHY: &str = "`sandbox.project_top: writable` in the person's policy: without a \
network namespace the project's top is granted whole, so commands can read `.env` and \
write `.git/hooks` and `.moat` there";
pub struct Plan<'a> {
pub asked: Asked,
pub tier: &'a Tier,
pub port_only: Option<String>,
pub kernel: Option<&'a Kernel>,
pub token: ToolToken,
pub port: Option<u16>,
pub project_top_open: bool,
}
pub fn plan(plan: Plan<'_>) -> EffectiveBoundary {
let requested = match plan.asked {
Asked::Default => Requested::Default,
Asked::Isolate => Requested::Isolated,
Asked::NoIsolate => Requested::Lightweight,
};
let mut downgrades = Vec::new();
let tier = match plan.tier {
Tier::Isolated { .. } => openmoat_core::boundary::Tier::Isolated,
Tier::Lightweight(why) => {
match why {
Lightweight::Platform => {}
Lightweight::OptedOut => downgrades.push(Downgrade {
what: Weaker::Tier,
chosen: true,
why: "`openmoat run --no-isolate`".to_owned(),
}),
Lightweight::Unavailable(why) => downgrades.push(Downgrade {
what: Weaker::Tier,
chosen: false,
why: why.clone(),
}),
}
openmoat_core::boundary::Tier::Lightweight
}
};
let mode = match &plan.port_only {
Some(why) => {
downgrades.push(Downgrade {
what: Weaker::Network,
chosen: false,
why: why.clone(),
});
NetworkMode::PortOnly
}
None => platform::NETWORK,
};
if plan.project_top_open {
downgrades.push(Downgrade {
what: Weaker::ProjectTop,
chosen: true,
why: PROJECT_TOP_WHY.to_owned(),
});
}
let landlock = plan.kernel.map(|kernel| match kernel {
Kernel::Abi(abi) => Landlock {
abi: Some(*abi),
unavailable: None,
enforcement: None,
},
Kernel::Missing(why) => Landlock {
abi: None,
unavailable: Some(why.clone()),
enforcement: None,
},
});
EffectiveBoundary {
version: VERSION,
os: std::env::consts::OS.to_owned(),
requested,
tier,
downgrades,
network: Network {
mode,
proxy_port: plan.port,
},
landlock,
tool_token: plan.token,
restrictions: platform::restrictions(
plan.tier.isolated(),
plan.port_only.is_some(),
plan.kernel,
),
launch: None,
}
}
pub fn print_plan(
(args, home): (&RunArgs, &Home),
ctx: &EvalContext,
owner: Owner,
(tier, strict): (&Tier, Option<&str>),
) -> Result<Code> {
if let Some(why) = platform::unavailable() {
bail!("{why}");
}
let landlock = platform::landlock();
if let Some(kernel) = landlock.as_ref().filter(|k| !k.meets_floor()) {
bail!("{}", abi::summary(kernel));
}
let port_only = (!tier.isolated())
.then(platform::no_network_namespace)
.flatten();
super::refuse_port_only(args.netns, strict, port_only.as_deref())?;
let agent_host = match owner {
Owner::Agent(host) => Some(host),
Owner::Unknown => None,
};
let roots: Vec<String> = ctx
.project
.iter()
.cloned()
.chain(args.writes.iter().map(|w| super::resolved(w)))
.collect();
let record = plan(Plan {
asked: Asked::of(args),
tier,
port_only: port_only.clone(),
kernel: landlock.as_ref(),
token: tool_shell::token(agent_host, tool_shell::would_wrap(&roots)?),
port: None,
project_top_open: port_only.is_some()
&& home.load_policy()?.project_top() == ProjectTop::Writable,
});
match args.format {
Format::Json => render::json(&record)?,
Format::Text => render::boundary::write(&mut std::io::stdout().lock(), &record)?,
}
Ok(Code::Ok)
}
pub fn show(record: &EffectiveBoundary) -> Result<()> {
Ok(render::boundary::write(
&mut std::io::stderr().lock(),
record,
)?)
}
pub fn launched(
home: &Home,
(session, cwd): (&str, &str),
record: &mut EffectiveBoundary,
check: Option<LaunchCheck>,
) -> Result<()> {
if let Some(check) = check {
record.launched(check);
}
fail_row_for_test()?;
let missing = record.missing();
let verdict = if missing.is_empty() {
Verdict::Allow
} else {
Verdict::Deny
};
let json = serde_json::to_string(record)?;
write_row(home, (session, cwd), (TOOL, RUN_BOUNDARY), verdict, json)?;
if !missing.is_empty() {
bail!(
"`openmoat run` stopped the agent: what its sandbox promises is not all in force \
({}); the audit log has the run's boundary row (`{RUN_BOUNDARY}`)",
missing.join("; ")
);
}
Ok(())
}
const FAIL_ROW_ENV: &str = "MOAT_TEST_FAIL_BOUNDARY_ROW_MS";
fn fail_row_for_test() -> Result<()> {
if !cfg!(debug_assertions) {
return Ok(());
}
let Some(ms) = std::env::var(FAIL_ROW_ENV)
.ok()
.and_then(|v| v.parse().ok())
else {
return Ok(());
};
std::thread::sleep(std::time::Duration::from_millis(ms));
bail!("writing the run's boundary row failed ({FAIL_ROW_ENV}, a test switch)")
}
#[cfg(test)]
mod tests;