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

+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> {