Skip to content
DoS Audit finding Advanced

Incorrect parameters passed to UniV3 may cause funds stuck in the vault

The vault passed zero minimum amounts to UniV3's decreaseLiquidity, which could return zero amounts, and then called collect with both amounts zero, causing a revert. This made deposits, withdrawals, and rebalancing unavailable for the vault.

libratus Follow

Originally disclosed on Code4rena

Submitted by libratus

Note: this issue happened on the deployed version of GoodEntry and was discovered when using https://alpha.goodentry.io

Due to incorrect parameters and validation when working with UniV3 LP the vault may enter a state where rebalancing reverts. This means any deposits and withdrawals from the vault become unavailable.

Code walkthrough

When rebalancing a vault, the existing positions need to be removed from Uni. This is done in removeFromTick function.

    if (aBal > 0){
      lendingPool.withdraw(address(tr), aBal, address(this));
      tr.withdraw(aBal, 0, 0);
    }

Here, zeros are passed as amount0Min and amount1Min arguments. The execution continues in TokenisableRange.withdraw function. decreaseLiquidity is called to remove liquidity from Uni.

    (removed0, removed1) = POS_MGR.decreaseLiquidity(
      INonfungiblePositionManager.DecreaseLiquidityParams({
        tokenId: tokenId,
        liquidity: uint128(removedLiquidity),
        amount0Min: amount0Min,
        amount1Min: amount1Min,
        deadline: block.timestamp
      })
    );

Here there is an edge-case that for really small change in liquidity the returned values removed0 and removed1 can be 0s (will be explained at the end of a section).

Then, collect is called and removed0,removed1 are passed as arguments.

    POS_MGR.collect(
      INonfungiblePositionManager.CollectParams({
        tokenId: tokenId,
        recipient: msg.sender,
        amount0Max: uint128(removed0),
        amount1Max: uint128(removed1)
      })
    );

However, collect reverts when both of these values are zeros - https://github.com/Uniswap/v3-periphery/blob/main/contracts/NonfungiblePositionManager.sol#L316

As a result, any deposit/withdraw/rebalancing of the vault will revert when it will be attempting to remove existing liquidity.

When can decreaseLiquidity return 0s

This edge-case is possible to achieve as it happened in the currently deployed alpha version of the product. The sponsor confirmed that the code deployed is the same as presented for the audit.

The tick that caused the revert has less than a dollar of liquidity. Additionally, that tick has outstanding debt and so the aBal value was small enough to cause the issue. In the scenario that happened on-chain aBal is only 33446.

    uint aBal = ERC20(aTokenAddress).balanceOf(address(this));
    uint sBal = tr.balanceOf(aTokenAddress);

    // if there are less tokens available than the balance (because of outstanding debt), withdraw what's available
    if (aBal > sBal) aBal = sBal;
    if (aBal > 0){
      lendingPool.withdraw(address(tr), aBal, address(this));
      tr.withdraw(aBal, 0, 0);
    }

Proof of Concept

The issue can be demonstrated on the contracts deployed on the arbitrum mainnet. This is the foundry test:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";

interface IGeVault {
    function withdraw(uint liquidity, address token) external returns (uint amount);
}

contract GeVaultTest is Test {
    IGeVault public vault;

    function setUp() public {
        vault = IGeVault(0xdcc16DEfe27cd4c455e5520550123B4054D1b432);
        // 0xdcc16DEfe27cd4c455e5520550123B4054D1b432 btc vault
    }

    function testWithdraw() public {
        // Account that has a position in the vault
        vm.prank(0x461F5f86026961Ee7098810CC7Ec07874077ACE6);

        // Trying to withdraw 1 USDC
        vault.withdraw(1e6, 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8);
    }
}

The test can be executed by forking the arbitrum mainnet

forge test -vvv --fork-url <your_arb_rpc> --fork-block-number 118634741

The result is an error in UniV3 NonfungiblePosition.collect method

   │   │   │   ├─ [1916] 0xC36442b4a4522E871399CD717aBDD847Ab11FE88::collect((697126, 0xdcc16DEfe27cd4c455e5520550123B4054D1b432, 0, 0))
    │   │   │   │   └─ ← "EvmError: Revert"

Recommended Mitigation Steps

It is unclear why this collect call is needed because the fees are already collected a few lines above in claimFees. I suggest removing the second collect call altogether. If it’s needed then perhaps only collect if one of removed0/removed1 is non-zero.

gzeon (Judge) commented:

The writeup looks correct but it seems to be quite an edge case and therefore the risk is low. But @Keref can you review?

Keref (Good Entry) commented:

Yes, it was confirmed. The auditor also contacted me in private and we solved the bug, checking that it didn’t try to collect (0, 0).

gzeon (Judge) commented:

Cool, I believe severity to be Medium as this clearly affected the availability of the protocol. However, I am not fully convinced that this will make the asset stuck permanently without an upgrade. For example, it seems to be possible to borrow everything from the lendingpool so that aBal is set to 0 and the withdraw will be skipped.

libratus (Warden) commented:

Yes, it might be correct that there are ways to unstuck the funds without an upgrade.

Good Entry Mitigated:

Prevent collect from reverting by adding a check that it doesnt try to collect 0. PR: https://github.com/GoodEntry-io/ge/commit/bbbac57c110223f45851494971a34f57c55922c7

Status: Mitigation confirmed. Full details in reports from kutugu, xuwinnie and 3docSec.


The original report as published on Code4rena, by libratus. 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 code did not check whether the amounts returned by decreaseLiquidity were zero before calling collect, and collect reverts when both amounts are zero.

Impact

Deposits and withdrawals from the vault became unavailable, though the judge noted the funds might be unstuck without an upgrade by borrowing from the lending pool.

What the attacker needed

A vault position with very small liquidity; outstanding debt making available balance tiny; a user attempts deposit or withdrawal

The fix

The project mitigated the issue by adding a check to prevent collect from being called with zero amounts.

How it was found

  1. A vault position had very small liquidity and outstanding debt, so the available balance was tiny.
  2. During rebalancing, the vault called decreaseLiquidity with zero minimum amounts.
  3. decreaseLiquidity returned zero for both removed amounts.
  4. The vault then called collect with both amounts set to zero, which reverted.
  5. Any deposit, withdrawal, or rebalancing that tried to remove that liquidity also reverted.

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 vault is a smart contract that holds users' funds and invests them. When it tried to remove a small amount of liquidity from a trading pool, it asked the pool to collect zero tokens, which the pool refuses, so the whole operation failed and users could not withdraw.

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 libratus

View all

4 more disclosed reports, best first.

More from Good Entry

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.