Skip to content

[SEC] Implement Safe Math and Overflow Protection for Aggregations #23

Description

@Queenode

🟡 Priority: High
Difficulty: Hard
Estimated Effort: 2-3 days
Relevant Files: contracts/src/lib.rs
Labels: security, priority:high, rust

Requirements

  1. 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.
  2. 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.
  3. 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");
  4. 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).
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions