Submitted by osmanozdemir1, also found by Aymen0909, 0xbepresent, QiuhaoLi, nonseodion, hash, 0xCiphky (1, 2), TrungOre, ggg_ttt_hhh, rvierdiiev, and InAllHonesty
https://github.com/code-423n4/2023-10-wildcat/blob/c5df665f0bc2ca5df6f06938d66494b11e7bdada/src/libraries/FeeMath.sol#L168-L172
To explain this issue, I will need to mention two things: the fee structure of the protocol and how closing a market works. Lets start with the fees.
Lenders earn interest with two different types of fees: Base interest and delinquency fee. The base interest depends on the annual interest rate of the market and it is paid by the borrower no matter what. On the other hand, the delinquency fee is a penalty fee and it is paid by the borrower if the reserves of the market drop below the required reserves amount.
The important part is how the penalty fees are calculated and Ill be focusing on penalty fees at the moment. Every market has a delinquency grace period, which is a period that is not penalized. If a market is delinquent but the grace period is not passed yet, there is no penalty fee. After the grace period is passed, the penalty fee is applied.
The most crucial part starts now. The penalty fee does not become 0 immediately after the delinquency is cured. The penalty fee is still being applied, even after the delinquency is cured, until the grace tracker counts down to zero.
An example from the protocol gitbook/borrowers section is: Note: this means that if a markets grace period is 3 days, and it takes 5 days to cure delinquency, this means that 4 days of penalty APR are paid.
Here, you can find the code snippet of penalty fee calculation.
Now, lets check how to close a market. Here is the closeMarket() function:
https://github.com/code-423n4/2023-10-wildcat/blob/c5df665f0bc2ca5df6f06938d66494b11e7bdada/src/market/WildcatMarket.sol#L142C1-L161C4
While closing the market, the total debt is calculated and the required amount is transferred to the market. This way all debts are covered. However, the covered total debt is calculated with the current scale factor. As you can see above, this function does not check if there are still penalties to be paid. It should have checked the state.timeDelinquent.
If the state.timeDelinquent > grace period when closing the market (which means the borrower still has more penalties to pay), the scale factor will keep increasing after every state update.
The borrower didnt pay the remaining penalties when closing the market, but who will pay it?
The borrower might intentionally do it to escape from the penalty, or the borrower may not even be aware of the situation.
The protocol does not check if there are remaining penalties and doesnt charge the borrower enough while closing the market.
I provided a coded PoC below that shows every step of the vulnerability.
Borrowers who are aware of this may create charming markets with lower base rate but higher penalty rate (they know they wont pay the half of it). Or the borrowers may not be aware of this, but the protocol doesnt take the required penalty from them. They unintentionally do not pay the penalty, but the lender will have to cover it.
You can use the protocols own test setup to prove this issue:
Below, you can find the test results:
Foundry
I think there might be two different solutions: the easier one and the other.
The easy solution is just not to allow the borrower to close the market until all the penalty fees are accumulated. This can easily be done by checking state.timeDelinquent in the closeMarket() function.
That one is simple, but I dont think it is fair for the borrower, as they will have to pay the base rate as well, for that additional amount of time. Maybe the borrower will be inclined to pay the current debt + future penalties and close the market as soon as possible.
Thats why I think closing the market can still be allowed, even if there are penalties to accumulate. However, the problem is we can not know the exact amount of future penalties due to the compounding mechanism. It will depend on how many times the state is updated while the grace counter counts down.
Therefore, I believe a buffer amount should be added. If the borrowers want to close the market, they should pay current debt + expected future penalties + buffer amount, and the excess amount from the buffer should be transferred back to the borrower after every lender withdraws their funds.
Context
laurenceday (Wildcat) commented:
laurenceday (Wildcat) confirmed
