Error in Updating _checkpoint in the increaseUnlockTime Function
The increaseUnlockTime function incorrectly set the old lock's end time to the new unlock time instead of the previous end time, causing wrong voting power calculations. An attacker could exploit this to obtain incorrect voting power at a given block or distort total voting power.
Submitted by Aymen0909, also found by 0xf15ers, 0xSky, auditor0517, bin2chen, CertoraInc, csanuragjain, JohnSmith, scaraven, tabish, wagmi, and yixxas
The potentiel impact of this error are :
- Give wrong voting power to a user at a given block.
- Give wrong total voting power at a given block.
- Give wrong total voting power.
Proof of Concept
The error occured in this line : https://github.com/code-423n4/2022-08-fiatdao/blob/main/contracts/VotingEscrow.sol#L513
In the increaseUnlockTime function the oldLocked.end passed to the function _checkpoint is wrong as it is the same as the new newLock end time (called unlock_time) instead of being equal to oldUnlockTime .
In the given CheckpointMath.md file it is stated that checkpoint details for increaseUnlockTime function should be :
Lock | amount | end |
old | owner.delegated | owner.end |
new | owner.delegated | T |
BUT with this error you get a different checkpoint details :
Lock | amount | end |
old | owner.delegated | T |
new | owner.delegated | T |
The error is illustrated in the code below :
LockedBalance memory locked_ = locked[msg.sender];
uint256 unlock_time = _floorToWeek(_unlockTime); // Locktime is rounded down to weeks
/* @audit comment
the unlock_time represent the newLock end time
*/
// Validate inputs
require(locked_.amount > 0, "No lock");
require(unlock_time > locked_.end, "Only increase lock end");
require(unlock_time <= block.timestamp + MAXTIME, "Exceeds maxtime");
// Update lock
uint256 oldUnlockTime = locked_.end;
locked_.end = unlock_time;
/* @audit comment
The locked_ end time is update from oldUnlockTime ==> unlock_time
*/
locked[msg.sender] = locked_;
if (locked_.delegatee == msg.sender) {
// Undelegated lock
require(oldUnlockTime > block.timestamp, "Lock expired");
LockedBalance memory oldLocked = _copyLock(locked_);
oldLocked.end = unlock_time;
/* @audit comment
The oldLocked.end is set to unlock_time instead of oldUnlockTime
*/
_checkpoint(msg.sender, oldLocked, locked_);
}
The impact of this is when calculating the userOldPoint.bias in the _checkpoint function you get an incorrect value equal to userNewPoint.bias (because oldLocked.end == _newLocked.end which is wrong).
240 userOldPoint.bias =
241 userOldPoint.slope *
242 int128(int256(_oldLocked.end - block.timestamp));
The wrong userOldPoint.bias value is later used to calculate and update the bias value for the new point in PointHistory.
359 lastPoint.bias =
360 lastPoint.bias +
361 userNewPoint.bias -
362 userOldPoint.bias;
372 pointHistory[epoch] = lastPoint;
And added to that the wrong oldLocked.end is used to get oldSlopeDelta value which is used to update the slopeChanges.
271 oldSlopeDelta = slopeChanges[_oldLocked.end];
380 oldSlopeDelta = oldSlopeDelta + userOldPoint.slope;
381 if (_newLocked.end == _oldLocked.end) {
382 oldSlopeDelta = oldSlopeDelta - userNewPoint.slope; // It was a new deposit, not extension
383 }
384 slopeChanges[_oldLocked.end] = oldSlopeDelta;
As the PointHistory and the slopeChanges values are used inside the functions balanceOfAt() , _supplyAt(), totalSupply(), totalSupplyAt() to calculate the voting power, this error could give wrong voting power at a given block of a user or can give wrong total voting power.
Recommended Mitigation Steps
The line 513 in the VotingEscrow.sol contract :
513 oldLocked.end = unlock_time;
Need to be replaced with the following :
513 oldLocked.end = oldUnlockTime;
lacoop6tu (FIAT DAO) confirmed, but disagreed with severity and commented:
As majority of wardens reported, this is Medium finding
2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
Justin Goro (judge) decreased severity to Medium and commented:
The severity will be downgraded but otherwise a good report.
IllIllI (warden) reviewed mitigation:
The sponsor addressed the finding with the fix for Issue 5. The fix properly changes the code to match the invariants specification, and matches the logical expectation that the ‘old’ field uses the ‘old’ timestamp.
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 code assigned the new unlock time to the old lock's end field instead of preserving the previous end time, leading to incorrect checkpoint calculations.
Impact
This could give a user wrong voting power at a given block or wrong total voting power, but the severity was rated Medium because assets were not at direct risk.
What the attacker needed
A user must call increaseUnlockTime
The fix
The fix was to set oldLocked.end to oldUnlockTime instead of unlock_time, and this was confirmed to be addressed.
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.
In a voting system, a user's voting power changes over time. The contract keeps a history of these changes. A mistake in updating that history meant the system recorded the wrong starting point for the change, so the calculated voting power could be too high or too low. This could let someone have more voting power than they should.
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.