Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(sandbox): add local jail grants and per-call scratch directory

Steven Enamakel7c56b5549bfc
diff --git a/crates/openhuman-core/src/sandbox/ops.rs b/crates/openhuman-core/src/sandbox/ops.rs
index 4a8f3bdb02..226fd1008e 100644
--- a/crates/openhuman-core/src/sandbox/ops.rs
+++ b/crates/openhuman-core/src/sandbox/ops.rs
@@ -2,6 +2,7 @@
//! routed execution.
use super::docker;
+use super::grants::{resolve_local_jail_grants, JailGrants};
use super::types::{
ElevatedOp, SandboxBackendHandle, SandboxBackendKind, SandboxExecRequest, SandboxExecResult,
SandboxPolicy, SandboxStatus, ELEVATED_TOOLS,
@@ -67,11 +68,21 @@ pub fn resolve_sandbox_policy(
_ => true,
};
+ // The jail denies whatever it is not told about, so a local policy carries
+ // the toolchain/git grants everyday commands need (see `sandbox::grants`).
+ let grants = if backend == SandboxBackendKind::Local {
+ resolve_local_jail_grants(dirs::home_dir().as_deref(), &runtime_config.local_jail)
+ } else {
+ JailGrants::default()
+ };
+
tracing::debug!(
mode = ?mode,
backend = ?backend,
is_remote = is_remote_session,
action_dir = %action_dir.display(),
+ read_only_grants = grants.read_only.len(),
+ read_write_grants = grants.read_write.len(),
"[sandbox] resolved policy"
);
@@ -79,7 +90,8 @@ pub fn resolve_sandbox_policy(
backend,
workspace_root: action_dir.to_path_buf(),
state_dir: state_dir.to_path_buf(),
- read_only_mounts: vec![],
+ read_only_mounts: grants.read_only,
+ read_write_mounts: grants.read_write,
allow_network,
env_passthrough: SANDBOX_ENV_PASSTHROUGH
.iter()
@@ -100,10 +112,16 @@ pub fn resolve_sandbox_policy(
/// Landlock the noop path is never reached and a regression to "always Ready"
/// passes unnoticed. That is exactly how the original defect survived.
pub(crate) fn local_status_for_backend(backend_name: &str) -> SandboxStatus {
- if backend_name == cwd_jail::NOOP_BACKEND_NAME {
- // The noop backend enforces nothing — it spawns the command as-is. It is
- // a documented passthrough rather than a failure, so `Inactive`
- // ("backend not initialized") is the honest report, not `Error`.
+ if backend_name == cwd_jail::NOOP_BACKEND_NAME
+ || backend_name == cwd_jail::detect::UNSUPPORTED_BACKEND_NAME
+ {
+ // The noop backend enforces nothing — it spawns the command as-is. The
+ // `unsupported` backend is what `pick_backend` answers when no OS jail
+ // is usable (no Landlock in the kernel, Windows, ...); it refuses to
+ // spawn, and `execute_local_jail` then falls back to the noop backend,
+ // so commands still run unconfined. Both are a documented passthrough
+ // rather than a failure, so `Inactive` ("backend not initialized") is
+ // the honest report, not `Error`.
SandboxStatus::Inactive
} else {
SandboxStatus::Ready
@@ -245,43 +263,54 @@ pub fn sandbox_capture_root(state_dir: &Path) -> PathBuf {
state_dir.join("artifacts").join("sandbox-capture")
}
-/// A per-call capture directory, `<capture root>/<uuid>`, removed with its
-/// contents on drop so every exit path (spawn failure, wait error, timeout)
-/// cleans up.
-struct CaptureDir(PathBuf);
+/// Where the local jail's per-call writable scratch lives (`TMPDIR` points
+/// here): `<state_dir>/artifacts/sandbox-scratch`.
+pub fn sandbox_scratch_root(state_dir: &Path) -> PathBuf {
+ state_dir.join("artifacts").join("sandbox-scratch")
+}
-impl CaptureDir {
- fn create(state_dir: &Path) -> anyhow::Result<Self> {
- let dir = sandbox_capture_root(state_dir).join(uuid::Uuid::new_v4().to_string());
- std::fs::create_dir_all(&dir).map_err(|e| {
+/// A fresh per-call directory, `<root>/<uuid>`, removed with its contents on
+/// drop so every exit path (spawn failure, wait error, timeout) cleans up.
+struct CallDir {
+ path: PathBuf,
+ kind: &'static str,
+}
+
+impl CallDir {
+ fn create(root: PathBuf, kind: &'static str) -> anyhow::Result<Self> {
+ let path = root.join(uuid::Uuid::new_v4().to_string());
+ std::fs::create_dir_all(&path).map_err(|e| {
anyhow::anyhow!(
- "Failed to create sandbox capture dir {}: {e}",
- dir.display()
+ "Failed to create sandbox {kind} dir {}: {e}",
+ path.display()
)
})?;
- tracing::debug!(dir = %dir.display(), "[sandbox:local] created capture dir");
- Ok(Self(dir))
+ tracing::debug!(dir = %path.display(), kind, "[sandbox:local] created per-call dir");
+ Ok(Self { path, kind })
}
fn stdout(&self) -> PathBuf {
- self.0.join("stdout")
+ self.path.join("stdout")
}
fn stderr(&self) -> PathBuf {
- self.0.join("stderr")
+ self.path.join("stderr")
}
}
-impl Drop for CaptureDir {
+impl Drop for CallDir {
fn drop(&mut self) {
- match std::fs::remove_dir_all(&self.0) {
- Ok(()) => {
- tracing::debug!(dir = %self.0.display(), "[sandbox:local] removed capture dir")
- }
+ match std::fs::remove_dir_all(&self.path) {
+ Ok(()) => tracing::debug!(
+ dir = %self.path.display(),
+ kind = self.kind,
+ "[sandbox:local] removed per-call dir"
+ ),
Err(e) => tracing::warn!(
- dir = %self.0.display(),
+ dir = %self.path.display(),
+ kind = self.kind,
error = %e,
- "[sandbox:local] failed to remove capture dir"
+ "[sandbox:local] failed to remove per-call dir"
),
}
}
@@ -309,9 +338,16 @@ async fn execute_local_jail(
for ro in &policy.read_only_mounts {
jail = jail.add_read_only(ro);
}
+ for rw in &policy.read_write_mounts {
+ jail = jail.add_read_write(rw);
+ }
- let capture = CaptureDir::create(&policy.state_dir)?;
- jail = jail.add_read_write(&capture.0);
+ let capture = CallDir::create(sandbox_capture_root(&policy.state_dir), "capture")?;
+ jail = jail.add_read_write(&capture.path);
+ // `/tmp` is not granted, so `mktemp`, compilers and package managers get a
+ // private writable TMPDIR instead; it is removed when the call ends.
+ let scratch = CallDir::create(sandbox_scratch_root(&policy.state_dir), "scratch")?;
+ jail = jail.add_read_write(&scratch.path);
let stdout_file = capture.stdout();
let stderr_file = capture.stderr();
// Platform-aware output-capture wrap: `{ … ; } > … 2> …` on sh/bash,
@@ -326,6 +362,7 @@ async fn execute_local_jail(
cmd.env(var, val);
}
}
+ cmd.env("TMPDIR", &scratch.path);
for (k, v) in extra_env {
cmd.env(k, v);
}
@@ -364,6 +401,7 @@ async fn execute_local_jail(
let stdout = std::fs::read_to_string(&stdout_file).unwrap_or_default();
let stderr_content = std::fs::read_to_string(&stderr_file).unwrap_or_default();
drop(capture);
+ drop(scratch);
Ok(SandboxExecResult {
exit_code: wait_result.0,
diff --git a/crates/openhuman-core/src/sandbox/types.rs b/crates/openhuman-core/src/sandbox/types.rs
index 3d00b4001b..d36b7f98ab 100644
--- a/crates/openhuman-core/src/sandbox/types.rs
+++ b/crates/openhuman-core/src/sandbox/types.rs
@@ -32,6 +32,9 @@ pub struct SandboxPolicy {
pub state_dir: PathBuf,
/// Additional read-only mounts (e.g. `/usr/lib`, managed node).
pub read_only_mounts: Vec<PathBuf>,
+ /// Additional read-write mounts (e.g. `~/.cargo`). Local jail only.
+ #[serde(default)]
+ pub read_write_mounts: Vec<PathBuf>,
/// Whether outbound network is allowed inside the sandbox.
pub allow_network: bool,
/// Environment variables to passthrough into the sandbox.