Submitted by sl1, also found by carrotsmuggler, Aymen0909, glorySec, 0xDemon, Tendency, Beepidibop, and 0x70C9
Liquidations in the system are done via LendingTerm._call(), which will auction the loans collateral to repay outstanding debt. A loan can be called only if the term has been offboarded or if a loan missed a periodic partialRepay. To understand the issue, we must understand how the loan is created and how it can be called.
First, when a loan is created through a call to _borrow(), the contract checks if the borrowAmount is lesser than the maxBorrow:
It calculates maxBorrow using params.maxDebtPerCollateralToken. For example, if maxDebtPerCollateralToken is 2000e18, then with 15e18 tokens of collateral, a user can borrow up to 30_000 of CREDIT tokens.
Its also important to understand how liquidations work in the system. If we were to look at _call() function we could notice that it only allows liquidations in two specific cases:
It will only allow to call a position if a term is depreciated or if the loan missed periodic partial repayment.
This approach creates problems and opens up a griefing attack vector.
There are few ways it can go wrong. Lets firstly discuss an issue that will arise even for a non-malicious user.
In order for a user to not get liquidated, he must call partialRepay() before a specific deadline set in terms parameters. If we look at the _partialRepay() function:
We can see, that it enforces user to repay at least params.minPartialRepayPercent, which may not always be enough for a position to stay healthy. By healthy I mean a position that does not breach maxDebtPerCollateralToken value, which is a parameter of a LendingTerm.
Imagine a scenario:
User borrows 30_000 CREDIT with 15 TOKENS of collateral. His debtPerCollateral value is 30_000 / 15 = 2000, which is exactly equal to maxDebtPerCollateralToken.
Now a year has passed, the loanDebt (debt + interest) is 34_500, a user is obligated to repay at least 34_500 * 0.1 = 3450. After partial repayment his debtPerCollateral value is (34500 - 3450) / 15 = 2070. While he breached the maxDebtPerCollateralToken value, his position is not callable, because he did not miss a periodic partial repayment.
Also its worth noting, that currently even if a user missed a periodic partial repayment, he can still make a call to partialRepay() if his position was not yet called. Because of this, it will be easier for such situation to occur, since the interest will be accruing and when a user finally calls partialRepay() he is still only obligated to repay at least minPartialRepayPercent for a position that went even deeper underwater.
Currently, due to a combination of multiple factors, bad debt can essentially occur and it will be impossible to liquidate such position without offboarding a term.
Now lets talk about a potential malicious behaviour that is encouraged in the current implementation. Periodic partial repayments are not enforced for every term, they may or may not be enabled, so the only condition for a liquidation in this case is a depreceated term. This means that basically every position is essentially unliquitable, because partialRepayDelayPassed() will always return false in such case:
A malicious user can abuse this by not repaying his loan or by not adding collateral to his loan when interest accrues above maxDebtPerCollateralToken.
There will be no way to do anything with such positions, the only possible solution would be to offboard a full term. This will obviously damage the protocol, as offboarding a term means calling every position, which subsequently increases a chance of a loss occurring. Also by offboarding a term, lenders will miss out on interest, because every position is force-closed.
Its hard and unreasonable to know every set of parameters that can lead to debtPerCollateral being greater than maxDebtPerCollateralToken even after partialRepay. After a discussion with the sponsor, it was said that ultimately the protocol team will not be the only one to deploy new terms, so its better to enforce a proper liquidation flow in the contract.
Here is the PoC demonstrating this issue. For the sake of simplicity I did not change the protocols test suite configuration, but I just wanted to show that this is possible with any parameters (even the ones expected by the team), because a loan can still be partially repaid even if it missed partial repay deadline. I mentioned this earlier in my report, saying that this is the reason that makes this situation even easier to occur.
Please add this function to LendingTerm.t.sol and run it with forge test --match-test 'testBreakMaxDebtPerCollateralToken' -vv.
In conclusion I want to say that parameters of a LendingTerm are only limited to a logical sort of degree, i.e:
Because of this the situation is bound to occur. Its better to enforce strict rules on the smart contract level.
If periodic partial repays are turned on, the possible solution would be to enforce the maxDebtPerCollateral check in _partialRepay, that will enforce users to repay an amount that would make debtPerCollateralToken value lesser than maxDebtPerCollateralToken value.
Something like this would be sufficient enough:
In the case of partial repays being turned on, its important to have this check in _partialRepay() rather than in _call(), because otherwise almost every position would immediately go underwater and be liquidatable as soon as it gets opened if a user borrowed up to the maximum amount. Its fine to have debtPerCollateralToken greater than maxDebtPerCollateralToken until user makes a partialRepay.
In the case of periodic partial repays being turned off, the check must be in _call(), since there will be no partial repays. Check if debtPerCollateralToken value is lesser than maxDebtPerCollateral token, otherwise liquidate a position, but that would mean that users wont be able to borrow up to maxDebtPerCollateral, since their position will immediately go underwater; it seems to be a matter of trade-offs. This the potential way to modify _call():
eswak (Ethereum Credit Guild) confirmed and commented:
sl1 (warden) commented:
TrungOre (judge) commented:
Note: For full discussion, see here.
