The `liquidationType1` function in the borrowLiquidation contract reverts unexpectedly when calculating the yields
The liquidation function reverts when the exchange rate of a deposited asset decreases slightly, because a subtraction underflows. This prevents eligible positions from being liquidated.
Source: https://github.com/sherlock-audit/2024-11-autonomint-judging/issues/519
Found by
0x23r0, 0xe4669da, Cybrid, John44, LordAlive, Pablo, ParthMandale, Ruhum, futureHack, volodya
Summary
The liquidationType1 function in the borrowLiquidation contract reverts unexpectedly when calculating the yields:
uint256 yields = depositDetail.depositedAmount - ((depositDetail.depositedAmountInETH * 1 ether) / exchangeRate);
This happens when the calculated value of ((depositDetail.depositedAmountInETH * 1 ether) / exchangeRate) exceeds depositDetail.depositedAmount. This edge case arises due to very small changes in the exchangeRate value, leading to an unexpected revert caused by a subtraction of a larger value from a smaller one. This behavior is rooted that not taken the exchange rate that changed to a smaller value during liquidation.
Root Cause
When a user deposits collateral, the deposited amount is stored both in terms of the asset (e.g., WeETH) and its equivalent in ETH, using the exchangeRate at the time of deposit. The calculations are as follows:
- The user deposits
2e18WeETHtokens with anexchangeRateof1029750180160445400(WeETH/ETH). - The deposited amount in ETH is calculated in the deposit function:
params.depositingAmount = (libParams.exchangeRate * params.depositingAmount) / 1 ether;
params.depositingAmount = (1029750180160445400 * 2e18) / 1 ether = 2059500360320890800
- The
depositingAmountin ETH is stored in depositDetail.depositedAmountInETH in the treasury contract:
borrowing[user].depositDetails[borrowerIndex].depositedAmountInETH = uint128(depositingAmount); // 2059500360320890800
- Meanwhile, the original depositingAmount (in
WeETH) is stored indepositDetail.depositedAmount:
depositDetail.depositedAmount = uint128(depositingAmount); // 2e18
Liquidation Yield Calculation
During liquidation, the yields calculation uses the stored depositDetail.depositedAmount and depositDetail.depositedAmountInETH:
uint256 yields = depositDetail.depositedAmount - ((depositDetail.depositedAmountInETH * 1 ether) / exchangeRate);
If the exchangeRate has slightly decreased (e.g., by 1 wei), the denominator in the division becomes slightly smaller, making the result of ((depositDetail.depositedAmountInETH * 1 ether) / exchangeRate) slightly larger. As a result, this value can exceed depositDetail.depositedAmount, leading to an underflow (revert).
For example:
-
At deposit:
depositDetail.depositedAmount = 2e18depositDetail.depositedAmountInETH = 2059500360320890800exchangeRate = 1029750180160445400
-
At liquidation:
- New
exchangeRate = 1029750180160445399(slightly decreased by 1 wei).
- New
-
Yield calculation:
yields = 2e18 - ((2059500360320890800 * 1 ether) / 1029750180160445399) --> `-1`
Substitute values:
yields = 2e18 - 2000000000000000001 = -1 (revert the liquidation)
Here, the logic is implemented in such a way that it assumes the exchange rate of ETH-backed assets like WeETH or WrETH will never decrease if the ETH price increases, but this is incorrect.
If we check the very latest data on chainlink data feeds of ETH/USD and weETH/ETH on the optimism network:
- ETH/USD: https://data.chain.link/feeds/optimism/mainnet/eth-usd
- weETH/ETH: https://data.chain.link/feeds/optimism/mainnet/weeth-eth
Based on that feeds if a user deposits collateral on December 18 with an ETH price of 393133, and by December 21 the price has decreased to 347491, the ETH price has decreased significantly. Based on the protocol's logic, this position should be liquidated. However, the protocol team assumes that if the price of ETH decreased, the ETH-backed tokens, like weETH or wrsETH (which the protocol accepts as deposits), will not affected. This is not always the case.
If we check the Chainlink price feed for weETH:
On December 18, the exchange rate was 1.0550. By December 21, although the price of ETH had decreased significantly, the price of weETH also decreased. On December 21, the exchange rate was 1.0549 so the exchange rate is decreased. This discrepancy will definitely cause the liquidation to revert based on the calculated yields.
pics:
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
check Root Cause
Impact
Positions eligible for liquidation may fail to liquidate due to unexpected reverts.
PoC
-
Deposit
2e18WeETHwith anexchangeRateof1029750180160445400.depositDetail.depositedAmount = 2e18.depositDetail.depositedAmountInETH = 2059500360320890800.
-
Trigger liquidation when the
exchangeRatedecrease slightly (e.g.,1029750180160445399). -
Observe the revert during yield calculation:
uint256 yields = depositDetail.depositedAmount - ((depositDetail.depositedAmountInETH * 1 ether) / exchangeRate);
Mitigation
Here, we don't know whether the exchange rate returned from the oracle is larger or smaller. If it's larger, there is no problem, but if the exchange rate returned from the oracle is smaller, it will revert.
I don't know the best logic implemented that ensures liquidation does not revert.
uint256 yields = depositDetail.depositedAmount - ((depositDetail.depositedAmountInETH * 1 ether) / exchangeRate);
One way to completely remove this is because the result of this calculation is pass to treasury.updateYieldsFromLiquidatedLrts(yields);.
If we check in the treasury, its only increment the yieldsFromLiquidatedLrts and, this state variable is never used in any calculation in the protocol.
function updateYieldsFromLiquidatedLrts(
uint256 yields
) external onlyCoreContracts {
yieldsFromLiquidatedLrts += yields;
}
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 assumes the exchange rate of ETH-backed assets will never decrease, so it subtracts a value that can exceed the stored deposit amount, causing an underflow.
What the attacker needed
User deposits ETH-backed collateral; exchange rate decreases slightly
The fix
The report suggests removing the calculation because its result is only added to a state variable that is never used elsewhere, but does not state a definitive fix.
How it was found
- Deposit 2e18 WeETH with an exchange rate of 1029750180160445400, storing depositedAmount and depositedAmountInETH.
- Trigger liquidation when the exchange rate decreases slightly (e.g., to 1029750180160445399).
- Observe the revert during yield calculation.
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.
When you deposit collateral, the system records both the amount of tokens and their value in ETH at that moment. Later, if the exchange rate between those tokens and ETH drops even a tiny bit, a calculation in the liquidation code tries to subtract a larger number from a smaller one, which the programming language does not allow, so the whole liquidation fails.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.