mod boundary;
#[cfg(any(windows, test))]
mod command_line;
mod env;
mod hook_off;
#[cfg(any(target_os = "macos", target_os = "linux"))]
mod inherited;
mod isolation;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
mod release_age;
mod required;
pub mod score;
mod temp;
pub mod tier;
mod tool_shell;
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
mod unsupported;
#[cfg(windows)]
pub(crate) mod windows;
#[cfg(target_os = "linux")]
use linux as platform;
#[cfg(target_os = "macos")]
use macos as platform;
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
use unsupported as platform;
#[cfg(windows)]
use windows as platform;
use std::ffi::{OsStr, OsString};
use std::io::Write as _;
use std::net::{Ipv4Addr, TcpListener};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use anyhow::{Context as _, Result, bail};
use openmoat_core::Policy;
use openmoat_proxy::Credential;
use zeroize::Zeroizing;
use super::proxy::{self, Exit};
use super::sandbox::write_report;
use crate::cli::{IsolatedArgs, Netns, RunArgs};
use crate::context::{self, path_string};
use crate::environment::find_on_path;
use crate::exit::Code;
use crate::guard_relay::{self, Service, random_hex};
use crate::home::Home;
use crate::proxy_ca::Trust;
use crate::sandbox::Grants;
use crate::sandbox::Report;
use crate::sandbox::git_env;
use crate::sandbox::kerberos;
use crate::sandbox::landlock::abi::{self, Kernel};
use crate::sandbox::own_login::{self, Owner};
use crate::sandbox::shared_temp;
use crate::snapshot::RunSnapshots;
use crate::taint::run::RUN_SESSION_ENV;
use isolation::{Asked, Offered, Tier};
use temp::RunTemp;
const PROXY_VARS: [&str; 6] = [
"HTTP_PROXY",
"HTTPS_PROXY",
"ALL_PROXY",
"http_proxy",
"https_proxy",
"all_proxy",
];
pub fn run(args: &RunArgs) -> Result<Code> {
let home = proxy::installed()?;
let ctx = context::eval_context(&home, None, None)?;
if ctx.project.is_none() {
bail!(
"`openmoat run` confines the agent to a project: start it in the project, not in \
your home directory, one of its ancestors or a filesystem root"
);
}
let (name, rest) = args.command.split_first().context("no agent to run")?;
let program = resolve(name)?;
let owner = Owner::of(&name.to_string_lossy(), &path_string(&program));
let strict = required::check(&home, &ctx, owner, &name.to_string_lossy())?;
let tier = isolation::choose(Asked::of(args), &platform::isolation(), strict.as_deref())?;
if args.print_boundary {
return boundary::print_plan((args, &home), &ctx, owner, (&tier, strict.as_deref()));
}
let git = git_env::add(&|var| std::env::var(var).ok())?;
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
.context("listening on a loopback port for the proxy")?;
let port = listener.local_addr()?.port();
let mut policy = home.load_policy()?;
let agent_host = match owner {
Owner::Agent(host) => Some(host),
Owner::Unknown => None,
};
let hook_off = hook_off::check(&home, Path::new(&ctx.cwd), agent_host, rest, &policy)?;
crate::install::self_paths::protect_recorded(&home, &mut policy)?;
let ccname = std::env::var("KRB5CCNAME").ok();
kerberos::for_run(&mut policy, ccname.as_deref());
let run_temp = (!tier.isolated()).then(RunTemp::create).transpose()?;
let terminating = crate::proxy_ca::load(&home, &policy)?;
let trust = terminating
.as_ref()
.map(|t| match &run_temp {
Some(temp) => Trust::write_in(&t.cert_pem, Path::new(&temp.path())),
None => Trust::write(&t.cert_pem),
})
.transpose()?;
let ca_notice = terminating.as_ref().and_then(|t| t.notice.clone());
let write_roots: Vec<String> = ctx
.project
.iter()
.cloned()
.chain(run_temp.as_ref().map(RunTemp::path))
.chain(args.writes.iter().map(|w| resolved(w)))
.collect();
let tool_shell = tool_shell::prepare(agent_host, &write_roots)?;
if let Some(notice) = tool_shell::off_notice(agent_host, tool_shell.as_ref()) {
eprintln!("{notice}");
}
let grants = Grants {
proxy_port: Some(port),
tmpdir: run_temp.as_ref().map(RunTemp::path),
program: Some(path_string(&program)),
writes: args.writes.iter().map(|w| resolved(w)).collect(),
terminals: platform::terminals(),
trust: trust.as_ref().map(|t| path_string(t.dir())),
tool_shell: tool_shell.as_ref().map(tool_shell::ToolShell::grant),
};
let state = home.root();
let (mut sandboxed, kept) = own_login::for_session(&policy, &ctx, owner, &grants.writes);
if run_temp.is_some() {
shared_temp::for_run(&mut sandboxed, cfg!(target_os = "macos"));
}
let mut agent = if tier.isolated() {
platform::isolate(&sandboxed, &ctx, grants, &program, state).map_err(|error| {
match isolation::failed_default(&tier, strict.is_some()) {
Some(hint) => error.context(hint),
None => error,
}
})?
} else {
platform::confine(&sandboxed, &ctx, grants, &program, state)?
};
own_login::report(agent.report_mut(), &kept, &ctx);
refuse_port_only(args.netns, strict.as_deref(), agent.warning())?;
if let hook_off::Preflight::Override { args, .. } = &hook_off {
agent.command().args(*args);
}
agent.command().args(rest);
let asked_env = &args.environment;
let allowed = env::Allowed::new(platform::BASE_ENV, agent_host, &policy, &asked_env.envs);
let env_mode = env::Mode::choose(policy.run_env(), strict.as_deref(), asked_env.scrub_env);
let release_age = package_release_age(&policy, ctx.project.as_deref());
agent.command().envs(release_age.vars);
let brokered = env::brokered(&policy);
for (name, placeholder) in &brokered {
agent.command().env(name, placeholder);
}
let credential = session_credential()?;
let listener_addr = listener.local_addr()?;
let url = credential.proxy_url(listener_addr);
let run_session = credential.session().to_owned();
isolation::record(&home, &run_session, &ctx.cwd, &tier)?;
hook_off::record(&home, (&run_session, &ctx.cwd), &hook_off)?;
let landlock = platform::landlock();
if agent.project_top_open() {
isolation::record_project_top(&home, &run_session, &ctx.cwd)?;
}
let strict_score = policy.sandbox_strict();
let mut record = boundary::plan(boundary::Plan {
asked: Asked::of(args),
tier: &tier,
port_only: agent
.warning()
.map(|warning| platform::no_network_namespace().unwrap_or_else(|| warning.to_owned())),
kernel: landlock.as_ref(),
token: tool_shell::token(agent_host, tool_shell.is_some()),
port: Some(port),
project_top_open: agent.project_top_open(),
});
boundary::refuse_incomplete(strict_score, &record)?;
let cwd = ctx.cwd.clone();
let snapshots = RunSnapshots::start(&home, &ctx, args.no_snapshot, &run_session);
let (guard, guard_token) = guard_service(&home, &run_session, agent_host)?;
let exit = Exit::open(&home, policy, ctx, Some(credential), &write_roots)?
.with_guard(guard)
.with_tls(terminating.map(|t| t.tls));
notice(
&program,
port,
exit.session(),
&agent,
(&tier, landlock.as_ref(), args.verbose),
)?;
if args.verbose {
boundary::show(&record)?;
}
let notices = ca_notice
.into_iter()
.chain(hook_off::notice(agent_host, &hook_off))
.chain(release_age.notices)
.chain(trust.iter().filter_map(Trust::notice));
for line in notices {
eprintln!("{line}");
}
std::thread::spawn(move || {
if let Err(error) = exit.serve(&listener) {
eprintln!("moat run: the proxy stopped, so the agent has no network: {error:#}");
}
});
for var in PROXY_VARS {
agent.command().env(var, url.as_str());
}
agent.command().env(RUN_SESSION_ENV, &run_session);
agent
.command()
.env(guard_relay::SOCKET_ENV, listener_addr.to_string())
.env(guard_relay::TOKEN_ENV, guard_token.as_str());
if let Some(wrapper) = &tool_shell {
wrapper.apply(agent.command());
}
agent
.command()
.env_remove("NO_PROXY")
.env_remove("no_proxy");
agent.command().envs(git);
if let Some(temp) = &run_temp {
agent.command().env(temp::TMPDIR, temp.path());
}
for (var, path) in trust.iter().flat_map(Trust::env) {
agent.command().env(var, path);
}
record.environment = Some(env::apply(
agent.command(),
(&allowed, &env_mode, &brokered),
args.verbose,
));
signal_hook::flag::register(
signal_hook::consts::SIGINT,
Arc::new(AtomicBool::new(false)),
)
.context("keeping OpenMoat running on Ctrl-C")?;
let status = agent
.status(|check| {
let record = (&mut record, strict_score);
boundary::launched(&home, (&run_session, &cwd), record, check)
})
.with_context(|| format!("starting {}", program.display()))?;
snapshots.finish();
Ok(if status.success() {
Code::Ok
} else {
Code::Failed
})
}
pub fn inside(args: &IsolatedArgs) -> Result<Code> {
platform::inside(args)
}
pub fn tool_shell_entry() -> Option<Code> {
tool_shell::entry()
}
fn session_credential() -> Result<Credential> {
let random = random_hex(36).context("making the proxy credential")?;
let session = format!("run-{}-{}", crate::time::now_ms(), &random[..8]);
Ok(Credential::new(&session, random[8..].as_bytes())?)
}
fn guard_service(
home: &Home,
run_session: &str,
agent: Option<openmoat_hosts::Host>,
) -> Result<(Service, Zeroizing<String>)> {
let token = random_hex(32).context("making the hook token")?;
let moat = std::env::current_exe()
.and_then(std::fs::canonicalize)
.context("locating the moat binary")?;
let service = Service::new(home.clone(), run_session, token.as_bytes(), moat, agent);
Ok((service, token))
}
const MAX_PACKAGE_CONFIG_BYTES: u64 = 1 << 20;
fn package_release_age(policy: &Policy, project: Option<&str>) -> release_age::ReleaseAge {
let in_project = |file: &str| project.map(|p| Path::new(p).join(file));
let small = |path: &Path| {
let small = std::fs::metadata(path).is_ok_and(|m| m.len() < MAX_PACKAGE_CONFIG_BYTES);
small.then(|| std::fs::read_to_string(path).ok()).flatten()
};
let var = |name: &str| std::env::var(name).ok();
release_age::env(
policy.package_min_release_age_days(),
crate::time::now_ms(),
&release_age::Found {
inherited: &|name| std::env::var_os(name).map(|v| v.to_string_lossy().into_owned()),
project_has: &|file| in_project(file).is_some_and(|p| p.exists()),
project_file: &|file| small(&in_project(file)?),
user_file: &|tool| {
let home = crate::home::user_home().ok();
small(&release_age::user_config_path(tool, &var, home)?)
},
},
)
}
fn planned_environment(
policy: &Policy,
host: Option<openmoat_hosts::Host>,
args: &RunArgs,
(strict, project): (Option<&str>, Option<&str>),
) -> Result<openmoat_core::boundary::RunEnvironment> {
let mode = env::Mode::choose(policy.run_env(), strict, args.environment.scrub_env);
let allowed = env::Allowed::new(platform::BASE_ENV, host, policy, &args.environment.envs);
let brokered = env::brokered(policy);
let git = git_env::add(&|var| std::env::var(var).ok())?;
let own: Vec<String> = PROXY_VARS
.iter()
.chain(&[
"NO_PROXY",
"no_proxy",
temp::TMPDIR,
RUN_SESSION_ENV,
guard_relay::SOCKET_ENV,
guard_relay::TOKEN_ENV,
])
.map(|name| (*name).to_owned())
.chain(git.into_iter().map(|(name, _)| name))
.chain(
package_release_age(policy, project)
.vars
.into_iter()
.map(|(name, _)| name.to_owned()),
)
.chain(brokered.iter().map(|(name, _)| name.clone()))
.collect();
let inherited = || std::env::vars_os().map(|(name, _)| name);
let withheld = env::withheld(inherited(), &allowed, &|name| {
own.iter().any(|set| env::same_name(name, OsStr::new(set)))
});
Ok(env::record(&mode, inherited(), &withheld, &brokered))
}
fn resolve(name: &OsStr) -> Result<PathBuf> {
let path = Path::new(name);
let found = if path.components().count() > 1 {
Some(path.to_path_buf())
} else {
find_on_path(&name.to_string_lossy())
};
let found = found.with_context(|| format!("{} is not on PATH", path.display()))?;
std::fs::canonicalize(&found).with_context(|| format!("resolving {}", found.display()))
}
fn resolved(path: &Path) -> String {
let resolved = std::fs::canonicalize(path).or_else(|_| context::absolute(path));
path_string(resolved.as_deref().unwrap_or(path))
}
pub fn unavailable() -> Option<String> {
platform::unavailable()
}
pub fn mcp_sandbox_default() -> bool {
platform::unavailable().is_none() && platform::MCP_SANDBOX_BY_DEFAULT
}
pub fn mcp_sandbox_off() -> Option<String> {
(!platform::MCP_SANDBOX_BY_DEFAULT && platform::unavailable().is_none()).then(|| {
"the Windows tier of `openmoat run` is a preview that holds writes only and starts no \
batch file such as `npx`; `openmoat mcp wrap --sandbox` asks for it"
.to_owned()
})
}
pub fn port_only_network() -> Option<String> {
platform::port_only_network()
}
pub fn landlock() -> Option<Kernel> {
platform::landlock()
}
pub fn bubblewrap() -> Option<bool> {
platform::bubblewrap()
}
pub fn bubblewrap_userns() -> Option<std::result::Result<(), String>> {
platform::bubblewrap_userns()
}
pub fn project_open(policy: &Policy, ctx: &openmoat_core::EvalContext) -> Option<String> {
platform::project_open(policy, ctx)
}
pub fn withheld_names(policy: &Policy, hosts: &[openmoat_hosts::Host]) -> Vec<OsString> {
let mut allowed = env::Allowed::new(platform::BASE_ENV, None, policy, &[]);
for host in hosts {
allowed.add_host(*host);
}
let brokered = env::brokered(policy);
env::withheld(
std::env::vars_os().map(|(name, _)| name),
&allowed,
&|name| brokered.iter().any(|(set, _)| name == set.as_str()),
)
}
pub fn withheld_line(names: &[OsString], policy: &Policy, strict: Option<&str>) -> String {
env::doctor_line(
names,
env::Mode::choose(policy.run_env(), strict, false).scrubbed,
)
}
pub fn full_boundary_gap() -> Option<String> {
platform::unavailable()
.or_else(|| match platform::isolation() {
Offered::Unavailable(why) => Some(format!("the Isolated tier cannot start: {why}")),
Offered::Not | Offered::Available => None,
})
.or_else(platform::no_network_namespace)
}
pub fn tier_row(strict: Option<&str>) -> Option<(bool, String)> {
isolation::doctor_row(&platform::isolation(), strict)
}
fn refuse_port_only(netns: Netns, strict: Option<&str>, port_only: Option<&str>) -> Result<()> {
match (strict, netns, port_only) {
(Some(asks), _, Some(why)) => bail!(
"{asks} requires `openmoat run` to start with its full boundary \
(`require.run: full-boundary`), and the agent cannot get a network of its own here, \
so it does not start; no `--netns` value lowers this.\n{why}"
),
(None, Netns::Require, Some(why)) => bail!(
"`openmoat run --netns=require`: the agent cannot get a network of its own here, so \
it does not start.\n{why}"
),
_ => Ok(()),
}
}
fn notice(
program: &Path,
port: u16,
session: &str,
agent: &platform::Confined,
(tier, landlock, verbose): (&Tier, Option<&Kernel>, bool),
) -> Result<()> {
let report: &Report = agent.report();
let mut err = std::io::stderr().lock();
let sees = if tier.isolated() {
"; it sees only the project, the read roots and --write paths"
} else {
""
};
let network = if platform::NETWORK == openmoat_core::boundary::NetworkMode::ProxyVariables {
"network through OpenMoat's proxy for programs that honour HTTP_PROXY (not held by the \
OS)"
.to_owned()
} else if agent.warning().is_some() {
format!("network only to port {port} on any host, OpenMoat's proxy")
} else {
"network only through OpenMoat's proxy".to_owned()
};
writeln!(
err,
"moat run: {} in a sandbox made from the policy{sees}, {network} on 127.0.0.1:{port} \
(audit session {session})",
program.display()
)?;
if let Some(warning) = agent.warning() {
writeln!(err, "{warning}")?;
}
if let Some(fallback) = isolation::notice(tier) {
writeln!(err, "{fallback}")?;
}
if let Some(open) = agent.project_notice() {
writeln!(err, "{open}")?;
}
let off = crate::sandbox::install::own_sandbox_off();
if verbose {
writeln!(
err,
" proxy: 127.0.0.1:{port}\n \
turn the agent's own sandbox off{off}: sandboxes \
do not nest, so the agent's cannot start in here. This one holds the agent and \
every command it starts together, so it is less precise per command than the \
agent's own sandbox."
)?;
write_report(&mut err, report)?;
if let Some(kernel) = landlock {
writeln!(err, " {}", abi::summary(kernel))?;
for line in abi::lines(kernel) {
writeln!(err, " {line}")?;
}
}
} else {
writeln!(
err,
"moat run: turn the agent's own sandbox off{off}; \
sandbox ready ({} stricter, {} wider; --verbose for details)",
report.losses.len(),
report.allowances.len()
)?;
}
Ok(())
}
#[cfg(test)]
mod tests;