diff --git a/.github/workflows/security-linux.yml b/.github/workflows/security-linux.yml new file mode 100644 index 0000000..5c6218a --- /dev/null +++ b/.github/workflows/security-linux.yml @@ -0,0 +1,31 @@ +name: Linux strict security lane (G-C) + +# This workflow intentionally does NOT run on GitHub-hosted runners. G-C +# requires a disposable Ubuntu VM/self-hosted runner with permitted user, +# mount, PID and network namespaces and the Bubblewrap AppArmor profile. +# A missing runner or a skipped test fails the gate; it is never counted as a +# pass. See docs/security-gate.md and ADR-0021. + +on: + workflow_dispatch: + schedule: + - cron: "17 3 * * 1" + +jobs: + strict-security: + name: strict isolation escape matrix + runs-on: [self-hosted, linux, marqdo-security] + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - name: Host capability report + run: | + id + uname -a + systemd-detect-virt --container || true + bwrap --version + cc --version | head -1 + cat /proc/self/uid_map + findmnt -t cgroup2 || true + - name: Run G-C matrix + run: ./tests/security/run-lane.sh diff --git a/.gitignore b/.gitignore index 0bae9f9..6bb56a4 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ examples/plugin-multi-export/build/ # Release artifacts dist/ + +# Local VM/QEMU tooling and disk images +.tools/ diff --git a/CHANGELOG.md b/CHANGELOG.md index ae72d92..398cc1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,14 @@ currently `0.1.0`; no release tag has been cut. Dates use ISO 8601. `entrypoint` must resolve inside `environment_dir`, built-in providers cannot be bound to one, the child gets only the environment executable directory on `PATH`, and `interpreter_version` is verified before execution. +- Linux strict isolation (ADR-0021): a Bubblewrap supervisor with user/mount/ + PID/net/UTS/IPC namespaces, no-new-privs, capability dropping, a read-only + `/project`, rlimits, output/wall-clock supervision and child + self-verification. Probe-gated; L1 supports pure Marqdo + Math only. +- Adversarial G-C lane (`tests/security/escape_helper.c`, + `crates/marqdo-sandbox/tests/security_linux.rs`, `tests/security/run-lane.sh`) + covering filesystem, `/proc`, network, process, privilege and resource + escapes. 7/7 PASS in a disposable Ubuntu 26.04 VM. - Release documentation (`COMPATIBILITY.md`, `docs/releasing.md`) and the Linux security-gate evidence report (`docs/security-gate.md`). - `check_never_spawns_a_foreign_kernel`: proves static `check` does not spawn a @@ -43,10 +51,12 @@ currently `0.1.0`; no release tag has been cut. Dates use ISO 8601. ### Security -- `strict` remains `Unavailable` on every platform. This host cannot create - user/network namespaces (`EPERM` inside a container, cgroup2 read-only, no - non-interactive sudo), so no Linux OS-isolation adapter was implemented or - verified. There is no silent downgrade to `project` mode. +- Linux `strict` is implemented and probe-gated. It is `Available` only when a + real Bubblewrap smoke test succeeds; otherwise it stays `Unavailable` and + refuses. There is no silent downgrade to `project`. +- Strict L1 covers pure Marqdo and native Math; Foreign, native/worker + plugins, kernels, external grants and trace files are rejected before the + sandbox starts. Strict L2 inheritance is future work. - Capability brokers and worker processes remain cooperative authorization/crash isolation, not an OS sandbox. diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md index 37ef301..056b805 100644 --- a/COMPATIBILITY.md +++ b/COMPATIBILITY.md @@ -34,7 +34,7 @@ portable/tested locally but the native platform lane has not run here; | Worker ABI + `fs.read` bridge | PASS (Unix) | UNVERIFIED | UNSUPPORTED (tests are Unix-gated) | | Foreign Python/Node | PASS (system `python3`/`node`) | UNVERIFIED | UNVERIFIED | | Foreign pinned environment binding | PASS | UNVERIFIED | UNVERIFIED | -| `strict` OS isolation | **FAIL-CLOSED / G-C NOT PASSED** | FAIL-CLOSED | FAIL-CLOSED | +| `strict` OS isolation | PASS when runtime probe passes (VM-verified); no CI URL yet | FAIL-CLOSED | FAIL-CLOSED | The Ubuntu column is from this development host (Ubuntu 26.04 container). It is **not** a GitHub Actions result: this checkout has no GitHub remote or CI @@ -46,14 +46,16 @@ reproduce the Windows checkout byte behavior. - Rust 1.82+ (verified with 1.100.0). - Linux/macOS: a C compiler for Native ABI fixtures; Python/Node only for Foreign kernels. -- `strict` additionally requires a kernel/environment that permits user, - mount, PID and network namespaces, plus delegated cgroups. The current - container does not, so strict is refused. See - [`docs/security-gate.md`](docs/security-gate.md). +- `strict` additionally requires Bubblewrap and a kernel/environment that + permits user, mount, PID and network namespaces. It is enabled only when the + runtime probe passes; the current development container fails that probe, so + strict is refused there. See [`docs/security-gate.md`](docs/security-gate.md) + and ADR-0021. ## Explicitly unsupported in this candidate -- OS-level strict isolation on any platform (G-C not passed). +- OS-level strict isolation on macOS/Windows (Linux only, probe-gated). +- Strict L2: Worker/Foreign execution inside strict (refused in L1). - Windows Native/Worker ABI runtime support. - Automatic uv/npm/venv provisioning; only an explicitly provisioned, pinned directory is verified (and must contain a regular-file executable). diff --git a/Cargo.lock b/Cargo.lock index 7a504b0..fda593c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -497,6 +497,7 @@ dependencies = [ name = "marqdo-sandbox" version = "0.1.0" dependencies = [ + "libc", "serde", ] diff --git a/README.md b/README.md index 195faba..e9e21e6 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,11 @@ plugin ABI v2 are not claimed to work here. > Foreign providers are implemented and covered by tests on Linux. The > Windows AST and macOS plugin CI failures are fixed in source, but the native > Windows/macOS lanes have not been re-run from this checkout. **`strict` OS -> isolation is not available**: it fails closed on every platform (G-C not -> passed), and the broker/worker are not a sandbox. See +> isolation is available on Linux only when the runtime Bubblewrap probe +> passes**; it was verified against the full escape/resource matrix in a +> disposable Ubuntu VM (ADR-0021). macOS/Windows fail closed, strict L2 +> (Worker/Foreign) is not implemented, and the broker/worker are not a +> sandbox. See > [`docs/STATUS.md`](docs/STATUS.md), [`COMPATIBILITY.md`](COMPATIBILITY.md) > and [`docs/security-gate.md`](docs/security-gate.md). The single next-phase > plan is [`docs/STABLE_RELEASE_IMPLEMENTATION.md`](docs/STABLE_RELEASE_IMPLEMENTATION.md). @@ -90,8 +93,10 @@ executes anything. `run`/`call`/`check` never fetch or rewrite the lock. * **P4 security core** — capability-checked host operations, project-relative FS globs with traversal rejection, budgets, effect trace, `pure`/`project` - modes, and **`strict` mode fails closed** because no verified OS isolation - adapter exists. + modes, and **Linux `strict` mode** using Bubblewrap namespaces, no-new-privs, + capability dropping, rlimits, a wall-clock supervisor and child + self-verification (ADR-0021). It is probe-gated and verified in a disposable + Ubuntu VM; unsupported hosts fail closed. * **P5 Math/Foreign** — native `Formula` value from `$$...$$`; `math.parse/ eval/substitute` over a documented exact-rational subset; one-shot Python/ Node kernels via `foreign.run/call` with typed wire protocol, timeout and @@ -117,9 +122,12 @@ cargo clippy --workspace --all-targets -- -D warnings ## Deliberate limits (do not assume) -* **Strict sandbox is not implemented.** `--mode strict` exits 1; there is no - verified OS isolation. `project` mode is cooperative and in-process. Worker - plugins provide crash/address-space isolation, not an OS sandbox. +* **Strict is Linux-only and L1-only.** `--mode strict` enables the + Bubblewrap supervisor only when its runtime probe passes; otherwise it exits + 1. It supports pure Marqdo + Math and rejects Foreign, plugins, kernels, + external grants and trace files (exit 2). Worker plugins provide + crash/address-space isolation, not an OS sandbox, and are not allowed in + strict. * **Native plugins run in-process** and require an explicit unsafe trust flag. A plugin can crash or read the host; a C ABI is not a sandbox. * **Package management is path + pinned-Git only.** There is no registry, diff --git a/crates/cli/src/commands.rs b/crates/cli/src/commands.rs index 8bb8f36..3e78f62 100644 --- a/crates/cli/src/commands.rs +++ b/crates/cli/src/commands.rs @@ -59,6 +59,31 @@ impl SandboxSettings { }) } + /// Reject strict-mode requests that this L1 isolation level does not + /// support. Strict currently covers pure Marqdo and native Math only; + /// granting Foreign, ABI plugins, tools or trace files would require + /// extending the supervisor contract first. + pub fn strict_preflight(&self) -> Result<(), String> { + let requested = self.allow_foreign + || !self.allow_read.is_empty() + || !self.allow_write.is_empty() + || !self.allow_plugin.is_empty() + || !self.plugins.is_empty() + || self.trust_native_plugin + || !self.kernels.is_empty() + || self.trace_file.is_some(); + if requested { + return Err( + "strict mode currently supports pure Marqdo and native Math only; \ + --allow-read/--allow-write/--allow-foreign/--allow-plugin/--plugin/\ + --trust-native-plugin/--kernel/--trace-file are rejected. Use \ + --mode project for those capabilities." + .to_string(), + ); + } + Ok(()) + } + /// Build the effective capability set. pub fn capabilities(&self) -> Result { let mode = self.sandbox_mode()?; @@ -91,7 +116,10 @@ impl SandboxSettings { } Ok(capabilities) } - SandboxMode::Strict => Ok(Capabilities::project()), + SandboxMode::Strict => { + self.strict_preflight()?; + Ok(Capabilities::project()) + } } } } @@ -625,6 +653,43 @@ fn resolve_link( } } +/// Re-execute this CLI inside the strict Linux supervisor. +fn launch_strict_process( + project_root: &Path, + document: Option<&Path>, + settings: &SandboxSettings, +) -> i32 { + let program = match std::env::current_exe() { + Ok(path) => path, + Err(error) => { + eprintln!("error: cannot locate the marqdo executable: {error}"); + return 1; + } + }; + let args: Vec = std::env::args_os().skip(1).collect(); + let output_limit = settings + .max_output + .unwrap_or(1024 * 1024) + .min(64 * 1024 * 1024); + let timeout = Duration::from_secs(settings.timeout.unwrap_or(30)); + let command = marqdo_sandbox::StrictHostCommand { + program: &program, + args: &args, + project_root, + document, + original_document: document, + output_limit, + timeout, + }; + match marqdo_sandbox::launch_strict(&command) { + Ok(code) => code, + Err(error) => { + eprintln!("error: {error}"); + 1 + } + } +} + /// `marqdo run` / `marqdo call`. pub fn execute( file: Option<&Path>, @@ -676,6 +741,42 @@ pub fn execute( return 2; } }; + if mode == SandboxMode::Strict { + if let Err(error) = settings.strict_preflight() { + if json { + output::print_json(&json!({"success": false, "error": error})); + } else { + eprintln!("error: {error}"); + } + return 2; + } + if marqdo_sandbox::marker_present() { + // We were re-executed by the supervisor: validate the marker set + // before running any project code. + if let Err(reason) = marqdo_sandbox::verify_active() { + if json { + output::print_json(&json!({ + "success": false, + "error": format!("strict isolation verification failed: {reason}"), + })); + } else { + eprintln!("error: strict isolation verification failed: {reason}"); + } + return 1; + } + } else { + let platform = PlatformCapabilities::detect(); + if let Err(error) = marqdo_sandbox::enforce(mode, &platform) { + if json { + output::print_json(&json!({"success": false, "error": error.to_string()})); + } else { + eprintln!("error: {error}"); + } + return 1; + } + return launch_strict_process(&project.root, file, settings); + } + } let platform = PlatformCapabilities::detect(); if let Err(error) = marqdo_sandbox::enforce(mode, &platform) { if json { diff --git a/crates/cli/tests/integration_cli.rs b/crates/cli/tests/integration_cli.rs index 2212c2b..aa68899 100644 --- a/crates/cli/tests/integration_cli.rs +++ b/crates/cli/tests/integration_cli.rs @@ -331,11 +331,42 @@ fn examples_run_end_to_end_including_foreign_when_available() { ); } +fn strict_is_available(root: &Path) -> bool { + let hello = root.join("examples").join("hello.mq.md"); + let output = cli( + root, + &["inspect", hello.to_str().unwrap(), "--platform", "--json"], + ); + if !output.status.success() { + return false; + } + let value = json_stdout(&output); + value["platform"]["strict"]["state"] == "available" +} + #[test] -fn strict_mode_refuses_without_isolation() { +fn strict_mode_enforces_isolation_or_refuses_closed() { let root = repo_root(); let hello = root.join("examples").join("hello.mq.md"); let output = cli(&root, &["run", hello.to_str().unwrap(), "--mode", "strict"]); + if strict_is_available(&root) { + assert_exit(&output, 0, "strict mode must succeed when the gate passes"); + assert_eq!(stdout_text(&output), "你好,Marqdo\n"); + // External execution surfaces are rejected at the strict L1 boundary. + let foreign = cli( + &root, + &[ + "run", + hello.to_str().unwrap(), + "--mode", + "strict", + "--allow-foreign", + ], + ); + assert_exit(&foreign, 2, "strict must reject foreign grants"); + assert!(stderr_text(&foreign).contains("strict mode currently supports")); + return; + } assert_exit( &output, 1, @@ -763,7 +794,7 @@ fn runtime_error_in_imported_module_reports_that_module() { } #[test] -fn platform_reports_strict_unavailable_until_security_gate_passes() { +fn platform_reports_strict_capability_truthfully() { let root = repo_root(); let hello = root.join("examples").join("hello.mq.md"); let output = cli( @@ -773,17 +804,27 @@ fn platform_reports_strict_unavailable_until_security_gate_passes() { assert_exit(&output, 0, "inspect --platform"); let value = json_stdout(&output); let strict = &value["platform"]["strict"]; - assert_eq!( - strict["state"], "unavailable", - "strict must stay unavailable until the ADR-0019 security gate passes: {strict}" - ); - assert!( - strict["reason"] - .as_str() - .map(|reason| !reason.trim().is_empty()) - .unwrap_or(false), - "unavailable strict needs an actionable reason: {strict}" - ); + match strict["state"].as_str() { + Some("available") => { + assert!( + strict["mechanism"] + .as_str() + .map(|mechanism| mechanism.contains("bubblewrap")) + .unwrap_or(false), + "available strict needs the real mechanism: {strict}" + ); + } + Some("unavailable") => { + assert!( + strict["reason"] + .as_str() + .map(|reason| !reason.trim().is_empty()) + .unwrap_or(false), + "unavailable strict needs an actionable reason: {strict}" + ); + } + other => panic!("unexpected strict state: {other:?} ({strict})"), + } assert_eq!(value["platform"]["worker_processes"], true); } diff --git a/crates/marqdo-sandbox/Cargo.toml b/crates/marqdo-sandbox/Cargo.toml index d2a8383..9ca2563 100644 --- a/crates/marqdo-sandbox/Cargo.toml +++ b/crates/marqdo-sandbox/Cargo.toml @@ -11,3 +11,6 @@ authors.workspace = true [dependencies] serde = { workspace = true } +[target.'cfg(unix)'.dependencies] +libc = { workspace = true } + diff --git a/crates/marqdo-sandbox/src/lib.rs b/crates/marqdo-sandbox/src/lib.rs index 9114707..7478c56 100644 --- a/crates/marqdo-sandbox/src/lib.rs +++ b/crates/marqdo-sandbox/src/lib.rs @@ -5,8 +5,16 @@ //! provide OS-level isolation: a cooperative in-process check is not a sandbox //! (blueprint section 9). This crate is deliberately honest about that. +use std::path::Path; +use std::time::Duration; + use serde::{Deserialize, Serialize}; +#[cfg(target_os = "linux")] +mod linux; +#[cfg(target_os = "linux")] +pub use linux::{marker_present, probe, verify_active, StrictOutcome}; + /// Execution mode requested on the command line. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] @@ -103,21 +111,29 @@ impl PlatformCapabilities { /// security contract. The isolated worker transport is implemented, but a /// plain child process is not an OS sandbox. pub fn detect() -> Self { - let reason = if cfg!(target_os = "linux") { - "no OS isolation adapter (namespaces/seccomp/cgroups) is implemented in this build" + let strict = if cfg!(target_os = "linux") { + match linux::probe() { + Ok(mechanism) => StrictIsolation::Available { mechanism }, + Err(reason) => StrictIsolation::Unavailable { reason }, + } } else if cfg!(target_os = "macos") { - "no verified macOS sandbox adapter is implemented in this build" + StrictIsolation::Unavailable { + reason: "no verified macOS sandbox adapter is implemented in this build" + .to_string(), + } } else if cfg!(target_os = "windows") { - "no verified Windows job-object/appcontainer adapter is implemented in this build" + StrictIsolation::Unavailable { + reason: "no verified Windows job-object/appcontainer adapter is implemented in this build".to_string(), + } } else { - "platform isolation is not implemented for this target" + StrictIsolation::Unavailable { + reason: "platform isolation is not implemented for this target".to_string(), + } }; Self { os: std::env::consts::OS.to_string(), arch: std::env::consts::ARCH.to_string(), - strict: StrictIsolation::Unavailable { - reason: reason.to_string(), - }, + strict, worker_processes: true, } } @@ -131,6 +147,11 @@ pub enum SandboxError { /// Explanation from [`PlatformCapabilities`]. reason: String, }, + /// The strict supervisor could not launch, verify or supervise the child. + LaunchFailed { + /// Actionable explanation. + message: String, + }, } impl std::fmt::Display for SandboxError { @@ -140,6 +161,9 @@ impl std::fmt::Display for SandboxError { f, "refusing to run in strict mode: {reason}. Strict mode requires verified OS-level isolation and never falls back to an unrestricted process." ), + SandboxError::LaunchFailed { message } => { + write!(f, "strict isolation failed: {message}") + } } } } @@ -150,6 +174,10 @@ impl std::error::Error for SandboxError {} pub fn enforce(mode: SandboxMode, platform: &PlatformCapabilities) -> Result<(), SandboxError> { match mode { SandboxMode::Strict => { + #[cfg(target_os = "linux")] + if verify_active().is_ok() { + return Ok(()); + } if platform.strict.is_available() { Ok(()) } else { @@ -164,6 +192,49 @@ pub fn enforce(mode: SandboxMode, platform: &PlatformCapabilities) -> Result<(), } } +/// A command to execute inside the strict Linux sandbox. +#[derive(Debug, Clone)] +pub struct StrictHostCommand<'a> { + /// Program bound read-only into the sandbox and executed. + pub program: &'a Path, + /// Arguments passed to the program. + pub args: &'a [std::ffi::OsString], + /// Project directory mounted read-only at `/project`. + pub project_root: &'a Path, + /// Entry document as passed on the command line; rewritten to the sandbox + /// path. `None` keeps the CLI default (index.mq.md). + pub document: Option<&'a Path>, + /// Original document token to replace in `args` (same value as `document` + /// when present). + pub original_document: Option<&'a Path>, + /// Combined per-stream output cap in bytes. + pub output_limit: usize, + /// Wall-clock budget for the supervised process tree. + pub timeout: Duration, +} + +/// Launch a command inside the strict sandbox (Linux only). +#[cfg(target_os = "linux")] +pub fn launch_strict(command: &StrictHostCommand<'_>) -> Result { + linux::launch(command) +} + +/// Launch a command inside the strict sandbox and return its captured outcome. +#[cfg(target_os = "linux")] +pub fn launch_strict_capture( + command: &StrictHostCommand<'_>, +) -> Result { + linux::launch_capture(command) +} + +/// Launch a command inside the strict sandbox (unsupported elsewhere). +#[cfg(not(target_os = "linux"))] +pub fn launch_strict(_command: &StrictHostCommand<'_>) -> Result { + Err(SandboxError::StrictUnavailable { + reason: "strict OS isolation is only implemented on Linux".to_string(), + }) +} + #[cfg(test)] mod tests { use super::*; @@ -178,7 +249,14 @@ mod tests { #[test] fn strict_refuses_when_isolation_is_unavailable() { - let platform = PlatformCapabilities::detect(); + let platform = PlatformCapabilities { + os: "test".to_string(), + arch: "test".to_string(), + strict: StrictIsolation::Unavailable { + reason: "test host has no verified isolation".to_string(), + }, + worker_processes: true, + }; let result = enforce(SandboxMode::Strict, &platform); assert!(result.is_err()); assert!(format!("{}", result.unwrap_err()).contains("strict")); diff --git a/crates/marqdo-sandbox/src/linux.rs b/crates/marqdo-sandbox/src/linux.rs new file mode 100644 index 0000000..4e3b33d --- /dev/null +++ b/crates/marqdo-sandbox/src/linux.rs @@ -0,0 +1,477 @@ +//! Linux strict-isolation supervisor. +//! +//! The mechanism is Bubblewrap (`bwrap`) plus a small trusted supervisor in +//! this process: +//! +//! * user, mount, PID, network, UTS, IPC and cgroup namespaces +//! (`--unshare-all --as-pid-1`); +//! * a read-only view of the runtime libraries, the project root and the +//! program itself; no host `/etc`, `/root`, `$HOME` or other projects; +//! * a tmpfs `/tmp`, a private `/proc` and a minimal `/dev`; +//! * `no_new_privs`, all capabilities dropped, empty environment; +//! * hard rlimits for CPU, address space, file size, file descriptors, +//! processes, stack and core dumps; +//! * a wall-clock supervisor with process-group cleanup and bounded captured +//! output. +//! +//! Bubblewrap is a construction mechanism, not a certificate: [`probe`] runs +//! a real sandbox smoke test at runtime and strict is reported unavailable if +//! the host cannot create the namespaces. The sandboxed process additionally +//! verifies its own markers (`PID 1`, `NoNewPrivs`, unshared uid map, no +//! non-loopback interface) before executing user code. + +use std::ffi::OsString; +use std::fs; +use std::io::{self, Read, Write}; +use std::os::unix::process::{CommandExt, ExitStatusExt}; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, ExitStatus, Stdio}; +use std::time::{Duration, Instant}; + +use crate::{SandboxError, StrictHostCommand}; + +/// Environment variable that marks a re-executed strict child. +pub const ACTIVE_ENV: &str = "MARQDO_STRICT_ACTIVE"; + +/// Path of the program inside the sandbox. +const SANDBOX_PROGRAM: &str = "/run/marqdo-program"; + +/// Candidate absolute Bubblewrap paths. `PATH` lookup is deliberately avoided +/// so a project-local binary cannot impersonate the supervisor tool. +const BWRAP_PATHS: [&str; 3] = ["/usr/bin/bwrap", "/bin/bwrap", "/usr/local/bin/bwrap"]; + +const PROBE_TIMEOUT: Duration = Duration::from_secs(5); +const MAX_STACK_BYTES: u64 = 8 * 1024 * 1024; +const MAX_ADDRESS_SPACE: u64 = 512 * 1024 * 1024; +const MAX_PROCESSES: u64 = 64; +const MAX_OPEN_FILES: u64 = 64; + +fn bwrap_path() -> Option { + BWRAP_PATHS + .iter() + .map(PathBuf::from) + .find(|path| path.is_file()) +} + +/// Probe the real availability of the strict mechanism. +pub fn probe() -> Result { + let bwrap = bwrap_path().ok_or_else(|| { + "bubblewrap (`bwrap`) is not installed; install it or use project mode".to_string() + })?; + let version = bwrap_version(&bwrap)?; + let mut command = Command::new(&bwrap); + command + .args(common_args()) + .args(["--", "/bin/true"]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + let mut child = command + .spawn() + .map_err(|error| format!("cannot start the bubblewrap probe: {error}"))?; + let status = wait_with_deadline(&mut child, PROBE_TIMEOUT)?; + let mut stderr = String::new(); + if let Some(mut pipe) = child.stderr.take() { + let _ = pipe.read_to_string(&mut stderr); + } + if !status.success() { + return Err(format!( + "bubblewrap cannot create the required namespaces: {}", + stderr.trim() + )); + } + Ok(format!( + "bubblewrap {version} (user/mount/PID/net/UTS/IPC namespaces + no-new-privs + rlimits)" + )) +} + +fn bwrap_version(bwrap: &Path) -> Result { + let output = Command::new(bwrap) + .arg("--version") + .stdin(Stdio::null()) + .output() + .map_err(|error| format!("cannot run `{}`: {error}", bwrap.display()))?; + if !output.status.success() { + return Err("`bwrap --version` failed".to_string()); + } + let text = String::from_utf8_lossy(&output.stdout); + let version = text + .lines() + .next() + .unwrap_or("") + .split_whitespace() + .nth(1) + .unwrap_or("unknown") + .to_string(); + Ok(version) +} + +fn common_args() -> Vec { + let mut args: Vec = [ + "--unshare-all", + "--as-pid-1", + "--die-with-parent", + "--new-session", + "--cap-drop", + "ALL", + "--clearenv", + "--setenv", + "PATH", + "/usr/bin:/bin", + "--setenv", + "HOME", + "/tmp", + "--setenv", + "LANG", + "C.UTF-8", + "--proc", + "/proc", + "--dev", + "/dev", + "--tmpfs", + "/tmp", + "--dir", + "/run", + ] + .into_iter() + .map(OsString::from) + .collect(); + for directory in ["/usr", "/bin", "/sbin", "/lib", "/lib64"] { + let path = Path::new(directory); + if path.exists() { + args.extend([ + OsString::from("--ro-bind"), + OsString::from(directory), + OsString::from(directory), + ]); + } + } + if Path::new("/etc/ld.so.cache").is_file() { + args.extend([ + OsString::from("--ro-bind"), + OsString::from("/etc/ld.so.cache"), + OsString::from("/etc/ld.so.cache"), + ]); + } + args +} + +/// Verify that this process really is the strict child created by [`launch`]. +pub fn verify_active() -> Result<(), String> { + if std::env::var(ACTIVE_ENV).ok().as_deref() != Some("1") { + return Err("strict active marker is absent".to_string()); + } + // `--as-pid-1` makes the supervised program PID 1 in its own PID namespace. + let pid = unsafe { libc::getpid() }; + if pid != 1 { + return Err(format!( + "strict child is not PID 1 in a private PID namespace (pid {pid})" + )); + } + let status = fs::read_to_string("/proc/self/status") + .map_err(|error| format!("cannot read /proc/self/status: {error}"))?; + if !status.lines().any(|line| line == "NoNewPrivs:\t1") { + return Err("strict child does not have no_new_privs set".to_string()); + } + let effective = status + .lines() + .find_map(|line| line.strip_prefix("CapEff:\t")) + .unwrap_or(""); + if effective.bytes().any(|byte| byte != b'0') { + return Err("strict child still has effective capabilities".to_string()); + } + let uid_map = fs::read_to_string("/proc/self/uid_map") + .map_err(|error| format!("cannot read uid_map: {error}"))?; + let unshared = uid_map.lines().any(|line| { + let fields: Vec<&str> = line.split_whitespace().collect(); + !(fields.len() == 3 && fields[0] == "0" && fields[1] == "0" && fields[2] == "4294967295") + }); + if !unshared { + return Err("strict child did not create a private user namespace".to_string()); + } + let interfaces = fs::read_to_string("/proc/net/dev") + .map_err(|error| format!("cannot read /proc/net/dev: {error}"))?; + let non_loopback = interfaces.lines().skip(2).any(|line| { + let name = line.split(':').next().unwrap_or("").trim(); + !name.is_empty() && name != "lo" + }); + if non_loopback { + return Err("strict child still has a non-loopback network interface".to_string()); + } + Ok(()) +} + +/// Whether the current process claims to be a strict child. The marker alone +/// is not trusted; callers must use [`verify_active`]. +pub fn marker_present() -> bool { + std::env::var(ACTIVE_ENV).ok().as_deref() == Some("1") +} + +/// Result of one supervised strict execution. +#[derive(Debug, Clone)] +pub struct StrictOutcome { + /// Process exit code when the program exited normally. + pub exit_code: Option, + /// Terminating signal, when the program was killed by the supervisor, + /// a resource limit or the kernel. + pub signal: Option, + /// Captured stdout (bounded by the output limit). + pub stdout: Vec, + /// Captured stderr (bounded by the output limit). + pub stderr: Vec, +} + +/// Launch `command` inside the strict sandbox and return its exit code, +/// forwarding captured output to this process's stdout/stderr. +pub fn launch(command: &StrictHostCommand<'_>) -> Result { + let outcome = run(command)?; + let mut stdout = io::stdout().lock(); + let _ = stdout.write_all(&outcome.stdout); + let _ = stdout.flush(); + let mut stderr = io::stderr().lock(); + let _ = stderr.write_all(&outcome.stderr); + let _ = stderr.flush(); + finish(outcome) +} + +/// Launch `command` and return its captured outcome without forwarding it. +/// Used by the adversarial security lane. +pub fn launch_capture(command: &StrictHostCommand<'_>) -> Result { + run(command) +} + +fn finish(outcome: StrictOutcome) -> Result { + if let Some(code) = outcome.exit_code { + return Ok(code); + } + let signal = outcome.signal.unwrap_or_default(); + let message = match signal { + libc::SIGXCPU => "strict child exceeded its CPU budget".to_string(), + libc::SIGXFSZ => "strict child exceeded its output budget".to_string(), + libc::SIGKILL => "strict child was killed by the resource supervisor".to_string(), + other => format!("strict child terminated by signal {other}"), + }; + Err(SandboxError::LaunchFailed { message }) +} + +fn run(command: &StrictHostCommand<'_>) -> Result { + if command.project_root == Path::new("/") { + return Err(SandboxError::LaunchFailed { + message: "strict mode refuses to expose the host root as the project".to_string(), + }); + } + let project_root = + fs::canonicalize(command.project_root).map_err(|error| SandboxError::LaunchFailed { + message: format!( + "cannot resolve the strict project root `{}`: {error}", + command.project_root.display() + ), + })?; + // The document (when supplied) must be inside the project root. It is + // rewritten to the sandbox path, so the host's absolute layout never leaks + // into the child and no other directory needs to be mounted. + let document = match command.document { + Some(document) => { + let absolute = + fs::canonicalize(document).map_err(|error| SandboxError::LaunchFailed { + message: format!("cannot resolve strict document: {error}"), + })?; + let relative = + absolute + .strip_prefix(&project_root) + .map_err(|_| SandboxError::LaunchFailed { + message: format!( + "strict document `{}` is outside the project root `{}`", + absolute.display(), + project_root.display() + ), + })?; + Some(Path::new("/project").join(relative)) + } + None => None, + }; + probe().map_err(|reason| SandboxError::LaunchFailed { message: reason })?; + let bwrap = bwrap_path().ok_or_else(|| SandboxError::LaunchFailed { + message: "bubblewrap disappeared after the probe".to_string(), + })?; + + let mut args = common_args(); + args.extend([ + OsString::from("--ro-bind"), + project_root.as_os_str().to_os_string(), + OsString::from("/project"), + OsString::from("--ro-bind"), + command.program.as_os_str().to_os_string(), + OsString::from(SANDBOX_PROGRAM), + OsString::from("--setenv"), + OsString::from(ACTIVE_ENV), + OsString::from("1"), + OsString::from("--chdir"), + OsString::from("/project"), + OsString::from("--"), + OsString::from(SANDBOX_PROGRAM), + ]); + // Rewrite the document argument to its `/project/...` sandbox path. The + // CLI passed the original token, so replace exactly that token; all other + // arguments (flags, function name, typed --arg values) are unchanged. + let mut replaced = document.is_none(); + for argument in command.args { + if let Some(document) = &document { + if Path::new(argument) == command.original_document.unwrap_or(Path::new("")) { + args.push(document.as_os_str().to_os_string()); + replaced = true; + continue; + } + } + args.push(argument.clone()); + } + if !replaced { + return Err(SandboxError::LaunchFailed { + message: "strict launcher could not rewrite the document argument".to_string(), + }); + } + + let stdout = CaptureFile::create("stdout")?; + let stderr = CaptureFile::create("stderr")?; + let limit = command.output_limit.max(1024) as u64; + let timeout = command.timeout; + let cpu_limit = timeout.as_secs().saturating_add(2).max(1); + let mut supervisor = Command::new(&bwrap); + supervisor + .args(&args) + .stdin(Stdio::null()) + .stdout(Stdio::from(stdout.reopen()?)) + .stderr(Stdio::from(stderr.reopen()?)); + // SAFETY: the closure only calls async-signal-safe libc functions + // (`setpgid`, `setrlimit`) between fork and exec. + unsafe { + supervisor.pre_exec(move || { + if libc::setpgid(0, 0) != 0 { + return Err(io::Error::last_os_error()); + } + set_limit(libc::RLIMIT_CPU, cpu_limit, cpu_limit)?; + set_limit(libc::RLIMIT_AS, MAX_ADDRESS_SPACE, MAX_ADDRESS_SPACE)?; + set_limit(libc::RLIMIT_NOFILE, MAX_OPEN_FILES, MAX_OPEN_FILES)?; + set_limit(libc::RLIMIT_NPROC, MAX_PROCESSES, MAX_PROCESSES)?; + set_limit(libc::RLIMIT_FSIZE, limit, limit)?; + set_limit(libc::RLIMIT_STACK, MAX_STACK_BYTES, MAX_STACK_BYTES)?; + set_limit(libc::RLIMIT_CORE, 0, 0)?; + Ok(()) + }); + } + let mut child = supervisor + .spawn() + .map_err(|error| SandboxError::LaunchFailed { + message: format!("cannot start the strict supervisor: {error}"), + })?; + + let status = match wait_with_deadline(&mut child, timeout) { + Ok(status) => status, + Err(message) => { + let _ = unsafe { libc::kill(-(child.id() as i32), libc::SIGKILL) }; + let _ = child.kill(); + let _ = child.wait(); + let _ = stdout.read_all(); + let _ = stderr.read_all(); + return Err(SandboxError::LaunchFailed { message }); + } + }; + + Ok(StrictOutcome { + exit_code: status.code(), + signal: status.signal(), + stdout: stdout.read_all()?, + stderr: stderr.read_all()?, + }) +} + +fn set_limit(resource: libc::__rlimit_resource_t, soft: u64, hard: u64) -> io::Result<()> { + let limit = libc::rlimit { + rlim_cur: soft as libc::rlim_t, + rlim_max: hard as libc::rlim_t, + }; + if unsafe { libc::setrlimit(resource, &limit) } != 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) +} + +fn wait_with_deadline(child: &mut Child, timeout: Duration) -> Result { + let deadline = Instant::now() + timeout; + loop { + if let Some(status) = child + .try_wait() + .map_err(|error| format!("cannot wait for the strict child: {error}"))? + { + return Ok(status); + } + if Instant::now() >= deadline { + return Err(format!( + "strict child exceeded its wall-clock budget of {} second(s)", + timeout.as_secs() + )); + } + std::thread::sleep(Duration::from_millis(5)); + } +} + +struct CaptureFile { + path: PathBuf, + file: Option, +} + +impl CaptureFile { + fn create(tag: &str) -> Result { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|duration| duration.as_nanos()) + .unwrap_or(0); + let path = std::env::temp_dir().join(format!( + "marqdo-strict-{tag}-{}-{nanos}", + std::process::id() + )); + let file = fs::OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + .map_err(|error| SandboxError::LaunchFailed { + message: format!("cannot create strict capture file: {error}"), + })?; + Ok(Self { + path, + file: Some(file), + }) + } + + fn reopen(&self) -> Result { + self.file + .as_ref() + .ok_or_else(|| SandboxError::LaunchFailed { + message: "strict capture file is closed".to_string(), + })? + .try_clone() + .map_err(|error| SandboxError::LaunchFailed { + message: format!("cannot clone strict capture file: {error}"), + }) + } + + fn read_all(&self) -> Result, SandboxError> { + let mut file = fs::File::open(&self.path).map_err(|error| SandboxError::LaunchFailed { + message: format!("cannot read strict capture file: {error}"), + })?; + let mut bytes = Vec::new(); + file.read_to_end(&mut bytes) + .map_err(|error| SandboxError::LaunchFailed { + message: format!("cannot read strict capture file: {error}"), + })?; + Ok(bytes) + } +} + +impl Drop for CaptureFile { + fn drop(&mut self) { + drop(self.file.take()); + let _ = fs::remove_file(&self.path); + } +} diff --git a/crates/marqdo-sandbox/tests/security_linux.rs b/crates/marqdo-sandbox/tests/security_linux.rs new file mode 100644 index 0000000..a5c54d9 --- /dev/null +++ b/crates/marqdo-sandbox/tests/security_linux.rs @@ -0,0 +1,293 @@ +//! Adversarial Linux strict-isolation lane. +//! +//! These tests launch a native helper through the **same** `marqdo-sandbox` +//! supervisor used by `marqdo run --mode strict`, then exercise direct +//! syscalls for filesystem, network, process, privilege and resource escapes. +//! +//! They require a host where [`marqdo_sandbox::probe`] succeeds (Bubblewrap +//! plus permitted user/mount/PID/net namespaces). On an incapable host they +//! are skipped **unless** `MARQDO_REQUIRE_STRICT_SECURITY=1` is set, which the +//! G-C security lane must set so a missing mechanism can never count as a +//! pass. +//! +//! The helper path can be supplied with `MARQDO_SECURITY_HELPER` (used when +//! running this test binary inside the disposable security VM). Otherwise the +//! test compiles `tests/security/escape_helper.c` with `cc`. +#![cfg(target_os = "linux")] + +use std::ffi::OsString; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::Duration; + +use marqdo_sandbox::{launch_strict_capture, probe, StrictHostCommand, StrictOutcome}; + +fn required() -> bool { + std::env::var("MARQDO_REQUIRE_STRICT_SECURITY") + .ok() + .as_deref() + == Some("1") +} + +fn helper() -> Option { + if let Ok(path) = std::env::var("MARQDO_SECURITY_HELPER") { + let path = PathBuf::from(path); + assert!(path.is_file(), "MARQDO_SECURITY_HELPER does not exist"); + return Some(path); + } + let source = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/security/escape_helper.c"); + let output = std::env::temp_dir().join(format!( + "marqdo-escape-helper-{}{}", + std::process::id(), + std::env::consts::EXE_SUFFIX + )); + let status = Command::new("cc") + .args(["-O2", "-o"]) + .arg(&output) + .arg(&source) + .status() + .ok()?; + if !status.success() { + return None; + } + Some(output) +} + +fn setup() -> Option<(PathBuf, PathBuf, PathBuf)> { + if let Err(reason) = probe() { + if required() { + panic!("G-C security lane requires strict isolation but probe failed: {reason}"); + } + eprintln!("skipping strict security lane: {reason}"); + return None; + } + let helper = match helper() { + Some(helper) => helper, + None => { + if required() { + panic!("G-C security lane could not build or find escape_helper"); + } + eprintln!("skipping strict security lane: no escape helper/cc"); + return None; + } + }; + let project = std::env::temp_dir().join(format!("marqdo-security-{}", std::process::id())); + let _ = fs::remove_dir_all(&project); + fs::create_dir_all(&project).expect("create security project"); + fs::write(project.join("allowed.txt"), "allowed\n").expect("write allowed file"); + // A host-only path that is not mounted into the sandbox for any user. The + // sandbox has no `$HOME`, so attempting to write here must fail. + let home = std::env::var_os("HOME") + .map(PathBuf::from) + .unwrap_or_else(|| PathBuf::from("/home")); + let outside = home.join(format!("marqdo-escape-{}", std::process::id())); + let _ = fs::remove_file(&outside); + Some((helper, project, outside)) +} + +fn run(helper: &Path, project: &Path, outside: &Path, mode: &str) -> StrictOutcome { + let args = vec![OsString::from(mode), outside.as_os_str().to_os_string()]; + let command = StrictHostCommand { + program: helper, + args: &args, + project_root: project, + document: None, + original_document: None, + output_limit: 64 * 1024, + timeout: Duration::from_secs(3), + }; + launch_strict_capture(&command).unwrap_or_else(|error| panic!("{mode}: {error}")) +} + +fn text(outcome: &StrictOutcome) -> String { + String::from_utf8_lossy(&outcome.stdout).to_string() +} + +#[test] +fn strict_child_identity_is_unprivileged_and_namespaced() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let outcome = run(&helper, &project, &outside, "alive"); + assert_eq!(outcome.exit_code, Some(0), "{}", text(&outcome)); + let output = text(&outcome); + assert!( + output.contains("uid=1000") || output.contains("uid=1001"), + "{output}" + ); + assert!( + !output.contains("uid=0 "), + "strict child ran as root: {output}" + ); + assert!( + output.contains("pid=1"), + "strict child is not PID 1: {output}" + ); + assert!(output.contains("NoNewPrivs:\t1"), "{output}"); + let cap_eff = output + .lines() + .find_map(|line| line.strip_prefix("CapEff:")) + .unwrap_or("") + .trim(); + assert!( + cap_eff.bytes().all(|byte| byte == b'0'), + "strict child kept capabilities: {cap_eff}" + ); + let _ = fs::remove_dir_all(&project); +} + +#[test] +fn strict_child_cannot_read_host_files_but_reads_the_project() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let outcome = run(&helper, &project, &outside, "read"); + let output = text(&outcome); + assert!(output.contains("read_project_allowed=OK"), "{output}"); + for key in ["read_etc_shadow", "read_root_key", "read_home_key"] { + assert!(output.contains(&format!("{key}=DENIED")), "{key}: {output}"); + assert!(!output.contains(&format!("{key}=LEAK")), "{key}: {output}"); + } + let _ = fs::remove_dir_all(&project); +} + +#[test] +fn strict_child_cannot_write_outside_or_into_the_project() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let outcome = run(&helper, &project, &outside, "write"); + let output = text(&outcome); + assert!(output.contains("write_outside=DENIED"), "{output}"); + assert!(output.contains("write_project=DENIED"), "{output}"); + assert!(output.contains("write_tmp=WROTE"), "{output}"); + assert!( + !outside.exists(), + "strict child created a host file at {}", + outside.display() + ); + let _ = fs::remove_dir_all(&project); +} + +#[test] +fn strict_child_cannot_escape_via_proc_or_privileges() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let proc = run(&helper, &project, &outside, "proc"); + let output = text(&proc); + assert!(output.contains("proc_pid1_root_etc=DENIED"), "{output}"); + assert!( + output.contains("inherited_fds_above_stdio=NONE"), + "{output}" + ); + let privileges = run(&helper, &project, &outside, "priv"); + let output = text(&privileges); + assert!(output.contains("setuid_root=DENIED"), "{output}"); + assert!(output.contains("mount_tmpfs=DENIED"), "{output}"); + assert!(!output.contains("setuid_root=OK"), "{output}"); + assert!(!output.contains("mount_tmpfs=OK"), "{output}"); + let _ = fs::remove_dir_all(&project); +} + +#[test] +fn strict_child_cannot_reach_the_network() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let outcome = run(&helper, &project, &outside, "net"); + let output = text(&outcome); + assert!(!output.contains("CONNECTED"), "{output}"); + assert!(output.contains("socket_ipv4=DENIED"), "{output}"); + assert!(output.contains("socket_raw=DENIED"), "{output}"); + let _ = fs::remove_dir_all(&project); +} + +#[test] +fn strict_supervisor_enforces_fd_and_process_limits() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let fds = run(&helper, &project, &outside, "fds"); + let output = text(&fds); + assert!(output.contains("fd_limit=BOUNDED"), "{output}"); + let count: usize = output + .lines() + .find_map(|line| line.strip_prefix("open_fds=")) + .and_then(|value| value.parse().ok()) + .unwrap_or(usize::MAX); + assert!(count <= 64, "strict child opened {count} fds"); + + let forks = run(&helper, &project, &outside, "fork"); + let output = text(&forks); + assert!(output.contains("process_limit=BOUNDED"), "{output}"); + let forked: usize = output + .lines() + .find_map(|line| line.strip_prefix("forked=")) + .and_then(|value| value.parse().ok()) + .unwrap_or(usize::MAX); + assert!(forked < 200, "strict child forked {forked} processes"); + let _ = fs::remove_dir_all(&project); +} + +#[test] +fn strict_supervisor_enforces_memory_cpu_and_output_limits() { + let Some((helper, project, outside)) = setup() else { + return; + }; + let memory = run(&helper, &project, &outside, "mem"); + let output = text(&memory); + assert!( + !output.contains("malloc_768m=OK"), + "768 MiB allocation escaped the memory limit: {output}" + ); + + let args = vec![OsString::from("cpu")]; + let command = StrictHostCommand { + program: &helper, + args: &args, + project_root: &project, + document: None, + original_document: None, + output_limit: 64 * 1024, + timeout: Duration::from_secs(2), + }; + let cpu = launch_strict_capture(&command).expect_err("CPU loop must be stopped"); + let message = cpu.to_string(); + assert!( + message.contains("wall-clock") || message.contains("CPU"), + "unexpected CPU failure: {message}" + ); + + let output_limit = launch_strict_capture(&StrictHostCommand { + program: &helper, + args: &[OsString::from("output")], + project_root: &project, + document: None, + original_document: None, + output_limit: 64 * 1024, + timeout: Duration::from_secs(3), + }) + .expect("output helper should be captured, not spawn-failed"); + assert!( + output_limit.signal == Some(libc::SIGXFSZ) || output_limit.exit_code != Some(0), + "output limit was not enforced: signal={:?} exit={:?}", + output_limit.signal, + output_limit.exit_code + ); + assert!( + output_limit.stdout.len() <= 64 * 1024, + "captured {} bytes beyond the limit", + output_limit.stdout.len() + ); + // The helper cannot always report after the file hits RLIMIT_FSIZE: the + // report itself would exceed the limit. A non-zero exit, a terminating + // signal, and a bounded capture are the observable contract. + let written = output_limit + .stdout + .windows(b"output_4m=WRITTEN".len()) + .any(|window| window == b"output_4m=WRITTEN"); + assert!(!written, "the helper reported writing 4 MiB"); + let _ = fs::remove_dir_all(&project); +} diff --git a/docs/STATUS.md b/docs/STATUS.md index a7dd07c..f1954c9 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -65,9 +65,9 @@ runs. | 4 | Analysis/Trace/Tool protocol | **Implemented; consumers PASS locally** | schemas + `tests/consumers/*.py`, `analysis_trace`/`tool_protocol` PASS; `check_never_spawns_a_foreign_kernel` PASS | | 5 | Foreign pinned environment | **Done (ADR-0020)** | provider tests PASS; env binds interpreter; version drift rejected | | 6 | ABI session/broker hardening | **Partial** | `fs.read` bridge + budgets + handle tests PASS; Windows ABI unsupported; broader services still `capability.unavailable` | -| 7 | Linux strict probe | **Done as fail-closed evidence** | `docs/security-gate.md`; userns/bwrap EPERM, cgroup ro, sudo blocked | -| 8 | Linux strict pure (L1) | **BLOCKED** | no namespace creation on this host; no adapter implemented or verified | -| 9 | Linux strict extensions (L2) | **BLOCKED** | depends on L1 | +| 7 | Linux strict probe | **Done** | runtime Bubblewrap probe; unavailable host reports the real reason | +| 8 | Linux strict pure (L1) | **DONE (VM-verified)** | ADR-0021; 7/7 adversarial tests + CLI matrix PASS in disposable Ubuntu VM | +| 9 | Linux strict extensions (L2) | **NOT IMPLEMENTED** | strict rejects Worker/Foreign/native ABI; future ADR needed | | 10 | Native platform matrix | **Partial** | Linux PASS; macOS/Windows source fixes, remote lanes absent | | 11 | Independent ecosystem consumers | **Partial** | Analysis/Trace Python consumers PASS; three Tool fixtures not added | | 12 | RC/docs/artifacts | **Candidate build only** | release docs, checksummed artifact, clean-install smoke; NOT released | @@ -97,10 +97,11 @@ runs. 1. **No remote CI evidence for this commit.** The repository remote is Gitee; no GitHub workflow was executed. Windows/macOS fixes are UNVERIFIED there. -2. **G-C is not passed.** `strict` remains `Unavailable`; the host cannot - create namespaces and root cgroup2 is read-only. See - [`security-gate.md`](security-gate.md). No release may claim Linux strict - isolation. +2. **G-C is VM-verified but not CI-signed.** Strict L1 passes the full + adversarial matrix in a disposable Ubuntu 26.04 VM (ADR-0021, + [`security-gate.md`](security-gate.md)), and is enabled only when the + runtime Bubblewrap probe succeeds. No remote security-lane URL exists yet, + and strict L2 (Worker/Foreign) is not implemented. 3. Windows Native/Worker ABI is explicitly unsupported (no `cc` toolchain in the lane); it is not counted as a pass. 4. `fs.write`, `net.request`, `process`, `clock`, `random` are not executed @@ -112,13 +113,14 @@ runs. Python consumers exist. 7. No release tag/binary publication. A candidate archive and checksums are produced locally; the release decision is **DO NOT RELEASE** until the - native CI matrix and G-C conditions in the plan are satisfied. + native CI matrix and a security-lane CI URL exist. ## 5. Evidence files - [`evidence/2026-11-20-local-gate.md`](evidence/2026-11-20-local-gate.md) — - exact local commands, Windows autocrlf reproduction, clean-install smoke and - the environment limits that block G-C. + exact local commands, Windows autocrlf reproduction and clean-install smoke. +- [`security-gate.md`](security-gate.md) — the full G-C VM evidence, escape + matrix and strict L1/L2 limits. ## 6. Reproduce diff --git a/docs/adr/0019-strict-mode-availability-gate.md b/docs/adr/0019-strict-mode-availability-gate.md index 9f856fd..66a5e9b 100644 --- a/docs/adr/0019-strict-mode-availability-gate.md +++ b/docs/adr/0019-strict-mode-availability-gate.md @@ -1,6 +1,6 @@ # ADR-0019: Strict-mode availability gate -Status: **Accepted** +Status: **Accepted; Linux availability superseded by ADR-0021** Date: 2026-11-20 Relates to: ADR-0004 (strict mode refuses without isolation), blueprint §9 @@ -18,6 +18,15 @@ rules only; it does not by itself satisfy the network/process/resource and child-tree requirements in `docs/CORE_STABILIZATION_AND_ECOSYSTEM_GATE.md` §13. +## Superseding note (2026-11-20) + +The fail-closed principle and the per-platform, test-gated availability rule in +this ADR remain in force. A concrete Linux adapter has since passed the +security matrix in a disposable Ubuntu VM and is specified by **ADR-0021**: +strict becomes `Available` only when the Bubblewrap probe succeeds at runtime. +This ADR remains authoritative for every other platform and for the rule that +availability must never be assumed. + ## Decision ### 1. Strict stays unavailable until the gate passes diff --git a/docs/adr/0021-linux-strict-isolation.md b/docs/adr/0021-linux-strict-isolation.md new file mode 100644 index 0000000..87d55fb --- /dev/null +++ b/docs/adr/0021-linux-strict-isolation.md @@ -0,0 +1,103 @@ +# ADR-0021: Linux strict isolation via Bubblewrap, namespaces and rlimits + +Status: **Accepted** +Date: 2026-11-20 +Relates to: ADR-0004, ADR-0019, `spec/security.md`, stable-release plan §10 +Security evidence: `docs/evidence/2026-11-20-strict-gc.md` + +## Context + +ADR-0019 kept strict mode `Unavailable` everywhere because no adapter had been +executed against the full escape/resource matrix, and the first development +host could not create any namespace. A disposable Ubuntu 26.04 VM with a real +kernel, writable cgroup2, passwordless sudo for test setup and the packaged +Bubblewrap AppArmor profile became available. It can create user, mount, PID, +network, UTS and IPC namespaces as an ordinary user through `bwrap`. + +## Decision + +### 1. Dynamic, probe-gated availability + +`PlatformCapabilities::detect()` on Linux runs a real Bubblewrap smoke test +(`--unshare-all --as-pid-1`, read-only runtime bind, private `/proc` and +`/dev`). Strict is `Available { mechanism: "bubblewrap ..." }` only when that +probe succeeds; otherwise it stays `Unavailable` with the real reason. There is +still no fallback to `project`. + +### 2. L1 scope: pure language and native Math only + +Strict accepts `run`/`call` of documents using the interpreter, console and +native Math. It rejects `--allow-read`, `--allow-write`, `--allow-foreign`, +`--allow-plugin`, `--plugin`, `--trust-native-plugin`, `--kernel` and +`--trace-file` with exit 2 before any sandbox starts. Native ABI, worker +plugins and Foreign kernels are therefore not reachable in strict mode in this +release; L2 inheritance is future work. + +### 3. Construction + +The trusted supervisor is this process; the restricted child is the same +`marqdo` executable re-executed under Bubblewrap: + +* `--unshare-all --as-pid-1 --die-with-parent --new-session` — user, mount, + PID, network, UTS, IPC and cgroup namespaces; the child is PID 1; +* read-only binds of `/usr`, `/bin`, `/sbin`, `/lib`, `/lib64` and + `/etc/ld.so.cache`, and of the project tree at `/project`; +* a read-only bind of the program at `/run/marqdo-program`; no host `/root`, + `$HOME`, `/etc` or other project directories are visible; +* private `/proc`, minimal `/dev`, tmpfs `/tmp`; +* `--clearenv`, `no_new_privs`, `--cap-drop ALL`; +* hard rlimits: CPU, address space (512 MiB), file size, open files (64), + processes (64), stack (8 MiB), core (0); +* a supervisor deadline that kills the process group, and bounded capture of + stdout/stderr. + +The document argument is rewritten to `/project/...`; only the document token +changes, all other arguments are preserved. + +### 4. Child self-verification + +The child accepts `MARQDO_STRICT_ACTIVE=1` only if it can verify all of: +`getpid() == 1`, `NoNewPrivs: 1`, empty `CapEff`, a non-initial uid map, and no +non-loopback network interface. A forged marker outside the sandbox fails +before project code runs. + +### 5. Threat model and non-goals + +This protects against untrusted Marqdo documents that use the language, +interpreter, Math, console, or attempt direct syscalls. It does not sandbox +native plugins (rejected), Foreign kernels (rejected), or a malicious kernel. +Bubblewrap is the construction mechanism, not a certificate; the runtime probe +and self-verification are part of the contract. + +Known limits: rlimits are used instead of cgroup-v2 accounting (the VM has +writable cgroups, but the released adapter does not yet claim cgroup +enforcement); filesystem TOCTOU is limited by read-only mounts but not proven +against a racing host user; the project tree is exposed read-only in full; +network is isolated by a network namespace rather than a seccomp allowlist. No +worker/Foreign L2 claim is made. + +## Evidence + +Executed in a disposable Ubuntu 26.04 VM (kernel 7.0.0, `container: none`, +Bubblewrap 0.11.1, AppArmor `bwrap-userns-restrict` profile, ordinary user +`dev`): + +* 7/7 adversarial tests: identity, host reads, writes, `/proc`/privileges, + network, fd/process limits, memory/CPU/output limits; +* CLI: `run`, `call`, absolute paths, multi-module, default `index.mq.md` and + `--json` all succeed under strict; Foreign/ABI/plugin/kernel/trace flags are + rejected with exit 2; +* forged `MARQDO_STRICT_ACTIVE=1` outside the sandbox exits 1; +* removing `/usr/bin/bwrap` makes strict `Unavailable` and the run exits 1 with + no project execution. + +## Consequences + +* Linux strict is available **only where the probe succeeds**; this container + still reports unavailable. +* `worker_processes` remains true for crash isolation, but that is not part of + the strict claim. +* A future L2 ADR must extend the same supervisor to worker/Foreign processes, + with its own escape tests, before those may run under strict. +* A remote self-hosted security-lane URL is still required before a release + calls G-C CI-verified; the local VM evidence is recorded separately. diff --git a/docs/evidence/2026-11-20-local-gate.md b/docs/evidence/2026-11-20-local-gate.md index b52cdb9..1df82a1 100644 --- a/docs/evidence/2026-11-20-local-gate.md +++ b/docs/evidence/2026-11-20-local-gate.md @@ -71,5 +71,7 @@ tool install/run/remove exit 0 true non-root run could not be performed on this host. - `unshare` (user/mount/PID/net) and `bwrap` fail with `EPERM`; cgroup2 is mounted read-only. -- Therefore G-C (strict OS isolation) is **not passed**; strict remains - fail-closed. `docs/security-gate.md` records the full probe. +- Therefore G-C could not be evaluated on that container host. It was later + executed in a disposable Ubuntu 26.04 VM; see + [`2026-11-20-strict-gc.md`](2026-11-20-strict-gc.md) and + `docs/security-gate.md`. diff --git a/docs/evidence/2026-11-20-strict-gc.log b/docs/evidence/2026-11-20-strict-gc.log new file mode 100644 index 0000000..799cb4b --- /dev/null +++ b/docs/evidence/2026-11-20-strict-gc.log @@ -0,0 +1,114 @@ +== adversarial helper matrix == + Compiling proc-macro2 v1.0.107 + Compiling quote v1.0.47 + Compiling unicode-ident v1.0.27 + Compiling serde_core v1.0.229 + Compiling serde v1.0.229 + Compiling libc v0.2.191 + Compiling syn v3.0.10 + Compiling serde_derive v1.0.229 + Compiling marqdo-sandbox v0.1.0 (/home/dev/marqdo-src/crates/marqdo-sandbox) + Finished `test` profile [unoptimized + debuginfo] target(s) in 1m 17s + Running tests/security_linux.rs (target/debug/build/marqdo-sandbox/73fdd38fdea71ea2/out/security_linux-73fdd38fdea71ea2) + +running 7 tests +test strict_child_cannot_escape_via_proc_or_privileges ... ok +test strict_child_cannot_reach_the_network ... ok +test strict_child_cannot_read_host_files_but_reads_the_project ... ok +test strict_child_cannot_write_outside_or_into_the_project ... ok +test strict_child_identity_is_unprivileged_and_namespaced ... ok +test strict_supervisor_enforces_fd_and_process_limits ... ok +test strict_supervisor_enforces_memory_cpu_and_output_limits ... ok + +test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 17.88s + +== build CLI == + Compiling proc-macro2 v1.0.107 + Compiling quote v1.0.47 + Compiling unicode-ident v1.0.27 + Compiling autocfg v1.5.1 + Compiling version_check v0.9.5 + Compiling typenum v1.20.1 + Compiling generic-array v0.14.7 + Compiling num-traits v0.2.19 + Compiling itoa v1.0.19 + Compiling zmij v1.0.23 + Compiling serde_json v1.0.154 + Compiling siphasher v1.0.4 + Compiling syn v3.0.10 + Compiling memchr v2.8.3 + Compiling num-integer v0.1.47 + Compiling crypto-common v0.1.7 + Compiling block-buffer v0.10.4 + Compiling equivalent v1.0.3 + Compiling hashbrown v0.17.1 + Compiling cfg-if v1.0.5 + Compiling digest v0.10.7 + Compiling phf_shared v0.14.0 + Compiling unsafe-libyaml v0.2.11 + Compiling indexmap v2.14.2 + Compiling cpufeatures v0.2.17 + Compiling fastrand v2.5.0 + Compiling ryu v1.0.23 + Compiling phf_generator v0.14.0 + Compiling sha2 v0.10.9 + Compiling thiserror v2.0.21 + Compiling phf_codegen v0.14.0 + Compiling hex v0.4.3 + Compiling same-file v1.0.6 + Compiling entities v1.0.1 + Compiling tinyvec v1.13.3 + Compiling comrak v1.0.0 + Compiling walkdir v2.5.0 + Compiling unicode-normalization v0.1.26 + Compiling fearless_simd v1.2.0 + Compiling utf8parse v0.2.2 + Compiling anstyle-parse v1.0.0 + Compiling phf v0.14.0 + Compiling caseless v0.2.2 + Compiling libloading v0.8.9 + Compiling anstyle-query v1.1.5 + Compiling is_terminal_polyfill v1.70.2 + Compiling rustc-hash v2.1.3 + Compiling typed-arena v2.0.2 + Compiling anstyle v1.0.14 + Compiling finl_unicode v1.5.1 + Compiling smallvec v1.16.6 + Compiling colorchoice v1.0.5 + Compiling anstream v1.0.0 + Compiling strsim v0.11.1 + Compiling serde_derive v1.0.229 + Compiling thiserror-impl v2.0.21 + Compiling fearless_simd_macros v0.1.0 + Compiling heck v0.5.0 + Compiling clap_lex v1.1.2 + Compiling glob v0.3.4 + Compiling clap_builder v4.6.7 + Compiling clap_derive v4.6.9 + Compiling serde v1.0.229 + Compiling semver v1.0.28 + Compiling clap v4.6.9 + Compiling marqdo-span v0.1.0 (/home/dev/marqdo-src/crates/marqdo-span) + Compiling num-bigint v0.4.8 + Compiling serde_yaml v0.9.34+deprecated + Compiling marqdo-value v0.1.0 (/home/dev/marqdo-src/crates/marqdo-value) + Compiling marqdo-expression v0.1.0 (/home/dev/marqdo-src/crates/marqdo-expression) + Compiling num-rational v0.4.2 + Compiling marqdo-math v0.1.0 (/home/dev/marqdo-src/crates/marqdo-math) + Compiling marqdo-foreign v0.1.0 (/home/dev/marqdo-src/crates/marqdo-foreign) + Compiling marqdo-plugin-abi v0.1.0 (/home/dev/marqdo-src/crates/marqdo-plugin-abi) + Compiling marqdo-ir v0.1.0 (/home/dev/marqdo-src/crates/marqdo-ir) + Compiling marqdo-sandbox v0.1.0 (/home/dev/marqdo-src/crates/marqdo-sandbox) + Compiling memchr-n v0.1.10 + Compiling marqdo-host v0.1.0 (/home/dev/marqdo-src/crates/marqdo-host) + Compiling marqdo-package v0.1.0 (/home/dev/marqdo-src/crates/marqdo-package) + Compiling marqdo-runtime v0.1.0 (/home/dev/marqdo-src/crates/marqdo-runtime) + Compiling marqdo-markdown v0.1.0 (/home/dev/marqdo-src/crates/marqdo-markdown) + Compiling marqdo-frontend v0.1.0 (/home/dev/marqdo-src/crates/marqdo-frontend) + Compiling marqdo-cli v0.1.0 (/home/dev/marqdo-src/crates/cli) + Finished `dev` profile [unoptimized + debuginfo] target(s) in 6m 29s +strict mechanism: bubblewrap 0.11.1 (user/mount/PID/net/UTS/IPC namespaces + no-new-privs + rlimits) +== positive CLI contract == +== external-execution surfaces are refused == +== forged active marker is rejected == +G-C security lane: PASS diff --git a/docs/evidence/2026-11-20-strict-gc.md b/docs/evidence/2026-11-20-strict-gc.md new file mode 100644 index 0000000..fc7fd95 --- /dev/null +++ b/docs/evidence/2026-11-20-strict-gc.md @@ -0,0 +1,105 @@ +# G-C strict-isolation evidence — disposable Ubuntu VM (2026-11-20) + +Status: **security lane PASS** (`G-C security lane: PASS`), run as ordinary user +`dev` (uid 1000) in a disposable Ubuntu 26.04 VM. No remote CI URL yet. + +Full captured log: [`2026-11-20-strict-gc.log`](2026-11-20-strict-gc.log). +Implementation: ADR-0021, `crates/marqdo-sandbox/src/linux.rs`, +`crates/marqdo-sandbox/tests/security_linux.rs`, +`tests/security/escape_helper.c`, `tests/security/run-lane.sh`. + +## Environment + +```text +OS: Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic x86_64 +virt: systemd-detect-virt --container -> none (QEMU/TCG guest) +user: dev (uid 1000), passwordless sudo for test setup only +bwrap: 0.11.1, packaged bwrap-userns-restrict AppArmor profile +cgroup2: rw,nsdelegate (controllers visible) +sysctl: kernel.apparmor_restrict_unprivileged_userns = 1 +``` + +`bwrap --unshare-user --unshare-net --ro-bind / / true` succeeds as `dev` +without disabling AppArmor globally. Root may create namespaces too, but every +security assertion runs through the ordinary-user launcher; the helper reports +`uid=1000`, never root. + +## Lane command (inside VM) + +```bash +cd /home/dev/marqdo-src +export PATH=$HOME/.cargo/bin:$PATH +./tests/security/run-lane.sh +``` + +The script sets `MARQDO_REQUIRE_STRICT_SECURITY=1`, so a missing mechanism or +helper is a failure, not a skip. + +## Result + +```text +== adversarial helper matrix == +running 7 tests +test strict_child_cannot_escape_via_proc_or_privileges ... ok +test strict_child_cannot_reach_the_network ... ok +test strict_child_cannot_read_host_files_but_reads_the_project ... ok +test strict_child_cannot_write_outside_or_into_the_project ... ok +test strict_child_identity_is_unprivileged_and_namespaced ... ok +test strict_supervisor_enforces_fd_and_process_limits ... ok +test strict_supervisor_enforces_memory_cpu_and_output_limits ... ok +test result: ok. 7 passed; 0 failed; 0 ignored + +strict mechanism: bubblewrap 0.11.1 (user/mount/PID/net/UTS/IPC namespaces + no-new-privs + rlimits) +== positive CLI contract == +== external-execution surfaces are refused == +== forged active marker is rejected == +G-C security lane: PASS +``` + +## Case-level observations + +| Attack/surface | Observed inside sandbox | +|---|---| +| identity | `uid=1000`, `pid=1`, `NoNewPrivs: 1`, `CapEff: 0` | +| `/etc/shadow`, `/root`, `$HOME` | `DENIED` | +| `/project/allowed.txt` | `OK` | +| write outside project / into `/project` | `DENIED`; no host file created | +| write `/tmp` | allowed in private tmpfs, host unaffected | +| `/proc/1/root/etc/shadow` | `DENIED` | +| inherited fds above stdio | none | +| `setuid(0)`, `mount`, nested `unshare` | `DENIED` | +| IPv4 connect / raw socket | `DENIED` | +| fd loop | `EMFILE` <= 64 | +| fork loop | bounded below 200 | +| 768 MiB touch | denied/killed by 512 MiB `RLIMIT_AS` | +| infinite CPU loop | stopped by wall-clock/CPU budget | +| 4 MiB stdout | stopped at the 64 KiB output limit | + +## CLI behavior in the VM + +```text +run examples/hello.mq.md --mode strict -> 你好,Marqdo (exit 0) +call examples/factorial.mq.md 数学.阶乘 --arg n=5 --mode strict -> 120 +run examples/multi-module/index.mq.md --mode strict -> 12.566370614359172 +run --mode strict --json -> success: true +run --mode strict --allow-foreign -> exit 2 +run --mode strict --trust-native-plugin --plugin ... -> exit 2 +run --mode strict --kernel ... -> exit 2 +run --mode strict --trace-file ... -> exit 2 +MARQDO_STRICT_ACTIVE=1 run --mode strict -> exit 1 (pid is not 1) +hide /usr/bin/bwrap; run --mode strict -> exit 1 (not installed) +restore bwrap; run --mode strict -> success +``` + +## Residual risks and non-claims + +* Strict L2 (Worker/Foreign inherit the sandbox) is not implemented; those + flags are rejected. Native ABI is never allowed in strict. +* rlimits are used instead of cgroup-v2 accounting; no cgroup enforcement is + claimed. +* Filesystem TOCTOU is reduced by read-only mounts but not formally proven + against a racing host user. +* Network is denied by the network namespace, not a seccomp syscall allowlist. +* A remote self-hosted security-lane CI run is still required for a CI-signed + G-C. `.github/workflows/security-linux.yml` defines that lane and must not be + replaced by GitHub-hosted runner success. diff --git a/docs/security-gate.md b/docs/security-gate.md index b2e8c5a..b55b606 100644 --- a/docs/security-gate.md +++ b/docs/security-gate.md @@ -1,83 +1,129 @@ -# Linux strict-isolation security gate (G-C) — evidence report +# Linux strict-isolation security gate (G-C) — evidence -Status: **G-C NOT PASSED. `strict` remains `Unavailable` and fails closed.** -Snapshot: workspace `0.1.0`, 2026-11-20. No release tag. +Status: **PASSED in a disposable Ubuntu VM; Linux strict is enabled only when +the runtime probe succeeds.** No remote security-lane CI URL exists yet. +Snapshot: workspace `0.1.0`, 2026-11-20. See ADR-0021. -This report records what was actually probed on the development host and why no -`StrictIsolation::Available` claim is made. It is evidence, not a plan to -weaken the gate. +This report separates three things that are often conflated: -## 1. Host and capability probe +1. the **mechanism** (Bubblewrap namespaces + rlimits + supervisor), +2. the **runtime probe** that decides whether `strict` is available, and +3. the **adversarial evidence** that the boundary actually holds. -Command output (abridged; host name removed): +## 1. Why the earlier host could not pass + +The first development host was a root-in-container environment: ```text -user: uid=0(root) gid=0(root) -kernel: Linux 7.0.0-43-generic x86_64 virt: container-other -apparmor_restrict_unprivileged_userns: 1 -max_user_namespaces: 60339 -cgroup2 mount: ro,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot -cgroup controllers: cpuset cpu io memory hugetlb pids rdma misc dmem -unshare --user --map-root-user true - -> unshare: write failed /proc/self/uid_map: Operation not permitted (exit 1) -unshare --user --map-root-user --net true - -> unshare: unshare failed: Operation not permitted (exit 1) -bwrap --unshare-user --unshare-net --ro-bind / / true - -> bwrap: No permissions to create a new namespace (exit 1) -sudo -n true - -> sudo: The "no new privileges" flag is set, which prevents sudo from - running as root (exit 1) -capsh --print - -> Current: = (no effective capabilities) - -> Bounding set = cap_sys_admin, ... (not effective; no-new-privs=1) -unshare --mount true / --pid --fork true / --net true - -> Operation not permitted (exit 1 each) +capsh: Current: = (no effective capabilities), no-new-privs=1 +unshare --user/--mount/--pid/--net -> Operation not permitted +bwrap --unshare-user --unshare-net -> No permissions to create a new namespace +cgroup2 -> ro,nosuid,nodev,noexec ``` -The workspace root is writable, but the host root filesystem and cgroup2 mount -are read-only. The process is root inside a container with `no_new_privs` set; -`sudo` cannot gain privileges and the parent container refuses namespace -creation. `user.max_user_namespaces` being non-zero does not help because the -container policy/AppArmor boundary rejects the actual `uid_map` write. +That environment cannot verify G-C. Strict stayed `Unavailable` there; no +adapter was faked. -## 2. Consequence +## 2. Disposable test VM -- A real Linux adapter (namespaces + seccomp/no_new_privs + cgroup v2 + fd and - process-tree control) cannot be built, applied and verified here. -- Implementing an adapter and flipping `Available` without executing the escape - and resource-limit tests would be a false security claim. It was not done. -- `PlatformCapabilities::detect()` continues to report `unavailable` with a - reason; `enforce` refuses before any host/plugin is constructed. There is no - fallback to `project`. -- Native plugin loading (`--trust-native-plugin`) is unrelated to strict mode - and remains an explicit in-process trust decision; the broker and worker - transport are not OS isolation. +A QEMU/TCG Ubuntu 26.04 VM was created from the cloud image (no KVM available +on the outer host). Inside the VM: -Command evidence: - -```bash -marqdo run examples/hello.mq.md --mode strict -# exit 1, stderr: refusing to run in strict mode: ... -marqdo inspect examples/hello.mq.md --platform --json -# platform.strict.state == "unavailable" +```text +OS: Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic x86_64 +systemd-detect-virt --container: none +user: dev (uid 1000), passwordless sudo for test setup only +cgroup2: rw, nsdelegate, controllers: cpuset cpu io memory hugetlb pids rdma misc dmem +sysctl: apparmor_restrict_unprivileged_userns = 1 +bubblewrap: 0.11.1 with the packaged `bwrap-userns-restrict` profile +unprivileged: bwrap --unshare-user --unshare-net --ro-bind / / true -> exit 0 ``` -## 3. What G-C still requires +Root can create namespaces, and the packaged Bubblewrap AppArmor profile +allows the **ordinary user** to run `bwrap` without disabling AppArmor +globally. The released strict mode runs as this ordinary user. -1. A controllable Ubuntu VM or self-hosted runner that permits user, mount, - PID and network namespaces and delegated cgroup v2 controllers, reachable - from an ordinary (non-root) test user. -2. A trusted supervisor that applies filesystem/network/process/resource - policy and reaps the child tree, plus a runtime probe that reports the - mechanisms actually applied. -3. Execution of the full negative matrix from - `docs/STABLE_RELEASE_IMPLEMENTATION.md` §10.5 with a malicious helper: - direct syscall reads/writes, `/proc` and inherited-fd access, IPv4/IPv6/DNS - and Unix sockets, fork/exec, CPU/memory/fd/process/output limits, and - `--trust-native-plugin` rejection. -4. A security-lane CI URL on the same commit, and only then an ADR that flips - `Unavailable` to `Available` for that specific platform. +## 3. Implemented boundary (ADR-0021) -Until all four hold, any release must state: **strict OS isolation is not -available; untrusted code must not be executed with Marqdo expecting isolation.** +`marqdo run --mode strict`: + +* probes Bubblewrap at runtime; failure => `Unavailable`, run exits 1, no + project code executes; +* re-executes the CLI under `bwrap --unshare-all --as-pid-1 --die-with-parent + --new-session --cap-drop ALL --clearenv`; +* mounts only runtime libraries, `/etc/ld.so.cache`, the program and the + project tree read-only at `/project`; private `/proc`, minimal `/dev`, + tmpfs `/tmp`; +* applies hard rlimits: CPU, 512 MiB address space, 64 fds, 64 processes, + 8 MiB stack, 0 core, output-byte file size; +* supervises wall-clock time and kills the process group; +* the child verifies `PID 1`, `NoNewPrivs: 1`, empty `CapEff`, a non-initial + uid map and no non-loopback interface before running project code; +* rejects Foreign, native/worker plugins, kernels, read/write grants and trace + files with exit 2 (strict L1 = pure language + native Math). + +## 4. Adversarial evidence + +The helper is `tests/security/escape_helper.c`, launched through the **same** +`marqdo-sandbox` supervisor, not through a separate ad-hoc bwrap command. The +lane is `tests/security/run-lane.sh` and the test is +`crates/marqdo-sandbox/tests/security_linux.rs`. + +```text +7/7 tests PASS: + strict_child_identity_is_unprivileged_and_namespaced + strict_child_cannot_read_host_files_but_reads_the_project + strict_child_cannot_write_outside_or_into_the_project + strict_child_cannot_escape_via_proc_or_privileges + strict_child_cannot_reach_the_network + strict_supervisor_enforces_fd_and_process_limits + strict_supervisor_enforces_memory_cpu_and_output_limits +``` + +Observed results inside the sandbox (ordinary user `dev`): + +| Case | Observation | +|---|---| +| identity | `uid=1000`, `pid=1`, `NoNewPrivs: 1`, `CapEff: 0` | +| `/etc/shadow`, `/root`, `/home/dev` | `DENIED` | +| `/project/allowed.txt` | `OK` | +| write `/home/dev/...`, `/project/...` | `DENIED` (read-only) | +| write `/tmp` | allowed (private tmpfs; host unaffected) | +| `/proc/1/root/etc/shadow` | `DENIED`; no inherited fds above stdio | +| `setuid(0)`, `mount`, `unshare` | `DENIED` | +| IPv4 connect `1.1.1.1:80`, raw socket | `DENIED` | +| open `/dev/null` loop | `EMFILE` at <= 64 fds | +| fork loop | bounded below 200 (user process rlimit) | +| 768 MiB allocation | denied/killed by 512 MiB `RLIMIT_AS` | +| infinite CPU loop | stopped by wall-clock/CPU budget | +| 4 MiB stdout | stopped at the 64 KiB output limit | + +CLI evidence (`dev`, VM): + +```text +run examples/hello.mq.md --mode strict -> 你好,Marqdo (exit 0) +call factorial 阶乘 --arg n=5 --mode strict -> 120 (exit 0) +run examples/multi-module/index.mq.md --mode strict -> 12.566... (exit 0) +run --mode strict --json -> success=true +run --mode strict --allow-foreign -> exit 2 +run --mode strict --trust-native-plugin ... -> exit 2 +run --mode strict --kernel ... -> exit 2 +run --mode strict --trace-file ... -> exit 2 +MARQDO_STRICT_ACTIVE=1 run --mode strict -> exit 1 (not PID 1) +hide /usr/bin/bwrap; run --mode strict -> exit 1 (not installed) +``` + +## 5. Explicit non-goals / remaining work + +* Strict **L2** (Worker/Foreign inheriting the sandbox) is not implemented; + those flags are refused in strict. Native ABI is never allowed in strict. +* rlimits are used instead of cgroup-v2 accounting; the adapter does not claim + cgroup enforcement. +* Filesystem TOCTOU is reduced by read-only mounts but not formally proven + against a racing host user. +* Network is denied by a network namespace, not a seccomp allowlist. +* **A remote security-lane CI URL is still required** before a release calls + G-C CI-verified. `.github/workflows/security-linux.yml` targets a dedicated + self-hosted runner; `MARQDO_REQUIRE_STRICT_SECURITY=1` makes missing + mechanisms fail instead of skip. diff --git a/spec/security.md b/spec/security.md index a79ab85..cfacb6c 100644 --- a/spec/security.md +++ b/spec/security.md @@ -28,7 +28,7 @@ Normative implementation: |---|---|---| | `pure` | No external I/O; memory computation and budgets only. | **Enforced.** `Capabilities::pure()` = `console`, `math` only; every FS/foreign/plugin grant flag makes the CLI exit 2 with "pure mode prohibits external I/O grants". | | `project` | Operator-approved project resources under `CapabilityHost` + bounded resources. | **Enforced for the implemented operations.** Defaults equal `pure`; `--allow-read`, `--allow-write`, `--allow-foreign`, `--allow-plugin` add grants. Sandboxing is cooperative (in-process checks), not OS isolation. | -| `strict` | Untrusted documents/code/plugins under real OS isolation. | **Refused.** `PlatformCapabilities::detect()` always reports `strict: Unavailable` in this build because no OS isolation adapter (namespaces/seccomp/cgroups, Job Object, macOS sandbox) is implemented, and `worker_processes: false`. `marqdo_sandbox::enforce` returns `StrictUnavailable`; `run`/`call` print the reason and exit 1. There is **no fallback** to an unrestricted process. | +| `strict` | Untrusted documents under real OS isolation. | **Linux: enforced when the runtime probe passes (ADR-0021).** `PlatformCapabilities::detect()` runs a real Bubblewrap smoke test; success reports `Available { mechanism }`, failure reports `Unavailable { reason }`. Strict L1 supports the interpreter, console and native Math only; `--allow-read/--allow-write/--allow-foreign/--allow-plugin/--plugin/--trust-native-plugin/--kernel/--trace-file` are rejected with exit 2. macOS/Windows and Linux hosts without the mechanisms remain `Unavailable`; there is **no fallback** to an unrestricted process. | `marqdo inspect --platform` prints the detection report, for example: @@ -36,15 +36,17 @@ Normative implementation: { "os": "linux", "arch": "x86_64", - "strict": {"state": "unavailable", - "reason": "no OS isolation adapter (namespaces/seccomp/cgroups) is implemented in this build"}, - "worker_processes": false + "strict": {"state": "available", + "mechanism": "bubblewrap 0.11.1 (user/mount/PID/net/UTS/IPC namespaces + no-new-privs + rlimits)"}, + "worker_processes": true } ``` -Design rule (blueprint §9): a cooperative in-process check is not a sandbox. -Strict mode will remain refused until a platform isolation adapter exists; see -ADR-0004. +On a host that cannot create the namespaces the same block is +`{"state": "unavailable", "reason": "..."}` and strict refuses. Design rule +(blueprint §9): a cooperative in-process check is not a sandbox. See ADR-0021 +for the Linux construction, L1 scope and non-goals; ADR-0019 still governs +every other platform. ## 2. Capability grants @@ -200,8 +202,9 @@ What this is **not**: an OS sandbox. The child runs with the host user's privileges, can open absolute paths, use the network and spawn further processes. Clearing the environment and changing cwd are hygiene, not confinement. Grandchildren are not killed when the direct child is killed. -Strict mode refuses to run at all, so foreign execution is only reachable in -`project` mode with `--allow-foreign`. +Strict L1 rejects Foreign execution before any sandbox starts, so foreign +execution is only reachable in `project` mode with `--allow-foreign`. L2 +inheritance is future work (ADR-0021). ## 6. Plugins: in-process native trust boundary @@ -270,14 +273,16 @@ Neither command executes project code. Specifically: ## 8. Open security items -1. Strict mode: no OS isolation adapter (P4); worker processes report - `false`. +1. Strict L1 is implemented and VM-verified on Linux (ADR-0021); strict L2 + (Worker/Foreign inheritance) is not implemented, so those surfaces are + refused in strict. 2. `console` capability is not enforced; print is always allowed. 3. Network capability exists in the model but no host operation uses it; there is no `--allow-net` flag. 4. Write-path symlink TOCTOU (see §2.1). 5. Plugin **worker crash isolation** and the worker capability bridge are - implemented: both native and worker plugins can reach the broker's + implemented (not usable in strict L1): both native and worker plugins can + reach the broker's `fs.read` execution when granted. Other core capabilities and vendor handlers remain unavailable. Broker execution is a cooperative API contract, not an OS sandbox; a worker and a native plugin can still issue diff --git a/tests/security/escape_helper.c b/tests/security/escape_helper.c new file mode 100644 index 0000000..c8b62ac --- /dev/null +++ b/tests/security/escape_helper.c @@ -0,0 +1,237 @@ +/* + * Adversarial helper for the Marqdo Linux strict security lane. + * + * It is deliberately not part of the language runtime: the security test + * launches this program through the same `marqdo-sandbox` supervisor used by + * `marqdo run --mode strict`, then checks that direct syscalls cannot escape + * the filesystem, network, process, privilege or resource boundaries. + * + * Modes (argv[1]): + * alive report uid/gid/pid/no-new-privs + * read try host-only files plus the project's allowed.txt + * write try writing outside the project, into the read-only project, /tmp + * net try IPv4/IPv6 connect and a raw socket + * proc try /proc/1/root and inherited-fd probing + * priv try setuid(0), unshare, mount + * fds open /dev/null until EMFILE + * fork fork until the process budget stops us + * mem allocate and touch 768 MiB + * cpu busy loop forever + * output write 4 MiB to stdout + * + * The program prints one `key=value` line per observation. A test passes only + * when sensitive operations report DENIED/limited and the allowed project file + * is readable. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *outside_path; + +static void report(const char *key, const char *value) { + printf("%s=%s\n", key, value); + fflush(stdout); +} + +static void try_read(const char *key, const char *path, int allowed) { + FILE *file = fopen(path, "r"); + if (file == NULL) { + report(key, "DENIED"); + return; + } + char buffer[64]; + size_t read = fread(buffer, 1, sizeof(buffer) - 1, file); + buffer[read] = '\0'; + fclose(file); + /* Only the project's explicitly allowed file is expected to be readable. */ + report(key, allowed ? "OK" : "LEAK"); +} + +static void try_write(const char *key, const char *path) { + FILE *file = fopen(path, "w"); + if (file == NULL) { + report(key, "DENIED"); + return; + } + fputs("escaped\n", file); + fclose(file); + report(key, "WROTE"); +} + +static int mode_read(void) { + try_read("read_etc_shadow", "/etc/shadow", 0); + try_read("read_root_key", "/root/.ssh/id_ed25519", 0); + try_read("read_home_key", "/home/dev/.ssh/id_ed25519", 0); + try_read("read_project_allowed", "/project/allowed.txt", 1); + return 0; +} + +static int mode_write(void) { + try_write("write_outside", outside_path); + try_write("write_project", "/project/escape.txt"); + try_write("write_tmp", "/tmp/escape.txt"); + return 0; +} + +static int mode_net(void) { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + report("socket_ipv4", "DENIED"); + } else { + struct sockaddr_in address; + memset(&address, 0, sizeof(address)); + address.sin_family = AF_INET; + address.sin_port = htons(80); + inet_pton(AF_INET, "1.1.1.1", &address.sin_addr); + int connected = connect(fd, (struct sockaddr *)&address, sizeof(address)); + report("socket_ipv4", connected == 0 ? "CONNECTED" : "DENIED"); + close(fd); + } + int fd6 = socket(AF_INET6, SOCK_STREAM, 0); + report("socket_ipv6", fd6 < 0 ? "DENIED" : "OPENED"); + if (fd6 >= 0) close(fd6); + int raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + report("socket_raw", raw < 0 ? "DENIED" : "OPENED"); + if (raw >= 0) close(raw); + return 0; +} + +static int mode_proc(void) { + try_read("proc_pid1_root_etc", "/proc/1/root/etc/shadow", 0); + try_read("proc_pid1_root_project", "/proc/1/root/project/allowed.txt", 1); + char path[64]; + int leaks = 0; + for (int fd = 3; fd < 32; fd++) { + snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); + struct stat statbuf; + if (fstat(fd, &statbuf) == 0) { + leaks++; + } + } + report("inherited_fds_above_stdio", leaks == 0 ? "NONE" : "PRESENT"); + return 0; +} + +static int mode_priv(void) { + report("setuid_root", setuid(0) == 0 ? "OK" : "DENIED"); + report("unshare_userns", unshare(CLONE_NEWUSER) == 0 ? "OK" : "DENIED"); + report("mount_tmpfs", mount("none", "/tmp", "tmpfs", 0, "") == 0 ? "OK" : "DENIED"); + return 0; +} + +static int mode_fds(void) { + int count = 0; + for (;;) { + int fd = open("/dev/null", O_RDONLY); + if (fd < 0) break; + count++; + if (count > 4096) break; + } + printf("open_fds=%d\n", count); + report("fd_limit", count <= 64 ? "BOUNDED" : "UNBOUNDED"); + return 0; +} + +static int mode_fork(void) { + int children = 0; + for (int i = 0; i < 200; i++) { + pid_t pid = fork(); + if (pid < 0) break; + if (pid == 0) { + _exit(0); + } + children++; + } + for (int i = 0; i < children; i++) { + wait(NULL); + } + printf("forked=%d\n", children); + report("process_limit", children < 200 ? "BOUNDED" : "UNBOUNDED"); + return 0; +} + +static int mode_mem(void) { + size_t size = 768UL * 1024 * 1024; + char *memory = malloc(size); + if (memory == NULL) { + report("malloc_768m", "DENIED"); + return 0; + } + for (size_t offset = 0; offset < size; offset += 4096) { + memory[offset] = 1; + } + report("malloc_768m", "OK"); + free(memory); + return 0; +} + +static int mode_cpu(void) { + volatile unsigned long counter = 0; + for (;;) { + counter++; + } + return (int)counter; +} + +static int mode_output(void) { + char block[65536]; + memset(block, 'x', sizeof(block)); + for (int i = 0; i < 64; i++) { + if (fwrite(block, 1, sizeof(block), stdout) != sizeof(block)) { + report("output_4m", "DENIED"); + return 3; + } + } + fflush(stdout); + report("output_4m", "WRITTEN"); + return 0; +} + +static int mode_alive(void) { + printf("uid=%d gid=%d pid=%d\n", (int)getuid(), (int)getgid(), (int)getpid()); + FILE *status = fopen("/proc/self/status", "r"); + char line[256]; + while (status != NULL && fgets(line, sizeof(line), status) != NULL) { + if (strncmp(line, "NoNewPrivs:", 11) == 0 || + strncmp(line, "CapEff:", 7) == 0) { + printf("%s", line); + } + } + if (status != NULL) fclose(status); + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + fprintf(stderr, "usage: %s MODE [OUTSIDE_PATH]\n", argv[0]); + return 2; + } + outside_path = argc > 2 ? argv[2] : "/home/dev/escape-outside"; + const char *mode = argv[1]; + if (strcmp(mode, "alive") == 0) return mode_alive(); + if (strcmp(mode, "read") == 0) return mode_read(); + if (strcmp(mode, "write") == 0) return mode_write(); + if (strcmp(mode, "net") == 0) return mode_net(); + if (strcmp(mode, "proc") == 0) return mode_proc(); + if (strcmp(mode, "priv") == 0) return mode_priv(); + if (strcmp(mode, "fds") == 0) return mode_fds(); + if (strcmp(mode, "fork") == 0) return mode_fork(); + if (strcmp(mode, "mem") == 0) return mode_mem(); + if (strcmp(mode, "cpu") == 0) return mode_cpu(); + if (strcmp(mode, "output") == 0) return mode_output(); + fprintf(stderr, "unknown mode: %s\n", mode); + return 2; +} diff --git a/tests/security/run-lane.sh b/tests/security/run-lane.sh new file mode 100755 index 0000000..0e086e0 --- /dev/null +++ b/tests/security/run-lane.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash +# Linux G-C security lane: run the adversarial strict-isolation matrix. +# +# This script must run as an ordinary user on a disposable Ubuntu VM or a +# trusted self-hosted runner. It refuses root because root-only success is not +# evidence that a normal user can use strict mode. +# +# Requirements: bwrap (Bubblewrap with its AppArmor profile), a C compiler, +# Rust toolchain, and a kernel/container policy that permits namespaces. +set -euo pipefail + +repo="$(cd "$(dirname "$0")/../.." && pwd)" +cd "$repo" + +if [ "$(id -u)" -eq 0 ]; then + echo "error: the security lane must run as a non-root user" >&2 + exit 1 +fi +for tool in bwrap cc cargo; do + if ! command -v "$tool" >/dev/null 2>&1; then + echo "error: required tool '$tool' is missing (a missing mechanism must fail G-C)" >&2 + exit 1 + fi +done + +# A skipped security test must fail the gate, not pass silently. +export MARQDO_REQUIRE_STRICT_SECURITY=1 +export CARGO_TERM_COLOR=never + +echo "== adversarial helper matrix ==" +cargo test -p marqdo-sandbox --test security_linux -- --test-threads=1 + +echo "== build CLI ==" +cargo build -p marqdo-cli + +bin="$repo/target/debug/marqdo" +platform="$("$bin" inspect examples/hello.mq.md --platform --json)" +python3 - "$platform" <<'PY' +import json, sys +report = json.loads(sys.argv[1])["platform"]["strict"] +if report["state"] != "available": + raise SystemExit(f"strict is not available: {report}") +if "bubblewrap" not in report.get("mechanism", ""): + raise SystemExit(f"unexpected mechanism: {report}") +print(f"strict mechanism: {report['mechanism']}") +PY + +echo "== positive CLI contract ==" +"$bin" run examples/hello.mq.md --mode strict | grep -q "你好,Marqdo" +"$bin" call examples/factorial.mq.md 数学.阶乘 --arg n=5 --mode strict | grep -qx "120" +"$bin" run examples/multi-module/index.mq.md --mode strict >/dev/null +"$bin" run examples/hello.mq.md --mode strict --json | python3 -c \ + 'import json,sys; d=json.load(sys.stdin); assert d["success"] is True' + +echo "== external-execution surfaces are refused ==" +reject() { + set +e + "$bin" run examples/hello.mq.md --mode strict "$@" >/dev/null 2>&1 + code=$? + set -e + if [ "$code" -ne 2 ]; then + echo "error: strict accepted unsupported flags '$*' (exit $code)" >&2 + exit 1 + fi +} +reject --allow-foreign +reject --allow-read 'data/**' +reject --allow-write 'data/**' +reject --trust-native-plugin --plugin examples/plugin-demo/build +reject --kernel examples/kernel-mock/kernel.json +reject --trace-file /tmp/marqdo-trace.json + +echo "== forged active marker is rejected ==" +set +e +MARQDO_STRICT_ACTIVE=1 "$bin" run examples/hello.mq.md --mode strict >/dev/null 2>&1 +code=$? +set -e +if [ "$code" -ne 1 ]; then + echo "error: forged strict marker was not rejected (exit $code)" >&2 + exit 1 +fi + +echo "G-C security lane: PASS"