fix(worker): security audit of R0 — no core dumps, tkill/tgkill limited to SIGABRT on self, mmap without PROT_EXEC, Broken outcome for a broken child environment, frame vs canvas check, unsafe denied with local allows, sandbox tests and derivation on the release profile (syscall marker); wasm proof exports behind a feature; ci: release-images hardened (typed input via env, advisories gate, ephemeral builder, env secrets + logout, ref gate, pinned checkout)

This commit is contained in:
bchanot committed 2026-10-13 00:18:53 +02:00
1 parent 9d3e0294e2
commit 11813f5560
14 files changed
+293 -58

No files matched your search

+7 -2
View File
@@ -133,6 +133,7 @@ fn map_image_error(error: ImageError) -> Reject {
}
/// 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<Vec<u8>, Reject> {
let expected = (width as usize)
.checked_mul(height as usize)
@@ -164,6 +165,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.
let packed = unsafe { libwebp_sys::WebPGetEncoderVersion() };
@@ -235,8 +237,11 @@ impl TestHooks {
#[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.
// 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? {
+6
View File
@@ -14,6 +14,12 @@ pub const ENCODE_FAILED: i32 = 16;
pub const OUTPUT_FAILED: i32 = 17;
pub const INTERNAL: i32 = 18;
/// 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.
pub fn is_content_rejection(code: i32) -> bool {
(INPUT_TOO_LARGE..=OUTPUT_FAILED).contains(&code)
}
/// Stable name of an exit code for logs and tests.
pub fn name(code: i32) -> &'static str {
match code {
+55 -5
View File
@@ -6,7 +6,10 @@
use std::collections::BTreeMap;
use std::fmt;
use seccompiler::{BpfProgram, SeccompAction, SeccompFilter, SeccompRule, TargetArch};
use seccompiler::{
BpfProgram, SeccompAction, SeccompCmpArgLen, SeccompCmpOp, SeccompCondition, SeccompFilter,
SeccompRule, TargetArch,
};
use crate::limits;
@@ -84,6 +87,7 @@ impl std::error::Error for SandboxError {}
/// derivation run of the test stage); a release child always passes `true`.
pub fn apply(limits: &Limits, seccomp: bool) -> Result<(), SandboxError> {
ensure_single_thread()?;
disable_core_dumps()?;
set_rlimits(limits)?;
if seccomp {
install_seccomp()?;
@@ -91,6 +95,20 @@ pub fn apply(limits: &Limits, seccomp: bool) -> Result<(), SandboxError> {
Ok(())
}
/// No core dump of the child: a dump would hold the uploaded photo (personal data) and
/// could fill the VPS disk (SIGABRT under `RLIMIT_AS`, SIGSYS, SIGXCPU all dump by default).
/// `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.
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()));
}
rlimit::setrlimit(rlimit::Resource::CORE, 0, 0)
.map_err(|error| SandboxError::Rlimit(format!("RLIMIT_CORE: {error}")))
}
/// Every syscall name the filter allows, for the derivation script and the tests.
pub fn allowed_names() -> Vec<&'static str> {
ALLOWED_SYSCALLS
@@ -137,12 +155,46 @@ fn set_rlimits(limits: &Limits) -> Result<(), SandboxError> {
}
/// Builds the BPF program: allow the listed syscalls, kill the process on anything else.
///
/// Argument filters on the abort path and on `mmap`: `tkill`/`tgkill` may only send SIGABRT
/// to this very process (the child is single-threaded, so tid == pid), never a signal to the
/// keyed parent or a sibling; `mmap` may not map executable memory.
pub fn build_filter() -> Result<BpfProgram, SandboxError> {
let rules: BTreeMap<i64, Vec<SeccompRule>> = ALLOWED_SYSCALLS
let pid = u64::from(std::process::id());
let sigabrt = u64::try_from(libc::SIGABRT).unwrap_or(6);
let prot_exec = u64::try_from(libc::PROT_EXEC).unwrap_or(4);
let mut rules: BTreeMap<i64, Vec<SeccompRule>> = ALLOWED_SYSCALLS
.iter()
.chain(ABORT_PATH_SYSCALLS)
.map(|(_, number)| (*number, Vec::new()))
.collect();
let seccomp_err = |error: seccompiler::BackendError| SandboxError::Seccomp(error.to_string());
let eq = |index: u8, value: u64| {
SeccompCondition::new(index, SeccompCmpArgLen::Qword, SeccompCmpOp::Eq, value)
.map_err(seccomp_err)
};
rules.insert(
libc::SYS_tkill,
vec![SeccompRule::new(vec![eq(0, pid)?, eq(1, sigabrt)?]).map_err(seccomp_err)?],
);
rules.insert(
libc::SYS_tgkill,
vec![
SeccompRule::new(vec![eq(0, pid)?, eq(1, pid)?, eq(2, sigabrt)?])
.map_err(seccomp_err)?,
],
);
let no_exec = SeccompCondition::new(
2,
SeccompCmpArgLen::Qword,
SeccompCmpOp::MaskedEq(prot_exec),
0,
)
.map_err(seccomp_err)?;
rules.insert(
libc::SYS_mmap,
vec![SeccompRule::new(vec![no_exec]).map_err(seccomp_err)?],
);
let filter = SeccompFilter::new(
rules,
SeccompAction::KillProcess,
@@ -150,9 +202,7 @@ pub fn build_filter() -> Result<BpfProgram, SandboxError> {
target_arch()?,
)
.map_err(|error| SandboxError::Seccomp(error.to_string()))?;
filter
.try_into()
.map_err(|error: seccompiler::BackendError| SandboxError::Seccomp(error.to_string()))
filter.try_into().map_err(seccomp_err)
}
fn install_seccomp() -> Result<(), SandboxError> {
+17 -4
View File
@@ -17,8 +17,12 @@ 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 exit codes.
/// The child refused the input with one of its content exit codes (12 to 17).
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.
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).
Signalled { signal: i32 },
@@ -30,7 +34,8 @@ pub enum ChildOutcome {
Failed(String),
}
/// A finished child run.
/// A finished child run. `stderr` is text the child controls (up to 4 KiB): never logged
/// raw by R1, control characters escaped and the field allowlist applied (LRN-044).
#[derive(Debug)]
pub struct ChildRun {
pub outcome: ChildOutcome,
@@ -124,7 +129,11 @@ impl Spawner {
}
std::thread::sleep(Duration::from_millis(20));
}
Err(error) => return failed(format!("wait: {error}")),
Err(error) => {
let _ = child.kill();
let _ = child.wait();
return failed(format!("wait: {error}"));
}
}
};
let _ = writer.join();
@@ -150,7 +159,11 @@ fn classify(status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
Ok(_) => ChildOutcome::Produced(output),
Err(issue) => ChildOutcome::Failed(format!("output refused: {issue}")),
},
Some(code) => ChildOutcome::Rejected {
Some(code) if exit_codes::is_content_rejection(code) => ChildOutcome::Rejected {
code,
name: exit_codes::name(code),
},
Some(code) => ChildOutcome::Broken {
code,
name: exit_codes::name(code),
},
+30 -9
View File
@@ -79,13 +79,20 @@ pub fn inspect(bytes: &[u8]) -> Result<WebpSummary, WebpIssue> {
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,
};
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);
}
_ => {}
}
chunks.push(fourcc);
offset = end + (size & 1);
@@ -164,11 +171,25 @@ mod tests {
#[test]
fn a_square_vp8x_file_passes() {
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"VP8 ", &[0; 20])]);
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());
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"VP8 ", &vp8)]);
let summary = expect_square(&bytes, 512);
assert_eq!(summary.map(|s| (s.width, s.height)), Ok((512, 512)));
}
#[test]
fn a_frame_disagreeing_with_its_canvas_is_refused() {
let mut vp8 = vec![0u8; 10];
vp8[3..6].copy_from_slice(&[0x9d, 0x01, 0x2a]);
vp8[6..8].copy_from_slice(&256u16.to_le_bytes());
vp8[8..10].copy_from_slice(&512u16.to_le_bytes());
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"VP8 ", &vp8)]);
assert_eq!(inspect(&bytes), Err(WebpIssue::BadBitstream));
}
#[test]
fn metadata_chunks_are_refused() {
let bytes = riff(&[(b"VP8X", &vp8x(512, 512)), (b"EXIF", b"x")]);
@@ -177,7 +198,7 @@ mod tests {
#[test]
fn wrong_sizes_are_refused() {
let bytes = riff(&[(b"VP8X", &vp8x(511, 512)), (b"VP8 ", &[0; 20])]);
let bytes = riff(&[(b"VP8X", &vp8x(511, 512))]);
assert_eq!(
expect_square(&bytes, 512),
Err(WebpIssue::Dimensions {