From 1d87f98539a5fffa3ee6543dc1019d0bd144ab4c Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 20 Nov 2026 22:57:49 +0800 Subject: [PATCH] fix(strict): bind the bundled stdlib and verify strict resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Strict child now gets the verified stdlib tree read-only at /run/marqdo-stdlib with MARQDO_STDLIB_DIR set, so Compat-C 打印 works under strict without exposing a host path (ADR-0021 update). - Add a strict-lane negative: resource.open(file) is refused with permission.denied; hello/factorial/multi-module and --json still pass. - StrictHostCommand gains stdlib_dir; CLI discovers the bundle and sandbox binds it; sandbox tests constructors updated. Evidence: G-C lane PASS on the disposable Ubuntu 26.04 VM (7/7 adversarial matrix, positive CLI, strict language-resource refusal, forged marker), plus fmt/clippy/423 local tests. --- crates/cli/src/commands.rs | 5 ++++ crates/cli/src/stdlib.rs | 5 ++++ crates/marqdo-sandbox/src/lib.rs | 2 ++ crates/marqdo-sandbox/src/linux.rs | 12 +++++++++ crates/marqdo-sandbox/tests/security_linux.rs | 3 +++ docs/adr/0021-linux-strict-isolation.md | 13 ++++++++-- docs/security-vm.md | 2 +- spec/security.md | 2 +- tests/security/run-lane.sh | 25 +++++++++++++++++++ 9 files changed, 65 insertions(+), 4 deletions(-) diff --git a/crates/cli/src/commands.rs b/crates/cli/src/commands.rs index fd08696..9c969a7 100644 --- a/crates/cli/src/commands.rs +++ b/crates/cli/src/commands.rs @@ -682,6 +682,10 @@ fn launch_strict_process( .unwrap_or(1024 * 1024) .min(64 * 1024 * 1024); let timeout = Duration::from_secs(settings.timeout.unwrap_or(30)); + let stdlib_dir = crate::stdlib::StdlibBundle::discover() + .ok() + .flatten() + .map(|bundle| bundle.root().to_path_buf()); let command = marqdo_sandbox::StrictHostCommand { program: &program, args: &args, @@ -690,6 +694,7 @@ fn launch_strict_process( original_document: document, output_limit, timeout, + stdlib_dir: stdlib_dir.as_deref(), }; match marqdo_sandbox::launch_strict(&command) { Ok(code) => code, diff --git a/crates/cli/src/stdlib.rs b/crates/cli/src/stdlib.rs index bbc464f..8e5801a 100644 --- a/crates/cli/src/stdlib.rs +++ b/crates/cli/src/stdlib.rs @@ -89,6 +89,11 @@ impl StdlibBundle { }) } + /// Bundle root. + pub fn root(&self) -> &Path { + &self.root + } + /// Declared stdlib version. pub fn version(&self) -> &str { &self.version diff --git a/crates/marqdo-sandbox/src/lib.rs b/crates/marqdo-sandbox/src/lib.rs index 76f5bb3..d56d497 100644 --- a/crates/marqdo-sandbox/src/lib.rs +++ b/crates/marqdo-sandbox/src/lib.rs @@ -232,6 +232,8 @@ pub struct StrictHostCommand<'a> { pub output_limit: usize, /// Wall-clock budget for the supervised process tree. pub timeout: Duration, + /// Verified bundled stdlib directory to bind read-only when present. + pub stdlib_dir: Option<&'a Path>, } /// Launch a command inside the strict sandbox (Linux only). diff --git a/crates/marqdo-sandbox/src/linux.rs b/crates/marqdo-sandbox/src/linux.rs index ece9fa4..18ff082 100644 --- a/crates/marqdo-sandbox/src/linux.rs +++ b/crates/marqdo-sandbox/src/linux.rs @@ -317,6 +317,16 @@ fn run_with( })?; let mut args = common_args(); + if let Some(stdlib) = command.stdlib_dir { + args.extend([ + OsString::from("--ro-bind"), + stdlib.as_os_str().to_os_string(), + OsString::from("/run/marqdo-stdlib"), + OsString::from("--setenv"), + OsString::from("MARQDO_STDLIB_DIR"), + OsString::from("/run/marqdo-stdlib"), + ]); + } args.extend([ OsString::from("--ro-bind"), project_root.as_os_str().to_os_string(), @@ -548,6 +558,7 @@ mod tests { original_document: None, output_limit: 4096, timeout: Duration::from_secs(1), + stdlib_dir: None, }; let error = run_with( &command, @@ -585,6 +596,7 @@ mod tests { original_document: None, output_limit: 4096, timeout: Duration::from_secs(1), + stdlib_dir: None, }; let error = run_with( &command, diff --git a/crates/marqdo-sandbox/tests/security_linux.rs b/crates/marqdo-sandbox/tests/security_linux.rs index 225e269..067c1fa 100644 --- a/crates/marqdo-sandbox/tests/security_linux.rs +++ b/crates/marqdo-sandbox/tests/security_linux.rs @@ -111,6 +111,7 @@ fn run(helper: &Path, project: &Path, outside: &Path, mode: &str) -> StrictOutco original_document: None, output_limit: 64 * 1024, timeout: Duration::from_secs(3), + stdlib_dir: None, }; launch_strict_capture(&command).unwrap_or_else(|error| panic!("{mode}: {error}")) } @@ -267,6 +268,7 @@ fn strict_supervisor_enforces_memory_cpu_and_output_limits() { original_document: None, output_limit: 64 * 1024, timeout: Duration::from_secs(2), + stdlib_dir: None, }; let cpu = launch_strict_capture(&command).expect_err("CPU loop must be stopped"); let message = cpu.to_string(); @@ -283,6 +285,7 @@ fn strict_supervisor_enforces_memory_cpu_and_output_limits() { original_document: None, output_limit: 64 * 1024, timeout: Duration::from_secs(3), + stdlib_dir: None, }) .expect("output helper should be captured, not spawn-failed"); assert!( diff --git a/docs/adr/0021-linux-strict-isolation.md b/docs/adr/0021-linux-strict-isolation.md index 87d55fb..07143ae 100644 --- a/docs/adr/0021-linux-strict-isolation.md +++ b/docs/adr/0021-linux-strict-isolation.md @@ -54,14 +54,23 @@ The trusted supervisor is this process; the restricted child is the same The document argument is rewritten to `/project/...`; only the document token changes, all other arguments are preserved. -### 4. Child self-verification +### 4. Bundled stdlib mount + +After CM-A07 Compat-C, `打印` is implemented by the bundled `.mq.md` stdlib. +Strict therefore bind-mounts the verified `stdlib/` tree read-only at +`/run/marqdo-stdlib` and sets `MARQDO_STDLIB_DIR` for the child, just like the +program itself. The tree contains only Markdown/read-only data; it grants no +new OS capability, and the loader still verifies its digest in the child. File, +network, process and provider resources remain refused. + +### 5. 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 +### 6. 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 diff --git a/docs/security-vm.md b/docs/security-vm.md index 1bd2ced..a0ee6a6 100644 --- a/docs/security-vm.md +++ b/docs/security-vm.md @@ -126,7 +126,7 @@ export PATH="$HOME/.cargo/bin:$PATH" ./tests/security/run-lane.sh ``` -Expected final line: `G-C security lane: PASS`. The script exports +Expected final line: `G-C security lane: PASS`. The lane also checks that the read-only bundled stdlib works for `打印` and that a project-file `resource.open` is refused in strict mode. The script exports `MARQDO_REQUIRE_STRICT_SECURITY=1`; a missing bubblewrap, compiler or helper is a failure, never a skip. diff --git a/spec/security.md b/spec/security.md index b0c9262..0b267dd 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. `resource.open` supports stdout write, granted project-file read/write (ADR-0025/0026) and granted in-process provider kinds (ADR-0033); network/process remain unsupported. Sandboxing is cooperative (in-process checks), not OS isolation; in-process providers are trusted, not sandboxed. | -| `strict` | Untrusted documents under real OS isolation. | **Linux: enforced when the runtime probe passes (ADR-0021).** `resource.open` is **refused** in strict except for the existing `stdio/stdout` console channel (console grant); file/network/process resources remain refused until a provider passes the isolation lane (CM-A11, ADR-0030). `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. | +| `strict` | Untrusted documents under real OS isolation. | **Linux: enforced when the runtime probe passes (ADR-0021).** `resource.open` is **refused** in strict except for the existing `stdio/stdout` console channel (console grant); file/network/process resources remain refused until a provider passes the isolation lane (CM-A11, ADR-0030). `PlatformCapabilities::detect()` runs a real Bubblewrap smoke test; success reports `Available { mechanism }`, failure reports `Unavailable { reason }`. Strict L1 supports the interpreter, console, native Math and the read-only bundled stdlib (published at `/run/marqdo-stdlib` for the Compat-C print chain); `--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: diff --git a/tests/security/run-lane.sh b/tests/security/run-lane.sh index 0122a88..fae32fc 100755 --- a/tests/security/run-lane.sh +++ b/tests/security/run-lane.sh @@ -53,6 +53,31 @@ echo "== positive CLI contract ==" "$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 "== strict language resources are console-only ==" +mkdir -p .cache/security +cat > .cache/security/file-resource.mq.md <<'MQ' +## main + +**内存 = memory.new(1, 1)** + +> resource.open("file", "README.md", ["read"]) + +*None* +MQ +set +e +"$bin" run .cache/security/file-resource.mq.md --mode strict >/dev/null 2>.cache/security/file-resource.err +code=$? +set -e +if [ "$code" -eq 0 ]; then + echo "error: strict accepted a project file resource" >&2 + exit 1 +fi +grep -q "permission.denied" .cache/security/file-resource.err || { + echo "error: strict file-resource refusal had no permission.denied diagnostic" >&2 + cat .cache/security/file-resource.err >&2 + exit 1 +} + echo "== external-execution surfaces are refused ==" reject() { set +e