Stop --chdir from binding the working directory read-write

This commit is contained in:
2026-11-17 20:37:01 +01:00
parent 352ead2bf5
commit 4b801f811e
8 changed files with 72 additions and 21 deletions
+3
View File
@@ -66,11 +66,14 @@ impl Sandbox {
fn build(prefix: &[&str], extra_args: &[&str], guard: Option<HostGlobsGuard>) -> Self {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_agent-sandbox"));
cmd.args(prefix);
cmd.args(LAUNCH_DIR_WRITABLE);
cmd.args(extra_args);
Self { cmd, _guard: guard }
}
}
const LAUNCH_DIR_WRITABLE: [&str; 2] = ["--rw", "."];
fn scan_guard_for(extra_args: &[&str]) -> Option<HostGlobsGuard> {
extra_args
.contains(&"--blacklist")
+2 -1
View File
@@ -171,9 +171,10 @@ fn interrupt_during_grace_period_kills_child_processes_at_once() {
}
fn spawn_sandbox(args: &[&str], dir: &Path, script: &str) -> (Sandbox, Child) {
let dir = dir.to_str().unwrap();
let mut sandbox = Sandbox::new(args);
let child = sandbox
.args(["--chdir", dir.to_str().unwrap(), "--", "sh", "-c", script])
.args(["--rw", dir, "--chdir", dir, "--", "sh", "-c", script])
.stdout(Stdio::null())
.spawn()
.expect("agent-sandbox binary failed to execute");
+57 -8
View File
@@ -28,19 +28,65 @@ impl Drop for CleanupPath {
}
}
#[test]
fn cwd_is_writable() {
let _cleanup = CleanupPath::new("./sandbox_canary");
fn chdir_alone_makes_nothing_visible() {
let dir = TempDir::new().unwrap();
let dir_str = dir.path().to_str().unwrap();
let output = Sandbox::new(&[])
.args(["--", "bash", "-c", "touch ./sandbox_canary && echo ok"])
let output = Sandbox::new(&["--chdir", dir_str])
.args(["--", "true"])
.output()
.expect("agent-sandbox binary failed to execute");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(!output.status.success(), "chdir into an unbound path ran");
assert!(
stderr.contains(&format!(
"Can't chdir to {dir_str}: No such file or directory"
)),
"expected bwrap's chdir error, got: {stderr}"
);
}
#[test]
fn chdir_under_ro_is_read_only() {
let dir = TempDir::new().unwrap();
let dir_str = dir.path().to_str().unwrap();
let output = Sandbox::new(&["--ro", dir_str, "--chdir", dir_str])
.args([
"--",
"bash",
"-c",
"touch canary 2>/dev/null || echo readonly",
])
.output()
.expect("agent-sandbox binary failed to execute");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("ok"),
"expected 'ok' in stdout, got: {stdout}"
stdout.contains("readonly"),
"expected 'readonly' in stdout, got: {stdout}, stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(!dir.path().join("canary").exists());
}
#[test]
fn chdir_under_rw_is_writable() {
let dir = TempDir::new().unwrap();
let dir_str = dir.path().to_str().unwrap();
let output = Sandbox::new(&["--rw", dir_str, "--chdir", dir_str])
.args(["--", "touch", "canary"])
.output()
.expect("agent-sandbox binary failed to execute");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(dir.path().join("canary").exists());
}
#[test]
@@ -647,7 +693,7 @@ fn persistent_tmp_resumes_with_explicit_key_matching_derived_hash() {
let _cleanup_tmp = CleanupPath::new(format!("/tmp/agent-sandbox-{key}"));
let _cleanup_var = CleanupPath::new(format!("/var/tmp/agent-sandbox-{key}"));
let writer = Sandbox::new(&["--persistent-tmp", "--chdir", chdir_str])
let writer = Sandbox::new(&["--persistent-tmp", "--ro", chdir_str, "--chdir", chdir_str])
.args(["--", "bash", "-c", "echo persisted > /tmp/canary"])
.output()
.expect("write run failed");
@@ -660,12 +706,15 @@ fn persistent_tmp_resumes_with_explicit_key_matching_derived_hash() {
// Second run: different chdir (would have derived a different hash) plus an
// extra flag, but an explicit key pinning the same host directory.
let other_chdir = TempDir::new().unwrap();
let other_chdir_str = other_chdir.path().to_str().unwrap();
let reader = Sandbox::new(&[
"--persistent-tmp",
"--persistent-key",
&key,
"--ro",
other_chdir_str,
"--chdir",
other_chdir.path().to_str().unwrap(),
other_chdir_str,
"--unshare-net",
])
.args(["--", "cat", "/tmp/canary"])
+2 -2
View File
@@ -65,7 +65,7 @@ fn chdir_override() {
let dir = TempDir::new().expect("failed to create temp dir");
let dir_str = dir.path().to_str().unwrap();
let output = Sandbox::new(&["--chdir", dir_str])
let output = Sandbox::new(&["--ro", dir_str, "--chdir", dir_str])
.args(["--", "bash", "-c", "pwd"])
.output()
.expect("agent-sandbox binary failed to execute");
@@ -82,7 +82,7 @@ fn chdir_under_hardened_tmp() {
let dir = TempDir::new().expect("failed to create temp dir");
let dir_str = dir.path().to_str().unwrap();
let output = Sandbox::new(&["--hardened", "--chdir", dir_str])
let output = Sandbox::new(&["--hardened", "--rw", dir_str, "--chdir", dir_str])
.args(["--", "bash", "-c", "pwd && touch ./ok && echo done"])
.output()
.expect("agent-sandbox binary failed to execute");