LiquidityPool._getDelta and LiquidityPool._calculateMargin functions should execute require(!isInvalid && spotPrice > 0) instead of require(!isInvalid || spotPrice > 0)
The LiquidityPool contract uses a logical OR instead of AND when checking whether the asset price is valid, so trades can proceed even when the price feed marks the price as invalid. An attacker could open or close positions using an untrusted price, potentially causing financial loss.
Submitted by rbserver, also found by juancito, peakbolt, and chaduke
The following KangarooVault.transferPerpMargin and KangarooVault._openPosition functions execute require(!isInvalid && baseAssetPrice != 0), where isInvalid and baseAssetPrice are returned from calling LIQUIDITY_POOL.baseAssetPrice(). Because LIQUIDITY_POOL.baseAssetPrice() returns the return values of perpMarket.assetPrice(), the KangarooVault.transferPerpMargin and KangarooVault._openPosition functions would only consider perpMarket.assetPrice() as reliable when both !isInvalid and baseAssetPrice != 0 are true.
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, the following LiquidityPool._getDelta and LiquidityPool._calculateMargin functions execute require(!isInvalid || spotPrice > 0) and would consider perpMarket.assetPrice() as reliable when either !isInvalid or spotPrice > 0 is true. When perpMarket.assetPrice() returns a positive spotPrice and a true isInvalid, such spotPrice should be considered as invalid and untrusted; trades, such as these for opening and closing long and short positions using the LiquidityPool, that depend on the LiquidityPool._getDelta and LiquidityPool._calculateMargin functions’ return values, which then rely on such invalid spotPrice, should not be allowed. However, because !isInvalid || spotPrice > 0 is true in this case, calling the LiquidityPool._getDelta and LiquidityPool._calculateMargin functions will not revert, and such trades that should not be allowed can still be made.
function _getDelta() internal view returns (uint256 delta) {
(uint256 spotPrice, bool isInvalid) = baseAssetPrice();
uint256 pricingConstant = exchange.PRICING_CONSTANT();
require(!isInvalid || spotPrice > 0);
delta = spotPrice.mulDivDown(2e18, pricingConstant);
delta = delta.mulWadDown(exchange.normalizationFactor());
}
function _calculateMargin(int256 size) internal view returns (uint256 margin) {
(uint256 spotPrice, bool isInvalid) = baseAssetPrice();
require(!isInvalid || spotPrice > 0);
uint256 absSize = size.abs();
margin = absSize.mulDivDown(spotPrice, futuresLeverage);
}
function baseAssetPrice() public view override returns (uint256 spotPrice, bool isInvalid) {
(spotPrice, isInvalid) = perpMarket.assetPrice();
}
Proof of Concept
The following steps can occur for the described scenario.
- Calling the
perpMarket.assetPricefunction returns a positivespotPriceand a trueisInvalidat this moment. - Calling the
LiquidityPool._getDeltaandLiquidityPool._calculateMarginfunctions would not revert becauserequire(!isInvalid || spotPrice > 0)would be passed. - Trades, such as for closing a long position through calling the
LiquidityPool.closeLongfunction, can go through even though the usedspotPriceis invalid and untrusted. - In this case, such trades should not be allowed but are still made.
Tools Used
VSCode
Recommended Mitigation Steps
The LiquidityPool._getDelta and LiquidityPool._calculateMargin functions can be updated to execute require(!isInvalid && spotPrice > 0) instead of require(!isInvalid || spotPrice > 0).
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 require statement uses a logical OR (||) instead of AND (&&), so the check passes if either condition is true rather than requiring both to be true.
What the attacker needed
PerpMarket.assetPrice returns positive spotPrice and true…
The fix
The functions should be updated to use require(!isInvalid && spotPrice > 0) instead of the OR condition.
How it was found
- The perpMarket.assetPrice function returns a positive spotPrice and a true isInvalid.
- Calling LiquidityPool._getDelta and LiquidityPool._calculateMargin does not revert because the require passes.
- Trades such as closing a long position through LiquidityPool.closeLong can go through using the invalid spotPrice.
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.
Smart contracts often check conditions before allowing actions. Here, a check used 'or' instead of 'and', so the contract allowed trades even when the price was marked as invalid. This could let someone trade at a bad price, which might be exploited.
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.