Bad debt isn't cleared when `earningsAccumulator` is lower than a fixed-pool bad debt
The clearBadDebt function only clears a fixed-pool bad debt if the earnings accumulator is at least as large as the debt, so when the accumulator is slightly lower no debt is cleared. This leaves bad debt in the market, creating a discrepancy between tracked and actual funds that could lead to a bank run.
MahdiKarimi Follow with santipu_
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-04-interest-rate-model-judging/issues/66
The protocol has acknowledged this issue.
Found by
mahdikarimi, santipu_
Summary
When the bad debt from a fixed pool must be cleared but the earningsAccumulator value is slightly lower than the debt, it won't clear any amount of debt. If the bad debt amount is big enough, this may cause a bank run, and the last users to withdraw won't be able to because of this uncleared bad debt.
Vulnerability Detail
When a loan is liquidated and it has more debt than collateral, that extra debt (bad debt) must be cleared at the end of the liquidation to avoid a discrepancy between the tracked funds and the actual funds. The function in charge of clearing the bad debt is the following:
function clearBadDebt(address borrower) external {
if (msg.sender != address(auditor)) revert NotAuditor();
floatingAssets += accrueAccumulatedEarnings();
Account storage account = accounts[borrower];
uint256 accumulator = earningsAccumulator;
uint256 totalBadDebt = 0;
uint256 packedMaturities = account.fixedBorrows;
uint256 maturity = packedMaturities & ((1 << 32) - 1);
packedMaturities = packedMaturities >> 32;
while (packedMaturities != 0) {
if (packedMaturities & 1 != 0) {
FixedLib.Position storage position = fixedBorrowPositions[maturity][borrower];
uint256 badDebt = position.principal + position.fee;
>> if (accumulator >= badDebt) {
RewardsController memRewardsController = rewardsController;
if (address(memRewardsController) != address(0)) memRewardsController.handleBorrow(borrower);
accumulator -= badDebt;
totalBadDebt += badDebt;
floatingBackupBorrowed -= fixedPools[maturity].repay(position.principal);
delete fixedBorrowPositions[maturity][borrower];
account.fixedBorrows = account.fixedBorrows.clearMaturity(maturity);
emit RepayAtMaturity(maturity, msg.sender, borrower, badDebt, badDebt);
}
}
packedMaturities >>= 1;
maturity += FixedLib.INTERVAL;
}
// ...
}
The clearBadDebt function first clears the bad debt on the fixed pools using the earningsAccumulator on the market. However, when the accumulator is slightly lower than the bad debt on a fixed pool, it should clear the maximum debt possible but it won't clear any bad debt.
Imagine the following scenario:
- After a loan is liquidated and the full collateral is seized, it still has 1 ETH (
1e18) of debt in a fixed pool. - When
clearBadDebtis called, the earnings accumulator has 0.95 ETH (0.95e18) in it, which is less than the bad debt to be cleared. - The function, instead of clearing the maximum bad debt possible (i.e. 0.95 ETH), it won't clear any bad debt because the accumulator is slightly lower than the debt to clear.
This will cause the accrued bad debt to stay in the market, possibly causing a bank run in the long term if enough bad debt isn't cleared.
Impact
When the value of earningsAccumulator is slightly lower than the bad debt, the protocol won't clear any bad debt. If this happens enough times, the uncleared bad debt will become bigger and it will possibly cause a bank run in the future, and the last users to withdraw won't be able to because of the lack of funds within the protocol.
Code Snippet
Tool used
Manual Review
Recommendation
To mitigate this issue is recommended to clear the maximum amount of bad debt possible when the accumulated earnings are slightly lower than the bad debt to clear.
Discussion
Dliteofficial
@santipu03
I don't think this finding is valid, rather than intended behaviour. Plus, it isn't clear from this finding how the last user will be unable to withdraw their funds if the socialized bad debt doesn't affect the amount withdrawn by floating rate pool LPs. The debt is wiped out using earningsAccumulator.
I also believe this is intended behaviour because in an attempt to protect the LPs, the bad debt is only socialized using the earningAccumulator meaning that the LPs are only liable to the uncleared debt to the tune of the earnings accumulated over time
MehdiKarimi81
don't you think it should be a high issue since it can affect share price and the last user cannot withdraw assets? @santipu03
MehdiKarimi81
@santipu03
I don't think this finding is valid, rather than intended behaviour. Plus, it isn't clear from this finding how the last user will be unable to withdraw their funds if the socialized bad debt doesn't affect the amount withdrawn by floating rate pool LPs. The debt is wiped out using earningsAccumulator.
I also believe this is intended behaviour because in an attempt to protect the LPs, the bad debt is only socialized using the earningAccumulator meaning that the LPs are only liable to the uncleared debt to the tune of the earnings accumulated over time
Probably earningAccumulator won't be enough to clear bad debt so bad debt remains as part of floating assets which inflates share price while there is not enough funds in the contract so last user in unable to withdraw funds
Dliteofficial
@MehdiKarimi81 this doesn't track. The nexus of the finding is that the bad debt will not be clearable if the earnings accumulator doesn't have that capacity. I believe this is intended behaviour to limit the LP's liability
santipu03
The core of this finding is that the protocol isn't clearing any bad debt when it should clear it partially. Therefore, when the earnings accumulator is lower than the bad debt, it should be set to zero in order to clear the maximum bad debt. Instead, the protocol won't clear any bad debt, leading to a discrepancy between the tracked funds and the real funds, leading to a possible bank run if lenders see there's uncleared bad debt on the market.
santichez
This was a design choice that the team made, mostly because at that point the calculation of a partial fixed borrow repayment was pretty complex and this was considered not worth it given the really low likelihood of the whole context. Even if the accumulator is not sufficient to cover a maturity repayment, as soon as the accumulator gets filled the clearing of bad debt can be triggered again.
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
The code uses a strict greater-than-or-equal check that skips clearing entirely when the accumulator is slightly below the bad debt, instead of clearing the maximum possible amount.
Impact
If this happens repeatedly, uncleared bad debt could grow and potentially cause a bank run, leaving the last users unable to withdraw; the team notes the accumulator can be refilled and clearing retriggered later.
What the attacker needed
A loan is liquidated with bad debt; earnings accumulator is slightly lower than the bad debt
The fix
The recommendation is to clear the maximum amount of bad debt possible when the accumulated earnings are slightly lower than the bad debt; the team considers the current behaviour a design choice.
How it was found
- A loan is liquidated and the full collateral is seized, but 1 ETH of debt remains in a fixed pool.
- The earnings accumulator has 0.95 ETH, which is less than the bad debt.
- clearBadDebt is called, but because the accumulator is lower than the debt, no bad debt is cleared.
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.
In a lending protocol, when a borrower's collateral is worth less than their debt, the leftover is called bad debt. The protocol tries to cover this bad debt using accumulated earnings, but if the earnings are just a bit too small, it covers nothing instead of covering as much as it can. Over time, this can make the protocol's accounting show more money than actually exists, which might cause a rush of withdrawals.
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.