_vested() claimable amount calculation error
The vesting contract's _vested() function miscalculates the fully vested threshold by ignoring the initial unlock time offset, allowing the claimable amount to exceed the total vesting amount. An attacker could claim more tokens than they are entitled to.
Submitted by bin2chen, also found by bin2chen, immeas, KIntern_NA, ronnyx2017, and deadrxsezzz
The Vesting._vested() method is used to calculate the maximum claimable amount for the current user. The calculation formula is as follows: (_totalAmount * (block.timestamp - _start)) / _duration. If there is an __initialUnlockTimeOffset, it needs to be subtracted from _start before performing the calculation, i.e., _start = _start - __initialUnlockTimeOffset.
function _vested(uint256 _totalAmount) internal view returns (uint256) {
uint256 _cliff = cliff;
uint256 _start = start;
uint256 _duration = duration;
if (_start == 0) return 0; // Not started
if (_cliff > 0) {
_start = _start + _cliff; // Apply cliff offset
if (block.timestamp < _start) return 0; // Cliff not reached
}
@> if (block.timestamp >= _start + _duration) return _totalAmount; // Fully vested
_start = _start - __initialUnlockTimeOffset; // Offset initial unlock so it's claimable immediately
return (_totalAmount * (block.timestamp - _start)) / _duration; // Partially vested
}
The issue with the code snippet above is that the check for being “Fully vested” is incorrect; it does not take into account the __initialUnlockTimeOffset. The correct approach should be: if (block.timestamp >= _start - __initialUnlockTimeOffset + _duration) return _totalAmount;// Fully vested. Resulting in calculations that may be greater than the maximum number _totalAmount
Example: _totalAmount = 500 , duration = 1000 __initialUnlockTimeOffset = 100 start = 1000 block.timestamp= 1999 because block.timestamp < start + duration (1999 < 1000 + 1000) it will not return Fully vested.
Final calculation result: start = start - __initialUnlockTimeOffset = 1000 - 100 = 900. return = (_totalAmount * (block.timestamp - _start)) / _duration = 500 * (1999 - 900) / 1000 = 549.5.
It is greater 49.5 than the maximum _totalAmount=500.
Impact
Users can `claim’ more than they should.
Recommended Mitigation
function _vested(uint256 _totalAmount) internal view returns (uint256) {
uint256 _cliff = cliff;
uint256 _start = start;
uint256 _duration = duration;
if (_start == 0) return 0; // Not started
if (_cliff > 0) {
_start = _start + _cliff; // Apply cliff offset
if (block.timestamp < _start) return 0; // Cliff not reached
}
- if (block.timestamp >= _start + _duration) return _totalAmount; // Fully vested
+ if (block.timestamp >= _start - __initialUnlockTimeOffset + _duration) return _totalAmount; // Fully vested
_start = _start - __initialUnlockTimeOffset; // Offset initial unlock so it's claimable immediately
return (_totalAmount * (block.timestamp - _start)) / _duration; // Partially vested
}
PR here.
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
The fully vested check does not subtract the initial unlock time offset from the start time, so the partial vesting formula can produce a value larger than the total amount.
Impact
Users can claim more tokens than they should, potentially draining the contract's allocated vesting balance.
What the attacker needed
A vesting schedule with initial unlock offset
The fix
The fully vested condition should subtract the initial unlock time offset from the start time before adding the duration, as shown in the recommended mitigation.
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 gradually releases tokens over time. The code that calculates how many tokens are available to claim had a mistake: it didn't account for an early unlock period, so it could let someone claim more tokens than they should have been allocated.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.