fix(sandbox): add platform-neutral strict child inspection
CI / fmt + clippy + test (Linux x86_64) (push) Waiting to run
CI / schema instance validation + consumers (push) Waiting to run
CI / runtime contract (macos-latest) (push) Waiting to run
CI / runtime contract (windows-latest) (push) Waiting to run
ci/woodpecker/push/woodpecker Pipeline was successful

The CLI referenced Linux-only marker_present/verify_active directly, so
macOS and Windows builds failed with E0425. Add StrictChildState and
inspect_strict_child() to marqdo-sandbox: Linux performs the full identity
verification, while every other platform rejects even a forged
MARQDO_STRICT_ACTIVE marker. The Linux-only functions are no longer
re-exported from the crate root, so future callers cannot bypass the
platform boundary.

This is step 1 of the CI/security RFC (B01).
This commit is contained in:
cflmy committed 2026-11-21 16:36:49 +08:00
1 parent 32d8fe1f7a
commit f4a404bae9
3 files changed
+95 -28

No files matched your search

+24 -20
View File
@@ -4,6 +4,7 @@
//! runtime helpers, but keeps the run session out of the command table.
use super::*;
use marqdo_sandbox::StrictChildState;
pub fn execute(
file: Option<&Path>,
@@ -64,31 +65,34 @@ pub fn execute(
}
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() {
match marqdo_sandbox::inspect_strict_child() {
// A verified supervisor child may run project code.
Ok(StrictChildState::VerifiedChild) => {}
// Not a child: enforce capability and launch under the supervisor.
Ok(StrictChildState::NotChild) => {
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);
}
// A marker without a verified identity is never trusted, and the
// platform-neutral inspection keeps Linux-only symbols out of this
// module so macOS/Windows builds compile.
Err(reason) => {
let message = format!("strict isolation verification failed: {reason}");
if json {
output::print_json(&json!({
"success": false,
"error": format!("strict isolation verification failed: {reason}"),
}));
output::print_json(&json!({"success": false, "error": message}));
} else {
eprintln!("error: strict isolation verification failed: {reason}");
eprintln!("error: {message}");
}
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();
+68 -2
View File
@@ -13,7 +13,12 @@ use serde::{Deserialize, Serialize};
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
pub use linux::{marker_present, probe, verify_active, StrictOutcome};
pub use linux::{probe, StrictOutcome};
/// Environment variable set by the strict supervisor for its re-executed
/// child. The marker alone never authorizes strict execution; [`inspect_strict_child`]
/// verifies the child identity before anything is trusted.
pub const STRICT_ACTIVE_ENV: &str = "MARQDO_STRICT_ACTIVE";
/// Execution mode requested on the command line.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -160,6 +165,46 @@ fn detect_strict() -> StrictIsolation {
}
}
/// Whether the current process is a verified strict child.
///
/// This is the **only** supported platform boundary for strict-child
/// detection. Linux performs the full identity verification; every other
/// platform refuses even a forged marker instead of treating it as a child.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StrictChildState {
/// The process was not launched by the strict supervisor.
NotChild,
/// The process is the verified strict child and may run project code.
VerifiedChild,
}
/// Inspect whether this process is the strict supervisor's verified child.
///
/// Linux requires the marker **and** the full namespace/privilege identity
/// check in [`linux::verify_active`]. Non-Linux builds reject any marker:
/// without a verified OS isolation adapter there is no legitimate strict
/// child, so a forged `MARQDO_STRICT_ACTIVE` must fail closed.
pub fn inspect_strict_child() -> Result<StrictChildState, String> {
#[cfg(target_os = "linux")]
{
if !linux::marker_present() {
return Ok(StrictChildState::NotChild);
}
linux::verify_active().map(|()| StrictChildState::VerifiedChild)
}
#[cfg(not(target_os = "linux"))]
{
if std::env::var_os(STRICT_ACTIVE_ENV).is_some() {
return Err(
"strict active marker is unsupported on this platform; refusing to treat this process as a verified strict child"
.to_string(),
);
}
Ok(StrictChildState::NotChild)
}
}
/// Failure to apply the requested sandbox mode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SandboxError {
@@ -196,7 +241,7 @@ pub fn enforce(mode: SandboxMode, platform: &PlatformCapabilities) -> Result<(),
match mode {
SandboxMode::Strict => {
#[cfg(target_os = "linux")]
if verify_active().is_ok() {
if linux::verify_active().is_ok() {
return Ok(());
}
if platform.strict.is_available() {
@@ -270,6 +315,27 @@ mod tests {
assert_eq!(SandboxMode::parse("nope"), None);
}
#[cfg(target_os = "linux")]
#[test]
fn ordinary_process_is_not_a_strict_child() {
if std::env::var_os(STRICT_ACTIVE_ENV).is_none() {
assert_eq!(
inspect_strict_child().expect("inspection must not fail without a marker"),
StrictChildState::NotChild
);
}
}
#[cfg(not(target_os = "linux"))]
#[test]
fn forged_marker_is_rejected_on_non_linux() {
std::env::set_var(STRICT_ACTIVE_ENV, "1");
let result = inspect_strict_child();
std::env::remove_var(STRICT_ACTIVE_ENV);
let error = result.expect_err("a forged marker must never authorize strict execution");
assert!(error.contains("unsupported"), "unexpected error: {error}");
}
#[test]
fn strict_refuses_when_isolation_is_unavailable() {
let platform = PlatformCapabilities {
+3 -6
View File
@@ -30,9 +30,6 @@ 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";
@@ -158,7 +155,7 @@ fn common_args() -> Vec<OsString> {
/// 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") {
if std::env::var(crate::STRICT_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.
@@ -204,7 +201,7 @@ pub fn verify_active() -> Result<(), String> {
/// 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")
std::env::var(crate::STRICT_ACTIVE_ENV).ok().as_deref() == Some("1")
}
/// Result of one supervised strict execution.
@@ -335,7 +332,7 @@ fn run_with(
command.program.as_os_str().to_os_string(),
OsString::from(SANDBOX_PROGRAM),
OsString::from("--setenv"),
OsString::from(ACTIVE_ENV),
OsString::from(crate::STRICT_ACTIVE_ENV),
OsString::from("1"),
OsString::from("--chdir"),
OsString::from("/project"),