fix(cli): normalize strict failures to one JSON document
ci/woodpecker/push/woodpecker Pipeline was successful
CI / fmt + clippy + test (Linux x86_64) (push) Canceled after 0s
CI / schema instance validation + consumers (push) Canceled after 0s
CI / runtime contract (macos-latest) (push) Canceled after 0s
CI / runtime contract (windows-latest) (push) Canceled after 0s
CI / release candidate artifact (macos-latest) (push) Canceled after 0s
CI / release candidate artifact (ubuntu-latest) (push) Canceled after 0s
ci/woodpecker/push/woodpecker Pipeline was successful
CI / fmt + clippy + test (Linux x86_64) (push) Canceled after 0s
CI / schema instance validation + consumers (push) Canceled after 0s
CI / runtime contract (macos-latest) (push) Canceled after 0s
CI / runtime contract (windows-latest) (push) Canceled after 0s
CI / release candidate artifact (macos-latest) (push) Canceled after 0s
CI / release candidate artifact (ubuntu-latest) (push) Canceled after 0s
Capture the strict child through launch_strict_capture and classify the outcome before writing anything. In --json mode a control-plane failure now emits exactly one error document with a stable code instead of forwarding bubblewrap stderr and leaving stdout empty; a verified child's valid JSON is forwarded unchanged with its own exit code. Empty, partial or non-JSON child output becomes strict.child_invalid. Route the remaining run --json error paths (mode, grants, missing entry, trace) through the same contract, classify sandbox launch failures with StrictFailureKind so probe/spawn/timeout/resource failures stay distinguishable, and add deterministic routing tests plus forged-marker CLI coverage. Document the decision in ADR-0053 and spec/security.md 1.2. This is step 2 of the CI/security RFC (B02/B03).
This commit is contained in:
1 parent
f4a404bae9
commit
f817cceb3b
9 files changed
+634
-103
No files matched your search
@@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux;
|
||||
#[cfg(target_os = "linux")]
|
||||
pub use linux::{probe, StrictOutcome};
|
||||
pub use linux::probe;
|
||||
|
||||
/// Environment variable set by the strict supervisor for its re-executed
|
||||
/// child. The marker alone never authorizes strict execution; [`inspect_strict_child`]
|
||||
@@ -197,7 +197,8 @@ pub fn inspect_strict_child() -> Result<StrictChildState, String> {
|
||||
{
|
||||
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"
|
||||
"strict active marker is unsupported on this platform; refusing to treat this \
|
||||
process as a verified strict child"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
@@ -205,6 +206,56 @@ pub fn inspect_strict_child() -> Result<StrictChildState, String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of one supervised strict execution.
|
||||
///
|
||||
/// The outcome is a byte capture, **not** proof that the child completed
|
||||
/// isolation verification: the caller must still route it through the public
|
||||
/// strict contract (one JSON document in `--json` mode, no fallback).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StrictOutcome {
|
||||
/// Process exit code when the program exited normally.
|
||||
pub exit_code: Option<i32>,
|
||||
/// Terminating signal, when the program was killed by the supervisor,
|
||||
/// a resource limit or the kernel.
|
||||
pub signal: Option<i32>,
|
||||
/// Captured stdout (bounded by the output limit).
|
||||
pub stdout: Vec<u8>,
|
||||
/// Captured stderr (bounded by the output limit).
|
||||
pub stderr: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Stable classification of a strict control-plane failure.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum StrictFailureKind {
|
||||
/// The runtime probe could not construct the isolation mechanism.
|
||||
ProbeUnavailable,
|
||||
/// The supervisor process could not be spawned.
|
||||
SpawnFailed,
|
||||
/// The supervisor exceeded the wall-clock budget.
|
||||
Timeout,
|
||||
/// A resource limit or signal terminated the supervised child.
|
||||
ResourceLimit,
|
||||
/// The requested launch is invalid (host root, document outside project).
|
||||
InvalidRequest,
|
||||
/// Capturing or reading the supervised output failed.
|
||||
Io,
|
||||
}
|
||||
|
||||
impl StrictFailureKind {
|
||||
/// Stable machine-readable code consumed by `--json` error documents.
|
||||
pub const fn code(self) -> &'static str {
|
||||
match self {
|
||||
StrictFailureKind::ProbeUnavailable => "strict.probe_unavailable",
|
||||
StrictFailureKind::SpawnFailed => "strict.spawn_failed",
|
||||
StrictFailureKind::Timeout => "strict.timeout",
|
||||
StrictFailureKind::ResourceLimit => "strict.resource_limit",
|
||||
StrictFailureKind::InvalidRequest => "strict.launch_failed",
|
||||
StrictFailureKind::Io => "strict.launch_failed",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Failure to apply the requested sandbox mode.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum SandboxError {
|
||||
@@ -215,11 +266,31 @@ pub enum SandboxError {
|
||||
},
|
||||
/// The strict supervisor could not launch, verify or supervise the child.
|
||||
LaunchFailed {
|
||||
/// Machine-readable classification.
|
||||
kind: StrictFailureKind,
|
||||
/// Actionable explanation.
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl SandboxError {
|
||||
/// Build a classified launch failure.
|
||||
pub fn launch(kind: StrictFailureKind, message: impl Into<String>) -> Self {
|
||||
SandboxError::LaunchFailed {
|
||||
kind,
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Stable machine-readable failure code for `--json` error documents.
|
||||
pub const fn code(&self) -> &'static str {
|
||||
match self {
|
||||
SandboxError::StrictUnavailable { .. } => "strict.probe_unavailable",
|
||||
SandboxError::LaunchFailed { kind, .. } => kind.code(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SandboxError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
@@ -227,7 +298,7 @@ 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 } => {
|
||||
SandboxError::LaunchFailed { message, .. } => {
|
||||
write!(f, "strict isolation failed: {message}")
|
||||
}
|
||||
}
|
||||
@@ -291,7 +362,7 @@ pub fn launch_strict(command: &StrictHostCommand<'_>) -> Result<i32, SandboxErro
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn launch_strict_capture(
|
||||
command: &StrictHostCommand<'_>,
|
||||
) -> Result<linux::StrictOutcome, SandboxError> {
|
||||
) -> Result<StrictOutcome, SandboxError> {
|
||||
linux::launch_capture(command)
|
||||
}
|
||||
|
||||
@@ -303,6 +374,17 @@ pub fn launch_strict(_command: &StrictHostCommand<'_>) -> Result<i32, SandboxErr
|
||||
})
|
||||
}
|
||||
|
||||
/// Capture a strict execution (unsupported elsewhere): every platform must
|
||||
/// compile the same CLI routing code and fail closed instead of falling back.
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn launch_strict_capture(
|
||||
_command: &StrictHostCommand<'_>,
|
||||
) -> Result<StrictOutcome, SandboxError> {
|
||||
Err(SandboxError::StrictUnavailable {
|
||||
reason: "strict OS isolation is only implemented on Linux".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -28,7 +28,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, Command, ExitStatus, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::{SandboxError, StrictHostCommand};
|
||||
use crate::{SandboxError, StrictFailureKind, StrictHostCommand, StrictOutcome};
|
||||
|
||||
/// Path of the program inside the sandbox.
|
||||
const SANDBOX_PROGRAM: &str = "/run/marqdo-program";
|
||||
@@ -204,20 +204,6 @@ pub fn marker_present() -> bool {
|
||||
std::env::var(crate::STRICT_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<i32>,
|
||||
/// Terminating signal, when the program was killed by the supervisor,
|
||||
/// a resource limit or the kernel.
|
||||
pub signal: Option<i32>,
|
||||
/// Captured stdout (bounded by the output limit).
|
||||
pub stdout: Vec<u8>,
|
||||
/// Captured stderr (bounded by the output limit).
|
||||
pub stderr: Vec<u8>,
|
||||
}
|
||||
|
||||
/// 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<i32, SandboxError> {
|
||||
@@ -248,7 +234,10 @@ fn finish(outcome: StrictOutcome) -> Result<i32, SandboxError> {
|
||||
libc::SIGKILL => "strict child was killed by the resource supervisor".to_string(),
|
||||
other => format!("strict child terminated by signal {other}"),
|
||||
};
|
||||
Err(SandboxError::LaunchFailed { message })
|
||||
Err(SandboxError::launch(
|
||||
StrictFailureKind::ResourceLimit,
|
||||
message,
|
||||
))
|
||||
}
|
||||
|
||||
/// Probe function used by [`run`]. A function pointer keeps the production
|
||||
@@ -274,43 +263,52 @@ fn run_with(
|
||||
spawn_fn: SpawnFn,
|
||||
) -> Result<StrictOutcome, SandboxError> {
|
||||
if command.project_root == Path::new("/") {
|
||||
return Err(SandboxError::LaunchFailed {
|
||||
message: "strict mode refuses to expose the host root as the project".to_string(),
|
||||
});
|
||||
return Err(SandboxError::launch(
|
||||
StrictFailureKind::InvalidRequest,
|
||||
"strict mode refuses to expose the host root as the project",
|
||||
));
|
||||
}
|
||||
let project_root =
|
||||
fs::canonicalize(command.project_root).map_err(|error| SandboxError::LaunchFailed {
|
||||
message: format!(
|
||||
let project_root = fs::canonicalize(command.project_root).map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::InvalidRequest,
|
||||
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()
|
||||
),
|
||||
})?;
|
||||
let absolute = fs::canonicalize(document).map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::InvalidRequest,
|
||||
format!("cannot resolve strict document: {error}"),
|
||||
)
|
||||
})?;
|
||||
let relative = absolute.strip_prefix(&project_root).map_err(|_| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::InvalidRequest,
|
||||
format!(
|
||||
"strict document `{}` is outside the project root `{}`",
|
||||
absolute.display(),
|
||||
project_root.display()
|
||||
),
|
||||
)
|
||||
})?;
|
||||
Some(Path::new("/project").join(relative))
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
probe_fn().map_err(|reason| SandboxError::LaunchFailed { message: reason })?;
|
||||
let bwrap = bwrap_path().ok_or_else(|| SandboxError::LaunchFailed {
|
||||
message: "bubblewrap disappeared after the probe".to_string(),
|
||||
probe_fn()
|
||||
.map_err(|reason| SandboxError::launch(StrictFailureKind::ProbeUnavailable, reason))?;
|
||||
let bwrap = bwrap_path().ok_or_else(|| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::ProbeUnavailable,
|
||||
"bubblewrap disappeared after the probe",
|
||||
)
|
||||
})?;
|
||||
|
||||
let mut args = common_args();
|
||||
@@ -354,9 +352,10 @@ fn run_with(
|
||||
args.push(argument.clone());
|
||||
}
|
||||
if !replaced {
|
||||
return Err(SandboxError::LaunchFailed {
|
||||
message: "strict launcher could not rewrite the document argument".to_string(),
|
||||
});
|
||||
return Err(SandboxError::launch(
|
||||
StrictFailureKind::InvalidRequest,
|
||||
"strict launcher could not rewrite the document argument",
|
||||
));
|
||||
}
|
||||
|
||||
let stdout = CaptureFile::create("stdout")?;
|
||||
@@ -372,8 +371,11 @@ fn run_with(
|
||||
limit,
|
||||
cpu_limit,
|
||||
)
|
||||
.map_err(|error| SandboxError::LaunchFailed {
|
||||
message: format!("cannot start the strict supervisor: {error}"),
|
||||
.map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::SpawnFailed,
|
||||
format!("cannot start the strict supervisor: {error}"),
|
||||
)
|
||||
})?;
|
||||
|
||||
let status = match wait_with_deadline(&mut child, timeout) {
|
||||
@@ -384,7 +386,7 @@ fn run_with(
|
||||
let _ = child.wait();
|
||||
let _ = stdout.read_all();
|
||||
let _ = stderr.read_all();
|
||||
return Err(SandboxError::LaunchFailed { message });
|
||||
return Err(SandboxError::launch(StrictFailureKind::Timeout, message));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -482,8 +484,11 @@ impl CaptureFile {
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&path)
|
||||
.map_err(|error| SandboxError::LaunchFailed {
|
||||
message: format!("cannot create strict capture file: {error}"),
|
||||
.map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::Io,
|
||||
format!("cannot create strict capture file: {error}"),
|
||||
)
|
||||
})?;
|
||||
Ok(Self {
|
||||
path,
|
||||
@@ -494,24 +499,32 @@ impl CaptureFile {
|
||||
fn reopen(&self) -> Result<fs::File, SandboxError> {
|
||||
self.file
|
||||
.as_ref()
|
||||
.ok_or_else(|| SandboxError::LaunchFailed {
|
||||
message: "strict capture file is closed".to_string(),
|
||||
.ok_or_else(|| {
|
||||
SandboxError::launch(StrictFailureKind::Io, "strict capture file is closed")
|
||||
})?
|
||||
.try_clone()
|
||||
.map_err(|error| SandboxError::LaunchFailed {
|
||||
message: format!("cannot clone strict capture file: {error}"),
|
||||
.map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::Io,
|
||||
format!("cannot clone strict capture file: {error}"),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn read_all(&self) -> Result<Vec<u8>, SandboxError> {
|
||||
let mut file = fs::File::open(&self.path).map_err(|error| SandboxError::LaunchFailed {
|
||||
message: format!("cannot read strict capture file: {error}"),
|
||||
let mut file = fs::File::open(&self.path).map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::Io,
|
||||
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}"),
|
||||
})?;
|
||||
file.read_to_end(&mut bytes).map_err(|error| {
|
||||
SandboxError::launch(
|
||||
StrictFailureKind::Io,
|
||||
format!("cannot read strict capture file: {error}"),
|
||||
)
|
||||
})?;
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
@@ -566,7 +579,8 @@ mod tests {
|
||||
let _ = fs::remove_dir_all(&project);
|
||||
|
||||
match &error {
|
||||
SandboxError::LaunchFailed { message } => {
|
||||
SandboxError::LaunchFailed { kind, message } => {
|
||||
assert_eq!(*kind, StrictFailureKind::SpawnFailed);
|
||||
assert!(
|
||||
message.contains("cannot start the strict supervisor"),
|
||||
"unexpected launch diagnostic: {message}"
|
||||
@@ -601,7 +615,12 @@ mod tests {
|
||||
|_, _, _, _, _, _| unreachable!("host root must be rejected before spawning"),
|
||||
)
|
||||
.expect_err("host root must be rejected");
|
||||
assert!(matches!(error, SandboxError::LaunchFailed { .. }));
|
||||
match &error {
|
||||
SandboxError::LaunchFailed { kind, .. } => {
|
||||
assert_eq!(*kind, StrictFailureKind::InvalidRequest);
|
||||
}
|
||||
other => panic!("expected LaunchFailed, got {other:?}"),
|
||||
}
|
||||
assert!(error.to_string().contains("host root"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user