Insufficient funding fee rounding protection
The funding fee calculation only tracks fractional remainders when the fee rounds down to zero, ignoring fractional parts when the fee is greater than one. This causes funding fees to be underpaid, potentially leading to liquidations or reduced profits.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/170
Found by
IllIllI
Summary
Since funding fees may be small when there isn't much price movement, the code has a feature to keep track of partial amounts when the chargeable fee rounds down to zero. This isn't enough when the fee is 1.X times the minimum value.
Vulnerability Detail
If the funding fee is 0.9, the code correctly stores it until the fee is greater than one. If the funding fee is 1.9, the code ignores the 0.9 part. If 0.9 was enough of a fee to track in the first case, it should also be large enough for the second case.
Impact
Funding fees are not fully paid when they should be, which may cause liquidations down the line, or less profit than there should be.
Code Snippet
The code accounts for the special case of the amount rounding down to zero when the factor is applied, but doesn't account for fractional amounts when the amount is greater than one:
// File: gmx-synthetics/contracts/market/MarketUtils.sol : MarketUtils.getFundingFeeAmount() #1
1237 function getFundingFeeAmount(
1238 int256 latestFundingAmountPerSize,
1239 int256 positionFundingAmountPerSize,
1240 uint256 positionSizeInUsd
1241 ) internal pure returns (bool, int256) {
1242 int256 fundingDiffFactor = (latestFundingAmountPerSize - positionFundingAmountPerSize);
1243 int256 amount = Precision.applyFactor(positionSizeInUsd, fundingDiffFactor);
1244
1245 @> return (fundingDiffFactor != 0 && amount == 0, amount);
1246: }
Tool used
Manual Review
Recommendation
Track fractional amounts even when the amount is not equal to zero
Discussion
xvi10
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
The code only tracks fractional funding fees when the calculated amount is zero, not when it is greater than zero but has a fractional part.
Impact
Funding fees are not fully paid, which may cause liquidations or less profit than expected.
What the attacker needed
None
The fix
The code should track fractional amounts even when the calculated fee is not zero.
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.
Funding fees are periodic payments between traders. When the fee is very small, the code saves the fraction for later, but only if the fee is less than one. If the fee is 1.9, the 0.9 is lost, so traders pay less than they should.