Some functions that call Exchange.getMarkPrice function do not check if Exchange.getMarkPrice function’s returned markPrice is 0
Some functions that call Exchange.getMarkPrice do not check if the returned markPrice is zero. When the underlying spot price is zero, markPrice becomes zero, which can make collateral checks pass incorrectly and allow removal of collateral that should not be allowed.
Submitted by rbserver
The following Exchange.getMarkPrice function uses pool.baseAssetPrice()’s returned baseAssetPrice, which is spotPrice returned by perpMarket.assetPrice(), to calculate and return the markPrice. When such spotPrice is 0, this function would return a 0 markPrice.
function getMarkPrice() public view override returns (uint256 markPrice, bool isInvalid) {
(uint256 baseAssetPrice, bool invalid) = pool.baseAssetPrice();
isInvalid = invalid;
(int256 fundingRate,) = getFundingRate();
fundingRate = fundingRate / 1 days;
int256 currentTimeStamp = int256(block.timestamp);
int256 fundingLastUpdatedTimestamp = int256(fundingLastUpdated);
int256 totalFunding = wadMul(fundingRate, (currentTimeStamp - fundingLastUpdatedTimestamp));
int256 normalizationUpdate = 1e18 - totalFunding;
uint256 newNormalizationFactor = normalizationFactor.mulWadDown(uint256(normalizationUpdate));
uint256 squarePrice = baseAssetPrice.mulDivDown(baseAssetPrice, PRICING_CONSTANT);
markPrice = squarePrice.mulWadDown(newNormalizationFactor);
}
function baseAssetPrice() public view override returns (uint256 spotPrice, bool isInvalid) {
(spotPrice, isInvalid) = perpMarket.assetPrice();
}
As shown by the code below, calling the KangarooVault.transferPerpMargin and KangarooVault._openPosition functions would revert if baseAssetPrice returned by LIQUIDITY_POOL.baseAssetPrice() is 0 no matter what the returned isInvalid is. This means that the price returned by perpMarket.assetPrice() should not be trusted and used whenever such price is 0.
function transferPerpMargin(int256 marginDelta) external requiresAuth nonReentrant {
if (marginDelta < 0) {
...
(uint256 baseAssetPrice, bool isInvalid) = LIQUIDITY_POOL.baseAssetPrice();
require(!isInvalid && baseAssetPrice != 0);
...
} else {
...
}
...
}
function _openPosition(uint256 amt, uint256 minCost) internal {
...
(uint256 baseAssetPrice, bool isInvalid) = LIQUIDITY_POOL.baseAssetPrice();
require(!isInvalid && baseAssetPrice != 0);
...
}
However, some functions that call the Exchange.getMarkPrice function do not additionally check if the Exchange.getMarkPrice function’s returned markPrice is 0, which can lead to unexpected consequences. For example, the following KangarooVault.removeCollateral function executes (uint256 markPrice,) = LIQUIDITY_POOL.getMarkPrice(). When markPrice is 0, which is caused by a 0 spotPrice returned by perpMarket.assetPrice(), such price should be considered as invalid and should not be used; yet, in this case, such 0 markPrice can cause minColl to also be 0, which then makes require(positionData.totalCollateral >= minColl + collateralToRemove) much more likely to be passed. In this situation, calling the KangarooVault.removeCollateral function can remove the specified collateralToRemove collateral from the Power Perp position but this actually should not be allowed because such 0 spotPrice and 0 markPrice should be considered as invalid and should not be used.
function removeCollateral(uint256 collateralToRemove) external requiresAuth nonReentrant {
(uint256 markPrice,) = LIQUIDITY_POOL.getMarkPrice();
uint256 minColl = positionData.shortAmount.mulWadDown(markPrice);
minColl = minColl.mulWadDown(collRatio);
require(positionData.totalCollateral >= minColl + collateralToRemove);
usedFunds -= collateralToRemove;
positionData.totalCollateral -= collateralToRemove;
emit RemoveCollateral(positionData.positionId, collateralToRemove);
}
function getMarkPrice() public view override returns (uint256, bool) {
return exchange.getMarkPrice();
}
Proof of Concept
The following steps can occur for the described scenario.
- The
KangarooVault.removeCollateralfunction is called withcollateralToRemovebeing 100e18. markPricereturned byLIQUIDITY_POOL.getMarkPrice()is 0 because a 0spotPriceis returned byperpMarket.assetPrice().- Due to the 0
markPrice,minCollis 0, andpositionData.totalCollateral >= minColl + collateralToRemovecan be true even ifpositionData.totalCollateralis also 100e18 at this moment. - Calling the
KangarooVault.removeCollateralfunction does not revert, and 100e18 collateral is remove from the Power Perp position. - However, a 0
spotPricereturned byperpMarket.assetPrice()and a 0markPricereturned byLIQUIDITY_POOL.getMarkPrice()should be considered as invalid and should not be used. In this case, removing 100e18 collateral from the Power Perp position should not be allowed or succeed but it does.
Tools Used
VSCode
Recommended Mitigation Steps
Functions, such as the KangarooVault.removeCollateral function, that call the Exchange.getMarkPrice function can be updated to additionally check if the Exchange.getMarkPrice function’s returned markPrice is 0. If it is 0, calling these functions should revert.
mubaris (Polynomial) confirmed
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 functions calling getMarkPrice do not validate that the returned markPrice is non-zero, unlike other functions that explicitly require baseAssetPrice != 0.
Impact
When the spot price is zero, the zero mark price can cause collateral checks to pass incorrectly, allowing removal of collateral that should not be allowed.
What the attacker needed
SpotPrice returned by perpMarket.assetPrice() is 0; attacker can call removeCollateral
The fix
Functions that call Exchange.getMarkPrice should be updated to additionally check if the returned markPrice is 0 and revert if it is.
How it was found
- The KangarooVault.removeCollateral function is called with collateralToRemove being 100e18.
- markPrice returned by LIQUIDITY_POOL.getMarkPrice() is 0 because a 0 spotPrice is returned by perpMarket.assetPrice().
- Due to the 0 markPrice, minColl is 0, and positionData.totalCollateral >= minColl + collateralToRemove can be true even if positionData.totalCollateral is also 100e18 at this moment.
- Calling the KangarooVault.removeCollateral function does not revert, and 100e18 collateral is remove from the Power Perp position.
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 smart contract calculates a price for an asset. If that price comes back as zero, it should be treated as invalid. Some functions forget to check for zero, so they use the zero price and make bad decisions, like allowing too much collateral to be removed.
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.