use anyhow::{Context as _, Result, bail};
use openmoat_audit::NewEvent;
use openmoat_core::rule_id::{RUN_ISOLATION_UNAVAILABLE, RUN_NO_ISOLATE, RUN_PROJECT_TOP_OPEN};
use openmoat_core::{Decision, Verdict};
use crate::cli::RunArgs;
use crate::home::Home;
use crate::taint::run::LINK_HOST;
const TOOL: &str = "isolation";
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux offers the Isolated tier; the decision is the same code everywhere"
)
)]
pub enum Offered {
#[cfg_attr(
all(target_os = "linux", not(test)),
expect(
dead_code,
reason = "Linux always offers the tier or says why not; only other platforms build this"
)
)]
Not,
Available,
Unavailable(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Asked {
Default,
Isolate,
NoIsolate,
}
impl Asked {
pub fn of(args: &RunArgs) -> Self {
match (args.tier.isolate, args.tier.no_isolate) {
(true, _) => Self::Isolate,
(false, true) => Self::NoIsolate,
(false, false) => Self::Default,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tier {
Isolated {
asked: bool,
},
Lightweight(Lightweight),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Lightweight {
Platform,
OptedOut,
Unavailable(String),
}
impl Tier {
pub fn isolated(&self) -> bool {
matches!(self, Self::Isolated { .. })
}
}
pub fn choose(asked: Asked, offered: &Offered, strict: Option<&str>) -> Result<Tier> {
Ok(match (asked, offered, strict) {
(Asked::Isolate, _, _) => Tier::Isolated { asked: true },
(Asked::Default | Asked::NoIsolate, Offered::Not, _) => {
Tier::Lightweight(Lightweight::Platform)
}
(Asked::NoIsolate, _, Some(asks)) => bail!(
"{asks} requires `openmoat run` to start with its full boundary \
(`require.run: full-boundary`), which on Linux is the Isolated tier, so \
`--no-isolate` does not start; no flag lowers this"
),
(Asked::NoIsolate, _, None) => Tier::Lightweight(Lightweight::OptedOut),
(Asked::Default, Offered::Available, _) => Tier::Isolated { asked: false },
(Asked::Default, Offered::Unavailable(why), Some(asks)) => bail!(
"{asks} requires `openmoat run` to start with its full boundary \
(`require.run: full-boundary`), which on Linux is the Isolated tier, and it \
cannot start here, so the program does not start: {why}"
),
(Asked::Default, Offered::Unavailable(why), None) => {
Tier::Lightweight(Lightweight::Unavailable(why.clone()))
}
})
}
pub fn notice(tier: &Tier) -> Option<String> {
let Tier::Lightweight(Lightweight::Unavailable(why)) = tier else {
return None;
};
Some(format!(
"moat run: the Isolated tier, `openmoat run`'s default on Linux, cannot start here: \
{why}. This run is the Lightweight tier and is recorded as such in the audit log; \
`--no-isolate` chooses it on purpose, `--isolate` refuses to start instead"
))
}
pub fn failed_default(tier: &Tier, strict: bool) -> Option<&'static str> {
match tier {
Tier::Isolated { asked: false } if !strict => Some(
"`openmoat run` starts the Isolated tier by default on Linux, and it did not start; \
`openmoat run --no-isolate` starts the Lightweight tier instead",
),
_ => None,
}
}
pub fn row(tier: &Tier) -> Option<(&'static str, String)> {
match tier {
Tier::Lightweight(Lightweight::OptedOut) => Some((
RUN_NO_ISOLATE,
"`openmoat run --no-isolate`: the person started the Lightweight tier instead of \
the Isolated one, so commands could see the rest of the machine the policy's read \
roots allow, and inside the project the paths the policy denies were covered only \
as far as `openmoat run` said when it started"
.to_owned(),
)),
Tier::Lightweight(Lightweight::Unavailable(why)) => Some((
RUN_ISOLATION_UNAVAILABLE,
format!(
"`openmoat run` could not start the Isolated tier ({why}) and started the \
Lightweight tier, so commands could see the rest of the machine the policy's \
read roots allow, and inside the project the paths the policy denies were \
covered only as far as `openmoat run` said when it started"
),
)),
Tier::Isolated { .. } | Tier::Lightweight(Lightweight::Platform) => None,
}
}
pub fn record(home: &Home, session: &str, cwd: &str, tier: &Tier) -> Result<()> {
let Some((rule, reason)) = row(tier) else {
return Ok(());
};
write_row(home, (session, cwd), (TOOL, rule), Verdict::Allow, reason)
}
pub fn record_project_top(home: &Home, session: &str, cwd: &str) -> Result<()> {
let reason = "`sandbox.project_top: writable` in the person's policy: `openmoat run` had no \
network namespace and granted the project's top whole, so commands could read \
`.env` and write `.git/hooks` and `.moat` there"
.to_owned();
write_row(
home,
(session, cwd),
(TOOL, RUN_PROJECT_TOP_OPEN),
Verdict::Allow,
reason,
)
}
pub(super) fn write_row(
home: &Home,
(session, cwd): (&str, &str),
(tool, rule): (&str, &str),
verdict: Verdict,
reason: String,
) -> Result<()> {
let decision = Decision::single(verdict, rule, reason);
let store = crate::audit_key::open_existing(home)
.context("opening the audit log to record the run's sandbox")?;
store.record(&NewEvent {
host: LINK_HOST,
session_id: session,
call_id: None,
cwd: Some(cwd),
tool,
action: None,
decision: &decision,
latency_us: 0,
})?;
Ok(())
}
pub fn doctor_row(offered: &Offered, strict: Option<&str>) -> Option<(bool, String)> {
match (offered, strict) {
(Offered::Not, _) => None,
(Offered::Available, _) => Some((
false,
"openmoat run starts the Isolated tier (bubblewrap works here); \
`--no-isolate` starts the Lightweight tier and is recorded in the audit log"
.to_owned(),
)),
(Offered::Unavailable(why), Some(asks)) => Some((
true,
format!(
"openmoat run refuses to start: {asks} requires the full boundary, which \
on Linux is the Isolated tier, and it cannot start here: {why}"
),
)),
(Offered::Unavailable(why), None) => Some((
true,
format!(
"openmoat run cannot start the Isolated tier here, so it warns and starts \
the Lightweight tier, which shows commands more of the machine and covers less \
of the project (another row names what stays open there): {why}"
),
)),
}
}
#[cfg(test)]
mod tests;