fix: enable overflow-checks and use checked counter increments - #833
Open
Martins-594 wants to merge 18 commits into
Open
fix: enable overflow-checks and use checked counter increments#833Martins-594 wants to merge 18 commits into
Martins-594 wants to merge 18 commits into
Conversation
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.
Overview
This PR fixes the silent overflow gap between the debug and release WASM builds.
[profile.release]currently leavesoverflow-checksunset, so Cargo defaults it tofalseand the deployed contract can wrapu32counters and proposal timestamps instead of trapping. The PR enablesoverflow-checks = true, replaces every listed unchecked+ 1withchecked_add(...).unwrap_or_else(|| panic_with_error!(e, ContractError::ArithmeticOverflow)), bounds the admin proposal duration, and adds regression tests at the overflow boundary.Related Issue
Closes the bounty issue: [Security] Counters use unchecked
+ 1in a release profile with overflow checks disabled.Changes
🔒 Overflow-Safe Arithmetic
[MODIFY]
src/mint.rscurrent_count + 1andcurrent_total + 1in both mint and batch paths withchecked_add(1).unwrap_or_else(|| panic_with_error!(e, ContractError::ArithmeticOverflow)).[MODIFY]
src/bridge.rscurrent_nonce + 1and all matching count/total increments with the same checked-arithmetic pattern.[MODIFY]
src/governance.rscount + 1,votes_for += 1,votes_against += 1, andstart_time + duration_secondswith checked arithmetic.[MIN_PROPOSAL_DURATION_SECONDS, MAX_PROPOSAL_DURATION_SECONDS]range increate_admin_proposal, rejecting caller-supplied durations outside that range.⚙️ Release Profile & Error Handling
Cargo.tomloverflow-checks = trueto[profile.release], matching the overflow behavior tested in debug builds.src/errors.rsContractError::ArithmeticOverflowvariant for every guarded increment/duration path.🧪 Tests & WASM Size Limit
src/security_test.rsArithmeticOverflow..github/wasm-size-limitoverflow-checks = truedelta.Verification Results
overflow-checks = truein release profile, with WASM size delta recordedCargo.toml; delta+2.1 KB,.github/wasm-size-limitupdated+ 1increments inmint.rs,bridge.rs, andgovernance.rsusechecked_add+ArithmeticOverflowcreate_admin_proposalrejects durations outside a documented[MIN, MAX]range[MIN_PROPOSAL_DURATION_SECONDS, MAX_PROPOSAL_DURATION_SECONDS]enforcedArithmeticOverflowat the boundary for proposal duration and outbound nonceCloses #651