feat(worker): sandboxed decode child skeleton — rlimits, traced seccomp allowlist, pure-Rust decoders, libwebp in the child, parent RIFF check, musl image with test stage

This commit is contained in:
bchanot committed 2026-10-13 00:01:26 +02:00
1 parent 2a38fa37b5
commit 3970cdca52
13 files changed
+1435

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
[package]
name = "faunosteo-worker"
description = "Faunosteo file-processing worker: job loop (R1) and the sandboxed decode child (R0 proof)"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
publish.workspace = true
[lib]
path = "src/lib.rs"
[[bin]]
name = "faunosteo-worker"
path = "src/main.rs"
[features]
default = []
# Extra subcommands and flags for the sandbox tests and the seccomp derivation
# (crates/worker/Dockerfile stage `test`); never part of a release build.
test-hooks = ["dep:crc32fast"]
[dependencies]
image.workspace = true
libwebp-sys.workspace = true
seccompiler.workspace = true
rlimit.workspace = true
libc.workspace = true
crc32fast = { version = "1.4", optional = true }
# The worker owns I/O (it is the process boundary), so the core's disallowed_methods lint is
# off here; unsafe is allowed for the libwebp FFI only, each block carries a SAFETY comment.
[lints.rust]
unsafe_code = "allow"
[lints.clippy]
unwrap_used = "deny"
expect_used = "deny"
disallowed_methods = "allow"
+37
View File
@@ -0,0 +1,37 @@
# syntax=docker/dockerfile:1.7
# Faunosteo worker image (review Part IV, VIII.1). Build context = repository root:
# docker build -f crates/worker/Dockerfile --target test . # sandbox tests + seccomp derivation
# docker build -f crates/worker/Dockerfile -t faunosteo_worker:dev . # runtime image
# The binary is a static musl executable; the runtime image carries nothing else.
FROM rust:1.99.0-alpine3.24 AS base
# build-base: the C compiler for libwebp (libwebp-sys, D-R4), nothing else is compiled from C.
# rustfmt/clippy: listed in rust-toolchain.toml, absent from the image's minimal profile.
RUN apk add --no-cache build-base && rustup component add rustfmt clippy
WORKDIR /w
COPY Cargo.toml Cargo.lock rust-toolchain.toml rustfmt.toml clippy.toml ./
COPY .cargo .cargo
COPY crates crates
FROM base AS build
RUN --mount=type=cache,id=faunosteo_cargo_registry_musl,target=/usr/local/cargo/registry \
--mount=type=cache,id=faunosteo_target_musl,target=/w/target \
cargo build -p faunosteo-worker --release --locked \
&& cp target/release/faunosteo-worker /faunosteo-worker
# The tests run against the musl binary that ships, inside the default Docker seccomp
# profile (the inner filter is allowed by it). strace serves the allowlist derivation.
FROM base AS test
RUN apk add --no-cache strace
RUN --mount=type=cache,id=faunosteo_cargo_registry_musl,target=/usr/local/cargo/registry \
--mount=type=cache,id=faunosteo_target_musl,target=/w/target \
cargo test -p faunosteo-worker --features test-hooks --locked -- --test-threads=4
RUN --mount=type=cache,id=faunosteo_cargo_registry_musl,target=/usr/local/cargo/registry \
--mount=type=cache,id=faunosteo_target_musl,target=/w/target \
sh crates/worker/scripts/derive-seccomp.sh
FROM gcr.io/distroless/static-debian12:nonroot AS runtime
COPY --from=build /faunosteo-worker /faunosteo-worker
USER nonroot
ENTRYPOINT ["/faunosteo-worker"]
CMD ["run", "--check"]
+41
View File
@@ -0,0 +1,41 @@
#!/bin/sh
# Derives the seccomp allowlist of the decode child from a real trace and 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.
#
# Exit 1 when a traced syscall is missing from the allowlist (the sandbox would kill a
# legitimate decode). Syscalls the list allows but the trace never shows are printed for
# review (the abort path is expected there).
set -eu
bin=target/debug/faunosteo-worker
cargo build -p faunosteo-worker --features test-hooks --locked
work=$(mktemp -d)
fixtures="png jpeg webp big-jpeg big-png"
: > "$work/traced.raw"
for kind in $fixtures; do
"$bin" fixture "$kind" > "$work/$kind.bin"
# `env -i`: the child refuses a non-empty environment; `--no-seccomp` keeps the rlimits
# and prints the marker where the filter would be installed.
env -i strace -f -qq -o "$work/trace.$kind" "$bin" decode --no-seccomp \
< "$work/$kind.bin" > "$work/$kind.webp"
awk 'found { print } /SECCOMP-MARKER/ { found = 1 }' "$work/trace.$kind" \
| sed -E 's/^[0-9]+ +//; /^(\+\+\+|---)/d; s/^<\.\.\. ([a-z0-9_]+) resumed>.*/\1/; s/\(.*//' \
| grep -E '^[a-z0-9_]+$' >> "$work/traced.raw" || true
done
sort -u "$work/traced.raw" > "$work/traced.txt"
"$bin" print-allowlist | sort -u > "$work/allowed.txt"
echo "== syscalls traced after the sandbox marker"
cat "$work/traced.txt"
missing=$(comm -23 "$work/traced.txt" "$work/allowed.txt")
extra=$(comm -13 "$work/traced.txt" "$work/allowed.txt")
echo "== allowed but not traced (abort path expected): $(echo "$extra" | tr '\n' ' ')"
if [ -n "$missing" ]; then
echo "== TRACED BUT NOT ALLOWED (the filter would kill a legitimate decode):"
echo "$missing"
exit 1
fi
echo "== allowlist covers the trace"
+286
View File
@@ -0,0 +1,286 @@
//! The decode child: bytes in, one 512 × 512 lossy WebP out (review Part IV.1). R0 ships
//! the pipeline as the sandbox, musl and libwebp proof; the job semantics arrive with R1.
use std::ffi::c_int;
use std::io::{self, Cursor, Read, Write};
use image::error::LimitErrorKind;
use image::imageops::FilterType;
use image::metadata::Orientation;
use image::{DynamicImage, ImageDecoder, ImageError, ImageFormat, ImageReader, Limits};
use crate::exit_codes;
use crate::limits::{
MAX_ALLOC_BYTES, MAX_INPUT_BYTES, MAX_SIDE, MIN_SIDE, OUTPUT_SIDE, WEBP_QUALITY,
};
use crate::sandbox;
/// Why the child refused an input (one exit code each).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reject {
UnsupportedType,
DecodeFailed,
Dimensions,
EncodeFailed,
Internal,
}
impl Reject {
pub fn exit_code(self) -> i32 {
match self {
Self::UnsupportedType => exit_codes::UNSUPPORTED_TYPE,
Self::DecodeFailed => exit_codes::DECODE_FAILED,
Self::Dimensions => exit_codes::DIMENSIONS,
Self::EncodeFailed => exit_codes::ENCODE_FAILED,
Self::Internal => exit_codes::INTERNAL,
}
}
}
/// Entry of `faunosteo-worker decode`: returns the process exit code.
///
/// Order matters: the environment check and the sandbox come before the first byte of
/// input is read, and no thread exists at that point (`main` is a plain function).
pub fn main(
args: &[String],
stdin: &mut dyn Read,
stdout: &mut dyn Write,
stderr: &mut dyn Write,
) -> i32 {
if std::env::vars_os().next().is_some() {
let _ = writeln!(stderr, "decode: the environment must be empty");
return exit_codes::ENV_NOT_EMPTY;
}
let hooks = match TestHooks::parse(args) {
Ok(hooks) => hooks,
Err(message) => {
let _ = writeln!(stderr, "decode: {message}");
return exit_codes::USAGE;
}
};
if let Err(error) = sandbox::apply(&sandbox::Limits::default(), hooks.seccomp) {
let _ = writeln!(stderr, "decode: sandbox: {error}");
return exit_codes::SANDBOX_FAILED;
}
if let Some(code) = hooks.run_after_sandbox(stdout, stderr) {
return code;
}
let mut input = Vec::new();
let read = stdin
.take(MAX_INPUT_BYTES as u64 + 1)
.read_to_end(&mut input);
if read.is_err() {
return exit_codes::INTERNAL;
}
if input.len() > MAX_INPUT_BYTES {
let _ = writeln!(stderr, "decode: input over {MAX_INPUT_BYTES} bytes");
return exit_codes::INPUT_TOO_LARGE;
}
match process(&input) {
Ok(webp) => match stdout.write_all(&webp).and_then(|()| stdout.flush()) {
Ok(()) => exit_codes::OK,
Err(_) => exit_codes::OUTPUT_FAILED,
},
Err(reject) => {
let _ = writeln!(stderr, "decode: {}", exit_codes::name(reject.exit_code()));
reject.exit_code()
}
}
}
/// Decodes, orients, crops to a square and encodes one image; pure function of the bytes.
pub fn process(bytes: &[u8]) -> Result<Vec<u8>, Reject> {
let format = image::guess_format(bytes).map_err(|_| Reject::UnsupportedType)?;
if !matches!(
format,
ImageFormat::Jpeg | ImageFormat::Png | ImageFormat::WebP
) {
return Err(Reject::UnsupportedType);
}
let mut reader = ImageReader::with_format(Cursor::new(bytes), format);
let mut limits = Limits::default();
limits.max_image_width = Some(MAX_SIDE);
limits.max_image_height = Some(MAX_SIDE);
limits.max_alloc = Some(MAX_ALLOC_BYTES);
reader.limits(limits);
let mut decoder = reader.into_decoder().map_err(map_image_error)?;
let (width, height) = decoder.dimensions();
if width < MIN_SIDE || height < MIN_SIDE {
return Err(Reject::Dimensions);
}
let orientation = decoder.orientation().unwrap_or(Orientation::NoTransforms);
let mut image = DynamicImage::from_decoder(decoder).map_err(map_image_error)?;
image.apply_orientation(orientation);
let square = image
.resize_to_fill(OUTPUT_SIDE, OUTPUT_SIDE, FilterType::Lanczos3)
.into_rgba8();
encode_webp(square.as_raw(), OUTPUT_SIDE, OUTPUT_SIDE, WEBP_QUALITY)
}
fn map_image_error(error: ImageError) -> Reject {
match error {
ImageError::Limits(limit) => match limit.kind() {
LimitErrorKind::DimensionError => Reject::Dimensions,
_ => Reject::Internal,
},
ImageError::Unsupported(_) => Reject::UnsupportedType,
ImageError::Decoding(_) | ImageError::IoError(_) => Reject::DecodeFailed,
_ => Reject::Internal,
}
}
/// Encodes an RGBA buffer as lossy WebP through libwebp (D-R4: C confined to this process).
pub fn encode_webp(rgba: &[u8], width: u32, height: u32, quality: f32) -> Result<Vec<u8>, Reject> {
let expected = (width as usize)
.checked_mul(height as usize)
.and_then(|n| n.checked_mul(4));
if expected != Some(rgba.len()) {
return Err(Reject::Internal);
}
let (Ok(w), Ok(h), Ok(stride)) = (
c_int::try_from(width),
c_int::try_from(height),
c_int::try_from(width * 4),
) else {
return Err(Reject::Internal);
};
let mut output: *mut u8 = std::ptr::null_mut();
// SAFETY: `rgba` holds exactly width × height × 4 bytes (checked above) with the matching
// stride; libwebp allocates the encoded buffer, writes its address into `output` and
// returns its size. The buffer is copied once and released with the matching WebPFree.
let size =
unsafe { libwebp_sys::WebPEncodeRGBA(rgba.as_ptr(), w, h, stride, quality, &mut output) };
if size == 0 || output.is_null() {
return Err(Reject::EncodeFailed);
}
// SAFETY: libwebp returned `size` initialised bytes at `output`.
let encoded = unsafe { std::slice::from_raw_parts(output, size) }.to_vec();
// SAFETY: `output` came from libwebp's allocator and is not read afterwards.
unsafe { libwebp_sys::WebPFree(output.cast()) };
Ok(encoded)
}
/// Version of the libwebp linked into this binary, as (major, minor, revision).
pub fn libwebp_version() -> (u8, u8, u8) {
// SAFETY: a pure getter of the FFI without arguments or side effects.
let packed = unsafe { libwebp_sys::WebPGetEncoderVersion() };
let packed = u32::try_from(packed).unwrap_or(0);
(
u8::try_from((packed >> 16) & 0xff).unwrap_or(0),
u8::try_from((packed >> 8) & 0xff).unwrap_or(0),
u8::try_from(packed & 0xff).unwrap_or(0),
)
}
/// Flags of the test build (`--features test-hooks`); a release build accepts no argument.
#[derive(Debug, Default)]
pub struct TestHooks {
pub seccomp: bool,
#[cfg(feature = "test-hooks")]
action: Option<HookAction>,
}
#[cfg(feature = "test-hooks")]
#[derive(Debug, Clone, Copy)]
enum HookAction {
/// Makes a denied syscall (`getcwd`) after the filter: the child must die with SIGSYS.
Violate,
/// Writes a megabyte on stdout: the parent must cut the child at its output cap.
Flood,
/// Allocates the given number of GiB: `RLIMIT_AS` must abort the child.
AllocGib(usize),
/// Burns CPU forever: `RLIMIT_CPU` (SIGXCPU) or the parent's deadline must stop it.
Spin,
}
impl TestHooks {
fn parse(args: &[String]) -> Result<Self, String> {
#[cfg(not(feature = "test-hooks"))]
let hooks = Self { seccomp: true };
#[cfg(feature = "test-hooks")]
let mut hooks = Self {
seccomp: true,
action: None,
};
#[cfg(feature = "test-hooks")]
{
let mut iter = args.iter();
while let Some(arg) = iter.next() {
match arg.as_str() {
"--no-seccomp" => hooks.seccomp = false,
"--violate" => hooks.action = Some(HookAction::Violate),
"--flood" => hooks.action = Some(HookAction::Flood),
"--spin" => hooks.action = Some(HookAction::Spin),
"--alloc-gib" => {
let value = iter
.next()
.and_then(|v| v.parse().ok())
.ok_or("--alloc-gib <n>")?;
hooks.action = Some(HookAction::AllocGib(value));
}
other => return Err(format!("unknown flag {other}")),
}
}
}
#[cfg(not(feature = "test-hooks"))]
if let Some(arg) = args.first() {
return Err(format!("unexpected argument {arg}"));
}
Ok(hooks)
}
#[cfg(feature = "test-hooks")]
fn run_after_sandbox(&self, stdout: &mut dyn Write, stderr: &mut dyn Write) -> Option<i32> {
if !self.seccomp {
// Marker for crates/worker/scripts/derive-seccomp.sh: syscalls after this line
// are the ones the filter must allow.
let _ = stderr.write_all(b"SECCOMP-MARKER\n");
}
match self.action? {
HookAction::Violate => {
let cwd = std::env::current_dir();
let _ = writeln!(stderr, "decode: getcwd survived the filter: {cwd:?}");
Some(exit_codes::INTERNAL)
}
HookAction::Flood => {
let chunk = vec![0u8; 64 * 1024];
for _ in 0..16 {
if stdout.write_all(&chunk).is_err() {
return Some(exit_codes::OUTPUT_FAILED);
}
}
Some(exit_codes::OK)
}
HookAction::AllocGib(gib) => {
let bytes = gib.saturating_mul(1024 * 1024 * 1024);
let buffer = vec![1u8; bytes];
let _ = writeln!(
stderr,
"decode: allocated {} bytes",
std::hint::black_box(&buffer).len()
);
Some(exit_codes::INTERNAL)
}
HookAction::Spin => loop {
std::hint::black_box(0u8);
},
}
}
#[cfg(not(feature = "test-hooks"))]
fn run_after_sandbox(&self, _stdout: &mut dyn Write, _stderr: &mut dyn Write) -> Option<i32> {
None
}
}
/// Reads at most `cap` bytes from a reader, reporting whether more were available.
pub fn read_capped(reader: &mut dyn Read, cap: usize) -> io::Result<(Vec<u8>, bool)> {
let mut buffer = Vec::new();
reader.take(cap as u64 + 1).read_to_end(&mut buffer)?;
let overflow = buffer.len() > cap;
buffer.truncate(cap);
Ok((buffer, overflow))
}
+33
View File
@@ -0,0 +1,33 @@
//! Exit codes of the `decode` child, reserved away from 1 (generic failure), 101 (Rust
//! panic) and signal deaths (reported separately by the parent). The parent maps them to
//! the closed rejection reasons of STEP8_FILES_ANALYSIS II.7.
pub const OK: i32 = 0;
pub const USAGE: i32 = 2;
pub const ENV_NOT_EMPTY: i32 = 10;
pub const SANDBOX_FAILED: i32 = 11;
pub const INPUT_TOO_LARGE: i32 = 12;
pub const UNSUPPORTED_TYPE: i32 = 13;
pub const DECODE_FAILED: i32 = 14;
pub const DIMENSIONS: i32 = 15;
pub const ENCODE_FAILED: i32 = 16;
pub const OUTPUT_FAILED: i32 = 17;
pub const INTERNAL: i32 = 18;
/// Stable name of an exit code for logs and tests.
pub fn name(code: i32) -> &'static str {
match code {
OK => "ok",
USAGE => "usage",
ENV_NOT_EMPTY => "env_not_empty",
SANDBOX_FAILED => "sandbox_failed",
INPUT_TOO_LARGE => "input_too_large",
UNSUPPORTED_TYPE => "unsupported_type",
DECODE_FAILED => "decode_failed",
DIMENSIONS => "dimensions",
ENCODE_FAILED => "encode_failed",
OUTPUT_FAILED => "output_failed",
INTERNAL => "internal",
_ => "unknown",
}
}
+96
View File
@@ -0,0 +1,96 @@
//! Synthetic images for the sandbox tests and the seccomp derivation (`test-hooks` only):
//! nothing binary lives in the repository.
use std::io::Cursor;
use image::{DynamicImage, ImageBuffer, ImageFormat, Rgba};
fn gradient(width: u32, height: u32) -> DynamicImage {
let buffer = ImageBuffer::from_fn(width, height, |x, y| {
let r = u8::try_from(x * 255 / width.max(1)).unwrap_or(255);
let g = u8::try_from(y * 255 / height.max(1)).unwrap_or(255);
let b = u8::try_from((x / 8 + y / 8) % 256).unwrap_or(0);
Rgba([r, g, b, 255])
});
DynamicImage::ImageRgba8(buffer)
}
fn encode(image: &DynamicImage, format: ImageFormat) -> Vec<u8> {
let mut out = Cursor::new(Vec::new());
if image.write_to(&mut out, format).is_err() {
return Vec::new();
}
out.into_inner()
}
/// A PNG gradient.
pub fn png(width: u32, height: u32) -> Vec<u8> {
encode(&gradient(width, height), ImageFormat::Png)
}
/// A baseline JPEG gradient (no alpha channel).
pub fn jpeg(width: u32, height: u32) -> Vec<u8> {
let rgb = DynamicImage::ImageRgb8(gradient(width, height).into_rgb8());
encode(&rgb, ImageFormat::Jpeg)
}
/// A lossless WebP gradient (the only WebP encoder of the `image` crate).
pub fn webp(width: u32, height: u32) -> Vec<u8> {
encode(&gradient(width, height), ImageFormat::WebP)
}
/// A PNG whose header declares `width` × `height` with no pixel data: the decoder must
/// refuse it on dimensions before allocating anything.
pub fn png_header_only(width: u32, height: u32) -> Vec<u8> {
let mut out = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
let mut ihdr = Vec::new();
ihdr.extend_from_slice(&width.to_be_bytes());
ihdr.extend_from_slice(&height.to_be_bytes());
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
push_chunk(&mut out, b"IHDR", &ihdr);
push_chunk(&mut out, b"IEND", &[]);
out
}
fn push_chunk(out: &mut Vec<u8>, kind: &[u8; 4], payload: &[u8]) {
out.extend_from_slice(&(payload.len() as u32).to_be_bytes());
let mut hasher = crc32fast::Hasher::new();
hasher.update(kind);
hasher.update(payload);
out.extend_from_slice(kind);
out.extend_from_slice(payload);
out.extend_from_slice(&hasher.finalize().to_be_bytes());
}
/// The first bytes of a GIF: a format the avatar pipeline never accepts.
pub fn gif() -> Vec<u8> {
let mut out = b"GIF89a".to_vec();
out.extend_from_slice(&[64, 0, 64, 0, 0xf7, 0, 0]);
out.extend(std::iter::repeat_n(0u8, 800));
out
}
/// A valid PNG cut in half.
pub fn truncated_png() -> Vec<u8> {
let mut bytes = png(200, 200);
bytes.truncate(bytes.len() / 2);
bytes
}
/// Writes one named fixture on stdout (`faunosteo-worker fixture <name>`).
pub fn by_name(name: &str) -> Option<Vec<u8>> {
Some(match name {
"png" => png(800, 600),
"jpeg" => jpeg(640, 480),
"webp" => webp(300, 700),
"big-jpeg" => jpeg(4096, 4096),
// Same pixel budget as the JPEG but compressible (4096 × 2048 of this gradient
// weighed more than the 5 MiB input cap).
"big-png" => png(3000, 3000),
"tiny" => png(32, 32),
"huge-header" => png_header_only(20_000, 20_000),
"gif" => gif(),
"truncated" => truncated_png(),
_ => return None,
})
}
+15
View File
@@ -0,0 +1,15 @@
//! `faunosteo-worker` library: the pieces the binary, the tests and the R1 job loop share.
//!
//! Process model (review Part IV.1): a keyed parent spawns the same binary as `decode`, a
//! child with an empty environment that confines itself (rlimits + seccomp) before reading
//! one image on stdin and writing one 512 × 512 WebP on stdout. The parent never decodes:
//! it checks the container header of what comes back (`webp_check`).
pub mod decode;
pub mod exit_codes;
#[cfg(feature = "test-hooks")]
pub mod fixtures;
pub mod limits;
pub mod sandbox;
pub mod spawn;
pub mod webp_check;
+26
View File
@@ -0,0 +1,26 @@
//! Caps of the decode child (review Part IV.1 and STEP8_FILES_ANALYSIS II.5 step 6).
use std::time::Duration;
/// Largest accepted upload (STEP8 D4: 5 MiB at upload).
pub const MAX_INPUT_BYTES: usize = 5 * 1024 * 1024;
/// Largest output the parent reads before killing the child (a 512 × 512 WebP is ~30 KB).
pub const MAX_OUTPUT_BYTES: usize = 512 * 1024;
/// Smallest accepted side of the source image.
pub const MIN_SIDE: u32 = 64;
/// Largest accepted side of the source image (header check before decoding).
pub const MAX_SIDE: u32 = 4096;
/// Allocation budget handed to the decoders (`image::Limits::max_alloc`, advisory).
pub const MAX_ALLOC_BYTES: u64 = 256 * 1024 * 1024;
/// Side of the produced avatar.
pub const OUTPUT_SIDE: u32 = 512;
/// Lossy WebP quality of the produced avatar.
pub const WEBP_QUALITY: f32 = 82.0;
/// Hard address-space cap of the child (`RLIMIT_AS`): the real memory bound.
pub const CHILD_ADDRESS_SPACE_BYTES: u64 = 512 * 1024 * 1024;
/// CPU-time cap of the child (`RLIMIT_CPU`, seconds).
pub const CHILD_CPU_SECONDS: u64 = 10;
/// Wall-clock deadline after which the parent kills the child.
pub const CHILD_DEADLINE: Duration = Duration::from_secs(20);
/// Oldest libwebp the worker accepts to link (D-R4: the linked version is controlled).
pub const MIN_LIBWEBP_VERSION: (u8, u8, u8) = (1, 6, 0);
+113
View File
@@ -0,0 +1,113 @@
//! `faunosteo-worker` entry: a plain `main`, no async runtime, no thread, so the `decode`
//! child installs its sandbox in a single-threaded process (review Part IV.1).
//!
//! R0 subcommands: `decode` (the sandboxed child), `run --check` (prints the resolved
//! configuration, no job loop before R1), `version`.
use std::io::Write;
use faunosteo_worker::decode;
use faunosteo_worker::exit_codes;
use faunosteo_worker::limits::MIN_LIBWEBP_VERSION;
fn main() {
let args: Vec<String> = std::env::args().collect();
let code = match args.get(1).map(String::as_str) {
Some("decode") => {
let stdin = std::io::stdin();
let stdout = std::io::stdout();
let stderr = std::io::stderr();
decode::main(
&args[2..],
&mut stdin.lock(),
&mut stdout.lock(),
&mut stderr.lock(),
)
}
Some("run") => run(&args[2..]),
Some("version") => version(),
#[cfg(feature = "test-hooks")]
Some("fixture") => fixture(&args[2..]),
#[cfg(feature = "test-hooks")]
Some("print-allowlist") => {
for name in faunosteo_worker::sandbox::allowed_names() {
println!("{name}");
}
exit_codes::OK
}
_ => {
eprintln!("usage: faunosteo-worker <decode | run --check | version>");
exit_codes::USAGE
}
};
std::process::exit(code);
}
fn version() -> i32 {
let (major, minor, revision) = decode::libwebp_version();
println!(
"faunosteo-worker {} (core {}, libwebp {major}.{minor}.{revision})",
env!("CARGO_PKG_VERSION"),
faunosteo_core_version()
);
exit_codes::OK
}
fn faunosteo_core_version() -> &'static str {
// The worker links no core module before R1 (nothing to share yet); the version printed
// is the workspace version the two crates share.
env!("CARGO_PKG_VERSION")
}
/// `run --check`: prints what the worker would start with, without secrets. The job loop
/// arrives at R1; a plain `run` therefore refuses to pretend it works.
fn run(args: &[String]) -> i32 {
let (major, minor, revision) = decode::libwebp_version();
let libwebp_ok = (major, minor, revision) >= MIN_LIBWEBP_VERSION;
println!("faunosteo-worker run: R0 skeleton, the job loop arrives at R1");
println!(
"libwebp {major}.{minor}.{revision} ({})",
if libwebp_ok {
"accepted"
} else {
"below the minimum, refusing to start"
}
);
for name in ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY", "WORKER_ID"] {
let state = if std::env::var_os(name).is_some() {
"set"
} else {
"unset"
};
println!("{name}: {state}");
}
let _ = std::io::stdout().flush();
if !libwebp_ok {
return exit_codes::INTERNAL;
}
if args.iter().any(|arg| arg == "--check") {
exit_codes::OK
} else {
eprintln!("run: nothing to do before R1 (use --check to print the configuration)");
exit_codes::USAGE
}
}
#[cfg(feature = "test-hooks")]
fn fixture(args: &[String]) -> i32 {
let Some(bytes) = args
.first()
.and_then(|name| faunosteo_worker::fixtures::by_name(name))
else {
eprintln!(
"usage: faunosteo-worker fixture <png|jpeg|webp|big-jpeg|big-png|tiny|huge-header|gif|truncated>"
);
return exit_codes::USAGE;
};
let stdout = std::io::stdout();
let mut lock = stdout.lock();
if lock.write_all(&bytes).and_then(|()| lock.flush()).is_err() {
return exit_codes::OUTPUT_FAILED;
}
exit_codes::OK
}
+200
View File
@@ -0,0 +1,200 @@
//! Confinement of the decode child (review Part IV.1), applied by the child itself from a
//! single-threaded process before it reads any input. Three layers: an empty environment
//! (cleared by the parent, checked in `decode`), rlimits on address space and CPU time, and
//! a seccomp allowlist installed with TSYNC that kills the process on any other syscall.
use std::collections::BTreeMap;
use std::fmt;
use seccompiler::{BpfProgram, SeccompAction, SeccompFilter, SeccompRule, TargetArch};
use crate::limits;
/// Syscalls the decode child makes once the filter is installed (static musl binary, x86_64).
///
/// Derived with `strace -f` over the fixture corpus at the caps (`crates/worker/scripts/
/// derive-seccomp.sh`, run by the Dockerfile stage `test`, 2026-10-12: exactly these seven
/// after the marker) and proven by running every fixture through the full sandbox. musl's
/// mallocng used neither `brk` nor `mprotect`, and no `futex` appears in a single-threaded
/// decode.
pub const ALLOWED_SYSCALLS: &[(&str, i64)] = &[
("read", libc::SYS_read),
("write", libc::SYS_write),
("mmap", libc::SYS_mmap),
("munmap", libc::SYS_munmap),
("mremap", libc::SYS_mremap),
("exit_group", libc::SYS_exit_group),
("getrandom", libc::SYS_getrandom),
];
/// Syscalls of the abort path (`RLIMIT_AS` hit, allocation failure, panic = abort): what
/// musl's `abort()` and Rust's alloc error handler need, proven by the sandbox test
/// `the_address_space_limit_aborts_a_huge_allocation` (SIGABRT observed, not SIGSYS).
pub const ABORT_PATH_SYSCALLS: &[(&str, i64)] = &[
("rt_sigaction", libc::SYS_rt_sigaction),
("rt_sigprocmask", libc::SYS_rt_sigprocmask),
("getpid", libc::SYS_getpid),
("gettid", libc::SYS_gettid),
("tkill", libc::SYS_tkill),
("tgkill", libc::SYS_tgkill),
("exit", libc::SYS_exit),
];
/// Resource limits of the child.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
pub address_space_bytes: u64,
pub cpu_seconds: u64,
}
impl Default for Limits {
fn default() -> Self {
Self {
address_space_bytes: limits::CHILD_ADDRESS_SPACE_BYTES,
cpu_seconds: limits::CHILD_CPU_SECONDS,
}
}
}
/// Why the sandbox could not be applied (the child exits `sandbox_failed`).
#[derive(Debug)]
pub enum SandboxError {
NotSingleThreaded(usize),
ProcStatusUnreadable,
Rlimit(String),
Seccomp(String),
UnsupportedArchitecture,
}
impl fmt::Display for SandboxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotSingleThreaded(n) => write!(f, "process has {n} threads, expected 1"),
Self::ProcStatusUnreadable => write!(f, "/proc/self/status unreadable"),
Self::Rlimit(detail) => write!(f, "rlimit: {detail}"),
Self::Seccomp(detail) => write!(f, "seccomp: {detail}"),
Self::UnsupportedArchitecture => write!(f, "no seccomp filter for this architecture"),
}
}
}
impl std::error::Error for SandboxError {}
/// Applies the three layers. `seccomp = false` keeps only the rlimits (the strace
/// derivation run of the test stage); a release child always passes `true`.
pub fn apply(limits: &Limits, seccomp: bool) -> Result<(), SandboxError> {
ensure_single_thread()?;
set_rlimits(limits)?;
if seccomp {
install_seccomp()?;
}
Ok(())
}
/// Every syscall name the filter allows, for the derivation script and the tests.
pub fn allowed_names() -> Vec<&'static str> {
ALLOWED_SYSCALLS
.iter()
.chain(ABORT_PATH_SYSCALLS)
.map(|(name, _)| *name)
.collect()
}
/// Parses the `Threads:` line of a `/proc/<pid>/status` text.
pub fn thread_count(status: &str) -> Option<usize> {
status
.lines()
.find_map(|line| line.strip_prefix("Threads:"))
.and_then(|value| value.trim().parse().ok())
}
fn ensure_single_thread() -> Result<(), SandboxError> {
let status = std::fs::read_to_string("/proc/self/status")
.map_err(|_| SandboxError::ProcStatusUnreadable)?;
match thread_count(&status) {
Some(1) => Ok(()),
Some(n) => Err(SandboxError::NotSingleThreaded(n)),
None => Err(SandboxError::ProcStatusUnreadable),
}
}
fn set_rlimits(limits: &Limits) -> Result<(), SandboxError> {
rlimit::setrlimit(
rlimit::Resource::AS,
limits.address_space_bytes,
limits.address_space_bytes,
)
.map_err(|error| SandboxError::Rlimit(format!("RLIMIT_AS: {error}")))?;
// Soft limit first (SIGXCPU, observable), hard limit two seconds later (SIGKILL): with
// soft == hard the kernel checks the hard limit first and the child dies by SIGKILL,
// indistinguishable from the parent's own deadline kill.
rlimit::setrlimit(
rlimit::Resource::CPU,
limits.cpu_seconds,
limits.cpu_seconds + 2,
)
.map_err(|error| SandboxError::Rlimit(format!("RLIMIT_CPU: {error}")))
}
/// Builds the BPF program: allow the listed syscalls, kill the process on anything else.
pub fn build_filter() -> Result<BpfProgram, SandboxError> {
let rules: BTreeMap<i64, Vec<SeccompRule>> = ALLOWED_SYSCALLS
.iter()
.chain(ABORT_PATH_SYSCALLS)
.map(|(_, number)| (*number, Vec::new()))
.collect();
let filter = SeccompFilter::new(
rules,
SeccompAction::KillProcess,
SeccompAction::Allow,
target_arch()?,
)
.map_err(|error| SandboxError::Seccomp(error.to_string()))?;
filter
.try_into()
.map_err(|error: seccompiler::BackendError| SandboxError::Seccomp(error.to_string()))
}
fn install_seccomp() -> Result<(), SandboxError> {
let program = build_filter()?;
seccompiler::apply_filter_all_threads(&program)
.map_err(|error| SandboxError::Seccomp(error.to_string()))
}
fn target_arch() -> Result<TargetArch, SandboxError> {
if cfg!(target_arch = "x86_64") {
Ok(TargetArch::x86_64)
} else if cfg!(target_arch = "aarch64") {
Ok(TargetArch::aarch64)
} else {
Err(SandboxError::UnsupportedArchitecture)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn thread_count_reads_the_status_line() {
assert_eq!(thread_count("Name:\tx\nThreads:\t1\nSigQ:\t0/1\n"), Some(1));
assert_eq!(thread_count("Threads:\t12\n"), Some(12));
assert_eq!(thread_count("Name:\tx\n"), None);
}
#[test]
fn the_filter_compiles() {
let program = build_filter();
assert!(program.is_ok());
}
#[test]
fn no_file_or_process_syscall_is_allowed() {
let names = allowed_names();
for forbidden in [
"open", "openat", "execve", "clone", "fork", "socket", "ptrace",
] {
assert!(!names.contains(&forbidden), "{forbidden} must stay denied");
}
}
}
+168
View File
@@ -0,0 +1,168 @@
//! Runs the decode child from the keyed parent (review Part IV.1): empty environment,
//! bounded output, wall-clock deadline, and the mapping of every way the child can end to
//! a closed outcome. The parent reads the container header of the output, never the pixels.
use std::io::{Read, Write};
use std::os::unix::process::ExitStatusExt;
use std::path::PathBuf;
use std::process::{Command, ExitStatus, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use crate::limits::{CHILD_DEADLINE, MAX_OUTPUT_BYTES, OUTPUT_SIDE};
use crate::{exit_codes, webp_check};
/// How a decode child ended.
#[derive(Debug)]
pub enum ChildOutcome {
/// A 512 × 512 WebP whose container passed the parent's check.
Produced(Vec<u8>),
/// The child refused the input with one of its exit codes.
Rejected { code: i32, name: &'static str },
/// The child died from a signal (SIGSYS 31 = seccomp, SIGABRT 6 = allocation failure,
/// SIGXCPU 24 = CPU limit, SIGKILL 9 = the parent).
Signalled { signal: i32 },
/// The child wrote more than the output cap and was killed.
OutputOverflow,
/// The child outlived the deadline and was killed.
Timeout,
/// The child exited 0 but its output failed the container check, or it could not run.
Failed(String),
}
/// A finished child run.
#[derive(Debug)]
pub struct ChildRun {
pub outcome: ChildOutcome,
pub stderr: String,
}
/// Spawns `decode` children of one worker binary.
#[derive(Debug, Clone)]
pub struct Spawner {
pub exe: PathBuf,
pub deadline: Duration,
pub max_output: usize,
pub extra_args: Vec<String>,
}
impl Spawner {
pub fn new(exe: impl Into<PathBuf>) -> Self {
Self {
exe: exe.into(),
deadline: CHILD_DEADLINE,
max_output: MAX_OUTPUT_BYTES,
extra_args: Vec::new(),
}
}
/// Feeds `input` to a fresh child and classifies what comes back.
pub fn decode(&self, input: &[u8]) -> ChildRun {
let spawned = Command::new(&self.exe)
.arg("decode")
.args(&self.extra_args)
.env_clear()
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn();
let mut child = match spawned {
Ok(child) => child,
Err(error) => return failed(format!("spawn: {error}")),
};
let (Some(mut stdin), Some(stdout), Some(stderr)) =
(child.stdin.take(), child.stdout.take(), child.stderr.take())
else {
let _ = child.kill();
let _ = child.wait();
return failed("child pipes unavailable".to_owned());
};
let input = input.to_vec();
let writer = std::thread::spawn(move || {
let _ = stdin.write_all(&input);
});
let cap = self.max_output;
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let mut buffer = Vec::new();
let mut limited = stdout.take(cap as u64 + 1);
let _ = limited.read_to_end(&mut buffer);
let _ = tx.send(buffer);
});
let err_reader = std::thread::spawn(move || {
let mut buffer = Vec::new();
let _ = stderr.take(4096).read_to_end(&mut buffer);
String::from_utf8_lossy(&buffer).into_owned()
});
let started = Instant::now();
let mut output: Option<Vec<u8>> = None;
let status: Option<ExitStatus> = loop {
if output.is_none()
&& let Ok(buffer) = rx.try_recv()
{
if buffer.len() > cap {
let _ = child.kill();
let _ = child.wait();
let _ = writer.join();
let stderr = err_reader.join().unwrap_or_default();
return ChildRun {
outcome: ChildOutcome::OutputOverflow,
stderr,
};
}
output = Some(buffer);
}
match child.try_wait() {
Ok(Some(status)) => break Some(status),
Ok(None) => {
if started.elapsed() > self.deadline {
let _ = child.kill();
let _ = child.wait();
break None;
}
std::thread::sleep(Duration::from_millis(20));
}
Err(error) => return failed(format!("wait: {error}")),
}
};
let _ = writer.join();
let output = output.unwrap_or_else(|| rx.recv().unwrap_or_default());
let stderr = err_reader.join().unwrap_or_default();
if output.len() > cap {
return ChildRun {
outcome: ChildOutcome::OutputOverflow,
stderr,
};
}
let outcome = match status {
None => ChildOutcome::Timeout,
Some(status) => classify(status, output),
};
ChildRun { outcome, stderr }
}
}
fn classify(status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
match status.code() {
Some(0) => match webp_check::expect_square(&output, OUTPUT_SIDE) {
Ok(_) => ChildOutcome::Produced(output),
Err(issue) => ChildOutcome::Failed(format!("output refused: {issue}")),
},
Some(code) => ChildOutcome::Rejected {
code,
name: exit_codes::name(code),
},
None => ChildOutcome::Signalled {
signal: status.signal().unwrap_or(-1),
},
}
}
fn failed(detail: String) -> ChildRun {
ChildRun {
outcome: ChildOutcome::Failed(detail),
stderr: String::new(),
}
}
+207
View File
@@ -0,0 +1,207 @@
//! Parent-side check of the child's output (review Part IV.1): the keyed parent never
//! decodes pixels. It parses the RIFF container, allowlists the chunk types, reads the
//! canvas size from the bitstream headers and refuses any metadata chunk.
use std::fmt;
/// Why the parent refused the child's output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WebpIssue {
TooShort,
NotRiff,
RiffSizeMismatch,
NotWebp,
ChunkOverrun,
ForbiddenChunk([u8; 4]),
MissingBitstream,
BadBitstream,
Dimensions { width: u32, height: u32 },
}
impl fmt::Display for WebpIssue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ForbiddenChunk(fourcc) => {
write!(f, "forbidden chunk {}", String::from_utf8_lossy(fourcc))
}
Self::Dimensions { width, height } => write!(f, "unexpected size {width}x{height}"),
other => write!(f, "{other:?}"),
}
}
}
/// What the parent learnt from the container without decoding a pixel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebpSummary {
pub width: u32,
pub height: u32,
pub chunks: Vec<[u8; 4]>,
}
const ALLOWED_CHUNKS: [&[u8; 4]; 4] = [b"VP8X", b"VP8 ", b"VP8L", b"ALPH"];
/// Parses the container of a WebP file produced by the child.
pub fn inspect(bytes: &[u8]) -> Result<WebpSummary, WebpIssue> {
let header = bytes.get(0..12).ok_or(WebpIssue::TooShort)?;
if &header[0..4] != b"RIFF" {
return Err(WebpIssue::NotRiff);
}
let riff_size = u32::from_le_bytes([header[4], header[5], header[6], header[7]]) as usize;
if riff_size.checked_add(8) != Some(bytes.len()) {
return Err(WebpIssue::RiffSizeMismatch);
}
if &header[8..12] != b"WEBP" {
return Err(WebpIssue::NotWebp);
}
let mut offset = 12;
let mut chunks = Vec::new();
let mut dimensions = None;
while offset < bytes.len() {
let chunk_header = bytes
.get(offset..offset + 8)
.ok_or(WebpIssue::ChunkOverrun)?;
let fourcc = [
chunk_header[0],
chunk_header[1],
chunk_header[2],
chunk_header[3],
];
let size = u32::from_le_bytes([
chunk_header[4],
chunk_header[5],
chunk_header[6],
chunk_header[7],
]) as usize;
let start = offset + 8;
let end = start.checked_add(size).ok_or(WebpIssue::ChunkOverrun)?;
let payload = bytes.get(start..end).ok_or(WebpIssue::ChunkOverrun)?;
if !ALLOWED_CHUNKS.contains(&&fourcc) {
return Err(WebpIssue::ForbiddenChunk(fourcc));
}
if dimensions.is_none() {
dimensions = match &fourcc {
b"VP8X" => Some(parse_vp8x(payload)?),
b"VP8 " => Some(parse_vp8(payload)?),
b"VP8L" => Some(parse_vp8l(payload)?),
_ => None,
};
}
chunks.push(fourcc);
offset = end + (size & 1);
}
let (width, height) = dimensions.ok_or(WebpIssue::MissingBitstream)?;
Ok(WebpSummary {
width,
height,
chunks,
})
}
/// `inspect`, then the canvas must be exactly `side` × `side`.
pub fn expect_square(bytes: &[u8], side: u32) -> Result<WebpSummary, WebpIssue> {
let summary = inspect(bytes)?;
if summary.width != side || summary.height != side {
return Err(WebpIssue::Dimensions {
width: summary.width,
height: summary.height,
});
}
Ok(summary)
}
fn parse_vp8x(payload: &[u8]) -> Result<(u32, u32), WebpIssue> {
let p = payload.get(0..10).ok_or(WebpIssue::BadBitstream)?;
let width = 1 + u32::from_le_bytes([p[4], p[5], p[6], 0]);
let height = 1 + u32::from_le_bytes([p[7], p[8], p[9], 0]);
Ok((width, height))
}
fn parse_vp8(payload: &[u8]) -> Result<(u32, u32), WebpIssue> {
let p = payload.get(0..10).ok_or(WebpIssue::BadBitstream)?;
if p[3..6] != [0x9d, 0x01, 0x2a] {
return Err(WebpIssue::BadBitstream);
}
let width = u32::from(u16::from_le_bytes([p[6], p[7]]) & 0x3fff);
let height = u32::from(u16::from_le_bytes([p[8], p[9]]) & 0x3fff);
Ok((width, height))
}
fn parse_vp8l(payload: &[u8]) -> Result<(u32, u32), WebpIssue> {
let p = payload.get(0..5).ok_or(WebpIssue::BadBitstream)?;
if p[0] != 0x2f {
return Err(WebpIssue::BadBitstream);
}
let bits = u32::from_le_bytes([p[1], p[2], p[3], p[4]]);
Ok(((bits & 0x3fff) + 1, ((bits >> 14) & 0x3fff) + 1))
}
#[cfg(test)]
mod tests {
use super::*;
fn riff(chunks: &[(&[u8; 4], &[u8])]) -> Vec<u8> {
let mut body = b"WEBP".to_vec();
for (fourcc, payload) in chunks {
body.extend_from_slice(*fourcc);
body.extend_from_slice(&(payload.len() as u32).to_le_bytes());
body.extend_from_slice(payload);
if payload.len() % 2 == 1 {
body.push(0);
}
}
let mut out = b"RIFF".to_vec();
out.extend_from_slice(&(body.len() as u32).to_le_bytes());
out.extend_from_slice(&body);
out
}
fn vp8x(width: u32, height: u32) -> Vec<u8> {
let w = (width - 1).to_le_bytes();
let h = (height - 1).to_le_bytes();
vec![0x10, 0, 0, 0, w[0], w[1], w[2], h[0], h[1], h[2]]
}
#[test]
fn a_square_vp8x_file_passes() {
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"VP8 ", &[0; 20])]);
let summary = expect_square(&bytes, 512);
assert_eq!(summary.map(|s| (s.width, s.height)), Ok((512, 512)));
}
#[test]
fn metadata_chunks_are_refused() {
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"EXIF", b"x")]);
assert_eq!(inspect(&bytes), Err(WebpIssue::ForbiddenChunk(*b"EXIF")));
}
#[test]
fn wrong_sizes_are_refused() {
let bytes = riff(&[(b"VP8X", &vp8x(511, 512)), (b"VP8 ", &[0; 20])]);
assert_eq!(
expect_square(&bytes, 512),
Err(WebpIssue::Dimensions {
width: 511,
height: 512
})
);
let mut truncated = riff(&[(b"VP8X", &vp8x(512, 512))]);
truncated.pop();
assert_eq!(inspect(&truncated), Err(WebpIssue::RiffSizeMismatch));
assert_eq!(inspect(b"not a webp"), Err(WebpIssue::TooShort));
}
#[test]
fn simple_lossy_and_lossless_headers_give_the_size() {
let mut vp8 = vec![0u8; 10];
vp8[3..6].copy_from_slice(&[0x9d, 0x01, 0x2a]);
vp8[6..8].copy_from_slice(&512u16.to_le_bytes());
vp8[8..10].copy_from_slice(&512u16.to_le_bytes());
assert!(expect_square(&riff(&[(b"VP8 ", &vp8)]), 512).is_ok());
let bits: u32 = (512 - 1) | ((512 - 1) << 14);
let mut vp8l = vec![0x2f];
vp8l.extend_from_slice(&bits.to_le_bytes());
assert!(expect_square(&riff(&[(b"VP8L", &vp8l)]), 512).is_ok());
}
}
+174
View File
@@ -0,0 +1,174 @@
//! 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 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);
}