Skip to content
DoS Audit finding Intermediate

DnGmxJuniorVaultManager#harvestFees can push junior vault borrowedUSDC above borrow cap and DOS vault

The harvestFees function converts WETH fees to USDC and deposits them into Aave, increasing the junior vault's aUSDC balance and thus its calculated borrowed USDC. If the junior vault is already at its borrow cap, this pushes the borrowed amount above the cap, causing availableBorrow to underflow and revert, which blocks deposits and withdrawals.

0x52 Follow with ctf_sec

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-rage-trade-judging/issues/67

Found by

0x52, ctf_sec

Summary

DnGmxJuniorVaultManager#harvestFees grants fees to the senior vault by converting the WETH to USDC and staking it directly. The result is that the senior vault gains value indirectly by increasing the debt of the junior vault. If the junior vault is already at it's borrow cap this will push it's total borrow over the borrow cap causing DnGmxSeniorVault#availableBorrow to underflow and revert. This is called each time a user deposits or withdraws from the junior vault meaning that the junior vault can no longer deposit or withdraw.

Vulnerability Detail

  if (_seniorVaultWethRewards > state.wethConversionThreshold) {
      // converts senior tranche share of weth into usdc and deposit into AAVE
      // Deposit aave vault share to AAVE in usdc
      uint256 minUsdcAmount = _getTokenPriceInUsdc(state, state.weth).mulDivDown(
          _seniorVaultWethRewards * (MAX_BPS - state.slippageThresholdSwapEthBps),
          MAX_BPS * PRICE_PRECISION
      );
      // swaps weth into usdc
      (uint256 aaveUsdcAmount, ) = state._swapToken(
          address(state.weth),
          _seniorVaultWethRewards,
          minUsdcAmount
      );

      // supplies usdc into AAVE
      state._executeSupply(address(state.usdc), aaveUsdcAmount);

      // resets senior tranche rewards
      state.seniorVaultWethRewards = 0;

The above lines converts the WETH owed to the senior vault to USDC and deposits it into Aave. Increasing the aUSDC balance of the junior vault.

function getUsdcBorrowed() public view returns (uint256 usdcAmount) {
    return
        uint256(
            state.aUsdc.balanceOf(address(this)).toInt256() -
                state.dnUsdcDeposited -
                state.unhedgedGlpInUsdc.toInt256()
        );
}

The amount of USDC borrowed is calculated based on the amount of aUSDC that the junior vault has. By depositing the fees directly above, the junior vault has effectively "borrowed" more USDC. This can be problematic if the junior vault is already at it's borrow cap.

function availableBorrow(address borrower) public view returns (uint256 availableAUsdc) {
    uint256 availableBasisCap = borrowCaps[borrower] - IBorrower(borrower).getUsdcBorrowed();
    uint256 availableBasisBalance = aUsdc.balanceOf(address(this));

    availableAUsdc = availableBasisCap < availableBasisBalance ? availableBasisCap : availableBasisBalance;
}

If the vault is already at it's borrow cap then the line calculating availableBasisCap will underflow and revert.

Impact

availableBorrow will revert causing deposits/withdraws to revert

Code Snippet

https://github.com/sherlock-audit/2022-10-rage-trade/blob/main/dn-gmx-vaults/contracts/vaults/DnGmxSeniorVault.sol#L350-L355

Tool used

Manual Review

Recommendation

Check if borrowed exceeds borrow cap and return zero to avoid underflow:

function availableBorrow(address borrower) public view returns (uint256 availableAUsdc) {

+   uint256 borrowCap = borrowCaps[borrower];
+   uint256 borrowed = IBorrower(borrower).getUsdcBorrowed();

+   if (borrowed > borrowCap) return 0;

+   uint256 availableBasisCap = borrowCap - borrowed;

-   uint256 availableBasisCap = borrowCaps[borrower] - IBorrower(borrower).getUsdcBorrowed();
    uint256 availableBasisBalance = aUsdc.balanceOf(address(this));

    availableAUsdc = availableBasisCap < availableBasisBalance ? availableBasisCap : availableBasisBalance;
}

Discussion

0x00052

Typo. Only meant to mention #52

0xDosa

Fix PR: https://github.com/RageTrade/delta-neutral-gmx-vaults/pull/33

0x00052

Fix looks good. Underflow protection added as recommended

The original report as published on Sherlock, by 0x52. 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 availableBorrow function subtracts the borrowed amount from the borrow cap without checking if the borrowed amount exceeds the cap, leading to an underflow.

Impact

Deposits and withdrawals from the junior vault revert, causing a denial of service.

What the attacker needed

Junior vault at borrow cap; harvestFees called

The fix

The fix adds a check to return zero if the borrowed amount exceeds the borrow cap, preventing the underflow.

How it was found

  1. The junior vault is at its borrow cap.
  2. harvestFees is called, converting WETH to USDC and depositing it into Aave, increasing the junior vault's aUSDC balance.
  3. The increased aUSDC balance causes getUsdcBorrowed to return a value above the borrow cap.
  4. availableBorrow is called (e.g., during deposit or withdraw) and underflows, reverting the transaction.

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 has a limit on how much it can borrow. When fees are harvested, they are added as USDC to the vault, which makes it look like it borrowed more. If it was already at the limit, the calculation of remaining borrow capacity goes negative and crashes, so users can no longer deposit or 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 0x52

View all

211 more disclosed reports, best first.

More from Rage Trade

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.