Large number of steps in a vesting may lead to loss of beneficiary funds or uneven vesting distribution
A vesting contract calculates a per-step release rate by integer division, which can round down to zero or one when the number of steps is very large. This can cause beneficiaries to lose their vested tokens or receive an uneven distribution.
Submitted by gesha17
https://github.com/code-423n4/2024-12-secondswap/blob/main/contracts/SecondSwap_StepVesting.sol#L298
Some token owners may create StepVesting contracts with very large numbers of steps, to simulate a continous vesting. This can lead to two edge cases for the releaseRate of a beneficiary:
- Consider a scenario where a vesting is created that has less tokens than there are number of steps in the plan. Then the releaseRate would be calculated to 0, so users will essentially lose their vestings. The severity of this would depend on how many steps there are and how much the vesting is. Suppose there are 100000000 steps in a vesting plan with duration 1 year. A user receives a vesting for 90e6 USDC. So the releaseRate will be calculated as 90e6/100000000 = 0.9, which is rounded down to 0. So the user will lose his tokens.
- This can also lead to a situation where the release rate becomes 1 and a large amount of the tokens get vested at the very last step, creating a very uneven vesting distribution. A requirement is that the number of steps is more than tokenAmount/2. So if the amount of steps is say 10000000 USDC or 10e6 USDC, the number of steps has to be more than 5e6 USDC for the bug to occur. So likelihood is very low.
This is only a real concern with tokens that have very low decimals, which are in scope as per the competition page.
Protocol function is impacted as beneficiaries will not get their liquidity released correctly, so their funds will essentially be temporarily locked.
Proof of Concept
Suppose a scenario where an issuer wants to continuosly release 90000 tokens over the period of 1 day and decides to set 50000 steps.
start time = 0 end time = 86400 numSteps = 50000 amount = 90000 tokens
Let’s analyse how releaseRate will be calculated for the beneficiary:
https://github.com/code-423n4/2024-12-secondswap/blob/main/contracts/SecondSwap_StepVesting.sol#L298
function _createVesting(
address _beneficiary,
uint256 _totalAmount,
uint256 _stepsClaimed,
bool _isInternal
) internal {
...
if (numOfSteps - _vestings[_beneficiary].stepsClaimed != 0) {
// @audit release rate calculation may round down to 1 or 0
_vestings[_beneficiary].releaseRate =
(_vestings[_beneficiary].totalAmount - _vestings[_beneficiary].amountClaimed) /
(numOfSteps - _vestings[_beneficiary].stepsClaimed);
}
...
}
stepDuration = 86400/50000 = 1 releaseRate = 90000/50000 = 1
So at step 49999, total vested is 49999 and at step 50000, the rest 40001 tokens get vested at step 50000 which creates a very uneven distribution.
Also, as we can see if the amount of tokens is less than the number of steps, the releaseRate will round down to 0.
Recommended mitigation steps
Mitigation is non-trivial as it would require partially changing protocol design - instead of calculating release rate, calculate release amount based on how many steps are passed in the claimable() function.
calvinx (SecondSwap) commented:
This is a low likelihood scenario.
Due to the low likelihood, this could be low/med.
Clarify in one or two statements what is the impact, and why would you think it is high?
Otherwise, this will be set as low.
Hello Judge,
The impact is high because users will basically lose their vested tokens since releaseRate is rounded down to 0. I don’t think likelihood can be set to low either because a malicious user can abuse this bug to create listings with carefully selected amount such that releaseRate rounds down to 0. An honest user would buy the vestings but then find out that in fact they bought nothing.
Based on the input above, this is a valid medium.
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 when calculating the per-step release rate, without ensuring the rate is at least one or handling remainders.
Impact
Beneficiaries could lose their vested tokens if the release rate rounds to zero, or receive a very uneven distribution if it rounds to one; the likelihood is low and only affects tokens with very low decimals.
What the attacker needed
Token with very low decimals; vesting with many steps relative to amount
The fix
The release amount should be calculated based on the number of steps passed in the claimable function instead of a fixed release rate.
How it was found
- Create a vesting with a very large number of steps relative to the token amount.
- Observe that the release rate is calculated as total amount divided by number of steps.
- See that the rate rounds down to zero or one, causing loss or uneven distribution.
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 vesting contract releases tokens gradually over many steps. The amount released per step is calculated by dividing the total tokens by the number of steps. If there are more steps than tokens, the division rounds down to zero, so the user gets nothing. If the division rounds to one, most tokens are released only at the final step.