Comptroller rewards can be artificially inflated and drained by manipulating [totalStaked - totalFrozen] (or: wrong rewards calculation)
The Comptroller's reward calculation uses a manipulable effective stake amount (totalStaked minus totalFrozen), allowing a staker to inflate rewards by adding a tiny stake that is not approved for borrowing. An attacker could drain the Comptroller's reward tokens.
Submitted by kenzo
By adding a small of amount of staking to a normal user scenario, and not approving this small amount as a loan for anybody, a staker can gain disproportionate amounts of comptroller rewards, even to the point of draining the contract. For example: Stakers A,B,C stake 100, 65, 20, approve it for borrower Z, then staker B stakes an additional 0.07 DAI, and borrower Z borrows 185. This will result in disproportionate amount of rewards.
As far as I see, this is the main line that causes the inflated amount (deep breath): In calculateRewardsByBlocks, you set:
userManagerData.totalStaked = userManagerContract.totalStaked() - userManagerData.totalFrozen;
Note that a staker can make this amount very small (depending of course on the current numbers of the protocol). (A more advanced attacker might diminish the effect of the current numbers of the protocol by initiating fake loans to himself and not paying them.) This field is then passed to calculateRewards, and passed further to getInflationIndexNew, and further to getInflationIndex. passed to calculateRewards : Comptroller.sol L167
passed to getInflationIndexNew : Comptroller.sol L259
passed to getInflationIndex : Comptroller.sol L238
Now we actually use it in the following line (as effectiveAmount):
return blockDelta * inflationPerBlock(effectiveAmount).wadDiv(effectiveAmount) + inflationIndex;
So 2 things are happening here:
- mul by
inflationPerBlock(effectiveAmount)- uses the lookup table in Comptroller. This value gets bigger as effectiveAmount gets smaller, and if effectiveAmount is in the area of 10**18, we will get the maximum amount of the lookup. - div by
effectiveAmount- as we saw, this can be made small, thereby enlarging the result.
All together, this calculation will be set to curInflationIndex and then used in the following line:
return (curInflationIndex - startInflationIndex).wadMul(effectiveStakeAmount).wadMul(inflationIndex);
Note the curInflationIndex - startInflationIndex: per my POC (see below), this can result in a curInflationIndex which is orders of magnitude larger (200x) than startInflationIndex. This creates a huge inflation of rewards.
Impact
Comptroller rewards can be drained.
Proof of Concept
See the following script for a POC of reward drainage. It is based on the scenario in test/integration/testUserManager:
Stakers A,B,C stake 100, 65, 20, and borrower Z borrows 185. But the difference in my script is that just before borrower Z borrows 185, staker B stakes an additional 0.07 DAI. (This will be the small amount that is totalStaked - totalFrozen).
Then, we wait 11 blocks to make the loan overdue, call updateOverdueInfo so totalFrozen would be updated, and then staker B calls withdrawRewards. He ends up with 873 unionTokens out of the 1000 the Comptroller has been seeded with. And this number can be enlarged by changing the small additional amount that staker B staked.
In this scenario, when calling withdrawRewards, the calculated curInflationIndex will be 215 WAD, while startInflationIndex is 1 WAD, and this is the main issue as I understand it.
File password: “union”. https://pastebin.com/3bJF8mTe
Tools Used
Manual analysis, hardhat
Recommended Mitigation Steps
Are you sure that this line should deduct the totalFrozen?
userManagerData.totalStaked = userManagerContract.totalStaked() - userManagerData.totalFrozen;
Per my tests, if we change it to just
userManagerData.totalStaked = userManagerContract.totalStaked();
Then we are getting normal results again and no drainage. And the var is called just totalStaked… So maybe this is the change that needs to be made? But maybe you have a reason to deduct the totalFrozen. If so, then a mitigation will perhaps be to limit curInflationIndex somehow, maybe by changing the lookup table, or limiting it to a percentage from startInflationIndex ; but even then, there is also the issue of dividing by userManagerData.totalStaked which can be made quite small as the user has control over that.
GalloDaSballo (judge) commented:
Agree with the finding, the warden has found a specific attack that can leak value, while the leak value marks it as Medium Severity, will think over if the economic exploit is big enough to warrant a high severity
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 reward calculation uses a manipulable effective stake amount (totalStaked minus totalFrozen) as a divisor and as input to a lookup table, allowing an attacker to make it very small and inflate rewards.
Impact
Comptroller rewards can be drained, as demonstrated by a proof of concept where a staker obtained 873 out of 1000 seeded tokens.
What the attacker needed
A staker with some stake; a borrower to borrow the staked amount; ability to add a small stake not approved for borrowing
The fix
The line should be changed to not deduct totalFrozen, i.e., set totalStaked to userManagerContract.totalStaked() without subtracting totalFrozen.
How it was found
- Stakers A, B, and C stake 100, 65, and 20 DAI respectively, and approve their stakes for borrower Z.
- Staker B stakes an additional 0.07 DAI, which is not approved for borrowing.
- Borrower Z borrows 185 DAI.
- Wait 11 blocks to make the loan overdue, then call updateOverdueInfo to update totalFrozen.
- Staker B calls withdrawRewards and receives 873 unionTokens out of the 1000 seeded in the Comptroller.
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.
The smart contract calculates rewards based on how much is staked, but it subtracts the amount that is frozen (borrowed but not repaid). By adding a very small stake that is not borrowed, a user can make the effective stake tiny, which makes the reward formula produce a huge number. This lets the attacker take far more rewards than they should, potentially emptying the reward pool.
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.