Rust Fund

AI First Flight #9
Beginner FriendlyRust
EXP
View results
Submission Details
Impact: low
Likelihood: low
Invalid

Unchecked addition on fund.amount_raised in contribute

Description

contribute (programs/rustfund/src/lib.rs) increments the fund counter with a plain +=, while every other arithmetic operation in the program uses checked_add / checked_sub and maps failure to ErrorCode::CalculationOverflow:

// contribute
fund.amount_raised += amount; // unchecked
// refund / withdraw — the rest of the program
.checked_sub(amount).ok_or(ProgramError::InsufficientFunds)?;
.checked_add(amount).ok_or(ErrorCode::CalculationOverflow)?;

In release builds (anchor build compiles with --release) Rust integer overflow wraps silently unless overflow-checks = true is set in the release profile. The workspace Cargo.toml [profile.release] does not enable it, so the += above is a wrapping add on-chain.

Risk

Impact: Low — on wrap, amount_raised would become a small number: the goal check (once added) would report an unsuccessful campaign, and withdraw would move only the wrapped remainder, leaving the real contributions stranded in the PDA. The program defines CalculationOverflow precisely to prevent this and applies it in every other arithmetic path, so this is an inconsistency in the overflow policy rather than an exploitable path today.

Likelihood: Low — reaching u64::MAX lamports (~1.8e19, ≈ 18.4 billion SOL) exceeds total SOL supply; not reachable with real balances.

Proof of Concept

Not reproducible with realistic balances on a validator. The wrapping behaviour itself in release mode:

// cargo test --release (overflow-checks off, same as the on-chain build)
#[test]
fn wrapping_add_in_release() {
let mut amount_raised: u64 = u64::MAX - 1;
let amount: u64 = 2;
amount_raised += amount; // no panic in release
assert_eq!(amount_raised, 0); // counter silently reset
}

Recommended Mitigation

Use the same checked arithmetic as the rest of the program:

fund.amount_raised = fund.amount_raised
.checked_add(amount)
.ok_or(ErrorCode::CalculationOverflow)?;

Optionally also enable overflow checks for the on-chain build so any future unchecked operation fails loudly:

[profile.release]
overflow-checks = true
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 1 hour ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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

Give us feedback!