Skip to content

Guard AVX2 entry points and the input length check - #48

Open
sunsided wants to merge 1 commit into
vstroebel:mainfrom
sunsided:fix/avx2-soundness-and-length-overflow
Open

sunsided wants to merge 1 commit into
vstroebel:mainfrom
sunsided:fix/avx2-soundness-and-length-overflow

Conversation

@sunsided

Copy link
Copy Markdown

Two small soundness and robustness fixes, plus a fuzz harness typo.

AVX2 entry points

With simd + benchmark, fdct_avx2 and RgbImageAVX2 (and the BGR/RGBA variants) are safe public items, but they ran AVX2 instructions without checking the CPU. Calling them on a machine without AVX2 is undefined behavior reached from safe code. The criterion benches call them this way.

  • fdct_avx2 is now itself #[target_feature(enable = "avx2")], so calling it from a non-AVX2 context requires unsafe. AVX2Operations::fdct calls it under a SAFETY note: the encoder only selects AVX2Operations after is_x86_feature_detected!("avx2"). The internal dispatch is unchanged and costs nothing extra.
  • The AVX2 image buffers assert! AVX2 support in fill_buffers, once per row. ImageBuffer::fill_buffers is a safe trait method, so it can't be marked unsafe.

Input length on 32-bit targets

Encoder::encode checked data.len() < width * height * bpp with plain usize math. On 32-bit targets such as wasm32, 65535 × 65535 × 4 overflows. Debug builds panic on the multiply. Release builds wrap to a small number, so too-short data passes the check and the encoder panics later on a slice index. The product is now computed with checked_mul and saturates, so the call returns BadImageData.

Fuzz target

encode_rgb_custom_q_table built each table value with a as u16 + (b as u16) << 8, which parses as (a + b) << 8 and drops the low byte. It now uses u16::from_le_bytes.

Tested with cargo test --features simd, a simd,benchmark build, a wasm32-unknown-unknown check, and a cargo check of the fuzz crate. The 32-bit overflow path itself was not run.

fdct_avx2 and RgbImageAVX2 (and its siblings) are reachable as safe
public items with the `benchmark` feature, but ran AVX2 instructions
without checking the CPU, which is undefined behavior on machines
without AVX2. fdct_avx2 is now itself #[target_feature(enable = "avx2")],
so calls from a non-AVX2 context require `unsafe` and the encoder's
dispatch states why it is sound. The AVX2 image buffers assert AVX2
support in fill_buffers, once per row.

Encoder::encode computed width * height * bytes_per_pixel in usize.
On 32-bit targets (e.g. wasm32) large dimensions wrap in release
builds, so too-short data could pass the check and panic later on a
slice index. The product is now checked and saturates, which returns
BadImageData instead.

Also fix operator precedence in the custom quantization table fuzz
target: `a + b << 8` built `(a + b) << 8` and lost the low byte.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant