RustFund

First Flight #36
Beginner FriendlyRust
100 EXP
View results
Submission Details
Severity: low
Invalid

Use checked_add and checked_sub to Prevent Arithmetic Overflows

Summary

The smart contract performs arithmetic operations (addition and subtraction) without using Rust’s built-in safety checks (checked_add and checked_sub). This could lead to arithmetic overflow, causing unintended behavior, especially when dealing with large values of SOL.

Vulnerability Details

In functions such as contribute, refund, and withdraw, direct arithmetic operations are used to update balances:

fund.amount_raised += amount; // Unsafe addition
**ctx.accounts.fund.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.fund.to_account_info().lamports().checked_sub(amount).ok_or(ProgramError::InsufficientFunds)?;

While subtraction uses checked_sub in some cases, addition operations like increasing amount_raised should also use checked_add to prevent integer overflow.

Impact

  • If a contributor donates an excessively large amount, amount_raised could overflow, causing incorrect fund tracking.

  • If a subtraction results in a negative value, it could cause a panic or unexpected behavior in transaction execution.

  • Potentially leads to loss of funds if the contract starts miscalculating balances.

Tools Used

  • Manual code review

  • Rust compiler warnings for unsafe arithmetic

  • Static analysis

Recommendations

  • Use checked_add and checked_sub for all arithmetic operations. Example fix for contribute:

fund.amount_raised = fund.amount_raised.checked_add(amount).ok_or(ErrorCode::CalculationOverflow)?;
  • Ensure all balance updates follow safe arithmetic operations.

Updates

Appeal created

bube Lead Judge 3 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

[Invalid] Arithmetic overflow in `contribute` function

The max value of u64 is: 18,446,744,073,709,551,615 or around 18.4 billion SOL, given that the total supply of SOL on Solana is 512.50M, the scenario when the `contribute` function will revert due to overflow is very very unlikely to happen. Therefore, this is informational finding.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.