Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Lambda - Comptroller.withdrawRewards: totalFrozen subtracted two times from totalStaked #64

Closed
sherlock-admin opened this issue Nov 4, 2022 · 0 comments

Comments

@sherlock-admin
Copy link
Contributor

sherlock-admin commented Nov 4, 2022

Lambda

medium

Comptroller.withdrawRewards: totalFrozen subtracted two times from totalStaked

Summary

In Comptroller.withdrawRewards, userManagerState.totalFrozen is subtracted two times from totalStaked, leading to a wrong inflation index or even a broken system because the calculation underflows.

Vulnerability Detail

_getUserManagerState calculates the totalStaked like this:

        userManagerState.totalFrozen = userManager.totalFrozen();
        userManagerState.totalStaked = userManager.totalStaked() - userManagerState.totalFrozen;

totalStaked is therefore already set to totalStaked - totalFrozen. However, in withdrawRewards, the following calculation is performed:

uint256 totalStaked_ = userManagerState.totalStaked - userManagerState.totalFrozen;

totalFrozen is therefore subtracted from totalStaked again, meaning we have:

totalStaked_ = userManager.totalStaked() - userManagerState.totalFrozen() - userManagerState.totalFrozen()

Impact

When userManagerState.totalFrozen() is sufficiently large, this can lead to an underflow, meaning that withdrawRewards (and therefore UserManager.stake, UserManager.unstake, and UserManager.withdrawRewards) can never be executed, which bricks the system. But even if it does not underflow, the following update of gInflationIndex will use a wrong value.

Code Snippet

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

Tool used

Manual Review

Recommendation

Do not subtract totalFrozen a second time.

duplicate of #26

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant