Skip to content

fix: bound speculative allocation on untrusted vector lengths - #50

Open
chiliec wants to merge 1 commit into
agis:mainfrom
chiliec:fix-untrusted-vector-length-prealloc-dos
Open

chiliec wants to merge 1 commit into
agis:mainfrom
chiliec:fix-untrusted-vector-length-prealloc-dos

Conversation

@chiliec

@chiliec chiliec commented Sep 2, 2026

Copy link
Copy Markdown

Problem

decode_list and decode_byte_vector size a Vec directly from the vector's LEB128 length prefix, which is fully attacker-controlled (up to u32::MAX):

// decode_list
let len = decode_u32(reader)?;
let mut items = Vec::with_capacity(len.try_into().unwrap());

// decode_byte_vector
let len = decode_u32(reader)?;
let mut b = vec![0u8; len.try_into().unwrap()];

A tiny module that declares a vector of 0xFFFFFFFF elements makes the decoder request gigabytes of memory before reading a single element, aborting the whole process on the allocation failure. This is a remote OOM DoS for anything that decodes untrusted WebAssembly modules.

decode_list backs almost every section (types, functions, imports, exports, tables, memories, globals, elements, data, tags), and decode_byte_vector/decode_name back names and data segments, so the input needed is minimal.

Proof of concept

A 15-byte module (\0asm, version, function section id 3, then vector length 0xFFFFFFFF):

00 61 73 6D 01 00 00 00 03 06 FF FF FF FF 0F

decoding it aborts:

memory allocation of 17179869180 bytes failed   # 4294967295 * size_of::<TypeIdx>()
SIGABRT

The decode_byte_vector path (custom-section name) is even more direct — vec![0u8; len] commits real pages. Under a 512 MiB virtual-memory cap it deterministically aborts on the vulnerable code and returns a clean error on the fix.

Fix

Don't trust the length prefix for allocation size. Cap the speculative capacity at a small bound (MAX_PREALLOC_ELEMENTS = 1024) and let the vector grow as elements are actually parsed — the real length is still enforced by the parse loop / stream length. decode_byte_vector now streams via take() + io::copy and reports UnexpectedEof when the declared length exceeds the available bytes.

With the fix, the PoC above returns:

Err(DecodeFunctionSection(DecodeList(ParseElement { position: 0, source: ... UnexpectedEof })))

and the largest allocation requested drops from 16 GiB to 4 KiB.

Tests

  • Added huge_vector_length_does_not_preallocate in tests/error_types.rs — a u32::MAX function-section length now fails with a graceful element-level ParseElement error instead of aborting.
  • Verified RED→GREEN, including a deterministic reproduction of the abort under ulimit -v on the pre-fix code.
  • cargo build --workspace, cargo clippy --all-targets --all-features --workspace (with -Dwarnings), and the unit + integration + error_types suites all pass (190 tests). The z_spec suite needs the spec-testsuite submodule (CI checks out with submodules: true), so it wasn't run in this sandbox.

Validation:

cargo clippy --all-targets --all-features --workspace
cargo test --lib --test tests --test error_types

decode_list and decode_byte_vector allocated a Vec sized directly from the
LEB128 vector-length prefix, which is attacker-controlled up to u32::MAX.
A ~15-byte module declaring a vector of 0xFFFFFFFF elements makes the
decoder request gigabytes up front (e.g. 4294967295 * size_of::<T>) before
reading a single element, aborting the process with an allocation failure
(remote OOM DoS for any service decoding untrusted modules).

Cap the pre-allocation and let the vector grow as elements are actually
read. decode_byte_vector now streams via take()+io::copy and reports EOF
when the declared length exceeds the available bytes.
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