🟡 Priority: High
Difficulty: Hard
Estimated Effort: 2-3 days
Relevant Files: contracts/src/lib.rs
Labels: security, priority:high, rust
Requirements
-
Explicit Safe Math
- While Rust panics on integer overflow in debug mode, it performs two's complement wrapping in release mode unless explicitly configured otherwise (the
Cargo.toml currently has overflow-checks = true for release, which is good, but explicit code handles it better).
- Replace standard operators (
+, *) with checked arithmetic methods (checked_add, checked_mul) when calculating pool sizes and totals.
-
Vulnerable Areas
pool_size = contribution_amount * frozen_count;
current_contribution + amount
- If an admin maliciously initializes a group with
contribution_amount = i128::MAX, the multiplication for pool_size will overflow.
-
Implementation
- Update
payout():
let pool_size = contribution_amount.checked_mul(frozen_count).expect("Math overflow in pool calculation");
- Update
contribute():
let new_total = current_contribution.checked_add(amount).expect("Math overflow in contribution sum");
-
Validation on Initialize
- Add bounds checking in
initialize().
- Prevent unrealistic contribution amounts (e.g.,
contribution_amount <= 0 should panic).
- Ensure
contribution_amount is within a reasonable maximum bounds (e.g., < 1_000_000_000_000_000 stroops).
-
Testing
- Unit test: Initialize with negative amount (panics).
- Unit test: Initialize with zero amount (panics).
- Unit test: Simulate a scenario that would cause
pool_size to exceed i128::MAX and verify the expect() panic triggers cleanly instead of wrapping.
- Target: 100% coverage on boundary conditions.
🟡 Priority: High
Difficulty: Hard
Estimated Effort: 2-3 days
Relevant Files:
contracts/src/lib.rsLabels:
security,priority:high,rustRequirements
Explicit Safe Math
Cargo.tomlcurrently hasoverflow-checks = truefor release, which is good, but explicit code handles it better).+,*) with checked arithmetic methods (checked_add,checked_mul) when calculating pool sizes and totals.Vulnerable Areas
pool_size = contribution_amount * frozen_count;current_contribution + amountcontribution_amount = i128::MAX, the multiplication forpool_sizewill overflow.Implementation
payout():contribute():Validation on Initialize
initialize().contribution_amount <= 0should panic).contribution_amountis within a reasonable maximum bounds (e.g.,< 1_000_000_000_000_000stroops).Testing
pool_sizeto exceedi128::MAXand verify theexpect()panic triggers cleanly instead of wrapping.