Submitted by 0xRobocop, also found by mojito_auditor, KIntern_NA, xuwinnie, and rvierdiiev
The repay function in the BigBang contract is used for users to repay their loans. The mechanics of the function are simple:
The internal function _repay handles the state changes regarding the user debt. Specifically, fees are taken by withdrawing all the users debt from yieldbox and burning the proportion that does not correspond to fees. The fees stay in the contracts balance to later be taken by the penrose contract. The logic can be seen here:
The problem is that the function burns less than it should, hence, taking more fees than it should.
I will provide a symbolic proof and coded proof to illustrate the issue. To show the issue clearly we will assume that there is no opening fee, and that the yearly fee is of 10%. Hence, for the coded PoC it is important to change the values of bigBangEthDebtRate and borrowOpeningFee:
How much fees do the protocol should take?. The answer of this question can be represented in the following equation:
ProtocolFees = CurrentUserDebt - OriginalUserDebt
The fees accrued for the protocol is the difference of the current debt of the user and the original debt of the user. If we examine the implementation of the _repay function we found the next:
The important variables are:
At the following line the contract takes amount which is the real user debt from yield box:
Then it burns some tokens:
But how toBurn is calculated?:
toBurn is just part. Hence, the contract is computing the fees as:
ProtocolFees = amount - part. Rewriting this with the first equation terms will be:
ProtocolFees = CurrentDebt - part.
But it is part equal to OriginalDebt?. Remember that part is not the actual debt, is just the part of the real debt to be paid, this can be found in a comment in the code:
So they are equal only for the first borrower, but for the others this wont be the case since the relation of elastic and part wont be 1:1 due to accrued fees, making part < OriginalDebt, and hence the protocol taking more fees. Lets use some number to showcase it better:
When B repays its debt, he needs to repay 1,100 units. Then the contract burns the proportion that was real debt, the problem as stated above is that the function burns the part and not the original debt, hence the contract will burn 909.09 units. Hence it took:
1100 - 909.09 = 190.91 units
The contract took 190.91 in fees rather than 100 units.
Follow the next steps to run the coded PoC:
Hardhat
Not only store the user borrow part but also the original debt which is debtAsked + openingFee. So, during repayment the contract can compute the real fees generated.
0xRektora (Tapioca) confirmed
