wasmparser: Relocation range end can wrap on 32-bit targets#2497
Merged
dicej merged 2 commits intobytecodealliance:mainfrom Apr 17, 2026
Merged
Conversation
RelocationEntry::relocation_range() derived a Range<usize> from untrusted relocation offsets using `self.offset as usize` and start + self.ty.extent(). On 32-bit targets, large parsed offsets can overflow the end computation, causing a debug-build panic or release-build wraparound instead of rejection. That can be reproduced with a 30-byte wasm module consisting of a header and a single `reloc.CODE` custom section with one entry: | Field | Value | Encoding | |--------|---------------------------|--------------------------------| | type | `FunctionIndexLeb` (0x00) | extent = 5, no addend | | offset | `0xFFFF_FFFC` | LEB128: `FC FF FF FF 0F` | | index | 0 | LEB128: `00` | $ cargo build -p wasmparser --example reloc_overflow --target wasm32-wasip1 $ wasmtime target/wasm32-wasip1/debug/examples/reloc_overflow.wasm parsed: type=FunctionIndexLeb, offset=0xFFFFFFFC, extent=5 thread 'main' panicked at crates/wasmparser/src/readers/core/reloc.rs:246:33: attempt to add with overflow In release mode the panic becomes a silent wraparound: $ cargo build --release -p wasmparser --example reloc_overflow --target wasm32-wasip1 $ wasmtime target/wasm32-wasip1/release/examples/reloc_overflow.wasm parsed: type=FunctionIndexLeb, offset=0xFFFFFFFC, extent=5 relocation_range() = 0xFFFFFFFC..0x1 BUG: range end < start due to overflow Autonomously found by https://swival.dev /scan
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.
RelocationEntry::relocation_range() derived a Range from untrusted relocation offsets using
self.offset as usizeand start + self.ty.extent().On 32-bit targets, large parsed offsets can overflow the end computation, causing a debug-build panic or release-build wraparound instead of rejection.
That can be reproduced with a 30-byte wasm module consisting of a header and a single
reloc.CODEcustom section with one entry:FunctionIndexLeb(0x00)0xFFFF_FFFCFC FF FF FF 0F00$ cargo build -p wasmparser --example reloc_overflow --target wasm32-wasip1 $ wasmtime target/wasm32-wasip1/debug/examples/reloc_overflow.wasm
parsed: type=FunctionIndexLeb, offset=0xFFFFFFFC, extent=5
thread 'main' panicked at crates/wasmparser/src/readers/core/reloc.rs:246:33: attempt to add with overflow
In release mode the panic becomes a silent wraparound:
$ cargo build --release -p wasmparser --example reloc_overflow --target wasm32-wasip1 $ wasmtime target/wasm32-wasip1/release/examples/reloc_overflow.wasm
parsed: type=FunctionIndexLeb, offset=0xFFFFFFFC, extent=5 relocation_range() = 0xFFFFFFFC..0x1
BUG: range end < start due to overflow
Autonomously found by https://swival.dev /scan