Rust Fund

AI First Flight #9
Beginner FriendlyRust
EXP
View results
Submission Details
Severity: medium
Valid

refund() subtracts fund lamports without decrementing fund.amount_raised, breaking withdraw()

Root + Impact

Description

withdraw() relies on the invariant that the fund PDA holds fund.amount_raised lamports above its rent-exempt floor and calls checked_sub(fund.amount_raised). However, refund() subtracts contribution.amount directly from the fund PDA's lamports without decrementing fund.amount_raised. Once contribution.amount is properly tracked, any refund leaves fund.amount_raised overstated, causing withdraw() to revert with InsufficientFunds and trapping remaining funds.

**fund.to_account_info().try_borrow_mut_lamports()? -= amount;
**ctx.accounts.contributor.to_account_info().try_borrow_mut_lamports()? += amount;
@> // Missing: fund.amount_raised = fund.amount_raised.checked_sub(amount)...
contribution.amount = 0;

Risk

Likelihood: Medium

Occurs whenever one or more contributors refund from a fund (once contribution.amount is tracked) and the creator later attempts to withdraw.

Impact: Medium

Desynchronizes fund.amount_raised from the actual lamports in the fund PDA, causing withdraw() to revert with InsufficientFunds and permanently locking remaining non-rent lamports.

Proof of Concept

Two contributors each donate 10 SOL (fund.amount_raised = 20 SOL, fund.lamports = 31 SOL including 11 SOL initial balance). Both contributors refund 10 SOL each, returning fund.lamports to 11 SOL, while fund.amount_raised remains 20 SOL. When the creator calls withdraw(), checked_sub(11 SOL, 20 SOL) fails with InsufficientFunds.

// 1. Two users donate 10 SOL each -> fund.lamports = 31 SOL, fund.amount_raised = 20 SOL
// 2. Both users refund -> fund.lamports = 11 SOL, fund.amount_raised = 20 SOL (stale)
// 3. Creator calls withdraw() -> checked_sub(11 SOL, 20 SOL) reverts with InsufficientFunds

Recommended Mitigation

Decrement fund.amount_raised using checked subtraction inside refund() whenever lamports are returned to a contributor so fund.amount_raised always matches the withdrawable balance.

**fund.to_account_info().try_borrow_mut_lamports()? -= amount;
**ctx.accounts.contributor.to_account_info().try_borrow_mut_lamports()? += amount;
+fund.amount_raised = fund.amount_raised.checked_sub(amount).ok_or(ErrorCode::CalculationOverflow)?;
contribution.amount = 0;
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 3 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[M-03] Fund Creator Can't Withdraw If Someone Has Refunded Their Contribution

# \[H-02] Fund Creator Can't Withdraw If Someone Has Refunded Their Contribution ## Description The `refund` function does not update `fund.amount_raised`, causing an inconsistency between the fund's actual balance and the recorded raised amount. As a result, when the fund creator tries to withdraw funds, the transaction may fail due to insufficient balance, effectively locking funds in the contract. ## Vulnerability Details The issue arises in the `refund` function, which transfers funds back to the contributor but does not update the `amount_raised` field: ```rust pub fn refund(ctx: Context<FundRefund>) -> Result<()> { let amount = ctx.accounts.contribution.amount; if ctx.accounts.fund.deadline != 0 && ctx.accounts.fund.deadline > Clock::get().unwrap().unix_timestamp.try_into().unwrap() { return Err(ErrorCode::DeadlineNotReached.into()); } ctx.accounts.fund.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.fund.to_account_info().lamports() .checked_sub(amount) .ok_or(ProgramError::InsufficientFunds)?; ctx.accounts.contributor.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.contributor.to_account_info().lamports() .checked_add(amount) .ok_or(ErrorCode::CalculationOverflow)?; // Reset contribution amount after refund ctx.accounts.contribution.amount = 0; Ok(()) } ``` The issue becomes evident when the fund creator attempts to withdraw using the following function: ```rust pub fn withdraw(ctx: Context<FundWithdraw>) -> Result<()> { let amount = ctx.accounts.fund.amount_raised; ctx.accounts.fund.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.fund.to_account_info().lamports() .checked_sub(amount) .ok_or(ProgramError::InsufficientFunds)?; ctx.accounts.creator.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.creator.to_account_info().lamports() .checked_add(amount) .ok_or(ErrorCode::CalculationOverflow)?; Ok(()) } ``` Since `amount_raised` is never updated when a refund occurs, the creator will attempt to withdraw more than what actually exists in the fund, causing an insufficient funds error and failing the transaction. ## Impact - If any contributor requests a refund, the total balance in the fund decreases. However, `fund.amount_raised` remains unchanged, leading to an overestimated available balance. - When the fund creator calls `withdraw`, they attempt to transfer `fund.amount_raised`, which no longer matches the actual available balance. - This results in a failed transaction, effectively locking funds in the contract since the withdraw function will always fail if refunds have been processed. ## Proof of Concept This issue is not currently caught by tests because the `contribute` function itself has a bug (not updating `contribution.amount`), preventing the refund function from executing properly. Once the contribute function is fixed, the issue will be clearly visible in test cases. ## Recommendations The `refund` function must update `fund.amount_raised` to ensure the contract state reflects the actual balance after refunds. ### Fixed Code: ```diff pub fn refund(ctx: Context<FundRefund>) -> Result<()> { let amount = ctx.accounts.contribution.amount; if ctx.accounts.fund.deadline != 0 && ctx.accounts.fund.deadline > Clock::get().unwrap().unix_timestamp.try_into().unwrap() { return Err(ErrorCode::DeadlineNotReached.into()); } ctx.accounts.fund.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.fund.to_account_info().lamports() .checked_sub(amount) .ok_or(ProgramError::InsufficientFunds)?; ctx.accounts.contributor.to_account_info().try_borrow_mut_lamports()? = ctx.accounts.contributor.to_account_info().lamports() .checked_add(amount) .ok_or(ErrorCode::CalculationOverflow)?; // Reset contribution amount after refund ctx.accounts.contribution.amount = 0; + // Fix: Decrease the fund's recorded amount_raised + let fund = &mut ctx.accounts.fund; + fund.amount_raised = fund.amount_raised.checked_sub(amount).ok_or(ErrorCode::CalculationOverflow)?; Ok(()) } ```

Support

FAQs

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

Give us feedback!