fix(worker): second security pass — content-only rejection codes (resource_limit, explicit list), negative proofs of the seccomp argument filters, PR_GET_DUMPABLE self-check, strict WebP chunk structure, iCCP-bomb fixture, separate test target cache; ci: release-images builder actually used (job-level DOCKER_CONFIG, --builder), literal cleanup path, token scoped to the login step, npm advisories gate, digests from build metadata
This commit is contained in:
1 parent
11813f5560
commit
a8b644a515
12 files changed
+278
-55
No files matched your search
@@ -21,7 +21,11 @@ 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,
|
||||
}
|
||||
|
||||
@@ -31,6 +35,7 @@ impl Reject {
|
||||
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,
|
||||
}
|
||||
@@ -120,15 +125,16 @@ pub fn process(bytes: &[u8]) -> Result<Vec<u8>, Reject> {
|
||||
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::Internal,
|
||||
_ => Reject::ResourceLimit,
|
||||
},
|
||||
ImageError::Unsupported(_) => Reject::UnsupportedType,
|
||||
ImageError::Decoding(_) | ImageError::IoError(_) => Reject::DecodeFailed,
|
||||
_ => Reject::Internal,
|
||||
_ => Reject::DecodeFailed,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +155,7 @@ pub fn encode_webp(rgba: &[u8], width: u32, height: u32, quality: f32) -> Result
|
||||
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
|
||||
// SAFETY [gated 2026-10-13]: `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 =
|
||||
@@ -167,7 +173,7 @@ pub fn encode_webp(rgba: &[u8], width: u32, height: u32, quality: f32) -> Result
|
||||
/// Version of the libwebp linked into this binary, as (major, minor, revision).
|
||||
#[allow(unsafe_code)]
|
||||
pub fn libwebp_version() -> (u8, u8, u8) {
|
||||
// SAFETY: a pure getter of the FFI without arguments or side effects.
|
||||
// SAFETY [gated 2026-10-13]: 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);
|
||||
(
|
||||
@@ -196,6 +202,10 @@ enum HookAction {
|
||||
AllocGib(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 {
|
||||
@@ -216,6 +226,8 @@ impl TestHooks {
|
||||
"--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()
|
||||
@@ -272,6 +284,22 @@ impl TestHooks {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,6 +309,36 @@ impl TestHooks {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 [gated 2026-10-13]: integer arguments only; the syscall is expected to be
|
||||
// refused by the filter (KillProcess) and never to reach the parent.
|
||||
unsafe { libc::syscall(libc::SYS_tkill, libc::getppid(), libc::SIGTERM) as i32 }
|
||||
}
|
||||
|
||||
/// `mmap` of one executable page.
|
||||
pub fn mmap_exec() -> i32 {
|
||||
// SAFETY [gated 2026-10-13]: an anonymous private mapping with no file and a null
|
||||
// hint; the filter is expected to kill the process before it exists.
|
||||
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<u8>, bool)> {
|
||||
let mut buffer = Vec::new();
|
||||
|
||||
@@ -13,11 +13,18 @@ pub const DIMENSIONS: i32 = 15;
|
||||
pub const ENCODE_FAILED: i32 = 16;
|
||||
pub const OUTPUT_FAILED: i32 = 17;
|
||||
pub const INTERNAL: i32 = 18;
|
||||
/// The decoders' allocation or complexity budget was exceeded by the content.
|
||||
pub const RESOURCE_LIMIT: i32 = 19;
|
||||
|
||||
/// Codes that judge the content (the upload is refused); every other non-zero code means
|
||||
/// the child's environment is broken and the parent must alert rather than reject.
|
||||
/// Codes that judge the content (the upload is refused). Every other non-zero code means
|
||||
/// the child's environment is broken (`Broken` outcome: the parent alerts rather than
|
||||
/// rejects). Explicit list on purpose: a new code never lands in the wrong class by
|
||||
/// accident, and no content can produce a "broken" code.
|
||||
pub fn is_content_rejection(code: i32) -> bool {
|
||||
(INPUT_TOO_LARGE..=OUTPUT_FAILED).contains(&code)
|
||||
matches!(
|
||||
code,
|
||||
INPUT_TOO_LARGE | UNSUPPORTED_TYPE | DECODE_FAILED | DIMENSIONS | RESOURCE_LIMIT
|
||||
)
|
||||
}
|
||||
|
||||
/// Stable name of an exit code for logs and tests.
|
||||
@@ -34,6 +41,7 @@ pub fn name(code: i32) -> &'static str {
|
||||
ENCODE_FAILED => "encode_failed",
|
||||
OUTPUT_FAILED => "output_failed",
|
||||
INTERNAL => "internal",
|
||||
RESOURCE_LIMIT => "resource_limit",
|
||||
_ => "unknown",
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,34 @@ fn push_chunk(out: &mut Vec<u8>, kind: &[u8; 4], payload: &[u8]) {
|
||||
out.extend_from_slice(&hasher.finalize().to_be_bytes());
|
||||
}
|
||||
|
||||
/// A small valid-looking PNG whose `iCCP` profile inflates to `inflated_bytes` (hundreds of
|
||||
/// kilobytes of compressed zeros): the decoder's allocation budget must refuse it as content
|
||||
/// (`resource_limit`), never as a broken environment.
|
||||
pub fn png_iccp_bomb(inflated_bytes: usize) -> Vec<u8> {
|
||||
use std::io::Write;
|
||||
let mut out = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
let mut ihdr = Vec::new();
|
||||
ihdr.extend_from_slice(&64u32.to_be_bytes());
|
||||
ihdr.extend_from_slice(&64u32.to_be_bytes());
|
||||
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
|
||||
push_chunk(&mut out, b"IHDR", &ihdr);
|
||||
let mut iccp = b"p\0\0".to_vec();
|
||||
let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best());
|
||||
let zeros = vec![0u8; 1 << 20];
|
||||
let mut written = 0;
|
||||
while written < inflated_bytes {
|
||||
let chunk = (inflated_bytes - written).min(zeros.len());
|
||||
if encoder.write_all(&zeros[..chunk]).is_err() {
|
||||
break;
|
||||
}
|
||||
written += chunk;
|
||||
}
|
||||
iccp.extend_from_slice(&encoder.finish().unwrap_or_default());
|
||||
push_chunk(&mut out, b"iCCP", &iccp);
|
||||
push_chunk(&mut out, b"IEND", &[]);
|
||||
out
|
||||
}
|
||||
|
||||
/// 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();
|
||||
@@ -89,6 +117,7 @@ pub fn by_name(name: &str) -> Option<Vec<u8>> {
|
||||
"big-png" => png(3000, 3000),
|
||||
"tiny" => png(32, 32),
|
||||
"huge-header" => png_header_only(20_000, 20_000),
|
||||
"iccp-bomb" => png_iccp_bomb(300 << 20),
|
||||
"gif" => gif(),
|
||||
"truncated" => truncated_png(),
|
||||
_ => return None,
|
||||
|
||||
@@ -100,11 +100,18 @@ pub fn apply(limits: &Limits, seccomp: bool) -> Result<(), SandboxError> {
|
||||
/// `PR_SET_DUMPABLE 0` is the kernel-side stop, `RLIMIT_CORE 0` the belt.
|
||||
#[allow(unsafe_code)]
|
||||
fn disable_core_dumps() -> Result<(), SandboxError> {
|
||||
// SAFETY: prctl(PR_SET_DUMPABLE, 0) takes no pointer and changes only this process.
|
||||
// SAFETY [gated 2026-10-13]: prctl with integer arguments only, changes this process only.
|
||||
let rc = unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 0, 0, 0, 0) };
|
||||
if rc != 0 {
|
||||
return Err(SandboxError::Rlimit("PR_SET_DUMPABLE".to_owned()));
|
||||
}
|
||||
// SAFETY [gated 2026-10-13]: a getter with integer arguments.
|
||||
let dumpable = unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) };
|
||||
if dumpable != 0 {
|
||||
return Err(SandboxError::Rlimit(format!(
|
||||
"PR_GET_DUMPABLE = {dumpable}"
|
||||
)));
|
||||
}
|
||||
rlimit::setrlimit(rlimit::Resource::CORE, 0, 0)
|
||||
.map_err(|error| SandboxError::Rlimit(format!("RLIMIT_CORE: {error}")))
|
||||
}
|
||||
|
||||
@@ -17,11 +17,13 @@ use crate::{exit_codes, webp_check};
|
||||
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 content exit codes (12 to 17).
|
||||
/// The child refused the input with one of its content exit codes
|
||||
/// (`exit_codes::is_content_rejection`).
|
||||
Rejected { code: i32, name: &'static str },
|
||||
/// The child could not even start properly (usage, non-empty environment, sandbox
|
||||
/// failure, internal error): the confinement environment is broken, not the upload.
|
||||
/// R1 alerts and stops on this outcome instead of rejecting the job.
|
||||
/// The child could not run properly (usage, non-empty environment, sandbox failure,
|
||||
/// encode or output failure, internal error): the confinement environment is broken,
|
||||
/// not the upload. No content can produce this outcome (every decode-time error maps
|
||||
/// to a content code). R1 alerts and stops on it instead of rejecting the job.
|
||||
Broken { 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).
|
||||
|
||||
@@ -55,8 +55,9 @@ pub fn inspect(bytes: &[u8]) -> Result<WebpSummary, WebpIssue> {
|
||||
}
|
||||
|
||||
let mut offset = 12;
|
||||
let mut chunks = Vec::new();
|
||||
let mut dimensions = None;
|
||||
let mut chunks: Vec<[u8; 4]> = Vec::new();
|
||||
let mut canvas: Option<(u32, u32)> = None;
|
||||
let mut frame: Option<(u32, u32)> = None;
|
||||
while offset < bytes.len() {
|
||||
let chunk_header = bytes
|
||||
.get(offset..offset + 8)
|
||||
@@ -79,25 +80,43 @@ pub fn inspect(bytes: &[u8]) -> Result<WebpSummary, WebpIssue> {
|
||||
if !ALLOWED_CHUNKS.contains(&&fourcc) {
|
||||
return Err(WebpIssue::ForbiddenChunk(fourcc));
|
||||
}
|
||||
let declared = match &fourcc {
|
||||
b"VP8X" => Some(parse_vp8x(payload)?),
|
||||
b"VP8 " => Some(parse_vp8(payload)?),
|
||||
b"VP8L" => Some(parse_vp8l(payload)?),
|
||||
_ => None,
|
||||
};
|
||||
// The canvas (VP8X) and the frame (VP8 / VP8L) must agree: a container whose frame
|
||||
// disagrees with its canvas is not something the encoder produces.
|
||||
match (dimensions, declared) {
|
||||
(None, Some(size)) => dimensions = Some(size),
|
||||
(Some(canvas), Some(frame)) if canvas != frame => {
|
||||
return Err(WebpIssue::BadBitstream);
|
||||
// Structure the encoder produces, and nothing else: an optional VP8X first, then an
|
||||
// optional ALPH (extended files only), then exactly one VP8 or VP8L frame.
|
||||
match &fourcc {
|
||||
b"VP8X" => {
|
||||
if !chunks.is_empty() {
|
||||
return Err(WebpIssue::BadBitstream);
|
||||
}
|
||||
canvas = Some(parse_vp8x(payload)?);
|
||||
}
|
||||
_ => {}
|
||||
b"ALPH" => {
|
||||
if canvas.is_none() || frame.is_some() {
|
||||
return Err(WebpIssue::BadBitstream);
|
||||
}
|
||||
}
|
||||
b"VP8 " | b"VP8L" => {
|
||||
if frame.is_some() {
|
||||
return Err(WebpIssue::BadBitstream);
|
||||
}
|
||||
frame = Some(if &fourcc == b"VP8 " {
|
||||
parse_vp8(payload)?
|
||||
} else {
|
||||
parse_vp8l(payload)?
|
||||
});
|
||||
}
|
||||
_ => return Err(WebpIssue::ForbiddenChunk(fourcc)),
|
||||
}
|
||||
chunks.push(fourcc);
|
||||
offset = end + (size & 1);
|
||||
}
|
||||
let (width, height) = dimensions.ok_or(WebpIssue::MissingBitstream)?;
|
||||
let frame = frame.ok_or(WebpIssue::MissingBitstream)?;
|
||||
// The canvas (VP8X) and the frame must agree: the encoder never writes otherwise.
|
||||
if let Some(canvas) = canvas
|
||||
&& canvas != frame
|
||||
{
|
||||
return Err(WebpIssue::BadBitstream);
|
||||
}
|
||||
let (width, height) = canvas.unwrap_or(frame);
|
||||
Ok(WebpSummary {
|
||||
width,
|
||||
height,
|
||||
@@ -190,15 +209,55 @@ mod tests {
|
||||
assert_eq!(inspect(&bytes), Err(WebpIssue::BadBitstream));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chunk_structure_is_strict() {
|
||||
let mut vp8 = vec![0u8; 20];
|
||||
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());
|
||||
// two frames
|
||||
assert_eq!(
|
||||
inspect(&riff(&[(b"VP8 ", &vp8), (b"VP8 ", &vp8)])),
|
||||
Err(WebpIssue::BadBitstream)
|
||||
);
|
||||
// ALPH without VP8X
|
||||
assert_eq!(
|
||||
inspect(&riff(&[(b"ALPH", &[0; 4]), (b"VP8 ", &vp8)])),
|
||||
Err(WebpIssue::BadBitstream)
|
||||
);
|
||||
// VP8X not first
|
||||
assert_eq!(
|
||||
inspect(&riff(&[(b"VP8 ", &vp8), (b"VP8X", &vp8x(512, 512))])),
|
||||
Err(WebpIssue::BadBitstream)
|
||||
);
|
||||
// the encoder's extended layout
|
||||
assert!(
|
||||
expect_square(
|
||||
&riff(&[
|
||||
(b"VP8X", &vp8x(512, 512)),
|
||||
(b"ALPH", &[0; 4]),
|
||||
(b"VP8 ", &vp8)
|
||||
]),
|
||||
512
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_chunks_are_refused() {
|
||||
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"EXIF", b"x")]);
|
||||
// refused on the chunk type before any structural rule
|
||||
assert_eq!(inspect(&bytes), Err(WebpIssue::ForbiddenChunk(*b"EXIF")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_sizes_are_refused() {
|
||||
let bytes = riff(&[(b"VP8X", &vp8x(511, 512))]);
|
||||
let mut vp8 = vec![0u8; 20];
|
||||
vp8[3..6].copy_from_slice(&[0x9d, 0x01, 0x2a]);
|
||||
vp8[6..8].copy_from_slice(&511u16.to_le_bytes());
|
||||
vp8[8..10].copy_from_slice(&512u16.to_le_bytes());
|
||||
let bytes = riff(&[(b"VP8X", &vp8x(511, 512)), (b"VP8 ", &vp8)]);
|
||||
assert_eq!(
|
||||
expect_square(&bytes, 512),
|
||||
Err(WebpIssue::Dimensions {
|
||||
@@ -209,6 +268,10 @@ mod tests {
|
||||
let mut truncated = riff(&[(b"VP8X", &vp8x(512, 512))]);
|
||||
truncated.pop();
|
||||
assert_eq!(inspect(&truncated), Err(WebpIssue::RiffSizeMismatch));
|
||||
assert_eq!(
|
||||
inspect(&riff(&[(b"VP8X", &vp8x(512, 512))])),
|
||||
Err(WebpIssue::MissingBitstream)
|
||||
);
|
||||
assert_eq!(inspect(b"not a webp"), Err(WebpIssue::TooShort));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user