//! 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, /// A decoder budget (`image::Limits`, allocation, complexity) exceeded by the content. ResourceLimit, /// libwebp failed on the buffer the child produced itself: an environment fault. EncodeFailed, /// A programming or I/O fault of the child, never a judgement on the content. 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::ResourceLimit => exit_codes::RESOURCE_LIMIT, 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, 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) } /// Every decode-time error is a judgement on the content (a content code): an uploader must /// never be able to produce a "broken environment" outcome (security audit of R0). fn map_image_error(error: ImageError) -> Reject { match error { ImageError::Limits(limit) => match limit.kind() { LimitErrorKind::DimensionError => Reject::Dimensions, _ => Reject::ResourceLimit, }, ImageError::Unsupported(_) => Reject::UnsupportedType, _ => Reject::DecodeFailed, } } /// Encodes an RGBA buffer as lossy WebP through libwebp (D-R4: C confined to this process). #[allow(unsafe_code)] pub fn encode_webp(rgba: &[u8], width: u32, height: u32, quality: f32) -> Result, 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 Some(stride_bytes) = width.checked_mul(4) else { 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(stride_bytes), ) else { return Err(Reject::Internal); }; let mut output: *mut u8 = std::ptr::null_mut(); // SAFETY: WebPEncodeRGBA reads `height` rows of `stride` bytes starting at `rgba`; // `rgba.len() == width * height * 4` and `stride == width * 4` are established above with // overflow-checked arithmetic, so every read stays inside the slice, which outlives the // call. `&mut output` is a valid `*mut *mut u8` for the duration of the call: libwebp // stores the address of a buffer it allocated there, or leaves it null and returns 0. // libwebp retains no pointer once the call returns. 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: a non-zero return means libwebp initialised exactly `size` bytes at `output` // (both non-zero and non-null, checked just above). The slice lives only for the copy // below and is dropped before the buffer is released. let encoded = unsafe { std::slice::from_raw_parts(output, size) }.to_vec(); // SAFETY: `output` was allocated by libwebp for this call and must be released by its // own WebPFree; this is the single release and the pointer is not used afterwards. unsafe { libwebp_sys::WebPFree(output.cast()) }; Ok(encoded) } /// Version of the libwebp linked into this binary, as (major, minor, revision). #[allow(unsafe_code)] pub fn libwebp_version() -> (u8, u8, u8) { // SAFETY: WebPGetEncoderVersion takes no argument, touches no memory owned by the caller // and has no side effect: it returns the version packed into a C int. 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, } #[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), /// 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. SignalParent, /// Maps executable memory: the `mmap` argument filter must kill the child. MmapExec, } impl TestHooks { /// Shared with the `verify` child, which takes the same flags. pub(crate) fn parse(args: &[String]) -> Result { #[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), "--signal-parent" => hooks.action = Some(HookAction::SignalParent), "--mmap-exec" => hooks.action = Some(HookAction::MmapExec), "--alloc-gib" => { let value = iter .next() .and_then(|v| v.parse().ok()) .ok_or("--alloc-gib ")?; hooks.action = Some(HookAction::AllocGib(value)); } "--alloc-small-gib" => { let value = iter .next() .and_then(|v| v.parse().ok()) .ok_or("--alloc-small-gib ")?; hooks.action = Some(HookAction::AllocSmallGib(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")] pub(crate) fn run_after_sandbox( &self, stdout: &mut dyn Write, stderr: &mut dyn Write, ) -> Option { if !self.seccomp { // Marker for crates/worker/scripts/derive-seccomp.sh: one `sched_yield`, a syscall // the decode path never makes, right where the filter would be installed. The // syscalls after it are the ones the filter must allow. (A string marker is not // readable by strace once PR_SET_DUMPABLE is 0.) std::thread::yield_now(); 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::AllocSmallGib(gib) => { let chunks = gib.saturating_mul(1024 * 1024 * 1024) / 4096; let kept: Vec> = (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); }, HookAction::SignalParent => { let survived = negative_hooks::signal_parent(); let _ = writeln!( stderr, "decode: tkill(parent) survived the filter: {survived}" ); Some(exit_codes::INTERNAL) } HookAction::MmapExec => { let survived = negative_hooks::mmap_exec(); let _ = writeln!( stderr, "decode: mmap(PROT_EXEC) survived the filter: {survived}" ); Some(exit_codes::INTERNAL) } } } #[cfg(not(feature = "test-hooks"))] pub(crate) fn run_after_sandbox( &self, _stdout: &mut dyn Write, _stderr: &mut dyn Write, ) -> Option { None } } /// Negative proofs of the seccomp argument filters (`test-hooks` only): each call must die /// with SIGSYS before returning; a return value means the filter let it through. #[cfg(feature = "test-hooks")] #[allow(unsafe_code)] mod negative_hooks { /// `tkill(getppid(), SIGTERM)`: a signal to another process. pub fn signal_parent() -> i32 { // SAFETY: a raw syscall with three integer arguments and no pointer: the kernel // validates them, so the call cannot touch this process's memory. What the proof // checks is behaviour: under the filter the process dies with SIGSYS before any // signal exists; without the filter the parent would receive SIGTERM, which is why // this module is compiled with the test hooks only and reached by `--signal-parent`. unsafe { libc::syscall(libc::SYS_tkill, libc::getppid(), libc::SIGTERM) as i32 } } /// `mmap` of one executable page. pub fn mmap_exec() -> i32 { // SAFETY: an anonymous private mapping with a null address hint and no file // descriptor: no existing mapping is touched, and the returned address is only // compared with MAP_FAILED, never dereferenced. Under the filter the process dies // with SIGSYS before the mapping exists; without it one page leaks in a test process. let page = unsafe { libc::mmap( std::ptr::null_mut(), 4096, libc::PROT_READ | libc::PROT_EXEC, libc::MAP_PRIVATE | libc::MAP_ANONYMOUS, -1, 0, ) }; i32::from(page != libc::MAP_FAILED) } } /// 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, 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)) }