|
| 1 | +//! Writable PATH candidates must never run while constructing the sandbox. |
| 2 | +
|
| 3 | +#![cfg(target_os = "linux")] |
| 4 | + |
| 5 | +use codex_protocol::models::PermissionProfile; |
| 6 | +use codex_protocol::permissions::NetworkSandboxPolicy; |
| 7 | +use codex_utils_absolute_path::AbsolutePathBuf; |
| 8 | +use codex_utils_cargo_bin::cargo_bin; |
| 9 | +use codex_utils_cargo_bin::write_executable; |
| 10 | +use pretty_assertions::assert_eq; |
| 11 | +use std::os::unix::fs::PermissionsExt; |
| 12 | +use std::process::Command; |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn child_workdir_does_not_probe_or_launch_writable_parent_bwrap() { |
| 16 | + // Bazel can mount the default temp directory noexec. |
| 17 | + let temp = tempfile::tempdir_in(std::env::current_dir().expect("current directory")) |
| 18 | + .or_else(|_| tempfile::tempdir()) |
| 19 | + .expect("create fixture"); |
| 20 | + let workspace = temp.path().join("workspace"); |
| 21 | + let cwd = workspace.join("service"); |
| 22 | + let bin = workspace.join("bin"); |
| 23 | + let marker = temp.path().join("outside-workspace"); |
| 24 | + std::fs::create_dir_all(&cwd).expect("create cwd"); |
| 25 | + std::fs::create_dir_all(&bin).expect("create bin"); |
| 26 | + let fake_bwrap = bin.join("bwrap"); |
| 27 | + std::fs::write( |
| 28 | + &fake_bwrap, |
| 29 | + "#!/bin/sh\n: > \"$BWRAP_PROBE_MARKER\"\nprintf '%s\\n' '--as-pid-1 --perms --argv0 --ro-bind-fd'\n", |
| 30 | + ) |
| 31 | + .expect("write fake bwrap"); |
| 32 | + std::fs::set_permissions(&fake_bwrap, std::fs::Permissions::from_mode(0o755)) |
| 33 | + .expect("make fake bwrap executable"); |
| 34 | + let policy = PermissionProfile::read_only() |
| 35 | + .file_system_sandbox_policy() |
| 36 | + .with_additional_writable_roots( |
| 37 | + &workspace, |
| 38 | + &[AbsolutePathBuf::try_from(workspace.clone()).expect("absolute workspace")], |
| 39 | + ); |
| 40 | + let profile = |
| 41 | + PermissionProfile::from_runtime_permissions(&policy, NetworkSandboxPolicy::Restricted); |
| 42 | + let profile_json = serde_json::to_string(&profile).expect("serialize profile"); |
| 43 | + let trusted_path = std::env::var_os("PATH").expect("PATH is set"); |
| 44 | + let run = |search_path: &std::ffi::OsStr, script: &str| { |
| 45 | + Command::new(cargo_bin("codex-linux-sandbox").expect("resolve sandbox binary")) |
| 46 | + .arg("--sandbox-policy-cwd") |
| 47 | + .arg(&workspace) |
| 48 | + .arg("--command-cwd") |
| 49 | + .arg(&cwd) |
| 50 | + .arg("--permission-profile") |
| 51 | + .arg(&profile_json) |
| 52 | + .args(["--", "/bin/sh", "-c", script]) |
| 53 | + .current_dir(&cwd) |
| 54 | + .env("PATH", search_path) |
| 55 | + .env("BWRAP_PROBE_MARKER", &marker) |
| 56 | + .output() |
| 57 | + .expect("run sandbox") |
| 58 | + }; |
| 59 | + let baseline = run(&trusted_path, "printf sandboxed"); |
| 60 | + let stderr = String::from_utf8_lossy(&baseline.stderr); |
| 61 | + if !baseline.status.success() |
| 62 | + && [ |
| 63 | + "bubblewrap is unavailable", |
| 64 | + "No permissions to create a new namespace", |
| 65 | + "setting up uid map: Permission denied", |
| 66 | + ] |
| 67 | + .iter() |
| 68 | + .any(|message| stderr.contains(message)) |
| 69 | + { |
| 70 | + eprintln!( |
| 71 | + "sandbox unavailable: {}", |
| 72 | + String::from_utf8_lossy(&baseline.stderr) |
| 73 | + ); |
| 74 | + return; |
| 75 | + } |
| 76 | + assert_eq!(baseline.status.success(), true, "{baseline:?}"); |
| 77 | + let denied_write = run(&trusted_path, ": > \"$BWRAP_PROBE_MARKER\""); |
| 78 | + assert_eq!(denied_write.status.success(), false, "{denied_write:?}"); |
| 79 | + assert!( |
| 80 | + !marker.exists(), |
| 81 | + "sandbox must deny the outside-workspace write" |
| 82 | + ); |
| 83 | + let hostile_path = std::env::join_paths( |
| 84 | + [bin] |
| 85 | + .into_iter() |
| 86 | + .chain(std::env::split_paths(&trusted_path)), |
| 87 | + ) |
| 88 | + .expect("join PATH"); |
| 89 | + let output = run(&hostile_path, "printf sandboxed"); |
| 90 | + assert!(!marker.exists(), "writable bwrap ran before confinement"); |
| 91 | + assert_eq!(output.status.success(), true, "{output:?}"); |
| 92 | + assert_eq!(output.stdout, b"sandboxed"); |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn full_disk_write_with_managed_network_does_not_probe_writable_bwrap() { |
| 97 | + let temp = tempfile::tempdir_in(std::env::current_dir().unwrap()).unwrap(); |
| 98 | + let bin = temp.path().join("bin"); |
| 99 | + let cwd = temp.path().join("cwd"); |
| 100 | + std::fs::create_dir(&bin).unwrap(); |
| 101 | + std::fs::create_dir(&cwd).unwrap(); |
| 102 | + let bwrap = bin.join("bwrap"); |
| 103 | + write_executable(&bwrap, "#!/bin/sh\n: > \"$BWRAP_PROBE_MARKER\"\nexit 1\n").unwrap(); |
| 104 | + let run = |profile: &PermissionProfile, marker: &std::path::Path| { |
| 105 | + Command::new(cargo_bin("codex-linux-sandbox").expect("resolve sandbox binary")) |
| 106 | + .arg("--sandbox-policy-cwd") |
| 107 | + .arg(&cwd) |
| 108 | + .arg("--permission-profile") |
| 109 | + .arg(serde_json::to_string(profile).unwrap()) |
| 110 | + .args(["--managed-network", "{}", "--", "/bin/true"]) |
| 111 | + .current_dir(&cwd) |
| 112 | + .env_clear() |
| 113 | + .env("PATH", &bin) |
| 114 | + .env("HTTP_PROXY", "http://127.0.0.1:9") |
| 115 | + .env("BWRAP_PROBE_MARKER", marker) |
| 116 | + .output() |
| 117 | + .unwrap() |
| 118 | + }; |
| 119 | + // Confirm this host reaches the capability probe without needing working |
| 120 | + // namespaces or bundled bubblewrap. Both invocations use managed networking. |
| 121 | + let control_marker = temp.path().join("control"); |
| 122 | + let control = run(&PermissionProfile::read_only(), &control_marker); |
| 123 | + assert!( |
| 124 | + control_marker.exists(), |
| 125 | + "probe was not reached: {control:?}" |
| 126 | + ); |
| 127 | + |
| 128 | + let marker = temp.path().join("unrestricted"); |
| 129 | + let output = run(&PermissionProfile::Disabled, &marker); |
| 130 | + assert!( |
| 131 | + !marker.exists(), |
| 132 | + "writable bwrap ran before network confinement: {output:?}" |
| 133 | + ); |
| 134 | +} |
0 commit comments