Multiplication after Division error leading to larger precision loss
The smart contract code performs division before multiplication in several places, causing precision loss in financial calculations. An attacker could potentially exploit this to manipulate funding rates or profit/loss accounting, though the report does not specify a concrete attack.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/113
Found by
Breeje, IllIllI, chaduke, joestakey, stopthecap
Summary
There are couple of instance of using result of a division for multiplication while can cause larger precision loss.
Vulnerability Detail
File: MarketUtils.sol
948: cache.fundingUsd = (cache.sizeOfLargerSide / Precision.FLOAT_PRECISION) * cache.durationInSeconds * cache.fundingFactorPerSecond;
952: if (result.longsPayShorts) {
953: cache.fundingUsdForLongCollateral = cache.fundingUsd * cache.oi.longOpenInterestWithLongCollateral / cache.oi.longOpenInterest;
954: cache.fundingUsdForShortCollateral = cache.fundingUsd * cache.oi.longOpenInterestWithShortCollateral / cache.oi.longOpenInterest;
955: } else {
956: cache.fundingUsdForLongCollateral = cache.fundingUsd * cache.oi.shortOpenInterestWithLongCollateral / cache.oi.shortOpenInterest;
957: cache.fundingUsdForShortCollateral = cache.fundingUsd * cache.oi.shortOpenInterestWithShortCollateral / cache.oi.shortOpenInterest;
958: }
In above case, value of cache.fundingUsd is calculated by first dividing cache.sizeOfLargerSide with Precision.FLOAT_PRECISION which is 10**30. Then the resultant is multiplied further. This result in larger Loss of precision.
Later the same cache.fundingUsd is used to calculate cache.fundingUsdForLongCollateral and cache.fundingUsdForShortCollateral by multiplying further which makes the precision error even more big.
Same issue is there in calculating cache.positionPnlUsd in PositionUtils.
File: PositionUtils.sol
if (position.isLong()) {
cache.sizeDeltaInTokens = Calc.roundUpDivision(position.sizeInTokens() * sizeDeltaUsd, position.sizeInUsd());
} else {
cache.sizeDeltaInTokens = position.sizeInTokens() * sizeDeltaUsd / position.sizeInUsd();
}
}
cache.positionPnlUsd = cache.totalPositionPnl * cache.sizeDeltaInTokens.toInt256() / position.sizeInTokens().toInt256();
Impact
Precision Loss in accounting.
Code Snippet
Given above.
Tool used
Manual Review
Recommendation
First Multiply all the numerators and then divide it by the product of all the denominator.
Discussion
xvi10
it is calculated in this way to reduce the risk of the multiplication causing an overflow
IllIllI000
@xvi10 you can use unchecked blocks to avoid phantom overflows:
pragma solidity 0.8.17;
import "forge-std/Test.sol";
contract It is Test {
function testItOk() external pure returns (uint256) {
uint256 v1 = type(uint256).max;
uint256 result;
unchecked {
result = (3 * v1 * v1) / (v1 * v1);
}
return result;
}
function testItBroken() external pure returns (uint256) {
uint256 v1 = type(uint256).max;
uint256 result;
result = (3 * v1 * v1) / (v1 * v1);
return result;
}
}
$ forge test -vvvv
[⠰] Compiling...
No files changed, compilation skipped
Running 2 tests for src/T.sol:It
[FAIL. Reason: Arithmetic over/underflow] testItBroken():(uint256) (gas: 270)
Traces:
[270] It::testItBroken()
└─ ← "Arithmetic over/underflow"
[PASS] testItOk():(uint256) (gas: 148)
Traces:
[148] It::testItOk()
└─ ← 3
Test result: FAILED. 1 passed; 1 failed; finished in 650.57µs
Failing tests:
Encountered 1 failing test in src/T.sol:It
[FAIL. Reason: Arithmetic over/underflow] testItBroken():(uint256) (gas: 270)
Encountered a total of 1 failing tests, 1 tests succeeded
xvi10
i see, ok we can look into it
xvi10
looked into https://github.com/Uniswap/v3-core/blob/412d9b236a1e75a98568d49b1aeb21e3a1430544/contracts/libraries/FullMath.sol#L8, but the code needs to be updated to support Solidity ^0.8.0
for now we have improved the precision of the calculations
Fix in: https://github.com/gmx-io/gmx-synthetics/commit/93832021772e4020a66ac7260854eb6ace11230e, https://github.com/gmx-io/gmx-synthetics/commit/89f4e8cc8cddbd8d7f5925f032e0419d6b6228ee
xvi10
There is an implementation in the OpenZeppelin Math library, have updated the code to use that instead: https://github.com/gmx-io/gmx-synthetics/commit/26ecb0c125b1e8873d2f165d200b649b0ea0dcab
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 code divides before multiplying to avoid overflow, but this introduces precision loss.
Impact
Precision loss in accounting could lead to incorrect funding rates and profit/loss calculations, potentially allowing an attacker to exploit the discrepancy.
What the attacker needed
None
The fix
The code was fixed by improving precision, using OpenZeppelin's Math library.
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 stored as integers, so division can lose precision. If you divide first and then multiply, the error gets bigger. This report points out places where the code does that, which could lead to incorrect financial calculations.