Improper handling of cases when withdrawable assets = 0
The vault's exchange rate function returns a 1:1 rate when the yield vault has zero withdrawable assets, incorrectly marking the vault as collateralised. This could let users deposit or liquidate under false assumptions, though no direct loss of funds is shown.
The function calculates the exchange rate between the amount of assets withdrawable from the YieldVault and the amount of shares minted by this Vault.
However, if _withdrawableAssets is 0, then the function returns _assetUnit (which means 1-1 ratio). This means, that even when the vault has no withdrawable assets from _yieldVault, it’s still considered collateralized.
To illustrate the oddity of this special case, consider when _withdrawableAssets= 1 and _totalSupplyAmount> 0; in this scenario, _currentExchangeRate returns 0 and the vault is considered under-collateralized (since 1 <_assetUnit). However, if _withdrawableAssets= 0, the vault is considered collateralized.
This case has profound impact since a lot of vault logic is based on the vault’s collateralized status.
Below is a POC for the above example. For ease of testing, let’s place these 2 test cases in file vault/test/unit/Vault/Deposit.t.sol under contract VaultDepositTest, then test them using commands:
forge test --match-path test/unit/Vault/Deposit.t.sol --match-test testOneWithdrawableAmount -vvvv
forge test --match-path test/unit/Vault/Deposit.t.sol --match-test testZeroWithdrawableAmount -vvvv
testOneWithdrawableAmount is to demonstrate when _withdrawableAssets= 1 and the vault is considered not collateralized. testZeroWithdrawableAmount is to demonstrate when _withdrawableAssets= 0 and the vault is considered collateralized.
function testZeroWithdrawableAmount() public {
vm.startPrank(alice);
uint256 _amount = 1000e18;
underlyingAsset.mint(alice, _amount);
underlyingAsset.approve(address(vault), type(uint256).max);
vault.deposit(_amount, alice);
// Now make the withdrawable asset = 0
// Burn the balance of yieldVault
uint256 yieldVaultAsset = underlyingAsset.balanceOf(address(yieldVault));
underlyingAsset.burn(address(yieldVault), yieldVaultAsset);
assertEq(underlyingAsset.balanceOf(address(yieldVault)), 0);
// Although the vault has no asset withdrawable in yieldVault
// the exchange rate is 10**18 = assetUnit and the vault is "collateralized"
assertEq(yieldVault.maxWithdraw(address(vault)), 0);
assertEq(vault.exchangeRate(), 10**18);
assertEq(vault.isVaultCollateralized(), true);
}
function testOneWithdrawableAmount() public {
vm.startPrank(alice);
uint256 _amount = 1000e18;
underlyingAsset.mint(alice, _amount);
underlyingAsset.approve(address(vault), type(uint256).max);
vault.deposit(_amount, alice);
// Now make the withdrawable asset = 0
// Burn the balance of yieldVault
uint256 yieldVaultAsset = underlyingAsset.balanceOf(address(yieldVault));
underlyingAsset.burn(address(yieldVault), yieldVaultAsset -1);
assertEq(underlyingAsset.balanceOf(address(yieldVault)), 1);
// vault only has 1 asset token withdrawable, and the exchangeRate is 0
// the vault is not collateralized
assertEq(yieldVault.maxWithdraw(address(vault)), 1);
assertEq(vault.exchangeRate(), 0);
assertEq(vault.isVaultCollateralized(), false);
}
Recommended Mitigation Steps
Since _withdrawableAssets is the dividend, there seems to be no harm in removing the check if _withdrawableAssets= 0. Therefore, I recommend removing it from the condition.
There is definitely a bug here, but I don’t see why it should be high severity. “This case has a profound impact since a lot of vault logic is based on the vault’s collateralized status” is a bit light to pass the burden or proof. At first sight, as there are no funds in the vault anymore, there is no loss of funds. Then there is the case where assets are re-added to the vault for some reason; but this would be a Medium severity scenario, considering it’s very unlikely and not described by the report.
Sorry I didn’t make it clearer. As you can see in the report, the affected function is _currentExchangeRate, so not only is the Vault incorrectly specified as collateralized, the currentExchangeRate also = 1 (assetUnit). These 2 functions affect many other functions, for example:
Function mintYieldFee requires that vault is collateralized, so a user can mint the yield fee even when it’s not.
Function liquidate requires that vault is collateralized, so a user can liquidate asset even when vault’s withdrawable asset = 0.
For deposit function, first deposit will calculate maxDeposit to limit the input tokens:
function maxDeposit(address) public view virtual override returns (uint256) {
return _isVaultCollateralized() ? `type(uint96).max` : 0;
}
If the Vault is under-collateralized, a user can’t deposit since maxDeposit will return 0; however, a user can still deposit in this case:
User calls exchangeRate and receives 1, this can lead them to bad decisions.
To conclude, I agree that this will not lead to direct loss of funds for the vault, but will greatly impact the user, making them send tokens to the vault (through liquidate or deposit) when it’s not collateralized and will potentially suffer economically. I think it should be marked as High severity.
@ktg9 - I still think that the original reports perfectly fit with the definition of Med severity:
“leak value with a hypothetical attack path with stated assumptions, but external requirements”.
The described scenarios require that we are in a situation with withdrawable assets = 0 and that some users behave incorrectly. Also note that I can’t consider scenarios that are not described in the original report.
This issue has been fixed and the exchange rate between the Vault and YieldVault is now 1 to 1. We’ve replaced the exchange rate function by a collateral one, which simplifies the conversions from shares to assets and assets to shares.
If the Vault is collateralized, meaning the amount of withdrawable assets from the YieldVault is greater than the amount of underlying assets supplied to the YieldVault, then users can only withdraw the amount they deposited. Otherwise, any remaining collateral within the YieldVault is available and distributed proportionally among depositors.
The original report as published on Code4rena, by ktg. 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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The exchange rate function treats a zero withdrawable amount as a special case and returns the asset unit (1:1) instead of zero, so the collateralisation check passes.
Impact
The vault may be treated as collateralised when it is not, allowing deposits, liquidations, or yield fee minting under false assumptions; the judge rated this Medium because no direct loss of funds is demonstrated and the scenario is unlikely.
What the attacker needed
Yield vault has zero withdrawable assets; user acts on the incorrect collateral status
The fix
The issue was fixed by replacing the exchange rate function with a collateral check and making vault shares 1:1 with the underlying asset.
How it was found
Alice deposits underlying assets into the vault.
The yield vault's balance of underlying assets is burned so that maxWithdraw returns zero.
The vault's exchange rate is checked and returns the asset unit.
The vault is reported as collateralised despite having no withdrawable assets.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A vault holds user deposits and invests them elsewhere. It checks whether it has enough assets to cover everyone's shares. When the outside investment has nothing left to withdraw, the check wrongly says everything is fine, so users may put more money in or take actions based on a false sense of safety.
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.