Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
decode_listanddecode_byte_vectorsize aVecdirectly from the vector's LEB128 length prefix, which is fully attacker-controlled (up tou32::MAX):A tiny module that declares a vector of
0xFFFFFFFFelements 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_listbacks almost every section (types, functions, imports, exports, tables, memories, globals, elements, data, tags), anddecode_byte_vector/decode_nameback names and data segments, so the input needed is minimal.Proof of concept
A 15-byte module (
\0asm, version, function section id3, then vector length0xFFFFFFFF):decoding it aborts:
The
decode_byte_vectorpath (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_vectornow streams viatake()+io::copyand reportsUnexpectedEofwhen the declared length exceeds the available bytes.With the fix, the PoC above returns:
and the largest allocation requested drops from 16 GiB to 4 KiB.
Tests
huge_vector_length_does_not_preallocateintests/error_types.rs— au32::MAXfunction-section length now fails with a graceful element-levelParseElementerror instead of aborting.ulimit -von the pre-fix code.cargo build --workspace,cargo clippy --all-targets --all-features --workspace(with-Dwarnings), and the unit + integration +error_typessuites all pass (190 tests). Thez_specsuite needs the spec-testsuite submodule (CI checks out withsubmodules: true), so it wasn't run in this sandbox.Validation: