Exchange: totalFunding calculation should be done with a simple multiplication operation instead of a wadMul operation
The funding rate calculation incorrectly used a wadMul operation, dividing by 1e18 when multiplying a per-second rate by a time difference, leading to a wrong total funding value. This miscalculation affects the mark price and multiple critical protocol functions.
carlitox477 Follow with KIntern_NA
Originally disclosed on Code4rena
Submitted by carlitox477, also found by KIntern_NA
wad operations are meant to be done with int/uint which represent numbers with 18 decimals.
While the funding rate follows this representation, a simple time difference does not.
In Exchange.getMarkPrice as well as in Exchange._updateFundingRate a wadMul operation is done to multiply the funding rate per second by a simple time difference, leading to wrong calculation of normalizationFactor and mark price, affecting critical parts of the protocol.
Proof of Concept
In Exchange.getMarkPrice first we get the funding rate per second:
(int256 fundingRate,) = getFundingRate();
fundingRate = fundingRate / 1 days; // Funding rate per second
Immediately after, the total funding since last update is calculated:
int256 currentTimeStamp = int256(block.timestamp);
int256 fundingLastUpdatedTimestamp = int256(fundingLastUpdated);
// @audit funding rate X (time interval) / 1e18 = Number without decimal
int256 totalFunding = wadMul(fundingRate, (currentTimeStamp - fundingLastUpdatedTimestamp));
int256 totalFunding = wadMul(fundingRate, (currentTimeStamp - fundingLastUpdatedTimestamp));
is the same that
$TOTAL_ FUNDING_{t_{1}; t_{2}} = \frac{FUNDING_ RATE_{sec} \times (t_{2} - t_{1})}{10^{18}}$
However, the division by $10^{18}$ should not happen, given that time difference does not represent a number with 18 decimals.
This ends up in a miscalculation of totalFunding variable, and as a consequence, a miscalculation of mark price.
The same issue happens in _updateFundingRate function.
Impact
Here a complete list of functions affected by this bug:
-
Exchange.\_updateFundingRate(Exchange.sol#L416)Exchange.openTrade(Exchange.sol#L95)Exchange.closeTrade(Exchange.sol#L108)Exchange.addCollateral(Exchange.sol#L121)Exchange.removeCollateral(Exchange.sol#L134)Exchange.liquidate(Exchange.sol#L147)
-
Exchange.getMarkPrice(Exchange.sol#L196):-
Exchange.\_addCollateral(Exchange.sol#L358) -
Exchange.\_removeCollateral(Exchange.sol#L384) -
KangarooVault.getTokenPrice(KangarooVault.sol#L353) -
ShortCollateral.liquidate(ShortCollateral.sol#L133) -
ShortCollateral.getMinCollateral(ShortCollateral.sol#L163) -
ShortCollateral.canLiquidate(ShortCollateral.sol#L193) -
ShortCollateral.maxLiquidatableDebt(ShortCollateral.sol#L216) -
LiquidityPool.orderFee(LiquidityPool.sol#L388) -
LiquidityPool.getMarkPrice(LiquidityPool.sol#L405)LiquidityPool.getTokenPrice(LiquidityPool.sol#L352)LiquidityPool.openLong(LiquidityPool.sol#L437)LiquidityPool.closeLong(LiquidityPool.sol#L469)LiquidityPool.openShort(LiquidityPool.sol#L501)LiquidityPool.closeShort(LiquidityPool.sol#L533)LiquidityPool.liquidate(LiquidityPool.sol#L558)KangarooVault.removeCollateral(KangarooVault.sol#L437)KangarooVault.\_openPosition(KangarooVault.sol#L568)
-
As it can be seen, this bug affects multiple critical part of the protocol, calculating the correct mark price as well as updating the funding rate is essential for the protocol correct behavior.
Recommended Mitigation steps
Simply replace current wad operation for a simple multiplication.
function getMarkPrice() public view override returns (uint256 markPrice, bool isInvalid) {
// Get base asset price from oracles
(uint256 baseAssetPrice, bool invalid) = pool.baseAssetPrice();
isInvalid = invalid;
// Get funding rate per second
// max 1% or 1e16
(int256 fundingRate,) = getFundingRate();
fundingRate = fundingRate / 1 days;
int256 currentTimeStamp = int256(block.timestamp);
int256 fundingLastUpdatedTimestamp = int256(fundingLastUpdated);
- int256 totalFunding = wadMul(fundingRate, (currentTimeStamp - fundingLastUpdatedTimestamp));
+ int256 totalFunding = 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 _updateFundingRate() internal {
(int256 fundingRate,) = getFundingRate();
fundingRate = fundingRate / 1 days;
int256 currentTimeStamp = int256(block.timestamp);
int256 fundingLastUpdatedTimestamp = int256(fundingLastUpdated);
- int256 totalFunding = wadMul(fundingRate, (currentTimeStamp - fundingLastUpdatedTimestamp));
+ int256 totalFunding = fundingRate, (currentTimeStamp - fundingLastUpdatedTimestamp);
int256 normalizationUpdate = 1e18 - totalFunding;
normalizationFactor = normalizationFactor.mulWadDown(uint256(normalizationUpdate));
emit UpdateFundingRate(fundingLastUpdated, normalizationFactor);
fundingLastUpdated = block.timestamp;
}
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 developer used a wad multiplication function on a value that was not in wad format (the time difference), causing an unintended division by 1e18.
Impact
This bug affects multiple critical parts of the protocol, causing incorrect mark price and funding rate calculations, which could lead to wrong trading, collateral, and liquidation outcomes.
What the attacker needed
None
The fix
The fix is to replace the wadMul operation with a simple multiplication, as shown in the recommended mitigation.
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 smart contracts, numbers are often stored as integers with 18 extra decimal places (called 'wad'). Multiplying two such numbers requires dividing by 1e18 to keep the scale correct. Here, the code multiplied a rate (which has 18 decimals) by a time difference (which does not), but still divided by 1e18, making the result far too small and breaking price calculations.
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.