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:
Comptroller.sol L140
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):
Comptroller.sol L315
So 2 things are happening here:
All together, this calculation will be set to curInflationIndex and then used in the following line:
Comptroller.sol L263
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.
Comptroller rewards can be drained.
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
Manual analysis, hardhat
Are you sure that this line should deduct the totalFrozen?
Comptroller.sol L140
Per my tests, if we change it to just
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.
kingjacob (Union) confirmed
GalloDaSballo (judge) commented:
