Files
faunosteo/crates/worker/src/decode.rs
T

287 lines
10 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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))
}