Skip to content
Smart contracts Audit finding Intermediate

`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.

Jeiwan Follow with Lambda, dipp and 1 other

Originally disclosed on Sherlock

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:

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L248

Note that returned value of userManagerState.totalStaked is equivalent to userManager.totalStaked() - userManager.totalFrozen():

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L306-L317

However, in the Comptroller::withdrawRewards function, the returned value userManagerState.totalStaked will be subtracted by totalFrozen again:

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L260-L261

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

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/token/Comptroller.sol?plain=1#L248

should be:

Solidity
        uint256 totalStaked_ = userManagerState.totalStaked;

The original report as published on Sherlock, by Jeiwan. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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.

More by Jeiwan

View all

69 more disclosed reports, best first.

More from Union Finance

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.