Users wouldn’t refund from the lost ETH crowdfunds due to the lack of ETH
The refund calculation could return more than the original contribution due to rounding, causing the refund transfer to revert when the contract lacks sufficient ETH. An attacker could not exploit this for gain, but users would be unable to reclaim their funds after a lost crowdfund.
hansfriese Follow with 0x52 and evan
Originally disclosed on Code4rena
Submitted by hansfriese, also found by 0x52 and evan
After the ETH crowdfunds are lost, contributors wouldn’t refund their funds because the crowdfunds contract doesn’t have enough ETH balance.
Proof of Concept
The core flaw is _calculateRefundAmount() might return more refund amount than the original contribution amount.
function _calculateRefundAmount(uint96 votingPower) internal view returns (uint96 amount) {
amount = (votingPower * 1e4) / exchangeRateBps;
// Add back fee to contribution amount if applicable.
address payable fundingSplitRecipient_ = fundingSplitRecipient;
uint16 fundingSplitBps_ = fundingSplitBps;
if (fundingSplitRecipient_ != address(0) && fundingSplitBps_ > 0) {
amount = (amount * 1e4) / (1e4 - fundingSplitBps_); //@audit might be greater than original contribution
}
}
When users contribute to the ETH crowdfunds, it subtracts the fee from the contribution amount.
File: 2023-04-party\contracts\crowdfund\ETHCrowdfundBase.sol
226: uint16 fundingSplitBps_ = fundingSplitBps;
227: if (fundingSplitRecipient_ != address(0) && fundingSplitBps_ > 0) {
228: uint96 feeAmount = (amount * fundingSplitBps_) / 1e4;
229: amount -= feeAmount;
230: }
During the calculation, it calculates feeAmount first which is rounded down and subtracts from the contribution amount. It means the final amount after subtracting the fee would be rounded up.
So when we calculate the original amount using _calculateRefundAmount(), we might get a greater value.
This shows the detailed example and POC.
- Let’s assume
fundingSplitBps = 1e3(10%), exchangeRateBps = 1e4. - A user contributed
1e18 - 1wei of ETH. After subtracting the fee, the voting power was1e18 - 1 - (1e18 - 1) / 10 = 9 * 1e17 - Let’s assume there are no other contributors and the crowdfund was lost.
- When the user calls
refund(), the refund amount will be9 * 1e17 * 1e4 / 9000 = 1e18in_calculateRefundAmount() - So it will try to transfer 1e18 wei of ETH from the crowdfund contract that contains 1e18 - 1 wei only. As a result, the transfer will revert and the user can’t refund his funds.
function test_refund_reverts() public {
InitialETHCrowdfund crowdfund = _createCrowdfund({
initialContribution: 0,
initialContributor: payable(address(0)),
initialDelegate: address(0),
minContributions: 0,
maxContributions: type(uint96).max,
disableContributingForExistingCard: false,
minTotalContributions: 3 ether,
maxTotalContributions: 5 ether,
duration: 7 days,
fundingSplitBps: 1000, //10% fee
fundingSplitRecipient: payable(_randomAddress()) //recipient exists
});
Party party = crowdfund.party();
uint256 ethAmount = 1 ether - 1; //contribute amount
address member = _randomAddress();
vm.deal(member, ethAmount);
// Contribute
vm.prank(member);
crowdfund.contribute{ value: ethAmount }(member, "");
assertEq(address(member).balance, 0);
assertEq(address(crowdfund).balance, ethAmount); //crowdfund's balance = 1 ether - 1
skip(7 days);
assertTrue(crowdfund.getCrowdfundLifecycle() == ETHCrowdfundBase.CrowdfundLifecycle.Lost);
// Claim refund
vm.prank(member);
uint256 tokenId = 1;
crowdfund.refund(tokenId); //reverts as it tried to withdraw 1 ether
}
Recommended Mitigation Steps
When we subtract the fee in _processContribution(), we should calculate the final amount using 1e4 - fundingSplitBps directly. Then there will be 2 rounds down in _processContribution() and _calculateRefundAmount and the refund amount won’t be greater than the original amount.
if (fundingSplitRecipient_ != address(0) && fundingSplitBps_ > 0) {
amount = (amount * (1e4 - fundingSplitBps_)) / 1e4;
}
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the fee is subtracted using a rounded-down fee amount, and the refund calculation then reverses that with a rounded-up result, so the computed refund can exceed the original contribution.
Impact
Users could be unable to refund their contributions after a lost crowdfund because the transfer reverts when the contract lacks enough ETH.
What the attacker needed
A crowdfund with a funding split fee; the crowdfund ends in the Lost state; a contribution amount that triggers rounding
The fix
The fee should be subtracted by multiplying the amount by (1e4 - fundingSplitBps) and dividing by 1e4, so both calculations round down and the refund never exceeds the original contribution.
How it was found
- Set the funding split to 10% and the exchange rate to 1e4.
- Contribute an amount one wei less than one ether, so the fee is subtracted and voting power is recorded.
- Let the crowdfund fail so it enters the Lost state.
- Call refund, which computes a refund larger than the contract balance and reverts.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you put money into a crowdfund, a small fee is taken. If the crowdfund fails, you should get back what you put in, minus the fee. But because of how the maths rounds numbers, the contract sometimes thinks it owes you slightly more than it actually holds, so the refund fails and your money is stuck.