From 3dcdee6d2bbebc86ec2978919342a49ed9a0e11a Mon Sep 17 00:00:00 2001 From: Cameron Mcloughlin Date: Tue, 17 Nov 2026 12:43:21 +0000 Subject: [PATCH] sandbox: New linux sandbox (#67062) Note: this PR does not change any behaviour of zed itself. The old sandbox implementation is still in use. The code in this PR is not considered secure. I'll need more time to torture it before I'm confident :) The old sandbox used `bwrap` to set up mounts to restrict FS access, and an in-sandbox network proxy to forward HTTP requests. It has significant limitations however: - it cannot restrict FS access to paths that don't exist at sandbox creation time. This means that files that become available while a command is running are unprotected - it cannot restrict FS access based on a glob - each individual mount must be specified explicitly - it cannot restrict network access that is not HTTP (i.e. SSH traffic). - it cannot change permissions while a sandboxed command is running - the grants given at the start of the execution are fixed. Agents can request to re-run the app with broader permissions, which is fine, but not great UX - it cannot detect whether a command failed because it hit the sandbox boundary or not. Again, agents can guess, but we'd like something more robust - it cannot dynamically prompt the user to escalate privileges The new sandbox has a different different design which solves these problems: - all restrictions go through seccomp user notifications - this allows us to register a supervisor process with the kernel to validate the syscalls of another program. This supervisor can run arbitrary userspace code to inspect syscalls before allowing/denying/modifying them - we create and upload a policy object to this supervisor which checks syscalls against this policy - if the policy says that the operation requires "ask"-ing, the supervisor will ask zed to show a prompt to the user and report a decision This design is a lot more unified, and since it is much more flexible, is also potentially suitable for sandboxing more parts of zed (user terminals, LSP servers, etc.). Release Notes: - N/A --- .config/typos.toml | 3 + Cargo.lock | 16 +- Cargo.toml | 2 + crates/http_proxy/Cargo.toml | 9 - crates/http_proxy/src/pinned_host.rs | 10 - crates/sandbox/Cargo.toml | 26 +- crates/sandbox/src/bwrap_test_helper.rs | 609 ----- crates/sandbox/src/wsl_sandbox_test_helper.rs | 18 +- crates/sandbox_linux/Cargo.toml | 36 + crates/sandbox_linux/DESIGN.md | 173 ++ crates/sandbox_linux/LICENSE-GPL | 1 + crates/sandbox_linux/examples/end_to_end.rs | 2280 +++++++++++++++++ crates/sandbox_linux/examples/run.rs | 162 ++ crates/sandbox_linux/src/broker.rs | 1209 +++++++++ crates/sandbox_linux/src/dns.rs | 380 +++ crates/sandbox_linux/src/helper.rs | 324 +++ crates/sandbox_linux/src/net.rs | 1370 ++++++++++ crates/sandbox_linux/src/policy.rs | 1496 +++++++++++ crates/sandbox_linux/src/process.rs | 130 + crates/sandbox_linux/src/protocol.rs | 275 ++ crates/sandbox_linux/src/resolve.rs | 285 +++ crates/sandbox_linux/src/sandbox_linux.rs | 703 +++++ crates/sandbox_linux/src/seccomp.rs | 487 ++++ crates/sandbox_linux/src/supervisor.rs | 657 +++++ crates/sandbox_linux/src/sys.rs | 258 ++ crates/zed/Cargo.toml | 3 + crates/zed/src/main.rs | 2 + nix/modules/packages.nix | 3 +- nix/tests/sandboxing/README.md | 77 + nix/tests/sandboxing/bwrap_smoke_test.sh | 117 - nix/tests/sandboxing/default.nix | 436 +--- nix/tests/sandboxing/helper.nix | 23 +- tooling/xtask/src/main.rs | 2 +- tooling/xtask/src/tasks/sandbox_tests.rs | 17 +- 34 files changed, 10392 insertions(+), 1207 deletions(-) delete mode 100644 crates/sandbox/src/bwrap_test_helper.rs create mode 100644 crates/sandbox_linux/Cargo.toml create mode 100644 crates/sandbox_linux/DESIGN.md create mode 120000 crates/sandbox_linux/LICENSE-GPL create mode 100644 crates/sandbox_linux/examples/end_to_end.rs create mode 100644 crates/sandbox_linux/examples/run.rs create mode 100644 crates/sandbox_linux/src/broker.rs create mode 100644 crates/sandbox_linux/src/dns.rs create mode 100644 crates/sandbox_linux/src/helper.rs create mode 100644 crates/sandbox_linux/src/net.rs create mode 100644 crates/sandbox_linux/src/policy.rs create mode 100644 crates/sandbox_linux/src/process.rs create mode 100644 crates/sandbox_linux/src/protocol.rs create mode 100644 crates/sandbox_linux/src/resolve.rs create mode 100644 crates/sandbox_linux/src/sandbox_linux.rs create mode 100644 crates/sandbox_linux/src/seccomp.rs create mode 100644 crates/sandbox_linux/src/supervisor.rs create mode 100644 crates/sandbox_linux/src/sys.rs create mode 100644 nix/tests/sandboxing/README.md delete mode 100644 nix/tests/sandboxing/bwrap_smoke_test.sh diff --git a/.config/typos.toml b/.config/typos.toml index 193af71b8ac..ca6084b7004 100644 --- a/.config/typos.toml +++ b/.config/typos.toml @@ -118,4 +118,7 @@ check-filename = true [default.extend-words] flate = "flate" scap = "scap" +# `RTA_OIF` in `crates/sandbox_linux/src/net.rs` is a rtnetlink attribute +# ("output interface"), not a misspelling of "of". +OIF = "OIF" writeable = "writeable" diff --git a/Cargo.lock b/Cargo.lock index b44f987b873..10553b0b211 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15810,13 +15810,24 @@ dependencies = [ "log", "nix 0.30.1", "seccompiler", - "serde", - "serde_json", "smol", "tempfile", "windows-registry 0.6.1", ] +[[package]] +name = "sandbox_linux" +version = "0.1.0" +dependencies = [ + "anyhow", + "libc", + "log", + "nix 0.30.1", + "serde", + "serde_json", + "tempfile", +] + [[package]] name = "scc" version = "3.8.8" @@ -22619,6 +22630,7 @@ dependencies = [ "reqwest_client", "rope", "sandbox", + "sandbox_linux", "search", "semver", "serde", diff --git a/Cargo.toml b/Cargo.toml index a5ad9f5e7a7..208b1f2c140 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -188,6 +188,7 @@ members = [ "crates/rope", "crates/rpc", "crates/sandbox", + "crates/sandbox_linux", "crates/scheduler", "crates/schema_generator", "crates/search", @@ -464,6 +465,7 @@ rodio = { git = "https://github.com/RustAudio/rodio", rev = "3ee9745862279ce1d18 rope = { path = "crates/rope" } rpc = { path = "crates/rpc" } sandbox = { path = "crates/sandbox" } +sandbox_linux = { path = "crates/sandbox_linux" } scheduler = { path = "crates/scheduler" } search = { path = "crates/search" } session = { path = "crates/session" } diff --git a/crates/http_proxy/Cargo.toml b/crates/http_proxy/Cargo.toml index 648002ec401..724af2d044d 100644 --- a/crates/http_proxy/Cargo.toml +++ b/crates/http_proxy/Cargo.toml @@ -12,15 +12,6 @@ workspace = true path = "src/http_proxy.rs" doctest = false -[features] -# Compiles in the `ZED_SANDBOX_PROXY_ALLOW_LOCAL_IPS` escape hatch that lets the -# proxy reach loopback/private/link-local addresses. This disables the proxy's -# DNS-rebinding/SSRF protection, so it must NEVER be enabled in a real Zed -# build (debug or release) — it exists solely for the NixOS sandbox integration -# tests, whose echo servers live on the VM's private network. Enabled only via -# `sandbox/nixos-test` when building `bwrap-test-helper`. -nixos-integration-tests = [] - [dependencies] anyhow.workspace = true base64.workspace = true diff --git a/crates/http_proxy/src/pinned_host.rs b/crates/http_proxy/src/pinned_host.rs index dd93fb1be73..4ccced6c3c5 100644 --- a/crates/http_proxy/src/pinned_host.rs +++ b/crates/http_proxy/src/pinned_host.rs @@ -156,16 +156,6 @@ impl PinnedHost { /// blocks them for direct connections from the sandbox; code running outside the /// sandbox (the proxy, the fetch tool) must not reopen them. pub fn is_forbidden_ip(ip: IpAddr) -> bool { - // Escape hatch for the NixOS sandbox integration tests only: their echo - // servers live on the VM's private network, which this filter would - // otherwise reject. It is compiled in ONLY under the - // `nixos-integration-tests` feature (enabled via `sandbox/nixos-test` when - // building `bwrap-test-helper`), so in a real Zed build the env var has no - // effect and cannot disable DNS-rebinding/SSRF protection. - #[cfg(feature = "nixos-integration-tests")] - if std::env::var_os("ZED_SANDBOX_PROXY_ALLOW_LOCAL_IPS").is_some() { - return false; - } match ip { IpAddr::V4(v4) => is_forbidden_ipv4(v4), IpAddr::V6(v6) => { diff --git a/crates/sandbox/Cargo.toml b/crates/sandbox/Cargo.toml index 39539da158f..32d96dea48b 100644 --- a/crates/sandbox/Cargo.toml +++ b/crates/sandbox/Cargo.toml @@ -13,30 +13,11 @@ path = "src/sandbox.rs" doctest = false [features] -# Builds `bwrap-test-helper`, the behavior helper driven by the NixOS sandbox -# VM tests in `nix/tests/sandboxing`. Off by default so normal builds don't -# produce the test binary. Pulls in serde only when enabled (the helper parses -# its check list as JSON), and enables the matching `http_proxy` escape hatch so -# the in-process proxy can reach the VM's private-network echo servers. -nixos-test = [ - "dep:serde", - "dep:serde_json", - "http_proxy/nixos-integration-tests", -] - -# Builds `wsl-sandbox-test-helper`, the Windows analog of `bwrap-test-helper`. -# It drives the real WSL/Bubblewrap sandbox end-to-end (see +# Builds `wsl-sandbox-test-helper`, which drives the WSL/Bubblewrap sandbox (see # `script/test-wsl-sandbox.ps1` / `cargo xtask wsl-sandbox-tests`). Off by # default so normal builds and the workspace test run don't produce it. wsl-test = [] -# Behavior test helper for the Linux Bubblewrap sandbox. Only built when the -# `nixos-test` feature is enabled (and only meaningful on Linux). -[[bin]] -name = "bwrap-test-helper" -path = "src/bwrap_test_helper.rs" -required-features = ["nixos-test"] - # Behavior test helper for the Windows WSL/Bubblewrap sandbox. Only built when # the `wsl-test` feature is enabled (and only meaningful on Windows). [[bin]] @@ -49,14 +30,9 @@ anyhow.workspace = true futures.workspace = true http_proxy.workspace = true log.workspace = true -# Used only by the `nixos-test` helper binary to parse its check list; gated -# behind the `nixos-test` feature so normal builds don't pull it in. -serde = { workspace = true, features = ["derive"], optional = true } [target.'cfg(target_os = "linux")'.dependencies] libc.workspace = true -# The `nixos-test` helper binary only parses its check list on Linux. -serde_json = { workspace = true, optional = true } # Safe wrappers for the SCM_RIGHTS fd-passing and `fstat` the bind validator # needs, so that code doesn't hand-roll `msghdr`/`CMSG_*`/`mem::zeroed` unsafe. nix = { workspace = true, features = ["fs", "socket", "uio"] } diff --git a/crates/sandbox/src/bwrap_test_helper.rs b/crates/sandbox/src/bwrap_test_helper.rs deleted file mode 100644 index 0b68bb7b793..00000000000 --- a/crates/sandbox/src/bwrap_test_helper.rs +++ /dev/null @@ -1,609 +0,0 @@ -//! Test binary used exclusively by the NixOS integration tests for the -//! Bubblewrap sandbox at `nix/tests/sandboxing`. See the comment there. -//! -//! It drives the sandbox crate's *public* API only (`Sandbox`, `SandboxPolicy`, -//! …) — never platform internals — so it doubles as a check that the public API -//! is sufficient to express and enforce the policies the agent needs. -//! -//! The list of checks to run is *data*, declared by the Nix test and handed to -//! this binary as a JSON file (path in `ZED_SANDBOX_CHECKS`). Each check -//! describes a sandbox policy, an operation to attempt under that policy, and -//! the expected outcome; this binary executes each and asserts the result. - -#![allow( - clippy::disallowed_methods, - reason = "a single-threaded test helper that intentionally blocks on child processes" -)] - -#[cfg(not(target_os = "linux"))] -fn main() { - eprintln!("bwrap-test-helper is only supported on Linux"); - std::process::exit(1); -} - -#[cfg(target_os = "linux")] -fn main() { - imp::main(); -} - -#[cfg(target_os = "linux")] -mod imp { - use std::io::{Read as _, Write as _}; - use std::net::TcpStream; - use std::path::Path; - use std::time::Duration; - - use anyhow::{Context as _, Result, bail}; - use sandbox::{ - CommandAndArgs, HostFilesystemLocation, Sandbox, SandboxError, SandboxFsPolicy, - SandboxNetPolicy, SandboxPolicy, - }; - use serde::Deserialize; - - /// Internal subcommand: round-trip a byte through the echo server at the - /// given `host:port`, honoring `HTTP_PROXY` when set (the restricted-network - /// case routes through the sandbox proxy via HTTP CONNECT). Exits 0 on a - /// successful round-trip, non-zero otherwise. Run *inside* the sandbox. - const SUBCOMMAND_ECHO_CHECK: &str = "__echo_check"; - - /// Internal subcommand: connect to the unix-domain socket at the given path - /// and round-trip a byte through it. Exits 0 on a successful round-trip, - /// non-zero on any failure (including `socket(AF_UNIX)` being blocked once - /// the seccomp guard lands). Run *inside* the sandbox. - const SUBCOMMAND_UNIX_CONNECT_CHECK: &str = "__unix_connect_check"; - - /// Default port for echo targets given as a bare hostname (e.g. `echo1`). - const DEFAULT_ECHO_PORT: &str = "7000"; - - pub fn main() { - // If we were re-exec'd as the restricted-network bridge, this starts the - // bridge and execs the wrapped command without returning. - sandbox::run_sandbox_launcher_if_invoked(); - - let args: Vec = std::env::args().collect(); - let result = match args.get(1).map(String::as_str) { - Some(SUBCOMMAND_ECHO_CHECK) => run_echo_check(args.get(2).map(String::as_str)), - Some(SUBCOMMAND_UNIX_CONNECT_CHECK) => { - run_unix_connect_check(args.get(2).map(String::as_str)) - } - _ => run_checks(), - }; - - if let Err(error) = result { - eprintln!("[sandbox_test]: FAILED: {error:#}"); - std::process::exit(1); - } - } - - /// Filesystem policy as declared in a check (`fs` field). - #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq)] - #[serde(rename_all = "lowercase")] - enum FsMode { - /// Reads allowed everywhere; writes confined to `writablePaths`. - #[default] - Restricted, - /// Writes allowed anywhere. - Unrestricted, - } - - /// Network policy as declared in a check (`networkAccess` field). - #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq)] - #[serde(rename_all = "lowercase")] - enum NetMode { - /// All outbound network blocked. - #[default] - Blocked, - /// All outbound network allowed. - Unrestricted, - /// Outbound HTTP(S) allowed only to `allowedDomains`. - Restricted, - } - - /// One declarative check: a sandbox policy, an operation, and the expected - /// result. Deserialized from the JSON the Nix test produces. - /// - /// Exactly one operation field (`read`, `write`, `network`, `socketPath`, or - /// `canCreate`) must be set. Policy fields default to the most-confined policy - /// (restricted filesystem with no writable paths, blocked network). - #[derive(Debug, Default, Deserialize)] - #[serde(rename_all = "camelCase", deny_unknown_fields)] - struct Check { - /// Optional human label; falls back to an auto-generated description. - #[serde(default)] - name: Option, - - // ---- policy ---- - #[serde(default)] - fs: FsMode, - #[serde(default)] - writable_paths: Vec, - /// Regular files to create before the policy is built. Used to set up a - /// single-file-worktree fixture: a protected path like - /// `/settings.json/.git` then routes through a real file, so - /// capturing it reports `NotADirectory` — the case production must skip - /// rather than fail on (the `settings.json/.git` regression). - #[serde(default)] - seed_files: Vec, - #[serde(default)] - network_access: NetMode, - #[serde(default)] - allowed_domains: Vec, - /// Paths to protect from writes even if they fall under a writable path. - #[serde(default)] - protected_paths: Vec, - - // ---- operation (exactly one) ---- - /// Read this host path from inside the sandbox. - #[serde(default)] - read: Option, - /// Write to this host path from inside the sandbox. - #[serde(default)] - write: Option, - /// Connect to this echo host (`hostname` or `hostname:port`) from inside - /// the sandbox. - #[serde(default)] - network: Option, - /// Connect to this unix-domain socket path from inside the sandbox. - #[serde(default)] - socket_path: Option, - /// Assert that `Sandbox::can_create` for this policy matches the value: - /// `true` => the sandbox can be created, `false` => it cannot. - #[serde(default)] - can_create: Option, - - // ---- expectation ---- - /// Expected outcome for `read` / `write` / `network`. - #[serde(default)] - succeeds: Option, - /// Expected error kind for `canCreate = false` - /// (`bwrap_not_found` | `setuid_rejected` | `probe_failed`). When - /// omitted, any creation failure is accepted. - #[serde(default)] - error: Option, - } - - fn run_checks() -> Result<()> { - // The restricted-network proxy runs in *this* process and the echo - // servers live on the VM's private network, which the proxy's - // DNS-rebinding protection would otherwise reject. The proxy only honors - // this var when built with `http_proxy/nixos-integration-tests` (pulled - // in by `sandbox/nixos-test`, which this binary requires), so it has no - // effect in a real Zed build. - // SAFETY: single-threaded at this point. - unsafe { - std::env::set_var("ZED_SANDBOX_PROXY_ALLOW_LOCAL_IPS", "1"); - } - - let checks_path = - std::env::var("ZED_SANDBOX_CHECKS").context("ZED_SANDBOX_CHECKS must be set")?; - let raw = std::fs::read_to_string(&checks_path) - .with_context(|| format!("failed to read checks file {checks_path}"))?; - let specs: Vec = - serde_json::from_str(&raw).context("failed to parse checks JSON")?; - let echo_port = - std::env::var("ZED_TEST_ECHO_PORT").unwrap_or_else(|_| DEFAULT_ECHO_PORT.to_string()); - - println!("[sandbox_test]: running {} check(s)", specs.len()); - - let mut checks = Checks::new(); - for spec in &specs { - run_check(spec, &echo_port, &mut checks)?; - } - checks.finish() - } - - fn policy_of(check: &Check) -> Result { - let fs = match check.fs { - FsMode::Unrestricted => SandboxFsPolicy::Unrestricted { - protected_paths: capture_protected_paths(&check.protected_paths), - protected_file_names: Vec::new(), - }, - FsMode::Restricted => { - let mut writable_paths = Vec::new(); - for path in &check.writable_paths { - // A granted directory must exist before its inode can be - // pinned, so create it up front, then capture it. - std::fs::create_dir_all(path) - .with_context(|| format!("failed to create writable path {path}"))?; - writable_paths.push( - HostFilesystemLocation::capture(path) - .with_context(|| format!("failed to capture writable path {path}"))?, - ); - } - let protected_paths = capture_protected_paths(&check.protected_paths); - SandboxFsPolicy::Restricted { - writable_paths, - protected_paths, - protected_file_names: Vec::new(), - } - } - }; - let network = match check.network_access { - NetMode::Unrestricted => SandboxNetPolicy::Unrestricted, - NetMode::Blocked => SandboxNetPolicy::Blocked, - NetMode::Restricted => SandboxNetPolicy::Restricted { - allowed_domains: check.allowed_domains.clone(), - }, - }; - Ok(SandboxPolicy { fs, network }) - } - - /// Capture protected paths best-effort, mirroring production - /// (`SandboxWrap::to_policy`): protect only the ones that exist right now - /// (`capture` succeeding is the existence check) and silently drop the rest. - /// A protection can't be materialized, and `.git` protection has an inherent - /// accepted loophole (`git init` in a non-git dir), so a currently-absent or - /// otherwise-uncapturable protected path is dropped rather than fatal. These - /// are never created here; whether one exists is exactly what several checks - /// turn on. - fn capture_protected_paths(paths: &[String]) -> Vec { - paths - .iter() - .filter_map(|path| HostFilesystemLocation::capture(path).ok()) - .collect() - } - - fn describe(check: &Check) -> String { - if let Some(name) = &check.name { - return name.clone(); - } - let protected = if check.protected_paths.is_empty() { - String::new() - } else { - format!(",protected_paths={:?}", check.protected_paths) - }; - let seed = if check.seed_files.is_empty() { - String::new() - } else { - format!(",seed_files={:?}", check.seed_files) - }; - let policy = format!( - "fs={:?},net={:?}{protected}{seed}", - check.fs, check.network_access - ); - let op = if let Some(path) = &check.read { - format!("read {path}") - } else if let Some(path) = &check.write { - format!("write {path}") - } else if let Some(host) = &check.network { - format!("network {host}") - } else if let Some(path) = &check.socket_path { - format!("socket_connect {path}") - } else if let Some(expected) = check.can_create { - format!("can_create == {expected}") - } else { - "".to_string() - }; - format!("[{policy}] {op}") - } - - fn run_check(check: &Check, echo_port: &str, checks: &mut Checks) -> Result<()> { - let label = describe(check); - - if let Some(expect_can_create) = check.can_create { - let policy = policy_of(check)?; - let outcome = Sandbox::can_create(&policy); - let passed = match (&outcome, expect_can_create) { - (Ok(()), true) => true, - (Ok(()), false) => false, - (Err(_), true) => false, - (Err(error), false) => check - .error - .as_deref() - .map(|expected| error_matches(error, expected)) - .unwrap_or(true), - }; - checks.check(&format!("{label} (got {outcome:?})"), passed); - return Ok(()); - } - - let succeeds = check - .succeeds - .with_context(|| format!("check {label:?} has an operation but no `succeeds`"))?; - - // Create any regular-file fixtures before the policy is built, so a - // protected path routed through one (e.g. `/.git`) reports - // `NotADirectory` at capture time. - for path in &check.seed_files { - let path = Path::new(path); - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent).with_context(|| { - format!("failed to create parent of seed file {}", path.display()) - })?; - } - std::fs::write(path, b"seed\n") - .with_context(|| format!("failed to create seed file {}", path.display()))?; - } - - let actual = if let Some(path) = &check.read { - run_read(check, path)? - } else if let Some(path) = &check.write { - run_write(check, path)? - } else if let Some(host) = &check.network { - run_network(check, host, echo_port)? - } else if let Some(path) = &check.socket_path { - run_socket_connect(check, path)? - } else { - bail!("check {label:?} has no operation"); - }; - - checks.check(&label, actual == succeeds); - Ok(()) - } - - /// Seed a host file, then `cat` it from inside the sandbox. Reads are always - /// granted (root is bound read-only), so this proves the sandbox doesn't - /// *block* reads of existing host files. - fn run_read(check: &Check, path: &str) -> Result { - let path = Path::new(path); - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent) - .with_context(|| format!("failed to create parent of {}", path.display()))?; - } - std::fs::write(path, b"sandbox-test\n") - .with_context(|| format!("failed to seed readable file {}", path.display()))?; - - // Build the policy only after the fixtures exist: capturing a - // `HostFilesystemLocation` pins the inode, so the path must be present. - let policy = policy_of(check)?; - let mut sandbox = Sandbox::new(policy).map_err(sandbox_err)?; - run_command( - &mut sandbox, - "sh", - &["-c", &format!("cat {}", shell_quote(path))], - ) - } - - /// Attempt to write a host file from inside the sandbox. "Succeeded" means - /// the command exited 0 *and* the bytes actually landed on the host file — - /// a write that only hits the sandbox's ephemeral tmpfs counts as blocked, - /// since it never escaped the sandbox. - fn run_write(check: &Check, path: &str) -> Result { - let path = Path::new(path); - if let Some(parent) = path.parent() { - // Create the parent on the host so the only thing under test is the - // sandbox's write permission, not a missing directory. This also - // makes a protected parent exist before the policy captures it. - std::fs::create_dir_all(parent) - .with_context(|| format!("failed to create parent of {}", path.display()))?; - } - // Start from a clean slate so `exists()` afterwards is meaningful. - let _ = std::fs::remove_file(path); - - // Build the policy only after the fixtures exist: capturing a - // `HostFilesystemLocation` pins the inode, so the path must be present. - let policy = policy_of(check)?; - let mut sandbox = Sandbox::new(policy).map_err(sandbox_err)?; - let command_ok = run_command( - &mut sandbox, - "sh", - &[ - "-c", - &format!("printf sandbox-test > {}", shell_quote(path)), - ], - )?; - Ok(command_ok && path.exists()) - } - - /// Connect to `host` (`hostname` or `hostname:port`) from inside the - /// sandbox via the `__echo_check` subcommand, which honors `HTTP_PROXY` for - /// the restricted-network case. - fn run_network(check: &Check, host: &str, echo_port: &str) -> Result { - let target = if host.contains(':') { - host.to_string() - } else { - format!("{host}:{echo_port}") - }; - let exe = current_exe_str()?; - let policy = policy_of(check)?; - let mut sandbox = Sandbox::new(policy).map_err(sandbox_err)?; - run_command(&mut sandbox, &exe, &[SUBCOMMAND_ECHO_CHECK, &target]) - } - - /// Attempt to connect to the unix-domain socket at `path` from inside the - /// sandbox via the `__unix_connect_check` subcommand, returning whether the - /// round-trip succeeded. A read-only bind mount of `/` leaves the socket - /// reachable, so a sandboxed command can currently `connect()` to a session - /// IPC socket owned by a process *outside* the sandbox — the escape a - /// `socket(AF_UNIX)` seccomp filter is meant to block. When that guard lands, - /// `socket(AF_UNIX)` returns `EPERM`, the subcommand fails, and this returns - /// `false`. - fn run_socket_connect(check: &Check, path: &str) -> Result { - let exe = current_exe_str()?; - let policy = policy_of(check)?; - let mut sandbox = Sandbox::new(policy).map_err(sandbox_err)?; - run_command(&mut sandbox, &exe, &[SUBCOMMAND_UNIX_CONNECT_CHECK, path]) - } - - fn error_matches(error: &SandboxError, expected: &str) -> bool { - matches!( - (error, expected), - (SandboxError::BwrapNotFound, "bwrap_not_found") - | (SandboxError::BwrapSetuidRejected, "setuid_rejected") - | (SandboxError::SandboxProbeFailed, "probe_failed") - ) - } - - fn sandbox_err(error: SandboxError) -> anyhow::Error { - anyhow::anyhow!("failed to create sandbox: {error}") - } - - /// Inner command: prove (or fail to prove) reachability of an echo server. - /// - /// With `HTTP_PROXY` set we open an HTTP `CONNECT` tunnel through the proxy - /// and then echo a byte; a policy denial shows up as a non-200 status. With - /// no proxy we connect directly. Either way, a clean round-trip means the - /// destination was reachable under the active network policy. - fn run_echo_check(target: Option<&str>) -> Result<()> { - let target = target.context("echo check requires a host:port argument")?; - let proxy = std::env::var("HTTP_PROXY") - .ok() - .filter(|value| !value.trim().is_empty()) - .or_else(|| { - std::env::var("http_proxy") - .ok() - .filter(|value| !value.trim().is_empty()) - }); - - let mut stream = match proxy { - Some(proxy_url) => { - let proxy_addr = proxy_url - .trim() - .strip_prefix("http://") - .unwrap_or(proxy_url.trim()) - .trim_end_matches('/') - .to_string(); - let mut stream = TcpStream::connect(&proxy_addr) - .with_context(|| format!("failed to connect to proxy {proxy_addr}"))?; - stream.set_read_timeout(Some(Duration::from_secs(10)))?; - write!( - stream, - "CONNECT {target} HTTP/1.1\r\nHost: {target}\r\n\r\n" - ) - .context("failed to send CONNECT to proxy")?; - let status = read_status_line(&mut stream)?; - if !status.contains(" 200") { - bail!("proxy refused CONNECT to {target}: {status:?}"); - } - stream - } - None => { - let stream = TcpStream::connect(target) - .with_context(|| format!("failed to connect to {target}"))?; - stream.set_read_timeout(Some(Duration::from_secs(10)))?; - stream - } - }; - - stream - .write_all(b"ping\n") - .context("failed to write to echo server")?; - let mut buffer = [0u8; 32]; - let read = stream - .read(&mut buffer) - .context("failed to read from echo server")?; - let echoed = String::from_utf8_lossy(&buffer[..read]); - if echoed.contains("ping") { - Ok(()) - } else { - bail!("echo server returned unexpected data: {echoed:?}"); - } - } - - /// Inner command: connect to the unix-domain socket at `path` and round-trip - /// a byte through it. - /// - /// Any failure — `socket(AF_UNIX)` being denied (how the seccomp guard will - /// manifest, as `EPERM`), `connect()` failing, or a bad round-trip — exits - /// non-zero, so the caller reads it as "not connected". A clean round-trip - /// (exit 0) means the socket outside the sandbox was reachable. - fn run_unix_connect_check(path: Option<&str>) -> Result<()> { - use std::os::unix::net::UnixStream; - - let path = path.context("unix connect check requires a socket path argument")?; - let mut stream = UnixStream::connect(path) - .with_context(|| format!("failed to connect to unix socket {path}"))?; - stream.set_read_timeout(Some(Duration::from_secs(10)))?; - stream - .write_all(b"ping\n") - .context("failed to write to unix socket")?; - let mut buffer = [0u8; 32]; - let read = stream - .read(&mut buffer) - .context("failed to read from unix socket")?; - let echoed = String::from_utf8_lossy(&buffer[..read]); - if echoed.contains("ping") { - Ok(()) - } else { - bail!("unix socket returned unexpected data: {echoed:?}"); - } - } - - /// Read an HTTP status line (up to the first CRLF), then drain the rest of - /// the header block (up to the blank line) so the stream is positioned at - /// the tunneled body. - fn read_status_line(stream: &mut TcpStream) -> Result { - let mut header = Vec::new(); - let mut byte = [0u8; 1]; - loop { - let read = stream - .read(&mut byte) - .context("failed reading proxy reply")?; - if read == 0 { - break; - } - header.push(byte[0]); - if header.ends_with(b"\r\n\r\n") { - break; - } - if header.len() > 64 * 1024 { - bail!("proxy reply headers too large"); - } - } - let text = String::from_utf8_lossy(&header); - Ok(text.lines().next().unwrap_or_default().to_string()) - } - - /// Tracks assertion results so we can report a summary and a single exit code. - struct Checks { - passed: usize, - failed: usize, - } - - impl Checks { - fn new() -> Self { - Self { - passed: 0, - failed: 0, - } - } - - fn check(&mut self, description: &str, condition: bool) { - if condition { - self.passed += 1; - println!("[sandbox_test]: PASS: {description}"); - } else { - self.failed += 1; - println!("[sandbox_test]: FAIL: {description}"); - } - } - - fn finish(self) -> Result<()> { - println!( - "[sandbox_test]: summary: {} passed, {} failed", - self.passed, self.failed - ); - if self.failed > 0 { - bail!("{} sandbox assertion(s) failed", self.failed); - } - Ok(()) - } - } - - /// Wrap and run a command inside `sandbox`, returning whether it exited 0. - fn run_command(sandbox: &mut Sandbox, program: &str, args: &[&str]) -> Result { - let command = CommandAndArgs { - program: program.to_string(), - args: args.iter().map(|arg| arg.to_string()).collect(), - env: Default::default(), - cwd: None, - }; - let output = futures::executor::block_on(sandbox.execute(&command)) - .map_err(|error| anyhow::anyhow!("failed to run sandboxed command: {error}"))?; - Ok(output.status.success()) - } - - fn current_exe_str() -> Result { - Ok(std::env::current_exe() - .context("failed to resolve current executable")? - .to_str() - .context("current executable path is not valid UTF-8")? - .to_string()) - } - - /// Single-quote a path for safe interpolation into an `sh -c` script. - fn shell_quote(path: &Path) -> String { - let raw = path.to_string_lossy(); - format!("'{}'", raw.replace('\'', "'\\''")) - } -} diff --git a/crates/sandbox/src/wsl_sandbox_test_helper.rs b/crates/sandbox/src/wsl_sandbox_test_helper.rs index a9c9345e216..6fd67067f10 100644 --- a/crates/sandbox/src/wsl_sandbox_test_helper.rs +++ b/crates/sandbox/src/wsl_sandbox_test_helper.rs @@ -1,10 +1,7 @@ -//! Behavior test helper for the Windows WSL Bubblewrap sandbox — the Windows -//! analog of `bwrap-test-helper`. +//! Behavior test helper for the Windows WSL Bubblewrap sandbox. //! -//! Where the Linux helper is the sandboxed process itself (it re-execs under -//! the launcher), here the sandboxed process is a *Linux* program inside WSL -//! while this helper runs on Windows. So instead of a status channel and a -//! launcher, the helper drives the real [`sandbox::Sandbox`] (`new` + `wrap`), +//! The sandboxed process is a *Linux* program inside WSL while this helper runs +//! on Windows. The helper drives the real [`sandbox::Sandbox`] (`new` + `wrap`), //! spawns the command line it produces, and inspects exit codes and //! host-side filesystem effects to confirm every grant the sandbox makes and //! every restriction it imposes actually holds — including the Windows-specific @@ -13,8 +10,8 @@ //! //! It targets the **default** WSL distro (matching real Zed usage for native //! Windows paths); provision that distro before running (see -//! `script/test-wsl-sandbox.ps1`). Like the Linux helper, it **skips** (rather -//! than fails) the enforcement assertions when the environment can't actually +//! `script/test-wsl-sandbox.ps1`). It **skips** (rather than fails) the +//! enforcement assertions when the environment can't actually //! enforce a sandbox, so a misconfigured WSL doesn't masquerade as a sandbox //! regression. Set `ZED_TEST_SANDBOX_REQUIRE_ENFORCED=1` to turn that skip into //! a failure once you've provisioned an environment that *should* enforce. @@ -70,8 +67,6 @@ mod imp { allow_fs_write: bool, } - /// Tag prefixed to every result line, matching `bwrap-test-helper` so both - /// helpers' output reads the same. const RESULT_TAG: &str = "[sandbox_test]:"; /// `CREATE_NO_WINDOW`: keep `wsl.exe` (a console-subsystem binary) from @@ -723,8 +718,7 @@ mod imp { } } - /// Tracks assertion results so the helper can report a summary and a single - /// exit code, mirroring `bwrap-test-helper`. + /// Tracks assertion results so the helper can report a summary and a single exit code. struct Checks { passed: usize, failed: usize, diff --git a/crates/sandbox_linux/Cargo.toml b/crates/sandbox_linux/Cargo.toml new file mode 100644 index 00000000000..60a59800ee7 --- /dev/null +++ b/crates/sandbox_linux/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "sandbox_linux" +version = "0.1.0" +edition.workspace = true +publish.workspace = true +license = "GPL-3.0-or-later" + +[lints] +workspace = true + +[lib] +path = "src/sandbox_linux.rs" +doctest = false + +# The end-to-end harness is an example, not a test target: CI runs the suite +# with `cargo nextest run --workspace`, which skips targets whose +# required-features are unset. The NixOS VM tests build it explicitly +# (nix/tests/sandboxing/helper.nix). +[features] +__end_to_end_harness = [] + +[[example]] +name = "end_to_end" +path = "examples/end_to_end.rs" +required-features = ["__end_to_end_harness"] + +[target.'cfg(target_os = "linux")'.dependencies] +anyhow.workspace = true +libc.workspace = true +log.workspace = true +nix = { workspace = true, features = ["net", "socket", "uio"] } +serde = { workspace = true, features = ["derive"] } +serde_json.workspace = true + +[target.'cfg(target_os = "linux")'.dev-dependencies] +tempfile.workspace = true diff --git a/crates/sandbox_linux/DESIGN.md b/crates/sandbox_linux/DESIGN.md new file mode 100644 index 00000000000..a0903ea767f --- /dev/null +++ b/crates/sandbox_linux/DESIGN.md @@ -0,0 +1,173 @@ +# Design + +This is a (currently experimental) re-implementation of the sandbox on Linux, with the following goals: +- dynamic changes to the sandbox policy, without restarting the untrusted process +- more flexible policies (e.g. globs, restricting files that don't exist yet) +- reduced "false rejections" (e.g. allowing non-HTTP traffic) + +The "ultimate goal" is that such a sandbox should be reasonable to leave *enabled by default for all processes* (e.g. user terminals, LSP servers, MCP servers, git clients, etc.). + +## Security posture + +Traditional wisdom is that there is a fundamental difference between "missing functionality" and "security issues". I disagree - if the sandbox doesn't allow users to do what they want, the vast majority will simply disable the sandbox. To put it another way, a highly effective privilege escalation attack for an agent is to: +- try to use docker (or similar) to run a legitimate task +- explain to the user: "I can't use docker because of the sandbox, can you disable it?" +- the user disables the sandbox + +## Limitations of the current Zed sandbox + +The current Zed sandbox is, in a vacuum, very secure. However, users can still be exposed to risk in various ways. These ways usually look something like: +- edit the project using sandboxed tools +- this edited configuration triggers an out-of-sandbox program to run malicious code + +For example, a malicious agent can edit your project to add a new Rust proc-macro crate that downloads and runs a malicious script. Rust-analyzer, which runs outside the sandbox, will then execute this proc-macro code and you are compromised. + +We cannot just "run everything in the sandbox", because the current sandbox is only really usable for the agent terminal because it's not very flexible. For example, if we sandbox your user terminal, if you need internet access, we need to kill that process, and restart with the right permissions. + +On Linux, we also have the restriction that filesystem protections can only apply to: + - specific files and directories (globs not supported) + - files/directories that exist at sandbox creation time + +The implementation is also (subjectively) ugly: +- `HostFilesystemLocation` is a bit of a monstrosity, but is required for security on Linux/WSL, due to race conditions when creating namespaces through `bwrap` + - It also doesn't fully guarantee TOCTOU-freedom on WSL when going across the plan9 bridge to the NTFS side +- The HTTP proxy is an entirely separate mechanism to FS sandboxing +- There's lots of "wrapper programs" and awkward message passing + +The new design unifies a lot of this. + +## The new design + +The new design aims to fix this by broadly shifting from "setting up an environment with restricted permissions" to "intercepting syscalls and dynamically resolving them against the policy". + +There are two components to this: +- filesystem (including domain sockets and friends) +- networking + +Both of these rely on seccomp user notifications. This is a Linux kernel feature that extends seccomp's syscall-filtering ability, by allowing a user "supervisor" program to (conceptually) dynamically allow/deny[^1] syscalls. This supervisor can run arbitrary code, including code that sends a prompt to the user to confirm the action. + +The rough flow would then become: +- each sandboxed program has a policy, which is uploaded to the supervisor +- when the untrusted program tries to make a syscall that is sensitive, the supervisor approves/denies/prompts the user, depending on the policy, and the specific syscall made +- when the user changes the policy (either manually, or by approving a prompt), the new policy is uploaded to the supervisor, and takes effect immediately + +### Implementation + +I will refer to three processes: +- the owner process - the process that sets up the sandbox (i.e. Zed) +- the sandbox process - the process that the untrusted command runs in + - technically this is actually two processes, an in-sandbox helper that then spawns the untrusted command +- the supervisor process - the process that is registered with the kernel to validate syscalls made by the sandbox process + +There is a `SandboxPolicy` object which contains a list of rules. For example, for filesystem sandboxing a rule looks like: +```rust +struct FsRule { + path: PathPattern, + read: Option, // read a file at this path + write: Option, // write a file at this path + connect: Option, // connect to a socket at this path +} + +enum Access { + Allow, + Deny, + Ask, +} +``` + +The owner process sets the policy, and a more cache-friendly representation gets sent to the supervisor. When the sandbox process tries to make a syscall that is not explicitly allowed to run untrapped, the arguments get sent to the supervisor. + +The supervisor makes the call on behalf of the sandboxed program, and copies arguments into its own memory to close a TOCTOU where the sandboxed process can pass a pointer to the supervisor, get the data validated, then change the data behind that pointer before the syscall is actually executed. + +### Network-specific considerations + +The same principle applies to network sandboxing, with some extra machinery. + +In particular, the kernel deals only with IP addresses, but we would like to be able to grant permission to access domain names such as `google.com`. We do this by (loosely): +- configuring the namespace such that DNS queries go to a resolver running in the supervisor process +- when the sandboxed process makes a DNS request for `google.com`, we return a synthetic IP address +- the supervisor records the mapping between `google.com` and the synthetic IP address +- when the sandboxed process tries to connect to that synthetic IP address, the supervisor can convert it back to `google.com`, and look at the policy to understand how to react (allow, deny, ask) + - the real DNS lookup also goes through a rebinding check, so I can't grant access to `attacker_controlled_domain.com` and have that grant access to `127.0.0.1` for example + +There is then a relay, that maps these synthetic IP addresses outside the sandbox. This lives in the supervisor. It creates real connections to the actual IP addresses for the domains (using the system DNS resolver). So when it succeeds, there are two legs: +- sandbox <-> supervisor +- supervisor <-> `google.com` + +The relay just plainly copies bytes from one to the other. + +### `Ask` mode + +When a permission is in `Ask` mode, it blocks the syscall indefinitely(ish) until the owner process responds. The `Handler` type in a `Ask` variant is created by providing a callback that takes a `Responder`. This type is how the custom user code can indicate to the supervisor what the result was. It *must* do this using the dedicated methods. Dropping it is considered an error, which: +- panics in debug mode +- fails closed and warns in release mode + +When it responds, by default it responds *only for the current syscall*. Unlike the old sandbox implementation, there's no obvious way to say "allow this syscall again in the future", because it often refers to ephemeral state (e.g. file descriptors). For example, if you allow writes to `/foo/bar/baz.rs`, and you want that approval to persist for the rest of the sandbox, should that apply to: +- only `/foo/bar/baz.rs` +- `/foo/bar` +- `/foo` +- `/` + +The `linux_sandbox` crate doesn't address this issue, but provides the `Decision` type, which is what a `Responder` actually responds with. It contains a decision for this syscall, but also a list of rules to remember for the rest of this session. It is up zed to determine what options to show to the user. + +It's almost always wrong to not supply any extra rules to remember. + +## Tradeoffs + +The one major tradeoff is performance: this design imposes a round trip to the supervisor on every syscall that may be blocked (opening files and sockets, writing, connecting). On a 9950x3d, a trapped `open` costs ~10 microseconds compared to ~0.4 microseconds untrapped. Relaying a 1 gigabit stream cost about 2.5% of a CPU core.. `stat` and friends, directory listing, and `execve` are not restricted, so don't get the overhead. + +## Implementation ideas (ai generated) + +### Future improvements + +#### Synthetic IPv6 addresses for domains + +The sandbox's DNS only hands out synthetic IPv4 addresses (from `198.18.0.0/15`). A connection to a domain has two legs, joined by the supervisor's relay, which copies bytes rather than translating packets: + +1. app to relay, inside the sandbox, using the synthetic address; +2. supervisor to the real server, on the host network, using whatever the name resolves to there (IPv6 included). + +So leg 1 is always IPv4, while the real server can still be IPv6-only. Queries for IPv6 addresses (`AAAA`) get an empty answer (`NOERROR`, no records) rather than `NXDOMAIN`, so resolvers fall back to the IPv4 answer cleanly. + +Raw IPv6 addresses (IPv6 grants, `curl http://[2606:4700::1]`) still work: the relay listens at the exact address the app connects to, with `IP_FREEBIND` set on the socket. Without it, IPv6 `bind()` only accepts addresses assigned to an interface and ignores the namespace's local routes, unlike IPv4. + +What this gives up, all uncommon: +- explicitly IPv6-only invocations (`curl -6`, `ssh -6`) get no address for a domain; +- tools in IPv6-only environments that query only `AAAA` records; +- agents debugging IPv6 itself get a confusing result. + +If one of these matters, add a synthetic IPv6 range (a ULA `/64`) alongside the IPv4 one, using the same `IP_FREEBIND` relay path. + +#### Revoking network grants + +Narrowing the policy currently leaves existing TCP connections and UDP flows alive, just as already-open files keep working. If revoking a grant should cut access off, `update_policy` could report the connections and flows the new policy wouldn't allow (like `still_open` does for files) and offer to close them. + +#### Relaying DNS to real servers + +Port 53 of every address in the sandbox is answered by the supervisor's DNS. That's how queries reach it whatever server `resolv.conf` names, but it also means `dig @8.8.8.8` gets our answers: it's indistinguishable from the system resolver asking its configured server. Relaying instead has costs: real answers make later connections raw-IP requests, so domain rules no longer apply, and an allowed resolver is a channel for exfiltrating data in queries. A middle ground: relay port 53 only when an `Ip` rule names that exact address and port, and answer everything else ourselves (including under `Any` or `LocalNetwork` rules). + +#### Async supervisor + +Every relayed TCP connection takes two threads, and every UDP flow and DNS query one, so a busy `npm install` can mean hundreds of threads. An event loop in the supervisor (`smol`, which Zed already uses: `async-io` plus a small executor) would make these tasks instead; seccomp notifications would stay with their leader/follower receivers, since prompts block. + +Measured (9950X3D; benchmark harness in the benchmarking thread): at 1 Gbit/s every relay model costs 2.5–3% of a core, so throughput alone doesn't motivate this. For bursts of short connections, async halves the relay's CPU per exchange (21 vs 48 µs) and replaces 626 threads at 1000 concurrent connections with 3. Not urgent; revisit if supervisor resource usage becomes a problem. + +#### Exact errors for non-blocking connects + +A `connect` on a non-blocking socket succeeds as soon as it's allowed, and the host connection is made in the background: holding the syscall instead would freeze the app's event loop for a round trip per connection. A host that refuses or can't be reached then shows up as a reset after a successful connect (`curl: (56) Recv failure: Connection reset by peer` instead of `Connection refused`). Most clients use non-blocking sockets (curl, Go, Node, Python sockets with a timeout), so this is the common case. A middle ground: wait briefly for the host connect (a few milliseconds, enough for local and fast failures) before returning early. + +#### Sandbox differences skill + +In-sandbox behaviour is subtly different from unsandboxed Linux (exact error codes on denied operations, resets instead of refusals after non-blocking connects, `getsockname` paths, `SO_PEERCRED` pids). Collect the differences into a documented "skill" that agents can be given, so tools don't misread sandbox artefacts as environment failures. + +#### Datagram unix sockets + +`socket` and `socketpair` refuse `SOCK_DGRAM` unix sockets: one could send to any socket by path with `sendmsg`, whose address is behind a pointer, and seccomp can't read the address to notice. The same trap would also let unconnected UDP `sendto` reach ports no rule names: today those datagrams go nowhere, because a relay socket only exists for ports the policy names. This breaks `sd_notify`, syslog clients and datagram pairs used for IPC. + +Measured (9950X3D; benchmark harness in the benchmarking thread): trapping every `send*` costs +10.8 µs of CPU per send with the current notification path — the supervisor saturates a core at 1 Gbit/s of QUIC-sized datagrams. An optimized path (answer on the receiving thread, `SYNC_WAKE_UP`, cached pidfds) brings it to ~+2.2 µs per send: +28% of a core at 1 Gbit/s, and +1% or less with GSO or `sendmmsg` batches of 16. Whether that budget is acceptable is a product call. + +#### Unix socket peer credentials + +The supervisor makes brokered `connect` calls on its copy of the app's socket, so a server's `SO_PEERCRED` reports the supervisor's pid (the uid is the same). Servers that authorize by pid would see the wrong process. + +[^1]: Technically, the supervisor doesn't "allow or deny" the syscall, since this introduces a race condition, where a malicious program makes a syscall, then, in between approval and the kernel executing it, changes the contents of one of the arguments which is behind a pointer (e.g. file paths). Instead, the syscall arguments (including any data behind a pointer) are copied into the supervisor, and the supervisor *makes the syscall on behalf of the sandboxed program*, and returns the result. \ No newline at end of file diff --git a/crates/sandbox_linux/LICENSE-GPL b/crates/sandbox_linux/LICENSE-GPL new file mode 120000 index 00000000000..89e542f750c --- /dev/null +++ b/crates/sandbox_linux/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/sandbox_linux/examples/end_to_end.rs b/crates/sandbox_linux/examples/end_to_end.rs new file mode 100644 index 00000000000..0dff7dfa50f --- /dev/null +++ b/crates/sandbox_linux/examples/end_to_end.rs @@ -0,0 +1,2280 @@ +//! End-to-end checks against a real sandbox, driven by a table of scenarios. +//! +//! Each [`Scenario`] in the table below is data: the fixtures it needs on the +//! host, the policy it runs under, one action the untrusted command attempts, +//! and what must happen — including how long the action must be blocked on an +//! `Ask` prompt, how many prompts may be shown, which denial must be reported, +//! and what must hold on the host afterwards. Every scenario runs in a fresh +//! directory under a fresh sandbox, so each policy states exactly what it +//! allows and scenarios can't interact. +//! +//! Behaviour that is inherently a sequence — narrowing the policy while a file +//! is held open, stopping a running command, a prompt cancelled by the command +//! ending — is kept as dedicated checks after the table. +//! +//! Declared as an example gated behind `__end_to_end_harness`, not a test +//! target: CI's `cargo nextest run --workspace` never builds or runs it. The +//! sandbox re-executes this binary as its own supervisor and helper, so it +//! needs its own `main()`; the NixOS VM tests build it with Crane and run it +//! as `sandbox-linux-test-helper` (nix/tests/sandboxing/default.nix). +//! +//! The actions need Python 3, `unshare` and the usual coreutils on PATH, +//! checked before running. Echo peers run in-process on the host, so no +//! network services are needed. Fixtures live under +//! `/tmp/sandbox-escape-test-*`, so a sandbox bug can't write anywhere that +//! matters. + +#[cfg(not(target_os = "linux"))] +fn main() {} + +#[cfg(target_os = "linux")] +fn main() { + sandbox_linux::run_hidden_entrypoint_if_invoked(); + imp::main(); +} + +#[cfg(target_os = "linux")] +#[allow( + clippy::disallowed_methods, + reason = "a standalone test binary that blocks on sandboxed commands" +)] +mod imp { + use std::fmt::Write as _; + use std::os::unix::fs::FileTypeExt as _; + use std::path::{Path, PathBuf}; + use std::process::{Output, Stdio}; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::{Arc, Mutex}; + use std::time::{Duration, Instant}; + + use sandbox_linux::{ + Access::{self, Allow, Deny}, + Decision, Denial, DomainPattern, FsRule, Handler, NetHost, NetRule, NetTarget, Permission, + PortSet, Protocol, RequestedAccess, Rule, SandboxContext, SandboxPolicy, UntrustedCommand, + }; + + /// A delayed `Ask` answer takes this long, so scenarios that block on it + /// can assert the syscall was genuinely held, not failed straight away. + const ASK_DELAY: Duration = Duration::from_millis(600); + /// Slack when asserting a syscall was blocked, so scheduler jitter under a + /// loaded machine can't flake the timing check. + const TIMING_SLACK: Duration = Duration::from_millis(150); + + /// The host's own namespaces, to prove the sandboxed command left them. + struct Ctx { + namespaces: Vec<(&'static str, String)>, + } + + /// The echo peers and free ports scenarios connect to. + struct World { + /// Kept bound, never listened: `tcp_closed` stays deterministically refused. + _closed: std::os::fd::OwnedFd, + tcp_allowed: u16, + tcp_asked: u16, + tcp_unlisted: u16, + /// Allowed by policy, but nothing listens there. + tcp_closed: u16, + tcp_rebinding: u16, + udp_allowed: u16, + udp_unlisted: u16, + unix_allowed: PathBuf, + unix_denied: PathBuf, + unix_denied_bind: PathBuf, + } + + /// One scenario: fixtures, policy, action, expectation. + struct Scenario { + name: &'static str, + /// This scenario's own directory: its cwd, and the root its fixtures + /// and policy are expressed from. + home: PathBuf, + fixtures: Vec, + policy: SandboxPolicy, + action: Action, + expected: Expected, + /// The action must take at least this long: it was held on a prompt + /// rather than failing straight away. + blocked_for: Option, + /// How many prompts the scenario's handler must have answered. + expected_prompts: Option, + /// A denial the harness must see reported through `Sandbox::on_denial`. + reported: Option, + /// What must hold on the host afterwards. + host_checks: Vec, + prompts: Arc, + denials: Arc>>, + } + + /// What exists on the host before the sandbox runs. + enum Fixture { + File { + path: PathBuf, + contents: &'static str, + }, + Directory { + path: PathBuf, + }, + Symlink { + target: PathBuf, + link: PathBuf, + }, + } + + /// What the untrusted command attempts. + enum Action { + // Filesystem. + Read { + path: PathBuf, + }, + Write { + path: PathBuf, + }, + Append { + path: PathBuf, + }, + CreateDirectory { + path: PathBuf, + }, + Delete { + path: PathBuf, + }, + Rename { + from: PathBuf, + to: PathBuf, + }, + Symlink { + target: PathBuf, + link: PathBuf, + }, + HardLink { + target: PathBuf, + link: PathBuf, + }, + List { + path: PathBuf, + }, + Stat { + path: PathBuf, + }, + /// `open(2)` through the client, asserting the errno on denial. + /// `r`: `O_RDONLY`; `w`: `O_WRONLY | O_TRUNC`; `wc`: plus `O_CREAT`. + Open { + path: PathBuf, + mode: &'static str, + }, + /// Write (and read back) a file under the sandbox's `TMPDIR`. + WriteTemp { + file: &'static str, + }, + /// `cat /proc/$HOST_PID/environ`: another process's environment. + ReadHostEnvironment, + /// `kill -0` a process outside the execution. + SignalHost, + /// `unshare -r`: a new user namespace. + NewUserNamespace, + /// `cat /proc/self/status` and `/proc/$$/cmdline`. + ReadOwnProc, + /// Assert the sandbox's namespaces differ from the host's. + AssertNamespaces, + /// Assert the command's capability sets are empty. + AssertNoCapabilities, + /// A composite shell snippet, for steps that only mean something + /// together (`chmod` then `test -w`, signalling a group, ...). + Shell(String), + + // Unix sockets. + ConnectUnix { + path: PathBuf, + }, + BindUnix { + path: PathBuf, + }, + /// Serve on a unix socket in the sandbox's `TMPDIR`, then connect. + UnixServer { + file: &'static str, + }, + /// Serve on an abstract (namespace-local) name, then connect. + AbstractUnixServer { + name: &'static str, + }, + /// `socketpair` of this kind (`SOCK_*`). + SocketPair { + kind: &'static str, + }, + /// A datagram unix socket: `socket()` or a `socketpair`. + DatagramUnix, + + // Network. + Connect { + host: String, + port: u16, + }, + ConnectNonBlocking { + host: String, + port: u16, + }, + /// Resolve a name with the sandbox's DNS, then connect to it. + ConnectByName { + name: &'static str, + port: u16, + }, + /// Serve and connect on TCP inside the sandbox; no rule needed. + LocalServer, + Udp { + host: String, + port: u16, + connected: bool, + }, + /// A raw DNS query for a name. + Dns { + name: &'static str, + }, + /// `getaddrinfo` a name. + Resolve { + name: &'static str, + }, + /// A ping (`ICMP`) socket. + Ping, + } + + /// What must happen. + #[derive(Debug)] + enum Expected { + /// The command exits 0. + Allowed, + /// The command exits non-zero, any way it likes. + Denied, + /// The client asserts the failing syscall returned one of these errnos + /// (or, for `open`, one of `EACCES`/`EROFS`) and exits 0 when it does. + Errno(&'static [i32]), + /// A non-blocking connect succeeds locally, then the connection is + /// reset: the host leg was refused. + Reset, + /// The name resolves, to a fake address from the sandbox's DNS. + FakeAddress, + /// The name does not resolve (NXDOMAIN). + NxDomain, + /// The name resolves, but has no record of the type asked. + EmptyRecord, + } + + impl Expected { + fn holds(&self, output: &Output) -> bool { + match self { + Expected::Denied => !output.status.success(), + _ => output.status.success(), + } + } + } + + /// What must hold on the host after a scenario ran. + enum HostCheck { + Contents { path: PathBuf, equals: &'static str }, + Absent(PathBuf), + Socket(PathBuf), + } + + /// How an `Ask` handler answers. + #[derive(Clone)] + enum Ask { + Immediate(Answer), + /// Answer after a delay, from another thread, so the syscall (and + /// with it the command) is genuinely blocked meanwhile. + Delayed(Duration, Answer), + } + + #[derive(Clone)] + enum Answer { + Allow, + Deny, + /// Allow, and remember a rule letting later writes through. + Remember(String), + Panic, + } + + impl Action { + /// The command the sandbox runs for this action, as an `sh -c` script. + fn script(&self, ctx: &Ctx, expected: &Expected) -> String { + match self { + Action::Read { path } => format!("cat {}", shell(path)), + Action::Write { path } => format!("echo content > {}", shell(path)), + Action::Append { path } => format!("echo content >> {}", shell(path)), + Action::CreateDirectory { path } => format!("mkdir -p {}", shell(path)), + Action::Delete { path } => format!("rm -r {}", shell(path)), + Action::Rename { from, to } => { + format!("mv {} {}", shell(from), shell(to)) + } + Action::Symlink { target, link } => { + format!("ln -s {} {}", shell(target), shell(link)) + } + Action::HardLink { target, link } => { + format!("ln {} {}", shell(target), shell(link)) + } + Action::List { path } => format!("ls {}", shell(path)), + Action::Stat { path } => format!("stat {}", shell(path)), + Action::Open { path, mode } => py_open(path, mode), + Action::WriteTemp { file } => { + format!("echo x > \"$TMPDIR/{file}\" && cat \"$TMPDIR/{file}\"") + } + Action::ReadHostEnvironment => "cat /proc/$HOST_PID/environ".to_string(), + Action::SignalHost => "kill -0 $HOST_PID".to_string(), + Action::NewUserNamespace => "unshare -r true".to_string(), + Action::ReadOwnProc => { + "cat /proc/self/status >/dev/null && cat /proc/$$/cmdline >/dev/null" + .to_string() + } + Action::AssertNamespaces => { + let mut script = String::from("python3 - <<'PY'\nimport os\n"); + for (namespace, host) in &ctx.namespaces { + let _ = writeln!( + script, + "assert os.readlink('/proc/self/ns/{namespace}') != {host:?}, \ + '{namespace} namespace is the host''s'" + ); + } + format!("{script}PY") + } + Action::AssertNoCapabilities => { + "python3 -c 'status = open(\"/proc/self/status\").read(); \ + assert \"CapEff:\t0000000000000000\" in status, status'" + .to_string() + } + Action::Shell(script) => script.clone(), + Action::ConnectUnix { path } => { + let expected = match expected { + Expected::Errno(errnos) => net_errnos(errnos), + _ => String::new(), + }; + net(&format!("unix-connect {} {expected}", shell(path))) + } + Action::BindUnix { path } => { + let expected = match expected { + Expected::Errno(errnos) => net_errnos(errnos), + _ => String::new(), + }; + net(&format!("unix-bind {} {expected}", shell(path))) + } + Action::UnixServer { file } => net(&format!("unix-server \"$TMPDIR/{file}\"")), + Action::AbstractUnixServer { name } => net(&format!("unix-server @{name}")), + Action::SocketPair { kind } => net(&format!("socketpair {kind}")), + Action::DatagramUnix => net("datagram-unix"), + Action::Connect { host, port } => { + let expected = match expected { + Expected::Errno(errnos) => net_errnos(errnos), + _ => String::new(), + }; + net(&format!("tcp {host} {port} {expected}")) + } + Action::ConnectNonBlocking { host, port } => { + let expected = match expected { + Expected::Reset => "reset".to_string(), + Expected::Errno(errnos) => net_errnos(errnos), + _ => String::new(), + }; + net(&format!("tcp-nonblocking {host} {port} {expected}")) + } + Action::ConnectByName { name, port } => { + let expected = match expected { + Expected::Errno(errnos) => net_errnos(errnos), + _ => String::new(), + }; + net(&format!("tcp-name {name} {port} {expected}")) + } + Action::LocalServer => net("local-server"), + Action::Udp { + host, + port, + connected, + } => net(&format!( + "udp {host} {port} {}", + if *connected { "connect" } else { "sendto" } + )), + Action::Dns { name } => { + let verdict = match expected { + Expected::FakeAddress => "fake", + Expected::NxDomain => "nxdomain", + Expected::EmptyRecord => "empty", + _ => "fake", + }; + let record = match expected { + Expected::EmptyRecord => "28", + _ => "1", + }; + net(&format!("dns {name} {verdict} {record}")) + } + Action::Resolve { name } => net(&format!("resolve {name}")), + Action::Ping => { + let expected = match expected { + Expected::Errno(errnos) => net_errnos(errnos), + _ => String::new(), + }; + net(&format!("ping-socket {expected}")) + } + } + } + } + + /// Quote a path for a shell script. + fn shell(path: &Path) -> String { + let text = path.to_string_lossy(); + if text.chars().any(|character| { + character.is_whitespace() || "[]*?{}()<>|&;\"'`\\$#".contains(character) + }) { + format!("'{}'", text.replace('\'', "'\\''")) + } else { + text.into_owned() + } + } + + /// The client script (`net.py`) is written into each scenario's home, + /// which is also the command's cwd, so scripts refer to it relatively. + fn net(arguments: &str) -> String { + format!("python3 net.py {arguments}") + } + + /// The errno names the net client asserts (Python `errno` attributes). + fn net_errnos(errnos: &'static [i32]) -> String { + errnos + .iter() + .map(|errno| match *errno { + libc::EACCES => "EACCES", + libc::EPERM => "EPERM", + libc::ECONNREFUSED => "ECONNREFUSED", + libc::EROFS => "EROFS", + _ => unreachable!("unmapped errno in the scenario table"), + }) + .collect::>() + .join(" ") + } + + /// Assert `open(path, mode)` fails with a permission error (`EACCES`, + /// or `EROFS` for writes: the message tools expect for a read-only + /// mount). + fn py_open(path: &Path, mode: &str) -> String { + let flags = match mode { + "r" => "os.O_RDONLY", + "w" => "os.O_WRONLY | os.O_TRUNC", + "wc" => "os.O_WRONLY | os.O_CREAT | os.O_TRUNC", + _ => unreachable!("unknown open mode"), + }; + let lines = [ + "import errno, os".to_string(), + format!("path = {:?}", shell(path)), + format!("flags = {flags}"), + "try:".to_string(), + " descriptor = os.open(path, flags)".to_string(), + "except OSError as error:".to_string(), + " assert error.errno in (errno.EACCES, errno.EROFS), error".to_string(), + "else:".to_string(), + " os.close(descriptor)".to_string(), + " raise AssertionError('denied open succeeded: ' + path)".to_string(), + ]; + let script = lines.join("\n"); + format!("python3 - <<'PY'\n{script}\nPY") + } + + /// The `Ask` handler for one scenario: counts its prompts and answers + /// them as told. + fn ask_handler(prompts: Arc, ask: Ask) -> Handler { + Handler::new(move |_, responder| { + prompts.fetch_add(1, Ordering::SeqCst); + match ask.clone() { + Ask::Immediate(answer) => answer_the(responder, answer), + Ask::Delayed(delay, answer) => { + std::thread::spawn(move || { + std::thread::sleep(delay); + answer_the(responder, answer); + }); + } + } + }) + } + + fn answer_the(responder: sandbox_linux::Responder, answer: Answer) { + match answer { + Answer::Allow => responder.respond(Decision::allow()), + Answer::Deny => responder.respond(Decision::deny()), + Answer::Remember(pattern) => responder.respond(Decision { + allow: true, + remember: vec![Rule::Fs(FsRule::new(pattern).unwrap().write(Allow))], + }), + Answer::Panic => panic!("test handler panics on purpose"), + } + } + + /// Builds the scenario table: each scenario gets its own numbered + /// directory under `root`, so scenarios are independent. + struct Table { + root: PathBuf, + scenarios: Vec, + } + + impl Table { + fn new(root: PathBuf) -> Self { + Self { + root, + scenarios: Vec::new(), + } + } + + /// Add a scenario: fixtures, policy, action, expectation. The policy + /// is built from the handler its `Ask` rules must use, and the + /// scenario's home (its fixtures and relative paths live there). + fn push( + &mut self, + name: &'static str, + fixtures: Vec, + ask: Ask, + action: Action, + expected: Expected, + policy: impl FnOnce(Handler, &Path) -> SandboxPolicy, + ) { + let home = self + .root + .join("scenarios") + .join(format!("{:03}", self.scenarios.len())); + let prompts = Arc::new(AtomicUsize::new(0)); + let denials = Arc::new(Mutex::new(Vec::new())); + let ask = match ask { + Ask::Immediate(Answer::Remember(pattern)) => Ask::Immediate(Answer::Remember( + format!("{}/ask/{pattern}", home.display()), + )), + Ask::Delayed(delay, Answer::Remember(pattern)) => Ask::Delayed( + delay, + Answer::Remember(format!("{}/ask/{pattern}", home.display())), + ), + other => other, + }; + let handler = ask_handler(prompts.clone(), ask); + let policy = policy(handler, &home); + self.scenarios.push(Scenario { + name, + home, + fixtures, + policy, + action, + expected, + blocked_for: None, + expected_prompts: None, + reported: None, + host_checks: Vec::new(), + prompts, + denials, + }); + } + + /// The action must be held on its prompt for at least `at_least`. + fn blocked_for(&mut self, at_least: Duration) { + self.scenarios.last_mut().unwrap().blocked_for = Some(at_least); + } + + fn expected_prompts(&mut self, count: usize) { + self.scenarios.last_mut().unwrap().expected_prompts = Some(count); + } + + fn reported(&mut self, mut request: RequestedAccess) { + // Denials carry absolute paths; the table writes relative ones. + match &mut request { + RequestedAccess::Fs { path, .. } => { + let home = self.scenarios.last().unwrap().home.clone(); + if path.is_relative() { + *path = home.join(&*path); + } + } + RequestedAccess::Net { .. } => {} + } + self.scenarios.last_mut().unwrap().reported = Some(request); + } + + fn after(&mut self, checks: Vec) { + self.scenarios.last_mut().unwrap().host_checks = checks; + } + } + + fn file(path: &str, contents: &'static str) -> Fixture { + Fixture::File { + path: PathBuf::from(path), + contents, + } + } + fn directory(path: &str) -> Fixture { + Fixture::Directory { + path: PathBuf::from(path), + } + } + fn symlink(target: &str, link: &str) -> Fixture { + Fixture::Symlink { + target: PathBuf::from(target), + link: PathBuf::from(link), + } + } + + /// The full scenario table. `root` is the fixture root (every scenario's + /// home, and every peer socket, lives under it), `world` the peers and + /// free ports. Scenario-relative paths (fixtures, actions, host checks) + /// resolve inside each scenario's own home. + fn table(root: &Path, world: &World) -> Vec { + let mut t = Table::new(root.to_path_buf()); + + // Policies, as closures over (handler, home). + let read_all = |_: Handler, _: &Path| SandboxPolicy { + fs: vec![FsRule::new("/").unwrap().read(Allow)], + net: Vec::new(), + }; + let writable = |_: Handler, home: &Path| SandboxPolicy { + fs: vec![ + FsRule::new("/").unwrap().read(Allow), + FsRule::new(home.to_string_lossy()).unwrap().write(Allow), + ], + net: Vec::new(), + }; + let proj_writable_secret_denied = |_: Handler, home: &Path| SandboxPolicy { + fs: vec![ + FsRule::new("/").unwrap().read(Allow), + FsRule::new(home.join("proj").to_string_lossy()) + .unwrap() + .write(Allow), + FsRule::new(home.join("proj/.git").to_string_lossy()) + .unwrap() + .write(Deny), + FsRule::new(home.join("secret").to_string_lossy()) + .unwrap() + .read(Deny), + FsRule::new("/**/*.env").unwrap().read(Deny), + ], + net: Vec::new(), + }; + let ask_write = |handler: Handler, home: &Path| SandboxPolicy { + fs: vec![ + FsRule::new("/").unwrap().read(Allow), + FsRule::new(home.join("ask").to_string_lossy()) + .unwrap() + .write(Access::Ask(handler)), + ], + net: Vec::new(), + }; + let unix_policy = |_: Handler, home: &Path| SandboxPolicy { + fs: vec![ + FsRule::new("/").unwrap().read(Allow), + FsRule::new(home.to_string_lossy()).unwrap().write(Allow), + FsRule::new(world.unix_allowed.to_string_lossy()) + .unwrap() + .connect(Allow), + ], + net: Vec::new(), + }; + let net_policy = |handler: Handler, _: &Path| SandboxPolicy { + fs: vec![FsRule::new("/").unwrap().read(Allow)], + net: vec![ + // No `tcp_rebinding` here: the name rule allows that port, + // and the rebinding check must then deny the address it + // resolves to. + NetRule::new(NetTarget::Ip("127.0.0.1".parse().unwrap()), Allow) + .ports(PortSet::Ranges(vec![ + world.tcp_allowed..=world.tcp_allowed, + world.tcp_closed..=world.tcp_closed, + ])) + .tcp_only(), + NetRule::new(NetTarget::Ip("127.0.0.1".parse().unwrap()), Allow) + .ports(PortSet::Ranges(vec![world.udp_allowed..=world.udp_allowed])) + .udp_only(), + NetRule::new( + NetTarget::Domain(DomainPattern::new("localhost").unwrap()), + Allow, + ) + .ports(PortSet::Ranges(vec![ + world.tcp_allowed..=world.tcp_allowed, + world.tcp_rebinding..=world.tcp_rebinding, + ])) + .tcp_only(), + NetRule::new(NetTarget::Loopback, Access::Ask(handler.clone())) + .ports(PortSet::Ranges(vec![world.tcp_asked..=world.tcp_asked])) + .tcp_only(), + NetRule::new( + NetTarget::Domain(DomainPattern::new("ask.test").unwrap()), + Access::Ask(handler), + ), + ], + }; + // Enough for the loader and `sh` to run, nothing for the target. + let loader_only = |_: Handler, _: &Path| SandboxPolicy { + fs: vec![ + FsRule::new("/nix").unwrap().read(Allow), + FsRule::new("/run/current-system").unwrap().read(Allow), + FsRule::new("/dev/null").unwrap().read(Access::Allow), + ], + net: Vec::new(), + }; + + let allow = Ask::Immediate(Answer::Allow); + + // -- Filesystem: the sandbox holds up its side of the bargain -- + + t.push( + "read an allowed file", + vec![file("proj/app.txt", "app")], + allow.clone(), + Action::Read { + path: PathBuf::from("proj/app.txt"), + }, + Expected::Allowed, + read_all, + ); + t.push( + "read a file no rule allows", + vec![file("proj/app.txt", "app")], + allow.clone(), + Action::Read { + path: PathBuf::from("proj/app.txt"), + }, + Expected::Denied, + loader_only, + ); + t.push( + "write into a writable grant", + vec![directory("proj")], + allow.clone(), + Action::Write { + path: PathBuf::from("proj/new.txt"), + }, + Expected::Allowed, + writable, + ); + t.after(vec![HostCheck::Contents { + path: PathBuf::from("proj/new.txt"), + equals: "content\n", + }]); + t.push( + "a write-only policy still denies reads", + vec![file("proj/app.txt", "app")], + allow.clone(), + Action::Read { + path: PathBuf::from("proj/app.txt"), + }, + Expected::Denied, + loader_only, + ); + t.push( + "create directories, rename, link, delete inside a grant", + vec![], + allow.clone(), + Action::Shell( + "mkdir -p a/b/c && echo hi > a/f.txt && mv a/f.txt a/g.txt \ + && ln -s g.txt a/link && echo x >> a/link && ln a/g.txt a/hard \ + && rm -r a/b && cat a/g.txt a/hard && ls a && stat a/g.txt" + .to_string(), + ), + Expected::Allowed, + writable, + ); + t.push( + "TMPDIR is writable", + vec![], + allow.clone(), + Action::WriteTemp { file: "f.txt" }, + Expected::Allowed, + writable, + ); + t.push( + "/dev/null, /dev/stderr and pipes are usable", + vec![], + allow.clone(), + Action::Shell( + "echo x > /dev/null && echo x > /dev/stderr && echo x > /dev/stdout".to_string(), + ), + Expected::Allowed, + read_all, + ); + t.push( + "the command's own /proc entries are readable", + vec![], + allow.clone(), + Action::ReadOwnProc, + Expected::Allowed, + read_all, + ); + t.push( + "the command has no capabilities", + vec![], + allow.clone(), + Action::AssertNoCapabilities, + Expected::Allowed, + read_all, + ); + t.push( + "the command's namespaces are private", + vec![], + allow.clone(), + Action::AssertNamespaces, + Expected::Allowed, + read_all, + ); + t.push( + "append to a file in a grant", + vec![file("proj/log.txt", "line one\n")], + allow.clone(), + Action::Append { + path: PathBuf::from("proj/log.txt"), + }, + Expected::Allowed, + writable, + ); + t.after(vec![HostCheck::Contents { + path: PathBuf::from("proj/log.txt"), + equals: "line one\ncontent\n", + }]); + t.push( + "create a directory in a grant", + vec![], + allow.clone(), + Action::CreateDirectory { + path: PathBuf::from("proj/a/b/c"), + }, + Expected::Allowed, + writable, + ); + t.push( + "list a denied directory is still allowed (listing is not reading)", + vec![directory("secret")], + allow.clone(), + Action::List { + path: PathBuf::from("secret"), + }, + Expected::Allowed, + proj_writable_secret_denied, + ); + t.push( + "stat a denied file is still allowed", + vec![file("proj/.git/config", "original")], + allow.clone(), + Action::Stat { + path: PathBuf::from("proj/.git/config"), + }, + Expected::Allowed, + proj_writable_secret_denied, + ); + t.push( + "symlink a denied target is allowed (writing the link, not reading it)", + vec![file("secret/key", "password"), directory("proj")], + allow.clone(), + Action::Symlink { + target: PathBuf::from("secret/key"), + link: PathBuf::from("proj/link"), + }, + Expected::Allowed, + proj_writable_secret_denied, + ); + t.push( + "a symlink into a grant lets a write through", + vec![ + directory("outside"), + directory("proj"), + symlink("proj", "outside/qux"), + ], + allow.clone(), + Action::Write { + path: PathBuf::from("outside/qux/via.txt"), + }, + Expected::Allowed, + proj_writable_secret_denied, + ); + t.after(vec![HostCheck::Contents { + path: PathBuf::from("proj/via.txt"), + equals: "content\n", + }]); + + // -- Filesystem: escapes must fail -- + + t.push( + "read a denied file", + vec![file("secret/key", "password")], + allow.clone(), + Action::Open { + path: PathBuf::from("secret/key"), + mode: "r", + }, + Expected::Errno(&[libc::EACCES]), + proj_writable_secret_denied, + ); + t.reported(RequestedAccess::Fs { + permission: Permission::Read, + path: PathBuf::from("secret/key"), + }); + t.push( + "write a protected file", + vec![file("proj/.git/config", "original")], + allow.clone(), + Action::Open { + path: PathBuf::from("proj/.git/config"), + mode: "w", + }, + Expected::Errno(&[libc::EACCES, libc::EROFS]), + proj_writable_secret_denied, + ); + t.reported(RequestedAccess::Fs { + permission: Permission::Write, + path: PathBuf::from("proj/.git/config"), + }); + t.after(vec![HostCheck::Contents { + path: PathBuf::from("proj/.git/config"), + equals: "original", + }]); + t.push( + "delete a protected file", + vec![file("proj/.git/config", "original")], + allow.clone(), + Action::Delete { + path: PathBuf::from("proj/.git/config"), + }, + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "rename a protected directory", + vec![directory("proj/.git")], + allow.clone(), + Action::Rename { + from: PathBuf::from("proj/.git"), + to: PathBuf::from("proj/gitx"), + }, + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "read a .env file (glob-deny)", + vec![file("proj/app.env", "API_KEY=hunter2")], + allow.clone(), + Action::Read { + path: PathBuf::from("proj/app.env"), + }, + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "read a .env through a symlink (glob-deny)", + vec![file("proj/app.env", "API_KEY=hunter2")], + allow.clone(), + Action::Shell("ln -sf app.env sym.txt && cat sym.txt".to_string()), + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "copy a .env file", + vec![file("proj/app.env", "API_KEY=hunter2")], + allow.clone(), + Action::Shell("cp app.env copy.txt".to_string()), + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "rename a .env file", + vec![file("proj/app.env", "API_KEY=hunter2")], + allow.clone(), + Action::Rename { + from: PathBuf::from("proj/app.env"), + to: PathBuf::from("proj/app.txt"), + }, + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "hard link a .env file out of its rule", + vec![file("proj/app.env", "API_KEY=hunter2")], + allow.clone(), + Action::HardLink { + target: PathBuf::from("proj/app.env"), + link: PathBuf::from("proj/hard.txt"), + }, + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "write outside the grant", + vec![directory("outside")], + allow.clone(), + Action::Open { + path: PathBuf::from("outside/new.txt"), + mode: "wc", + }, + Expected::Errno(&[libc::EACCES, libc::EROFS]), + proj_writable_secret_denied, + ); + t.reported(RequestedAccess::Fs { + permission: Permission::Write, + path: PathBuf::from("outside/new.txt"), + }); + t.push( + "write through .. to a sibling", + vec![], + allow.clone(), + Action::Shell("mkdir -p sub && echo x > sub/../../outside/h".to_string()), + Expected::Denied, + proj_writable_secret_denied, + ); + t.push( + "read a host process's environment", + vec![], + allow.clone(), + Action::ReadHostEnvironment, + Expected::Denied, + read_all, + ); + t.push( + "signal a host process", + vec![], + allow.clone(), + Action::SignalHost, + Expected::Denied, + read_all, + ); + t.push( + "create a new user namespace", + vec![], + allow.clone(), + Action::NewUserNamespace, + Expected::Denied, + read_all, + ); + + // -- Ask: the syscall blocks until answered -- + + t.push( + "an asked write, answered late, still succeeds", + vec![directory("ask")], + Ask::Delayed(ASK_DELAY, Answer::Allow), + Action::Write { + path: PathBuf::from("ask/slow.txt"), + }, + Expected::Allowed, + ask_write, + ); + t.expected_prompts(1); + t.blocked_for(ASK_DELAY - TIMING_SLACK); + t.after(vec![HostCheck::Contents { + path: PathBuf::from("ask/slow.txt"), + equals: "content\n", + }]); + + t.push( + "an asked write, answered late with a denial, fails", + vec![directory("ask")], + Ask::Delayed(ASK_DELAY, Answer::Deny), + Action::Write { + path: PathBuf::from("ask/no.txt"), + }, + Expected::Denied, + ask_write, + ); + t.expected_prompts(1); + t.blocked_for(ASK_DELAY - TIMING_SLACK); + t.reported(RequestedAccess::Fs { + permission: Permission::Write, + path: PathBuf::from("ask/no.txt"), + }); + t.after(vec![HostCheck::Absent(PathBuf::from("ask/no.txt"))]); + + t.push( + "a remembered rule covers a later write", + vec![directory("ask")], + Ask::Delayed(ASK_DELAY, Answer::Remember("remember*".to_string())), + Action::Shell("echo x > ask/remember-a.txt && echo x > ask/remember-b.txt".to_string()), + Expected::Allowed, + ask_write, + ); + t.expected_prompts(1); + t.blocked_for(ASK_DELAY - TIMING_SLACK); + t.after(vec![ + HostCheck::Contents { + path: PathBuf::from("ask/remember-a.txt"), + equals: "x\n", + }, + HostCheck::Contents { + path: PathBuf::from("ask/remember-b.txt"), + equals: "x\n", + }, + ]); + + t.push( + "a panicking handler denies", + vec![directory("ask")], + Ask::Immediate(Answer::Panic), + Action::Write { + path: PathBuf::from("ask/panic.txt"), + }, + Expected::Denied, + ask_write, + ); + t.expected_prompts(1); + t.after(vec![HostCheck::Absent(PathBuf::from("ask/panic.txt"))]); + + // -- Unix sockets -- + + t.push( + "connect to an allowed unix socket", + vec![], + allow.clone(), + Action::ConnectUnix { + path: world.unix_allowed.clone(), + }, + Expected::Allowed, + unix_policy, + ); + t.push( + "connect to another unix socket is refused", + vec![], + allow.clone(), + Action::ConnectUnix { + path: world.unix_denied.clone(), + }, + Expected::Errno(&[libc::EACCES]), + unix_policy, + ); + t.push( + "connect through a symlink to a refused socket", + vec![], + allow.clone(), + Action::Shell(format!( + "ln -s {} link.sock && python3 net.py unix-connect link.sock EACCES", + shell(&world.unix_denied) + )), + Expected::Allowed, + unix_policy, + ); + t.push( + "serve on a unix socket in TMPDIR", + vec![], + allow.clone(), + Action::UnixServer { + file: "server.sock", + }, + Expected::Allowed, + unix_policy, + ); + t.push( + "bind a unix socket in a writable directory", + vec![], + allow.clone(), + Action::BindUnix { + path: PathBuf::from("bound.sock"), + }, + Expected::Allowed, + unix_policy, + ); + t.after(vec![HostCheck::Socket(PathBuf::from("bound.sock"))]); + t.push( + "bind a unix socket outside the grant is refused", + vec![], + allow.clone(), + Action::BindUnix { + path: world.unix_denied_bind.clone(), + }, + Expected::Errno(&[libc::EACCES, libc::EROFS]), + unix_policy, + ); + t.push( + "serve on an abstract unix socket (namespace-local)", + vec![], + allow.clone(), + Action::AbstractUnixServer { + name: "zed-sandbox-test", + }, + Expected::Allowed, + unix_policy, + ); + t.push( + "stream socketpairs work", + vec![], + allow.clone(), + Action::SocketPair { + kind: "SOCK_STREAM", + }, + Expected::Allowed, + unix_policy, + ); + t.push( + "seqpacket socketpairs work", + vec![], + allow.clone(), + Action::SocketPair { + kind: "SOCK_SEQPACKET", + }, + Expected::Allowed, + unix_policy, + ); + t.push( + "datagram unix sockets fail with EPERM", + vec![], + allow.clone(), + Action::DatagramUnix, + Expected::Errno(&[libc::EPERM]), + unix_policy, + ); + + // -- Network -- + + t.push( + "tcp to an allowed port", + vec![], + allow.clone(), + Action::Connect { + host: "127.0.0.1".to_string(), + port: world.tcp_allowed, + }, + Expected::Allowed, + net_policy, + ); + t.push( + "non-blocking tcp to an allowed port", + vec![], + allow.clone(), + Action::ConnectNonBlocking { + host: "127.0.0.1".to_string(), + port: world.tcp_allowed, + }, + Expected::Allowed, + net_policy, + ); + t.push( + "blocking tcp to an allowed closed port is refused", + vec![], + allow.clone(), + Action::Connect { + host: "127.0.0.1".to_string(), + port: world.tcp_closed, + }, + Expected::Errno(&[libc::ECONNREFUSED]), + net_policy, + ); + t.push( + "non-blocking tcp to an allowed closed port is reset", + vec![], + allow.clone(), + Action::ConnectNonBlocking { + host: "127.0.0.1".to_string(), + port: world.tcp_closed, + }, + Expected::Reset, + net_policy, + ); + t.push( + "tcp to an unlisted port is refused with EACCES", + vec![], + allow.clone(), + Action::Connect { + host: "127.0.0.1".to_string(), + port: world.tcp_unlisted, + }, + Expected::Errno(&[libc::EACCES]), + net_policy, + ); + t.reported(RequestedAccess::Net { + host: NetHost::Ip("127.0.0.1".parse().unwrap()), + port: world.tcp_unlisted, + protocol: Protocol::Tcp, + }); + t.push( + "tcp to a public address is refused with EACCES", + vec![], + allow.clone(), + Action::Connect { + host: "1.1.1.1".to_string(), + port: 80, + }, + Expected::Errno(&[libc::EACCES]), + net_policy, + ); + t.reported(RequestedAccess::Net { + host: NetHost::Ip("1.1.1.1".parse().unwrap()), + port: 80, + protocol: Protocol::Tcp, + }); + t.push( + "tcp to an unknown fake address is refused", + vec![], + allow.clone(), + Action::Connect { + host: "198.18.255.254".to_string(), + port: 80, + }, + Expected::Errno(&[libc::ECONNREFUSED]), + net_policy, + ); + t.push( + "tcp within the sandbox needs no rule", + vec![], + allow.clone(), + Action::LocalServer, + Expected::Allowed, + net_policy, + ); + t.push( + "tcp to an allowed name", + vec![], + allow.clone(), + Action::ConnectByName { + name: "localhost", + port: world.tcp_allowed, + }, + Expected::Allowed, + net_policy, + ); + t.push( + "tcp to an allowed name resolving to an ip:port no rule allows (rebinding)", + vec![], + allow.clone(), + Action::ConnectByName { + name: "localhost", + port: world.tcp_rebinding, + }, + Expected::Errno(&[libc::EACCES]), + net_policy, + ); + t.reported(RequestedAccess::Net { + host: NetHost::Ip("127.0.0.1".parse().unwrap()), + port: world.tcp_rebinding, + protocol: Protocol::Tcp, + }); + let asked_name_policy = |handler: Handler, _: &Path| SandboxPolicy { + fs: vec![FsRule::new("/").unwrap().read(Allow)], + net: vec![ + // The fake IP maps back to the name, so the Ask rule must be a + // Domain rule (names only match Domain/Any targets), and last, + // so it wins on the asked port. + NetRule::new( + NetTarget::Domain(DomainPattern::new("localhost").unwrap()), + Allow, + ) + .ports(PortSet::Ranges(vec![ + world.tcp_allowed..=world.tcp_allowed, + world.tcp_rebinding..=world.tcp_rebinding, + ])) + .tcp_only(), + NetRule::new( + NetTarget::Domain(DomainPattern::new("localhost").unwrap()), + Access::Ask(handler), + ) + .ports(PortSet::Ranges(vec![world.tcp_asked..=world.tcp_asked])) + .tcp_only(), + // `localhost` resolves to loopback, which the rebinding check + // requires its own rule for. + NetRule::new(NetTarget::Ip("127.0.0.1".parse().unwrap()), Allow).tcp_only(), + ], + }; + t.push( + "an asked name, answered late, connects", + vec![], + Ask::Delayed(ASK_DELAY, Answer::Allow), + Action::ConnectByName { + name: "localhost", + port: world.tcp_asked, + }, + Expected::Allowed, + asked_name_policy, + ); + t.expected_prompts(1); + t.blocked_for(ASK_DELAY - TIMING_SLACK); + + t.push( + "udp to an allowed port, connected", + vec![], + allow.clone(), + Action::Udp { + host: "127.0.0.1".to_string(), + port: world.udp_allowed, + connected: true, + }, + Expected::Allowed, + net_policy, + ); + t.push( + "udp to an allowed port, sendto", + vec![], + allow.clone(), + Action::Udp { + host: "127.0.0.1".to_string(), + port: world.udp_allowed, + connected: false, + }, + Expected::Allowed, + net_policy, + ); + t.push( + "udp to an unlisted port is blocked", + vec![], + allow.clone(), + Action::Udp { + host: "127.0.0.1".to_string(), + port: world.udp_unlisted, + connected: true, + }, + Expected::Denied, + net_policy, + ); + t.reported(RequestedAccess::Net { + host: NetHost::Ip("127.0.0.1".parse().unwrap()), + port: world.udp_unlisted, + protocol: Protocol::Udp, + }); + + t.push( + "dns: an allowed name resolves to a fake address", + vec![], + allow.clone(), + Action::Dns { name: "localhost" }, + Expected::FakeAddress, + net_policy, + ); + t.push( + "dns: an asked name resolves", + vec![], + Ask::Immediate(Answer::Allow), + Action::Dns { name: "ask.test" }, + Expected::FakeAddress, + net_policy, + ); + t.push( + "dns: an unlisted name does not resolve", + vec![], + allow.clone(), + Action::Dns { + name: "unlisted.test", + }, + Expected::NxDomain, + net_policy, + ); + t.push( + "dns: AAAA answers are empty", + vec![], + allow.clone(), + Action::Dns { name: "localhost" }, + Expected::EmptyRecord, + net_policy, + ); + t.push( + "getaddrinfo goes through the sandbox's DNS", + vec![], + allow.clone(), + Action::Resolve { name: "ask.test" }, + Expected::FakeAddress, + net_policy, + ); + t.push( + "ping sockets fail", + vec![], + allow, + Action::Ping, + Expected::Errno(&[libc::EACCES]), + net_policy, + ); + + t.scenarios + } + + /// Run one scenario and report whether it held. + fn run_scenario( + context: &SandboxContext, + scenario: &Scenario, + ctx: &Ctx, + failures: &mut Vec, + ) { + // Fixtures, in their scenario's fresh directory. + std::fs::create_dir_all(&scenario.home).expect("creating scenario home"); + for fixture in &scenario.fixtures { + match fixture { + Fixture::File { path, contents } => { + let path = scenario.home.join(path); + std::fs::create_dir_all(path.parent().expect("a parent")).unwrap(); + std::fs::write(&path, contents) + .unwrap_or_else(|error| panic!("writing {}: {error}", path.display())); + } + Fixture::Directory { path } => { + std::fs::create_dir_all(scenario.home.join(path)).unwrap(); + } + Fixture::Symlink { target, link } => { + let link = scenario.home.join(link); + std::fs::create_dir_all(link.parent().expect("a parent")).unwrap(); + std::os::unix::fs::symlink(scenario.home.join(target), link) + .unwrap_or_else(|error| panic!("linking: {error}")); + } + } + } + + let sandbox = context + .create_sandbox(scenario.policy.clone()) + .expect("creating the scenario's sandbox"); + sandbox.on_denial({ + let denials = scenario.denials.clone(); + move |denial| denials.lock().unwrap().push(denial) + }); + + std::fs::write(scenario.home.join("net.py"), NET_CLIENT).unwrap(); + let script = scenario.action.script(ctx, &scenario.expected); + let started = Instant::now(); + let mut prepared = sandbox + .prepare(&command(&script, &scenario.home)) + .expect("preparing command"); + prepared.command_mut().stdin(Stdio::null()); + let output = prepared.output().expect("running the scenario"); + let elapsed = started.elapsed(); + + let mut notes = Vec::new(); + if !scenario.expected.holds(&output) { + notes.push(format!( + "expected {}, got {} (stderr: {})", + match &scenario.expected { + Expected::Allowed | Expected::FakeAddress | Expected::EmptyRecord => { + "success".to_string() + } + other => format!("{other:?}"), + }, + output.status, + String::from_utf8_lossy(&output.stderr).trim(), + )); + } + if let Some(at_least) = scenario.blocked_for + && elapsed < at_least + { + notes.push(format!( + "expected the action to block for at least {at_least:?}, it took {elapsed:?}" + )); + } + let prompts = scenario.prompts.load(Ordering::SeqCst); + if let Some(expected) = scenario.expected_prompts + && prompts != expected + { + notes.push(format!("expected {expected} prompt(s), saw {prompts}")); + } + if let Some(reported) = &scenario.reported { + let found = scenario + .denials + .lock() + .unwrap() + .iter() + .any(|denial| denial.requests.contains(reported)); + if !found { + notes.push(format!("no denial reported for {reported:?}")); + } + } + for check in &scenario.host_checks { + let path = scenario.home.join(check_path(check)); + let held = match check { + HostCheck::Contents { equals, .. } => std::fs::read_to_string(&path) + .map(|contents| contents == *equals) + .unwrap_or(false), + HostCheck::Absent(_) => !path.exists(), + HostCheck::Socket(_) => path + .symlink_metadata() + .map(|metadata| metadata.file_type().is_socket()) + .unwrap_or(false), + }; + if !held { + notes.push(format!( + "host check failed: {}", + check_path(check).display() + )); + } + } + + drop(sandbox); + let passed = notes.is_empty(); + println!("{:4} {}", if passed { "ok" } else { "FAIL" }, scenario.name); + if !passed { + failures.push(format!("{}: {}", scenario.name, notes.join("; "))); + } + } + + fn check_path(check: &HostCheck) -> PathBuf { + match check { + HostCheck::Contents { path, .. } => path.clone(), + HostCheck::Absent(path) | HostCheck::Socket(path) => path.clone(), + } + } + + fn command(script: &str, cwd: &Path) -> UntrustedCommand { + UntrustedCommand { + cmd: "sh".into(), + args: vec!["-c".into(), script.into()], + env: std::env::vars() + // The untrusted command needs `sh` and its loader chain to + // run at all; everything else is its own business. + .filter(|(name, _)| { + matches!( + name.as_str(), + "PATH" + | "HOME" + | "LD_LIBRARY_PATH" + | "LD_PRELOAD" + | "NIX_LD" + | "NIX_LD_LIBRARY_PATH" + ) + }) + .chain([("HOST_PID".to_string(), std::process::id().to_string())]) + .collect(), + cwd: cwd.to_path_buf(), + } + } + + /// The client script the network and socket actions run. + const NET_CLIENT: &str = r#" +import asyncio, errno, ipaddress, os, random, select, socket, struct, sys + +FAKE = ipaddress.ip_network("198.18.0.0/15") + +def echo(stream): + stream.sendall(b"ping") + try: + return stream.recv(4) == b"ping" + except BlockingIOError: + # A non-blocking socket: wait for the reply first. + assert select.select([stream], [], [], 10)[0], "no echo reply" + return stream.recv(4) == b"ping" + +def dns(name, record_type=1): + query_id = random.randrange(65536) + query = struct.pack(">HHHHHH", query_id, 0x0100, 1, 0, 0, 0) + for label in name.split("."): + query += bytes([len(label)]) + label.encode() + query += b"\0" + struct.pack(">HH", record_type, 1) + # Any server: the sandbox answers every address. + udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp.settimeout(5) + udp.sendto(query, ("8.8.8.8", 53)) + response, server = udp.recvfrom(4096) + assert server == ("8.8.8.8", 53), server + assert struct.unpack(">H", response[:2])[0] == query_id + code = response[3] & 0xf + answers = struct.unpack(">H", response[6:8])[0] + address = ( + socket.inet_ntoa(response[-4:]) + if answers and record_type == 1 + else None + ) + return code, address + +def main(command, *args): + if command == "tcp": + host, port = args[0], int(args[1]) + expected = [getattr(errno, name) for name in args[2:]] + # Blocking: the sandbox reports the host leg's exact failure to a + # blocking connect, and the echo reply arrives in its own time. + stream = socket.socket() + try: + stream.connect((host, port)) + except OSError as error: + assert error.errno in expected, error + return True + assert not expected, "connect succeeded: expected " + ", ".join(args[2:]) + return echo(stream) and stream.getpeername()[1] == port + if command == "tcp-nonblocking": + host, port = args[0], int(args[1]) + if "reset" in args[2:]: + # Succeed locally, then the host leg's refusal shows as a reset. + stream = socket.socket() + stream.setblocking(False) + result = stream.connect_ex((host, port)) + assert result == 0, os.strerror(result) + assert select.select([stream], [], [], 5)[0] + try: + stream.recv(1) + except ConnectionResetError: + return True + return False + expected = [getattr(errno, name) for name in args[2:]] + stream = socket.socket() + stream.setblocking(False) + result = stream.connect_ex((host, port)) + if result != 0: + assert result in expected, os.strerror(result) + return True + assert not expected, "connect succeeded: expected " + ", ".join(args[2:]) + return echo(stream) + if command == "tcp-name": + name, port = args[0], int(args[1]) + expected = [getattr(errno, name) for name in args[2:]] + code, address = dns(name) + assert code == 0, code + stream = socket.socket() + try: + stream.connect((address, port)) + except OSError as error: + assert error.errno in expected, error + return True + assert not expected, "connect succeeded: expected " + ", ".join(args[2:]) + return echo(stream) + if command == "local-server": + server = socket.create_server(("127.0.0.1", 0)) + client = socket.create_connection(server.getsockname(), timeout=5) + connection, _ = server.accept() + client.sendall(b"hi") + return connection.recv(2) == b"hi" + if command == "udp": + host, port, mode = args[0], int(args[1]), args[2] + udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp.settimeout(3) + if mode == "connect": + udp.connect((host, port)) + udp.send(b"ping") + return udp.recv(4) == b"ping" + udp.sendto(b"ping", (host, port)) + data, source = udp.recvfrom(4) + return data == b"ping" and source == (host, port) + if command == "dns": + name, verdict, record = args[0], args[1], int(args[2]) + code, address = dns(name, record) + if verdict == "nxdomain": + return code == 3 + if verdict == "empty": + return code == 0 and address is None + return ( + code == 0 + and ipaddress.ip_address(address) in FAKE + ) + if command == "resolve": + # getaddrinfo: the sandbox's DNS answers. + return ipaddress.ip_address(socket.gethostbyname(args[0])) in FAKE + if command == "ping-socket": + expected = [getattr(errno, name) for name in args] + try: + socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_ICMP) + except OSError as error: + assert error.errno in expected, error + return True + raise AssertionError("ping socket allowed") + if command == "unix-connect": + path = args[0] + expected = [getattr(errno, name) for name in args[1:]] + stream = socket.socket(socket.AF_UNIX) + stream.settimeout(5) + try: + stream.connect(path) + except OSError as error: + assert error.errno in expected, error + return True + assert not expected, "connect succeeded: expected " + ", ".join(args[1:]) + return echo(stream) + if command == "unix-bind": + path = args[0] + expected = [getattr(errno, name) for name in args[1:]] + try: + socket.socket(socket.AF_UNIX).bind(path) + except OSError as error: + assert error.errno in expected, error + return True + assert not expected, "bind succeeded: expected " + ", ".join(args[1:]) + return True + if command == "unix-server": + # A path, or a name starting with @ for an abstract address. + address = "\0" + args[0][1:] if args[0].startswith("@") else args[0] + server = socket.socket(socket.AF_UNIX) + server.bind(address) + server.listen() + client = socket.socket(socket.AF_UNIX) + client.settimeout(5) + client.connect(address) + connection, _ = server.accept() + client.sendall(b"hi") + return connection.recv(2) == b"hi" + if command == "socketpair": + kind = getattr(socket, args[0]) + first, second = socket.socketpair(socket.AF_UNIX, kind) + first.send(b"hi") + return second.recv(2) == b"hi" + if command == "datagram-unix": + # Both creation paths must fail with EPERM. + for operation in ( + lambda: socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM), + lambda: socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM), + ): + try: + operation() + except OSError as error: + assert error.errno == errno.EPERM, error + else: + raise AssertionError("datagram unix socket allowed") + return True + raise ValueError(command) + +sys.exit(0 if main(*sys.argv[1:]) else 1) +"#; + + fn tcp_echo_server() -> u16 { + use std::io::{Read as _, Write as _}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("binding"); + let port = listener.local_addr().unwrap().port(); + std::thread::spawn(move || { + for mut stream in listener.incoming().flatten() { + std::thread::spawn(move || { + let mut buffer = [0; 1024]; + while let Ok(length @ 1..) = stream.read(&mut buffer) { + if stream.write_all(&buffer[..length]).is_err() { + break; + } + } + }); + } + }); + port + } + + fn unix_echo_server(path: &Path) { + use std::io::{Read as _, Write as _}; + let listener = std::os::unix::net::UnixListener::bind(path).expect("binding"); + std::thread::spawn(move || { + for mut stream in listener.incoming().flatten() { + std::thread::spawn(move || { + let mut buffer = [0; 1024]; + while let Ok(length @ 1..) = stream.read(&mut buffer) { + if stream.write_all(&buffer[..length]).is_err() { + break; + } + } + }); + } + }); + } + + fn udp_echo_server() -> u16 { + let socket = std::net::UdpSocket::bind("127.0.0.1:0").expect("binding"); + let port = socket.local_addr().unwrap().port(); + std::thread::spawn(move || { + let mut buffer = [0; 1024]; + while let Ok((length, source)) = socket.recv_from(&mut buffer) { + socket.send_to(&buffer[..length], source).ok(); + } + }); + port + } + + /// A TCP socket bound to a free loopback port but never listened on: + /// connecting to it is deterministically refused. + fn bound_not_listening() -> (u16, std::os::fd::OwnedFd) { + use std::os::fd::{AsRawFd as _, FromRawFd as _}; + let mut address = libc::sockaddr_in { + sin_family: libc::AF_INET as libc::sa_family_t, + sin_port: 0, + sin_addr: libc::in_addr { + // `INADDR_LOOPBACK` is defined in host byte order. + s_addr: libc::htonl(libc::INADDR_LOOPBACK), + }, + sin_zero: [0; 8], + }; + // SAFETY: plain socket call; the fresh descriptor is owned below. + let descriptor = + unsafe { libc::socket(libc::AF_INET, libc::SOCK_STREAM | libc::SOCK_CLOEXEC, 0) }; + assert!(descriptor >= 0, "socket for the closed port"); + let socket = unsafe { std::os::fd::OwnedFd::from_raw_fd(descriptor) }; + // SAFETY: a sockaddr_in we own, with its length. + let bound = unsafe { + libc::bind( + socket.as_raw_fd(), + (&mut address as *mut libc::sockaddr_in).cast(), + std::mem::size_of::() as libc::socklen_t, + ) + }; + assert!( + bound >= 0, + "binding the closed port: {}", + std::io::Error::last_os_error() + ); + // SAFETY: getsockname fills the same sockaddr_in. + let mut length = std::mem::size_of::() as libc::socklen_t; + let named = unsafe { + libc::getsockname( + socket.as_raw_fd(), + (&mut address as *mut libc::sockaddr_in).cast(), + &mut length, + ) + }; + assert!(named >= 0, "naming the closed port"); + (u16::from_be(address.sin_port), socket) + } + + /// Build the world of peers and free ports under `root`. + fn world(root: &Path) -> World { + // A socket bound but never listened on: connecting is deterministically + // refused. (`TcpListener::bind` listens, so its backlog would accept.) + let (closed_port, closed) = bound_not_listening(); + std::fs::create_dir_all(root).unwrap(); + let sockets = root.join("sockets"); + std::fs::create_dir_all(&sockets).unwrap(); + let unix_allowed = sockets.join("allowed.sock"); + let unix_denied = sockets.join("denied.sock"); + let unix_denied_bind = root.parent().unwrap().join("denied-bind.sock"); + unix_echo_server(&unix_allowed); + unix_echo_server(&unix_denied); + World { + _closed: closed, + tcp_allowed: tcp_echo_server(), + tcp_asked: tcp_echo_server(), + tcp_unlisted: tcp_echo_server(), + tcp_closed: closed_port, + tcp_rebinding: tcp_echo_server(), + udp_allowed: udp_echo_server(), + udp_unlisted: udp_echo_server(), + unix_allowed, + unix_denied, + unix_denied_bind, + } + } + + /// This harness's single test, as `cargo nextest` lists and runs it. + const TEST_NAME: &str = "end_to_end"; + + pub fn main() { + let arguments: Vec<_> = std::env::args().skip(1).collect(); + // `cargo nextest` drives `harness = false` binaries with its own + // protocol: `--list --format terse [--ignored]` to discover tests, then + // ` --exact [--nocapture]` to run each one. This harness is + // a single unit: one test, none ignored. `cargo test` and the NixOS + // checks instead run it bare, or with the flags below. + if arguments.iter().any(|argument| argument == "--list") { + if !arguments.iter().any(|argument| argument == "--ignored") { + println!("{TEST_NAME}: test"); + } + return; + } + let (mut require_sandbox, mut expect_userns_disabled) = (false, false); + for argument in &arguments { + match argument.as_str() { + "--require-sandbox" => require_sandbox = true, + "--expect-userns-disabled" => expect_userns_disabled = true, + TEST_NAME | "--nocapture" | "--exact" | "--bench" => {} + other => panic!("unexpected argument {other}"), + } + } + let (require_sandbox, expect_userns_disabled) = (require_sandbox, expect_userns_disabled); + let fixtures = tempfile::Builder::new() + .prefix("sandbox-escape-test-") + .tempdir_in("/tmp") + .expect("creating work dir"); + let root = fixtures.path().to_path_buf(); + + let context = SandboxContext::new().expect("starting supervisor"); + let ctx = Ctx { + namespaces: ["user", "mnt", "net", "ipc"] + .into_iter() + .map(|namespace| { + ( + namespace, + std::fs::read_link(format!("/proc/self/ns/{namespace}")) + .expect("reading a namespace") + .to_string_lossy() + .into_owned(), + ) + }) + .collect(), + }; + assert!( + std::process::Command::new("sh") + .args([ + "-c", + "for tool in python3 unshare cat chmod cp grep kill ln ls mkdir mv rm sleep stat truncate; do command -v \"$tool\" >/dev/null || exit 1; done", + ]) + .status() + .expect("checking test dependencies") + .success(), + "missing test dependencies" + ); + + let world = world(&root); + let scenarios = table(&root, &world); + let mut failures = Vec::new(); + + // A probe first: whether the sandbox can start decides the run. + std::fs::create_dir_all(root.join("probe")).unwrap(); + let probe = { + let sandbox = context + .create_sandbox(SandboxPolicy { + fs: vec![FsRule::new("/").unwrap().read(Allow)], + net: Vec::new(), + }) + .expect("creating the probe sandbox"); + let mut prepared = sandbox + .prepare(&command("true", &root.join("probe"))) + .expect("preparing the probe"); + prepared.command_mut().stdin(Stdio::null()); + prepared.output().expect("running the probe") + }; + if expect_userns_disabled { + assert!(!probe.status.success(), "sandbox unexpectedly started"); + assert!( + String::from_utf8_lossy(&probe.stderr) + .contains("creating sandbox namespaces failed"), + "expected namespace setup failure, got: {probe:?}" + ); + println!("ok unavailable user namespaces fail closed"); + return; + } + if !probe.status.success() { + let stderr = String::from_utf8_lossy(&probe.stderr); + if !require_sandbox && stderr.contains("creating sandbox namespaces failed") { + eprintln!("skipping sandbox_linux end-to-end tests: {}", stderr.trim()); + return; + } + panic!("sandbox probe failed: {stderr}"); + } + + for scenario in &scenarios { + run_scenario(&context, scenario, &ctx, &mut failures); + } + + // -- Sequence checks, which orchestrate rather than act -- + + check_stop(&context, &root, &mut failures); + check_policy_update(&context, &root, &mut failures); + check_early_connect(&context, &root, &mut failures); + + drop(context); + if !failures.is_empty() { + eprintln!("\n{} failure(s):\n{}", failures.len(), failures.join("\n")); + panic!("sandbox isolation checks failed"); + } + println!("\nall checks passed"); + } + + /// `SIGTERM` to the helper stops the command and everything it spawned. + fn check_stop(context: &SandboxContext, root: &Path, failures: &mut Vec) { + let home = root.join("sequence").join("stop"); + std::fs::create_dir_all(&home).unwrap(); + let sandbox = context + .create_sandbox(SandboxPolicy { + fs: vec![ + FsRule::new("/").unwrap().read(Allow), + FsRule::new("/").unwrap().write(Allow), + ], + net: Vec::new(), + }) + .unwrap(); + let pid_file = sandbox.temp_dir().join("background.pid"); + let script = format!("sleep 300 & echo $! > {}; wait", pid_file.display()); + let mut prepared = sandbox + .prepare(&command(&script, &home)) + .expect("preparing command"); + prepared.command_mut().stdin(Stdio::null()); + let mut helper = prepared.spawn().expect("spawning command"); + let deadline = Instant::now() + Duration::from_secs(10); + let background = loop { + if let Some(pid) = std::fs::read_to_string(&pid_file) + .ok() + .and_then(|pid| pid.trim().parse::().ok()) + { + break Some(pid); + } + if Instant::now() > deadline { + break None; + } + std::thread::sleep(Duration::from_millis(20)); + }; + // SAFETY: plain kill of our own, not yet reaped, child. + unsafe { libc::kill(helper.id() as libc::pid_t, libc::SIGTERM) }; + let deadline = Instant::now() + Duration::from_secs(5); + let status = loop { + match helper.try_wait() { + Ok(Some(status)) => break Some(status), + Ok(None) if Instant::now() < deadline => { + std::thread::sleep(Duration::from_millis(20)) + } + _ => break None, + } + }; + if status.is_none() { + helper.kill().ok(); + } + // SAFETY: signal 0 only checks whether the process exists. + let background_gone = background.is_some_and(|pid| unsafe { libc::kill(pid, 0) } != 0); + let passed = + status.and_then(|status| status.code()) == Some(128 + libc::SIGTERM) && background_gone; + println!( + "{:4} stopping kills the command and its children", + if passed { "ok" } else { "FAIL" } + ); + if !passed { + failures.push("stopping kills the command and its children".to_string()); + } + } + + /// Narrow the policy while a command holds a file open for writing: new + /// writes fail, and the open file is reported. + fn check_policy_update(context: &SandboxContext, root: &Path, failures: &mut Vec) { + let home = root.join("sequence").join("policy"); + std::fs::create_dir_all(&home).unwrap(); + let base = SandboxPolicy { + fs: vec![ + FsRule::new("/").unwrap().read(Allow), + FsRule::new("/").unwrap().write(Allow), + ], + net: Vec::new(), + }; + let sandbox = context.create_sandbox(base.clone()).unwrap(); + let held = home.join("held.txt"); + let mut prepared = sandbox + .prepare(&command("exec 3>>held.txt; read line", &home)) + .expect("preparing command"); + prepared.command_mut().stdin(Stdio::piped()); + let mut holder = prepared.spawn().expect("spawning holder"); + let deadline = Instant::now() + Duration::from_secs(10); + while !held.exists() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(20)); + } + + let mut narrowed = base.clone(); + narrowed.fs.push(FsRule::new("/").unwrap().write(Deny)); + let still_open = sandbox.update_policy(&narrowed).expect("updating policy"); + let narrowed_reported = still_open + .iter() + .any(|open| open.path == held && open.write && !open.read); + + let mut check = |name: &'static str, script: &str, expected: bool| { + let mut prepared = sandbox + .prepare(&command(script, &home)) + .expect("preparing command"); + prepared.command_mut().stdin(Stdio::null()); + let output = prepared.output().expect("running the check"); + let passed = output.status.success() == expected; + println!("{:4} {name}", if passed { "ok" } else { "FAIL" }); + if !passed { + failures.push(format!( + "{name}: expected {}, got {} (stderr: {})", + if expected { "success" } else { "failure" }, + output.status, + String::from_utf8_lossy(&output.stderr).trim(), + )); + } + }; + check( + "write after narrowing", + "echo x > after-narrowing.txt", + false, + ); + drop(holder.stdin.take()); + holder.wait().ok(); + sandbox.update_policy(&base).expect("restoring policy"); + check( + "write after restoring", + "echo x > after-restoring.txt", + true, + ); + + let passed = narrowed_reported; + println!( + "{:4} narrowing reports the held file", + if passed { "ok" } else { "FAIL" } + ); + if !passed { + failures.push("narrowing reports the held file".to_string()); + } + } + + /// Hold the host-leg prompt open: the app must get its local connection + /// (and run another event-loop task) before we approve the host leg, and + /// ending the execution cancels the in-flight request. + fn check_early_connect(context: &SandboxContext, root: &Path, failures: &mut Vec) { + use std::io::{BufRead as _, BufReader}; + use std::sync::mpsc; + + let home = root.join("sequence").join("early-connect"); + std::fs::create_dir_all(&home).unwrap(); + let port = tcp_echo_server(); + for cancelled in [false, true] { + let (prompts, pending) = mpsc::channel(); + let handler = Handler::new(move |request, responder| { + assert!(request.items.iter().all(|item| matches!( + item, + RequestedAccess::Net { + host: NetHost::Ip(_), + .. + } + ))); + if let Err(error) = prompts.send(responder) { + error.0.respond(Decision::deny()); + } + }); + let sandbox = context + .create_sandbox(SandboxPolicy { + fs: vec![FsRule::new("/").unwrap().read(Allow)], + net: vec![ + NetRule::new( + NetTarget::Domain(DomainPattern::new("localhost").unwrap()), + Allow, + ) + .tcp_only(), + NetRule::new(NetTarget::Loopback, Access::Ask(handler)).tcp_only(), + NetRule::new(NetTarget::Ip("::1".parse().unwrap()), Allow).tcp_only(), + ], + }) + .unwrap(); + // Use the sandbox's raw DNS so the connect goes through the + // host-leg prompt (`localhost` from /etc/hosts wouldn't). + let dns_helpers = NET_CLIENT + .split("def main(command, *args):") + .next() + .unwrap(); + let script = format!( + r#"python3 - <<'PY' +{dns_helpers} +code, address = dns('localhost') +assert code == 0 +async def main(): + reader, writer = await asyncio.open_connection(address, {port}) + await asyncio.sleep(0) + print('connected and responsive', flush=True) + writer.write(b'ping') + assert await reader.readexactly(4) == b'ping' + writer.close() + await writer.wait_closed() +asyncio.run(main()) +PY"# + ); + let mut prepared = sandbox + .prepare(&command(&script, &home)) + .expect("preparing command"); + prepared + .command_mut() + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + let mut helper = prepared.spawn().unwrap(); + let stdout = helper.stdout.take().unwrap(); + let (lines, output) = mpsc::channel(); + let reader = std::thread::spawn(move || { + for line in BufReader::new(stdout).lines() { + if lines.send(line).is_err() { + break; + } + } + }); + let responder = pending.recv_timeout(Duration::from_secs(5)).unwrap(); + let responsive = output + .recv_timeout(Duration::from_secs(5)) + .is_ok_and(|line| line.is_ok_and(|line| line == "connected and responsive")); + let name = if cancelled { + "early connect is cancelled when its execution ends" + } else { + "event loop progresses before the host connection is approved" + }; + if cancelled { + // SAFETY: plain kill of our own, not yet reaped, child. + unsafe { libc::kill(helper.id() as i32, libc::SIGTERM) }; + let status = helper.wait().unwrap(); + let deadline = Instant::now() + Duration::from_secs(5); + while !responder.is_cancelled() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(20)); + } + let passed = responsive && status.code() == Some(143) && responder.is_cancelled(); + println!("{:4} {name}", if passed { "ok" } else { "FAIL" }); + if !passed { + failures.push(name.to_string()); + } + responder.respond(Decision::deny()); + } else { + responder.respond(Decision::allow()); + let result = helper.wait_with_output().unwrap(); + let passed = responsive && result.status.success(); + println!("{:4} {name}", if passed { "ok" } else { "FAIL" }); + if !passed { + failures.push(name.to_string()); + } + } + reader.join().unwrap(); + } + } +} diff --git a/crates/sandbox_linux/examples/run.rs b/crates/sandbox_linux/examples/run.rs new file mode 100644 index 00000000000..ddb6a5f3423 --- /dev/null +++ b/crates/sandbox_linux/examples/run.rs @@ -0,0 +1,162 @@ +//! Run a command in a `sandbox_linux` sandbox. +//! +//! ```sh +//! cargo run -p sandbox_linux --example run -- \ +//! --write /tmp/sandbox-escape-test-x/proj --protect /tmp/sandbox-escape-test-x/proj/.git \ +//! --hide '/**/*.env' --ask /tmp/sandbox-escape-test-x/ask \ +//! --allow-net '*.github.com:443' --ask-net '*' -- sh +//! ``` +//! +//! Everything is readable and nothing writable, and nothing on the network +//! is reachable, except: +//! - `--write PATTERN` allows writes, `--protect PATTERN` denies them; +//! - `--hide PATTERN` denies reads; +//! - `--ask PATTERN` asks on the terminal before each write; +//! - `--connect PATTERN` allows connecting to unix sockets; +//! - `--allow-net`, `--deny-net` and `--ask-net DESTINATION` for +//! connections, with destinations as `NetRule::parse` reads them, e.g. +//! `tcp/*.github.com:22,443`, `udp/10.0.0.0/8:53`, `loopback:5432`. +//! +//! Rules apply in the order given, last match wins. + +#[cfg(not(target_os = "linux"))] +fn main() {} + +#[cfg(target_os = "linux")] +#[allow( + clippy::disallowed_methods, + reason = "a manual test driver that blocks on the child" +)] +fn main() -> anyhow::Result<()> { + use std::io::{BufRead as _, Write as _}; + + use anyhow::{Context as _, bail}; + use sandbox_linux::{ + Access, Decision, FsRule, Handler, NetRule, PortSet, RequestedAccess, Rule, SandboxContext, + SandboxPolicy, UntrustedCommand, + }; + + sandbox_linux::run_hidden_entrypoint_if_invoked(); + + // Asks on the terminal. `a` allows and remembers the exact path (or host + // and port) for the rest of the run. + let ask = Handler::new(|request, responder| { + // The handler must not block, so ask on another thread. + std::thread::spawn(move || { + let answer = (|| -> std::io::Result { + let tty = std::fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/tty")?; + let mut writer = &tty; + writeln!(writer, "\n[sandbox] {:?} wants:", request.executable)?; + for item in &request.items { + match item { + RequestedAccess::Fs { permission, path } => { + writeln!(writer, " {permission:?} {}", path.display())? + } + RequestedAccess::Net { + host, + port, + protocol, + } => writeln!(writer, " {protocol:?} connection to {host}:{port}")?, + } + } + write!(writer, "allow? [y/a(lways)/N] ")?; + let mut answer = String::new(); + std::io::BufReader::new(&tty).read_line(&mut answer)?; + Ok(answer.trim().to_lowercase()) + })() + .unwrap_or_default(); + let decision = match answer.as_str() { + "y" => Decision::allow(), + "a" => Decision { + allow: true, + remember: request + .items + .iter() + .filter_map(|item| match item { + RequestedAccess::Fs { permission, path } => { + let rule = FsRule::new(path.to_str()?).ok()?; + Some(Rule::Fs(match permission { + sandbox_linux::Permission::Read => rule.read(Access::Allow), + sandbox_linux::Permission::Write => rule.write(Access::Allow), + sandbox_linux::Permission::Connect => { + rule.connect(Access::Allow) + } + })) + } + RequestedAccess::Net { + host, + port, + protocol, + } => { + let target = match host { + sandbox_linux::NetHost::Domain(name) => { + sandbox_linux::NetTarget::Domain( + sandbox_linux::DomainPattern::new(name.as_str()) + .ok()?, + ) + } + sandbox_linux::NetHost::Ip(address) => { + sandbox_linux::NetTarget::Ip( + address.to_string().parse().ok()?, + ) + } + }; + let rule = NetRule::new(target, Access::Allow) + .ports(PortSet::single(*port)); + Some(Rule::Net(match protocol { + sandbox_linux::Protocol::Tcp => rule.tcp_only(), + sandbox_linux::Protocol::Udp => rule.udp_only(), + })) + } + }) + .collect(), + }, + _ => Decision::deny(), + }; + responder.respond(decision); + }); + }); + + let mut rules = vec![FsRule::new("/")?.read(Access::Allow)]; + let mut net = Vec::new(); + let mut arguments = std::env::args().skip(1); + let mut command = Vec::new(); + while let Some(argument) = arguments.next() { + let mut value = || arguments.next().context("flag needs a value"); + match argument.as_str() { + "--write" => rules.push(FsRule::new(value()?)?.write(Access::Allow)), + "--protect" => rules.push(FsRule::new(value()?)?.write(Access::Deny)), + "--hide" => rules.push(FsRule::new(value()?)?.read(Access::Deny)), + "--ask" => rules.push(FsRule::new(value()?)?.write(Access::Ask(ask.clone()))), + "--connect" => rules.push(FsRule::new(value()?)?.connect(Access::Allow)), + "--allow-net" => net.push(NetRule::parse(&value()?, Access::Allow)?), + "--deny-net" => net.push(NetRule::parse(&value()?, Access::Deny)?), + "--ask-net" => net.push(NetRule::parse(&value()?, Access::Ask(ask.clone()))?), + "--" => command.extend(arguments.by_ref()), + other => bail!("unexpected argument {other}"), + } + } + let Some((cmd, args)) = command.split_first() else { + bail!( + "usage: run [--write P] [--protect P] [--hide P] [--ask P] [--connect P] [--allow-net D] \ + [--deny-net D] [--ask-net D] -- COMMAND..." + ); + }; + + let context = SandboxContext::new()?; + let sandbox = context.create_sandbox(SandboxPolicy { fs: rules, net })?; + let status = sandbox + .execute(&UntrustedCommand { + cmd: cmd.clone(), + args: args.to_vec(), + env: std::env::vars().collect(), + cwd: std::env::current_dir()?, + })? + .wait()?; + drop(sandbox); + drop(context); + std::process::exit(status.code().unwrap_or(1)); +} diff --git a/crates/sandbox_linux/src/broker.rs b/crates/sandbox_linux/src/broker.rs new file mode 100644 index 00000000000..11d42ea1229 --- /dev/null +++ b/crates/sandbox_linux/src/broker.rs @@ -0,0 +1,1209 @@ +//! Handling of one trapped syscall. +//! +//! - Filesystem syscalls are never continued: the supervisor resolves the +//! path itself ([`crate::resolve`]), checks the rules against the object it +//! ends up holding, and performs the operation on that object. +//! - Reads resolve in the sandbox's own root, so the fd we hand back lives +//! on its read-only mounts (a read fd can't be used to `fchmod`). +//! - Writes resolve from the host's root, on its writable mounts. +//! - Syscalls acting on another process by pid are checked (the pid is a +//! register, so it can't change under us) and then continued. A pid reused +//! between the check and the call is a remaining race. +//! - `connect` on IP sockets is decided by [`crate::net`]. Continuing it is +//! always safe: nothing outside the sandbox's network namespace can be +//! reached by address from inside it. +//! - `connect` and `bind` on unix sockets are checked against the +//! filesystem rules (sockets are found by path) and performed by us. + +use std::cell::OnceCell; +use std::collections::HashSet; +use std::ffi::{CString, OsStr}; +use std::io; +use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, OwnedFd}; +use std::os::unix::ffi::OsStrExt as _; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use crate::net::{self, Caller, TcpOutcome}; +use crate::policy::{HandlerId, Permission, RequestedAccess, WireAccess, WirePolicy}; +use crate::process::{self, proc_path_pid}; +use crate::resolve::{Mode, Resolved, Resolver}; +use crate::seccomp::{Brokered, brokered_syscall}; +use crate::supervisor::{AskOutcome, Execution, Shared}; +use crate::sys::{self, cvt, errno, path_cstring}; + +// From (generic ioctl encoding, shared by x86_64/aarch64). +const SECCOMP_IOCTL_NOTIF_RECV: libc::c_ulong = 0xc050_2100; +const SECCOMP_IOCTL_NOTIF_SEND: libc::c_ulong = 0xc018_2101; +const SECCOMP_IOCTL_NOTIF_ID_VALID: libc::c_ulong = 0x4008_2102; +const SECCOMP_IOCTL_NOTIF_ADDFD: libc::c_ulong = 0x4018_2103; +const SECCOMP_ADDFD_FLAG_SEND: u32 = 1 << 1; +const SECCOMP_USER_NOTIF_FLAG_CONTINUE: u32 = 1; + +const PRIO_PROCESS: i32 = 0; +const PRIO_PGRP: i32 = 1; +const IOPRIO_WHO_PROCESS: i32 = 1; +const IOPRIO_WHO_PGRP: i32 = 2; + +#[repr(C)] +#[derive(Default)] +struct NotificationResponse { + id: u64, + val: i64, + error: i32, + flags: u32, +} + +#[repr(C)] +struct AddFd { + id: u64, + flags: u32, + srcfd: u32, + newfd: u32, + newfd_flags: u32, +} + +enum Response { + /// Run the original syscall in the kernel. + Continue, + Errno(i32), + Success, + /// Install `fd` in the target and return its number. + InstallFd { + fd: OwnedFd, + cloexec: bool, + }, +} + +/// Receive one pending notification (main thread). +pub(crate) fn receive(listener: BorrowedFd<'_>) -> Option { + // SAFETY: plain old data; the kernel requires it zeroed before RECV. + let mut notification: libc::seccomp_notif = unsafe { std::mem::zeroed() }; + // SAFETY: RECV fills in the struct above. + let received = unsafe { + libc::ioctl( + listener.as_raw_fd(), + SECCOMP_IOCTL_NOTIF_RECV as _, + &mut notification as *mut libc::seccomp_notif, + ) + }; + // ENOENT: the target was interrupted before we picked it up. + (received == 0).then_some(notification) +} + +/// Handle and answer one notification (worker thread). +pub(crate) fn handle( + shared: &Arc, + execution: &Arc, + notification: libc::seccomp_notif, +) { + let request = Request { + listener: execution.listener.as_fd(), + id: notification.id, + // The UAPI field is named `pid`, but it is the trapping thread's id. + tid: notification.pid as i32, + number: notification.data.nr as i64, + args: notification.data.args, + status: OnceCell::new(), + }; + let policy = execution.sandbox.policy(); + let broker = Broker { + shared, + execution, + request: &request, + policy: &policy, + }; + let response = match brokered_syscall(request.number) { + Some(syscall) => broker + .handle(syscall) + .unwrap_or_else(|error| Response::Errno(error.raw_os_error().unwrap_or(libc::EIO))), + None => Response::Continue, + }; + send_response(&request, response); +} + +/// A trapped syscall, plus access to the target thread. +pub(crate) struct Request<'a> { + listener: BorrowedFd<'a>, + id: u64, + tid: i32, + number: i64, + args: [u64; 6], + /// The thread's `/proc//status` fields, read at most once. + status: OnceCell>, +} + +#[derive(Clone, Copy)] +struct ThreadStatus { + tgid: i32, + umask: u32, +} + +impl Request<'_> { + fn int(&self, index: usize) -> i32 { + self.args[index] as i32 + } + + /// Copy up to `buffer.len()` bytes out of the target, stopping early at + /// an unmapped page. The copy is ours; the target can't touch it + /// afterwards (it may race the copy itself, yielding torn bytes, which + /// are still just bytes). + fn read_memory(&self, address: u64, buffer: &mut [u8]) -> io::Result { + let local = libc::iovec { + iov_base: buffer.as_mut_ptr().cast(), + iov_len: buffer.len(), + }; + let remote = libc::iovec { + iov_base: address as *mut libc::c_void, + iov_len: buffer.len(), + }; + // SAFETY: `local` describes our buffer; the kernel checks `remote`. + let read = unsafe { libc::process_vm_readv(self.tid, &local, 1, &remote, 1, 0) }; + if read < 0 { + return Err(io::Error::last_os_error()); + } + Ok(read as usize) + } + + fn read_bytes(&self, address: u64, length: usize) -> io::Result> { + let mut buffer = vec![0u8; length]; + if self.read_memory(address, &mut buffer)? != length { + return Err(errno(libc::EFAULT)); + } + Ok(buffer) + } + + /// Copy the NUL-terminated string at argument `index` out of the target. + fn read_string(&self, index: usize) -> io::Result> { + let mut address = self.args[index]; + if address == 0 { + return Err(errno(libc::EFAULT)); + } + let mut result = Vec::new(); + let mut chunk = [0u8; 4096]; + while result.len() <= libc::PATH_MAX as usize { + // Never read across a page boundary in one go, so an unmapped page + // after the string doesn't fail the read. + let chunk_end = (address | 0xfff) + 1; + let length = (chunk_end - address) as usize; + let read = self.read_memory(address, &mut chunk[..length])?; + if read == 0 { + return Err(errno(libc::EFAULT)); + } + if let Some(nul) = chunk[..read].iter().position(|byte| *byte == 0) { + result.extend_from_slice(&chunk[..nul]); + return Ok(result); + } + result.extend_from_slice(&chunk[..read]); + address += read as u64; + } + Err(errno(libc::ENAMETOOLONG)) + } + + fn status(&self) -> Option { + *self.status.get_or_init(|| { + let status = process::read_status_head(self.tid)?; + let field = |name: &str| { + status + .lines() + .find_map(|line| line.strip_prefix(name)) + .map(str::trim) + }; + Some(ThreadStatus { + tgid: field("Tgid:")?.parse().ok()?, + umask: u32::from_str_radix(field("Umask:")?, 8).ok()?, + }) + }) + } + + fn umask(&self) -> u32 { + self.status().map_or(0o022, |status| status.umask) + } + + /// Whether the target is still blocked in this syscall. Checked after + /// reading from `/proc/` and before acting, so a recycled tid can't + /// feed us another process's data. + pub fn still_valid(&self) -> bool { + let mut id = self.id; + // SAFETY: ioctl with a pointer to a live u64. + unsafe { + libc::ioctl( + self.listener.as_raw_fd(), + SECCOMP_IOCTL_NOTIF_ID_VALID as _, + &mut id as *mut u64, + ) == 0 + } + } +} + +#[derive(Clone, Copy)] +enum View { + /// The sandbox's mount namespace (read-only mounts). + Sandbox, + /// The supervisor's own (the host's writable mounts). + Host, +} + +#[derive(Clone, Copy)] +enum UnixCall { + Connect, + Bind, +} + +struct Broker<'a> { + shared: &'a Arc, + execution: &'a Arc, + request: &'a Request<'a>, + policy: &'a WirePolicy, +} + +impl Broker<'_> { + fn handle(&self, syscall: Brokered) -> io::Result { + let request = self.request; + let int = |index| request.int(index); + let arg = |index: usize| request.args[index]; + const CWD: i32 = libc::AT_FDCWD; + match syscall { + Brokered::OpenAt => self.open(int(0), 1, int(2), arg(3) as u32), + #[cfg(target_arch = "x86_64")] + Brokered::Open => self.open(CWD, 0, int(1), arg(2) as u32), + #[cfg(target_arch = "x86_64")] + Brokered::Creat => self.open( + CWD, + 0, + libc::O_CREAT | libc::O_WRONLY | libc::O_TRUNC, + arg(1) as u32, + ), + Brokered::MkdirAt => self.mkdir(int(0), 1, arg(2) as u32), + #[cfg(target_arch = "x86_64")] + Brokered::Mkdir => self.mkdir(CWD, 0, arg(1) as u32), + Brokered::UnlinkAt => self.unlink(int(0), 1, int(2)), + #[cfg(target_arch = "x86_64")] + Brokered::Unlink => self.unlink(CWD, 0, 0), + #[cfg(target_arch = "x86_64")] + Brokered::Rmdir => self.unlink(CWD, 0, libc::AT_REMOVEDIR), + #[cfg(target_arch = "x86_64")] + Brokered::RenameAt => self.rename((int(0), 1), (int(2), 3), 0), + Brokered::RenameAt2 => self.rename((int(0), 1), (int(2), 3), arg(4) as u32), + #[cfg(target_arch = "x86_64")] + Brokered::Rename => self.rename((CWD, 0), (CWD, 1), 0), + Brokered::LinkAt => self.link((int(0), 1), (int(2), 3), int(4)), + #[cfg(target_arch = "x86_64")] + Brokered::Link => self.link((CWD, 0), (CWD, 1), 0), + Brokered::SymlinkAt => self.symlink(0, (int(1), 2)), + #[cfg(target_arch = "x86_64")] + Brokered::Symlink => self.symlink(0, (CWD, 1)), + Brokered::FchmodAt => self.chmod(int(0), 1, arg(2) as u32), + #[cfg(target_arch = "x86_64")] + Brokered::Chmod => self.chmod(CWD, 0, arg(1) as u32), + Brokered::Truncate => self.truncate(arg(1) as i64), + Brokered::FaccessAt => self.access(int(0), 1, int(2), 0), + Brokered::FaccessAt2 => self.access(int(0), 1, int(2), int(3)), + #[cfg(target_arch = "x86_64")] + Brokered::Access => self.access(CWD, 0, int(1), 0), + Brokered::UtimensAt => self.utimens(), + Brokered::Kill => self.kill(int(0), int(1)), + Brokered::Tkill => { + let group = process::thread_group(int(0)).ok_or_else(|| errno(libc::ESRCH))?; + self.continue_if_member(group, libc::EPERM) + } + Brokered::Tgkill | Brokered::RtSigqueueinfo | Brokered::RtTgsigqueueinfo => { + self.continue_if_member(int(0), libc::EPERM) + } + Brokered::PidfdOpen => self.continue_if_member(int(0), libc::ESRCH), + Brokered::Prlimit64 + | Brokered::SchedSetaffinity + | Brokered::SchedSetscheduler + | Brokered::SchedSetparam + | Brokered::SchedSetattr => match int(0) { + 0 => Ok(Response::Continue), + pid => self.continue_if_member(pid, libc::EPERM), + }, + Brokered::Setpriority => self.process_or_group(int(0), int(1), PRIO_PROCESS, PRIO_PGRP), + Brokered::IoprioSet => { + self.process_or_group(int(0), int(1), IOPRIO_WHO_PROCESS, IOPRIO_WHO_PGRP) + } + Brokered::Connect => self.connect(int(0), arg(1), arg(2) as u32), + Brokered::Bind => self.bind(int(0), arg(1), arg(2) as u32), + } + } + + /// The target's socket `fd` and its address family, or `None` if `fd` + /// isn't an open socket (the kernel reports that itself). + fn target_socket(&self, fd: i32) -> io::Result> { + // Through the helper, which can fetch the descriptor where we, the + // command's uncle, may be denied the ptrace access it takes. + let socket = match self.execution.network.fetch_fd(self.tgid()?, fd) { + Ok(socket) => socket, + Err(error) if error.raw_os_error() == Some(libc::EBADF) => return Ok(None), + Err(error) => return Err(error), + }; + if !self.request.still_valid() { + return Err(errno(libc::ESRCH)); + } + Ok( + sys::socket_option(socket.as_fd(), libc::SOL_SOCKET, libc::SO_DOMAIN) + .ok() + .map(|domain| (socket, domain)), + ) + } + + fn bind(&self, fd: i32, address: u64, length: u32) -> io::Result { + match self.target_socket(fd)? { + Some((socket, libc::AF_UNIX)) => { + self.unix_address_call(&socket, address, length, UnixCall::Bind) + } + // Addresses of other families only mean something inside the + // sandbox's network namespace. + _ => Ok(Response::Continue), + } + } + + fn connect(&self, fd: i32, address: u64, length: u32) -> io::Result { + let Some((socket, domain)) = self.target_socket(fd)? else { + return Ok(Response::Continue); + }; + match domain { + libc::AF_INET | libc::AF_INET6 => {} + libc::AF_UNIX => { + return self.unix_address_call(&socket, address, length, UnixCall::Connect); + } + // Netlink: within the namespace. + _ => return Ok(Response::Continue), + } + let socket = socket.as_fd(); + let length = (length as usize).min(size_of::()); + if address == 0 || length == 0 { + return Ok(Response::Continue); + } + let Some(destination) = net::decode_address(&self.request.read_bytes(address, length)?) + else { + // `AF_UNSPEC` (dissolving a UDP association), or nonsense the + // kernel rejects. + return Ok(Response::Continue); + }; + let socket_type = sys::socket_option(socket, libc::SOL_SOCKET, libc::SO_TYPE)?; + let protocol = sys::socket_option(socket, libc::SOL_SOCKET, libc::SO_PROTOCOL)?; + match (socket_type, protocol) { + (libc::SOCK_STREAM, libc::IPPROTO_TCP | libc::IPPROTO_MPTCP) => { + let still_valid = || self.request.still_valid(); + let caller = Caller { + tid: self.request.tid, + still_valid: &still_valid, + }; + match net::connect_tcp(self.shared, self.execution, socket, destination, &caller)? { + TcpOutcome::Continue => Ok(Response::Continue), + TcpOutcome::Relayed => Ok(Response::Success), + } + } + (libc::SOCK_DGRAM, libc::IPPROTO_UDP) => { + net::connect_udp(self.shared, self.execution, destination); + Ok(Response::Continue) + } + _ => Ok(Response::Continue), + } + } + + /// `connect` or `bind` of a unix socket. Never continued: the kernel + /// would read the address again, after the target had a chance to + /// change it. We make the call on our copy of the socket instead. + /// + /// - Abstract and unnamed addresses are looked up in the socket's own + /// network namespace (the sandbox's), so they're used as they are. + /// - A path is resolved like any other: `connect` needs `Connect` on the + /// socket it finds, `bind` needs `Write` on the entry it creates. The + /// call then goes through `/proc/self/fd`, reaching exactly what was + /// checked. (So `getsockname` on a bound socket shows that path.) + fn unix_address_call( + &self, + socket: &OwnedFd, + address: u64, + length: u32, + call: UnixCall, + ) -> io::Result { + let length = length as usize; + if address == 0 { + return Err(errno(libc::EFAULT)); + } + if !(size_of::()..=size_of::()).contains(&length) { + return Err(errno(libc::EINVAL)); + } + let bytes = self.request.read_bytes(address, length)?; + if i32::from(u16::from_ne_bytes([bytes[0], bytes[1]])) != libc::AF_UNIX { + return Err(errno(libc::EINVAL)); + } + let name = &bytes[2..]; + let mut _held = None; + let address = match name.first() { + None | Some(0) => bytes.clone(), + Some(_) => { + let end = name + .iter() + .position(|byte| *byte == 0) + .unwrap_or(name.len()); + let path = &name[..end]; + let through = match call { + UnixCall::Connect => { + let Resolved::Object(object) = self.resolve_bytes( + View::Sandbox, + libc::AT_FDCWD, + path, + Mode::Object { follow: true }, + )? + else { + return Err(errno(libc::ENOENT)); + }; + if sys::fstat(object.as_fd())?.st_mode & libc::S_IFMT != libc::S_IFSOCK { + return Err(errno(libc::ECONNREFUSED)); + } + let path = self + .object_path(&object)? + .ok_or_else(|| errno(libc::ECONNREFUSED))?; + self.authorize(&[(Permission::Connect, &path)])?; + let through = sys::proc_self_fd(object.as_fd()); + _held = Some(object); + through + } + UnixCall::Bind => { + let (parent, name, path) = + self.entry_bytes(libc::AT_FDCWD, path, libc::EADDRINUSE)?; + self.authorize(&[(Permission::Write, &path)])?; + let through = sys::proc_self_fd(parent.as_fd()) + .join(OsStr::from_bytes(name.as_bytes())); + _held = Some(parent); + through + } + }; + let through = through.as_os_str().as_bytes(); + if through.len() >= size_of::() - 2 { + return Err(errno(libc::ENAMETOOLONG)); + } + let mut address = bytes[..2].to_vec(); + address.extend_from_slice(through); + address.push(0); + address + } + }; + let (fd, pointer, length) = ( + socket.as_raw_fd(), + address.as_ptr().cast(), + address.len() as libc::socklen_t, + ); + // SAFETY: a sockaddr_un we built, with its length. + cvt(unsafe { + match call { + UnixCall::Connect => libc::connect(fd, pointer, length), + UnixCall::Bind => libc::bind(fd, pointer, length), + } + })?; + Ok(Response::Success) + } + + /// The root of the execution's mount namespace. Every process in it has + /// the same one: none can `chroot` or `pivot_root` without capabilities. + fn sandbox_root(&self) -> io::Result> { + if let Some(root) = self.execution.root.get() { + return Ok(root.as_fd()); + } + let root = sys::open( + Path::new(&format!("/proc/{}/root", self.request.tid)), + libc::O_PATH | libc::O_DIRECTORY, + )?; + // The tid could have been recycled since the notification; don't + // keep another process's root. + if !self.request.still_valid() { + return Err(errno(libc::ESRCH)); + } + Ok(self.execution.root.get_or_init(|| root).as_fd()) + } + + fn tgid(&self) -> io::Result { + self.request + .status() + .map(|status| status.tgid) + .ok_or_else(|| errno(libc::ESRCH)) + } + + /// The absolute path (in the sandbox's view) the target means by + /// `(dirfd, path argument)`. + fn absolute_path(&self, dirfd: i32, raw: &[u8]) -> io::Result { + if raw.is_empty() { + return Err(errno(libc::ENOENT)); + } + let raw = Path::new(OsStr::from_bytes(raw)); + if raw.is_absolute() { + return Ok(raw.to_path_buf()); + } + let link = if dirfd == libc::AT_FDCWD { + format!("/proc/{}/cwd", self.request.tid) + } else { + format!("/proc/{}/fd/{dirfd}", self.request.tid) + }; + // Read from outside, these render in the sandbox's view. + let base = std::fs::read_link(link).map_err(|_| errno(libc::EBADF))?; + if is_deleted(&base) { + return Err(errno(libc::ENOENT)); + } + if !base.is_absolute() { + return Err(errno(libc::ENOTDIR)); + } + Ok(base.join(raw)) + } + + fn resolve(&self, view: View, dirfd: i32, path_arg: usize, mode: Mode) -> io::Result { + let raw = self.request.read_string(path_arg)?; + self.resolve_bytes(view, dirfd, &raw, mode) + } + + fn resolve_bytes( + &self, + view: View, + dirfd: i32, + raw: &[u8], + mode: Mode, + ) -> io::Result { + let path = self.absolute_path(dirfd, raw)?; + let root = match view { + View::Sandbox => self.sandbox_root()?, + View::Host => self.shared.host_root(), + }; + let resolver = Resolver { + root, + tid: self.request.tid, + tgid: &|| self.tgid(), + tree: self.execution.tree, + }; + let resolved = resolver.resolve(&path, mode)?; + if !self.request.still_valid() { + return Err(errno(libc::ESRCH)); + } + Ok(resolved) + } + + /// The path of an object we hold, for checking the rules. `None` for + /// objects without one (pipes and sockets reached through the target's + /// own `/proc//fd`). + fn object_path(&self, object: &OwnedFd) -> io::Result> { + let path = sys::fd_path(object.as_fd())?; + if !path.is_absolute() { + return Ok(None); + } + if is_deleted(&path) { + return Err(errno(libc::ENOENT)); + } + self.check_process_path(&path)?; + Ok(Some(path)) + } + + fn entry_path(&self, parent: &OwnedFd, name: &CString) -> io::Result { + let parent = sys::fd_path(parent.as_fd())?; + if is_deleted(&parent) || !parent.is_absolute() { + return Err(errno(libc::ENOENT)); + } + let path = parent.join(OsStr::from_bytes(name.as_bytes())); + self.check_process_path(&path)?; + Ok(path) + } + + /// Until process rules exist, `/proc/` of anything outside this + /// execution is off limits (it would expose e.g. Zed's environment). + fn check_process_path(&self, path: &Path) -> io::Result<()> { + match proc_path_pid(path) { + Some(pid) if !self.execution.tree.contains(pid) => Err(errno(libc::EACCES)), + _ => Ok(()), + } + } + + fn authorize(&self, checks: &[(Permission, &Path)]) -> io::Result<()> { + let denied = |permission| match permission { + Permission::Read | Permission::Connect => errno(libc::EACCES), + // Tools treat `EACCES` as "write-protected" and may prompt (e.g. + // `rm`), but give up on `EROFS`. + Permission::Write => errno(libc::EROFS), + }; + let sandbox_id = self.execution.sandbox.id; + // Granted once, for this syscall only. + let mut granted: Vec = Vec::new(); + // Re-evaluated after every prompt, against the live policy: a handler + // may have remembered rules that now decide the rest. + loop { + let policy = self.execution.sandbox.policy(); + // Requests that need asking, grouped by handler; each handler + // that's involved decides everything it covers in one go. + let mut asks: Vec<(HandlerId, Permission, Vec)> = Vec::new(); + for &(permission, path) in checks { + let request = RequestedAccess::Fs { + permission, + path: path.to_path_buf(), + }; + if granted.contains(&request) { + continue; + } + match policy.fs_access(path, permission) { + WireAccess::Allow => {} + WireAccess::Deny => { + self.shared + .report_denial(sandbox_id, vec![request], self.request.tid); + return Err(denied(permission)); + } + WireAccess::Ask(handler) => { + match asks.iter_mut().find(|(id, _, _)| *id == handler) { + Some((_, _, requests)) => requests.push(request), + None => asks.push((handler, permission, vec![request])), + } + } + } + } + let Some((handler, first, requests)) = asks.into_iter().next() else { + return Ok(()); + }; + let still_valid = || self.request.still_valid(); + let sandbox = &self.execution.sandbox; + match self.shared.ask( + sandbox, + handler, + requests.clone(), + self.request.tid, + still_valid, + ) { + AskOutcome::Decided(true) => granted.extend(requests), + AskOutcome::Decided(false) => { + self.shared + .report_denial(sandbox_id, requests, self.request.tid); + return Err(denied(first)); + } + AskOutcome::Retry => {} + } + } + } + + fn open(&self, dirfd: i32, path_arg: usize, flags: i32, mode: u32) -> io::Result { + let access_mode = flags & libc::O_ACCMODE; + let reads = access_mode != libc::O_WRONLY; + let writes = access_mode != libc::O_RDONLY || flags & (libc::O_CREAT | libc::O_TRUNC) != 0; + let exclusive = flags & (libc::O_CREAT | libc::O_EXCL) == libc::O_CREAT | libc::O_EXCL; + // `O_CREAT | O_EXCL` never follows a final symlink. + let follow = flags & libc::O_NOFOLLOW == 0 && !exclusive; + let cloexec = flags & libc::O_CLOEXEC != 0; + let view = if writes { View::Host } else { View::Sandbox }; + + match self.resolve(view, dirfd, path_arg, Mode::Object { follow })? { + Resolved::Object(object) => { + if exclusive { + return Err(errno(libc::EEXIST)); + } + let stat = sys::fstat(object.as_fd())?; + if stat.st_mode & libc::S_IFMT == libc::S_IFLNK { + return Err(errno(libc::ELOOP)); + } + let Some(path) = self.object_path(&object)? else { + return Ok(Response::InstallFd { + fd: reopen(&object, flags)?, + cloexec, + }); + }; + let mut checks = Vec::new(); + if writes { + checks.push((Permission::Write, path.as_path())); + } + // Listing a directory isn't reading contents. + if reads && stat.st_mode & libc::S_IFMT != libc::S_IFDIR { + checks.push((Permission::Read, path.as_path())); + } + self.authorize(&checks)?; + let fd = if is_controlling_terminal_device(&stat) { + self.open_controlling_terminal(flags)? + } else { + reopen(&object, flags)? + }; + Ok(Response::InstallFd { fd, cloexec }) + } + Resolved::Entry { parent, name } => { + if flags & libc::O_CREAT == 0 { + return Err(errno(libc::ENOENT)); + } + let path = self.entry_path(&parent, &name)?; + let mut checks = vec![(Permission::Write, path.as_path())]; + if reads { + checks.push((Permission::Read, path.as_path())); + } + self.authorize(&checks)?; + let mode = mode & 0o7777 & !self.request.umask(); + // O_EXCL | O_NOFOLLOW: create exactly the entry we checked. + match sys::openat( + parent.as_raw_fd(), + &name, + flags | libc::O_EXCL | libc::O_NOFOLLOW | libc::O_NOCTTY, + mode, + ) { + Ok(fd) => { + // Our own umask applied too; restore the target's intent. + // SAFETY: fchmod on an fd we own. + cvt(unsafe { libc::fchmod(fd.as_raw_fd(), mode) })?; + Ok(Response::InstallFd { fd, cloexec }) + } + // Someone created it since we looked (e.g. a concurrent + // create that shared our prompt). Without O_EXCL the kernel + // would open it, so open it too: the same entry we checked, + // never through a symlink. + Err(error) if error.raw_os_error() == Some(libc::EEXIST) && !exclusive => { + let fd = sys::openat( + parent.as_raw_fd(), + &name, + flags & !libc::O_CREAT | libc::O_NOFOLLOW | libc::O_NOCTTY, + 0, + )?; + Ok(Response::InstallFd { fd, cloexec }) + } + Err(error) => Err(error), + } + } + } + } + + /// `/dev/tty` means "my controlling terminal": opened by us it would be + /// ours, so open the target's instead. + fn open_controlling_terminal(&self, flags: i32) -> io::Result { + let device = process::controlling_terminal(self.tgid()?).unwrap_or(0); + let (major, minor) = (libc::major(device), libc::minor(device)); + let path = match major { + 0 => return Err(errno(libc::ENXIO)), + 136..=143 => format!("/dev/pts/{}", (major - 136) * 256 + minor), + 4 if minor < 64 => format!("/dev/tty{minor}"), + _ => return Err(errno(libc::ENXIO)), + }; + sys::open( + Path::new(&path), + flags & !(libc::O_CREAT | libc::O_EXCL | libc::O_TRUNC) | libc::O_NOCTTY, + ) + } + + fn entry(&self, dirfd: i32, path_arg: usize) -> io::Result<(OwnedFd, CString, PathBuf)> { + self.entry_bytes(dirfd, &self.request.read_string(path_arg)?, libc::EINVAL) + } + + /// A directory entry to create or remove, as `(parent, name, path)`. + /// Fails with `exists` if `raw` names an existing object. + fn entry_bytes( + &self, + dirfd: i32, + raw: &[u8], + exists: i32, + ) -> io::Result<(OwnedFd, CString, PathBuf)> { + match self.resolve_bytes(View::Host, dirfd, raw, Mode::Entry)? { + Resolved::Entry { parent, name } => { + let path = self.entry_path(&parent, &name)?; + Ok((parent, name, path)) + } + Resolved::Object(_) => Err(errno(exists)), + } + } + + fn mkdir(&self, dirfd: i32, path_arg: usize, mode: u32) -> io::Result { + let (parent, name, path) = self.entry(dirfd, path_arg)?; + self.authorize(&[(Permission::Write, &path)])?; + let mode = mode & 0o7777 & !self.request.umask(); + // SAFETY: *at calls on a directory fd we own with a single name. + unsafe { + cvt(libc::mkdirat(parent.as_raw_fd(), name.as_ptr(), mode))?; + cvt(libc::fchmodat(parent.as_raw_fd(), name.as_ptr(), mode, 0))?; + } + Ok(Response::Success) + } + + fn unlink(&self, dirfd: i32, path_arg: usize, flags: i32) -> io::Result { + let (parent, name, path) = self.entry(dirfd, path_arg)?; + self.authorize(&[(Permission::Write, &path)])?; + // SAFETY: unlinkat on a directory fd we own with a single name. + cvt(unsafe { libc::unlinkat(parent.as_raw_fd(), name.as_ptr(), flags) })?; + Ok(Response::Success) + } + + /// Whether moving the entry at `from` to `to` keeps every rule's verdict + /// for the moved contents from widening. + fn check_move( + &self, + parent: &OwnedFd, + name: &CString, + from: &Path, + to: &Path, + ) -> io::Result<()> { + let fs = self.policy; + let allowed = if is_directory_entry(parent, name)? { + Permission::ALL + .iter() + .all(|permission| fs.same_beneath(from, to, *permission)) + } else { + !Permission::ALL + .iter() + .any(|permission| fs.widens(from, to, *permission)) + }; + if allowed { + Ok(()) + } else { + Err(errno(libc::EACCES)) + } + } + + fn rename(&self, old: (i32, usize), new: (i32, usize), flags: u32) -> io::Result { + let (old_parent, old_name, old_path) = self.entry(old.0, old.1)?; + let (new_parent, new_name, new_path) = self.entry(new.0, new.1)?; + self.authorize(&[ + (Permission::Write, &old_path), + (Permission::Write, &new_path), + ])?; + self.check_move(&old_parent, &old_name, &old_path, &new_path)?; + if flags & libc::RENAME_EXCHANGE != 0 { + self.check_move(&new_parent, &new_name, &new_path, &old_path)?; + } + // SAFETY: renameat2 on directory fds we own with single names. + let result = unsafe { + libc::syscall( + libc::SYS_renameat2, + old_parent.as_raw_fd(), + old_name.as_ptr(), + new_parent.as_raw_fd(), + new_name.as_ptr(), + flags, + ) + }; + cvt(result as i32)?; + Ok(Response::Success) + } + + fn link(&self, old: (i32, usize), new: (i32, usize), flags: i32) -> io::Result { + if flags & libc::AT_EMPTY_PATH != 0 { + return Err(errno(libc::EPERM)); + } + let (new_parent, new_name, new_path) = self.entry(new.0, new.1)?; + // A new name must not reach the file with access its old one lacked. + let check_source = |source: &Path| { + self.authorize(&[(Permission::Write, &new_path)])?; + let fs = self.policy; + if Permission::ALL + .iter() + .any(|permission| fs.widens(source, &new_path, *permission)) + { + return Err(errno(libc::EACCES)); + } + Ok(()) + }; + let result = if flags & libc::AT_SYMLINK_FOLLOW != 0 { + let Resolved::Object(object) = + self.resolve(View::Host, old.0, old.1, Mode::Object { follow: true })? + else { + return Err(errno(libc::ENOENT)); + }; + let source = self + .object_path(&object)? + .ok_or_else(|| errno(libc::ENOENT))?; + check_source(&source)?; + // SAFETY: linkat through our own fd's magic link. + unsafe { + libc::linkat( + libc::AT_FDCWD, + path_cstring(&sys::proc_self_fd(object.as_fd()))?.as_ptr(), + new_parent.as_raw_fd(), + new_name.as_ptr(), + libc::AT_SYMLINK_FOLLOW, + ) + } + } else { + let (old_parent, old_name, source) = self.entry(old.0, old.1)?; + check_source(&source)?; + // SAFETY: linkat on directory fds we own with single names. + unsafe { + libc::linkat( + old_parent.as_raw_fd(), + old_name.as_ptr(), + new_parent.as_raw_fd(), + new_name.as_ptr(), + 0, + ) + } + }; + cvt(result)?; + Ok(Response::Success) + } + + fn symlink(&self, content_arg: usize, link: (i32, usize)) -> io::Result { + // The link's content is never resolved by us. + let content = sys::cstring(&self.request.read_string(content_arg)?)?; + let (parent, name, path) = self.entry(link.0, link.1)?; + self.authorize(&[(Permission::Write, &path)])?; + // SAFETY: symlinkat on a directory fd we own with a single name. + cvt(unsafe { libc::symlinkat(content.as_ptr(), parent.as_raw_fd(), name.as_ptr()) })?; + Ok(Response::Success) + } + + /// Shared shape of chmod/truncate/access/utimens: act on an existing + /// object through its `/proc/self/fd` magic link. + fn with_object( + &self, + dirfd: i32, + path_arg: usize, + follow: bool, + act: impl FnOnce(&CString) -> libc::c_int, + ) -> io::Result { + let Resolved::Object(object) = + self.resolve(View::Host, dirfd, path_arg, Mode::Object { follow })? + else { + return Err(errno(libc::ENOENT)); + }; + let path = self + .object_path(&object)? + .ok_or_else(|| errno(libc::EPERM))?; + self.authorize(&[(Permission::Write, &path)])?; + cvt(act(&path_cstring(&sys::proc_self_fd(object.as_fd()))?))?; + Ok(Response::Success) + } + + fn chmod(&self, dirfd: i32, path_arg: usize, mode: u32) -> io::Result { + // SAFETY: fchmodat on a NUL-terminated path. + self.with_object(dirfd, path_arg, true, |path| unsafe { + libc::fchmodat(libc::AT_FDCWD, path.as_ptr(), mode, 0) + }) + } + + fn truncate(&self, length: i64) -> io::Result { + // SAFETY: truncate on a NUL-terminated path. + self.with_object(libc::AT_FDCWD, 0, true, |path| unsafe { + libc::truncate(path.as_ptr(), length) + }) + } + + fn access(&self, dirfd: i32, path_arg: usize, mode: i32, flags: i32) -> io::Result { + let follow = flags & libc::AT_SYMLINK_NOFOLLOW == 0; + // SAFETY: faccessat on a NUL-terminated path. + self.with_object(dirfd, path_arg, follow, |path| unsafe { + libc::faccessat( + libc::AT_FDCWD, + path.as_ptr(), + mode, + flags & libc::AT_EACCESS, + ) + }) + } + + fn utimens(&self) -> io::Result { + let request = self.request; + let times = match request.args[2] { + 0 => None, + address => { + let bytes = request.read_bytes(address, 32)?; + // Decode field by field; never transmute target bytes. + let field = |offset: usize| -> i64 { + let mut value = [0u8; 8]; + value.copy_from_slice(&bytes[offset..offset + 8]); + i64::from_ne_bytes(value) + }; + Some([ + libc::timespec { + tv_sec: field(0), + tv_nsec: field(8), + }, + libc::timespec { + tv_sec: field(16), + tv_nsec: field(24), + }, + ]) + } + }; + let follow = request.int(3) & libc::AT_SYMLINK_NOFOLLOW == 0; + self.with_object(request.int(0), 1, follow, |path| { + let times = times + .as_ref() + .map_or(std::ptr::null(), |times| times.as_ptr()); + // SAFETY: NUL-terminated path; null or two valid timespecs. + unsafe { libc::utimensat(libc::AT_FDCWD, path.as_ptr(), times, 0) } + }) + } + + fn continue_if_member(&self, pid: i32, error: i32) -> io::Result { + if self.execution.tree.contains(pid) { + Ok(Response::Continue) + } else { + Err(errno(error)) + } + } + + /// `kill`. Process groups and `-1` may include processes outside the + /// sandbox (e.g. Zed itself, if the sandbox inherited its group), so + /// those are emulated by signalling only the members. + fn kill(&self, pid: i32, signal: i32) -> io::Result { + if pid > 0 { + return self.continue_if_member(pid, libc::EPERM); + } + let caller = self.tgid()?; + let group = match pid { + 0 => { + process::process_info(caller) + .ok_or_else(|| errno(libc::ESRCH))? + .group + } + -1 => 0, + _ => pid.checked_neg().ok_or_else(|| errno(libc::EINVAL))?, + }; + let in_scope: Vec = process::all_processes() + .into_iter() + .filter(|process| pid == -1 || process.group == group) + .map(|process| process.pid) + .collect(); + let members: HashSet = self + .execution + .tree + .members() + .into_iter() + .map(|process| process.pid) + .collect(); + if pid != -1 && !in_scope.is_empty() && in_scope.iter().all(|pid| members.contains(pid)) { + return Ok(Response::Continue); + } + let targets: Vec = in_scope + .into_iter() + .filter(|target| members.contains(target) && !(pid == -1 && *target == caller)) + .collect(); + if targets.is_empty() { + return Err(errno(libc::ESRCH)); + } + if signal != 0 { + for target in targets { + // SAFETY: plain kill. + unsafe { libc::kill(target, signal) }; + } + } + Ok(Response::Success) + } + + /// `setpriority` / `ioprio_set`: a process, a process group (all of whose + /// members must be ours), or a user (never: it covers every process of + /// the user). + fn process_or_group( + &self, + which: i32, + who: i32, + process: i32, + group: i32, + ) -> io::Result { + if which == process { + return match who { + 0 => Ok(Response::Continue), + pid => self.continue_if_member(pid, libc::EPERM), + }; + } + if which != group { + return Err(errno(libc::EPERM)); + } + let group = match who { + 0 => { + process::process_info(self.tgid()?) + .ok_or_else(|| errno(libc::ESRCH))? + .group + } + group => group, + }; + let tree = self.execution.tree; + let all_members = process::all_processes() + .into_iter() + .filter(|process| process.group == group) + .all(|process| tree.contains(process.pid)); + if all_members { + Ok(Response::Continue) + } else { + Err(errno(libc::EPERM)) + } + } +} + +fn is_deleted(path: &Path) -> bool { + path.as_os_str().as_bytes().ends_with(b" (deleted)") +} + +fn is_controlling_terminal_device(stat: &libc::stat) -> bool { + stat.st_mode & libc::S_IFMT == libc::S_IFCHR && stat.st_rdev == libc::makedev(5, 0) +} + +fn is_directory_entry(parent: &OwnedFd, name: &CString) -> io::Result { + // SAFETY: plain old data, filled in by the kernel. + let mut stat: libc::stat = unsafe { std::mem::zeroed() }; + // SAFETY: valid directory fd, NUL-terminated name and stat pointer. + cvt(unsafe { + libc::fstatat( + parent.as_raw_fd(), + name.as_ptr(), + &mut stat, + libc::AT_SYMLINK_NOFOLLOW, + ) + })?; + Ok(stat.st_mode & libc::S_IFMT == libc::S_IFDIR) +} + +/// Open the object `object` refers to with the target's flags. Through the +/// magic link, the new fd uses the mount `object` was found on. +fn reopen(object: &OwnedFd, flags: i32) -> io::Result { + sys::open( + &sys::proc_self_fd(object.as_fd()), + flags & !(libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW) | libc::O_NOCTTY, + ) +} + +fn send_response(request: &Request, response: Response) { + let listener = request.listener.as_raw_fd(); + let mut reply = NotificationResponse { + id: request.id, + ..Default::default() + }; + match response { + Response::Continue => reply.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE, + Response::Errno(code) => reply.error = -code, + Response::Success => {} + Response::InstallFd { fd, cloexec } => { + let mut add = AddFd { + id: request.id, + flags: SECCOMP_ADDFD_FLAG_SEND, + srcfd: fd.as_raw_fd() as u32, + newfd: 0, + newfd_flags: if cloexec { libc::O_CLOEXEC as u32 } else { 0 }, + }; + // SAFETY: ADDFD with a correctly laid out struct. With + // ADDFD_FLAG_SEND (5.14) it also answers the syscall atomically. + let result = unsafe { + libc::ioctl( + listener, + SECCOMP_IOCTL_NOTIF_ADDFD as _, + &mut add as *mut AddFd, + ) + }; + if result >= 0 || io::Error::last_os_error().raw_os_error() != Some(libc::EINVAL) { + // Done, or the target is gone (ENOENT) and needs no answer. + return; + } + // Pre-5.14: install, then answer separately. + add.flags = 0; + // SAFETY: as above. + let installed = unsafe { + libc::ioctl( + listener, + SECCOMP_IOCTL_NOTIF_ADDFD as _, + &mut add as *mut AddFd, + ) + }; + if installed < 0 { + reply.error = -io::Error::last_os_error() + .raw_os_error() + .unwrap_or(libc::EIO); + } else { + reply.val = installed as i64; + } + } + } + // SAFETY: SEND with a correctly laid out struct. Failure means the target + // was interrupted or died, which needs no handling. + unsafe { + libc::ioctl( + listener, + SECCOMP_IOCTL_NOTIF_SEND as _, + &mut reply as *mut NotificationResponse, + ); + } +} diff --git a/crates/sandbox_linux/src/dns.rs b/crates/sandbox_linux/src/dns.rs new file mode 100644 index 00000000000..fc566a537de --- /dev/null +++ b/crates/sandbox_linux/src/dns.rs @@ -0,0 +1,380 @@ +//! The sandbox's DNS server. +//! +//! Every DNS query from the sandbox reaches the supervisor: port 53 of every +//! address in the sandbox's namespace is a socket it serves, whichever server +//! the command thinks it's asking. Names are answered with fake IPv4 +//! addresses from `198.18.0.0/15` (one per name per sandbox), so a later +//! connection to one says which name it's for, and the policy is checked +//! against the name. +//! +//! - A name no rule could allow doesn't exist (`NXDOMAIN`). +//! - A name only `Ask` rules cover gets an address without a host lookup: +//! nothing about it leaves the machine before the user decides. +//! - A name an `Allow` rule covers is looked up on the host first, so names +//! that don't exist still fail to resolve. +//! +//! Only `A` queries get answers: `AAAA` and other types get none, so clients +//! fall back to IPv4. `PTR` queries for fake addresses return their names. + +use std::collections::HashMap; +use std::io::{self, Read as _, Write as _}; +use std::net::{Ipv4Addr, TcpListener, TcpStream, ToSocketAddrs as _}; +use std::os::fd::{AsFd as _, OwnedFd}; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::{Duration, Instant}; + +use crate::net::{self, lock}; +use crate::policy::{NameVerdict, normalize_domain}; +use crate::supervisor::{Execution, SandboxState}; + +const FAKE_BASE: u32 = u32::from_be_bytes([198, 18, 0, 0]); +const FAKE_SIZE: u32 = 1 << 17; +/// Short, so a fake address isn't used long after a policy change. +const TTL: u32 = 10; +const HOST_LOOKUP_CACHE: Duration = Duration::from_secs(10); +const MAX_CONCURRENT_QUERIES: usize = 32; +const TCP_IDLE_TIMEOUT: Duration = Duration::from_secs(10); + +const TYPE_A: u16 = 1; +const TYPE_PTR: u16 = 12; +const TYPE_ANY: u16 = 255; +const CLASS_IN: u16 = 1; +const FORMAT_ERROR: u16 = 1; +const NAME_ERROR: u16 = 3; +const NOT_IMPLEMENTED: u16 = 4; + +pub(crate) fn is_fake(address: Ipv4Addr) -> bool { + u32::from(address).wrapping_sub(FAKE_BASE) < FAKE_SIZE +} + +/// A sandbox's fake addresses, which last as long as it does. +#[derive(Default)] +pub(crate) struct Names { + by_name: HashMap, + by_address: HashMap, + allocated: u32, + /// Whether names exist on the host, as recently looked up. + host_lookups: HashMap, +} + +impl Names { + fn address_for(&mut self, name: &str) -> Option { + if let Some(address) = self.by_name.get(name) { + return Some(*address); + } + if self.allocated + 1 >= FAKE_SIZE { + return None; + } + self.allocated += 1; + let address = Ipv4Addr::from(FAKE_BASE + self.allocated); + self.by_name.insert(name.to_string(), address); + self.by_address.insert(address, name.to_string()); + Some(address) + } + + pub fn name_for(&self, address: Ipv4Addr) -> Option { + self.by_address.get(&address).cloned() + } +} + +pub(crate) fn serve_udp(execution: &Arc, socket: &Arc) { + let in_flight = Arc::new(AtomicUsize::new(0)); + let mut buffer = vec![0; 4096]; + while !execution.network.is_closed() { + let datagram = match net::receive_datagram(socket.as_fd(), &mut buffer) { + Ok(Some(datagram)) => datagram, + Ok(None) => continue, + Err(error) if error.raw_os_error() == Some(libc::EINTR) => continue, + Err(error) => { + eprintln!("zed sandbox supervisor: receiving DNS queries: {error}"); + return; + } + }; + let query = buffer[..datagram.length].to_vec(); + let respond = { + let execution = execution.clone(); + let socket = socket.clone(); + move || { + if let Some(response) = answer(&execution.sandbox, &query) { + // The reply comes from the address that was asked. + if let Err(error) = net::send_datagram( + socket.as_fd(), + &response, + datagram.source, + datagram.destination, + ) { + eprintln!("zed sandbox supervisor: answering a DNS query: {error}"); + } + } + } + }; + // Answering may wait for a host lookup, so answer concurrently (up + // to a limit). + if in_flight.fetch_add(1, Ordering::SeqCst) < MAX_CONCURRENT_QUERIES { + let in_flight = in_flight.clone(); + let spawned = std::thread::Builder::new() + .name("zed-sandbox-dns-query".to_string()) + .spawn(move || { + respond(); + in_flight.fetch_sub(1, Ordering::SeqCst); + }); + if let Err(error) = spawned { + eprintln!("zed sandbox supervisor: answering a DNS query: {error}"); + } + } else { + in_flight.fetch_sub(1, Ordering::SeqCst); + respond(); + } + } +} + +pub(crate) fn serve_tcp(execution: &Arc, socket: &Arc) { + let listener = match socket.try_clone() { + Ok(listener) => TcpListener::from(listener), + Err(error) => { + eprintln!("zed sandbox supervisor: serving DNS over TCP: {error}"); + return; + } + }; + while !execution.network.is_closed() { + match listener.accept() { + Ok((stream, _)) => { + let execution = execution.clone(); + let spawned = std::thread::Builder::new() + .name("zed-sandbox-dns-tcp".to_string()) + .spawn(move || serve_tcp_connection(&execution.sandbox, stream)); + if let Err(error) = spawned { + eprintln!("zed sandbox supervisor: serving DNS over TCP: {error}"); + } + } + Err(error) if error.kind() == io::ErrorKind::Interrupted => {} + // Shut down with the execution. + Err(_) => return, + } + } +} + +/// Length-prefixed queries and answers, until the client stops. +fn serve_tcp_connection(sandbox: &SandboxState, mut stream: TcpStream) { + let mut serve = || -> io::Result<()> { + stream.set_read_timeout(Some(TCP_IDLE_TIMEOUT))?; + loop { + let mut length = [0; 2]; + stream.read_exact(&mut length)?; + let mut query = vec![0; usize::from(u16::from_be_bytes(length))]; + stream.read_exact(&mut query)?; + let Some(response) = answer(sandbox, &query) else { + return Ok(()); + }; + let mut message = (response.len() as u16).to_be_bytes().to_vec(); + message.extend(response); + stream.write_all(&message)?; + } + }; + // Ending (or timing out, or a malformed length) closes the connection, + // which is all a DNS client needs to know. + serve().ok(); +} + +struct Question { + name: String, + record_type: u16, + class: u16, + /// Where the question ends in the query. + end: usize, +} + +/// The response to one query, if it is one. +fn answer(sandbox: &SandboxState, query: &[u8]) -> Option> { + let flags = u16::from_be_bytes(query.get(2..4)?.try_into().ok()?); + if flags & 0x8000 != 0 { + return None; + } + let opcode = (flags >> 11) & 0xf; + let question_count = u16::from_be_bytes(query.get(4..6)?.try_into().ok()?); + if opcode != 0 { + return Some(response(query, NOT_IMPLEMENTED, None, None)); + } + let question = match question_count { + 1 => parse_question(query), + _ => None, + }; + let Some(question) = question else { + return Some(response(query, FORMAT_ERROR, None, None)); + }; + let echoed = &query[12..question.end]; + if question.class != CLASS_IN { + return Some(response(query, NOT_IMPLEMENTED, Some(echoed), None)); + } + if question.record_type == TYPE_PTR { + let name = reverse_lookup(sandbox, &question.name); + return Some(match name { + Some(name) => response(query, 0, Some(echoed), Some((TYPE_PTR, encode_name(&name)))), + None => response(query, NAME_ERROR, Some(echoed), None), + }); + } + Some(match resolve(sandbox, &question.name) { + None => response(query, NAME_ERROR, Some(echoed), None), + Some(address) if matches!(question.record_type, TYPE_A | TYPE_ANY) => response( + query, + 0, + Some(echoed), + Some((TYPE_A, address.octets().to_vec())), + ), + // The name exists, but has no records of this type. + Some(_) => response(query, 0, Some(echoed), None), + }) +} + +fn parse_question(query: &[u8]) -> Option { + let mut position = 12; + let mut labels = Vec::new(); + loop { + let length = usize::from(*query.get(position)?); + position += 1; + if length == 0 { + break; + } + // Longer "labels" are compression pointers, which questions don't use. + if length > 63 { + return None; + } + let label = query.get(position..position + length)?; + position += length; + if label.contains(&b'.') { + return None; + } + labels.push(String::from_utf8_lossy(label).into_owned()); + } + let record_type = u16::from_be_bytes(query.get(position..position + 2)?.try_into().ok()?); + let class = u16::from_be_bytes(query.get(position + 2..position + 4)?.try_into().ok()?); + Some(Question { + name: normalize_domain(&labels.join(".")), + record_type, + class, + end: position + 4, + }) +} + +fn response( + query: &[u8], + code: u16, + question: Option<&[u8]>, + answer: Option<(u16, Vec)>, +) -> Vec { + let query_flags = u16::from_be_bytes([query[2], query[3]]); + // A response, with the query's opcode and "recursion desired", and + // recursion available. + let flags = 0x8000 | (query_flags & 0x7800) | (query_flags & 0x0100) | 0x0080 | code; + let mut message = query[0..2].to_vec(); + message.extend(flags.to_be_bytes()); + message.extend(u16::from(question.is_some()).to_be_bytes()); + message.extend(u16::from(answer.is_some()).to_be_bytes()); + message.extend([0; 4]); + if let Some(question) = question { + message.extend(question); + } + if let Some((record_type, data)) = answer { + // The name: a pointer to the question's, right after the header. + message.extend([0xc0, 12]); + message.extend(record_type.to_be_bytes()); + message.extend(CLASS_IN.to_be_bytes()); + message.extend(TTL.to_be_bytes()); + message.extend((data.len() as u16).to_be_bytes()); + message.extend(data); + } + message +} + +fn encode_name(name: &str) -> Vec { + let mut encoded = Vec::new(); + for label in name.split('.').filter(|label| !label.is_empty()) { + encoded.push(label.len() as u8); + encoded.extend(label.as_bytes()); + } + encoded.push(0); + encoded +} + +/// The fake address for `name`, if it should resolve. +fn resolve(sandbox: &SandboxState, name: &str) -> Option { + let exists = match sandbox.policy().name_verdict(name) { + NameVerdict::Deny => false, + NameVerdict::Ask => true, + NameVerdict::Allow => exists_on_host(sandbox, name), + }; + if !exists { + return None; + } + lock(&sandbox.names).address_for(name) +} + +fn exists_on_host(sandbox: &SandboxState, name: &str) -> bool { + if let Some((looked_up, exists)) = lock(&sandbox.names).host_lookups.get(name) + && looked_up.elapsed() < HOST_LOOKUP_CACHE + { + return *exists; + } + let exists = !name.is_empty() + && (name, 0) + .to_socket_addrs() + .is_ok_and(|mut addresses| addresses.next().is_some()); + let mut names = lock(&sandbox.names); + names + .host_lookups + .retain(|_, (looked_up, _)| looked_up.elapsed() < HOST_LOOKUP_CACHE); + names + .host_lookups + .insert(name.to_string(), (Instant::now(), exists)); + exists +} + +/// The name of a fake address, from its `in-addr.arpa` name. +fn reverse_lookup(sandbox: &SandboxState, name: &str) -> Option { + let reversed = name.strip_suffix(".in-addr.arpa")?; + let mut octets = [0u8; 4]; + let mut parts = reversed.split('.'); + for octet in octets.iter_mut().rev() { + *octet = parts.next()?.parse().ok()?; + } + if parts.next().is_some() { + return None; + } + lock(&sandbox.names).name_for(Ipv4Addr::from(octets)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn query(name: &str, record_type: u16) -> Vec { + let mut query = vec![0x12, 0x34, 0x01, 0x00, 0, 1, 0, 0, 0, 0, 0, 0]; + query.extend(encode_name(name)); + query.extend(record_type.to_be_bytes()); + query.extend(CLASS_IN.to_be_bytes()); + query + } + + #[test] + fn test_question_round_trip() { + let query = query("Example.COM", TYPE_A); + let question = parse_question(&query).unwrap(); + assert_eq!(question.name, "example.com"); + assert_eq!(question.record_type, TYPE_A); + assert_eq!(question.end, query.len()); + + let response = response( + &query, + 0, + Some(&query[12..question.end]), + Some((TYPE_A, vec![198, 18, 0, 1])), + ); + assert_eq!(&response[0..2], &[0x12, 0x34]); + assert_eq!(u16::from_be_bytes([response[2], response[3]]), 0x8180); + assert_eq!(&response[6..8], &[0, 1]); + assert_eq!(&response[response.len() - 4..], &[198, 18, 0, 1]); + assert!(is_fake(Ipv4Addr::new(198, 19, 255, 255))); + assert!(!is_fake(Ipv4Addr::new(198, 20, 0, 0))); + } +} diff --git a/crates/sandbox_linux/src/helper.rs b/crates/sandbox_linux/src/helper.rs new file mode 100644 index 00000000000..bd318b876db --- /dev/null +++ b/crates/sandbox_linux/src/helper.rs @@ -0,0 +1,324 @@ +//! The helper (`zed --zed-sandbox-linux-helper`), which becomes the parent of +//! the sandboxed command. +//! +//! Spawned by Zed with two inherited sockets: [`SETUP_FD`] carries the +//! [`HelperSetup`], and [`ATTACH_FD`] reaches the supervisor. It then: +//! +//! 1. creates user, mount, network and IPC namespaces. The mount namespace +//! is a copy of the host's, made read-only: the kernel-enforced backstop +//! for any write syscall the supervisor doesn't handle. Paths are the +//! same inside and out. In the network namespace every address routes +//! to loopback, and DNS sockets are created for the supervisor +//! ([`crate::net`]). +//! 2. becomes a child subreaper, so every process the command spawns stays +//! its descendant (that's how the supervisor tells them apart); +//! 3. forks the command, which drops all capabilities, installs the seccomp +//! filter, hands the listener (and DNS sockets) to the supervisor and +//! execs; +//! 4. serves the supervisor's requests for sockets in the namespace, while +//! it waits for the command; then kills whatever it left running, and exits with +//! its status. + +#![allow( + clippy::disallowed_methods, + reason = "the helper is a standalone process that must block on its command" +)] + +use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd, RawFd}; +use std::os::unix::process::{CommandExt as _, ExitStatusExt as _}; +use std::path::Path; +use std::process::Command; +use std::sync::atomic::{AtomicBool, Ordering}; + +use anyhow::{Context as _, Result}; + +use crate::protocol::{AttachInfo, HelperSetup, receive_message, send_message}; +use crate::sys::{self, cvt}; + +pub(crate) const HELPER_FLAG: &str = "--zed-sandbox-linux-helper"; +pub(crate) const SETUP_FD: RawFd = 3; +pub(crate) const ATTACH_FD: RawFd = 4; + +/// Exit status for failures before the command runs (as with `env`/`bwrap`). +const SETUP_FAILED: i32 = 126; + +pub(crate) fn run() -> ! { + match run_helper() { + Ok(code) => std::process::exit(code), + Err(error) => { + eprintln!("zed sandbox: {error:#}"); + std::process::exit(SETUP_FAILED); + } + } +} + +fn run_helper() -> Result { + sys::set_parent_death_signal()?; + // SAFETY: Zed spawns the helper with exactly these two descriptors. + let (setup_socket, attach_socket) = unsafe { + ( + sys::inherited_fd(SETUP_FD).context("missing setup socket")?, + sys::inherited_fd(ATTACH_FD).context("missing attach socket")?, + ) + }; + let (setup, _) = receive_message::(setup_socket.as_fd())? + .context("Zed closed the setup channel")?; + drop(setup_socket); + + // SAFETY: getuid/getgid can't fail. + let (uid, gid) = unsafe { (libc::getuid(), libc::getgid()) }; + let namespaces = + libc::CLONE_NEWUSER | libc::CLONE_NEWNS | libc::CLONE_NEWNET | libc::CLONE_NEWIPC; + // SAFETY: plain unshare; we're single-threaded. + if let Err(error) = cvt(unsafe { libc::unshare(namespaces) }) { + anyhow::bail!( + "creating sandbox namespaces failed: {error}. Unprivileged user namespaces may be \ + disabled, or (on Ubuntu 23.10+) restricted by AppArmor" + ); + } + // Keep our own uid/gid inside. Capabilities come from having created the + // user namespace, and the command drops them. + std::fs::write("/proc/self/setgroups", "deny").context("writing setgroups")?; + std::fs::write("/proc/self/uid_map", format!("{uid} {uid} 1")).context("writing uid_map")?; + std::fs::write("/proc/self/gid_map", format!("{gid} {gid} 1")).context("writing gid_map")?; + bring_up_loopback()?; + crate::net::configure_namespace()?; + let dns_sockets = crate::net::create_dns_sockets()?; + + // Nothing done here may propagate back to the host. + sys::mount( + None, + Path::new("/"), + None, + libc::MS_REC | libc::MS_PRIVATE, + None, + ) + .context("making mounts private")?; + sys::mount_setattr(Path::new("/"), sys::MOUNT_ATTR_RDONLY, true) + .context("making the filesystem read-only")?; + // SAFETY: plain prctl. + cvt(unsafe { libc::prctl(libc::PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) }) + .context("becoming a subreaper")?; + + install_terminate_handler()?; + let command = spawn_command(&setup, &attach_socket, dns_sockets)?; + start_factory(attach_socket)?; + let status = wait_for_command(command)?; + kill_remaining(); + Ok(status) +} + +static TERMINATE_REQUESTED: AtomicBool = AtomicBool::new(false); + +extern "C" fn on_terminate(_signal: libc::c_int) { + TERMINATE_REQUESTED.store(true, Ordering::SeqCst); +} + +/// `SIGTERM` stops the whole execution: the command and everything it +/// spawned (which `SIGKILL`ing the helper alone would orphan). The handler +/// is installed without `SA_RESTART`, so it interrupts `waitpid`. The +/// command gets default dispositions back when it execs. +fn install_terminate_handler() -> Result<()> { + // SAFETY: plain old data. + let mut action: libc::sigaction = unsafe { std::mem::zeroed() }; + action.sa_sigaction = on_terminate as extern "C" fn(libc::c_int) as libc::sighandler_t; + action.sa_flags = 0; + // SAFETY: valid pointers to a sigaction we own. + unsafe { + libc::sigemptyset(&mut action.sa_mask); + cvt(libc::sigaction( + libc::SIGTERM, + &action, + std::ptr::null_mut(), + )) + .context("installing the SIGTERM handler")?; + } + Ok(()) +} + +fn wait_for_command(command: libc::pid_t) -> Result { + loop { + if TERMINATE_REQUESTED.load(Ordering::SeqCst) { + return Ok(128 + libc::SIGTERM); + } + let mut status = 0; + // SAFETY: plain waitpid. + let pid = unsafe { libc::waitpid(-1, &mut status, 0) }; + if pid == command { + return Ok(exit_code(status)); + } + if pid < 0 { + let error = std::io::Error::last_os_error(); + if error.raw_os_error() != Some(libc::EINTR) { + return Err(error).context("waiting for the command"); + } + } + } +} + +/// Kill everything the command left behind. As a subreaper we inherit each +/// orphan, so repeat until there are no children left. +fn kill_remaining() { + // SAFETY: getpid can't fail. + let helper = unsafe { libc::getpid() }; + loop { + for process in crate::process::all_processes() { + if process.parent == helper { + // SAFETY: plain kill. + unsafe { libc::kill(process.pid, libc::SIGKILL) }; + } + } + let mut status = 0; + // SAFETY: plain waitpid. + let pid = unsafe { libc::waitpid(-1, &mut status, 0) }; + if pid < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ECHILD) { + return; + } + } +} + +fn exit_code(status: i32) -> i32 { + let status = std::process::ExitStatus::from_raw(status); + status + .code() + .unwrap_or_else(|| 128 + status.signal().unwrap_or(0)) +} + +fn bring_up_loopback() -> Result<()> { + // SAFETY: plain socket call. + let socket = + cvt(unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM | libc::SOCK_CLOEXEC, 0) })?; + // SAFETY: freshly created, owned here. + let socket = unsafe { OwnedFd::from_raw_fd(socket) }; + // SAFETY: `ifreq` is plain old data. + let mut request: libc::ifreq = unsafe { std::mem::zeroed() }; + for (slot, byte) in request.ifr_name.iter_mut().zip(b"lo") { + *slot = *byte as libc::c_char; + } + // SAFETY: SIOC[GS]IFFLAGS with a valid `ifreq`. + unsafe { + cvt(libc::ioctl( + socket.as_raw_fd(), + libc::SIOCGIFFLAGS, + &mut request, + ))?; + request.ifr_ifru.ifru_flags |= libc::IFF_UP as libc::c_short; + cvt(libc::ioctl( + socket.as_raw_fd(), + libc::SIOCSIFFLAGS, + &request, + )) + .context("bringing up loopback")?; + } + Ok(()) +} + +/// Serve the supervisor's requests for sockets in our namespace, on a thread +/// that leaves `SIGTERM` to the main one (where it interrupts `waitpid`). +fn start_factory(attach_socket: OwnedFd) -> Result<()> { + // SAFETY: plain old data, initialized by sigemptyset. + let mut terminate: libc::sigset_t = unsafe { std::mem::zeroed() }; + // SAFETY: valid signal set pointers. + unsafe { + libc::sigemptyset(&mut terminate); + libc::sigaddset(&mut terminate, libc::SIGTERM); + libc::pthread_sigmask(libc::SIG_BLOCK, &terminate, std::ptr::null_mut()); + } + // The new thread inherits the blocked mask. + let spawned = std::thread::Builder::new() + .name("zed-sandbox-factory".to_string()) + .spawn(move || crate::net::serve_factory(attach_socket)); + // SAFETY: as above. + unsafe { libc::pthread_sigmask(libc::SIG_UNBLOCK, &terminate, std::ptr::null_mut()) }; + spawned.context("starting the socket factory")?; + Ok(()) +} + +fn spawn_command( + setup: &HelperSetup, + attach_socket: &OwnedFd, + dns_sockets: Vec, +) -> Result { + let filter = crate::seccomp::build_filter(); + let mut command = Command::new(&setup.command.cmd); + command + .args(&setup.command.args) + .env_clear() + .envs(&setup.command.env) + .current_dir(&setup.command.cwd); + let attach = attach_socket.as_raw_fd(); + let dns: Vec = dns_sockets + .iter() + .map(|socket| socket.as_raw_fd()) + .collect(); + // SAFETY: runs in the forked child before exec. The helper is + // single-threaded, so no lock can be held across the fork. + unsafe { + command.pre_exec(move || { + sys::set_parent_death_signal()?; + drop_capabilities()?; + let helper_pid = libc::getppid(); + let listener = crate::seccomp::install(&filter)?; + // From here on, opens go to the supervisor, which can't answer + // until it has the listener: send it before anything else. + // SAFETY: `attach` stays open in the child until exec (CLOEXEC). + let attach = BorrowedFd::borrow_raw(attach); + let mut fds = vec![listener.as_raw_fd()]; + fds.extend(&dns); + send_message(attach, &AttachInfo { helper_pid }, &fds) + .map_err(std::io::Error::other)?; + Ok(()) + }); + } + let child = command + .spawn() + .with_context(|| format!("running {}", setup.command.cmd))?; + // The supervisor has them now (they were close-on-exec in the command). + drop(dns_sockets); + Ok(child.id() as libc::pid_t) +} + +/// Clear every capability set. `exec` as a non-root uid already does this; +/// being explicit also covers a user running Zed as root. +fn drop_capabilities() -> std::io::Result<()> { + #[repr(C)] + struct CapabilityHeader { + version: u32, + pid: i32, + } + #[repr(C)] + #[derive(Clone, Copy)] + struct CapabilityData { + effective: u32, + permitted: u32, + inheritable: u32, + } + const LINUX_CAPABILITY_VERSION_3: u32 = 0x2008_0522; + + for capability in 0..64 { + // SAFETY: plain prctl; EINVAL past the last capability is expected. + unsafe { libc::prctl(libc::PR_CAPBSET_DROP, capability, 0, 0, 0) }; + } + // SAFETY: plain prctl. + cvt(unsafe { + libc::prctl( + libc::PR_CAP_AMBIENT, + libc::PR_CAP_AMBIENT_CLEAR_ALL, + 0, + 0, + 0, + ) + })?; + let header = CapabilityHeader { + version: LINUX_CAPABILITY_VERSION_3, + pid: 0, + }; + let data = [CapabilityData { + effective: 0, + permitted: 0, + inheritable: 0, + }; 2]; + // SAFETY: capset with a v3 header and two data words. + cvt(unsafe { libc::syscall(libc::SYS_capset, &header, data.as_ptr()) as i32 })?; + Ok(()) +} diff --git a/crates/sandbox_linux/src/net.rs b/crates/sandbox_linux/src/net.rs new file mode 100644 index 00000000000..5f32ee47282 --- /dev/null +++ b/crates/sandbox_linux/src/net.rs @@ -0,0 +1,1370 @@ +//! Network access from the sandbox. +//! +//! Each execution has its own network namespace in which every address is +//! local (`local` routes for `0.0.0.0/0` and `::/0` on loopback) and none +//! leads out. A packet to any address arrives back in the namespace, where +//! the supervisor picks it up: +//! +//! - TCP: `connect` is trapped. For an allowed destination the helper +//! listens at exactly the address the command asked for, and the +//! supervisor connects the command's socket to that listener and relays +//! to a host connection. Blocking sockets wait for the host first; +//! non-blocking sockets connect locally first to avoid stalling the +//! command's event loop, and are reset if the host connection fails. +//! - UDP: the supervisor owns a wildcard socket in the namespace for each +//! port a rule might allow, and relays each flow (source, destination) +//! through a host socket of its own once the policy allows it. Ports are +//! set up when the execution starts, when the policy changes, and when a +//! UDP socket is connected; datagrams sent with `sendto` to any other +//! port go nowhere. +//! - DNS: wildcard sockets on port 53 answer every query ([`crate::dns`]). +//! Names map to fake addresses, so a connection says which name it's for. +//! +//! Sockets in the namespace can only be created from inside it, so the +//! helper keeps serving [`FactoryRequest`]s on its attach socket. + +use std::collections::{HashMap, HashSet}; +use std::io::{self, IoSlice, IoSliceMut}; +use std::net::{ + IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6, TcpListener, TcpStream, + ToSocketAddrs as _, UdpSocket, +}; +use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, OwnedFd, RawFd}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, MutexGuard}; +use std::time::{Duration, Instant}; + +use anyhow::Context as _; +use nix::sys::socket::{ + ControlMessage, ControlMessageOwned, MsgFlags, SockaddrLike as _, SockaddrStorage, recvmsg, + sendmsg, +}; +use serde::{Deserialize, Serialize}; + +use crate::dns; +use crate::policy::{AddressClass, NetHost, Protocol, RequestedAccess}; +use crate::process::ProcessTree; +use crate::protocol::{FactoryRequest, FactoryResponse, receive_message, send_message}; +use crate::supervisor::{Execution, SandboxState, Shared}; +use crate::sys::{self, cvt, errno}; + +pub(crate) const DNS_PORT: u16 = 53; + +const UPSTREAM_CONNECT_TIMEOUT: Duration = Duration::from_secs(30); +/// Connecting the command's socket to a listener in its own namespace. +const BRIDGE_TIMEOUT: Duration = Duration::from_secs(5); +const FLOW_IDLE_TIMEOUT: Duration = Duration::from_secs(60); +/// How long a denied UDP flow's datagrams are dropped before the policy is +/// checked (and maybe asked) again. +const DENIED_FLOW_TIMEOUT: Duration = Duration::from_secs(5); +const MAX_QUEUED_DATAGRAMS: usize = 16; +const MAX_DATAGRAM_SIZE: usize = 65536; +/// `IPV6_FREEBIND` (Linux 4.15), missing from `libc`. +const IPV6_FREEBIND: i32 = 78; + +pub(crate) fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + match mutex.lock() { + Ok(guard) => guard, + Err(poisoned) => poisoned.into_inner(), + } +} + +fn spawn_thread(name: &str, body: impl FnOnce() + Send + 'static) { + if let Err(error) = std::thread::Builder::new() + .name(name.to_string()) + .spawn(body) + { + eprintln!("zed sandbox supervisor: starting {name}: {error}"); + } +} + +// Socket addresses as the kernel lays them out. + +pub(crate) fn encode_address(address: SocketAddr) -> Vec { + match address { + SocketAddr::V4(address) => { + let mut bytes = vec![0; size_of::()]; + bytes[0..2].copy_from_slice(&(libc::AF_INET as u16).to_ne_bytes()); + bytes[2..4].copy_from_slice(&address.port().to_be_bytes()); + bytes[4..8].copy_from_slice(&address.ip().octets()); + bytes + } + SocketAddr::V6(address) => { + let mut bytes = vec![0; size_of::()]; + bytes[0..2].copy_from_slice(&(libc::AF_INET6 as u16).to_ne_bytes()); + bytes[2..4].copy_from_slice(&address.port().to_be_bytes()); + bytes[8..24].copy_from_slice(&address.ip().octets()); + bytes[24..28].copy_from_slice(&address.scope_id().to_ne_bytes()); + bytes + } + } +} + +pub(crate) fn decode_address(bytes: &[u8]) -> Option { + let family = u16::from_ne_bytes(bytes.get(0..2)?.try_into().ok()?); + let port = u16::from_be_bytes(bytes.get(2..4)?.try_into().ok()?); + match i32::from(family) { + libc::AF_INET => { + let octets: [u8; 4] = bytes.get(4..8)?.try_into().ok()?; + Some(SocketAddr::from((octets, port))) + } + libc::AF_INET6 => { + let octets: [u8; 16] = bytes.get(8..24)?.try_into().ok()?; + let scope = bytes + .get(24..28) + .and_then(|scope| scope.try_into().ok()) + .map_or(0, u32::from_ne_bytes); + Some(SocketAddr::V6(SocketAddrV6::new( + octets.into(), + port, + 0, + scope, + ))) + } + _ => None, + } +} + +fn socket_name(fd: BorrowedFd<'_>, peer: bool) -> io::Result { + // SAFETY: plain old data, filled in by the kernel. + let mut storage: libc::sockaddr_storage = unsafe { std::mem::zeroed() }; + let mut length = size_of::() as libc::socklen_t; + let pointer = (&mut storage as *mut libc::sockaddr_storage).cast(); + // SAFETY: a sockaddr_storage-sized buffer with its length. + cvt(unsafe { + if peer { + libc::getpeername(fd.as_raw_fd(), pointer, &mut length) + } else { + libc::getsockname(fd.as_raw_fd(), pointer, &mut length) + } + })?; + // SAFETY: the kernel initialized the first `length` bytes. + let bytes = unsafe { + std::slice::from_raw_parts( + (&storage as *const libc::sockaddr_storage).cast::(), + (length as usize).min(size_of::()), + ) + }; + decode_address(bytes).ok_or_else(|| errno(libc::EAFNOSUPPORT)) +} + +fn connect_raw(fd: BorrowedFd<'_>, address: SocketAddr) -> io::Result<()> { + let bytes = encode_address(address); + // SAFETY: a correctly laid out sockaddr with its length. + cvt(unsafe { + libc::connect( + fd.as_raw_fd(), + bytes.as_ptr().cast(), + bytes.len() as libc::socklen_t, + ) + })?; + Ok(()) +} + +/// `connect` that may complete asynchronously (non-blocking sockets). +fn connect_and_wait( + fd: BorrowedFd<'_>, + address: SocketAddr, + timeout: Duration, + keep_waiting: &dyn Fn() -> bool, +) -> io::Result<()> { + match connect_raw(fd, address) { + Ok(()) => return Ok(()), + Err(error) if error.raw_os_error() == Some(libc::EINPROGRESS) => {} + Err(error) => return Err(error), + } + sys::wait_for(fd, libc::POLLOUT, timeout, keep_waiting)?; + match sys::socket_option(fd, libc::SOL_SOCKET, libc::SO_ERROR)? { + 0 => Ok(()), + error => Err(errno(error)), + } +} + +fn canonical(address: SocketAddr) -> SocketAddr { + SocketAddr::new(address.ip().to_canonical(), address.port()) +} + +// Creating sockets in the sandbox's namespace (the helper's side). + +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +pub(crate) enum SocketKind { + TcpListener, + /// Reports each datagram's destination address. + Udp, + /// A `NETLINK_SOCK_DIAG` socket, for listing the namespace's sockets. + /// Not bound: the address is ignored. + Diagnostics, +} + +/// A socket bound to `address`, which needn't belong to any interface. +pub(crate) fn create_socket(kind: SocketKind, address: SocketAddr) -> io::Result { + if let SocketKind::Diagnostics = kind { + return sys::socket(libc::AF_NETLINK, libc::SOCK_DGRAM, libc::NETLINK_SOCK_DIAG); + } + let (domain, ipv6) = match address { + SocketAddr::V4(_) => (libc::AF_INET, false), + SocketAddr::V6(_) => (libc::AF_INET6, true), + }; + let socket_type = match kind { + SocketKind::TcpListener => libc::SOCK_STREAM, + SocketKind::Udp | SocketKind::Diagnostics => libc::SOCK_DGRAM, + }; + let socket = sys::socket(domain, socket_type, 0)?; + let fd = socket.as_fd(); + sys::set_socket_option(fd, libc::SOL_SOCKET, libc::SO_REUSEADDR, 1)?; + if ipv6 { + sys::set_socket_option(fd, libc::IPPROTO_IPV6, libc::IPV6_V6ONLY, 1)?; + // Unlike IPv4, IPv6 only binds (and sends from) addresses an + // interface has, local route or not. + sys::set_socket_option(fd, libc::IPPROTO_IPV6, IPV6_FREEBIND, 1)?; + } + if let SocketKind::Udp = kind { + if ipv6 { + sys::set_socket_option(fd, libc::IPPROTO_IPV6, libc::IPV6_RECVPKTINFO, 1)?; + } else { + sys::set_socket_option(fd, libc::IPPROTO_IP, libc::IP_PKTINFO, 1)?; + } + } + let bytes = encode_address(address); + // SAFETY: a correctly laid out sockaddr with its length. + cvt(unsafe { + libc::bind( + fd.as_raw_fd(), + bytes.as_ptr().cast(), + bytes.len() as libc::socklen_t, + ) + })?; + if let SocketKind::TcpListener = kind { + // SAFETY: plain listen. + cvt(unsafe { libc::listen(fd.as_raw_fd(), 128) })?; + } + Ok(socket) +} + +/// Route every address to loopback. Called by the helper in its new network +/// namespace. (Ping would now get replies from every address, so the seccomp +/// filter refuses ping sockets.) +pub(crate) fn configure_namespace() -> anyhow::Result<()> { + add_local_route(libc::AF_INET).context("routing IPv4 addresses to loopback")?; + if ipv6_available() { + add_local_route(libc::AF_INET6).context("routing IPv6 addresses to loopback")?; + } + Ok(()) +} + +/// The DNS sockets for a new namespace: UDP and TCP on port 53 of every +/// address. +pub(crate) fn create_dns_sockets() -> anyhow::Result> { + let mut addresses = vec![SocketAddr::from((Ipv4Addr::UNSPECIFIED, DNS_PORT))]; + if ipv6_available() { + addresses.push(SocketAddr::from((Ipv6Addr::UNSPECIFIED, DNS_PORT))); + } + let mut sockets = Vec::new(); + for kind in [SocketKind::Udp, SocketKind::TcpListener] { + for &address in &addresses { + sockets.push( + create_socket(kind, address) + .with_context(|| format!("creating the DNS socket {address}"))?, + ); + } + } + Ok(sockets) +} + +fn ipv6_available() -> bool { + std::path::Path::new("/proc/net/if_inet6").exists() +} + +/// `ip route add local dev lo table local`. +fn add_local_route(family: i32) -> io::Result<()> { + let socket = sys::socket(libc::AF_NETLINK, libc::SOCK_RAW, libc::NETLINK_ROUTE)?; + // SAFETY: valid NUL-terminated name. + let loopback = unsafe { libc::if_nametoindex(c"lo".as_ptr()) }; + if loopback == 0 { + return Err(io::Error::last_os_error()); + } + const HEADER: usize = 16; + const ROUTE: usize = 12; + const ATTRIBUTE: usize = 8; + let mut message = Vec::with_capacity(HEADER + ROUTE + ATTRIBUTE); + // struct nlmsghdr + message.extend(((HEADER + ROUTE + ATTRIBUTE) as u32).to_ne_bytes()); + message.extend(libc::RTM_NEWROUTE.to_ne_bytes()); + let flags = libc::NLM_F_REQUEST | libc::NLM_F_ACK | libc::NLM_F_CREATE | libc::NLM_F_EXCL; + message.extend((flags as u16).to_ne_bytes()); + message.extend(1u32.to_ne_bytes()); + message.extend(0u32.to_ne_bytes()); + // struct rtmsg: no destination prefix bits, i.e. every address. + message.extend([ + family as u8, + 0, + 0, + 0, + libc::RT_TABLE_LOCAL, + libc::RTPROT_BOOT, + libc::RT_SCOPE_HOST, + libc::RTN_LOCAL, + ]); + message.extend(0u32.to_ne_bytes()); + // struct rtattr: RTA_OIF + message.extend((ATTRIBUTE as u16).to_ne_bytes()); + message.extend(libc::RTA_OIF.to_ne_bytes()); + message.extend(loopback.to_ne_bytes()); + + // SAFETY: sending a buffer we own; netlink addresses the kernel by + // default. + let sent = unsafe { + libc::send( + socket.as_raw_fd(), + message.as_ptr().cast(), + message.len(), + 0, + ) + }; + if sent < 0 { + return Err(io::Error::last_os_error()); + } + let mut reply = [0u8; 1024]; + // SAFETY: receiving into a buffer we own. + let received = unsafe { + libc::recv( + socket.as_raw_fd(), + reply.as_mut_ptr().cast(), + reply.len(), + 0, + ) + }; + if received < 0 { + return Err(io::Error::last_os_error()); + } + // An NLMSG_ERROR acknowledgement: the header, then a negated errno. + let message_type = reply + .get(4..6) + .and_then(|bytes| bytes.try_into().ok()) + .map(u16::from_ne_bytes); + let error = reply + .get(HEADER..HEADER + 4) + .and_then(|bytes| bytes.try_into().ok()) + .map(i32::from_ne_bytes); + match (message_type, error) { + (Some(message_type), Some(0)) if i32::from(message_type) == libc::NLMSG_ERROR => Ok(()), + (Some(message_type), Some(error)) if i32::from(message_type) == libc::NLMSG_ERROR => { + Err(errno(-error)) + } + _ => Err(errno(libc::EPROTO)), + } +} + +/// Serve the supervisor's [`FactoryRequest`]s until it hangs up (the +/// helper's side). +pub(crate) fn serve_factory(socket: OwnedFd) { + loop { + let request = match receive_message::(socket.as_fd()) { + Ok(Some((request, _))) => request, + Ok(None) => return, + Err(error) => { + eprintln!("zed sandbox: {error:#}"); + return; + } + }; + let (response, fd) = match request { + FactoryRequest::CreateSocket { kind, address } => match create_socket(kind, address) { + Ok(created) => (FactoryResponse::Created, Some(created)), + Err(error) => ( + FactoryResponse::Failed { + errno: error.raw_os_error().unwrap_or(libc::EIO), + }, + None, + ), + }, + FactoryRequest::FetchFd { pid, fd } => match sys::pidfd_getfd(pid, fd) { + Ok(fetched) => (FactoryResponse::Fetched, Some(fetched)), + Err(error) => ( + FactoryResponse::Failed { + errno: error.raw_os_error().unwrap_or(libc::EIO), + }, + None, + ), + }, + }; + let sent = match fd { + Some(fd) => send_message(socket.as_fd(), &response, &[fd.as_raw_fd()]), + None => send_message(socket.as_fd(), &response, &[]), + }; + if let Err(error) = sent { + eprintln!("zed sandbox: {error:#}"); + return; + } + } +} + +// The supervisor's side. + +/// One execution's network: its namespace's factory, and the relays. +pub(crate) struct Network { + factory: Mutex, + /// Held while connecting a command's socket to a temporary listener (and + /// while checking for the command's own listeners), so no two bridges + /// see each other's listeners. + bridge: Mutex<()>, + /// Sockets threads block on, shut down when the execution ends. + sockets: Mutex>>, + udp_ports: Mutex>, + flows: Mutex>>, + /// A socket-diagnostics socket in the namespace, once created. + diagnostics: Mutex>, + closed: AtomicBool, +} + +/// A socket in the sandbox's namespace, as socket diagnostics report it. +struct DiagnosedSocket { + local: SocketAddr, + inode: u64, +} + +impl Network { + pub fn new(factory: OwnedFd) -> Self { + Self { + factory: Mutex::new(factory), + diagnostics: Mutex::new(None), + bridge: Mutex::new(()), + sockets: Mutex::default(), + udp_ports: Mutex::default(), + flows: Mutex::default(), + closed: AtomicBool::new(false), + } + } + + pub fn is_closed(&self) -> bool { + self.closed.load(Ordering::SeqCst) + } + + /// The execution has ended: stop serving its namespace. (Its sockets + /// would otherwise keep the namespace, and their threads, alive.) + pub fn close(&self) { + self.closed.store(true, Ordering::SeqCst); + for socket in lock(&self.sockets).drain(..) { + // SAFETY: shutdown on a socket we own; wakes its readers. + unsafe { libc::shutdown(socket.as_raw_fd(), libc::SHUT_RDWR) }; + } + } + + fn watch(&self, socket: OwnedFd) -> Arc { + let socket = Arc::new(socket); + let mut sockets = lock(&self.sockets); + if self.is_closed() { + // SAFETY: as in `close`. + unsafe { libc::shutdown(socket.as_raw_fd(), libc::SHUT_RDWR) }; + } else { + sockets.push(socket.clone()); + } + socket + } + + /// The namespace's IPv4 and IPv6 sockets of `protocol` in any of + /// `states` (a bit per TCP state), from `NETLINK_SOCK_DIAG`. Unlike + /// `/proc/net/tcp`, which walks the whole host's connection table, a + /// listening-state query only walks listeners. + fn diagnose(&self, protocol: u8, states: u32) -> io::Result> { + const SOCK_DIAG_BY_FAMILY: u16 = 20; + const HEADER: usize = 16; + let mut diagnostics = lock(&self.diagnostics); + if diagnostics.is_none() { + *diagnostics = Some(self.create( + SocketKind::Diagnostics, + SocketAddr::from((Ipv4Addr::UNSPECIFIED, 0)), + )?); + } + let Some(socket) = diagnostics.as_ref() else { + return Err(errno(libc::ENETDOWN)); + }; + let mut sockets = Vec::new(); + for family in [libc::AF_INET as u8, libc::AF_INET6 as u8] { + // struct nlmsghdr, then struct inet_diag_req_v2 with a zeroed + // inet_diag_sockid: dump everything in `states`. + let mut request = Vec::with_capacity(HEADER + 56); + request.extend(((HEADER + 56) as u32).to_ne_bytes()); + request.extend(SOCK_DIAG_BY_FAMILY.to_ne_bytes()); + request.extend(((libc::NLM_F_REQUEST | libc::NLM_F_DUMP) as u16).to_ne_bytes()); + request.extend(1u32.to_ne_bytes()); + request.extend(0u32.to_ne_bytes()); + request.extend([family, protocol, 0, 0]); + request.extend(states.to_ne_bytes()); + request.extend([0u8; 48]); + // SAFETY: sending a buffer we own. + let sent = unsafe { + libc::send( + socket.as_raw_fd(), + request.as_ptr().cast(), + request.len(), + 0, + ) + }; + if sent < 0 { + return Err(io::Error::last_os_error()); + } + let mut buffer = vec![0u8; 32 * 1024]; + 'replies: loop { + // SAFETY: receiving into a buffer we own. + let received = unsafe { + libc::recv( + socket.as_raw_fd(), + buffer.as_mut_ptr().cast(), + buffer.len(), + 0, + ) + }; + if received < 0 { + return Err(io::Error::last_os_error()); + } + let mut messages = &buffer[..received as usize]; + while messages.len() >= HEADER { + let length = + u32::from_ne_bytes([messages[0], messages[1], messages[2], messages[3]]) + as usize; + let message_type = u16::from_ne_bytes([messages[4], messages[5]]); + if length < HEADER || length > messages.len() { + return Err(errno(libc::EPROTO)); + } + match i32::from(message_type) { + libc::NLMSG_DONE => break 'replies, + libc::NLMSG_ERROR => { + let code = messages + .get(HEADER..HEADER + 4) + .map_or(-libc::EPROTO, |code| { + i32::from_ne_bytes([code[0], code[1], code[2], code[3]]) + }); + return Err(errno(-code)); + } + _ => { + if let Some(socket) = parse_diagnosed(&messages[HEADER..length]) { + sockets.push(socket); + } + } + } + // Messages are 4-byte aligned. + messages = &messages[((length + 3) & !3).min(messages.len())..]; + } + } + } + Ok(sockets) + } + + fn create(&self, kind: SocketKind, address: SocketAddr) -> io::Result { + let factory = lock(&self.factory); + send_message( + factory.as_fd(), + &FactoryRequest::CreateSocket { kind, address }, + &[], + ) + .map_err(|_| errno(libc::ENETDOWN))?; + match receive_message::(factory.as_fd()) { + Ok(Some((FactoryResponse::Created, mut fds))) if fds.len() == 1 => Ok(fds.remove(0)), + Ok(Some((FactoryResponse::Failed { errno: code }, _))) => Err(errno(code)), + _ => Err(errno(libc::ENETDOWN)), + } + } + + /// A duplicate of process `pid`'s descriptor `fd`, fetched by the helper: + /// the supervisor may not fetch it itself (see [`FactoryRequest::FetchFd`]). + pub(crate) fn fetch_fd(&self, pid: i32, fd: RawFd) -> io::Result { + let factory = lock(&self.factory); + send_message(factory.as_fd(), &FactoryRequest::FetchFd { pid, fd }, &[]) + .map_err(|_| errno(libc::ENETDOWN))?; + match receive_message::(factory.as_fd()) { + Ok(Some((FactoryResponse::Fetched, mut fds))) if fds.len() == 1 => Ok(fds.remove(0)), + Ok(Some((FactoryResponse::Failed { errno: code }, _))) => Err(errno(code)), + _ => Err(errno(libc::ENETDOWN)), + } + } + + /// Connect the command's `socket` to a temporary listener at + /// `destination` (where it asked to connect) and return the accepted + /// end. + fn bridge(&self, socket: BorrowedFd<'_>, destination: SocketAddr) -> io::Result { + let target = canonical(destination); + let _bridge = lock(&self.bridge); + let listener = match self.create(SocketKind::TcpListener, target) { + // Something in the sandbox holds that exact address; the command + // will see another port as its peer's. + Err(error) if error.raw_os_error() == Some(libc::EADDRINUSE) => { + self.create(SocketKind::TcpListener, SocketAddr::new(target.ip(), 0))? + } + listener => listener?, + }; + let port = socket_name(listener.as_fd(), false)?.port(); + // In the command's own family (an IPv6 socket may have asked for an + // IPv4-mapped address). + let listener_address = SocketAddr::new(destination.ip(), port); + connect_and_wait(socket, listener_address, BRIDGE_TIMEOUT, &|| true)?; + let command_address = canonical(socket_name(socket, false)?); + + let listener = TcpListener::from(listener); + listener.set_nonblocking(true)?; + let deadline = Instant::now() + BRIDGE_TIMEOUT; + loop { + match listener.accept() { + Ok((stream, peer)) if canonical(peer) == command_address => { + stream.set_nonblocking(false)?; + return Ok(stream); + } + // Something else raced to the listener (e.g. a TCP Fast Open + // `sendto`, which isn't trapped). Turn it away. + Ok(_) => {} + Err(error) if error.kind() == io::ErrorKind::WouldBlock => { + if Instant::now() >= deadline { + return Err(errno(libc::ETIMEDOUT)); + } + match sys::wait_for(listener.as_fd(), libc::POLLIN, BRIDGE_TIMEOUT, &|| true) { + Ok(()) => {} + Err(error) if error.raw_os_error() == Some(libc::ETIMEDOUT) => {} + Err(error) => return Err(error), + } + } + Err(error) => return Err(error), + } + } + } +} + +/// Start serving a new execution's namespace: answer DNS on `dns_sockets` +/// and relay the UDP ports its policy names. +pub(crate) fn start(shared: &Arc, execution: &Arc, dns_sockets: Vec) { + for socket in dns_sockets { + let tcp = sys::socket_option(socket.as_fd(), libc::SOL_SOCKET, libc::SO_TYPE) + .is_ok_and(|kind| kind == libc::SOCK_STREAM); + let socket = execution.network.watch(socket); + let execution = execution.clone(); + spawn_thread("zed-sandbox-dns", move || { + if tcp { + dns::serve_tcp(&execution, &socket); + } else { + dns::serve_udp(&execution, &socket); + } + }); + } + ensure_udp_ports(shared, execution); +} + +/// Relay every UDP port the policy names explicitly. +pub(crate) fn ensure_udp_ports(shared: &Arc, execution: &Arc) { + for port in execution.sandbox.policy().udp_ports() { + ensure_udp_port(shared, execution, port); + } +} + +fn ensure_udp_port(shared: &Arc, execution: &Arc, port: u16) { + let network = &execution.network; + if port == DNS_PORT || !lock(&network.udp_ports).insert(port) { + return; + } + let mut addresses = vec![SocketAddr::from((Ipv4Addr::UNSPECIFIED, port))]; + if ipv6_available() { + addresses.push(SocketAddr::from((Ipv6Addr::UNSPECIFIED, port))); + } + for address in addresses { + match network.create(SocketKind::Udp, address) { + Ok(socket) => { + let socket = network.watch(socket); + let shared = shared.clone(); + let execution = execution.clone(); + spawn_thread("zed-sandbox-udp", move || { + serve_udp_port(&shared, &execution, &socket, port) + }); + } + Err(error) => { + eprintln!("zed sandbox supervisor: relaying UDP on {address}: {error}"); + } + } + } +} + +/// The host a connection to `address` in the sandbox is for. `None` for a +/// fake address no name was given. +fn destination_host(sandbox: &SandboxState, address: IpAddr) -> Option { + match address { + IpAddr::V4(address) if dns::is_fake(address) => { + lock(&sandbox.names).name_for(address).map(NetHost::Domain) + } + address => Some(NetHost::Ip(address)), + } +} + +/// The thread a decision is made for, and whether it still waits for one. +pub(crate) struct Caller<'a> { + pub tid: i32, + pub still_valid: &'a dyn Fn() -> bool, +} + +pub(crate) enum TcpOutcome { + /// Let the kernel connect: the destination is in the sandbox. + Continue, + /// Connected and relayed. + Relayed, +} + +/// A trapped `connect` of the TCP socket `socket` to `destination`. +pub(crate) fn connect_tcp( + shared: &Arc, + execution: &Arc, + socket: BorrowedFd<'_>, + destination: SocketAddr, + caller: &Caller<'_>, +) -> io::Result { + let target = canonical(destination); + // Served by the sandbox's own DNS listener. + if target.port() == DNS_PORT { + return Ok(TcpOutcome::Continue); + } + if socket_name(socket, true).is_ok() { + return Err(errno(libc::EISCONN)); + } + let network = &execution.network; + if AddressClass::of(target.ip()) == AddressClass::Loopback { + let _bridge = lock(&network.bridge); + if listening_in_sandbox(network, caller.tid, target) { + return Ok(TcpOutcome::Continue); + } + } + let sandbox = &execution.sandbox; + let host = destination_host(sandbox, target.ip()).ok_or_else(|| errno(libc::ECONNREFUSED))?; + let request = RequestedAccess::Net { + host: host.clone(), + port: target.port(), + protocol: Protocol::Tcp, + }; + if !shared.authorize(sandbox, &request, caller.tid, caller.still_valid) { + return Err(errno(libc::EACCES)); + } + + if is_nonblocking(socket)? { + // Holding the syscall until the host answers would freeze the + // caller's event loop for a round trip per connection. Connect it + // now and the host in the background instead; if that fails, its + // connection is reset (rather than `connect` failing). + let inside = network.bridge(socket, destination)?; + let shared = shared.clone(); + let execution = execution.clone(); + let tid = caller.tid; + std::thread::Builder::new() + .name("zed-sandbox-connect".to_string()) + .spawn(move || { + let network = &execution.network; + let still_valid = || !network.is_closed(); + let caller = Caller { + tid, + still_valid: &still_valid, + }; + match connect_host(&shared, &execution.sandbox, &host, target.port(), &caller) { + Ok(upstream) if still_valid() => relay(inside, upstream), + _ => reset(&inside), + } + })?; + return Ok(TcpOutcome::Relayed); + } + + let upstream = connect_host(shared, sandbox, &host, target.port(), caller)?; + let inside = network.bridge(socket, destination)?; + relay(inside, upstream); + Ok(TcpOutcome::Relayed) +} + +fn is_nonblocking(socket: BorrowedFd<'_>) -> io::Result { + // SAFETY: F_GETFL on a valid descriptor. Our duplicate shares the + // target's open file description, and so its flags. + let flags = cvt(unsafe { libc::fcntl(socket.as_raw_fd(), libc::F_GETFL) })?; + Ok(flags & libc::O_NONBLOCK != 0) +} + +/// Connect on the host to the first of `host`'s addresses that may be +/// reached and answers. +fn connect_host( + shared: &Shared, + sandbox: &SandboxState, + host: &NetHost, + port: u16, + caller: &Caller<'_>, +) -> io::Result { + let mut last_error = errno(libc::EHOSTUNREACH); + for address in upstream_addresses(host, port)? { + if !(caller.still_valid)() { + return Err(errno(libc::ECONNABORTED)); + } + if !may_reach(shared, sandbox, host, address, Protocol::Tcp, caller) { + last_error = errno(libc::EACCES); + continue; + } + if !(caller.still_valid)() { + return Err(errno(libc::ECONNABORTED)); + } + match connect_upstream(address, caller.still_valid) { + Ok(stream) => return Ok(stream), + Err(error) => last_error = error, + } + if !(caller.still_valid)() { + break; + } + } + Err(last_error) +} + +/// Close `stream` with a reset, as a refused connection would look once +/// established. +fn reset(stream: &TcpStream) { + let linger = libc::linger { + l_onoff: 1, + l_linger: 0, + }; + // SAFETY: a `linger` with its length. Closing with a zero linger time + // sends a reset. + let result = unsafe { + libc::setsockopt( + stream.as_raw_fd(), + libc::SOL_SOCKET, + libc::SO_LINGER, + (&linger as *const libc::linger).cast(), + size_of::() as libc::socklen_t, + ) + }; + if result < 0 { + // A plain close still ends the connection. + stream.shutdown(std::net::Shutdown::Both).ok(); + } +} + +/// A trapped `connect` of a UDP socket: the kernel does it, but datagrams +/// to that port need a relay socket first. +pub(crate) fn connect_udp( + shared: &Arc, + execution: &Arc, + destination: SocketAddr, +) { + let port = destination.port(); + if execution.sandbox.policy().udp_may_allow(port) { + ensure_udp_port(shared, execution, port); + } +} + +fn upstream_addresses(host: &NetHost, port: u16) -> io::Result> { + match host { + NetHost::Ip(address) => Ok(vec![SocketAddr::new(*address, port)]), + NetHost::Domain(name) => match (name.as_str(), port).to_socket_addrs() { + Ok(addresses) => Ok(addresses.collect()), + Err(_) => Err(errno(libc::EHOSTUNREACH)), + }, + } +} + +/// A name that resolves to a loopback or local-network address (e.g. by DNS +/// rebinding) only reaches it if the address is allowed too. +fn may_reach( + shared: &Shared, + sandbox: &SandboxState, + host: &NetHost, + address: SocketAddr, + protocol: Protocol, + caller: &Caller<'_>, +) -> bool { + if matches!(host, NetHost::Ip(_)) || AddressClass::of(address.ip()) == AddressClass::Public { + return true; + } + let request = RequestedAccess::Net { + host: NetHost::Ip(address.ip().to_canonical()), + port: address.port(), + protocol, + }; + shared.authorize(sandbox, &request, caller.tid, caller.still_valid) +} + +fn connect_upstream(address: SocketAddr, still_valid: &dyn Fn() -> bool) -> io::Result { + let domain = if address.is_ipv4() { + libc::AF_INET + } else { + libc::AF_INET6 + }; + let socket = sys::socket(domain, libc::SOCK_STREAM | libc::SOCK_NONBLOCK, 0)?; + connect_and_wait( + socket.as_fd(), + address, + UPSTREAM_CONNECT_TIMEOUT, + still_valid, + )?; + let stream = TcpStream::from(socket); + stream.set_nonblocking(false)?; + Ok(stream) +} + +/// `struct inet_diag_msg`: family, state, timer, retransmits, then +/// `inet_diag_sockid` (source port and address, in network order), ..., +/// and the inode at offset 68. +fn parse_diagnosed(message: &[u8]) -> Option { + let family = i32::from(*message.first()?); + let port = u16::from_be_bytes(message.get(4..6)?.try_into().ok()?); + let ip = match family { + libc::AF_INET => IpAddr::from(<[u8; 4]>::try_from(message.get(8..12)?).ok()?), + libc::AF_INET6 => IpAddr::from(<[u8; 16]>::try_from(message.get(8..24)?).ok()?), + _ => return None, + }; + let inode = u32::from_ne_bytes(message.get(68..72)?.try_into().ok()?); + Some(DiagnosedSocket { + local: SocketAddr::new(ip.to_canonical(), port), + inode: u64::from(inode), + }) +} + +/// Whether something in the sandbox listens where `target` (a loopback +/// address) points, so connecting there stays inside. +fn listening_in_sandbox(network: &Network, tid: i32, target: SocketAddr) -> bool { + const TCP_LISTEN: u32 = 10; + let listens_at = |local: SocketAddr| { + local.port() == target.port() + && (local.ip().is_unspecified() + || target.ip().is_unspecified() + || local.ip() == target.ip()) + }; + match network.diagnose(libc::IPPROTO_TCP as u8, 1 << TCP_LISTEN) { + Ok(listeners) => listeners.iter().any(|socket| listens_at(socket.local)), + // Socket diagnostics unavailable (no `inet_diag`): read `/proc`. + Err(_) => { + proc_sockets(tid, "tcp").any(|socket| socket.state == "0A" && listens_at(socket.local)) + } + } +} + +struct ProcSocket { + local: SocketAddr, + state: String, +} + +/// The sockets in `pid`'s network namespace from `/proc//net/` +/// and its IPv6 counterpart, with canonical addresses. +fn proc_sockets(pid: i32, table: &str) -> impl Iterator { + let contents: Vec = [table.to_string(), format!("{table}6")] + .iter() + .filter_map(|table| std::fs::read_to_string(format!("/proc/{pid}/net/{table}")).ok()) + .collect(); + contents.into_iter().flat_map(|contents| { + contents + .lines() + .skip(1) + .filter_map(parse_proc_socket) + .collect::>() + }) +} + +/// One line of `/proc/net/{tcp,udp}{,6}`: addresses are hex words in host +/// byte order. +fn parse_proc_socket(line: &str) -> Option { + let fields: Vec<&str> = line.split_whitespace().collect(); + let (address, port) = fields.get(1)?.split_once(':')?; + let port = u16::from_str_radix(port, 16).ok()?; + let mut octets = Vec::with_capacity(16); + for word in address.as_bytes().chunks(8) { + let word = u32::from_str_radix(std::str::from_utf8(word).ok()?, 16).ok()?; + octets.extend(word.to_ne_bytes()); + } + let ip = match octets.len() { + 4 => IpAddr::from(<[u8; 4]>::try_from(octets).ok()?), + 16 => IpAddr::from(<[u8; 16]>::try_from(octets).ok()?), + _ => return None, + }; + Some(ProcSocket { + local: SocketAddr::new(ip.to_canonical(), port), + state: fields.get(3)?.to_string(), + }) +} + +/// Copy both ways between the sandbox's end of a connection and the host's +/// until both sides have finished. +fn relay(inside: TcpStream, outside: TcpStream) { + let inside = Arc::new(inside); + let outside = Arc::new(outside); + let pump = |from: Arc, to: Arc| { + move || match splice_all(&from, &to) { + Ok(()) => { + // Pass the end of the stream on; the other direction may + // still be going. + to.shutdown(std::net::Shutdown::Write).ok(); + } + Err(_) => { + // A reset (or worse): end both directions. + from.shutdown(std::net::Shutdown::Both).ok(); + to.shutdown(std::net::Shutdown::Both).ok(); + } + } + }; + spawn_thread("zed-sandbox-relay", pump(inside.clone(), outside.clone())); + spawn_thread("zed-sandbox-relay", pump(outside, inside)); +} + +/// Move bytes from `from` to `to` through a pipe, without copying them +/// through userspace, until `from` ends. +fn splice_all(from: &TcpStream, to: &TcpStream) -> io::Result<()> { + let mut pipe = [0; 2]; + // SAFETY: pipe2 fills in two descriptors. + cvt(unsafe { libc::pipe2(pipe.as_mut_ptr(), libc::O_CLOEXEC) })?; + // SAFETY: fresh descriptors that nothing else owns. + let (pipe_read, pipe_write) = unsafe { + use std::os::fd::FromRawFd as _; + (OwnedFd::from_raw_fd(pipe[0]), OwnedFd::from_raw_fd(pipe[1])) + }; + let splice = |from: i32, to: i32, length: usize| -> io::Result { + loop { + // SAFETY: splice between descriptors we hold. + let moved = unsafe { + libc::splice( + from, + std::ptr::null_mut(), + to, + std::ptr::null_mut(), + length, + libc::SPLICE_F_MOVE, + ) + }; + if moved >= 0 { + return Ok(moved as usize); + } + let error = io::Error::last_os_error(); + if error.raw_os_error() != Some(libc::EINTR) { + return Err(error); + } + } + }; + loop { + let read = splice(from.as_raw_fd(), pipe_write.as_raw_fd(), 1 << 16)?; + if read == 0 { + return Ok(()); + } + let mut left = read; + while left > 0 { + let written = splice(pipe_read.as_raw_fd(), to.as_raw_fd(), left)?; + if written == 0 { + return Err(errno(libc::EPIPE)); + } + left -= written; + } + } +} + +// UDP. + +pub(crate) struct Datagram { + pub length: usize, + pub source: SocketAddr, + /// The address it was sent to (any address reaches a wildcard socket). + pub destination: IpAddr, +} + +/// Receive into `buffer` from a [`SocketKind::Udp`] socket. `None` for a +/// datagram without addresses (as after shutting the socket down). +pub(crate) fn receive_datagram( + socket: BorrowedFd<'_>, + buffer: &mut [u8], +) -> io::Result> { + let mut control = nix::cmsg_space!(libc::in6_pktinfo); + let mut iov = [IoSliceMut::new(buffer)]; + let message = recvmsg::( + socket.as_raw_fd(), + &mut iov, + Some(&mut control), + MsgFlags::MSG_CMSG_CLOEXEC, + )?; + let mut destination = None; + for control_message in message.cmsgs()? { + match control_message { + ControlMessageOwned::Ipv4PacketInfo(info) => { + // `s_addr` holds the address's bytes in network order. + destination = Some(IpAddr::from(info.ipi_addr.s_addr.to_ne_bytes())); + } + ControlMessageOwned::Ipv6PacketInfo(info) => { + destination = Some(IpAddr::from(info.ipi6_addr.s6_addr)); + } + _ => {} + } + } + let source = message.address.as_ref().and_then(storage_address); + Ok(match (source, destination) { + (Some(source), Some(destination)) => Some(Datagram { + length: message.bytes, + source, + destination, + }), + _ => None, + }) +} + +fn storage_address(storage: &SockaddrStorage) -> Option { + // SAFETY: `as_ptr` and `len` describe the storage's initialized sockaddr. + let bytes = unsafe { + std::slice::from_raw_parts(storage.as_ptr().cast::(), storage.len() as usize) + }; + decode_address(bytes) +} + +/// Send `payload` to `to` from address `from` (which no interface needs to +/// have: every address is local in the sandbox). +pub(crate) fn send_datagram( + socket: BorrowedFd<'_>, + payload: &[u8], + to: SocketAddr, + from: IpAddr, +) -> io::Result<()> { + let iov = [IoSlice::new(payload)]; + let to = SockaddrStorage::from(to); + let flags = MsgFlags::MSG_NOSIGNAL; + match from { + IpAddr::V4(from) => { + let info = libc::in_pktinfo { + ipi_ifindex: 0, + ipi_spec_dst: libc::in_addr { + s_addr: u32::from_ne_bytes(from.octets()), + }, + ipi_addr: libc::in_addr { s_addr: 0 }, + }; + let control = [ControlMessage::Ipv4PacketInfo(&info)]; + sendmsg(socket.as_raw_fd(), &iov, &control, flags, Some(&to))?; + } + IpAddr::V6(from) => { + let info = libc::in6_pktinfo { + ipi6_addr: libc::in6_addr { + s6_addr: from.octets(), + }, + ipi6_ifindex: 0, + }; + let control = [ControlMessage::Ipv6PacketInfo(&info)]; + sendmsg(socket.as_raw_fd(), &iov, &control, flags, Some(&to))?; + } + } + Ok(()) +} + +/// A UDP flow from a socket in the sandbox to one destination. +struct Flow { + state: Mutex, + last_active: Mutex, +} + +enum FlowState { + /// Being decided; datagrams wait. + Pending(Vec>), + Open(Arc), + Denied, +} + +impl Flow { + fn touch(&self) { + *lock(&self.last_active) = Instant::now(); + } + + fn idle(&self) -> Duration { + lock(&self.last_active).elapsed() + } +} + +fn serve_udp_port( + shared: &Arc, + execution: &Arc, + socket: &Arc, + port: u16, +) { + let network = &execution.network; + let mut buffer = vec![0; MAX_DATAGRAM_SIZE]; + while !network.is_closed() { + let datagram = match receive_datagram(socket.as_fd(), &mut buffer) { + Ok(Some(datagram)) => datagram, + Ok(None) => continue, + // A previous reply bounced (ICMP unreachable). + Err(error) + if matches!( + error.raw_os_error(), + Some(libc::EINTR | libc::ECONNREFUSED | libc::EHOSTUNREACH) + ) => + { + continue; + } + Err(error) => { + eprintln!("zed sandbox supervisor: receiving UDP on port {port}: {error}"); + return; + } + }; + let destination = SocketAddr::new(datagram.destination, port); + let payload = &buffer[..datagram.length]; + relay_datagram( + shared, + execution, + socket, + datagram.source, + destination, + payload, + ); + } +} + +fn relay_datagram( + shared: &Arc, + execution: &Arc, + socket: &Arc, + source: SocketAddr, + destination: SocketAddr, + payload: &[u8], +) { + let key = (source, destination); + let flow = { + let mut flows = lock(&execution.network.flows); + match flows.get(&key) { + Some(flow) => flow.clone(), + None => { + let flow = Arc::new(Flow { + state: Mutex::new(FlowState::Pending(Vec::new())), + last_active: Mutex::new(Instant::now()), + }); + flows.insert(key, flow.clone()); + let shared = shared.clone(); + let execution = execution.clone(); + let socket = socket.clone(); + let decided = flow.clone(); + spawn_thread("zed-sandbox-udp-flow", move || { + run_flow(&shared, &execution, &socket, key, &decided) + }); + flow + } + } + }; + flow.touch(); + match &mut *lock(&flow.state) { + FlowState::Pending(queue) => { + if queue.len() < MAX_QUEUED_DATAGRAMS { + queue.push(payload.to_vec()); + } + } + FlowState::Open(upstream) => { + // UDP is lossy: a failed send is a lost datagram. + upstream.send(payload).ok(); + } + FlowState::Denied => {} + } +} + +/// Decide a new flow, then relay its replies until it goes idle. +fn run_flow( + shared: &Shared, + execution: &Execution, + socket: &OwnedFd, + key: (SocketAddr, SocketAddr), + flow: &Arc, +) { + let network = &execution.network; + let (source, destination) = key; + match open_flow(shared, execution, source, destination) { + Ok(upstream) => { + let upstream = Arc::new(upstream); + let queued = + match std::mem::replace(&mut *lock(&flow.state), FlowState::Open(upstream.clone())) + { + FlowState::Pending(queued) => queued, + _ => Vec::new(), + }; + for payload in queued { + upstream.send(&payload).ok(); + } + let mut buffer = vec![0; MAX_DATAGRAM_SIZE]; + while !network.is_closed() && flow.idle() < FLOW_IDLE_TIMEOUT { + match upstream.recv(&mut buffer) { + Ok(length) => { + flow.touch(); + // Replies come from where the command sent to. + send_datagram(socket.as_fd(), &buffer[..length], source, destination.ip()) + .ok(); + } + // Timeouts, and ICMP errors from the far end. + Err(_) => {} + } + } + } + Err(_) => { + *lock(&flow.state) = FlowState::Denied; + let denied = Instant::now(); + while !network.is_closed() && denied.elapsed() < DENIED_FLOW_TIMEOUT { + std::thread::sleep(Duration::from_millis(250)); + } + } + } + let mut flows = lock(&network.flows); + if flows + .get(&key) + .is_some_and(|current| Arc::ptr_eq(current, flow)) + { + flows.remove(&key); + } +} + +fn open_flow( + shared: &Shared, + execution: &Execution, + source: SocketAddr, + destination: SocketAddr, +) -> io::Result { + let network = &execution.network; + let sandbox = &execution.sandbox; + let target = canonical(destination); + let host = destination_host(sandbox, target.ip()).ok_or_else(|| errno(libc::ECONNREFUSED))?; + let still_valid = || !network.is_closed(); + let caller = Caller { + tid: udp_sender(network, &execution.tree, source), + still_valid: &still_valid, + }; + let request = RequestedAccess::Net { + host: host.clone(), + port: target.port(), + protocol: Protocol::Udp, + }; + if !shared.authorize(sandbox, &request, caller.tid, caller.still_valid) { + return Err(errno(libc::EACCES)); + } + let address = upstream_addresses(&host, target.port())? + .into_iter() + .find(|address| may_reach(shared, sandbox, &host, *address, Protocol::Udp, &caller)) + .ok_or_else(|| errno(libc::EACCES))?; + let local: SocketAddr = if address.is_ipv4() { + (Ipv4Addr::UNSPECIFIED, 0).into() + } else { + (Ipv6Addr::UNSPECIFIED, 0).into() + }; + let upstream = UdpSocket::bind(local)?; + upstream.connect(address)?; + upstream.set_read_timeout(Some(Duration::from_secs(1)))?; + Ok(upstream) +} + +/// The process that owns the sandbox socket bound to `source`, for prompts +/// and denial reports; the helper if it can't be found. +fn udp_sender(network: &Network, tree: &ProcessTree, source: SocketAddr) -> i32 { + let source = canonical(source); + let inode = network + .diagnose(libc::IPPROTO_UDP as u8, u32::MAX) + .ok() + .and_then(|sockets| { + sockets + .into_iter() + .find(|socket| { + socket.local == source + || (socket.local.ip().is_unspecified() + && socket.local.port() == source.port()) + }) + .map(|socket| socket.inode) + }); + let holder = inode.and_then(|inode| { + let link = format!("socket:[{inode}]"); + tree.members().into_iter().find_map(|process| { + let entries = std::fs::read_dir(format!("/proc/{}/fd", process.pid)).ok()?; + entries + .flatten() + .any(|entry| { + std::fs::read_link(entry.path()) + .is_ok_and(|target| target.as_os_str() == link.as_str()) + }) + .then_some(process.pid) + }) + }); + holder.unwrap_or(tree.helper) +} diff --git a/crates/sandbox_linux/src/policy.rs b/crates/sandbox_linux/src/policy.rs new file mode 100644 index 00000000000..9027c0df4e7 --- /dev/null +++ b/crates/sandbox_linux/src/policy.rs @@ -0,0 +1,1496 @@ +//! Policy types. +//! +//! [`SandboxPolicy`] is what callers write: rules can carry [`Handler`]s, +//! which are closures in the caller's process. [`WirePolicy`] is what the +//! supervisor enforces: the same rules with each handler replaced by an id +//! the caller keeps a table of. + +use std::collections::BTreeSet; +use std::fmt; +use std::net::IpAddr; +use std::ops::RangeInclusive; +use std::os::unix::ffi::OsStrExt as _; +use std::path::{Component, Components, Path, PathBuf}; +use std::str::FromStr; +use std::sync::Arc; + +use anyhow::{Result, bail}; +use serde::{Deserialize, Serialize}; + +use crate::Responder; + +/// What a sandbox may do. +/// +/// Each resource has its own ordered rule list. Within a list, for each +/// permission, the last rule that matches and sets that permission decides; +/// a permission no matching rule sets is denied. Order between lists means +/// nothing. +#[derive(Clone, Debug, Default)] +pub struct SandboxPolicy { + /// Filesystem access, by host path. + /// + /// - Read means opening a file for its contents. Listing directories, + /// `stat`, `readlink` and `execve` are never restricted. + /// - Write means any modification: creating, deleting, renaming, + /// linking, opening for writing, `chmod`, `truncate`, timestamps, + /// binding a unix socket. + /// - Connect means connecting to a unix socket. Abstract sockets (which + /// have no path) are local to the execution and always allowed. + /// + /// Every permission matches paths the same way. + pub fs: Vec, + /// Outgoing network access, by destination: TCP connections and UDP + /// flows leaving the execution. Processes of one execution can always + /// reach each other over loopback. + /// + /// DNS queries are answered by the sandbox itself; names no rule could + /// allow don't resolve. + pub net: Vec, +} + +impl SandboxPolicy { + /// Every path readable, writable and connectable, every destination + /// reachable. + pub fn unrestricted() -> Self { + Self { + fs: vec![FsRule { + path: PathPattern::root(), + read: Some(Access::Allow), + write: Some(Access::Allow), + connect: Some(Access::Allow), + }], + net: vec![NetRule::new(NetTarget::Any, Access::Allow)], + } + } +} + +/// A rule of any resource kind, for handlers that add rules +/// ([`Decision::remember`]). +#[derive(Clone, Debug)] +pub enum Rule { + Fs(FsRule), + Net(NetRule), +} + +#[derive(Clone, Debug)] +pub struct FsRule { + pub path: PathPattern, + /// `None`: this rule has no opinion, and earlier rules still apply. + pub read: Option, + pub write: Option, + pub connect: Option, +} + +impl FsRule { + pub fn new(path: impl Into) -> Result { + Ok(Self { + path: PathPattern::new(path)?, + read: None, + write: None, + connect: None, + }) + } + + pub fn connect(mut self, access: Access) -> Self { + self.connect = Some(access); + self + } + + pub fn read(mut self, access: Access) -> Self { + self.read = Some(access); + self + } + + pub fn write(mut self, access: Access) -> Self { + self.write = Some(access); + self + } +} + +/// Whether connections to `target` on `ports` may be made. +#[derive(Clone, Debug)] +pub struct NetRule { + pub target: NetTarget, + pub ports: PortSet, + pub tcp: bool, + pub udp: bool, + pub connect: Access, +} + +impl NetRule { + /// A rule for every port, over both TCP and UDP. + pub fn new(target: NetTarget, connect: Access) -> Self { + Self { + target, + ports: PortSet::All, + tcp: true, + udp: true, + connect, + } + } + + pub fn ports(mut self, ports: PortSet) -> Self { + self.ports = ports; + self + } + + pub fn tcp_only(mut self) -> Self { + self.udp = false; + self + } + + pub fn udp_only(mut self) -> Self { + self.tcp = false; + self + } + + /// A rule from a destination written `[tcp/|udp/]TARGET[:PORTS]`, for + /// command lines and text fields: + /// - TARGET is `*` (anything), `loopback`, `local` (the local network), + /// an IP address or CIDR block (IPv6 in brackets when followed by + /// ports), or a domain (`*.example.com` for its subdomains); + /// - PORTS is `*` (the default) or a list like `80,443,8000-8999`; + /// - without a protocol prefix, the rule covers TCP and UDP. + pub fn parse(destination: &str, connect: Access) -> Result { + let destination = destination.trim(); + let (protocol, rest) = match destination.split_once('/') { + Some((protocol @ ("tcp" | "udp"), rest)) => (Some(protocol), rest), + _ => (None, destination), + }; + let (target, ports) = if let Some(bracketed) = rest.strip_prefix('[') { + let Some((target, after)) = bracketed.split_once(']') else { + bail!("`{destination}` has an unclosed `[`"); + }; + match after.strip_prefix(':') { + Some(ports) => (target, Some(ports)), + None if after.is_empty() => (target, None), + None => bail!("`{destination}`: expected `:` after `]`"), + } + } else if rest.matches(':').count() > 1 { + // A bare IPv6 address or block. + (rest, None) + } else { + match rest.split_once(':') { + Some((target, ports)) => (target, Some(ports)), + None => (rest, None), + } + }; + let target = match target { + "*" => NetTarget::Any, + "loopback" => NetTarget::Loopback, + "local" => NetTarget::LocalNetwork, + target => match target.parse() { + Ok(network) => NetTarget::Ip(network), + Err(_) => NetTarget::Domain(DomainPattern::new(target)?), + }, + }; + let ports = match ports { + None | Some("*") => PortSet::All, + Some(ports) => { + let mut ranges = Vec::new(); + for range in ports.split(',') { + let (start, end) = range.split_once('-').unwrap_or((range, range)); + let (Ok(start), Ok(end)) = (start.trim().parse(), end.trim().parse()) else { + bail!("`{destination}` has an invalid port `{range}`"); + }; + ranges.push(start..=end); + } + PortSet::Ranges(ranges) + } + }; + let rule = NetRule::new(target, connect).ports(ports); + Ok(match protocol { + Some("tcp") => rule.tcp_only(), + Some(_) => rule.udp_only(), + None => rule, + }) + } +} + +/// Where a connection goes. A connection to a name (resolved through the +/// sandbox's DNS) only matches [`NetTarget::Domain`] and [`NetTarget::Any`]; +/// a connection to a literal address matches the others. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum NetTarget { + Any, + Domain(DomainPattern), + Ip(IpNet), + /// Services on the host: `127.0.0.0/8`, `::1` and the unspecified + /// addresses. + Loopback, + /// Private, link-local, carrier-grade NAT and multicast addresses. + LocalNetwork, +} + +impl NetTarget { + fn matches(&self, host: &NetHost) -> bool { + match host { + NetHost::Domain(name) => self.matches_domain(name), + NetHost::Ip(address) => match self { + NetTarget::Any => true, + NetTarget::Domain(_) => false, + NetTarget::Ip(network) => network.contains(*address), + NetTarget::Loopback => AddressClass::of(*address) == AddressClass::Loopback, + NetTarget::LocalNetwork => AddressClass::of(*address) == AddressClass::LocalNetwork, + }, + } + } + + fn matches_domain(&self, name: &str) -> bool { + match self { + NetTarget::Any => true, + NetTarget::Domain(pattern) => pattern.matches(name), + _ => false, + } + } +} + +/// A host name (`example.com`), or every subdomain of one (`*.example.com`, +/// which doesn't match `example.com` itself). Case-insensitive. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] +pub struct DomainPattern { + /// Lowercase, without a trailing dot. + source: String, +} + +impl DomainPattern { + pub fn new(source: impl Into) -> Result { + let source = normalize_domain(&source.into()); + let name = source.strip_prefix("*.").unwrap_or(&source); + let valid_label = |label: &str| { + !label.is_empty() + && label + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_') + }; + if !name.split('.').all(valid_label) { + bail!("`{source}` is not a domain name or `*.` followed by one"); + } + Ok(Self { source }) + } + + pub fn as_str(&self) -> &str { + &self.source + } + + /// Whether `name` (normalized) matches. + fn matches(&self, name: &str) -> bool { + match self.source.strip_prefix("*.") { + Some(parent) => name + .strip_suffix(parent) + .is_some_and(|prefix| prefix.len() > 1 && prefix.ends_with('.')), + None => name == self.source, + } + } +} + +/// Lowercase, without a trailing dot: how names are compared. +pub(crate) fn normalize_domain(name: &str) -> String { + name.trim_end_matches('.').to_ascii_lowercase() +} + +impl TryFrom for DomainPattern { + type Error = anyhow::Error; + + fn try_from(source: String) -> Result { + Self::new(source) + } +} + +impl From for String { + fn from(pattern: DomainPattern) -> Self { + pattern.source + } +} + +/// A block of addresses in CIDR notation (`10.0.0.0/8`, `fd00::/8`), or a +/// single address. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] +pub struct IpNet { + address: IpAddr, + prefix: u8, +} + +impl IpNet { + pub fn new(address: IpAddr, prefix: u8) -> Result { + let address = address.to_canonical(); + let bits = if address.is_ipv4() { 32 } else { 128 }; + if prefix > bits { + bail!("prefix /{prefix} is too long for {address}"); + } + Ok(Self { + address: mask(address, prefix), + prefix, + }) + } + + pub fn contains(&self, address: IpAddr) -> bool { + let address = address.to_canonical(); + address.is_ipv4() == self.address.is_ipv4() && mask(address, self.prefix) == self.address + } +} + +fn mask(address: IpAddr, prefix: u8) -> IpAddr { + match address { + IpAddr::V4(address) => { + let mask = u32::MAX.checked_shl(32 - u32::from(prefix)).unwrap_or(0); + IpAddr::V4((u32::from(address) & mask).into()) + } + IpAddr::V6(address) => { + let mask = u128::MAX.checked_shl(128 - u32::from(prefix)).unwrap_or(0); + IpAddr::V6((u128::from(address) & mask).into()) + } + } +} + +impl FromStr for IpNet { + type Err = anyhow::Error; + + fn from_str(source: &str) -> Result { + let (address, prefix) = match source.split_once('/') { + Some((address, prefix)) => (address, Some(prefix)), + None => (source, None), + }; + let Ok(address) = address.parse::() else { + bail!("`{source}` is not an IP address or CIDR block"); + }; + let prefix = match prefix { + Some(prefix) => match prefix.parse() { + Ok(prefix) => prefix, + Err(_) => bail!("`{source}` has an invalid prefix length"), + }, + None if address.to_canonical().is_ipv4() => 32, + None => 128, + }; + Self::new(address, prefix) + } +} + +impl fmt::Display for IpNet { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "{}/{}", self.address, self.prefix) + } +} + +impl TryFrom for IpNet { + type Error = anyhow::Error; + + fn try_from(source: String) -> Result { + source.parse() + } +} + +impl From for String { + fn from(network: IpNet) -> Self { + network.to_string() + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum PortSet { + All, + Ranges(Vec>), +} + +impl PortSet { + pub fn single(port: u16) -> Self { + Self::Ranges(vec![port..=port]) + } + + pub fn contains(&self, port: u16) -> bool { + match self { + PortSet::All => true, + PortSet::Ranges(ranges) => ranges.iter().any(|range| range.contains(&port)), + } + } +} + +/// The kind of address a connection goes to, as [`NetTarget::Loopback`] +/// and [`NetTarget::LocalNetwork`] classify it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum AddressClass { + Loopback, + LocalNetwork, + Public, +} + +impl AddressClass { + pub fn of(address: IpAddr) -> Self { + match address.to_canonical() { + IpAddr::V4(address) => { + let carrier_grade_nat = + address.octets()[0] == 100 && address.octets()[1] & 0xc0 == 64; + if address.is_loopback() || address.is_unspecified() { + Self::Loopback + } else if address.is_private() + || address.is_link_local() + || carrier_grade_nat + || address.is_multicast() + || address.is_broadcast() + { + Self::LocalNetwork + } else { + Self::Public + } + } + IpAddr::V6(address) => { + let first = address.segments()[0]; + let unique_local = first & 0xfe00 == 0xfc00; + let link_local = first & 0xffc0 == 0xfe80; + if address.is_loopback() || address.is_unspecified() { + Self::Loopback + } else if unique_local || link_local || address.is_multicast() { + Self::LocalNetwork + } else { + Self::Public + } + } + } + } +} + +#[derive(Clone, Debug)] +pub enum Access { + Allow, + Deny, + /// Defer the decision to `Handler`, each time. The syscall blocks until + /// it responds. + Ask(Handler), +} + +/// Decides requests for access that a rule set to [`Access::Ask`]. +/// +/// Called on a background thread shared by every sandbox in the context, so +/// it must return promptly: answer through the [`Responder`], from any +/// thread, whenever the decision is made. A panicking handler denies. +/// +/// Requests to one handler in one sandbox are asked one at a time. While one +/// is open, an identical request shares its decision, and other requests +/// wait and are then re-checked against the policy (including any rules the +/// decision remembered) before being asked. +#[derive(Clone)] +pub struct Handler(pub(crate) Arc); + +impl Handler { + pub fn new(handler: impl Fn(AccessRequest, Responder) + Send + Sync + 'static) -> Self { + Self(Arc::new(handler)) + } +} + +impl fmt::Debug for Handler { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("Handler") + } +} + +/// What one blocked syscall needs. A single decision covers all of it. +#[derive(Clone, Debug)] +pub struct AccessRequest { + pub items: Vec, + /// The thread that made the syscall. + pub pid: i32, + pub executable: Option, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum RequestedAccess { + Fs { + permission: Permission, + path: PathBuf, + }, + /// A TCP connection, or the first datagram of a UDP flow. + Net { + host: NetHost, + port: u16, + protocol: Protocol, + }, +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum NetHost { + /// The name the command looked up (lowercase, no trailing dot). + Domain(String), + Ip(IpAddr), +} + +impl fmt::Display for NetHost { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + NetHost::Domain(name) => formatter.write_str(name), + NetHost::Ip(address) => write!(formatter, "{address}"), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Protocol { + Tcp, + Udp, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Permission { + Read, + Write, + /// Connecting to a unix socket. + Connect, +} + +impl Permission { + pub const ALL: [Permission; 3] = [Permission::Read, Permission::Write, Permission::Connect]; +} + +/// A handler's answer. +#[derive(Clone, Debug, Default)] +pub struct Decision { + pub allow: bool, + /// Rules appended to the sandbox's live policy before the syscall is + /// answered, so later requests they cover don't ask again. They last + /// until the sandbox is dropped or its policy is replaced. + pub remember: Vec, +} + +impl Decision { + pub fn allow() -> Self { + Self { + allow: true, + remember: Vec::new(), + } + } + + pub fn deny() -> Self { + Self::default() + } +} + +pub(crate) type HandlerId = u64; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) enum WireAccess { + Allow, + Deny, + Ask(HandlerId), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) enum WireRule { + Fs(WireFsRule), + Net(WireNetRule), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct WireFsRule { + pub path: PathPattern, + pub read: Option, + pub write: Option, + pub connect: Option, +} + +impl WireFsRule { + /// The only way to get at a rule's access: every permission is then + /// decided by the same path matching ([`WirePolicy::fs_access`]). + fn sets(&self, permission: Permission) -> Option { + match permission { + Permission::Read => self.read, + Permission::Write => self.write, + Permission::Connect => self.connect, + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct WireNetRule { + pub target: NetTarget, + pub ports: PortSet, + pub tcp: bool, + pub udp: bool, + pub connect: WireAccess, +} + +impl WireNetRule { + fn covers(&self, port: u16, protocol: Protocol) -> bool { + let protocol = match protocol { + Protocol::Tcp => self.tcp, + Protocol::Udp => self.udp, + }; + protocol && self.ports.contains(port) + } +} + +/// Whether a name could be connected to, by any port and protocol. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum NameVerdict { + Deny, + /// Only after asking. + Ask, + Allow, +} + +/// At most this many UDP ports get relay sockets up front. +const MAX_UDP_PORTS: usize = 256; + +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub(crate) struct WirePolicy { + pub fs: Vec, + pub net: Vec, +} + +impl WirePolicy { + pub fn add(&mut self, rule: WireRule) { + match rule { + WireRule::Fs(rule) => self.fs.push(rule), + WireRule::Net(rule) => self.net.push(rule), + } + } + + pub fn access(&self, request: &RequestedAccess) -> WireAccess { + match request { + RequestedAccess::Fs { permission, path } => self.fs_access(path, *permission), + RequestedAccess::Net { + host, + port, + protocol, + } => self + .net + .iter() + .rev() + .find(|rule| rule.covers(*port, *protocol) && rule.target.matches(host)) + .map_or(WireAccess::Deny, |rule| rule.connect), + } + } + + /// Approximate: an `Allow` for some ports that a later `Deny` shadows + /// still counts. Only a `Deny` covering every port and protocol hides + /// the rules before it. + pub fn name_verdict(&self, name: &str) -> NameVerdict { + let mut ask = false; + for rule in self.net.iter().rev() { + if !rule.target.matches_domain(name) { + continue; + } + match rule.connect { + WireAccess::Allow => return NameVerdict::Allow, + WireAccess::Ask(_) => ask = true, + WireAccess::Deny => { + if rule.ports == PortSet::All && rule.tcp && rule.udp { + break; + } + } + } + } + if ask { + NameVerdict::Ask + } else { + NameVerdict::Deny + } + } + + /// Whether some rule might allow UDP to `port` (ignoring shadowing). + pub fn udp_may_allow(&self, port: u16) -> bool { + self.net + .iter() + .any(|rule| rule.connect != WireAccess::Deny && rule.covers(port, Protocol::Udp)) + } + + /// The ports that rules naming explicit UDP ports might allow, so they + /// can be relayed before anything connects to them. + pub fn udp_ports(&self) -> BTreeSet { + let mut ports = BTreeSet::new(); + for rule in &self.net { + let PortSet::Ranges(ranges) = &rule.ports else { + continue; + }; + if !rule.udp || rule.connect == WireAccess::Deny { + continue; + } + for port in ranges.iter().cloned().flatten() { + if ports.len() >= MAX_UDP_PORTS { + return ports; + } + ports.insert(port); + } + } + ports + } + + pub fn fs_access(&self, path: &Path, permission: Permission) -> WireAccess { + let mut buffer = [&[][..]; 32]; + let Some(components) = matching_components(path, &mut buffer) else { + return WireAccess::Deny; + }; + self.fs + .iter() + .rev() + .filter_map(|rule| Some((rule, rule.sets(permission)?))) + .find(|(rule, _)| rule.path.matches_components(components.clone())) + .map_or(WireAccess::Deny, |(_, access)| access) + } + + /// Whether every rule about `permission` treats paths beneath `a` exactly + /// as it treats the same relative paths beneath `b`. If so, moving a + /// directory from `a` to `b` can't change `permission` for anything in it. + pub fn same_beneath(&self, a: &Path, b: &Path, permission: Permission) -> bool { + self.fs + .iter() + .filter(|rule| rule.sets(permission).is_some()) + .all(|rule| rule.path.state_after(a) == rule.path.state_after(b)) + } + + /// Whether giving the object at `from` the additional or new name `to` + /// would let `permission` reach it where it previously couldn't. + pub fn widens(&self, from: &Path, to: &Path, permission: Permission) -> bool { + self.fs_access(from, permission) != WireAccess::Allow + && self.fs_access(to, permission) == WireAccess::Allow + } +} + +impl Rule { + pub(crate) fn to_wire(&self, register: &mut impl FnMut(&Handler) -> HandlerId) -> WireRule { + match self { + Rule::Fs(rule) => WireRule::Fs(rule.to_wire(register)), + Rule::Net(rule) => WireRule::Net(rule.to_wire(register)), + } + } +} + +impl Access { + fn to_wire(&self, register: &mut impl FnMut(&Handler) -> HandlerId) -> WireAccess { + match self { + Access::Allow => WireAccess::Allow, + Access::Deny => WireAccess::Deny, + Access::Ask(handler) => WireAccess::Ask(register(handler)), + } + } +} + +impl NetRule { + pub(crate) fn to_wire(&self, register: &mut impl FnMut(&Handler) -> HandlerId) -> WireNetRule { + WireNetRule { + target: self.target.clone(), + ports: self.ports.clone(), + tcp: self.tcp, + udp: self.udp, + connect: self.connect.to_wire(register), + } + } +} + +impl FsRule { + pub(crate) fn to_wire(&self, register: &mut impl FnMut(&Handler) -> HandlerId) -> WireFsRule { + let mut convert = + |access: &Option| access.as_ref().map(|access| access.to_wire(register)); + WireFsRule { + path: self.path.clone(), + read: convert(&self.read), + write: convert(&self.write), + connect: convert(&self.connect), + } + } +} + +/// An absolute host path pattern. A pattern matches a path and everything +/// beneath it. Components may contain `*` (any run of characters other than +/// `/`) and `?` (one character), and a whole component of `**` matches any +/// number of components. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] +pub struct PathPattern { + source: String, + segments: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Segment { + Literal(Vec), + Wildcard(Vec), + AnyDepth, +} + +impl Segment { + fn matches(&self, component: &[u8]) -> bool { + match self { + Segment::Literal(name) => name == component, + Segment::Wildcard(pattern) => wildcard_matches(pattern, component), + Segment::AnyDepth => true, + } + } +} + +#[derive(Clone)] +enum MatchingComponents<'a> { + Cached(std::iter::Copied>), + Streaming(Components<'a>), +} + +impl<'a> Iterator for MatchingComponents<'a> { + type Item = &'a [u8]; + + fn next(&mut self) -> Option { + match self { + Self::Cached(components) => components.next(), + Self::Streaming(components) => components.find_map(|component| match component { + Component::Normal(name) => Some(name.as_bytes()), + _ => None, + }), + } + } +} + +/// Validate the whole path before a pattern can return early: `/a` must not +/// match `/a/../secret`. Cache typical paths on the stack so each rule needn't +/// split them again. Deep paths use a borrowed iterator, not a heap allocation +/// or a depth limit. +fn matching_components<'a>( + path: &'a Path, + buffer: &'a mut [&'a [u8]; 32], +) -> Option> { + let mut length = 0; + for component in path.components() { + match component { + Component::RootDir => {} + Component::Normal(name) => { + if let Some(slot) = buffer.get_mut(length) { + *slot = name.as_bytes(); + } + length += 1; + } + _ => return None, + } + } + Some(if length <= buffer.len() { + MatchingComponents::Cached(buffer[..length].iter().copied()) + } else { + MatchingComponents::Streaming(path.components()) + }) +} + +/// Where matching stands after consuming some leading path components. +#[derive(Debug, PartialEq, Eq)] +enum MatchState { + /// The components so far match, so everything beneath them does too. + Matched, + /// Segment positions that could still lead to a match (empty: none can). + Pending(BTreeSet), +} + +impl PathPattern { + pub fn new(source: impl Into) -> Result { + let source = source.into(); + if !source.starts_with('/') { + bail!("path pattern `{source}` must be absolute"); + } + let mut segments = Vec::new(); + for component in source.split('/').filter(|component| !component.is_empty()) { + segments.push(match component { + "." | ".." => bail!("path pattern `{source}` must not contain `.` or `..`"), + "**" => Segment::AnyDepth, + _ if component.contains("**") => { + bail!("`**` must be a whole component in path pattern `{source}`") + } + _ if component.contains(['*', '?']) => { + Segment::Wildcard(component.as_bytes().to_vec()) + } + _ => Segment::Literal(component.as_bytes().to_vec()), + }); + } + Ok(Self { source, segments }) + } + + /// A pattern matching exactly `path` and everything beneath it. + pub fn literal(path: &Path) -> Result { + let Some(source) = path.to_str() else { + bail!("path {} is not valid UTF-8", path.display()); + }; + if source.contains(['*', '?']) { + bail!("path {source} contains wildcard characters"); + } + Self::new(source) + } + + fn root() -> Self { + Self { + source: "/".to_string(), + segments: Vec::new(), + } + } + + pub fn as_str(&self) -> &str { + &self.source + } + + /// Whether `path` (absolute and normalized) is matched by this pattern or + /// lies beneath a match. + pub fn matches(&self, path: &Path) -> bool { + let mut buffer = [&[][..]; 32]; + matching_components(path, &mut buffer) + .is_some_and(|components| self.matches_components(components)) + } + + fn matches_components(&self, mut components: MatchingComponents<'_>) -> bool { + let mut position = 0; + // Only the latest `**` needs a bookmark. Earlier ones can stop as + // soon as their suffix reaches this one: extending an earlier `**` + // can't match anything the later one couldn't consume instead. + let mut restart = None; + loop { + if position == self.segments.len() { + // A matched pattern also covers every descendant. + return true; + } + if matches!(self.segments.get(position), Some(Segment::AnyDepth)) { + position += 1; + restart = Some((position, components.clone())); + continue; + } + match components.next() { + Some(component) if self.segments[position].matches(component) => { + position += 1; + } + _ => { + let Some((after_star, saved)) = &mut restart else { + return false; + }; + if saved.next().is_none() { + return false; + } + position = *after_star; + components = saved.clone(); + } + } + } + } + + fn state_after(&self, path: &Path) -> MatchState { + let mut components = Vec::new(); + for component in path.components() { + match component { + Component::RootDir => {} + Component::Normal(name) => components.push(name.as_bytes()), + Component::Prefix(_) | Component::CurDir | Component::ParentDir => { + return MatchState::Pending(BTreeSet::new()); + } + } + } + + let end = self.segments.len(); + let mut positions = self.closure(BTreeSet::from([0])); + for component in components { + if positions.contains(&end) { + return MatchState::Matched; + } + let mut next = BTreeSet::new(); + for &position in &positions { + match &self.segments[position] { + Segment::AnyDepth => { + next.insert(position); + } + Segment::Literal(name) => { + if component == name.as_slice() { + next.insert(position + 1); + } + } + Segment::Wildcard(pattern) => { + if wildcard_matches(pattern, component) { + next.insert(position + 1); + } + } + } + } + positions = self.closure(next); + } + if positions.contains(&end) { + MatchState::Matched + } else { + MatchState::Pending(positions) + } + } + + /// Add the positions reachable without consuming a component (`**` may + /// match zero components). + fn closure(&self, mut positions: BTreeSet) -> BTreeSet { + let mut stack: Vec = positions.iter().copied().collect(); + while let Some(position) = stack.pop() { + if matches!(self.segments.get(position), Some(Segment::AnyDepth)) + && positions.insert(position + 1) + { + stack.push(position + 1); + } + } + positions + } +} + +impl TryFrom for PathPattern { + type Error = anyhow::Error; + + fn try_from(source: String) -> Result { + Self::new(source) + } +} + +impl From for String { + fn from(pattern: PathPattern) -> Self { + pattern.source + } +} + +fn wildcard_matches(pattern: &[u8], text: &[u8]) -> bool { + let (mut position, mut consumed) = (0, 0); + let mut restart = None; + while consumed < text.len() { + match pattern.get(position) { + Some(b'*') => { + position += 1; + restart = Some((position, consumed)); + } + Some(byte) if *byte == b'?' || *byte == text[consumed] => { + position += 1; + consumed += 1; + } + _ => { + let Some((after_star, saved)) = &mut restart else { + return false; + }; + *saved += 1; + position = *after_star; + consumed = *saved; + } + } + } + pattern[position..].iter().all(|byte| *byte == b'*') +} + +#[cfg(test)] +mod tests { + use super::*; + use Access::{Allow, Deny}; + use Permission::*; + + struct CountingAllocator; + + thread_local! { + static ALLOCATIONS: std::cell::Cell> = const { + std::cell::Cell::new(None) + }; + } + + // Count only the calling test thread, not other tests running alongside it. + #[global_allocator] + static ALLOCATOR: CountingAllocator = CountingAllocator; + + unsafe impl std::alloc::GlobalAlloc for CountingAllocator { + unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 { + ALLOCATIONS.with(|count| count.set(count.get().map(|count| count + 1))); + // SAFETY: forward the allocator contract unchanged. + unsafe { std::alloc::System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: std::alloc::Layout) -> *mut u8 { + ALLOCATIONS.with(|count| count.set(count.get().map(|count| count + 1))); + // SAFETY: forward the allocator contract unchanged. + unsafe { std::alloc::System.alloc_zeroed(layout) } + } + + unsafe fn realloc( + &self, + pointer: *mut u8, + layout: std::alloc::Layout, + size: usize, + ) -> *mut u8 { + ALLOCATIONS.with(|count| count.set(count.get().map(|count| count + 1))); + // SAFETY: forward the allocator contract unchanged. + unsafe { std::alloc::System.realloc(pointer, layout, size) } + } + + unsafe fn dealloc(&self, pointer: *mut u8, layout: std::alloc::Layout) { + // SAFETY: forward the allocator contract unchanged. + unsafe { std::alloc::System.dealloc(pointer, layout) } + } + } + + fn pattern(source: &str) -> PathPattern { + PathPattern::new(source).unwrap() + } + + fn rule(source: &str) -> FsRule { + FsRule::new(source).unwrap() + } + + #[test] + fn test_allocation_free_matcher_equivalence() { + fn combinations(parts: &[&str], depth: usize) -> Vec { + let mut all = vec![String::new()]; + let mut level = vec![String::new()]; + for _ in 0..depth { + level = level + .iter() + .flat_map(|prefix| parts.iter().map(move |part| format!("{prefix}/{part}"))) + .collect(); + all.extend(level.iter().cloned()); + } + all + } + let patterns = combinations(&["a", "b", "*a", "?", "**"], 4); + let mut paths = combinations(&["a", "b", "aa", "ab"], 4); + paths.extend(["a", "./a", "/a/../b", "/a/./b", "//a//b/", "/", ""].map(str::to_owned)); + let patterns: Vec<_> = patterns + .iter() + .map(|source| pattern(&format!("/{source}"))) + .collect(); + for pattern in &patterns { + for path in &paths { + let path = Path::new(path); + assert_eq!( + pattern.matches(path), + pattern.state_after(path) == MatchState::Matched, + "{}: {path:?}", + pattern.as_str(), + ); + } + } + for patterns in patterns.chunks(9) { + let policy = wire( + patterns + .iter() + .enumerate() + .map(|(index, path)| { + let access = match index % 3 { + 0 => Allow, + 1 => Deny, + _ => Access::Ask(Handler::new(|_, _| {})), + }; + let mut rule = FsRule::new(path.as_str()).unwrap(); + match index % 4 { + 0 => rule.read = Some(access), + 1 => rule.write = Some(access), + 2 => rule.connect = Some(access), + _ => { + rule.read = Some(access.clone()); + rule.write = Some(access.clone()); + rule.connect = Some(access); + } + } + rule + }) + .collect(), + ); + for path in &paths { + let path = Path::new(path); + for permission in Permission::ALL { + let expected = policy + .fs + .iter() + .rev() + .filter_map(|rule| Some((rule, rule.sets(permission)?))) + .find(|(rule, _)| rule.path.state_after(path) == MatchState::Matched) + .map_or(WireAccess::Deny, |(_, access)| access); + assert_eq!(policy.fs_access(path, permission), expected); + } + } + } + } + + #[test] + fn test_long_patterns_and_non_utf8_paths() { + for length in [31, 32, 33, 63, 64, 65, 128, 256] { + let prefix = "/a".repeat(length); + let source = format!("{prefix}/**/b?*/**/tail"); + let pattern = pattern(&source); + for suffix in ["/b0/tail", "/c/b12/c/tail/child", "/b/tail", "/wrong"] { + let path = format!("{prefix}{suffix}"); + assert_eq!( + pattern.matches(Path::new(&path)), + pattern.state_after(Path::new(&path)) == MatchState::Matched, + ); + } + let path = format!("{prefix}/b0/tail"); + assert!(pattern.matches(Path::new(&path))); + } + let path = Path::new(std::ffi::OsStr::from_bytes(b"/a/\xff/tail")); + assert!(pattern("/a/?/tail").matches(path)); + assert!(pattern("/**/tail").matches(path)); + assert!(!pattern("/a/a*/tail").matches(path)); + let prefix = "/a".repeat(33); + for source in ["/**/a/*b/**/tail", "/**/a/**/b*/tail", "/**/tail"] { + let pattern = pattern(source); + for suffix in ["/ab/c/tail", "/a/b/tail", "/tail", "/wrong"] { + let path = format!("{prefix}{suffix}"); + assert_eq!( + pattern.matches(Path::new(&path)), + pattern.state_after(Path::new(&path)) == MatchState::Matched, + ); + } + } + } + + #[test] + fn test_policy_queries_do_not_allocate() { + use std::hint::black_box; + + let prefix = "/a".repeat(256); + let policy = wire(vec![ + rule("/").read(Allow), + rule("/**/*.env").read(Deny), + rule("/proj/**/socket?").connect(Allow), + rule(&format!("{prefix}/**/b?*/**/tail")).write(Allow), + ]); + let paths = [ + "/proj/.env".to_owned(), + "/proj/sub/socket1".to_owned(), + "/other/file".to_owned(), + format!("{prefix}/b0/tail"), + ]; + ALLOCATIONS.with(|count| count.set(Some(0))); + for path in &paths { + let path = black_box(Path::new(path)); + for rule in &policy.fs { + black_box(rule.path.matches(path)); + } + for permission in Permission::ALL { + black_box(policy.fs_access(path, permission)); + } + } + let allocations = ALLOCATIONS.with(|count| count.replace(None)).unwrap(); + assert_eq!(allocations, 0); + } + + #[test] + fn test_component_glob_equivalence() { + fn reference(pattern: &[u8], text: &[u8]) -> bool { + match pattern.split_first() { + None => text.is_empty(), + Some((b'*', rest)) => (0..=text.len()).any(|skip| reference(rest, &text[skip..])), + Some((b'?', rest)) => !text.is_empty() && reference(rest, &text[1..]), + Some((byte, rest)) => text.first() == Some(byte) && reference(rest, &text[1..]), + } + } + for encoded in 0..4096usize { + let mut value = encoded; + let mut pattern = Vec::new(); + for _ in 0..6 { + pattern.push(b"ab*?"[value % 4]); + value /= 4; + } + for length in 0..=5 { + for encoded in 0..1usize << length { + let text: Vec<_> = (0..length) + .map(|position| b"ab"[(encoded >> position) & 1]) + .collect(); + assert_eq!( + wildcard_matches(&pattern, &text), + reference(&pattern, &text), + "{pattern:?}: {text:?}", + ); + } + } + } + assert!(wildcard_matches(b"*", b"")); + assert!(!wildcard_matches(b"?", b"")); + // No recursive backtracking or call-stack limit. + let mut pattern = b"*a".repeat(1024); + pattern.push(b'b'); + assert!(!wildcard_matches(&pattern, &vec![b'a'; 2048])); + } + + /// Convert as `Sandbox` does, numbering handlers in order of appearance. + fn wire(rules: Vec) -> WirePolicy { + let mut next = 0; + let mut register = |_: &Handler| { + next += 1; + next - 1 + }; + WirePolicy { + fs: rules + .iter() + .map(|rule| rule.to_wire(&mut register)) + .collect(), + net: Vec::new(), + } + } + + fn wire_net(rules: Vec) -> WirePolicy { + let mut next = 0; + let mut register = |_: &Handler| { + next += 1; + next - 1 + }; + WirePolicy { + fs: Vec::new(), + net: rules + .iter() + .map(|rule| rule.to_wire(&mut register)) + .collect(), + } + } + + #[test] + fn test_net_rules() { + let domain = |source: &str| NetTarget::Domain(DomainPattern::new(source).unwrap()); + let ip = |source: &str| NetTarget::Ip(source.parse().unwrap()); + let policy = wire_net(vec![ + NetRule::new(domain("*.example.com"), Allow).ports(PortSet::single(443)), + NetRule::new(domain("ask.test"), Access::Ask(Handler::new(|_, _| {}))), + NetRule::new(ip("10.0.0.0/8"), Allow).tcp_only(), + NetRule::new(ip("10.1.2.3"), Deny), + NetRule::new(NetTarget::Loopback, Allow).ports(PortSet::Ranges(vec![5000..=5999])), + ]); + let access = |host: NetHost, port, protocol| { + policy.access(&RequestedAccess::Net { + host, + port, + protocol, + }) + }; + let name = |name: &str| NetHost::Domain(name.to_string()); + let address = |address: &str| NetHost::Ip(address.parse().unwrap()); + use Protocol::*; + assert_eq!(access(name("api.example.com"), 443, Tcp), WireAccess::Allow); + assert_eq!(access(name("example.com"), 443, Tcp), WireAccess::Deny); + assert_eq!(access(name("api.example.com"), 80, Tcp), WireAccess::Deny); + assert_eq!(access(name("ask.test"), 22, Udp), WireAccess::Ask(0)); + assert_eq!(access(address("10.9.9.9"), 22, Tcp), WireAccess::Allow); + assert_eq!(access(address("10.9.9.9"), 22, Udp), WireAccess::Deny); + assert_eq!(access(address("10.1.2.3"), 22, Tcp), WireAccess::Deny); + assert_eq!( + access(address("::ffff:10.9.9.9"), 22, Tcp), + WireAccess::Allow + ); + assert_eq!(access(address("127.0.0.1"), 5432, Tcp), WireAccess::Allow); + assert_eq!(access(address("::1"), 5432, Udp), WireAccess::Allow); + assert_eq!(access(address("127.0.0.1"), 6000, Tcp), WireAccess::Deny); + // Names only match name rules. + assert_eq!(access(name("localhost"), 5432, Tcp), WireAccess::Deny); + + assert_eq!(policy.name_verdict("api.example.com"), NameVerdict::Allow); + assert_eq!(policy.name_verdict("ask.test"), NameVerdict::Ask); + assert_eq!(policy.name_verdict("other.test"), NameVerdict::Deny); + // `ask.test` covers every UDP port. + assert!(policy.udp_may_allow(22)); + assert!(!wire_net(Vec::new()).udp_may_allow(22)); + let ports = policy.udp_ports(); + assert!(ports.contains(&443) && ports.contains(&5000)); + assert_eq!(ports.len(), MAX_UDP_PORTS); + } + + #[test] + fn test_address_classes() { + let class = |address: &str| AddressClass::of(address.parse().unwrap()); + assert_eq!(class("127.0.0.53"), AddressClass::Loopback); + assert_eq!(class("0.0.0.0"), AddressClass::Loopback); + assert_eq!(class("::ffff:127.0.0.1"), AddressClass::Loopback); + assert_eq!(class("192.168.1.1"), AddressClass::LocalNetwork); + assert_eq!(class("100.100.1.1"), AddressClass::LocalNetwork); + assert_eq!(class("100.128.1.1"), AddressClass::Public); + assert_eq!(class("fd12::1"), AddressClass::LocalNetwork); + assert_eq!(class("fe80::1"), AddressClass::LocalNetwork); + assert_eq!(class("2606:4700::1111"), AddressClass::Public); + assert!( + DomainPattern::new("*.Example.COM.") + .unwrap() + .matches("a.b.example.com") + ); + assert!(DomainPattern::new("exa*mple.com").is_err()); + assert!(DomainPattern::new("*").is_err()); + + let parse = |destination| NetRule::parse(destination, Allow).unwrap(); + let rule = parse("tcp/*.github.com:22,443,8000-8999"); + assert_eq!( + rule.target, + NetTarget::Domain(DomainPattern::new("*.github.com").unwrap()) + ); + assert_eq!( + rule.ports, + PortSet::Ranges(vec![22..=22, 443..=443, 8000..=8999]) + ); + assert!(rule.tcp && !rule.udp); + assert_eq!( + parse("[fd00::/8]:53").target, + NetTarget::Ip("fd00::/8".parse().unwrap()) + ); + assert_eq!(parse("::1").ports, PortSet::All); + assert_eq!(parse("udp/loopback").target, NetTarget::Loopback); + assert!(NetRule::parse("example.com:http", Allow).is_err()); + } + + #[test] + fn test_pattern_matching() { + assert!(pattern("/").matches(Path::new("/anything/at/all"))); + assert!(pattern("/proj").matches(Path::new("/proj"))); + assert!(pattern("/proj").matches(Path::new("/proj/src/main.rs"))); + assert!(!pattern("/proj").matches(Path::new("/project"))); + assert!(!pattern("/proj").matches(Path::new("/"))); + assert!(pattern("/proj/*.env").matches(Path::new("/proj/prod.env"))); + assert!(pattern("/proj/*.env").matches(Path::new("/proj/.env"))); + assert!(!pattern("/proj/*.env").matches(Path::new("/proj/sub/prod.env"))); + assert!(pattern("/**/*.env").matches(Path::new("/a/b/c.env"))); + assert!(pattern("/**/*.env").matches(Path::new("/c.env"))); + assert!(pattern("/proj/**/.git").matches(Path::new("/proj/.git/config"))); + assert!(pattern("/proj/**/.git").matches(Path::new("/proj/a/b/.git"))); + assert!(!pattern("/proj/**/.git").matches(Path::new("/proj/a/b/git"))); + assert!(pattern("/a/?").matches(Path::new("/a/b"))); + assert!(!pattern("/a/?").matches(Path::new("/a/bc"))); + assert!(!pattern("/proj").matches(Path::new("/proj/../etc"))); + } + + #[test] + fn test_pattern_validation() { + assert!(PathPattern::new("relative").is_err()); + assert!(PathPattern::new("/a/../b").is_err()); + assert!(PathPattern::new("/a/b**").is_err()); + assert!(PathPattern::literal(Path::new("/a/b*")).is_err()); + } + + #[test] + fn test_last_matching_rule_wins_per_permission() { + let handler = Handler::new(|_, _| {}); + let policy = wire(vec![ + rule("/").read(Allow), + rule("/proj").write(Allow), + rule("/proj/.git").write(Deny), + rule("/**/*.env").read(Deny), + rule("/proj/ask").write(Access::Ask(handler)), + ]); + let access = |path: &str, permission| policy.fs_access(Path::new(path), permission); + assert_eq!(access("/etc/passwd", Read), WireAccess::Allow); + assert_eq!(access("/etc/passwd", Write), WireAccess::Deny); + assert_eq!(access("/proj/src", Write), WireAccess::Allow); + assert_eq!(access("/proj/.git/config", Write), WireAccess::Deny); + assert_eq!(access("/proj/.env", Read), WireAccess::Deny); + // The `.env` rule says nothing about writes. + assert_eq!(access("/proj/.env", Write), WireAccess::Allow); + assert_eq!(access("/proj/ask/x", Write), WireAccess::Ask(0)); + assert_eq!( + WirePolicy::default().fs_access(Path::new("/x"), Read), + WireAccess::Deny + ); + assert_eq!( + wire(SandboxPolicy::unrestricted().fs).fs_access(Path::new("/x"), Write), + WireAccess::Allow + ); + } + + #[test] + fn test_directory_moves() { + let policy = wire(vec![ + rule("/").read(Allow), + rule("/proj").write(Allow), + rule("/proj/.git").write(Deny), + rule("/**/*.env").read(Deny), + rule("/home/*/secret").read(Deny), + ]); + let same = |a: &str, b: &str, permission| { + policy.same_beneath(Path::new(a), Path::new(b), permission) + }; + // `**/*.env` doesn't care where a directory lives. + assert!(same("/proj/a", "/proj/b", Read)); + assert!(same("/proj/a", "/proj/b", Write)); + // Moving the project would move `.git` out from under its rule. + assert!(!same("/proj", "/proj2", Write)); + // Contents named `secret` are only denied directly under `/home/*`. + assert!(!same("/home", "/tmp", Read)); + assert!(same("/home/u/a", "/tmp/a", Read)); + assert!(!same("/home/u", "/tmp/u", Read)); + } +} diff --git a/crates/sandbox_linux/src/process.rs b/crates/sandbox_linux/src/process.rs new file mode 100644 index 00000000000..041a67dc848 --- /dev/null +++ b/crates/sandbox_linux/src/process.rs @@ -0,0 +1,130 @@ +//! Which host processes belong to an execution. +//! +//! There's no pid namespace, so sandboxed processes share the host's pid +//! space. Every process of an execution descends from its helper, which is a +//! child subreaper: orphans are reparented to it rather than escaping to the +//! host's init. + +use std::collections::HashMap; +use std::path::{Component, Path}; + +pub(crate) struct ProcessInfo { + pub pid: i32, + pub parent: i32, + pub group: i32, +} + +/// Parse `/proc//stat`. The command name is in parentheses and may +/// itself contain spaces and parentheses, so fields are read after the last +/// `)`. +pub(crate) fn process_info(pid: i32) -> Option { + let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; + let (_, fields) = stat.rsplit_once(')')?; + let mut fields = fields.split_whitespace(); + let _state = fields.next()?; + let parent = fields.next()?.parse().ok()?; + let group = fields.next()?.parse().ok()?; + Some(ProcessInfo { pid, parent, group }) +} + +/// The controlling terminal device of `pid` (0: none). +pub(crate) fn controlling_terminal(pid: i32) -> Option { + let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; + let (_, fields) = stat.rsplit_once(')')?; + // state ppid pgrp session tty_nr + fields + .split_whitespace() + .nth(4)? + .parse::() + .ok() + .map(|tty| tty as u64) +} + +pub(crate) fn thread_group(tid: i32) -> Option { + read_status_head(tid)? + .lines() + .find_map(|line| line.strip_prefix("Tgid:")) + .and_then(|value| value.trim().parse().ok()) +} + +/// The start of `/proc//status` (`Name`, `Umask`, `State`, `Tgid`, +/// ...), in one `read`. Reading the whole file makes the kernel format +/// dozens of lines, through a growing buffer. +pub(crate) fn read_status_head(tid: i32) -> Option { + use std::io::Read as _; + let mut file = std::fs::File::open(format!("/proc/{tid}/status")).ok()?; + let mut buffer = [0u8; 512]; + let length = file.read(&mut buffer).ok()?; + Some(String::from_utf8_lossy(&buffer[..length]).into_owned()) +} + +pub(crate) fn all_processes() -> Vec { + let Ok(entries) = std::fs::read_dir("/proc") else { + return Vec::new(); + }; + entries + .filter_map(|entry| entry.ok()?.file_name().to_str()?.parse().ok()) + .filter_map(process_info) + .collect() +} + +/// The pid in a `/proc//...` path, if it is one. +pub(crate) fn proc_path_pid(path: &Path) -> Option { + let mut components = path.components(); + if components.next() != Some(Component::RootDir) + || components.next() != Some(Component::Normal("proc".as_ref())) + { + return None; + } + match components.next()? { + Component::Normal(name) => name.to_str()?.parse().ok(), + _ => None, + } +} + +#[derive(Clone, Copy, Debug)] +pub(crate) struct ProcessTree { + pub helper: i32, +} + +impl ProcessTree { + /// Whether `pid` (a process or thread id) strictly descends from the + /// helper. + pub fn contains(&self, pid: i32) -> bool { + let mut current = pid; + for _ in 0..4096 { + let Some(info) = process_info(current) else { + return false; + }; + if info.parent == self.helper { + return true; + } + if info.parent <= 1 { + return false; + } + current = info.parent; + } + false + } + + pub fn members(&self) -> Vec { + let processes = all_processes(); + let mut children: HashMap> = HashMap::new(); + for (index, process) in processes.iter().enumerate() { + children.entry(process.parent).or_default().push(index); + } + let mut members = Vec::new(); + let mut stack = vec![self.helper]; + while let Some(parent) = stack.pop() { + for &index in children.get(&parent).into_iter().flatten() { + stack.push(processes[index].pid); + members.push(index); + } + } + let mut processes: Vec> = processes.into_iter().map(Some).collect(); + members + .into_iter() + .filter_map(|index| processes[index].take()) + .collect() + } +} diff --git a/crates/sandbox_linux/src/protocol.rs b/crates/sandbox_linux/src/protocol.rs new file mode 100644 index 00000000000..5ce6983d3d4 --- /dev/null +++ b/crates/sandbox_linux/src/protocol.rs @@ -0,0 +1,275 @@ +//! Messages between Zed, the supervisor and the helper. +//! +//! Every channel is an anonymous `SOCK_SEQPACKET` socketpair, so each message +//! is one packet: a JSON body plus any descriptors as `SCM_RIGHTS`. Packets +//! are capped by the socket buffer (~200 KiB by default), so larger bodies +//! (e.g. a big environment) travel in a memfd passed as the first fd. +//! +//! ```text +//! Zed <--control--> supervisor ZedMessage / SupervisorMessage +//! Zed --setup-----> helper HelperSetup (once) +//! helper --attach----> supervisor AttachInfo + the seccomp listener (once) +//! helper <-attach----> supervisor FactoryRequest / FactoryResponse (then) +//! ``` + +use std::io::{IoSlice, IoSliceMut}; +use std::net::SocketAddr; +use std::os::fd::{AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd, RawFd}; +use std::path::PathBuf; + +use anyhow::{Context as _, Result, bail}; +use nix::sys::socket::{ControlMessage, ControlMessageOwned, MsgFlags, recvmsg, sendmsg}; +use serde::{Deserialize, Serialize, de::DeserializeOwned}; + +use crate::UntrustedCommand; +use crate::net::SocketKind; +use crate::policy::{HandlerId, RequestedAccess, WirePolicy, WireRule}; + +const MAX_FDS: usize = 8; +const MAX_INLINE_SIZE: usize = 64 * 1024; +const MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024; + +const INLINE_BODY: u8 = 0; +const MEMFD_BODY: u8 = 1; + +pub(crate) type SandboxId = u64; +pub(crate) type RequestId = u64; +pub(crate) type PromptId = u64; + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum ZedMessage { + Request { + id: RequestId, + request: Request, + }, + PromptReply { + prompt: PromptId, + allow: bool, + /// Appended to the sandbox's policy before the syscall is answered. + remember: Vec, + }, +} + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum Request { + /// `sandbox` is chosen by Zed, so it can register the policy's handlers + /// under it before the supervisor can ask any of them. + CreateSandbox { + sandbox: SandboxId, + policy: WirePolicy, + }, + UpdatePolicy { + sandbox: SandboxId, + policy: WirePolicy, + }, + DestroySandbox { + sandbox: SandboxId, + }, + /// Descriptors: the supervisor's end of a fresh attach socketpair, whose + /// other end goes to the helper of one execution in `sandbox`. + AttachExecution { + sandbox: SandboxId, + }, +} + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum SupervisorMessage { + Response { + id: RequestId, + response: Response, + }, + Prompt(PromptRequest), + /// The syscall behind a prompt was abandoned or timed out; a reply is no + /// longer needed. + PromptCancelled { + prompt: PromptId, + }, + Denied { + sandbox: SandboxId, + denial: Denial, + }, +} + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum Response { + PolicyUpdated { still_open: Vec }, + Done, + Error { message: String }, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct PromptRequest { + pub prompt: PromptId, + pub sandbox: SandboxId, + /// The handler (in Zed's table) that decides. + pub handler: HandlerId, + pub requests: Vec, + pub pid: i32, + pub executable: Option, +} + +/// An operation the policy, or a handler answering an [`Access::Ask`], +/// denied. +/// +/// [`Access::Ask`]: crate::Access::Ask +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Denial { + /// What the denied syscall needed that wasn't granted. + pub requests: Vec, + /// The thread that made the syscall. + pub pid: i32, + pub executable: Option, +} + +/// A file a sandboxed process still has open with access the current policy +/// no longer grants. Revoking a grant never affects open descriptors. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct OpenFile { + pub pid: i32, + pub path: PathBuf, + pub read: bool, + pub write: bool, +} + +/// Sent by the helper's command (before it execs) with its seccomp listener, +/// followed by the sandbox's DNS sockets. +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct AttachInfo { + /// Every process of the execution descends from the helper, which is a + /// child subreaper. + pub helper_pid: i32, +} + +/// Asks the helper for a socket in the sandbox's network namespace, where +/// only it can create them, or for a duplicate of a descriptor of one of its +/// processes. Answered with a [`FactoryResponse`], carrying the socket if +/// there was one. +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum FactoryRequest { + CreateSocket { + kind: SocketKind, + address: SocketAddr, + }, + /// Fetching a descriptor is `pidfd_getfd(2)`, which the kernel gates on + /// ptrace access. The supervisor may be denied that as a mere sibling of + /// the command (by Yama's `ptrace_scope`, or AppArmor's restriction of + /// unprivileged user namespaces), so it asks the helper, every command's + /// parent, instead. + FetchFd { pid: i32, fd: i32 }, +} + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum FactoryResponse { + Created, + Fetched, + Failed { errno: i32 }, +} + +/// Everything the helper needs to set up the sandbox and run the command. +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct HelperSetup { + pub command: UntrustedCommand, +} + +pub(crate) fn send_message( + socket: BorrowedFd<'_>, + message: &T, + fds: &[RawFd], +) -> Result<()> { + let body = serde_json::to_vec(message).context("serializing sandbox message")?; + if body.len() > MAX_MESSAGE_SIZE { + bail!("sandbox message too large ({} bytes)", body.len()); + } + if fds.len() >= MAX_FDS { + bail!( + "too many descriptors in one sandbox message ({})", + fds.len() + ); + } + let memfd; + let mut all_fds = Vec::with_capacity(fds.len() + 1); + let packet = if body.len() <= MAX_INLINE_SIZE { + let mut packet = vec![INLINE_BODY]; + packet.extend_from_slice(&body); + packet + } else { + memfd = body_memfd(&body)?; + all_fds.push(memfd.as_raw_fd()); + vec![MEMFD_BODY] + }; + all_fds.extend_from_slice(fds); + let iov = [IoSlice::new(&packet)]; + let control = [ControlMessage::ScmRights(&all_fds)]; + let control: &[ControlMessage] = if all_fds.is_empty() { &[] } else { &control }; + sendmsg::<()>( + socket.as_raw_fd(), + &iov, + control, + MsgFlags::MSG_NOSIGNAL, + None, + ) + .context("sending sandbox message")?; + Ok(()) +} + +/// Receive one message. `Ok(None)` means the peer closed the channel. +pub(crate) fn receive_message( + socket: BorrowedFd<'_>, +) -> Result)>> { + let mut body = vec![0u8; MAX_INLINE_SIZE + 1]; + let mut control = nix::cmsg_space!([RawFd; MAX_FDS]); + let mut fds = Vec::new(); + let (length, flags) = { + let mut iov = [IoSliceMut::new(&mut body)]; + let message = recvmsg::<()>( + socket.as_raw_fd(), + &mut iov, + Some(&mut control), + MsgFlags::MSG_CMSG_CLOEXEC, + ) + .context("receiving sandbox message")?; + for control_message in message.cmsgs().context("reading descriptors")? { + if let ControlMessageOwned::ScmRights(raw_fds) = control_message { + for raw_fd in raw_fds { + // SAFETY: the kernel installed each received descriptor + // fresh in our table; we take sole ownership here. + fds.push(unsafe { OwnedFd::from_raw_fd(raw_fd) }); + } + } + } + (message.bytes, message.flags) + }; + if flags.intersects(MsgFlags::MSG_TRUNC | MsgFlags::MSG_CTRUNC) { + bail!("sandbox message was truncated"); + } + if length == 0 && fds.is_empty() { + return Ok(None); + } + let message = match body.first() { + Some(&INLINE_BODY) => serde_json::from_slice(&body[1..length]), + Some(&MEMFD_BODY) if length == 1 && !fds.is_empty() => { + let mut file = std::fs::File::from(fds.remove(0)); + let mut contents = Vec::new(); + std::io::Read::read_to_end( + &mut std::io::Read::take(&mut file, MAX_MESSAGE_SIZE as u64), + &mut contents, + )?; + serde_json::from_slice(&contents) + } + _ => bail!("malformed sandbox message"), + } + .context("parsing sandbox message")?; + Ok(Some((message, fds))) +} + +fn body_memfd(body: &[u8]) -> Result { + // SAFETY: valid NUL-terminated name. + let fd = crate::sys::cvt(unsafe { + libc::memfd_create(c"zed-sandbox-message".as_ptr(), libc::MFD_CLOEXEC) + })?; + // SAFETY: a freshly returned descriptor that nothing else owns. + let mut file = std::fs::File::from(unsafe { OwnedFd::from_raw_fd(fd) }); + std::io::Write::write_all(&mut file, body)?; + std::io::Seek::rewind(&mut file)?; + Ok(file.into()) +} diff --git a/crates/sandbox_linux/src/resolve.rs b/crates/sandbox_linux/src/resolve.rs new file mode 100644 index 00000000000..dd636245fc6 --- /dev/null +++ b/crates/sandbox_linux/src/resolve.rs @@ -0,0 +1,285 @@ +//! Path resolution on behalf of a sandboxed thread. +//! +//! The supervisor never opens a path string the target gave it in one go: +//! the kernel would resolve `/proc/self` (and symlinks through it, like +//! `/dev/stdin`) as the *supervisor*, and a racing target could swap +//! components between our check and our use. Instead we walk one component +//! at a time from a root fd, holding each directory, and decide on the +//! object we end up holding. +//! +//! Most paths take a fast path: `openat2` resolves the whole path in the +//! kernel, confined to the root (`RESOLVE_IN_ROOT`, so `..` and absolute +//! symlinks behave as for the target) and refusing magic links. The result +//! is only used when it isn't on procfs, the one place where resolving as the +//! supervisor differs from resolving as the target; anything else falls back +//! to the walk. + +use std::collections::VecDeque; +use std::ffi::{CString, OsStr, OsString}; +use std::io; +use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, OwnedFd}; +use std::os::unix::ffi::OsStrExt as _; +use std::path::{Component, Path}; + +use crate::process::{ProcessTree, proc_path_pid}; +use crate::sys::{self, errno}; + +const MAX_SYMLINKS: usize = 40; +const PROC_SUPER_MAGIC: i64 = 0x9fa0; + +// Syscalls added after the per-arch tables were unified share one number. +const SYS_OPENAT2: libc::c_long = 437; +const RESOLVE_NO_MAGICLINKS: u64 = 0x02; +const RESOLVE_IN_ROOT: u64 = 0x10; + +pub(crate) enum Resolved { + /// The final component, unopened: in [`Mode::Entry`], or because it + /// doesn't exist. + Entry { parent: OwnedFd, name: CString }, + /// The final object, opened `O_PATH`. + Object(OwnedFd), +} + +#[derive(Clone, Copy)] +pub(crate) enum Mode { + /// Resolve the parent directory, and never look at the final component. + Entry, + /// Open the final component, following it if it's a symlink and `follow`. + Object { follow: bool }, +} + +pub(crate) struct Resolver<'a> { + pub root: BorrowedFd<'a>, + /// The thread the path belongs to, for `/proc/self` and + /// `/proc/thread-self`. + pub tid: i32, + /// The thread's process, looked up only if `/proc/self` is reached. + pub tgid: &'a dyn Fn() -> io::Result, + pub tree: ProcessTree, +} + +impl Resolver<'_> { + pub fn resolve(&self, path: &Path, mode: Mode) -> io::Result { + if let Some(resolved) = self.resolve_fast(path, mode) { + return Ok(resolved); + } + self.resolve_walk(path, mode) + } + + /// `None` whenever the walk has to decide (errors included, so that + /// error cases behave exactly as the walk makes them). + fn resolve_fast(&self, path: &Path, mode: Mode) -> Option { + match mode { + Mode::Object { follow } => { + let flags = if follow { 0 } else { libc::O_NOFOLLOW }; + let object = self.open_in_root(path.as_os_str(), flags).ok()?; + (!is_procfs(object.as_fd()).ok()?).then_some(Resolved::Object(object)) + } + Mode::Entry => { + let name = match path.components().next_back()? { + Component::Normal(name) => name, + _ => return None, + }; + let parent = self + .open_in_root(path.parent()?.as_os_str(), libc::O_DIRECTORY) + .ok()?; + if is_procfs(parent.as_fd()).ok()? { + return None; + } + Some(Resolved::Entry { + parent, + name: sys::cstring(name.as_bytes()).ok()?, + }) + } + } + } + + fn open_in_root(&self, path: &OsStr, flags: i32) -> io::Result { + #[repr(C)] + struct OpenHow { + flags: u64, + mode: u64, + resolve: u64, + } + let path = sys::cstring(path.as_bytes())?; + let how = OpenHow { + flags: (libc::O_PATH | libc::O_CLOEXEC | flags) as u64, + mode: 0, + resolve: RESOLVE_IN_ROOT | RESOLVE_NO_MAGICLINKS, + }; + // SAFETY: a NUL-terminated path and a correctly sized `open_how`. + let fd = unsafe { + libc::syscall( + SYS_OPENAT2, + self.root.as_raw_fd(), + path.as_ptr(), + &how as *const OpenHow, + size_of::(), + ) + }; + if fd < 0 { + return Err(io::Error::last_os_error()); + } + // SAFETY: a freshly returned descriptor that nothing else owns. + Ok(unsafe { std::os::fd::FromRawFd::from_raw_fd(fd as i32) }) + } + + fn resolve_walk(&self, path: &Path, mode: Mode) -> io::Result { + let mut pending = components(path); + let mut current = self.reopen_root()?; + let mut symlinks = 0; + loop { + let Some(component) = pending.pop_front() else { + // The path ended on a directory: `/`, `a/.`, `a/..`. + return match mode { + Mode::Entry => Err(errno(libc::EBUSY)), + Mode::Object { .. } => Ok(Resolved::Object(current)), + }; + }; + if component == "." { + continue; + } + if component == ".." { + if !self.is_root(current.as_fd())? { + current = open_path(current.as_fd(), OsStr::new(".."), libc::O_DIRECTORY)?; + } + continue; + } + + let last = pending.is_empty(); + let name = sys::cstring(component.as_bytes())?; + if last && matches!(mode, Mode::Entry) { + return Ok(Resolved::Entry { + parent: current, + name, + }); + } + let next = match open_path(current.as_fd(), &component, libc::O_NOFOLLOW) { + Ok(next) => next, + Err(error) if last && error.raw_os_error() == Some(libc::ENOENT) => { + return Ok(Resolved::Entry { + parent: current, + name, + }); + } + Err(error) => return Err(error), + }; + let follow = !last || matches!(mode, Mode::Object { follow: true }); + let is_symlink = sys::fstat(next.as_fd())?.st_mode & libc::S_IFMT == libc::S_IFLNK; + if !(is_symlink && follow) { + if last { + return Ok(Resolved::Object(next)); + } + current = next; + continue; + } + + symlinks += 1; + if symlinks > MAX_SYMLINKS { + return Err(errno(libc::ELOOP)); + } + if is_procfs(current.as_fd())? { + let directory = sys::fd_path(current.as_fd())?; + if directory == Path::new("/proc") { + // Ordinary symlinks, some relative to `self` + // (`mounts -> self/mounts`). `self` itself is rewritten + // to the target, since the kernel would answer for us. + let target = match component.as_bytes() { + b"self" => OsString::from((self.tgid)()?.to_string()), + b"thread-self" => { + OsString::from(format!("{}/task/{}", (self.tgid)()?, self.tid)) + } + _ => readlink(next.as_fd())?.into_os_string(), + }; + prepend(&mut pending, Path::new(&target)); + continue; + } + // Inside `/proc/`, symlinks are magic links (`cwd`, + // `exe`, `fd/N`, ...) that jump straight to an object. Follow + // them only for our own processes; the kernel would let us + // (as the supervisor) reach other processes' objects too. + if !proc_path_pid(&directory).is_some_and(|pid| self.tree.contains(pid)) { + return Err(errno(libc::EACCES)); + } + let target = open_path(current.as_fd(), &component, 0)?; + if last { + return Ok(Resolved::Object(target)); + } + current = target; + continue; + } + let target = readlink(next.as_fd())?; + if target.is_absolute() { + current = self.reopen_root()?; + } + prepend(&mut pending, &target); + } + } + + fn reopen_root(&self) -> io::Result { + open_path(self.root, OsStr::new("."), libc::O_DIRECTORY) + } + + fn is_root(&self, directory: BorrowedFd<'_>) -> io::Result { + let directory = sys::fstat(directory)?; + let root = sys::fstat(self.root)?; + Ok((directory.st_dev, directory.st_ino) == (root.st_dev, root.st_ino)) + } +} + +fn components(path: &Path) -> VecDeque { + let mut pending = VecDeque::new(); + prepend(&mut pending, path); + pending +} + +fn prepend(pending: &mut VecDeque, path: &Path) { + let names: Vec = path + .components() + .filter_map(|component| match component { + Component::Normal(name) => Some(name.to_os_string()), + Component::ParentDir => Some(OsString::from("..")), + Component::RootDir | Component::CurDir | Component::Prefix(_) => None, + }) + .collect(); + for name in names.into_iter().rev() { + pending.push_front(name); + } +} + +fn open_path(directory: BorrowedFd<'_>, name: &OsStr, flags: i32) -> io::Result { + sys::openat( + directory.as_raw_fd(), + &sys::cstring(name.as_bytes())?, + libc::O_PATH | flags, + 0, + ) +} + +/// The content of the symlink `link` (an `O_PATH | O_NOFOLLOW` fd). +fn readlink(link: BorrowedFd<'_>) -> io::Result { + let mut buffer = vec![0u8; libc::PATH_MAX as usize]; + // SAFETY: an empty path with a symlink fd reads that symlink; the buffer + // is valid for its length. + let length = unsafe { + libc::readlinkat( + link.as_raw_fd(), + c"".as_ptr(), + buffer.as_mut_ptr().cast(), + buffer.len(), + ) + }; + if length < 0 { + return Err(io::Error::last_os_error()); + } + buffer.truncate(length as usize); + Ok(OsString::from(OsStr::from_bytes(&buffer)).into()) +} + +fn is_procfs(directory: BorrowedFd<'_>) -> io::Result { + // SAFETY: plain old data, filled in by the kernel. + let mut stat: libc::statfs = unsafe { std::mem::zeroed() }; + // SAFETY: valid fd and pointer. + sys::cvt(unsafe { libc::fstatfs(directory.as_raw_fd(), &mut stat) })?; + Ok(stat.f_type as i64 == PROC_SUPER_MAGIC) +} diff --git a/crates/sandbox_linux/src/sandbox_linux.rs b/crates/sandbox_linux/src/sandbox_linux.rs new file mode 100644 index 00000000000..0d463f537eb --- /dev/null +++ b/crates/sandbox_linux/src/sandbox_linux.rs @@ -0,0 +1,703 @@ +//! Linux sandboxing by syscall interception. +//! +//! - [`SandboxContext`] owns a supervisor process (this binary, re-executed +//! with a hidden flag). +//! - [`SandboxContext::create_sandbox`] registers a [`SandboxPolicy`] with +//! the supervisor. [`Sandbox::update_policy`] replaces it while commands +//! run. +//! - [`Sandbox::execute`] spawns the helper (also this binary), which sets up +//! namespaces, installs a seccomp filter whose notifications go to the +//! supervisor, and runs the command. +//! +//! The sandbox sees the host filesystem at the same paths, mounted read-only. +//! Every open and every write-intent syscall goes to the supervisor, which +//! checks the policy and performs the operation itself. +//! +//! Each command gets a network namespace from which nothing outside is +//! reachable; the supervisor answers its DNS queries and relays the TCP +//! connections and UDP flows the policy allows (see `net.rs`). +//! +//! The binary must call [`run_hidden_entrypoint_if_invoked`] first thing in +//! `main`. + +#![cfg(target_os = "linux")] + +mod broker; +mod dns; +mod helper; +mod net; +mod policy; +mod process; +mod protocol; +mod resolve; +mod seccomp; +mod supervisor; +mod sys; + +use std::collections::{HashMap, HashSet}; +use std::os::fd::{AsFd as _, AsRawFd as _, OwnedFd, RawFd}; +use std::os::unix::process::CommandExt as _; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Output, Stdio}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, mpsc}; +use std::thread::JoinHandle; + +use anyhow::{Context as _, Result, anyhow, bail}; +use nix::sys::socket::{AddressFamily, SockFlag, SockType, socketpair}; +use serde::{Deserialize, Serialize}; + +pub use policy::{ + Access, AccessRequest, AddressClass, Decision, DomainPattern, FsRule, Handler, IpNet, NetHost, + NetRule, NetTarget, PathPattern, Permission, PortSet, Protocol, RequestedAccess, Rule, + SandboxPolicy, +}; +pub use protocol::{Denial, OpenFile}; + +use policy::{HandlerId, WireAccess, WireFsRule, WirePolicy}; +use protocol::{ + HelperSetup, PromptId, Request, RequestId, Response, SandboxId, SupervisorMessage, ZedMessage, +}; + +/// A command to run inside a sandbox. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct UntrustedCommand { + pub cmd: String, + pub args: Vec, + pub env: HashMap, + pub cwd: PathBuf, +} + +/// Answers one [`AccessRequest`] passed to a [`Handler`]. Answer from any +/// thread, at any time. +/// +/// Every request must be answered with [`Responder::respond`]: a decision +/// usually also remembers a rule, and silently dropping the responder would +/// lose that. Dropping it unanswered is treated as a bug: it panics in debug +/// builds, and logs a warning and denies in release builds. Dropping is fine +/// once [`Responder::is_cancelled`] is true, or while unwinding from a panic +/// (both deny). +pub struct Responder { + prompt: PromptId, + sandbox: SandboxId, + handlers: Arc, + control: Arc, + cancelled: Arc, + answered: bool, +} + +impl Responder { + pub fn respond(mut self, decision: Decision) { + self.send(decision); + } + + /// Whether the blocked syscall was abandoned (e.g. the process was + /// killed) or timed out, so an answer would no longer have any effect. + pub fn is_cancelled(&self) -> bool { + self.cancelled.load(Ordering::SeqCst) + } + + fn send(&mut self, decision: Decision) { + if std::mem::replace(&mut self.answered, true) { + return; + } + self.handlers.forget_prompt(self.prompt); + let mut register = |handler: &Handler| self.handlers.register(self.sandbox, handler); + let remember = decision + .remember + .iter() + .map(|rule| rule.to_wire(&mut register)) + .collect(); + let reply = ZedMessage::PromptReply { + prompt: self.prompt, + allow: decision.allow, + remember, + }; + if let Err(error) = protocol::send_message(self.control.as_fd(), &reply, &[]) { + eprintln!("zed sandbox: answering prompt failed: {error:#}"); + } + } +} + +impl Drop for Responder { + fn drop(&mut self) { + if self.answered { + return; + } + let is_bug = !self.is_cancelled() && !std::thread::panicking(); + // Deny before panicking, so the syscall isn't left blocked until the + // prompt times out. + self.send(Decision::deny()); + if is_bug { + if cfg!(debug_assertions) { + panic!("a sandbox Responder was dropped without responding"); + } + log::warn!("[sandbox] a Responder was dropped without responding; denying"); + } + } +} + +/// Every sandbox's handlers, by the id the supervisor refers to them with. +#[derive(Default)] +struct HandlerTable { + state: Mutex, +} + +type DenialHandler = Arc; + +#[derive(Default)] +struct HandlerTableState { + handlers: HashMap, + denial_handlers: HashMap, + /// Unanswered prompts, so a cancellation can reach their responders. + prompts: HashMap>, + next: HandlerId, +} + +impl HandlerTable { + fn state(&self) -> std::sync::MutexGuard<'_, HandlerTableState> { + match self.state.lock() { + Ok(state) => state, + Err(poisoned) => poisoned.into_inner(), + } + } + + /// The id of `handler` for `sandbox`, reusing the existing one if it was + /// registered before. + fn register(&self, sandbox: SandboxId, handler: &Handler) -> HandlerId { + let mut state = self.state(); + let existing = state + .handlers + .iter() + .find(|(_, (owner, known))| *owner == sandbox && Arc::ptr_eq(&known.0, &handler.0)); + if let Some((&id, _)) = existing { + return id; + } + let id = state.next; + state.next += 1; + state.handlers.insert(id, (sandbox, handler.clone())); + id + } + + fn get(&self, id: HandlerId) -> Option<(SandboxId, Handler)> { + self.state().handlers.get(&id).cloned() + } + + /// Drop `sandbox`'s handlers except those in `keep`. + fn retain(&self, sandbox: SandboxId, keep: &HashSet) { + self.state() + .handlers + .retain(|id, (owner, _)| *owner != sandbox || keep.contains(id)); + } + + fn denial_handler(&self, sandbox: SandboxId) -> Option { + self.state().denial_handlers.get(&sandbox).cloned() + } + + fn track_prompt(&self, prompt: PromptId) -> Arc { + let cancelled = Arc::new(AtomicBool::new(false)); + self.state().prompts.insert(prompt, cancelled.clone()); + cancelled + } + + fn forget_prompt(&self, prompt: PromptId) { + self.state().prompts.remove(&prompt); + } + + fn cancel_prompt(&self, prompt: PromptId) { + if let Some(cancelled) = self.state().prompts.remove(&prompt) { + cancelled.store(true, Ordering::SeqCst); + } + } + + /// The supervisor is gone: no answer can have an effect any more. + fn cancel_all_prompts(&self) { + for (_, cancelled) in self.state().prompts.drain() { + cancelled.store(true, Ordering::SeqCst); + } + } +} + +/// Run the supervisor or helper if this process was started as one; never +/// returns in that case. Call before anything else in `main`: the helper's +/// command line must not be interpreted as the host program's own. +pub fn run_hidden_entrypoint_if_invoked() { + match std::env::args_os() + .nth(1) + .as_deref() + .and_then(|flag| flag.to_str()) + { + Some(supervisor::SUPERVISOR_FLAG) => run_supervisor(), + Some(helper::HELPER_FLAG) => helper::run(), + _ => {} + } +} + +/// The supervisor process's main loop. Expects its control socket at fd 3. +pub fn run_supervisor() -> ! { + supervisor::run() +} + +/// Owns the supervisor process. Dropping it stops the supervisor; commands +/// still running then fail every trapped syscall with `ENOSYS`. +pub struct SandboxContext { + inner: Arc, +} + +struct ContextInner { + executable: PathBuf, + control: Arc, + supervisor: Mutex, + reader: Mutex>>, + pending: Arc>>>, + handlers: Arc, + next_request: AtomicU64, + next_sandbox: AtomicU64, +} + +impl SandboxContext { + /// Start a supervisor by re-executing the current binary. + pub fn new() -> Result { + let executable = std::env::current_exe().context("locating the current executable")?; + Self::with_executable(executable) + } + + /// Start a supervisor using `executable`, which must call + /// [`run_hidden_entrypoint_if_invoked`]. + #[allow( + clippy::disallowed_methods, + reason = "PoC: blocking spawn; callers must stay off the UI thread" + )] + pub fn with_executable(executable: PathBuf) -> Result { + let (ours, theirs) = seqpacket_pair()?; + let mut command = Command::new(&executable); + command + .arg(supervisor::SUPERVISOR_FLAG) + .stdin(Stdio::null()); + let child_fds = inherit_fds(&mut command, &[(theirs, supervisor::CONTROL_FD)])?; + let supervisor = command.spawn().context("starting the sandbox supervisor")?; + drop(child_fds); + + let control = Arc::new(ours); + let pending: Arc>>> = Arc::default(); + let handlers: Arc = Arc::default(); + let reader = std::thread::Builder::new() + .name("zed-sandbox-control".to_string()) + .spawn({ + let control = control.clone(); + let pending = pending.clone(); + let handlers = handlers.clone(); + move || read_control(&control, &pending, &handlers) + }) + .context("starting the sandbox control thread")?; + Ok(Self { + inner: Arc::new(ContextInner { + executable, + control, + supervisor: Mutex::new(supervisor), + reader: Mutex::new(Some(reader)), + pending, + handlers, + next_request: AtomicU64::new(0), + next_sandbox: AtomicU64::new(0), + }), + }) + } + + pub fn create_sandbox(&self, policy: SandboxPolicy) -> Result { + let id = self.inner.next_sandbox.fetch_add(1, Ordering::Relaxed); + let temp_dir = create_temp_dir()?; + // Dropping `sandbox` on an error below cleans up its directory and + // handlers without asking the supervisor to destroy it. + let mut sandbox = Sandbox { + context: self.inner.clone(), + id, + temp_dir, + created: false, + }; + let policy = sandbox.to_wire(&policy)?; + self.inner.call( + Request::CreateSandbox { + sandbox: id, + policy, + }, + &[], + )?; + sandbox.created = true; + Ok(sandbox) + } +} + +fn read_control( + control: &Arc, + pending: &Mutex>>, + handlers: &Arc, +) { + loop { + let message = match protocol::receive_message::(control.as_fd()) { + Ok(Some((message, _))) => message, + Ok(None) => break, + Err(error) => { + eprintln!("zed sandbox: reading from supervisor failed: {error:#}"); + break; + } + }; + match message { + SupervisorMessage::Response { id, response } => { + let sender = pending + .lock() + .ok() + .and_then(|mut pending| pending.remove(&id)); + if let Some(sender) = sender { + sender.send(response).ok(); + } + } + SupervisorMessage::Prompt(request) => { + // An unknown (or another sandbox's) handler, e.g. one a policy + // update just removed: deny. + let handler = handlers + .get(request.handler) + .filter(|(owner, _)| *owner == request.sandbox); + let Some((_, handler)) = handler else { + let reply = ZedMessage::PromptReply { + prompt: request.prompt, + allow: false, + remember: Vec::new(), + }; + if let Err(error) = protocol::send_message(control.as_fd(), &reply, &[]) { + eprintln!("zed sandbox: answering prompt failed: {error:#}"); + } + continue; + }; + let responder = Responder { + prompt: request.prompt, + sandbox: request.sandbox, + handlers: handlers.clone(), + control: control.clone(), + cancelled: handlers.track_prompt(request.prompt), + answered: false, + }; + let access_request = AccessRequest { + items: request.requests, + pid: request.pid, + executable: request.executable, + }; + // A panicking handler drops the responder while unwinding, + // which denies; keep this thread alive for everyone else. + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + (handler.0)(access_request, responder) + })); + if result.is_err() { + eprintln!("zed sandbox: an access handler panicked; denying"); + } + } + SupervisorMessage::PromptCancelled { prompt } => handlers.cancel_prompt(prompt), + SupervisorMessage::Denied { sandbox, denial } => { + if let Some(handler) = handlers.denial_handler(sandbox) { + let result = + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| handler(denial))); + if result.is_err() { + eprintln!("zed sandbox: a denial handler panicked"); + } + } + } + } + } + // Fail anyone still waiting for a response. + if let Ok(mut pending) = pending.lock() { + pending.clear(); + } + handlers.cancel_all_prompts(); +} + +impl ContextInner { + fn call(&self, request: Request, fds: &[RawFd]) -> Result { + let id = self.next_request.fetch_add(1, Ordering::Relaxed); + let (sender, receiver) = mpsc::channel(); + self.pending + .lock() + .map_err(|_| anyhow!("sandbox state poisoned"))? + .insert(id, sender); + protocol::send_message( + self.control.as_fd(), + &ZedMessage::Request { id, request }, + fds, + )?; + match receiver.recv().context("sandbox supervisor exited")? { + Response::Error { message } => Err(anyhow!(message)), + response => Ok(response), + } + } +} + +impl Drop for ContextInner { + fn drop(&mut self) { + // The supervisor exits on EOF. Shut down rather than close: the + // control thread (and any unanswered prompt) shares the socket. + // SAFETY: shutdown on a socket we own. + unsafe { libc::shutdown(self.control.as_raw_fd(), libc::SHUT_WR) }; + if let Ok(mut supervisor) = self.supervisor.lock() + && let Err(error) = supervisor.wait() + { + eprintln!("zed sandbox: waiting for supervisor failed: {error}"); + } + if let Some(reader) = self.reader.lock().ok().and_then(|mut reader| reader.take()) { + reader.join().ok(); + } + } +} + +/// A policy registered with the supervisor. Commands run in it share the +/// policy (and its updates), but each gets its own namespaces. +/// +/// Each sandbox has a private temporary directory, passed to commands as +/// `TMPDIR` and usable (read, write, connect) regardless of the policy. +/// Device files commands commonly need (`/dev/null`, terminals, ...) are readable and writable +/// unless the policy says otherwise. +pub struct Sandbox { + context: Arc, + id: SandboxId, + temp_dir: PathBuf, + /// Whether the supervisor knows about this sandbox. + created: bool, +} + +/// Device files a sandbox may use unless its policy overrides them. +const DEFAULT_DEVICES: [&str; 8] = [ + "/dev/null", + "/dev/zero", + "/dev/full", + "/dev/random", + "/dev/urandom", + "/dev/tty", + "/dev/ptmx", + "/dev/pts", +]; + +impl Sandbox { + pub fn temp_dir(&self) -> &Path { + &self.temp_dir + } + + /// Call `handler` whenever the policy, or an [`Access::Ask`] handler, + /// denies an operation in this sandbox (after the syscall has failed). + /// Like handlers, it runs on a background thread shared by every + /// sandbox in the context and must return promptly. Replaces any + /// previous one. + pub fn on_denial(&self, handler: impl Fn(Denial) + Send + Sync + 'static) { + self.context + .handlers + .state() + .denial_handlers + .insert(self.id, Arc::new(handler)); + } + + /// Replace the policy, including any rules handlers asked to remember. + /// Applies to every operation from now on, including in commands already + /// running; descriptors they already hold keep working, and are returned + /// if the new policy wouldn't grant them. + pub fn update_policy(&self, policy: &SandboxPolicy) -> Result> { + let policy = self.to_wire(policy)?; + let request = Request::UpdatePolicy { + sandbox: self.id, + policy, + }; + let still_open = match self.context.call(request, &[])? { + Response::PolicyUpdated { still_open } => still_open, + other => bail!("unexpected supervisor response: {other:?}"), + }; + Ok(still_open) + } + + /// The policy as the supervisor enforces it, registering its handlers: + /// default devices first (so the policy can override them), the + /// temporary directory last (so it can't). + /// + /// Handlers no longer referenced are dropped, which denies any prompt + /// still waiting on them. + fn to_wire(&self, policy: &SandboxPolicy) -> Result { + let handlers = &self.context.handlers; + let mut used = HashSet::new(); + let mut register = |handler: &Handler| { + let id = handlers.register(self.id, handler); + used.insert(id); + id + }; + let allow_all = |path: PathPattern| WireFsRule { + path, + read: Some(WireAccess::Allow), + write: Some(WireAccess::Allow), + connect: Some(WireAccess::Allow), + }; + let mut fs = Vec::with_capacity(policy.fs.len() + DEFAULT_DEVICES.len() + 1); + for device in DEFAULT_DEVICES { + fs.push(allow_all(PathPattern::new(device)?)); + } + fs.extend(policy.fs.iter().map(|rule| rule.to_wire(&mut register))); + fs.push(allow_all(PathPattern::literal(&self.temp_dir)?)); + let net = policy + .net + .iter() + .map(|rule| rule.to_wire(&mut register)) + .collect(); + handlers.retain(self.id, &used); + Ok(WirePolicy { fs, net }) + } + + /// Spawn `command` in this sandbox with inherited stdio. + pub fn execute(&self, command: &UntrustedCommand) -> Result { + Ok(self.prepare(command)?.spawn()?) + } + + /// Prepare `command` without spawning it. The supervisor is told about + /// the execution now; if it's never spawned, the supervisor forgets it + /// once the returned value is dropped. + pub fn prepare(&self, command: &UntrustedCommand) -> Result { + // The helper hands its seccomp listener to the supervisor over this + // pair; the supervisor learns which sandbox it belongs to from the + // request that delivered the other end. + let (supervisor_end, helper_attach) = seqpacket_pair()?; + self.context.call( + Request::AttachExecution { sandbox: self.id }, + &[supervisor_end.as_raw_fd()], + )?; + drop(supervisor_end); + + let mut command = command.clone(); + command.env.insert( + "TMPDIR".to_string(), + self.temp_dir.to_string_lossy().into_owned(), + ); + // The setup message sits in the socket buffer until the helper reads + // it, so it can be sent before spawning. + let (zed_setup, helper_setup) = seqpacket_pair()?; + protocol::send_message(zed_setup.as_fd(), &HelperSetup { command }, &[])?; + drop(zed_setup); + + let mut process = Command::new(&self.context.executable); + process.arg(helper::HELPER_FLAG); + let child_fds = inherit_fds( + &mut process, + &[ + (helper_setup, helper::SETUP_FD), + (helper_attach, helper::ATTACH_FD), + ], + )?; + Ok(SandboxedCommand { + command: process, + _child_fds: child_fds, + }) + } +} + +impl Drop for Sandbox { + fn drop(&mut self) { + self.context.handlers.retain(self.id, &HashSet::new()); + if self.created + && let Err(error) = self + .context + .call(Request::DestroySandbox { sandbox: self.id }, &[]) + { + eprintln!("zed sandbox: destroying sandbox failed: {error:#}"); + } + // Only now: the destroy round trip went through the control thread + // after every denial the supervisor sent before it, so all of them + // have been delivered. + self.context + .handlers + .state() + .denial_handlers + .remove(&self.id); + if let Err(error) = std::fs::remove_dir_all(&self.temp_dir) { + eprintln!( + "zed sandbox: removing {} failed: {error}", + self.temp_dir.display() + ); + } + } +} + +/// A sandboxed command that hasn't been spawned yet. Configure stdio through +/// [`SandboxedCommand::command_mut`]; don't change its program or arguments. +pub struct SandboxedCommand { + command: Command, + _child_fds: Vec, +} + +impl SandboxedCommand { + pub fn command_mut(&mut self) -> &mut Command { + &mut self.command + } + + #[allow( + clippy::disallowed_methods, + reason = "PoC: blocking spawn; callers must stay off the UI thread" + )] + pub fn spawn(mut self) -> std::io::Result { + self.command.spawn() + } + + #[allow( + clippy::disallowed_methods, + reason = "PoC: blocking spawn; callers must stay off the UI thread" + )] + pub fn output(mut self) -> std::io::Result { + self.command.output() + } +} + +fn create_temp_dir() -> Result { + use std::os::unix::fs::DirBuilderExt as _; + static COUNTER: AtomicU64 = AtomicU64::new(0); + let base = std::env::temp_dir() + .canonicalize() + .context("resolving the temporary directory")?; + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.subsec_nanos()); + let path = base.join(format!( + "zed-sandbox-{}-{}-{nanos}", + std::process::id(), + COUNTER.fetch_add(1, Ordering::Relaxed) + )); + std::fs::DirBuilder::new() + .mode(0o700) + .create(&path) + .with_context(|| format!("creating {}", path.display()))?; + Ok(path) +} + +fn seqpacket_pair() -> Result<(OwnedFd, OwnedFd)> { + socketpair( + AddressFamily::Unix, + SockType::SeqPacket, + None, + SockFlag::SOCK_CLOEXEC, + ) + .context("creating sandbox socketpair") +} + +/// Arrange for each `(fd, number)` to be inherited by `command` at `number`. +/// Returns the descriptors the caller must keep open until spawning. +fn inherit_fds(command: &mut Command, fds: &[(OwnedFd, RawFd)]) -> Result> { + // Move every source above the target range first, so one `dup2` can't + // clobber another source. + let mut moved = Vec::new(); + let mut mapping = Vec::new(); + for (fd, number) in fds { + let high = sys::duplicate_above(fd.as_fd(), 64)?; + mapping.push((high.as_raw_fd(), *number)); + moved.push(high); + } + // SAFETY: only async-signal-safe `dup2` calls between fork and exec. + unsafe { + command.pre_exec(move || { + for (source, target) in &mapping { + // `dup2` leaves the new descriptor without CLOEXEC. + sys::cvt(libc::dup2(*source, *target))?; + } + Ok(()) + }); + } + Ok(moved) +} diff --git a/crates/sandbox_linux/src/seccomp.rs b/crates/sandbox_linux/src/seccomp.rs new file mode 100644 index 00000000000..0abc6564fb9 --- /dev/null +++ b/crates/sandbox_linux/src/seccomp.rs @@ -0,0 +1,487 @@ +//! The seccomp filter installed on the untrusted command. +//! +//! It sends to the supervisor (`SECCOMP_RET_USER_NOTIF`): +//! - every open except `O_PATH` (read rules need the supervisor to decide); +//! - write-intent filesystem syscalls, decided from register arguments; +//! - syscalls that act on another process by pid; +//! - `connect` and `bind`. +//! +//! It denies syscalls the sandbox never needs (non-IP sockets, new user +//! namespaces, io_uring, ptrace, ...) and returns `ENOSYS` for every syscall +//! newer than the ones listed here, so a future kernel's additions are +//! unavailable rather than unreviewed. + +use std::io; +use std::os::fd::{FromRawFd as _, OwnedFd, RawFd}; + +use crate::sys::cvt; + +const SECCOMP_FILTER_FLAG_NEW_LISTENER: libc::c_ulong = 1 << 3; +const SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV: libc::c_ulong = 1 << 5; +const SECCOMP_RET_USER_NOTIF: u32 = 0x7fc0_0000; +const SECCOMP_RET_KILL_PROCESS: u32 = 0x8000_0000; + +// Syscalls added after the per-arch tables were unified share one number. +const SYS_PIDFD_OPEN: i64 = 434; +const SYS_CLONE3: i64 = 435; +const SYS_OPENAT2: i64 = 437; +const SYS_FACCESSAT2: i64 = 439; +const SYS_PROCESS_MADVISE: i64 = 440; +const SYS_FCHMODAT2: i64 = 452; +/// The first syscall number this filter hasn't been reviewed against +/// (`setxattrat`, Linux 6.13). +const FIRST_UNREVIEWED_SYSCALL: u32 = 463; + +#[cfg(target_arch = "x86_64")] +const AUDIT_ARCH: u32 = 0xc000_003e; +#[cfg(target_arch = "aarch64")] +const AUDIT_ARCH: u32 = 0xc000_00b7; +#[cfg(target_arch = "x86_64")] +const X32_SYSCALL_BIT: u32 = 0x4000_0000; + +/// The syscalls the supervisor handles. Anything else that writes (e.g. +/// `fchownat`, `setxattr`, `mknodat`) isn't trapped yet and fails against the +/// read-only mounts. TODO: broker every write-capable path syscall. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Brokered { + OpenAt, + #[cfg(target_arch = "x86_64")] + Open, + #[cfg(target_arch = "x86_64")] + Creat, + MkdirAt, + #[cfg(target_arch = "x86_64")] + Mkdir, + UnlinkAt, + #[cfg(target_arch = "x86_64")] + Unlink, + #[cfg(target_arch = "x86_64")] + Rmdir, + #[cfg(target_arch = "x86_64")] + RenameAt, + RenameAt2, + #[cfg(target_arch = "x86_64")] + Rename, + LinkAt, + #[cfg(target_arch = "x86_64")] + Link, + SymlinkAt, + #[cfg(target_arch = "x86_64")] + Symlink, + FchmodAt, + #[cfg(target_arch = "x86_64")] + Chmod, + Truncate, + FaccessAt, + FaccessAt2, + #[cfg(target_arch = "x86_64")] + Access, + UtimensAt, + Kill, + Tkill, + Tgkill, + RtSigqueueinfo, + RtTgsigqueueinfo, + PidfdOpen, + // Argument 0 is a pid, with 0 meaning the caller. + Prlimit64, + SchedSetaffinity, + SchedSetscheduler, + SchedSetparam, + SchedSetattr, + Setpriority, + IoprioSet, + Connect, + Bind, +} + +/// What the filter does with one syscall number. +#[derive(Clone, Copy)] +enum Action { + Return(u32), + /// Do `matched` if argument `arg`, masked with `mask`, equals one of + /// `values`, else do `otherwise`. + ArgumentIn { + arg: usize, + mask: u32, + values: &'static [u32], + matched: &'static Action, + otherwise: &'static Action, + }, + /// Return `set` if argument `arg` has any bit of `mask` set. + ArgumentHasBits { + arg: usize, + mask: u32, + set: u32, + clear: u32, + }, + /// Return `non_null` if the 64-bit argument `arg` is non-zero. + ArgumentNonNull { + arg: usize, + non_null: u32, + null: u32, + }, +} + +const ALLOW: u32 = libc::SECCOMP_RET_ALLOW; +const NOTIFY: u32 = SECCOMP_RET_USER_NOTIF; + +const fn errno(code: i32) -> u32 { + libc::SECCOMP_RET_ERRNO | (code as u32 & libc::SECCOMP_RET_DATA) +} + +/// `O_PATH` opens grant no access to contents; every other open is brokered. +const fn opens_contents(arg: usize) -> Action { + Action::ArgumentHasBits { + arg, + mask: libc::O_PATH as u32, + set: ALLOW, + clear: NOTIFY, + } +} + +const fn checks_write_access(arg: usize) -> Action { + Action::ArgumentHasBits { + arg, + mask: libc::W_OK as u32, + set: NOTIFY, + clear: ALLOW, + } +} + +const ALWAYS_NOTIFY: Action = Action::Return(NOTIFY); + +/// Syscalls the supervisor handles, with when to send them to it. +const BROKERED: &[(i64, Brokered, Action)] = { + use Brokered::*; + let always = ALWAYS_NOTIFY; + &[ + (libc::SYS_openat, OpenAt, opens_contents(2)), + (libc::SYS_mkdirat, MkdirAt, always), + (libc::SYS_unlinkat, UnlinkAt, always), + (libc::SYS_renameat2, RenameAt2, always), + (libc::SYS_linkat, LinkAt, always), + (libc::SYS_symlinkat, SymlinkAt, always), + (libc::SYS_fchmodat, FchmodAt, always), + (libc::SYS_truncate, Truncate, always), + (libc::SYS_faccessat, FaccessAt, checks_write_access(2)), + (SYS_FACCESSAT2, FaccessAt2, checks_write_access(2)), + // `utimensat(fd, NULL, ...)` is `futimens` on an already-open fd. + ( + libc::SYS_utimensat, + UtimensAt, + Action::ArgumentNonNull { + arg: 1, + non_null: NOTIFY, + null: ALLOW, + }, + ), + (libc::SYS_kill, Kill, always), + (libc::SYS_tkill, Tkill, always), + (libc::SYS_tgkill, Tgkill, always), + (libc::SYS_rt_sigqueueinfo, RtSigqueueinfo, always), + (libc::SYS_rt_tgsigqueueinfo, RtTgsigqueueinfo, always), + (SYS_PIDFD_OPEN, PidfdOpen, always), + (libc::SYS_prlimit64, Prlimit64, always), + (libc::SYS_sched_setaffinity, SchedSetaffinity, always), + (libc::SYS_sched_setscheduler, SchedSetscheduler, always), + (libc::SYS_sched_setparam, SchedSetparam, always), + (libc::SYS_sched_setattr, SchedSetattr, always), + (libc::SYS_setpriority, Setpriority, always), + (libc::SYS_ioprio_set, IoprioSet, always), + (libc::SYS_connect, Connect, always), + (libc::SYS_bind, Bind, always), + ] +}; + +/// The pre-`*at` path syscalls that only x86_64 still has. +#[cfg(target_arch = "x86_64")] +const BROKERED_LEGACY: &[(i64, Brokered, Action)] = { + use Brokered::*; + let always = ALWAYS_NOTIFY; + &[ + (libc::SYS_open, Open, opens_contents(1)), + (libc::SYS_creat, Creat, always), + (libc::SYS_mkdir, Mkdir, always), + (libc::SYS_unlink, Unlink, always), + (libc::SYS_rmdir, Rmdir, always), + (libc::SYS_renameat, RenameAt, always), + (libc::SYS_rename, Rename, always), + (libc::SYS_link, Link, always), + (libc::SYS_symlink, Symlink, always), + (libc::SYS_chmod, Chmod, always), + (libc::SYS_access, Access, checks_write_access(1)), + ] +}; + +#[cfg(not(target_arch = "x86_64"))] +const BROKERED_LEGACY: &[(i64, Brokered, Action)] = &[]; + +fn brokered_entries() -> impl Iterator { + BROKERED.iter().chain(BROKERED_LEGACY) +} + +pub(crate) fn brokered_syscall(number: i64) -> Option { + brokered_entries() + .find_map(|&(candidate, brokered, _)| (candidate == number).then_some(brokered)) +} + +const DENIED_WITH_EPERM: Action = Action::Return(errno(libc::EPERM)); +const DENIED_WITH_ENOSYS: Action = Action::Return(errno(libc::ENOSYS)); + +/// Syscalls the sandbox never needs, with how they're refused. +const DENIED: &[(i64, Action)] = { + const IP_FAMILIES: &[u32] = &[ + libc::AF_INET as u32, + libc::AF_INET6 as u32, + libc::AF_NETLINK as u32, + ]; + const UNIX_FAMILY: &[u32] = &[libc::AF_UNIX as u32]; + const ICMP_PROTOCOLS: &[u32] = &[libc::IPPROTO_ICMP as u32, libc::IPPROTO_ICMPV6 as u32]; + const DATAGRAM: &[u32] = &[libc::SOCK_DGRAM as u32]; + /// The type argument without `SOCK_NONBLOCK` / `SOCK_CLOEXEC`. + const SOCKET_TYPE_MASK: u32 = 0xf; + const ALLOWED: &Action = &Action::Return(ALLOW); + const REFUSED: &Action = &DENIED_WITH_EPERM; + // A datagram unix socket can send to any socket by path with + // `sendmsg`, whose address sits behind a pointer; trapping every + // `sendmsg` would slow all UDP. Stream and seqpacket sockets only reach + // a path through `connect`, which is brokered. + const UNIX_UNLESS_DATAGRAM: &Action = &Action::ArgumentIn { + arg: 1, + mask: SOCKET_TYPE_MASK, + values: DATAGRAM, + matched: REFUSED, + otherwise: ALLOWED, + }; + const NEW_USER_NAMESPACE: Action = Action::ArgumentHasBits { + arg: 0, + mask: libc::CLONE_NEWUSER as u32, + set: errno(libc::EPERM), + clear: ALLOW, + }; + &[ + ( + libc::SYS_socket, + Action::ArgumentIn { + // Ping sockets: every address is local in the sandbox, so + // every ping would get an answer. + arg: 2, + mask: u32::MAX, + values: ICMP_PROTOCOLS, + matched: &Action::Return(errno(libc::EACCES)), + otherwise: &Action::ArgumentIn { + arg: 0, + mask: u32::MAX, + values: IP_FAMILIES, + matched: ALLOWED, + otherwise: &Action::ArgumentIn { + arg: 0, + mask: u32::MAX, + values: UNIX_FAMILY, + matched: UNIX_UNLESS_DATAGRAM, + otherwise: REFUSED, + }, + }, + }, + ), + ( + libc::SYS_socketpair, + Action::ArgumentIn { + arg: 0, + mask: u32::MAX, + values: UNIX_FAMILY, + matched: UNIX_UNLESS_DATAGRAM, + otherwise: REFUSED, + }, + ), + // A nested user namespace could mount, creating new names for files + // the read rules deny. + (libc::SYS_unshare, NEW_USER_NAMESPACE), + (libc::SYS_clone, NEW_USER_NAMESPACE), + // Flags live behind a pointer; libc falls back to `clone`. + (SYS_CLONE3, DENIED_WITH_ENOSYS), + // Flags/modes live behind pointers; libc falls back to `openat` / + // `fchmodat`, which we can inspect. + (SYS_OPENAT2, DENIED_WITH_ENOSYS), + (SYS_FCHMODAT2, DENIED_WITH_ENOSYS), + (libc::SYS_io_uring_setup, DENIED_WITH_EPERM), + (libc::SYS_io_uring_enter, DENIED_WITH_EPERM), + (libc::SYS_io_uring_register, DENIED_WITH_EPERM), + (libc::SYS_ptrace, DENIED_WITH_EPERM), + (libc::SYS_process_vm_readv, DENIED_WITH_EPERM), + (libc::SYS_process_vm_writev, DENIED_WITH_EPERM), + (libc::SYS_setns, DENIED_WITH_EPERM), + (libc::SYS_perf_event_open, DENIED_WITH_EPERM), + (libc::SYS_kcmp, DENIED_WITH_EPERM), + (libc::SYS_migrate_pages, DENIED_WITH_EPERM), + (libc::SYS_move_pages, DENIED_WITH_EPERM), + (SYS_PROCESS_MADVISE, DENIED_WITH_EPERM), + ] +}; + +fn statement(code: u32, k: u32) -> libc::sock_filter { + libc::sock_filter { + code: code as u16, + jt: 0, + jf: 0, + k, + } +} + +fn jump(code: u32, k: u32, jt: u8, jf: u8) -> libc::sock_filter { + libc::sock_filter { + code: code as u16, + jt, + jf, + k, + } +} + +// Classic BPF opcodes. The filter is a tiny program over one register (the +// accumulator); each opcode ORs together an instruction class, an operand +// size and an addressing mode. + +/// `A = *(u32 *)(seccomp_data + k)`: load a 32-bit field of the syscall. +const LOAD_OPCODE: u32 = libc::BPF_LD | libc::BPF_W | libc::BPF_ABS; +/// `if A == k` jump `jt` instructions forward, else `jf`. +const JUMP_EQUAL_OPCODE: u32 = libc::BPF_JMP | libc::BPF_JEQ | libc::BPF_K; +/// `if A >= k` (unsigned) jump `jt`, else `jf`. +const JUMP_AT_LEAST_OPCODE: u32 = libc::BPF_JMP | libc::BPF_JGE | libc::BPF_K; +/// `if A & k != 0` jump `jt`, else `jf`. +const JUMP_ANY_BITS_OPCODE: u32 = libc::BPF_JMP | libc::BPF_JSET | libc::BPF_K; +/// `A &= k`. +const AND_OPCODE: u32 = libc::BPF_ALU | libc::BPF_AND | libc::BPF_K; +/// Stop and return `k`: the seccomp verdict (allow, errno, notify, kill). +const RETURN_OPCODE: u32 = libc::BPF_RET | libc::BPF_K; + +// Field offsets in `struct seccomp_data` (the program's input). +const NR_OFFSET: u32 = 0; +const ARCH_OFFSET: u32 = 4; + +/// Offset of the low 32 bits of argument `index` in `seccomp_data` (both +/// supported architectures are little-endian). +fn argument_offset(index: usize) -> u32 { + 16 + 8 * index as u32 +} + +fn compile_action(action: Action) -> Vec { + match action { + Action::Return(value) => vec![statement(RETURN_OPCODE, value)], + Action::ArgumentIn { + arg, + mask, + values, + matched, + otherwise, + } => { + // Comparisons, then `otherwise` (reached by falling through + // them all), then `matched`. Both end in a return. + let otherwise = compile_action(*otherwise); + let mut program = vec![statement(LOAD_OPCODE, argument_offset(arg))]; + if mask != u32::MAX { + program.push(statement(AND_OPCODE, mask)); + } + let count = values.len(); + for (index, value) in values.iter().enumerate() { + // Jump over the remaining comparisons and `otherwise`. + let skip = count - index - 1 + otherwise.len(); + program.push(jump(JUMP_EQUAL_OPCODE, *value, skip as u8, 0)); + } + program.extend(otherwise); + program.extend(compile_action(*matched)); + program + } + Action::ArgumentHasBits { + arg, + mask, + set, + clear, + } => vec![ + statement(LOAD_OPCODE, argument_offset(arg)), + jump(JUMP_ANY_BITS_OPCODE, mask, 0, 1), + statement(RETURN_OPCODE, set), + statement(RETURN_OPCODE, clear), + ], + Action::ArgumentNonNull { + arg, + non_null, + null, + } => vec![ + statement(LOAD_OPCODE, argument_offset(arg)), + jump(JUMP_EQUAL_OPCODE, 0, 0, 2), + statement(LOAD_OPCODE, argument_offset(arg) + 4), + jump(JUMP_EQUAL_OPCODE, 0, 1, 0), + statement(RETURN_OPCODE, non_null), + statement(RETURN_OPCODE, null), + ], + } +} + +pub(crate) fn build_filter() -> Vec { + let mut program = vec![ + statement(LOAD_OPCODE, ARCH_OFFSET), + jump(JUMP_EQUAL_OPCODE, AUDIT_ARCH, 1, 0), + statement(RETURN_OPCODE, SECCOMP_RET_KILL_PROCESS), + statement(LOAD_OPCODE, NR_OFFSET), + ]; + #[cfg(target_arch = "x86_64")] + program.extend([ + jump(JUMP_ANY_BITS_OPCODE, X32_SYSCALL_BIT, 0, 1), + statement(RETURN_OPCODE, SECCOMP_RET_KILL_PROCESS), + ]); + program.extend([ + jump(JUMP_AT_LEAST_OPCODE, FIRST_UNREVIEWED_SYSCALL, 0, 1), + statement(RETURN_OPCODE, errno(libc::ENOSYS)), + ]); + + let entries = DENIED + .iter() + .copied() + .chain(brokered_entries().map(|&(number, _, action)| (number, action))); + for (number, action) in entries { + // Every compiled action ends in a return, so the accumulator (the + // syscall number) is intact on the fall-through path. + let body = compile_action(action); + program.push(jump(JUMP_EQUAL_OPCODE, number as u32, 0, body.len() as u8)); + program.extend(body); + } + program.push(statement(RETURN_OPCODE, ALLOW)); + program +} + +/// Install `filter` on the calling thread and return the listener fd. Called +/// after `fork`, before `exec`. +pub(crate) fn install(filter: &[libc::sock_filter]) -> io::Result { + let program = libc::sock_fprog { + len: filter.len() as u16, + filter: filter.as_ptr().cast_mut(), + }; + // SAFETY: plain prctl. + cvt(unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) })?; + let install = |flags: libc::c_ulong| { + // SAFETY: `program` points at `filter`, which outlives the call. + unsafe { + libc::syscall( + libc::SYS_seccomp, + libc::SECCOMP_SET_MODE_FILTER, + flags, + &program as *const libc::sock_fprog, + ) + } + }; + // WAIT_KILLABLE_RECV (5.19): once the supervisor has picked up a + // notification, only a fatal signal can interrupt it. Otherwise a signal + // (e.g. Go's SIGURG preemption) arriving while we perform a mkdir or + // rename, or while a prompt is open, makes the target retry it. + let mut listener = + install(SECCOMP_FILTER_FLAG_NEW_LISTENER | SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV); + if listener < 0 && io::Error::last_os_error().raw_os_error() == Some(libc::EINVAL) { + listener = install(SECCOMP_FILTER_FLAG_NEW_LISTENER); + } + if listener < 0 { + return Err(io::Error::last_os_error()); + } + // SAFETY: a freshly returned descriptor that nothing else owns. + Ok(unsafe { OwnedFd::from_raw_fd(listener as RawFd) }) +} diff --git a/crates/sandbox_linux/src/supervisor.rs b/crates/sandbox_linux/src/supervisor.rs new file mode 100644 index 00000000000..712ffeb91ef --- /dev/null +++ b/crates/sandbox_linux/src/supervisor.rs @@ -0,0 +1,657 @@ +//! The supervisor process (`zed --zed-sandbox-linux-supervisor`). +//! +//! One per [`SandboxContext`](crate::SandboxContext). The main thread polls: +//! - the control socket from Zed ([`CONTROL_FD`]): requests, prompt replies; +//! - pending attach sockets, each waiting for one helper's listener fd; +//! - seccomp listeners, one per running execution, for when it ends. +//! +//! Each execution's notifications are received and handled by its own +//! [`Receivers`]. +//! +//! A notification can only arrive on the listener of the filter that trapped +//! it, so the listener identifies the sandbox; nothing the sandboxed process +//! sends can claim another identity. +//! +//! The process exits when Zed shuts down the control socket. Listeners close +//! with it, after which every trapped syscall fails with `ENOSYS`. + +use std::collections::{HashMap, VecDeque}; +use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, OwnedFd, RawFd}; +use std::path::Path; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Arc, Condvar, Mutex, OnceLock, RwLock, mpsc}; +use std::time::{Duration, Instant}; + +use anyhow::{Context as _, Result, bail}; + +use crate::broker; +use crate::dns; +use crate::net::{self, Network}; +use crate::policy::{HandlerId, Permission, RequestedAccess, WireAccess, WirePolicy, WireRule}; +use crate::process::ProcessTree; +use crate::protocol::{ + AttachInfo, Denial, OpenFile, PromptId, PromptRequest, Request, Response, SandboxId, + SupervisorMessage, ZedMessage, receive_message, send_message, +}; +use crate::sys; + +pub(crate) const SUPERVISOR_FLAG: &str = "--zed-sandbox-linux-supervisor"; +pub(crate) const CONTROL_FD: RawFd = 3; + +const PROMPT_TIMEOUT: Duration = Duration::from_secs(5 * 60); +/// Per execution. +const MAX_RECEIVERS: usize = 64; +/// Idle receivers beyond this many exit. +const SPARE_RECEIVERS: usize = 2; + +// From . +const SECCOMP_IOCTL_NOTIF_SET_FLAGS: libc::c_ulong = 0x4008_2104; +const SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP: libc::c_ulong = 1; + +pub(crate) struct SandboxState { + pub id: SandboxId, + policy: RwLock>, + pub names: Mutex, +} + +impl SandboxState { + pub fn policy(&self) -> Arc { + match self.policy.read() { + Ok(policy) => policy.clone(), + Err(poisoned) => poisoned.into_inner().clone(), + } + } + + fn modify_policy(&self, modify: impl FnOnce(&mut WirePolicy)) { + let mut current = match self.policy.write() { + Ok(current) => current, + Err(poisoned) => poisoned.into_inner(), + }; + modify(Arc::make_mut(&mut current)); + } +} + +struct PromptReply { + allow: bool, + remember: Vec, +} + +/// One running command (and everything it spawns). +pub(crate) struct Execution { + pub listener: OwnedFd, + pub sandbox: Arc, + pub tree: ProcessTree, + pub network: Network, + /// The root of its mount namespace, once opened. + pub root: OnceLock, + receivers: Receivers, +} + +/// The threads answering one execution's notifications, as leader and +/// followers: one thread at a time waits on the listener, and once it +/// receives a notification it leaves the waiting to another and handles the +/// notification itself. Answering on the receiving thread saves a hand-off +/// and lets the kernel wake it synchronously when the next notification +/// arrives. Handling can block (a prompt, a FIFO, a slow filesystem), so a +/// thread is added whenever none is left to wait. +#[derive(Default)] +struct Receivers { + leader: Mutex<()>, + /// Threads not handling a notification. + idle: AtomicUsize, + threads: AtomicUsize, +} + +fn start_receiver(shared: &Arc, execution: &Arc) { + let receivers = &execution.receivers; + if receivers.threads.fetch_add(1, Ordering::SeqCst) >= MAX_RECEIVERS { + receivers.threads.fetch_sub(1, Ordering::SeqCst); + return; + } + receivers.idle.fetch_add(1, Ordering::SeqCst); + let spawned = std::thread::Builder::new() + .name("zed-sandbox-receiver".to_string()) + .spawn({ + let shared = shared.clone(); + let execution = execution.clone(); + move || run_receiver(&shared, &execution) + }); + if let Err(error) = spawned { + receivers.threads.fetch_sub(1, Ordering::SeqCst); + receivers.idle.fetch_sub(1, Ordering::SeqCst); + eprintln!("zed sandbox supervisor: starting a receiver: {error}"); + } +} + +fn run_receiver(shared: &Arc, execution: &Arc) { + let receivers = &execution.receivers; + let exit = || { + receivers.idle.fetch_sub(1, Ordering::SeqCst); + receivers.threads.fetch_sub(1, Ordering::SeqCst); + }; + loop { + let notification = { + let _leader = net::lock(&receivers.leader); + loop { + let mut entry = poll_fd(execution.listener.as_raw_fd()); + // SAFETY: one valid pollfd. + let ready = unsafe { libc::poll(&mut entry, 1, -1) }; + if ready < 0 { + if std::io::Error::last_os_error().raw_os_error() == Some(libc::EINTR) { + continue; + } + return exit(); + } + if entry.revents & libc::POLLIN != 0 { + if let Some(notification) = broker::receive(execution.listener.as_fd()) { + break notification; + } + } else if entry.revents & (libc::POLLHUP | libc::POLLERR | libc::POLLNVAL) != 0 { + // Every task under this filter has exited. + return exit(); + } + } + }; + if receivers.idle.fetch_sub(1, Ordering::SeqCst) == 1 { + start_receiver(shared, execution); + } + broker::handle(shared, execution, notification); + if receivers.idle.fetch_add(1, Ordering::SeqCst) >= SPARE_RECEIVERS { + return exit(); + } + } +} + +/// What [`Shared::ask`] concluded. +pub(crate) enum AskOutcome { + Decided(bool), + /// Another prompt to the same handler finished while this one waited; + /// re-evaluate the (possibly extended) policy before asking. + Retry, +} + +/// The prompts of one handler in one sandbox, shown one at a time. +/// +/// While one is open, an identical request waits for and shares its +/// decision; any other request waits for it to finish and then retries, so +/// it sees rules the first one remembered. +#[derive(Default)] +struct PromptQueue { + state: Mutex, + changed: Condvar, +} + +#[derive(Default)] +struct PromptQueueState { + open: Option>, + /// Number of prompts finished so far. + finished: u64, + /// Recent `(prompt number, requests, decision)`, for identical requests + /// that waited on them. + recent: VecDeque<(u64, Vec, bool)>, +} + +const RECENT_DECISIONS: usize = 64; + +impl PromptQueue { + fn lock(&self) -> std::sync::MutexGuard<'_, PromptQueueState> { + match self.state.lock() { + Ok(state) => state, + Err(poisoned) => poisoned.into_inner(), + } + } +} + +/// State workers share with the main thread. +pub(crate) struct Shared { + control: OwnedFd, + prompts: Mutex>>, + next_prompt: AtomicU64, + queues: Mutex>>, + host_root: OwnedFd, +} + +impl Shared { + /// `/`, opened once. + pub fn host_root(&self) -> BorrowedFd<'_> { + self.host_root.as_fd() + } + + fn send(&self, message: &SupervisorMessage) { + // SEQPACKET sends are atomic, so workers and the main thread can + // share the socket. + if let Err(error) = send_message(self.control.as_fd(), message, &[]) { + eprintln!("zed sandbox supervisor: {error:#}"); + } + } + + /// Ask Zed's `handler` whether to allow `requests`, through the + /// handler's [`PromptQueue`]. Blocks until it answers, the prompt times + /// out (deny), or the syscall is abandoned (`still_valid` turns false). + /// Rules the handler asks to remember are added to the sandbox's policy + /// before any waiting request wakes up. + pub fn ask( + &self, + sandbox: &SandboxState, + handler: HandlerId, + requests: Vec, + tid: i32, + still_valid: impl Fn() -> bool, + ) -> AskOutcome { + let queue = match self.queues.lock() { + Ok(mut queues) => queues.entry((sandbox.id, handler)).or_default().clone(), + Err(_) => return AskOutcome::Decided(false), + }; + let mut state = queue.lock(); + if let Some(open) = &state.open { + let identical = *open == requests; + let awaited = state.finished + 1; + while state.finished < awaited { + if !still_valid() { + return AskOutcome::Decided(false); + } + state = match queue.changed.wait_timeout(state, Duration::from_secs(1)) { + Ok((state, _)) => state, + Err(poisoned) => poisoned.into_inner().0, + }; + } + let shared = state + .recent + .iter() + .find(|(number, _, _)| *number == awaited) + .filter(|(_, decided, _)| identical && *decided == requests) + .map(|(_, _, allow)| *allow); + return match shared { + Some(allow) => AskOutcome::Decided(allow), + None => AskOutcome::Retry, + }; + } + state.open = Some(requests.clone()); + drop(state); + + let allow = self.prompt(sandbox, handler, requests.clone(), tid, still_valid); + + let mut state = queue.lock(); + state.open = None; + state.finished += 1; + let number = state.finished; + state.recent.push_back((number, requests, allow)); + if state.recent.len() > RECENT_DECISIONS { + state.recent.pop_front(); + } + queue.changed.notify_all(); + AskOutcome::Decided(allow) + } + + fn prompt( + &self, + sandbox: &SandboxState, + handler: HandlerId, + requests: Vec, + tid: i32, + still_valid: impl Fn() -> bool, + ) -> bool { + let prompt = self.next_prompt.fetch_add(1, Ordering::Relaxed); + let (sender, receiver) = mpsc::channel(); + if let Ok(mut prompts) = self.prompts.lock() { + prompts.insert(prompt, sender); + } + self.send(&SupervisorMessage::Prompt(PromptRequest { + prompt, + sandbox: sandbox.id, + handler, + requests, + pid: tid, + executable: std::fs::read_link(format!("/proc/{tid}/exe")).ok(), + })); + let start = Instant::now(); + let decision = loop { + match receiver.recv_timeout(Duration::from_secs(1)) { + Ok(reply) => break Some(reply), + Err(mpsc::RecvTimeoutError::Timeout) => { + if !still_valid() || start.elapsed() > PROMPT_TIMEOUT { + break None; + } + } + Err(mpsc::RecvTimeoutError::Disconnected) => break None, + } + }; + if let Ok(mut prompts) = self.prompts.lock() { + prompts.remove(&prompt); + } + let Some(reply) = decision else { + self.send(&SupervisorMessage::PromptCancelled { prompt }); + return false; + }; + if !reply.remember.is_empty() { + sandbox.modify_policy(|policy| { + for rule in reply.remember { + policy.add(rule); + } + }); + } + reply.allow + } + + /// Whether `request` is allowed, asking its handler if the policy says + /// so. Denials are reported. + pub fn authorize( + &self, + sandbox: &SandboxState, + request: &RequestedAccess, + tid: i32, + still_valid: &dyn Fn() -> bool, + ) -> bool { + let allowed = loop { + match sandbox.policy().access(request) { + WireAccess::Allow => break true, + WireAccess::Deny => break false, + WireAccess::Ask(handler) => { + match self.ask(sandbox, handler, vec![request.clone()], tid, still_valid) { + AskOutcome::Decided(allowed) => break allowed, + // Re-check the policy, which may now decide. + AskOutcome::Retry => {} + } + } + } + }; + if !allowed { + self.report_denial(sandbox.id, vec![request.clone()], tid); + } + allowed + } + + /// Tell Zed a policy rule (or a handler) denied an operation. + pub fn report_denial(&self, sandbox: SandboxId, requests: Vec, tid: i32) { + self.send(&SupervisorMessage::Denied { + sandbox, + denial: Denial { + requests, + pid: tid, + executable: std::fs::read_link(format!("/proc/{tid}/exe")).ok(), + }, + }); + } + + fn reply(&self, prompt: PromptId, reply: PromptReply) { + let sender = self + .prompts + .lock() + .ok() + .and_then(|mut prompts| prompts.remove(&prompt)); + if let Some(sender) = sender { + sender.send(reply).ok(); + } + } +} + +pub(crate) fn run() -> ! { + // Relays write to sockets whose peer may be gone: that's an error to + // handle, not a reason to die. + // SAFETY: plain signal disposition change. + unsafe { libc::signal(libc::SIGPIPE, libc::SIG_IGN) }; + if let Err(error) = sys::set_parent_death_signal() { + eprintln!("zed sandbox supervisor: {error}"); + std::process::exit(1); + } + // SAFETY: Zed spawns the supervisor with exactly this descriptor. + let control = match unsafe { sys::inherited_fd(CONTROL_FD) } { + Ok(control) => control, + Err(error) => { + eprintln!("zed sandbox supervisor: missing control socket: {error}"); + std::process::exit(1); + } + }; + let host_root = match sys::open(Path::new("/"), libc::O_PATH | libc::O_DIRECTORY) { + Ok(root) => root, + Err(error) => { + eprintln!("zed sandbox supervisor: opening /: {error}"); + std::process::exit(1); + } + }; + let shared = Arc::new(Shared { + control, + prompts: Mutex::new(HashMap::new()), + next_prompt: AtomicU64::new(0), + queues: Mutex::new(HashMap::new()), + host_root, + }); + Supervisor { + shared, + sandboxes: HashMap::new(), + pending: Vec::new(), + executions: Vec::new(), + } + .run(); + std::process::exit(0); +} + +struct Supervisor { + shared: Arc, + sandboxes: HashMap>, + /// Attach sockets whose helper hasn't sent its listener yet. + pending: Vec<(OwnedFd, Arc)>, + /// Running executions. Each holds its own reference to its sandbox, so + /// destroying a sandbox doesn't strand its commands. + executions: Vec>, +} + +impl Supervisor { + fn run(&mut self) { + loop { + let mut poll_fds = vec![poll_fd(self.shared.control.as_raw_fd())]; + poll_fds.extend(self.pending.iter().map(|(fd, _)| poll_fd(fd.as_raw_fd()))); + // Only for hang-ups: receivers take the notifications. + poll_fds.extend(self.executions.iter().map(|execution| libc::pollfd { + fd: execution.listener.as_raw_fd(), + events: 0, + revents: 0, + })); + // SAFETY: a valid array of pollfds. + let ready = unsafe { libc::poll(poll_fds.as_mut_ptr(), poll_fds.len() as _, -1) }; + if ready < 0 { + let error = std::io::Error::last_os_error(); + if error.raw_os_error() == Some(libc::EINTR) { + continue; + } + eprintln!("zed sandbox supervisor: poll failed: {error}"); + return; + } + + let pending_count = self.pending.len(); + let (control_ready, rest) = poll_fds.split_at(1); + let (pending_ready, listeners_ready) = rest.split_at(pending_count); + + // Walk backwards so removals don't shift indices still to visit. + for (index, entry) in listeners_ready.iter().enumerate().rev() { + if entry.revents & (libc::POLLHUP | libc::POLLERR | libc::POLLNVAL) != 0 { + // Every task under this filter has exited. + self.executions.swap_remove(index).network.close(); + } + } + for (index, entry) in pending_ready.iter().enumerate().rev() { + if entry.revents == 0 { + continue; + } + let (socket, sandbox) = self.pending.swap_remove(index); + match receive_message::(socket.as_fd()) { + Ok(Some((info, mut fds))) if !fds.is_empty() => { + let listener = fds.remove(0); + // Wake the receiver that answers on the target's + // CPU right away (Linux 6.6). Older kernels refuse the + // flag, which only costs that optimization. + // SAFETY: SET_FLAGS takes the flags by value. + unsafe { + libc::ioctl( + listener.as_raw_fd(), + SECCOMP_IOCTL_NOTIF_SET_FLAGS as _, + SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP, + ) + }; + let execution = Arc::new(Execution { + listener, + sandbox, + tree: ProcessTree { + helper: info.helper_pid, + }, + // From now on the helper serves socket requests + // on the attach socket. + network: Network::new(socket), + root: OnceLock::new(), + receivers: Receivers::default(), + }); + start_receiver(&self.shared, &execution); + net::start(&self.shared, &execution, fds); + self.executions.push(execution); + } + // The helper exited (or misbehaved) before attaching. Its + // command never ran, since it attaches before exec. + Ok(_) => {} + Err(error) => eprintln!("zed sandbox supervisor: attach failed: {error:#}"), + } + } + if control_ready[0].revents != 0 && !self.handle_control() { + return; + } + } + } + + /// Handle one control message. Returns `false` once Zed has gone away. + fn handle_control(&mut self) -> bool { + let (message, fds) = match receive_message::(self.shared.control.as_fd()) { + Ok(Some(message)) => message, + Ok(None) => return false, + Err(error) => { + eprintln!("zed sandbox supervisor: bad control message: {error:#}"); + return false; + } + }; + match message { + ZedMessage::PromptReply { + prompt, + allow, + remember, + } => self.shared.reply(prompt, PromptReply { allow, remember }), + ZedMessage::Request { id, request } => { + let response = + self.handle_request(request, fds) + .unwrap_or_else(|error| Response::Error { + message: format!("{error:#}"), + }); + self.shared + .send(&SupervisorMessage::Response { id, response }); + } + } + true + } + + fn handle_request(&mut self, request: Request, mut fds: Vec) -> Result { + match request { + Request::CreateSandbox { sandbox, policy } => { + if self.sandboxes.contains_key(&sandbox) { + bail!("sandbox {sandbox} already exists"); + } + self.sandboxes.insert( + sandbox, + Arc::new(SandboxState { + id: sandbox, + policy: RwLock::new(Arc::new(policy)), + names: Mutex::default(), + }), + ); + Ok(Response::Done) + } + Request::UpdatePolicy { sandbox, policy } => { + let state = self.sandbox(sandbox)?; + state.modify_policy(|current| *current = policy); + let policy = state.policy(); + let mut still_open = Vec::new(); + for execution in &self.executions { + if Arc::ptr_eq(&execution.sandbox, &state) { + still_open.extend(open_files_beyond(execution, &policy)); + net::ensure_udp_ports(&self.shared, execution); + } + } + Ok(Response::PolicyUpdated { still_open }) + } + Request::DestroySandbox { sandbox } => { + self.sandboxes.remove(&sandbox); + if let Ok(mut queues) = self.shared.queues.lock() { + queues.retain(|(owner, _), _| *owner != sandbox); + } + Ok(Response::Done) + } + Request::AttachExecution { sandbox } => { + let state = self.sandbox(sandbox)?; + if fds.len() != 1 { + bail!("attach request needs exactly one socket"); + } + self.pending.push((fds.remove(0), state)); + Ok(Response::Done) + } + } + } + + fn sandbox(&self, sandbox: SandboxId) -> Result> { + self.sandboxes + .get(&sandbox) + .cloned() + .with_context(|| format!("unknown sandbox {sandbox}")) + } +} + +/// Files the execution's processes hold open with access `policy` no longer +/// grants. +fn open_files_beyond(execution: &Execution, policy: &WirePolicy) -> Vec { + let mut open_files = Vec::new(); + for process in execution.tree.members() { + let Ok(entries) = std::fs::read_dir(format!("/proc/{}/fd", process.pid)) else { + continue; + }; + for entry in entries.flatten() { + let Ok(path) = std::fs::read_link(entry.path()) else { + continue; + }; + if !path.is_absolute() || path.is_dir() { + continue; + } + let fdinfo = format!( + "/proc/{}/fdinfo/{}", + process.pid, + entry.file_name().to_string_lossy() + ); + let Some(flags) = std::fs::read_to_string(fdinfo).ok().and_then(|info| { + info.lines() + .find_map(|line| line.strip_prefix("flags:")) + .and_then(|flags| i32::from_str_radix(flags.trim(), 8).ok()) + }) else { + continue; + }; + if flags & libc::O_PATH != 0 { + continue; + } + let access_mode = flags & libc::O_ACCMODE; + let read = access_mode != libc::O_WRONLY + && policy.fs_access(&path, Permission::Read) != WireAccess::Allow; + let write = access_mode != libc::O_RDONLY + && policy.fs_access(&path, Permission::Write) != WireAccess::Allow; + if read || write { + open_files.push(OpenFile { + pid: process.pid, + path, + read, + write, + }); + } + } + } + open_files +} + +fn poll_fd(fd: RawFd) -> libc::pollfd { + libc::pollfd { + fd, + events: libc::POLLIN, + revents: 0, + } +} diff --git a/crates/sandbox_linux/src/sys.rs b/crates/sandbox_linux/src/sys.rs new file mode 100644 index 00000000000..fe779a3a0fc --- /dev/null +++ b/crates/sandbox_linux/src/sys.rs @@ -0,0 +1,258 @@ +//! Thin wrappers over the raw syscalls the sandbox needs that `libc` / `nix` +//! don't wrap (or that we want with exact semantics). + +use std::ffi::{CStr, CString}; +use std::io; +use std::os::fd::{AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd, RawFd}; +use std::os::unix::ffi::OsStrExt as _; +use std::path::{Path, PathBuf}; + +pub(crate) const MOUNT_ATTR_RDONLY: u64 = 0x1; + +// Syscalls added after the per-arch tables were unified share one number. +const SYS_PIDFD_OPEN: libc::c_long = 434; +const SYS_PIDFD_GETFD: libc::c_long = 438; +const SYS_MOUNT_SETATTR: libc::c_long = 442; +const AT_RECURSIVE: libc::c_uint = 0x8000; + +pub(crate) fn errno(code: i32) -> io::Error { + io::Error::from_raw_os_error(code) +} + +pub(crate) fn cvt(result: libc::c_int) -> io::Result { + if result < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(result) + } +} + +pub(crate) fn cstring(bytes: &[u8]) -> io::Result { + CString::new(bytes).map_err(|_| errno(libc::EINVAL)) +} + +pub(crate) fn path_cstring(path: &Path) -> io::Result { + cstring(path.as_os_str().as_bytes()) +} + +/// `openat(2)`. `O_CLOEXEC` is always added. +pub(crate) fn openat(dirfd: RawFd, path: &CStr, flags: i32, mode: u32) -> io::Result { + // SAFETY: valid NUL-terminated path. + let fd = cvt(unsafe { libc::openat(dirfd, path.as_ptr(), flags | libc::O_CLOEXEC, mode) })?; + // SAFETY: a freshly returned descriptor that nothing else owns. + Ok(unsafe { OwnedFd::from_raw_fd(fd) }) +} + +pub(crate) fn open(path: &Path, flags: i32) -> io::Result { + openat(libc::AT_FDCWD, &path_cstring(path)?, flags, 0) +} + +/// `/proc/self/fd/N`: opening or operating on this path reaches exactly the +/// object `fd` refers to, through the mount `fd` was opened on. +pub(crate) fn proc_self_fd(fd: BorrowedFd<'_>) -> PathBuf { + PathBuf::from(format!("/proc/self/fd/{}", fd.as_raw_fd())) +} + +/// The path of the object `fd` refers to, as rendered by the kernel. +/// +/// For objects in another mount namespace (e.g. a sandbox's), the kernel +/// renders the path as seen from that namespace's root. +pub(crate) fn fd_path(fd: BorrowedFd<'_>) -> io::Result { + std::fs::read_link(proc_self_fd(fd)) +} + +pub(crate) fn fstat(fd: BorrowedFd<'_>) -> io::Result { + // SAFETY: `stat` is plain old data, filled in by the kernel. + let mut stat: libc::stat = unsafe { std::mem::zeroed() }; + // SAFETY: valid fd and pointer to a `stat`. + cvt(unsafe { libc::fstat(fd.as_raw_fd(), &mut stat) })?; + Ok(stat) +} + +pub(crate) fn mount( + source: Option<&Path>, + target: &Path, + filesystem: Option<&str>, + flags: libc::c_ulong, + data: Option<&str>, +) -> io::Result<()> { + let source = source.map(path_cstring).transpose()?; + let target = path_cstring(target)?; + let filesystem = filesystem + .map(|name| cstring(name.as_bytes())) + .transpose()?; + let data = data.map(|data| cstring(data.as_bytes())).transpose()?; + let pointer = |value: &Option| { + value + .as_ref() + .map_or(std::ptr::null(), |value| value.as_ptr()) + }; + // SAFETY: every pointer is null or a valid NUL-terminated string. + cvt(unsafe { + libc::mount( + pointer(&source), + target.as_ptr(), + pointer(&filesystem), + flags, + pointer(&data).cast(), + ) + })?; + Ok(()) +} + +#[repr(C)] +struct MountAttr { + attr_set: u64, + attr_clr: u64, + propagation: u64, + userns_fd: u64, +} + +/// `mount_setattr(2)`: add `attributes` to the mount at `target` (and, with +/// `recursive`, every mount beneath it). +pub(crate) fn mount_setattr(target: &Path, attributes: u64, recursive: bool) -> io::Result<()> { + let target = path_cstring(target)?; + let attr = MountAttr { + attr_set: attributes, + attr_clr: 0, + propagation: 0, + userns_fd: 0, + }; + let flags = if recursive { AT_RECURSIVE } else { 0 }; + // SAFETY: valid path and a correctly sized `mount_attr`. + let result = unsafe { + libc::syscall( + SYS_MOUNT_SETATTR, + libc::AT_FDCWD, + target.as_ptr(), + flags, + &attr as *const MountAttr, + size_of::(), + ) + }; + if result < 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) +} + +/// Duplicate `fd` to the lowest free descriptor `>= minimum`, close-on-exec. +pub(crate) fn duplicate_above(fd: BorrowedFd<'_>, minimum: RawFd) -> io::Result { + // SAFETY: fcntl on a valid descriptor. + let duplicate = cvt(unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_DUPFD_CLOEXEC, minimum) })?; + // SAFETY: a freshly returned descriptor that nothing else owns. + Ok(unsafe { OwnedFd::from_raw_fd(duplicate) }) +} + +fn own_syscall_fd(result: libc::c_long) -> io::Result { + if result < 0 { + return Err(io::Error::last_os_error()); + } + // SAFETY: a freshly returned descriptor that nothing else owns. + Ok(unsafe { OwnedFd::from_raw_fd(result as RawFd) }) +} + +/// A duplicate of descriptor `fd` of process `pid` (a thread group leader). +pub(crate) fn pidfd_getfd(pid: i32, fd: RawFd) -> io::Result { + // SAFETY: plain syscalls; each returns a new descriptor or fails. + let pidfd = own_syscall_fd(unsafe { libc::syscall(SYS_PIDFD_OPEN, pid, 0) })?; + // SAFETY: as above. + own_syscall_fd(unsafe { libc::syscall(SYS_PIDFD_GETFD, pidfd.as_raw_fd(), fd, 0) }) +} + +/// `socket(2)`. `SOCK_CLOEXEC` is always added. +pub(crate) fn socket(domain: i32, kind: i32, protocol: i32) -> io::Result { + // SAFETY: plain socket call. + let fd = cvt(unsafe { libc::socket(domain, kind | libc::SOCK_CLOEXEC, protocol) })?; + // SAFETY: a freshly returned descriptor that nothing else owns. + Ok(unsafe { OwnedFd::from_raw_fd(fd) }) +} + +pub(crate) fn socket_option(fd: BorrowedFd<'_>, level: i32, name: i32) -> io::Result { + let mut value: libc::c_int = 0; + let mut length = size_of::() as libc::socklen_t; + // SAFETY: an int-sized buffer with its length. + cvt(unsafe { + libc::getsockopt( + fd.as_raw_fd(), + level, + name, + (&mut value as *mut libc::c_int).cast(), + &mut length, + ) + })?; + Ok(value) +} + +pub(crate) fn set_socket_option( + fd: BorrowedFd<'_>, + level: i32, + name: i32, + value: i32, +) -> io::Result<()> { + // SAFETY: an int-sized value with its length. + cvt(unsafe { + libc::setsockopt( + fd.as_raw_fd(), + level, + name, + (&value as *const libc::c_int).cast(), + size_of::() as libc::socklen_t, + ) + })?; + Ok(()) +} + +/// Wait until `fd` has one of `events`, giving up after `timeout` or as +/// soon as `keep_waiting` returns false. +pub(crate) fn wait_for( + fd: BorrowedFd<'_>, + events: libc::c_short, + timeout: std::time::Duration, + keep_waiting: &dyn Fn() -> bool, +) -> io::Result<()> { + let deadline = std::time::Instant::now() + timeout; + loop { + let mut poll_fd = libc::pollfd { + fd: fd.as_raw_fd(), + events, + revents: 0, + }; + // SAFETY: one valid pollfd. + let ready = unsafe { libc::poll(&mut poll_fd, 1, 250) }; + if ready > 0 { + return Ok(()); + } + if ready < 0 { + let error = io::Error::last_os_error(); + if error.raw_os_error() != Some(libc::EINTR) { + return Err(error); + } + } + if !keep_waiting() { + return Err(errno(libc::ECONNABORTED)); + } + if std::time::Instant::now() >= deadline { + return Err(errno(libc::ETIMEDOUT)); + } + } +} + +pub(crate) fn set_parent_death_signal() -> io::Result<()> { + // SAFETY: plain prctl. + cvt(unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL, 0, 0, 0) })?; + Ok(()) +} + +/// Take ownership of a descriptor this process inherited at a fixed number. +/// +/// # Safety +/// `fd` must be open and not owned by anything else in this process. +pub(crate) unsafe fn inherited_fd(fd: RawFd) -> io::Result { + // SAFETY: fcntl on a possibly-closed descriptor only reports EBADF. + let flags = cvt(unsafe { libc::fcntl(fd, libc::F_GETFD) })?; + // SAFETY: as above. + cvt(unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) })?; + // SAFETY: guaranteed by the caller. + Ok(unsafe { OwnedFd::from_raw_fd(fd) }) +} diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index 839f7299d54..f558e85e26a 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -248,6 +248,9 @@ gpui = { workspace = true, features = ["profiler", "windows-manifest"] } [target.'cfg(target_os = "windows")'.build-dependencies] windows_resources.workspace = true +[target.'cfg(target_os = "linux")'.dependencies] +sandbox_linux.workspace = true + [target.'cfg(any(target_os = "linux", target_os = "freebsd"))'.dependencies] gpui = { workspace = true, features = ["profiler", "wayland", "x11"] } ashpd = { workspace = true, features = ["notification"] } diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 1390452c1bd..c20f4ab9dd2 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -215,6 +215,8 @@ fn main() { // args are appended verbatim and would otherwise be misinterpreted as Zed's // own arguments. sandbox::run_sandbox_launcher_if_invoked(); + #[cfg(target_os = "linux")] + sandbox_linux::run_hidden_entrypoint_if_invoked(); #[cfg(unix)] util::prevent_root_execution(); diff --git a/nix/modules/packages.nix b/nix/modules/packages.nix index 5c9b7391d11..83f95b09dee 100644 --- a/nix/modules/packages.nix +++ b/nix/modules/packages.nix @@ -22,7 +22,6 @@ a11y-test = import ../tests/a11y.nix { inherit pkgs inputs; }; - } - // import ../tests/sandboxing { inherit pkgs inputs; }; + } // import ../tests/sandboxing { inherit pkgs inputs; }; }; } diff --git a/nix/tests/sandboxing/README.md b/nix/tests/sandboxing/README.md new file mode 100644 index 00000000000..87622d2dbbd --- /dev/null +++ b/nix/tests/sandboxing/README.md @@ -0,0 +1,77 @@ +# Linux sandbox VM tests + +These are `pkgs.testers.nixosTest` checks exported by the root flake on Linux. +They require Nix with flakes and a Linux builder with usable `/dev/kvm` +(including access for the Nix build user). No Zed UI is involved. + +The existing entrypoint discovers all `sandbox-*` checks: + +```sh +cargo xtask sandbox-tests --list +cargo xtask sandbox-tests +cargo xtask sandbox-tests sandbox-linux-working sandbox-linux-userns-disabled +``` + +Run individual checks or debug interactively with the NixOS test driver: + +```sh +nix build .#checks.x86_64-linux.sandbox-linux-working -L --accept-flake-config +nix build .#checks.x86_64-linux.sandbox-linux-userns-disabled -L --accept-flake-config +nix run .#checks.x86_64-linux.sandbox-linux-working.driverInteractive +``` + +Replace `x86_64-linux` with `aarch64-linux` on an appropriate builder. +`cargo xtask sandbox-tests --rebuild ...` passes `--rebuild` to Nix to rerun a +previously built check rather than reuse its successful cached result. + +For an untracked working tree (Nix's Git flake source omits new files), or a +shallow checkout whose Nix version cannot determine `revCount`, use `path:.` +instead of `.` in the direct Nix commands. Do not commit just to run a check. + +## Experimental `sandbox_linux` + +`sandbox-linux-working` builds the crate's `end_to_end` example with Crane and +installs that executable in the VM. It is an example rather than a test target, +gated behind the `__end_to_end_harness` feature, so CI's +`cargo nextest run --workspace` never builds or runs it. Its hidden +supervisor/helper re-exec entrypoints are preserved. The harness runs as a +normal user, not root, with namespace creation enabled and a 180-second outer +timeout. `--require-sandbox` makes setup failure fatal instead of taking the +optional host-test skip path. + +Coverage includes namespace separation, read/write globs and symlink/hard-link +escape attempts, host-side integrity checks, Ask decisions (including handler +panic and concurrent requests), live policy changes, process cleanup, TCP, +synthetic DNS, UDP and Unix socket permission checks, and nonblocking TCP +responsiveness/cancellation. Echo peers run locally outside the sandbox, inside +the disposable VM; no Internet service is needed. Escape targets and other +filesystem fixtures live under an automatically cleaned +`/tmp/sandbox-escape-test-*` directory. + +`sandbox-linux-userns-disabled` boots with `user.max_user_namespaces=0`. It +requires namespace setup to fail and verifies that the untrusted command's +canary was never written. This is an expected failure assertion, not a skip. + +For faster host-side checks (the strict test needs usable user namespaces, +seccomp notification support, Python 3, coreutils and util-linux on `PATH`): + +```sh +cargo test -p sandbox_linux --lib --locked +./script/clippy -p sandbox_linux +cargo build -p sandbox_linux --example end_to_end --features __end_to_end_harness --locked +target/debug/examples/end_to_end --require-sandbox +``` + +Without `--require-sandbox`, the end-to-end harness may skip if host namespace +creation is unavailable. Do not use that optional invocation as VM evidence. + +## CI + +These VM checks exercise `sandbox_linux`, not the existing Bubblewrap backend. +The Bubblewrap backend and its WSL tests remain in place. + +The generated `nix_build` GitHub workflow (`run-nix` / `run-bundling` labels) +currently builds only `.#default`; it does **not** execute these VM checks. +Run the commands above explicitly on a KVM-capable builder. The new checks are +also included in `nix flake check` on Linux and in the existing xtask's default +selection; no separate CI/test framework is introduced. diff --git a/nix/tests/sandboxing/bwrap_smoke_test.sh b/nix/tests/sandboxing/bwrap_smoke_test.sh deleted file mode 100644 index 9a6d6d4afa0..00000000000 --- a/nix/tests/sandboxing/bwrap_smoke_test.sh +++ /dev/null @@ -1,117 +0,0 @@ -#!/usr/bin/env bash -# Ad-hoc smoke test for the static `bwrap` binary. Exercises filesystem -# read/write isolation and network isolation by trying to "break out" of the -# sandbox in ways that must fail, alongside controls that must succeed. -# -# Usage: bwrap_smoke_test.sh /path/to/bwrap -set -u - -BWRAP=${1:?usage: bwrap_smoke_test.sh /path/to/bwrap} - -# Resolve real binaries (works on NixOS, where /bin is mostly empty). -CAT=$(command -v cat) -TOUCH=$(command -v touch) -SH=$(command -v sh) -PY=$(command -v python3) - -# Bind enough of the host (read-only) for dynamically-linked helpers to run. -# On NixOS that means the closure under /nix/store; /usr/bin elsewhere. -BASE=(--ro-bind /nix /nix --ro-bind /run /run --proc /proc --dev /dev --unshare-user) -[ -d /usr ] && BASE+=(--ro-bind /usr /usr) -[ -e /bin ] && BASE+=(--ro-bind /bin /bin) -[ -e /lib ] && BASE+=(--ro-bind /lib /lib) -[ -e /lib64 ] && BASE+=(--ro-bind /lib64 /lib64) -[ -e /etc ] && BASE+=(--ro-bind /etc /etc) - -PASS=0 -FAIL=0 -ok() { echo "PASS: $1"; PASS=$((PASS + 1)); } -bad() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); } - -# expect_ok -> command must exit 0 -expect_ok() { - desc=$1; shift - if "$@"; then ok "$desc"; else bad "$desc (expected success, got exit $?)"; fi -} -# expect_fail -> command must exit non-zero -expect_fail() { - desc=$1; shift - if "$@"; then bad "$desc (expected failure, but it succeeded)"; else ok "$desc"; fi -} - -# --------------------------------------------------------------------------- -# Set up a host scratch tree: -# $ROOT/secret/secret.txt -> must NOT be visible unless explicitly bound -# $ROOT/allowed/note.txt -> bound read-only into the sandbox -# $ROOT/writable/ -> bound read-write into the sandbox -# --------------------------------------------------------------------------- -ROOT=$(mktemp -d /tmp/bwrap-smoke.XXXXXX) -trap 'rm -rf "$ROOT"' EXIT -mkdir -p "$ROOT/secret" "$ROOT/allowed" "$ROOT/writable" -echo "TOP SECRET" > "$ROOT/secret/secret.txt" -echo "you may read me" > "$ROOT/allowed/note.txt" - -echo "==================================================================" -echo "bwrap: $BWRAP" -"$BWRAP" --version -echo "==================================================================" -echo "## Filesystem isolation" - -# The host secret must be invisible: sandbox sees a fresh tmpfs at $ROOT, -# so the secret path simply doesn't exist. -expect_fail "host secret file is NOT readable from sandbox" \ - "$BWRAP" "${BASE[@]}" --tmpfs "$ROOT" "$CAT" "$ROOT/secret/secret.txt" - -# Likewise nothing under /tmp leaks in by default (no bind for it). -expect_fail "host /tmp is NOT visible from sandbox" \ - "$BWRAP" "${BASE[@]}" --tmpfs /tmp "$CAT" "$ROOT/allowed/note.txt" - -# An explicitly read-only bind IS readable... -expect_ok "explicitly ro-bound file IS readable" \ - "$BWRAP" "${BASE[@]}" --ro-bind "$ROOT/allowed" /data "$CAT" /data/note.txt - -# ...but writing to a read-only bind must fail. -expect_fail "writing to a ro-bound dir is denied" \ - "$BWRAP" "${BASE[@]}" --ro-bind "$ROOT/allowed" /data "$TOUCH" /data/evil - -# Confirm the host file was not modified/created behind the sandbox's back. -expect_fail "ro-bind write did not leak to host" test -e "$ROOT/allowed/evil" - -# A read-write bind allows writes... -expect_ok "writing to a rw-bound dir succeeds" \ - "$BWRAP" "${BASE[@]}" --bind "$ROOT/writable" /data "$TOUCH" /data/hello - -# ...and the write really lands on the host file (proves the bind, not a tmpfs). -expect_ok "rw-bind write is visible on host" test -e "$ROOT/writable/hello" - -# Writing to a tmpfs overlay succeeds inside, but must NOT touch the host dir. -expect_ok "writing into a tmpfs works inside sandbox" \ - "$BWRAP" "${BASE[@]}" --tmpfs "$ROOT/writable" "$TOUCH" "$ROOT/writable/ephemeral" -expect_fail "tmpfs write did not leak to host" test -e "$ROOT/writable/ephemeral" - -echo "------------------------------------------------------------------" -echo "## Network isolation" - -NETCHECK='import socket; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.settimeout(5); s.connect(("1.1.1.1",53)); print("connected")' - -# With --unshare-net the sandbox has only loopback: outbound TCP must fail. -expect_fail "outbound TCP is blocked with --unshare-net" \ - "$BWRAP" "${BASE[@]}" --unshare-net "$PY" -c "$NETCHECK" - -# Loopback still exists inside the isolated netns. -expect_ok "loopback is up inside --unshare-net" \ - "$BWRAP" "${BASE[@]}" --unshare-net "$PY" -c \ - 'import socket; socket.socket().bind(("127.0.0.1",0)); print("lo ok")' - -# Without --unshare-net the host network is shared, so the same connect works -# (skipped automatically if the host itself has no outbound connectivity). -if timeout 6 "$PY" -c "$NETCHECK" >/dev/null 2>&1; then - expect_ok "outbound TCP works when network is shared" \ - "$BWRAP" "${BASE[@]}" "$PY" -c "$NETCHECK" -else - echo "SKIP: host has no outbound TCP; cannot test shared-network case" -fi - -echo "==================================================================" -echo "Summary: $PASS passed, $FAIL failed" -[ "$FAIL" -eq 0 ] diff --git a/nix/tests/sandboxing/default.nix b/nix/tests/sandboxing/default.nix index 94d56363201..1186a6e451d 100644 --- a/nix/tests/sandboxing/default.nix +++ b/nix/tests/sandboxing/default.nix @@ -1,424 +1,52 @@ -# NixOS VM tests for the Linux Bubblewrap (`bwrap`) sandbox. -# -# What actually changes the sandbox's behavior is the *host configuration* — -# whether a usable `bwrap` exists and whether the kernel lets us create user -# namespaces. We can only set those at boot, which is exactly what a VM gives -# us. So we run one machine per host configuration: -# -# working - `bwrap` present, unprivileged user namespaces available; -# the sandbox must be enforced. -# no-bwrap - no `bwrap` on PATH; `Sandbox::can_create` must report -# `BwrapNotFound` and the consumer must fail closed. -# setuid-bwrap - the only `bwrap` is setuid-root; `can_create` must report -# `BwrapSetuidRejected` (we refuse to run setuid bwrap). -# userns-disabled - `bwrap` present but unprivileged user namespaces are -# disabled via sysctl; `can_create` must report -# `SandboxProbeFailed`. -# -# The behavior to assert is declared *here* as a list of `checks`, serialized to -# JSON and handed to the `bwrap-test-helper` binary, which builds the described -# sandbox policy via the sandbox crate's public API, performs the operation, and -# asserts the outcome. Each check is one of: -# -# { read = "/path"; succeeds = true; } # read a host file -# { write = "/path"; succeeds = false; } # write a host file -# { network = "echo1"; succeeds = true; } # connect to an echo server -# { socketPath = "/run/x.sock"; succeeds = false; } # connect a unix socket -# { canCreate = false; error = "bwrap_not_found"; } # Sandbox::can_create -# -# plus optional policy fields applied to that check (defaults shown): -# -# fs = "restricted"; # or "unrestricted" -# writablePaths = [ ]; # writable subtrees when fs = "restricted" -# seedFiles = [ ]; # regular files to create before building the -# # policy (e.g. a single-file-worktree root) -# networkAccess = "blocked"; # or "unrestricted" / "restricted" -# allowedDomains = [ ]; # allowed hosts when networkAccess = "restricted" -# protectedPaths = [ ]; # paths that remain readable but not writable, -# # even if they fall under a writable subtree. -# -# Two echo servers (`echo1`, `echo2`) on separate nodes give the network checks -# real peers, so a restricted-network policy that allowlists `echo1` can be -# shown to reach `echo1` while `echo2` is blocked. -# -# Build one scenario: -# nix build .#checks.x86_64-linux.sandbox-working -# Interactive: -# nix run .#checks.x86_64-linux.sandbox-working.driverInteractive -{ - pkgs, - inputs, -}: +{ pkgs, inputs }: let - lib = pkgs.lib; - - bwrap-test-helper = import ./helper.nix { inherit pkgs inputs; }; - - # Both echo nodes listen on the same port; the helper appends it to a bare - # hostname (`echo1` -> `echo1:7000`). - echoPort = 7000; - - # A node running a raw TCP echo server (socat EXEC:cat). The helper round-trips - # a byte through it, directly or via the sandbox's HTTP CONNECT proxy. - mkEchoNode = { - networking.firewall.allowedTCPPorts = [ echoPort ]; - systemd.services.echo-server = { - description = "TCP echo server"; - wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; - serviceConfig = { - ExecStart = "${pkgs.socat}/bin/socat -d TCP-LISTEN:${toString echoPort},reuseaddr,fork EXEC:cat"; - DynamicUser = true; - Restart = "on-failure"; - }; - }; + helper = import ./helper.nix { + inherit pkgs inputs; }; - # A unix-domain socket, owned by a process *outside* the sandbox, that a - # sandboxed command must not be able to `connect()` to. It lives under `/run` - # (which the sandbox `--ro-bind`s along with the rest of `/`) and NOT under - # `/tmp` (which the restricted-fs sandbox masks with a tmpfs, hiding anything - # there), so it stays visible inside the sandbox and the block is what's - # actually under test. - unixSocketPath = "/run/zed-sandbox-test.sock"; - - # Quiet boot + a couple of cores; shared by every machine-under-test. - baseMachine = { - boot.consoleLogLevel = lib.mkForce 3; # be quiet pls :) - services.journald.extraConfig = lib.mkAfter "MaxLevelConsole=notice"; - environment.systemPackages = [ bwrap-test-helper ]; - virtualisation = { - memorySize = 1024; - cores = 2; - }; - - # A unix-socket echo server outside the sandbox, so a sandboxed `connect()` - # has a real peer: without a listener the connect would fail with - # ECONNREFUSED even absent the sandbox, giving a false "blocked" pass. - systemd.services.unix-echo-server = { - description = "Unix-domain-socket echo server"; - wantedBy = [ "multi-user.target" ]; - serviceConfig = { - ExecStart = "${pkgs.socat}/bin/socat -d UNIX-LISTEN:${unixSocketPath},fork,reuseaddr EXEC:cat"; - Restart = "on-failure"; - }; - }; - }; - - # Per-scenario host configuration. Each entry layers onto `baseMachine`. - machineConfigs = { - # Plain, working host: bwrap on PATH, user namespaces available. - working = - { ... }: - lib.mkMerge [ - baseMachine - { environment.systemPackages = [ pkgs.bubblewrap ]; } - ]; - - # No bwrap anywhere on PATH. - no-bwrap = { ... }: baseMachine; - - # The only bwrap is a setuid-root wrapper. We deliberately do NOT also put a - # plain `pkgs.bubblewrap` on PATH: `locate_bwrap` returns the first `bwrap` - # it finds on PATH, and we want that to be the setuid one so the helper sees - # `BwrapSetuidRejected`. - setuid-bwrap = - { ... }: - lib.mkMerge [ - baseMachine - { - security.wrappers.bwrap = { - source = "${pkgs.bubblewrap}/bin/bwrap"; - owner = "root"; - group = "root"; - setuid = true; - }; - } - ]; - - # bwrap present, but unprivileged user namespaces disabled at boot. Capping - # the count at 0 makes `bwrap --unshare-user` fail, exactly like a hardened - # host, so the probe reports `SandboxProbeFailed`. - userns-disabled = - { ... }: - lib.mkMerge [ - baseMachine - { - environment.systemPackages = [ pkgs.bubblewrap ]; - boot.kernel.sysctl."user.max_user_namespaces" = 0; - } - ]; - }; - - # Build a VM test for `machine` that runs `checks` through the helper. mkTest = - { - name, - machine, - checks, - }: - let - checksFile = pkgs.writeText "${name}-checks.json" (builtins.toJSON checks); - in + { name, userNamespaces }: pkgs.testers.nixosTest { inherit name; - nodes = { - echo1 = { ... }: mkEchoNode; - echo2 = { ... }: mkEchoNode; - machine = machine; + nodes.machine = { + boot.consoleLogLevel = pkgs.lib.mkForce 3; + services.journald.settings.Journal.MaxLevelConsole = pkgs.lib.mkForce "notice"; + boot.kernel.sysctl."user.max_user_namespaces" = if userNamespaces then 1024 else 0; + users.users.sandbox-test = { + isNormalUser = true; + uid = 1000; + }; + environment.systemPackages = [ + helper + pkgs.python3 + pkgs.util-linux + ]; + virtualisation = { + memorySize = 1024; + cores = 2; + }; }; testScript = '' - start_all() - - echo1.wait_for_unit("echo-server.service") - echo1.wait_for_open_port(${toString echoPort}) - echo2.wait_for_unit("echo-server.service") - echo2.wait_for_open_port(${toString echoPort}) - + machine.start() machine.wait_for_unit("multi-user.target") - machine.wait_until_succeeds("getent hosts echo1", timeout=30) - machine.wait_until_succeeds("getent hosts echo2", timeout=30) - - # The unix-socket checks need a real peer outside the sandbox; wait for - # the listener to be up before running the helper. - machine.wait_until_succeeds("test -S ${unixSocketPath}", timeout=30) - - # The helper logs each check tagged `[sandbox_test]:`. `succeed` fails the - # whole test on a non-zero exit; we print its output so the per-check - # results show up in the build log. print(machine.succeed( - "ZED_SANDBOX_CHECKS=${checksFile} " - "ZED_TEST_ECHO_PORT=${toString echoPort} " - "bwrap-test-helper 2>&1" + "su -l sandbox-test -c 'timeout 180 sandbox-linux-test-helper ${ + if userNamespaces then "--require-sandbox" else "--expect-userns-disabled" + }' 2>&1", + timeout=200, )) ''; }; - in { - sandbox-working = mkTest { - name = "sandbox-working"; - machine = machineConfigs.working; - # Each check is written out in full (no shared fragments) so the policy and - # expected outcome for every case are obvious at a glance. - checks = [ - # Reads of host files are always allowed, regardless of the write policy. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "blocked"; - read = "/sandbox-test/readable/host.txt"; - succeeds = true; - } - - # Write inside the granted subtree: allowed. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "blocked"; - write = "/sandbox-test/writable/ok.txt"; - succeeds = true; - } - - # Write outside the granted subtree: denied. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "blocked"; - write = "/sandbox-test/forbidden/no.txt"; - succeeds = false; - } - - # A writable path that doesn't exist yet must be bound at its exact - # location (created up front), never widened to an existing ancestor. If it - # widened to `/sandbox-test`, this write to a read-only file under that - # ancestor would succeed — the scope-widening sandbox escape this guards - # against. It must be denied. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/not-yet-created/deep" ]; - networkAccess = "blocked"; - write = "/sandbox-test/readable/host.txt"; - succeeds = false; - } - - # ...and the not-yet-existing path itself is created and writable. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/not-yet-created/deep" ]; - networkAccess = "blocked"; - write = "/sandbox-test/not-yet-created/deep/ok.txt"; - succeeds = true; - } - - # Regression: a protected `.git` path routed through a regular file must - # not break sandbox creation. A single-file worktree (e.g. `settings.json` - # opened on its own) is rooted at the file, so the agent synthesizes - # `settings.json/.git` as a protected path; capturing it reports - # `NotADirectory`. That case must be *skipped* (the path can't exist, so - # there's nothing to protect), not treated as a fatal capture error — the - # bug that surfaced as a "cannot capture protected sandbox path - # .../settings.json/.git" popup. `seedFiles` makes `settings.json` a real - # file so the protected path routes through it; the sandbox must still be - # created and a normal write inside the worktree must succeed. - { - name = "protected .git routed through a regular file is skipped, not fatal"; - fs = "restricted"; - writablePaths = [ "/sandbox-test/single-file-worktree" ]; - seedFiles = [ "/sandbox-test/single-file-worktree/settings.json" ]; - protectedPaths = [ "/sandbox-test/single-file-worktree/settings.json/.git" ]; - networkAccess = "blocked"; - write = "/sandbox-test/single-file-worktree/ok.txt"; - succeeds = true; - } - - # The fs escape hatch lets a command write anywhere. - { - fs = "unrestricted"; - networkAccess = "blocked"; - write = "/sandbox-test/forbidden/anywhere.txt"; - succeeds = true; - } - - # ---- Protected paths --------------------------------------------------- - # A path inside a writable subtree can be re-bound read-only over the - # writable bind (order matters: later binds win). On Linux, protected paths - # remain readable but writes are denied. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/repo" ]; - protectedPaths = [ "/sandbox-test/repo/protected" ]; - networkAccess = "blocked"; - write = "/sandbox-test/repo/protected/test"; - succeeds = false; - } - - # Protected paths affect only the protected subtree: ordinary writes - # elsewhere in the writable worktree are unaffected. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/repo" ]; - protectedPaths = [ "/sandbox-test/repo/protected" ]; - networkAccess = "blocked"; - write = "/sandbox-test/repo/src/main.rs"; - succeeds = true; - } - - # Protected paths block writes but NOT reads on Linux because the protected - # subtree is read-only. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/repo" ]; - protectedPaths = [ "/sandbox-test/repo/protected" ]; - networkAccess = "blocked"; - read = "/sandbox-test/repo/protected/HEAD"; - succeeds = true; - } - - # Protected paths stay read-only even when ordinary filesystem writes are - # otherwise unrestricted. - { - fs = "unrestricted"; - protectedPaths = [ "/sandbox-test/repo/protected" ]; - networkAccess = "blocked"; - write = "/sandbox-test/repo/protected/test"; - succeeds = false; - } - - # Blocked network: the echo server is unreachable. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "blocked"; - network = "echo1"; - succeeds = false; - } - - # Unrestricted network: both echo servers are reachable. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "unrestricted"; - network = "echo1"; - succeeds = true; - } - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "unrestricted"; - network = "echo2"; - succeeds = true; - } - - # Restricted network: only the allowlisted host is reachable. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "restricted"; - allowedDomains = [ "echo1" ]; - network = "echo1"; - succeeds = true; - } - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "restricted"; - allowedDomains = [ "echo1" ]; - network = "echo2"; - succeeds = false; - } - - # ---- Unix-domain socket escape ---------------------------------------- - # A sandboxed command must NOT be able to connect to a unix-domain socket - # owned by a process outside the sandbox (session-IPC escape). Currently - # FAILS (no seccomp guard yet); becomes a regression test once the - # socket(AF_UNIX) seccomp filter lands. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "blocked"; - socketPath = unixSocketPath; - succeeds = false; - } - - # The unix-socket block must hold regardless of network policy: our design - # decouples unix-socket blocking from the network grant, so even an - # unrestricted-network command must not reach an outside-the-sandbox - # session IPC socket. Also currently FAILS until the seccomp filter lands. - { - fs = "restricted"; - writablePaths = [ "/sandbox-test/writable" ]; - networkAccess = "unrestricted"; - socketPath = unixSocketPath; - succeeds = false; - } - - # On a working host the sandbox can be created. - { - fs = "restricted"; - networkAccess = "blocked"; - canCreate = true; - } - ]; + sandbox-linux-working = mkTest { + name = "sandbox-linux-working"; + userNamespaces = true; }; - - sandbox-no-bwrap = mkTest { - name = "sandbox-no-bwrap"; - machine = machineConfigs.no-bwrap; - checks = [ { canCreate = false; error = "bwrap_not_found"; } ]; - }; - - sandbox-setuid-bwrap = mkTest { - name = "sandbox-setuid-bwrap"; - machine = machineConfigs.setuid-bwrap; - checks = [ { canCreate = false; error = "setuid_rejected"; } ]; - }; - - sandbox-userns-disabled = mkTest { - name = "sandbox-userns-disabled"; - machine = machineConfigs.userns-disabled; - checks = [ { canCreate = false; error = "probe_failed"; } ]; + sandbox-linux-userns-disabled = mkTest { + name = "sandbox-linux-userns-disabled"; + userNamespaces = false; }; } diff --git a/nix/tests/sandboxing/helper.nix b/nix/tests/sandboxing/helper.nix index b157ed21661..0f885d8958b 100644 --- a/nix/tests/sandboxing/helper.nix +++ b/nix/tests/sandboxing/helper.nix @@ -1,5 +1,4 @@ -# Builds the `bwrap-test-helper` binary (sandbox crate, `nixos-test` feature) -# used by the Bubblewrap sandbox VM tests in this directory. +# Builds the re-executable sandbox_linux end-to-end harness for the VM tests. { pkgs, inputs, @@ -30,15 +29,18 @@ let ".config" ".cargo" ]; - name = "bwrap-test-helper-source"; + name = "sandbox-linux-test-helper-source"; }; commonArgs = { - pname = "bwrap-test-helper"; + pname = "sandbox-linux-test-helper"; + # An example, not a test target (see crates/sandbox_linux/Cargo.toml): + # built and run here only, never by CI's `cargo nextest run --workspace`. + cargoExtraArgs = "-p sandbox_linux --example end_to_end --features __end_to_end_harness --locked"; + cargoBuildCommand = "cargoWithProfile test --no-run"; version = "0.0.0"; inherit src; cargoLock = ../../../Cargo.lock; - cargoExtraArgs = "-p sandbox --bin bwrap-test-helper --features sandbox/nixos-test --locked"; CARGO_PROFILE = "dev"; doCheck = false; @@ -58,12 +60,19 @@ craneLib.buildPackage ( installPhase = '' runHook preInstall mkdir -p $out/bin - cp target/debug/bwrap-test-helper $out/bin/bwrap-test-helper + # Dependency artifacts can contain a dummy harness with another hash. + helper="$(${pkgs.jq}/bin/jq -ers ' + [.[] | select(.reason == "compiler-artifact" + and .target.name == "end_to_end" and .profile.test == true) + | .executable | select(. != null)] + | if length == 1 then .[0] else error("expected one test executable") end + ' "$cargoBuildLog")" + cp "$helper" $out/bin/sandbox-linux-test-helper runHook postInstall ''; meta = { - description = "Bubblewrap sandbox behavior test helper"; + description = "Sandbox behavior test helper"; platforms = lib.platforms.linux; }; } diff --git a/tooling/xtask/src/main.rs b/tooling/xtask/src/main.rs index 9594fc6b65c..945768f8763 100644 --- a/tooling/xtask/src/main.rs +++ b/tooling/xtask/src/main.rs @@ -22,7 +22,7 @@ enum CliCommand { /// Inspect the GPUI publish plan and its dependencies. #[command(subcommand)] Gpui(tasks::gpui::GpuiCommand), - /// Runs the Linux Bubblewrap sandboxing NixOS VM tests. + /// Runs the sandbox_linux NixOS VM tests. SandboxTests(tasks::sandbox_tests::SandboxTestsArgs), /// Runs the Windows WSL Bubblewrap sandbox behavior tests. WslSandboxTests(tasks::wsl_sandbox_tests::WslSandboxTestsArgs), diff --git a/tooling/xtask/src/tasks/sandbox_tests.rs b/tooling/xtask/src/tasks/sandbox_tests.rs index bf68ca27b43..e7fd5e1ece3 100644 --- a/tooling/xtask/src/tasks/sandbox_tests.rs +++ b/tooling/xtask/src/tasks/sandbox_tests.rs @@ -5,18 +5,17 @@ use std::process::Command; use anyhow::{Context as _, Result, bail}; use clap::Parser; -/// Runs the Linux Bubblewrap sandboxing NixOS VM tests (see `nix/tests/sandboxing`). +/// Runs the Linux sandboxing NixOS VM tests (see `nix/tests/sandboxing`). /// -/// Each test boots a real kernel under QEMU to exercise bwrap behavior across -/// host configurations: a working host (the sandbox must be enforced and the -/// full fs x network policy matrix must hold), and degraded hosts (no bwrap, -/// setuid-only bwrap, user namespaces disabled) where `Sandbox::can_create` -/// must report the specific failure and the consumer must fail closed. They -/// therefore only run on Linux with `/dev/kvm` available, and require `nix` -/// with flakes enabled. +/// Each test boots a real kernel under QEMU. The sandbox_linux checks run its +/// re-executable end-to-end harness (an example, never a cargo test target) as +/// an unprivileged user, requiring successful sandbox setup on the working host +/// and fail-closed execution when user namespaces are disabled. +/// They require Linux with `/dev/kvm` available, and `nix` with flakes enabled. +/// Nothing in CI runs these; this task and `nix flake check` are the entry points. #[derive(Parser)] pub struct SandboxTestsArgs { - /// Names of specific tests to run (e.g. `sandbox-working`). Defaults + /// Names of specific tests to run (e.g. `sandbox-linux-working`). Defaults /// to all. tests: Vec,