mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
3dcdee6d2b
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
123 lines
3.9 KiB
Rust
123 lines
3.9 KiB
Rust
#![allow(clippy::disallowed_methods, reason = "tooling is exempt")]
|
|
|
|
use std::process::Command;
|
|
|
|
use anyhow::{Context as _, Result, bail};
|
|
use clap::Parser;
|
|
|
|
/// Runs the Linux sandboxing NixOS VM tests (see `nix/tests/sandboxing`).
|
|
///
|
|
/// 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-linux-working`). Defaults
|
|
/// to all.
|
|
tests: Vec<String>,
|
|
|
|
/// Nix system to build for. Defaults to the host system.
|
|
#[arg(long)]
|
|
system: Option<String>,
|
|
|
|
/// Force already-built tests to re-run instead of returning the cached
|
|
/// result (passes `--rebuild` to `nix build`).
|
|
///
|
|
/// This only works once a test has been built at least once; after changing
|
|
/// the test code the derivation changes, so run without `--rebuild` first.
|
|
#[arg(long)]
|
|
rebuild: bool,
|
|
|
|
/// List the available tests and exit without running anything.
|
|
#[arg(long)]
|
|
list: bool,
|
|
}
|
|
|
|
/// Tests in `nix/tests/sandboxing` are exposed as flake checks with this prefix.
|
|
const TEST_PREFIX: &str = "sandbox-";
|
|
|
|
pub fn run_sandbox_tests(args: SandboxTestsArgs) -> Result<()> {
|
|
let system = args.system.unwrap_or_else(host_system);
|
|
let available = available_tests(&system)?;
|
|
|
|
if args.list {
|
|
for name in &available {
|
|
println!("{name}");
|
|
}
|
|
return Ok(());
|
|
}
|
|
|
|
let selected = if args.tests.is_empty() {
|
|
available
|
|
} else {
|
|
for name in &args.tests {
|
|
if !available.contains(name) {
|
|
bail!(
|
|
"unknown sandbox test {name:?}; available: {}",
|
|
available.join(", ")
|
|
);
|
|
}
|
|
}
|
|
args.tests
|
|
};
|
|
|
|
if selected.is_empty() {
|
|
bail!("no sandbox tests found for system `{system}`");
|
|
}
|
|
|
|
let mut command = Command::new("nix");
|
|
command.args(["build", "-L", "--keep-going"]);
|
|
if args.rebuild {
|
|
command.arg("--rebuild");
|
|
}
|
|
for name in &selected {
|
|
command.arg(format!(".#checks.{system}.{name}"));
|
|
}
|
|
|
|
eprintln!("Running sandbox tests: {}", selected.join(", "));
|
|
let status = command.status().context("failed to run `nix build`")?;
|
|
if !status.success() {
|
|
bail!("sandbox tests failed");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// The Nix system double for the host. These tests are Linux-only, so we always
|
|
/// target a `*-linux` system (overridable via `--system`).
|
|
fn host_system() -> String {
|
|
format!("{}-linux", std::env::consts::ARCH)
|
|
}
|
|
|
|
/// Query the flake for the names of the sandbox-test checks, so the task stays
|
|
/// in sync with `nix/tests/sandboxing` without a hardcoded list.
|
|
fn available_tests(system: &str) -> Result<Vec<String>> {
|
|
let output = Command::new("nix")
|
|
.args([
|
|
"eval",
|
|
&format!(".#checks.{system}"),
|
|
"--apply",
|
|
"checks: builtins.concatStringsSep \"\\n\" (builtins.attrNames checks)",
|
|
"--raw",
|
|
])
|
|
.output()
|
|
.context("failed to run `nix eval` to list checks (is `nix` installed?)")?;
|
|
if !output.status.success() {
|
|
bail!(
|
|
"`nix eval` failed: {}",
|
|
String::from_utf8_lossy(&output.stderr).trim()
|
|
);
|
|
}
|
|
|
|
let names = String::from_utf8(output.stdout).context("`nix eval` output was not UTF-8")?;
|
|
let mut tests: Vec<String> = names
|
|
.lines()
|
|
.filter(|name| name.starts_with(TEST_PREFIX))
|
|
.map(str::to_string)
|
|
.collect();
|
|
tests.sort();
|
|
Ok(tests)
|
|
}
|