Files

213 lines
6.0 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);
}