fix(worker): SAFETY comments state the invariants the code checks; WebP stride by checked arithmetic

The [gated <date>] markers read as an approval stamp: replaced by plain
SAFETY comments giving, for each of the seven unsafe sites, the FFI contract
and where the surrounding code establishes it (buffer length and stride
checks, non-zero size and non-null output before from_raw_parts, single
WebPFree, prctl integer arguments with the return code checked, the two
negative hooks compiled with test-hooks only). The stride is now computed
with checked_mul instead of width * 4.
This commit is contained in:
bchanot committed 2026-10-13 01:19:14 +02:00
1 parent f2cf93668e
commit 2d573883e5
2 files changed
+33 -13

No files matched your search

+26 -11
View File
@@ -147,25 +147,34 @@ pub fn encode_webp(rgba: &[u8], width: u32, height: u32, quality: f32) -> Result
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(width * 4),
c_int::try_from(stride_bytes),
) else {
return Err(Reject::Internal);
};
let mut output: *mut u8 = std::ptr::null_mut();
// 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.
// 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 [gated 2026-10-13]: libwebp returned `size` initialised bytes at `output`.
// 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 [gated 2026-10-13]: `output` came from libwebp's allocator and is not read afterwards.
// 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)
}
@@ -173,7 +182,8 @@ 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 [gated 2026-10-13]: a pure getter of the FFI without arguments or side effects.
// 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);
(
@@ -316,15 +326,20 @@ impl TestHooks {
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.
// 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 [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.
// 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(),
+7 -2
View File
@@ -100,12 +100,17 @@ 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 [gated 2026-10-13]: prctl with integer arguments only, changes this process only.
// SAFETY: prctl is a variadic FFI; for PR_SET_DUMPABLE the kernel reads one integer
// (the new flag) and requires the three remaining arguments to be zero, which is what is
// passed: no pointer, no memory of this process involved, and only this process's flag
// changes. The return code is checked below.
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.
// SAFETY: PR_GET_DUMPABLE reads this process's flag; the four extra arguments must be
// zero and are. The result is the flag (0 expected) or -1 on failure: any non-zero value
// is refused below.
let dumpable = unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) };
if dumpable != 0 {
return Err(SandboxError::Rlimit(format!(