`Comptroller::withdrawRewards` accounting error results in incorrect inflation index
In Comptroller::withdrawRewards, totalFrozen was subtracted twice from totalStaked, causing gInflationIndex to be updated with an incorrect value. If more than half of totalStaked is frozen, the function reverts, blocking stake, unstake and withdrawRewards.
Source: https://github.com/sherlock-audit/2022-10-union-finance-judging/issues/26
Found by
Jeiwan, Lambda, dipp, lemonmon
Summary
In Comptroller::withdrawRewards, totalFrozen was subtracted twice from totalStaked, which will update Comptroller::gInflationIndex based on incorrect information.
Also, if more than half of totalStaked is frozen, the Comptroller::withdrawRewards will revert, so no one can call UserManager::stake or UserManager::unstake.
Vulnerability Detail
In Comptroller:withdrawRewards calls _getUserManagerState and saves it as userManagerState:
Note that returned value of userManagerState.totalStaked is equivalent to userManager.totalStaked() - userManager.totalFrozen():
However, in the Comptroller::withdrawRewards function, the returned value userManagerState.totalStaked will be subtracted by totalFrozen again:
So, the totalStaked_ is equivalent to userManager.totalStaked() - 2 * userManager.totalFrozen(), which was used to calculate gInflationIndex in the line 261 of Comptroller.sol. It will result in incorrect update of the gInflationIndex.
Moreover, if more than half of total staked values are frozen, the line 260 in Comptroller.sol will revert from underflow. The Comptroller::withdrawRewards function is used in UserManager::stake, UserManager::unstake and UserManager::withdrawRewards, thus all of these function will stop working when the condition is met.
Impact
- Updated to incorrect
gInflationIndex - Revert when more than half of total staked is frozen
Code Snippet
https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L248 https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L306-L317 https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L260-L261
Tool used
Manual Review
Recommendation
the following line
should be:
uint256 totalStaked_ = userManagerState.totalStaked;
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 subtracts totalFrozen from a value that already had totalFrozen deducted, causing double subtraction and underflow.
Impact
The inflation index is updated incorrectly, and if more than half of total staked is frozen, stake, unstake and withdrawRewards revert.
What the attacker needed
More than half of total staked is frozen
The fix
The code should use userManagerState.totalStaked directly without subtracting totalFrozen again.
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 smart contract calculates a reward index using the total amount staked. It mistakenly subtracts the frozen amount twice, so the index is wrong. If too much is frozen, the calculation goes negative and the function stops working, blocking users from staking or unstaking.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.