feat(sandbox): implement probe-gated Linux strict isolation (ADR-0021)

Strict mode now re-executes the CLI inside a Bubblewrap supervisor with
user/mount/PID/net/UTS/IPC namespaces, no-new-privs, all capabilities dropped,
a read-only /project, private /proc and /dev, rlimits, output capture and
wall-clock process-group cleanup. The child verifies PID 1, NoNewPrivs, empty
CapEff, an unshared uid map and loopback-only networking before project code.

- PlatformCapabilities::detect runs a real bwrap probe; no probe, no strict
- strict L1 supports pure Marqdo + Math; Foreign/ABI/worker/kernel/grants/trace
  are rejected with exit 2 before sandboxing
- forged MARQDO_STRICT_ACTIVE is rejected; missing bwrap fails closed
- add adversarial helper, 7-test escape/resource matrix, seeded security lane
  script and self-hosted security workflow
- executed G-C lane as uid 1000 in a disposable Ubuntu 26.04 VM: 7/7 PASS
  plus positive CLI and refusal matrix; evidence in docs/evidence
- ADR-0021, spec/security.md, STATUS/COMPATIBILITY/README/CHANGELOG updated;
  ADR-0019 remains for non-Linux/fail-closed rules
This commit is contained in:
cflmy committed 2026-11-20 15:01:49 +08:00
1 parent 9b001a33a5
commit b6fc18233d
22 files changed
+1886 -132

No files matched your search

+102 -1
View File
@@ -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<Capabilities, String> {
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::ffi::OsString> = 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 {
+54 -13
View File
@@ -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);
}