Skip to content
DoS Audit finding Intermediate

_ensureMaxLoops causes liquidateAccount to fail in certain conditions

The `_ensureMaxLoops` function reverts if the iteration count exceeds `maxLoopsLimit`, but `liquidateAccount` may require more orders than the limit allows, preventing liquidation of an underwater account. An attacker could exploit this by creating a position that cannot be liquidated, potentially causing bad debt.

xuwinnie Follow with 0x8chars

Originally disclosed on Code4rena

Submitted by xuwinnie, also found by 0x8chars.

The function _ensureMaxLoops reverts if the iteration count exceeds the maxLoopsLimit. However, the limitation imposed by maxLoopsLimit hinders the functioning of liquidateAccount under certain conditions, as orderCount needs to reach twice the market count (which is also constrained by the maxLoopsLimit) in extreme cases.

Proof of Concept

Suppose maxLoopsLimit is set to 16 and currently 12 markets have been added, which is allowed by _ensureMaxLoops in function _addMarket:

allMarkets.push(VToken(vToken));
marketsCount = allMarkets.length;
_ensureMaxLoops(marketsCount);

Then, Alice enters all 12 markets by depositing and borrowing simultaneously, which is also allowed by _ensureMaxLoops in function enterMarkets:

uint256 len = vTokens.length;
uint256 accountAssetsLen = accountAssets[msg.sender].length;
_ensureMaxLoops(accountAssetsLen + len);

To illustrate, assume these 12 coins are all stablecoin with an equal value. Let’s call them USDA - USDL. Alice deposits 20 USDA, 1.1 USDB - 1.1 USDL, worth 32.1 USD in total. Then she borrows 2 USDA - 2 USDL, worth 24 USD in total. Unluckily, USDA de-pegs to 0.6 USD and Alice’s deposit value drops to 24.1 USD, which is below the liquidation threshold (also below the minLiquidatableCollateral). However, nobody can liquidate Alice’s account by calling liquidateAccount, because the least possible orderCount is 23, which exceeds maxLoopsLimit.

Let’s take a closer look at LiquidationOrder:

struct LiquidationOrder {
       VToken vTokenCollateral;
       VToken vTokenBorrowed;
       uint256 repayAmount;
}

In this case, the liquidator cannot perfectly match vTokenCollateral with vTokenBorrowed one-to-one. Because the value of collateral and debt is not equal, more than one order is needed to liquidate each asset. To generalize, if the asset count is n, in the worst case, 2n-1 orders are needed for a complete liquidation (not hard to prove).

Recommended Mitigation Steps

_ensureMaxLoops(ordersCount / 2);

Assessed type

Loop

0xean (judge) commented:

more than one order is needed to liquidate each asset

I am not sure I am tracking this assertion in the above report.

chechu (Venus) disputed and commented:

We can resolve it by just increasing the limit accepted by _ensureMaxLoops, with a VIP. By the way, the suggestion is not valid, because the number of iterations will be ordersCount, not ordersCount/2.

0xean (judge) commented:

We can resolve it by just increasing the limit accepted by _ensureMaxLoops, with a VIP.

I don’t think this is a valid mitigation and would still cause a disruption to the protocol that warrants the Medium severity.

Per the C4 docs

2 Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

0xean (judge) commented:

@chechu - the validity of this issue comes down to the assumption of there being more than 1 order per asset, let’s discuss that point specifically, because otherwise, this issue doesn’t seem to be valid.

chechu (Venus) confirmed and commented:

Sorry for the delay in my response. Reviewing it more carefully, the finding seems valid. It’s true that in Comptroller.liquidateAccount several orders per borrowed asset could be needed. Example:

Params:

  • liquidationIncentiveMantissa: 1.1
  • minLiquidatableCollateral: $100

Borrower position:

  • Collateral:

    • USDC: $20 (liquidation threshold: 0.8)
    • USDT: $20 (liquidation threshold: 0.8)
  • Borrow CAKE: $35

Every condition is satisfied to allow the execution of liquidationAccount:

  • Total collateral ($40) < minLiquidatableCollateral ($100)
  • collateralToSeize ($35 * 1.1 = $38.5) < total collateral ($40)
  • Shortfall ($35 - ($20 * 0.8 + $20 * 0.8) = $3) > 0

But the liquidator cannot repay $35 of the borrowed asset and get enough tokens of just one of the collaterals. So, the liquidator will need two orders:

  • order 1:

    • collateral USDC
    • borrowed asset: CAKE
    • repay amount: $17.5 (getting $19.25 of USDC)
  • order 2:

    • collateral USDT
    • borrowed asset: CAKE
    • repay amount: $17.5 (getting $19.25 of USDT)

This way, the final position of the borrower will be:

  • Collateral:

    • USDC: $0.75
    • USDT: $0.75
  • Borrow CAKE: $0 <— that is required at the end of the Comptroller.liquidateAccount, and it would be impossible to get only using one of the available collaterals

We will apply the mitigation suggested by the warden.


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

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 loop limit check in `liquidateAccount` uses the same limit as other functions, but liquidation can require up to twice the number of markets in orders, exceeding the limit.

Impact

Underwater accounts may become unliquidatable, potentially leading to bad debt for the protocol.

What the attacker needed

MaxLoopsLimit set low relative to markets; many markets added; borrower enters many markets; collateral value drops

The fix

The team will apply the mitigation suggested by the warden, which involves adjusting the loop limit check.

How it was found

  1. Set maxLoopsLimit to 16 and add 12 markets.
  2. Alice enters all 12 markets by depositing and borrowing.
  3. USDA de-pegs, making Alice's position undercollateralised.
  4. A liquidator tries to call liquidateAccount but the required orderCount exceeds maxLoopsLimit, causing a revert.

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 protocol lets users deposit collateral and borrow other assets. If the collateral value drops, anyone can liquidate the position to repay the debt and keep the protocol healthy. Here, a loop limit meant to prevent excessive computation can block liquidation when many assets are involved, leaving bad debt.

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 xuwinnie

View all

12 more disclosed reports, best first.

More from Venus Prime

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.