Files
faunosteo/crates/worker/tests/sandbox.rs
T
bchanot e967ce6736 test(worker): verify child at the input caps, brk bounded by the limits
Max frame (document and base 2 MiB, drawings 6 MiB) passes through the full
sandbox; refusals just beyond give caps verdicts; RLIMIT_AS and the argument
filters still stop the verify child; seccomp traced on the max input; the two
tracked misclassifications asserted as current behaviour.
2026-11-01 21:07:23 +01:00

449 lines
13 KiB
Rust

//! R0 proofs of the decode child, run against the real binary (`--features test-hooks`,
//! Linux): the full sandbox accepts the fixture corpus, refuses what it must, and every
//! other way the child can die is observed and classified by the parent.
#![cfg(all(feature = "test-hooks", target_os = "linux"))]
#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::process::{Command, Stdio};
use std::time::Duration;
use faunosteo_worker::spawn::{ChildOutcome, Spawner};
use faunosteo_worker::{exit_codes, fixtures, limits, webp_check};
const SIGABRT: i32 = 6;
const SIGXCPU: i32 = 24;
const SIGSYS: i32 = 31;
fn spawner() -> Spawner {
Spawner::new(env!("CARGO_BIN_EXE_faunosteo-worker"))
}
fn with_hook(flags: &[&str]) -> Spawner {
let mut spawner = spawner();
spawner.extra_args = flags.iter().map(|flag| (*flag).to_owned()).collect();
spawner
}
fn expect_rejected(bytes: &[u8], code: i32) {
let run = spawner().decode(bytes);
match run.outcome {
ChildOutcome::Rejected { code: got, .. } if got == code => {}
other => panic!(
"expected {}, got {other:?} (stderr: {})",
exit_codes::name(code),
run.stderr
),
}
}
#[test]
fn png_jpeg_and_webp_decode_through_the_full_sandbox() {
for (name, bytes) in [
("png", fixtures::png(800, 600)),
("jpeg", fixtures::jpeg(640, 480)),
("webp", fixtures::webp(300, 700)),
] {
let run = spawner().decode(&bytes);
match run.outcome {
ChildOutcome::Produced(out) => {
let summary = webp_check::inspect(&out).unwrap();
assert_eq!((summary.width, summary.height), (512, 512), "{name}");
assert!(
out.len() < limits::MAX_OUTPUT_BYTES,
"{name}: {} bytes",
out.len()
);
}
other => panic!("{name}: {other:?} (stderr: {})", run.stderr),
}
}
}
#[test]
fn inputs_at_the_caps_decode_within_the_limits() {
let big = fixtures::jpeg(4096, 4096);
let run = spawner().decode(&big);
assert!(
matches!(run.outcome, ChildOutcome::Produced(_)),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn too_small_images_are_refused_on_dimensions() {
expect_rejected(&fixtures::png(32, 32), exit_codes::DIMENSIONS);
}
#[test]
fn oversized_headers_are_refused_before_decoding() {
expect_rejected(
&fixtures::png_header_only(20_000, 20_000),
exit_codes::DIMENSIONS,
);
}
#[test]
fn an_iccp_bomb_is_a_content_rejection_not_a_broken_environment() {
// The png crate reports the inflated profile as a decoding error; the property proven
// here is that content can only ever be rejected, never classified as a broken
// environment (the outcome R1 alerts on).
let run = spawner().decode(&fixtures::png_iccp_bomb(300 << 20));
match run.outcome {
ChildOutcome::Rejected { code, .. }
if code == exit_codes::DECODE_FAILED || code == exit_codes::RESOURCE_LIMIT => {}
other => panic!(
"expected a content rejection, got {other:?} ({})",
run.stderr
),
}
}
#[test]
fn a_signal_to_the_parent_is_killed_by_the_argument_filter() {
let run = with_hook(&["--signal-parent"]).decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGSYS }),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn an_executable_mapping_is_killed_by_the_argument_filter() {
let run = with_hook(&["--mmap-exec"]).decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGSYS }),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn unsupported_formats_are_refused() {
expect_rejected(&fixtures::gif(), exit_codes::UNSUPPORTED_TYPE);
expect_rejected(
b"<svg xmlns='http://www.w3.org/2000/svg'/>",
exit_codes::UNSUPPORTED_TYPE,
);
}
#[test]
fn truncated_files_are_decode_failures() {
expect_rejected(&fixtures::truncated_png(), exit_codes::DECODE_FAILED);
}
#[test]
fn the_child_refuses_a_non_empty_environment() {
let status = Command::new(env!("CARGO_BIN_EXE_faunosteo-worker"))
.arg("decode")
.env_clear()
.env("LEAKED", "1")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
assert_eq!(status.code(), Some(exit_codes::ENV_NOT_EMPTY));
}
#[test]
fn a_denied_syscall_kills_the_child_with_sigsys() {
let run = with_hook(&["--violate"]).decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGSYS }),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn an_output_flood_is_cut_at_the_cap() {
let run = with_hook(&["--flood"]).decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::OutputOverflow),
"{:?}",
run.outcome
);
}
#[test]
fn the_address_space_limit_aborts_a_huge_allocation() {
let run = with_hook(&["--alloc-gib", "2"]).decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGABRT }),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn the_deadline_kills_a_spinning_child() {
let mut spawner = with_hook(&["--spin"]);
spawner.deadline = Duration::from_secs(2);
let run = spawner.decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::Timeout),
"{:?}",
run.outcome
);
}
#[test]
fn the_cpu_limit_stops_a_spinning_child_before_the_deadline() {
let mut spawner = with_hook(&["--spin"]);
spawner.deadline = Duration::from_secs(limits::CHILD_CPU_SECONDS + 10);
let run = spawner.decode(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGXCPU }),
"{:?}",
run.outcome
);
}
#[test]
fn the_linked_libwebp_is_recent_enough() {
assert!(faunosteo_worker::decode::libwebp_version() >= limits::MIN_LIBWEBP_VERSION);
}
// V1-2.3: the `verify` child under the same sandbox (contract W2, W3).
fn verifier(flags: &[&str]) -> Spawner {
let mut spawner = Spawner::for_verify(env!("CARGO_BIN_EXE_faunosteo-worker"));
spawner.extra_args = flags.iter().map(|flag| (*flag).to_owned()).collect();
spawner
}
fn expect_verdict(frame: &[u8]) -> serde_json::Value {
let run = verifier(&[]).run(frame);
match run.outcome {
ChildOutcome::Produced(out) => {
assert!(out.len() < limits::VERIFY_MAX_OUTPUT_BYTES);
serde_json::from_slice(&out).unwrap()
}
other => panic!("{other:?} (stderr: {})", run.stderr),
}
}
#[test]
fn a_clean_sheet_is_verified_passed_through_the_full_sandbox() {
let verdict = expect_verdict(&fixtures::sheet_frame(&fixtures::sheet_document()));
assert_eq!(verdict["outcome"], "passed", "{verdict}");
assert_eq!(verdict["review_id"], fixtures::sheet_binding().review_id);
}
#[test]
fn a_sheet_at_every_cap_is_verified_within_the_limits() {
let frame = fixtures::sheet_frame(&fixtures::sheet_document_at_caps());
let started = std::time::Instant::now();
let verdict = expect_verdict(&frame);
let elapsed = started.elapsed();
assert_eq!(verdict["outcome"], "passed", "{}", verdict["failures"]);
assert!(elapsed < limits::VERIFY_DEADLINE, "{elapsed:?}");
eprintln!(
"verify at caps: input {} bytes, verdict {} bytes, {elapsed:?}",
frame.len(),
verdict.to_string().len()
);
}
/// The core's hostile SVG corpus as the drawing of a submitted sheet: each one is judged by
/// the core inside the full sandbox (an integrity failure), never a crash or a signal.
#[test]
fn the_hostile_svg_corpus_fails_verification_inside_the_sandbox() {
let dir = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../core/tests/fixtures/svg/hostile"
);
let mut seen = 0;
for entry in std::fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
let drawing = std::fs::read(&path).unwrap();
let verdict = expect_verdict(&fixtures::sheet_frame_on(
&fixtures::sheet_document(),
&drawing,
));
let code = verdict["failure_code"].as_str().unwrap_or_default();
assert_eq!(verdict["outcome"], "failed", "{}", path.display());
assert!(
code.starts_with("verification.integrity.svg_"),
"{}: {code}",
path.display()
);
seen += 1;
}
assert!(seen >= 40, "hostile corpus read: {seen} files");
}
#[test]
fn the_verify_child_refuses_a_non_empty_environment() {
let status = Command::new(env!("CARGO_BIN_EXE_faunosteo-worker"))
.arg("verify")
.env_clear()
.env("LEAKED", "1")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
assert_eq!(status.code(), Some(exit_codes::ENV_NOT_EMPTY));
}
#[test]
fn an_input_over_the_verify_cap_is_refused_unread() {
let run = verifier(&[]).run(&vec![0u8; limits::VERIFY_MAX_INPUT_BYTES + 1]);
assert!(
matches!(run.outcome, ChildOutcome::Rejected { code, .. } if code == exit_codes::INPUT_TOO_LARGE),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn a_malformed_frame_is_a_broken_parent_not_content() {
let run = verifier(&[]).run(b"\x03\0\0\0{x}");
assert!(
matches!(run.outcome, ChildOutcome::Broken { code, .. } if code == exit_codes::FRAME_INVALID),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn a_denied_syscall_kills_the_verify_child_with_sigsys() {
let run = verifier(&["--violate"]).run(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGSYS }),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn a_verify_output_over_its_cap_is_cut() {
let mut spawner = verifier(&["--flood"]);
spawner.max_output = 64 * 1024;
let run = spawner.run(b"");
assert!(
matches!(run.outcome, ChildOutcome::OutputOverflow),
"{:?}",
run.outcome
);
}
// V1-2.3 reserve Q4: the `brk` the shared filter allows stays bounded in the verify child,
// under its own limits (1 GiB of address space).
#[test]
fn the_verify_child_aborts_on_a_huge_allocation() {
let run = verifier(&["--alloc-gib", "2"]).run(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGABRT }),
"{:?} {}",
run.outcome,
run.stderr
);
}
/// 2 GiB in 4 KiB chunks kept alive: the heap grows by `brk` first, then by `mmap`, and the
/// address-space limit stops it; the survival message of the hook never appears.
#[test]
fn the_verify_child_aborts_on_small_chunks_growth() {
let run = verifier(&["--alloc-small-gib", "2"]).run(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGABRT }),
"{:?} {}",
run.outcome,
run.stderr
);
assert!(!run.stderr.contains("kept"), "{}", run.stderr);
}
#[test]
fn an_executable_mapping_is_killed_in_the_verify_child() {
let run = verifier(&["--mmap-exec"]).run(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGSYS }),
"{:?} {}",
run.outcome,
run.stderr
);
}
#[test]
fn a_signal_to_the_parent_is_killed_in_the_verify_child() {
let run = verifier(&["--signal-parent"]).run(b"");
assert!(
matches!(run.outcome, ChildOutcome::Signalled { signal: SIGSYS }),
"{:?} {}",
run.outcome,
run.stderr
);
}
// V1-2.3 reserve Q3: inputs at and just beyond the caps, through the full sandbox.
#[test]
fn the_maximum_input_is_verified_within_the_limits() {
let frame = fixtures::verify_max_frame();
assert!(
frame.len() < limits::VERIFY_MAX_INPUT_BYTES,
"{}",
frame.len()
);
let started = std::time::Instant::now();
let run = verifier(&[]).run(&frame);
let elapsed = started.elapsed();
let ChildOutcome::Produced(out) = run.outcome else {
panic!("{:?} (stderr: {})", run.outcome, run.stderr);
};
assert!(out.len() < limits::VERIFY_MAX_OUTPUT_BYTES);
assert!(elapsed < limits::VERIFY_DEADLINE, "{elapsed:?}");
let verdict: serde_json::Value = serde_json::from_slice(&out).unwrap();
assert_eq!(verdict["outcome"], "passed", "{}", verdict["failures"]);
eprintln!(
"verify at the maximum: input {} bytes, verdict {} bytes, {elapsed:?}",
frame.len(),
out.len()
);
}
fn expect_caps_verdict(frame: &[u8]) {
let verdict = expect_verdict(frame);
assert_eq!(verdict["outcome"], "failed", "{}", verdict["failures"]);
assert_eq!(verdict["failure_code"], "verification.validation.caps");
}
#[test]
fn inputs_just_beyond_the_caps_fail_as_caps_through_the_sandbox() {
for frame in [
fixtures::beyond_total_frame(),
fixtures::beyond_count_frame(),
fixtures::beyond_drawing_frame(),
fixtures::beyond_document_frame(),
] {
expect_caps_verdict(&frame);
}
}
/// A frame of exactly the cap is read (here a malformed one: the frame check, not the size
/// check, refuses it), one more byte is refused unread.
#[test]
fn the_verify_input_cap_is_exact() {
let at_cap = verifier(&[]).run(&vec![0u8; limits::VERIFY_MAX_INPUT_BYTES]);
assert!(
matches!(at_cap.outcome, ChildOutcome::Broken { code, .. } if code == exit_codes::FRAME_INVALID),
"{:?} {}",
at_cap.outcome,
at_cap.stderr
);
}