Previous to this PR, both of these functions take a single `cwd`:71038381aa/codex-rs/core/src/seatbelt.rs (L19-L25)71038381aa/codex-rs/core/src/landlock.rs (L16-L23)whereas `cwd` and `sandbox_cwd` should be set independently (fixed in this PR). Added `sandbox_distinguishes_command_and_policy_cwds()` to `codex-rs/exec/tests/suite/sandbox.rs` to verify this.
71 lines
2.1 KiB
Rust
71 lines
2.1 KiB
Rust
use crate::protocol::SandboxPolicy;
|
|
use crate::spawn::StdioPolicy;
|
|
use crate::spawn::spawn_child_async;
|
|
use std::collections::HashMap;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
use tokio::process::Child;
|
|
|
|
/// Spawn a shell tool command under the Linux Landlock+seccomp sandbox helper
|
|
/// (codex-linux-sandbox).
|
|
///
|
|
/// Unlike macOS Seatbelt where we directly embed the policy text, the Linux
|
|
/// helper accepts a list of `--sandbox-permission`/`-s` flags mirroring the
|
|
/// public CLI. We convert the internal [`SandboxPolicy`] representation into
|
|
/// the equivalent CLI options.
|
|
pub async fn spawn_command_under_linux_sandbox<P>(
|
|
codex_linux_sandbox_exe: P,
|
|
command: Vec<String>,
|
|
command_cwd: PathBuf,
|
|
sandbox_policy: &SandboxPolicy,
|
|
sandbox_policy_cwd: &Path,
|
|
stdio_policy: StdioPolicy,
|
|
env: HashMap<String, String>,
|
|
) -> std::io::Result<Child>
|
|
where
|
|
P: AsRef<Path>,
|
|
{
|
|
let args = create_linux_sandbox_command_args(command, sandbox_policy, sandbox_policy_cwd);
|
|
let arg0 = Some("codex-linux-sandbox");
|
|
spawn_child_async(
|
|
codex_linux_sandbox_exe.as_ref().to_path_buf(),
|
|
args,
|
|
arg0,
|
|
command_cwd,
|
|
sandbox_policy,
|
|
stdio_policy,
|
|
env,
|
|
)
|
|
.await
|
|
}
|
|
|
|
/// Converts the sandbox policy into the CLI invocation for `codex-linux-sandbox`.
|
|
fn create_linux_sandbox_command_args(
|
|
command: Vec<String>,
|
|
sandbox_policy: &SandboxPolicy,
|
|
sandbox_policy_cwd: &Path,
|
|
) -> Vec<String> {
|
|
#[expect(clippy::expect_used)]
|
|
let sandbox_policy_cwd = sandbox_policy_cwd
|
|
.to_str()
|
|
.expect("cwd must be valid UTF-8")
|
|
.to_string();
|
|
|
|
#[expect(clippy::expect_used)]
|
|
let sandbox_policy_json =
|
|
serde_json::to_string(sandbox_policy).expect("Failed to serialize SandboxPolicy to JSON");
|
|
|
|
let mut linux_cmd: Vec<String> = vec![
|
|
sandbox_policy_cwd,
|
|
sandbox_policy_json,
|
|
// Separator so that command arguments starting with `-` are not parsed as
|
|
// options of the helper itself.
|
|
"--".to_string(),
|
|
];
|
|
|
|
// Append the original tool command.
|
|
linux_cmd.extend(command);
|
|
|
|
linux_cmd
|
|
}
|