_amountOut is representing assets and shares at the same time in the liquidate function
In the liquidate function, the _amountOut argument is used both as an asset amount and as a share amount, which is impossible because a conversion rate exists between them. This causes the function to compare values representing different things, so it will not behave as intended.
Aymen0909 Follow with KupiaSec and 0xWaitress
Originally disclosed on Code4rena
Submitted by Aymen0909, also found by KupiaSec, wangxx2026 (1, 2), and 0xWaitress
In the liquidate function from the Vault contract, the input argument _amountOut is used as if it was representing a value of asset amounts and share amounts at the same time; which is impossible as there is a conversion rate between them. This error will make the liquidate function behave in an expected manner, not the one that was intended.
Proof of Concept
The issue is occurring in the liquidate function below:
function liquidate(
address _account,
address _tokenIn,
uint256 _amountIn,
address _tokenOut,
uint256 _amountOut
) public virtual override returns (bool) {
_requireVaultCollateralized();
if (msg.sender != address(_liquidationPair))
revert LiquidationCallerNotLP(msg.sender, address(_liquidationPair));
if (_tokenIn != address(_prizePool.prizeToken()))
revert LiquidationTokenInNotPrizeToken(_tokenIn, address(_prizePool.prizeToken()));
if (_tokenOut != address(this))
revert LiquidationTokenOutNotVaultShare(_tokenOut, address(this));
if (_amountOut == 0) revert LiquidationAmountOutZero();
uint256 _liquidableYield = _liquidatableBalanceOf(_tokenOut);
// @audit _amountOut compared with _liquidableYield which represents an asset amount
// @audit _amountOut is considered as an asset amount
if (_amountOut > _liquidableYield)
revert LiquidationAmountOutGTYield(_amountOut, _liquidableYield);
_prizePool.contributePrizeTokens(address(this), _amountIn);
if (_yieldFeePercentage != 0) {
// @audit _amountOut used to increase fee shares so considered as representing a share amount
_increaseYieldFeeBalance(
(_amountOut * FEE_PRECISION) / (FEE_PRECISION - _yieldFeePercentage) - _amountOut
);
}
uint256 _vaultAssets = IERC20(asset()).balanceOf(address(this));
// @audit _amountOut compared with _vaultAssets which represents an asset amount
// @audit _amountOut is considered as an asset amount
if (_vaultAssets != 0 && _amountOut >= _vaultAssets) {
_yieldVault.deposit(_vaultAssets, address(this));
}
// @audit _amountOut representing a share amount minted to the _account
_mint(_account, _amountOut);
return true;
}
As you can see from the code above, the value of the argument _amountOut is used multiple times in the function logic and each time is representing either an asset amount or a share amount; which is impossible as there is a conversion formula used to transform the asset amount into the share amount (and inversely) with the function _convertToShares (or _convertToAssets).
From the function comments, I couldn’t figure out what the value of _amountOut actually represents, but because there is also another argument given to the liquidate function, which is _tokenOut == address(this), I’m supposing that _amountOut is representing as a share amount; which will mean that all the instances highlighted in the code above, when _amountOut is considered as an asset amount, are wrong.
Instance 1:
// @audit _amountOut compared with _liquidableYield which represents an asset amount
if (_amountOut > _liquidableYield)
revert LiquidationAmountOutGTYield(_amountOut, _liquidableYield);
Instance 2:
// @audit _amountOut compared with _vaultAssets which represents an asset amount
if (_vaultAssets != 0 && _amountOut >= _vaultAssets) {
_yieldVault.deposit(_vaultAssets, address(this));
}
And before comparing _amountOut to the asset amount values of _vaultAssets and _liquidableYield, it’s value should be converted to an asset amount with the function _convertToAssets.
This issue will cause problems for the protocol working, as the liquidate function logic will not behave as expected, because it’s comparing values that represent different things.
Note: If _amountOut is actually representing an asset amount (not a share amount as I supposed), the issue is still valid because _amountOut is also used as being a share amount inside the liquidate function. In that case, it should first be converted to a share amount with _convertToShares in order to get the correct behavior of the liquidate function.
Recommended Mitigation Steps
To solve this issue, I recommend to first convert the value of _amountOut in the liquidate function to an asset amount and store it in a local variable, _amountOutToAsset. In the function logic, use the correct variable, either _amountOut or _amountOutToAsset, when interacting with a share amount or an asset amount.
Assessed type
Error
asselstine (PoolTogether) confirmed
Fixed conversion and naming of field. PR: https://github.com/GenerationSoftware/pt-v5-vault/pull/6
Status: Mitigation confirmed. Full details in reports from dirk_y, rvierdiiev and 0xStalin.
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 function fails to convert _amountOut between asset and share units before using it in comparisons and minting.
Impact
The liquidate function logic will not behave as expected because it compares values that represent different things.
What the attacker needed
None
The fix
The issue was fixed by correcting the conversion and naming of the field.
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.
In a vault, 'assets' are the underlying tokens and 'shares' are what you get for depositing; they are not the same number because a conversion rate applies. The liquidate function takes one number and treats it sometimes as assets and sometimes as shares, so its checks and minting are inconsistent. This means the function may not do what the developers intended.
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.