Skip to content
DoS Audit finding Intermediate

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.

0x23r0 Follow with 0xe4669da, Cybrid and 7 others

Originally disclosed on Sherlock

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:

Solidity
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:

  1. The user deposits 2e18 WeETH tokens with an exchangeRate of 1029750180160445400 (WeETH/ETH).
  2. The deposited amount in ETH is calculated in the deposit function:
Solidity
params.depositingAmount = (libParams.exchangeRate * params.depositingAmount) / 1 ether;

params.depositingAmount = (1029750180160445400 * 2e18) / 1 ether = 2059500360320890800

  1. The depositingAmount in ETH is stored in depositDetail.depositedAmountInETH in the treasury contract:
Solidity
borrowing[user].depositDetails[borrowerIndex].depositedAmountInETH = uint128(depositingAmount); // 2059500360320890800
  1. Meanwhile, the original depositingAmount (in WeETH) is stored in depositDetail.depositedAmount:
Solidity
depositDetail.depositedAmount = uint128(depositingAmount); // 2e18

Liquidation Yield Calculation

During liquidation, the yields calculation uses the stored depositDetail.depositedAmount and depositDetail.depositedAmountInETH:

Solidity
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 = 2e18
    • depositDetail.depositedAmountInETH = 2059500360320890800
    • exchangeRate = 1029750180160445400
  • At liquidation:

    • New exchangeRate = 1029750180160445399 (slightly decreased by 1 wei).
  • Yield calculation:

JavaScript
yields = 2e18 - ((2059500360320890800 * 1 ether) / 1029750180160445399)  -->   `-1`

Substitute values:

JavaScript
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:

  1. ETH/USD: https://data.chain.link/feeds/optimism/mainnet/eth-usd
  2. 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

  1. Deposit 2e18 WeETH with an exchangeRate of 1029750180160445400.

    • depositDetail.depositedAmount = 2e18.
    • depositDetail.depositedAmountInETH = 2059500360320890800.
  2. Trigger liquidation when the exchangeRate decrease slightly (e.g., 1029750180160445399).

  3. Observe the revert during yield calculation:

Solidity
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.

Solidity
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.

Solidity
    function updateYieldsFromLiquidatedLrts(
        uint256 yields
    ) external onlyCoreContracts {
        yieldsFromLiquidatedLrts += yields;
    }

https://github.com/sherlock-audit/2024-11-autonomint/blob/main/Blockchain/Blockchian/contracts/Core_logic/Treasury.sol#L513-L517

The original report as published on Sherlock, by 0x23r0. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. Deposit 2e18 WeETH with an exchange rate of 1029750180160445400, storing depositedAmount and depositedAmountInETH.
  2. Trigger liquidation when the exchange rate decreases slightly (e.g., to 1029750180160445399).
  3. 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.

More by 0x23r0

View all

23 more disclosed reports, best first.

More from Autonomint Colored Dollar V1

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.