use std::time::{Duration, Instant};
use crate::report::{Finding, Severity};
#[derive(Debug, Clone)]
pub struct Limits {
pub max_file_size: u64,
pub max_decompressed: u64,
pub max_depth: u32,
pub max_duration: Duration,
pub max_pixels: u64,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_file_size: 64 << 20,
max_decompressed: 256 << 20,
max_depth: 4,
max_duration: Duration::from_secs(10),
max_pixels: 1 << 28,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LimitExceeded {
Decompression,
Depth,
Time,
Complexity,
Memory,
}
impl LimitExceeded {
pub fn code(self) -> &'static str {
match self {
Self::Decompression => "limit.decompression",
Self::Depth => "limit.depth",
Self::Time => "limit.time",
Self::Complexity => "limit.complexity",
Self::Memory => "limit.memory",
}
}
pub fn finding(self) -> Finding {
let message = match self {
Self::Decompression => "decompressed data exceeds the configured limit",
Self::Depth => "embedded files are nested deeper than the configured limit",
Self::Time => "scan exceeded the configured time limit",
Self::Complexity => "file structure is too complex to analyze completely",
Self::Memory => "scan exceeded the configured memory limit",
};
Finding::new(self.code(), Severity::Medium, message)
}
}
#[derive(Debug, Clone, Copy)]
pub struct MemoryLimit {
pub max: u64,
pub measure: fn() -> u64,
}
#[derive(Clone, Copy)]
pub(crate) struct MemoryWatch(Option<(MemoryLimit, u64)>);
impl MemoryWatch {
pub fn is_over(self) -> bool {
self.0
.is_some_and(|(limit, start)| (limit.measure)().saturating_sub(start) > limit.max)
}
pub fn headroom(self) -> Option<u64> {
self.0.map(|(limit, start)| {
let held = (limit.measure)().saturating_sub(start);
limit.max.saturating_sub(held)
})
}
}
#[derive(Debug)]
pub(crate) struct Budget {
decompressed_left: u64,
depth_left: u32,
deadline: Instant,
memory: Option<(MemoryLimit, u64)>,
memory_spent: bool,
}
impl Budget {
pub fn new(limits: &Limits) -> Self {
Self {
decompressed_left: limits.max_decompressed,
depth_left: limits.max_depth,
deadline: Instant::now() + limits.max_duration,
memory: None,
memory_spent: false,
}
}
pub fn with_memory(mut self, limit: Option<MemoryLimit>) -> Self {
self.memory = limit.map(|limit| (limit, (limit.measure)()));
self
}
pub fn consume(&mut self, n: u64) -> Result<(), LimitExceeded> {
match self.decompressed_left.checked_sub(n) {
Some(left) => {
self.decompressed_left = left;
Ok(())
}
None => {
self.decompressed_left = 0;
Err(LimitExceeded::Decompression)
}
}
}
pub fn remaining(&self) -> u64 {
self.decompressed_left
}
pub fn check_time(&self) -> Result<(), LimitExceeded> {
if Instant::now() > self.deadline {
return Err(LimitExceeded::Time);
}
if self.memory_spent || self.memory_watch().is_over() {
return Err(LimitExceeded::Memory);
}
Ok(())
}
pub fn memory_watch(&self) -> MemoryWatch {
MemoryWatch(self.memory)
}
pub fn memory_spent(&mut self) {
self.memory_spent = true;
}
pub fn decode_limit(&self) -> LimitExceeded {
if self.memory_spent {
LimitExceeded::Memory
} else {
LimitExceeded::Decompression
}
}
pub fn enter(&mut self) -> Result<(), LimitExceeded> {
match self.depth_left.checked_sub(1) {
Some(left) => {
self.depth_left = left;
Ok(())
}
None => Err(LimitExceeded::Depth),
}
}
pub fn leave(&mut self) {
self.depth_left += 1;
}
}
const BLOCK: usize = 64 << 10;
#[derive(Debug, PartialEq)]
pub(crate) enum Unpacked {
Corrupt,
Limit,
}
pub(crate) fn read_all(
mut read: impl std::io::Read,
cap: u64,
budget: &mut Budget,
) -> Result<Vec<u8>, Unpacked> {
let limit = budget.remaining().min(cap);
let watch = budget.memory_watch();
let mut output = Vec::new();
let mut block = vec![0; BLOCK];
loop {
let read = match read.read(&mut block) {
Ok(0) => break,
Ok(read) => read,
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
Err(_) => return Err(Unpacked::Corrupt),
};
output.extend_from_slice(&block[..read]);
held_to(output.len(), limit, watch, budget)?;
}
let _ = budget.consume(output.len() as u64);
Ok(output)
}
pub(crate) fn inflate(
data: &[u8],
zlib: bool,
cap: u64,
budget: &mut Budget,
) -> Result<Vec<u8>, Unpacked> {
use miniz_oxide::inflate::stream::{InflateState, inflate};
use miniz_oxide::{DataFormat, MZFlush, MZStatus};
let format = if zlib {
DataFormat::Zlib
} else {
DataFormat::Raw
};
let limit = budget.remaining().min(cap);
let watch = budget.memory_watch();
let mut state = InflateState::new_boxed(format);
let mut input = data;
let mut output = Vec::new();
let mut block = vec![0; BLOCK];
loop {
let result = inflate(&mut state, input, &mut block, MZFlush::None);
input = &input[result.bytes_consumed..];
output.extend_from_slice(&block[..result.bytes_written]);
held_to(output.len(), limit, watch, budget)?;
match result.status {
Ok(MZStatus::StreamEnd) => break,
Ok(_) if result.bytes_consumed == 0 && result.bytes_written == 0 => {
return Err(Unpacked::Corrupt);
}
Ok(_) => {}
Err(_) => return Err(Unpacked::Corrupt),
}
}
let _ = budget.consume(output.len() as u64);
Ok(output)
}
fn held_to(
held: usize,
limit: u64,
watch: MemoryWatch,
budget: &mut Budget,
) -> Result<(), Unpacked> {
if held as u64 > limit {
let _ = budget.consume(limit);
return Err(Unpacked::Limit);
}
if watch.is_over() {
budget.memory_spent();
return Err(Unpacked::Limit);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn memory_is_measured_from_the_start_of_the_scan() {
use std::sync::atomic::{AtomicU64, Ordering};
static HELD: AtomicU64 = AtomicU64::new(1000);
let limit = MemoryLimit {
max: 100,
measure: || HELD.load(Ordering::Relaxed),
};
let budget = Budget::new(&Limits::default()).with_memory(Some(limit));
HELD.store(1100, Ordering::Relaxed);
assert_eq!(budget.check_time(), Ok(()));
HELD.store(1101, Ordering::Relaxed);
assert_eq!(budget.check_time(), Err(LimitExceeded::Memory));
HELD.store(10, Ordering::Relaxed);
assert_eq!(budget.check_time(), Ok(()));
}
#[test]
fn decoders_stop_at_their_limit_and_at_the_scans_memory() {
use miniz_oxide::deflate::{compress_to_vec, compress_to_vec_zlib};
use std::cell::Cell;
let text = b"block after block ".repeat(40_000);
let zlib = compress_to_vec_zlib(&text, 6);
let mut budget = Budget::new(&Limits::default());
assert_eq!(
inflate(&zlib, true, u64::MAX, &mut budget).as_deref(),
Ok(&text[..])
);
assert_eq!(
budget.remaining(),
Limits::default().max_decompressed - text.len() as u64
);
let raw = compress_to_vec(&text, 6);
assert_eq!(
inflate(&raw, false, u64::MAX, &mut budget).as_deref(),
Ok(&text[..])
);
assert_eq!(
inflate(&zlib, true, 1000, &mut budget),
Err(Unpacked::Limit)
);
assert_eq!(budget.decode_limit(), LimitExceeded::Decompression);
let cut = &zlib[..zlib.len() / 2];
assert_eq!(
inflate(cut, true, u64::MAX, &mut budget),
Err(Unpacked::Corrupt)
);
assert_eq!(
inflate(b"\xFF\xFF\xFF", true, u64::MAX, &mut budget),
Err(Unpacked::Corrupt)
);
assert_eq!(read_all(&text[..], 10, &mut budget), Err(Unpacked::Limit));
thread_local!(static LOOKS: Cell<u64> = const { Cell::new(0) });
let limit = MemoryLimit {
max: 4 * BLOCK as u64,
measure: || {
LOOKS.with(|looks| {
looks.set(looks.get() + 1);
looks.get() * BLOCK as u64
})
},
};
let mut budget = Budget::new(&Limits::default()).with_memory(Some(limit));
assert_eq!(
inflate(&zlib, true, u64::MAX, &mut budget),
Err(Unpacked::Limit)
);
assert_eq!(budget.decode_limit(), LimitExceeded::Memory);
assert!(LOOKS.with(Cell::get) < 8);
assert_eq!(budget.memory_watch().headroom(), Some(0));
}
#[test]
fn decompression_budget_is_exhausted_once() {
let limits = Limits {
max_decompressed: 10,
..Limits::default()
};
let mut budget = Budget::new(&limits);
assert_eq!(budget.consume(6), Ok(()));
assert_eq!(budget.consume(5), Err(LimitExceeded::Decompression));
assert_eq!(budget.consume(1), Err(LimitExceeded::Decompression));
}
#[test]
fn depth_is_restored_on_leave() {
let limits = Limits {
max_depth: 1,
..Limits::default()
};
let mut budget = Budget::new(&limits);
assert_eq!(budget.enter(), Ok(()));
assert_eq!(budget.enter(), Err(LimitExceeded::Depth));
budget.leave();
assert_eq!(budget.enter(), Ok(()));
}
#[test]
fn elapsed_deadline_is_reported() {
let limits = Limits {
max_duration: Duration::ZERO,
..Limits::default()
};
let budget = Budget::new(&limits);
std::thread::sleep(Duration::from_millis(2));
assert_eq!(budget.check_time(), Err(LimitExceeded::Time));
}
}