`TARGET_HEALTH` calculation does not consider the adjust factors of the picked seize and repay markets
The liquidation health factor calculation uses an average adjust factor across all markets, but when a liquidator picks specific repay and seize markets with different adjust factors, the resulting health factor deviates from the target. This can cause losses for either the liquidator or the borrower and may lead to bad debt.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-04-interest-rate-model-judging/issues/117
The protocol has acknowledged this issue.
Found by
0x73696d616f
Summary
The TARGET_HEALTH calculation is correct, but returns the debt to pay considering that this debt corresponds to the average adjustFactor, which is false for more than 1 market, leading to significant deviation in the resulting health factor.
Vulnerability Detail
The calculation of the required debt to repay is explained in the MathPaper, and it can be understood that the resulting debt to repay is based on the average adjust factor of all markets.
However, when liquidating, repay and seize markets are picked, possibly having different adjust factors. Thus, depending on the picked repay and seized market, the resulting health factor will be significantly different than the TARGET_HEALTH. This will either lead to losses for the liquidator or the liquidatee, in case the resulting health factor is smaller or bigger, respectively.
If the resulting health factor is smaller, the liquidator would receive less assets and the protocol would be closer to accumulating bad debt (it may even be negative if the calculation is way off). Contrarily, if it is higher, the liquidator will have more assets removed than supposed, resulting in losses.
A test was carried out in Market.t.sol showing that depending on the market picked, the health factor is either approximately 1.37 or 1.15, due to the adjust factor.
The user
- Deposited
20_000e18in aDAImarket with an adjust factor of0.8. - Borrowed
20_000e18*0.8^2in theDAImarket. - Deposited
10_000e18in aWETHmarket with an adjust factor of0.9. - Borrowed
10_000e18*0.9^2in aWETHmarket.
The health factor is (20_000*0.8 + 10000*0.9) / (20000*0.8^2/0.8 + 10000*0.9^2/0.9) = 1.
1 second passes to make the health factor smaller than 1.
Now, depending on the picked repay and seize markets, the resulting health factor will be very different.
The average adjust factor is (20_000*0.8 + 10000*0.9) * (20000*0.8^2 + 10000*0.9^2) / (20000*0.8^2/0.8 + 10000*0.9^2/0.9) / (20000 + 10000) = 0.6967.
The close factor is (1.25 - 1) / (1.25 - 0.6967*1.1) = 0.5169.
The debt repayed using the close factor is (20000*0.8^2 + 10000*0.9^2)*0.5169 = 10803.
The Collateral repayed is 10803 * 1.1 = 11883.
The issue is that the debt and collateral are averaged on the adjust factor, but it is being repayed on a single market.
Repaying in the DAI market, the resulting health factor is ((20_000 - 11883)*0.8 + 10000*0.9) / ((20000*0.8^2 - 10803)/0.8 + 10000*0.9^2/0.9) = 1.3477.
If the test is inverted, repaying in the WETH market will lead to a health factor of 1.15.
function test_POC_WrongHealthFactor() external {
// Change to false to test liquidating in the WETH market
// in exactly the same conditions except the adjust factor
bool marketDAI = true;
uint256 assets = 10_000 ether;
ERC20 asset = market.asset();
uint256 marketAssets = marketDAI ? 2*assets : assets;
uint256 wethAssets = marketDAI ? assets : 2*assets;
vm.startPrank(ALICE);
// ALICE deposits and borrows DAI
deal(address(asset), ALICE, marketAssets);
market.deposit(marketAssets, ALICE);
market.borrow(marketAssets*8*8/10/10, ALICE, ALICE);
// ALICE deposits and borrows weth
deal(address(weth), ALICE, wethAssets);
weth.approve(address(marketWETH), wethAssets);
marketWETH.deposit(wethAssets, ALICE);
marketWETH.borrow(wethAssets*9*9/10/10, ALICE, ALICE);
vm.stopPrank();
skip(1);
// LIQUIDATION of DAI MARKET, 0.8 adjust factor
if (marketDAI) {
deal(address(asset), address(market), 100_000 ether);
address liquidator = makeAddr("liquidator");
deal(address(asset), liquidator, 100_000_000 ether);
vm.startPrank(liquidator);
asset.approve(address(market), type(uint256).max);
market.liquidate(ALICE, type(uint256).max, market);
vm.stopPrank();
}
// LIQUIDATION of WETH MARKET, 0.9 adjust factor
if (!marketDAI) {
deal(address(weth), address(marketWETH), 100_000 ether);
address liquidator = makeAddr("liquidator");
deal(address(weth), liquidator, 100_000_000 ether);
vm.startPrank(liquidator);
weth.approve(address(marketWETH), type(uint256).max);
marketWETH.liquidate(ALICE, type(uint256).max, marketWETH);
vm.stopPrank();
}
// RATIO is smaller than 1.25, liquidator did not liquidate as much as if it was in
// a single market
(uint256 collateral, uint256 debt) = auditor.accountLiquidity(address(ALICE), Market(address(0)), 0);
assertEq(collateral*1e18 / debt, marketDAI ? 1347680781176165186 : 1146310462433177450);
}
Impact
Losses for the liquidator or the liquidatee and possible accumulation of bad debt, depending on the picked market.
Code Snippet
https://github.com/exactly/papers/blob/main/ExactlyMathPaperV1.pdf https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/main/protocol/contracts/Auditor.sol#L238-L243
Tool used
Manual Review
Vscode
Foundry
Recommendation
The debt to repay should take into account the adjust factors of the seize and repay markets. In auditor::checkLiquidation(), the maxRepayAssets, `base.totalDebt.mulWadUp(Math.min(1e18, closeFactor)), must consider the adjust factors of the chosen seize and repay markets.
Discussion
Dliteofficial
@0x73696d616f I don't see why the protocol should consider the TH of the repay market if they already considered the TH of both the loan and collateral market. The repay market has no standing on the loan to be liquidated, it is just a market from which to pay from. The collateral is where the cover is from, the loan market is where the cover withdrawn is to be used.
@santipu03 I think this is Invalid
0x73696d616f
resulting debt to repay is based on the average adjust factor of all markets.
The math calculations arrive at an average adjust factor debt to repay and collateral to seize. When you pick a repay and a seize market, they will have different adjust factors than the average, and this must be accounted for.
Dliteofficial
Yes, I can see that, and I read the report fine. But by all markets, I believe that refers to the seize and repay market. I am not trying to antagonize your finding, I just don't think there is a reason to include the repay market in the calculation of the defaulter's TH since it has no effect on the collateral or the debt. The only exception to this would be when the defaulter's debt/collateral market is the same as the repay market
0x73696d616f
I just don't think there is a reason to include the repay market in the calculation of the defaulter's TH since it has no effect on the collateral or the debt
Why do you say this?
The only exception to this would be when the defaulter's debt/collateral market is the same as the repay market
And this?
Dliteofficial
Because looking at this on the basis of cause and effect, if the liquidator is repaying from a market different from that of the user's collateral and debt market, it has no effect in the repay market since the borrower doesn't have a borrow there or deposited collateral into the market. As for the exception, I was referring to if the user deposited in the repay market then the market should be considered
0x73696d616f
Because looking at this on the basis of cause and effect, if the liquidator is repaying from a market different from that of the user's collateral and debt market
This is not possible. The liquidator has to pick a debt and collateral market that the user is participating on. This is the whole point of the liquidation flow.
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 the liquidation calculation uses an average adjust factor across all markets instead of the adjust factors of the specific repay and seize markets chosen.
Impact
Losses for the liquidator or the liquidatee and possible accumulation of bad debt, depending on the picked market.
What the attacker needed
A user with debt and collateral in multiple markets; health factor below 1; liquidator chooses repay and seize markets
The fix
The debt to repay should take into account the adjust factors of the seize and repay markets.
How it was found
- Deposit 20,000 DAI in a market with adjust factor 0.8 and borrow 20,000*0.8^2 DAI.
- Deposit 10,000 WETH in a market with adjust factor 0.9 and borrow 10,000*0.9^2 WETH.
- Wait 1 second so the health factor drops below 1.
- Liquidate by repaying in the DAI market and observe the resulting health factor is about 1.35.
- Repeat the liquidation repaying in the WETH market and observe the resulting health factor is about 1.15.
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, each asset has an 'adjust factor' that reduces how much it counts as collateral. When someone is liquidated, the code calculates how much debt to repay using an average of all adjust factors, but the liquidator actually repays in one specific market. If that market's adjust factor differs from the average, the final health factor is wrong, which can unfairly benefit or harm the liquidator or the borrower.
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.