A high severity integer overflow vulnerability exists in the rustfund smart contract where the amount_raised field can overflow due to unchecked addition when users contribute funds. This occurs in the contribute function that uses the += operator without any overflow checks, potentially allowing an attacker to reset the counter and drain funds from the contract.
In the contribute function at line 50 in programs/rustfund/src/lib.rs, the contract increments the amount_raised field using the += operator:
This operation is performed without any bounds checking. In Rust, arithmetic operations on integer types will wrap around when they overflow in release mode without triggering any errors. For a u64 type, if the current value plus the added amount exceeds 2^64-1 (18,446,744,073,709,551,615), the value will wrap around to a small number or zero.
Solana programs should use checked arithmetic operations like checked_add() to detect overflows and handle them appropriately, rather than allowing silent wraparound which can lead to severe vulnerabilities.
This vulnerability could be exploited by a malicious actor to:
Reset the fund's amount_raised counter to a very small value
Withdraw more funds than they should have access to
Disrupt the accounting of the crowdfunding contract
Since this affects the core accounting mechanism of the contract and could result in direct fund loss, this is classified as a HIGH severity vulnerability.
The following test demonstrates the vulnerability by:
Creating a fund and making an initial contribution
Calculating the exact amount needed to cause an overflow
Showing how this would reset the fund's counter
Add to rustfund.ts
Test Output:
X-Ray Static Analysis Tool - Used to initially detect the integer overflow vulnerability
Anchor Test Framework - Used to build and execute the proof of concept
Replace the unchecked addition with a checked version:
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.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.