KangarooVault._resetTrade , LiquidityPool.rebalanceMargin , and LiquidityPool._getTotalMargin functions should check isInvalid , which is returned by external perp market contract’s remainingMargin function, and revert if such isInvalid is true
Several functions in KangarooVault and LiquidityPool ignore the isInvalid flag returned by the external perp market's remainingMargin function, so they use a margin value that should not be trusted. This can allow deposits and withdrawals to proceed with an inaccurate token price.
rbserver Follow with KIntern_NA
Originally disclosed on Code4rena
Submitted by rbserver, also found by KIntern_NA
The following KangarooVault._resetTrade, LiquidityPool.rebalanceMargin, and LiquidityPool._getTotalMargin functions call the external perp market contract’s remainingMargin function to get the remaining margin for the KangarooVault or LiquidityPool. Yet, none of these functions use isInvalid that can be returned by such remainingMargin function. When the returned remaining margin is invalid, such value should not be trusted and used. Using such invalid remaining margin can have negative effects.
function _resetTrade() internal {
positionData.positionId = 0;
(uint256 totalMargin,) = PERP_MARKET.remainingMargin(address(this));
PERP_MARKET.transferMargin(-int256(totalMargin));
usedFunds -= totalMargin;
...
}
function rebalanceMargin(int256 marginDelta) external requiresAuth nonReentrant {
int256 currentPosition = _getTotalPerpPosition();
uint256 marginRequired = _calculateMargin(currentPosition);
(uint256 currentMargin,) = perpMarket.remainingMargin(address(this));
int256 additionalMargin = marginDelta;
if (currentMargin >= marginRequired) {
marginDelta -= int256(currentMargin - marginRequired);
} else {
marginDelta += int256(marginRequired - currentMargin);
}
...
}
function _getTotalMargin() internal view returns (uint256) {
(uint256 margin,) = perpMarket.remainingMargin(address(this));
return margin;
}
For example, we can compare the KangarooVault.getTokenPrice and LiquidityPool.getTokenPrice functions. The following KangarooVault.getTokenPrice function checks the isInvalid returned by PERP_MARKET.remainingMargin(address(this)) so calling it will revert if PERP_MARKET.remainingMargin(address(this))’s returned totalMargin is invalid. In contrast, the LiquidityPool.getTokenPrice function does not do this.
function getTokenPrice() public view returns (uint256) {
...
(totalMargin, isInvalid) = PERP_MARKET.remainingMargin(address(this));
require(!isInvalid);
...
}
In the following LiquidityPool.getTokenPrice function, when _getTotalMargin()’s returned totalMargin, which is also margin returned by perpMarket.remainingMargin(address(this)) in the LiquidityPool._getTotalMargin function, is invalid, such totalMargin is still used to increase totalValue, which then affects the liquidity token’s price. The LiquidityPool.getTokenPrice function is called in functions like LiquidityPool.deposit and LiquidityPool.withdraw. Thus, calling such functions would not revert when such invalid totalMargin is used. As a result, the depositing and withdrawal actions that should not be allowed because of such invalid totalMargin can still be allowed unexpectedly.
function getTokenPrice() public view override returns (uint256) {
if (totalFunds == 0) {
return 1e18;
}
uint256 totalSupply = liquidityToken.totalSupply() + totalQueuedWithdrawals;
int256 skew = _getSkew();
if (skew == 0) {
return totalFunds.divWadDown(totalSupply);
}
(uint256 markPrice, bool isInvalid) = getMarkPrice();
require(!isInvalid);
uint256 totalValue = totalFunds;
uint256 amountOwed = markPrice.mulWadDown(powerPerp.totalSupply());
uint256 amountToCollect = markPrice.mulWadDown(shortToken.totalShorts());
uint256 totalMargin = _getTotalMargin();
totalValue += totalMargin + amountToCollect;
totalValue -= uint256((int256(amountOwed) + usedFunds));
return totalValue.divWadDown(totalSupply);
}
Proof of Concept
The following steps can occur for the described scenario.
- The
LiquidityPool.depositfunction is called to deposit some sUSD tokens. - When the
LiquidityPool.getTokenPricefunction is called,_getTotalMargin()’s returnedtotalMargin, which ismarginreturned byperpMarket.remainingMargin(address(this)), is invalid. However, such invalidtotalMargindoes not cause calling theLiquidityPool.getTokenPricefunction to revert. - The deposit action succeeds while the used liquidity token’s price that depends on the invalid
totalMarginis inaccurate. Such deposit action should not be allowed but it is.
Tools Used
VSCode
Recommended Mitigation Steps
The KangarooVault._resetTrade, LiquidityPool.rebalanceMargin, and LiquidityPool._getTotalMargin functions can be updated to check isInvalid that is returned by the external perp market contract’s remainingMargin function. If such isInvalid is true, 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 call remainingMargin but discard the isInvalid boolean, so they use a margin value that the external contract has marked as invalid.
Impact
Deposits and withdrawals could be allowed when they should not be, because the token price is calculated from an invalid margin value.
What the attacker needed
RemainingMargin returns isInvalid true
The fix
The functions should check the isInvalid flag returned by remainingMargin and revert when it is true.
How it was found
- Call LiquidityPool.deposit to deposit sUSD tokens.
- When getTokenPrice runs, _getTotalMargin returns an invalid totalMargin but does not revert.
- The deposit succeeds even though the liquidity token price based on the invalid margin is inaccurate.
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 asks another contract for a margin value, and that other contract also says whether the value is valid. Some functions ignore the validity flag and use the number anyway. That can make the price of a token wrong, so people can deposit or withdraw when they should not be able to.
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.