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.
This commit is contained in:
bchanot committed 2026-11-01 21:07:23 +01:00
1 parent a6de7d6b94
commit e967ce6736
5 files changed
+385 -3

No files matched your search

+4 -3
View File
@@ -3,8 +3,9 @@
# checks it against `sandbox::ALLOWED_SYSCALLS` (review Part IV.1: "derived by strace over
# the fixture corpus at the caps on the production image, not hand-written"). Runs in the
# Alpine `test` stage of crates/worker/Dockerfile, where the musl binary that ships is built.
# The verify child (V1-2.3) shares the filter: its framed fixtures (a small sheet and the
# sheet at every cap) are traced the same way.
# The verify child (V1-2.3) shares the filter: its framed fixtures (a small sheet, the
# sheet at every cap and the input at the maximum: 2 MiB document and base, four drawings)
# are traced the same way.
#
# Exit 1 when a traced syscall is missing from the allowlist (the sandbox would kill a
# legitimate decode or verification). Syscalls the list allows but the trace never shows are
@@ -31,7 +32,7 @@ trace() {
for kind in png jpeg webp big-jpeg big-png; do
trace decode "$kind"
done
for kind in verify-clean verify-caps; do
for kind in verify-clean verify-caps verify-max; do
trace verify "$kind"
done
sort -u "$work/traced.raw" > "$work/traced.txt"
+20
View File
@@ -210,6 +210,9 @@ enum HookAction {
Flood,
/// Allocates the given number of GiB: `RLIMIT_AS` must abort the child.
AllocGib(usize),
/// Grows the heap by the given GiB in 4 KiB chunks kept alive (the small-allocation path,
/// where musl extends the heap with `brk`): `RLIMIT_AS` must abort the child as well.
AllocSmallGib(usize),
/// Burns CPU forever: `RLIMIT_CPU` (SIGXCPU) or the parent's deadline must stop it.
Spin,
/// Sends SIGTERM to the parent through `tkill`: the argument filter must kill the child.
@@ -246,6 +249,13 @@ impl TestHooks {
.ok_or("--alloc-gib <n>")?;
hooks.action = Some(HookAction::AllocGib(value));
}
"--alloc-small-gib" => {
let value = iter
.next()
.and_then(|v| v.parse().ok())
.ok_or("--alloc-small-gib <n>")?;
hooks.action = Some(HookAction::AllocSmallGib(value));
}
other => return Err(format!("unknown flag {other}")),
}
}
@@ -296,6 +306,16 @@ impl TestHooks {
);
Some(exit_codes::INTERNAL)
}
HookAction::AllocSmallGib(gib) => {
let chunks = gib.saturating_mul(1024 * 1024 * 1024) / 4096;
let kept: Vec<Vec<u8>> = (0..chunks).map(|_| vec![1u8; 4096]).collect();
let _ = writeln!(
stderr,
"decode: kept {} chunks",
std::hint::black_box(&kept).len()
);
Some(exit_codes::INTERNAL)
}
HookAction::Spin => loop {
std::hint::black_box(0u8);
},
+217
View File
@@ -223,6 +223,148 @@ pub fn sheet_frame_on(document: &Value, drawing: &[u8]) -> Vec<u8> {
verify_frame::encode(&input).unwrap_or_default()
}
/// The server cap on a document text and on one drawing (2 MiB each, contract §12.2).
pub const DOCUMENT_CAP_BYTES: usize = 2 * 1024 * 1024;
const MIB: usize = 1024 * 1024;
/// Elements of the filler of a large drawing (the root and the closing path come on top,
/// well under the reader's 20 000).
const FILLER_PATHS: usize = 10_000;
/// `octet_length(value::jsonb::text)`'s spelling of `value`: `": "` after a key, `", "`
/// between members and elements (`core::domain::sheet::size`). Exact for the fixtures, whose
/// strings carry no control character.
pub fn pg_text(value: &Value) -> String {
match value {
Value::Array(items) => {
let parts: Vec<String> = items.iter().map(pg_text).collect();
format!("[{}]", parts.join(", "))
}
Value::Object(map) => {
let parts: Vec<String> = map
.iter()
.map(|(key, item)| format!("{}: {}", Value::from(key.as_str()), pg_text(item)))
.collect();
format!("{{{}}}", parts.join(", "))
}
other => other.to_string(),
}
}
/// The asset id of the `index`-th drawing of the large fixtures.
pub fn sheet_asset_id(index: usize) -> String {
format!("11111111-2222-4333-8444-{index:012}")
}
/// A document whose main view and `assets - 1` detail views each draw on their own asset
/// (`sheet_asset_id(0..assets)`), as the `pg_text` of exactly `target_bytes` bytes: the
/// attribution notes are the filler. A target below the unpadded size (0 for none) gives the
/// unpadded document.
pub fn sheet_document_text(assets: usize, target_bytes: usize) -> String {
let mut doc = sheet_document();
let mut views = vec![
json!({ "id": "main", "kind": "main", "name": name("V"), "svg_asset_id": sheet_asset_id(0), "view_box": null }),
];
let mut annotations = Vec::new();
for i in 1..assets {
views.push(
json!({ "id": format!("d{i}"), "kind": "detail", "name": name("D"),
"svg_asset_id": sheet_asset_id(i), "view_box": null, "parent_view_id": "main" }),
);
annotations.push(json!({ "id": format!("t{i}"), "kind": "detail_trigger", "view_id": "main", "category_id": "c",
"laterality": "unpaired", "geometry": { "type": "polygon", "points": [[0, 0], [1, 0], [1, 1]] },
"review_status": "reviewed", "name": null, "opens_view_id": format!("d{i}") }));
}
if let Some(list) = doc["annotations"].as_array_mut() {
list.extend(annotations);
}
doc["views"] = Value::Array(views);
doc["attribution"]["notes"] = Value::from("n");
let unpadded = pg_text(&doc).len();
let pad = target_bytes.saturating_sub(unpadded);
doc["attribution"]["notes"] = Value::from("n".repeat(1 + pad));
pg_text(&doc)
}
/// A drawing of exactly `size` bytes in its stored form: the prolog, a 10 × 10 root, 10 000
/// `<path d="M0 0L5 5"/>` and a last path whose `d` ends in as many spaces as `size` needs.
/// Below its fixed part (about 210 KB) it returns the unpadded drawing.
pub fn drawing_of_size(size: usize) -> Vec<u8> {
let head = concat!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">"#
);
let path = r#"<path d="M0 0L5 5"/>"#;
let tail = "</svg>";
let fixed = head.len() + (FILLER_PATHS + 1) * path.len() + tail.len();
let mut out = String::with_capacity(size.max(fixed));
out.push_str(head);
out.push_str(&path.repeat(FILLER_PATHS));
out.push_str(&format!(
r#"<path d="M0 0L5 5{}"/>"#,
" ".repeat(size.saturating_sub(fixed))
));
out.push_str(tail);
out.into_bytes()
}
/// A framed input on the large fixtures: `document` (and `base`) as given, the drawings on
/// `sheet_asset_id(0..)` in order, each row matching its bytes.
pub fn large_frame(document: &str, base: Option<&str>, drawings: &[Vec<u8>]) -> Vec<u8> {
let ids: Vec<String> = (0..drawings.len()).map(sheet_asset_id).collect();
let hashes: Vec<String> = drawings.iter().map(|d| sha256_hex(d)).collect();
let rows = drawings
.iter()
.enumerate()
.map(|(i, bytes)| VerdictDrawing {
asset_id: &ids[i],
sha256: &hashes[i],
byte_size: bytes.len() as u64,
bytes: Some(bytes),
})
.collect();
let input = VerdictInput {
binding: sheet_binding(),
document,
base_document: base,
drawings: rows,
};
verify_frame::encode(&input).unwrap_or_default()
}
/// The input at the maximum the policy allows: document and base of 2 097 152 bytes, four
/// drawings of 2 MiB, 2 MiB, 1 MiB and 1 MiB (6 291 456 in all).
pub fn verify_max_frame() -> Vec<u8> {
let document = sheet_document_text(4, DOCUMENT_CAP_BYTES);
let sizes = [2 * MIB, 2 * MIB, MIB, MIB];
let drawings: Vec<Vec<u8>> = sizes.into_iter().map(drawing_of_size).collect();
large_frame(&document, Some(&document), &drawings)
}
/// Four drawings of 2 MiB (8 MiB, over the 6 MiB total), no base: the frame stays under the
/// child's input cap.
pub fn beyond_total_frame() -> Vec<u8> {
let drawings: Vec<Vec<u8>> = (0..4).map(|_| drawing_of_size(2 * MIB)).collect();
large_frame(&sheet_document_text(4, 0), None, &drawings)
}
/// Five small drawings, one over the count cap.
pub fn beyond_count_frame() -> Vec<u8> {
let drawings: Vec<Vec<u8>> = (0..5).map(|_| SHEET_DRAWING.as_bytes().to_vec()).collect();
large_frame(&sheet_document_text(5, 0), None, &drawings)
}
/// One drawing of 2 097 153 bytes.
pub fn beyond_drawing_frame() -> Vec<u8> {
let drawings = [drawing_of_size(2 * MIB + 1)];
large_frame(&sheet_document_text(1, 0), None, &drawings)
}
/// A document of 2 097 153 bytes.
pub fn beyond_document_frame() -> Vec<u8> {
let document = sheet_document_text(1, DOCUMENT_CAP_BYTES + 1);
large_frame(&document, None, &[SHEET_DRAWING.as_bytes().to_vec()])
}
/// Writes one named fixture on stdout (`faunosteo-worker fixture <name>`).
pub fn by_name(name: &str) -> Option<Vec<u8>> {
Some(match name {
@@ -240,6 +382,81 @@ pub fn by_name(name: &str) -> Option<Vec<u8>> {
"truncated" => truncated_png(),
"verify-clean" => sheet_frame(&sheet_document()),
"verify-caps" => sheet_frame(&sheet_document_at_caps()),
"verify-max" => verify_max_frame(),
_ => return None,
})
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
use crate::verify;
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::domain::sheet::size::document_text_bytes;
fn verdict_of(frame: &[u8]) -> Value {
serde_json::from_slice(&verify::process(frame).unwrap()).unwrap()
}
fn assert_caps_failure(frame: &[u8], cap: &str) {
let verdict = verdict_of(frame);
assert_eq!(verdict["outcome"], "failed", "{}", verdict["failures"]);
assert_eq!(verdict["failure_code"], "verification.validation.caps");
let exceeded = verdict["caps"]["exceeded"].to_string();
assert!(exceeded.contains(cap), "{exceeded}");
}
#[test]
fn the_padded_document_measures_as_the_server_does() {
let text = sheet_document_text(4, DOCUMENT_CAP_BYTES);
assert_eq!(text.len(), DOCUMENT_CAP_BYTES);
let value = parse_json(&text).unwrap();
assert_eq!(document_text_bytes(&value), DOCUMENT_CAP_BYTES as u64);
}
#[test]
fn the_large_drawings_have_their_exact_size() {
for size in [2 * MIB, 2 * MIB + 1, MIB] {
assert_eq!(drawing_of_size(size).len(), size);
}
}
#[test]
fn the_maximum_input_passes_with_its_exact_sizes() {
let frame = verify_max_frame();
let decoded = verify_frame::decode(&frame).unwrap();
assert_eq!(decoded.document.len(), 2_097_152);
assert_eq!(decoded.base.map(str::len), Some(2_097_152));
let sizes: Vec<usize> = decoded
.drawing_bytes
.iter()
.map(|b| b.unwrap().len())
.collect();
assert_eq!(sizes, [2_097_152, 2_097_152, 1_048_576, 1_048_576]);
assert_eq!(sizes.iter().sum::<usize>(), 6_291_456);
let verdict = verdict_of(&frame);
assert_eq!(verdict["outcome"], "passed", "{}", verdict["failures"]);
assert_eq!(verdict["document_bytes"], 2_097_152);
}
#[test]
fn four_drawings_over_the_total_fail_as_caps() {
assert_caps_failure(&beyond_total_frame(), "drawings_total_bytes");
}
#[test]
fn five_drawings_fail_as_caps() {
assert_caps_failure(&beyond_count_frame(), "drawings");
}
#[test]
fn a_drawing_one_byte_over_fails_as_caps() {
assert_caps_failure(&beyond_drawing_frame(), "drawing_bytes");
}
#[test]
fn a_document_one_byte_over_fails_as_caps() {
assert_caps_failure(&beyond_document_frame(), "document_bytes");
}
}
+108
View File
@@ -338,3 +338,111 @@ fn a_verify_output_over_its_cap_is_cut() {
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
);
}
+36
View File
@@ -344,6 +344,42 @@ fn rows_outside_the_policy_are_never_fetched() {
}
}
// Tracked misclassification (owner brief 2026-11-01), not fixed: an object larger than the
// policy is passed without bytes, so the core judges it missing (`drawing_missing`, listed
// after `caps`: the row's declared size already exceeds the cap) where `size_mismatch` would
// name it.
#[test]
fn tracked_an_object_over_the_policy_is_reported_drawing_missing() {
let mut too_big = row();
too_big.byte_size = VERIFY_MAX_DRAWING_BYTES as u64 + 1;
let fake = Fake::with_input(input(document().to_string(), vec![too_big]));
assert_eq!(process(&fake, &InProcess), VerifyEnd::Failed);
assert_eq!(fake.names(), ["input", "complete"]);
let verdict = fake.verdict();
assert_eq!(
verdict["failures"],
json!([verdict_codes::CAPS, verdict_codes::DRAWING_MISSING])
);
assert_eq!(verdict["drawings"][0]["result"], "missing");
}
// Tracked misclassification (owner brief 2026-11-01), not fixed: a document naming enough
// drawings that the frame header passes 64 KiB is refused by the parent's own framing and
// reported `resource_limit` (a worker failure, retried) where `caps` would name it.
#[test]
fn tracked_a_header_over_64_kib_is_reported_resource_limit() {
let rows = vec![row(); 600];
let fake = Fake::with_input(input(document().to_string(), rows));
assert_eq!(
process(&fake, &Fixed::never()),
VerifyEnd::Reported(codes::RESOURCE_LIMIT)
);
assert_eq!(
fake.calls().last().unwrap(),
&fail_call(codes::RESOURCE_LIMIT)
);
}
#[test]
fn downloads_stop_at_the_drawing_count() {
let rows = vec![row(); 6];