fix(precompile): reject truncated and overflowing ABI words in the shielded-pool decoder - #126
Merged
Merged
Conversation
…ielded-pool decoder
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.
Harden the shielded-pool precompile ABI decoder against truncation and overflow
Every offset, length and count the precompile's ABI decoder reads is attacker-chosen calldata, reachable through a plain
eth_call— no signature, no gas spent. Two bug classes let malformed calldata through, and one of them panicked the runtime. This hardens the decoder and reorganizes the crate around the fix.The vulnerabilities
read_offset/read_length/decode_u32narroweduint256words withlow_u32(), keeping only the bottom 32 bits2^32 + 96read back as96; the bounds check validated an offset the sender never wrote and the decoder indexed elsewheredata_start + lengthandcount * 32wrapped; the wrapped sum passed the very bounds check meant to reject it (release builds have nooverflow-checks)Vec::with_capacity(count)reserved from a calldata count before the buffer was known to hold it2^30reserves gigabytes on input that could never decodeThe overflow case was worse than a mis-decode. On a pre-fix build, the wrapping length produced a
slice index starts at 128 but ends at 127panic that aborted execution with awasm unreachabletrap — reachable from an unsigned, gas-freeeth_call, i.e. a remote, unauthenticated runtime panic.The fix
word_to_usizechecks againstusize::MAX(which also keeps 32-bit Wasm and 64-bit native from disagreeing about what's acceptable);decode_u32refuses anything aboveu32::MAX.checked_add/checked_mul/checked_range. Wrapping is impossible; an inverted range can't be constructed.Vec::with_capacityreserves..unwrap()on a slice conversion became a propagated error — unreachable given the surrounding checks, but not worth keeping reachable-by-accident in a precompile.No ABI change. Every selector, parameter and head layout is untouched; well-formed calldata decodes exactly as before. What narrowed is the set of malformed inputs the decoder acts on.
Reorganization
Both files were split into directories by responsibility, no behaviour change:
src/abi/→mod(doc + re-exports),guard(checked arithmetic),scalar(uint32,bytes32),dynamic(bytes,bytes32[],bytes[])src/dispatch/→mod(record_and_dispatch: shared call/gas/result handling + rawdispatch()),origin(from_self/from_caller/unsigned, each building only its origin)abi/guardis the single place calldata arithmetic andusizeconversion live.dispatch/modholds the call→runtime-call conversion, gas charge and result mapping the three modes shared 90% of. Public API is unchanged —mod.rsre-exports, socalls/andlib.rsneed no edits. Tests moved alongside the code they cover.Verification
word_to_usize→low_u32breaks three tests; onechecked_add→wrapping_addbreaks another — the direct demonstration that without the check the crafted length passes the bounds check.eth_call: the same truncating and wrapping calldata reaches the pallet on a pre-fix build and is refused at the ABI layer on the fixed one; well-formed calldata still decodes on both. The pre-fix run reproduced the wasm trap; the fixed run shows zero panics.2^32,2^64,2^255,uint256::MAX; wrapping lengths; over-wideuint32; truncated heads; a 40-call hostile burst — block height checked after each batch to confirm the node keeps producing.