MerkleResistor: zero coinsPerSecond will brick tranche initialization and withdrawals
The contract calculates coinsPerSecond using integer division, which can result in zero for certain configurations. This causes a division-by-zero revert when computing startTime, preventing tranche initialization and withdrawals of vested tokens.
Submitted by hickuphh3, also found by GimelSec, gzeon, and scaraven
Details & Impact
It is possible for coinsPerSecond to be zero. In these cases, the startTime calculation
uint startTime = block.timestamp + vestingTime - (totalCoins / coinsPerSecond);
will revert from division by zero, preventing initialization, and by extension, withdrawals of vested tokens.
Proof of Concept
We assume vesting time chosen is the maximum (tree.maxEndTime) so that totalCoins = maxTotalPayments. These examples showcase some possibilities for which the calculated coinsPerSecond can be zero.
Example 1: High upfront percentage
pctUpFront = 99(99% up front)totalCoins = 10_000e6(10k USDC)vestingTime = 1 year
uint coinsPerSecond = (totalCoins * (uint(100) - tree.pctUpFront)) / (vestingTime * 100);
// 10_000e6 * (100 - 99) / (365 * 86400 * 100)
// = 0
Example 2: Small reward amount / token decimals
pctUpFront = 0totalCoins = 100_000e2(100k EURS)vestingTime = 180 days
uint coinsPerSecond = (totalCoins * (uint(100) - tree.pctUpFront)) / (vestingTime * 100);
// 100_000e2 * 100 / (180 * 86400 * 100)
// = 0
Recommended Mitigation Steps
Scale up coinsPerSecond by PRECISION, then scale down when executing withdrawals. While it isn’t foolproof, the possibility of coinsPerSecond being zero is reduced significantly.
uint coinsPerSecond = (totalCoins * (uint(100) - tree.pctUpFront)) * PRECISION / (vestingTime * 100);
currentWithdrawal = (block.timestamp - tranche.lastWithdrawalTime) * tranche.coinsPerSecond / PRECISION;
illuzen (FactoryDAO) confirmed, disagreed with severity and commented:
Example 1 = 3, not 0
Justin Goro (judge) decreased severity to Medium and commented:
Reducing severity because rewards are not staked user funds.
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 integer division truncation in the coinsPerSecond calculation, which can yield zero and cause a division-by-zero error when computing startTime.
Impact
Tranche initialization and withdrawals of vested tokens are blocked for affected configurations.
What the attacker needed
Vesting configuration with high upfront percentage or small…; no special privileges required
The fix
Scale up coinsPerSecond by a precision factor and scale down during withdrawals to reduce the chance of zero.
How it was found
- Choose a vesting configuration with a high upfront percentage or small reward amount.
- Calculate coinsPerSecond using the formula, which may result in zero.
- Attempt to initialize the tranche, which computes startTime by dividing by coinsPerSecond.
- The division by zero reverts, preventing initialization and withdrawals.
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.
Smart contracts often use whole numbers, so dividing can lose fractions. If a small number is divided by a large one, the result can be zero. Here, that zero is later used as a divisor, which makes the contract stop working and locks tokens.
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.