During the fuzz testing of the smart contract MarginCollateralConfiguration.sol, arithmetic overflow and underflow vulnerabilities were identified. These vulnerabilities occur in the conversion functions where token amounts are converted to UD60x18 format and vice versa.
Location:
Smart Contract: MarginCollateralConfiguration.sol
Functions:
convertTokenAmountToUd60x18
convertUd60x18ToTokenAmount
The convertTokenAmountToUd60x18 function multiplies the input amount by a factor based on the decimals specified. Similarly, convertUd60x18ToTokenAmount divides the input by this factor. However, multiplication and division operations are susceptible to overflow and underflow when dealing with large or very small values.
Root Cause:
Overflow in Multiplication: amount * factor can overflow if amount is large and factor is high.
Division by Zero: Division by factor could cause issues if factor is zero.
The following fuzz tests demonstrate how edge cases can cause overflow or underflow in the conversion functions. Copy the test and run it from test folder.
Run forge test --match-contract FuzzMarginCollateralConfigurationTest -vvv
The fuzz tests revealed that certain inputs cause the system to throw exceptions due to arithmetic underflows or overflows.
Failing tests: Encountered 2 failing tests in test/audit-test/FuzzMarginCollateralConfigurationTest.t.sol:FuzzMarginCollateralConfigurationTest [FAIL. Reason: revert: Decimals exceed system decimals; counterexample: calldata=0xf00e797c00000000000000000000000000000000000000000000000000000000000000b2000000000000000000000000000000000000000000000000000000004e487b70 args=[178, 1313373040 [1.313e9]]] testFuzzConvertTokenAmountToUd60x18(uint8,uint256) (runs: 2, μ: 9832, ~: 9832) [FAIL. Reason: revert: Decimals exceed system decimals; counterexample: calldata=0xa82e0ef200000000000000000000000000000000000000000000000000000000000000b2000000000000000000000000000000000000000000000000000000004e487b70 args=[178, 1313373040 [1.313e9]]] testFuzzConvertUd60x18ToTokenAmount(uint8,uint256) (runs: 2, μ: 9665, ~: 9665)
High
Incorrect token amount conversions can lead to invalid calculations affecting balances, lending, and collateral management, which can disrupt the entire system.
An overflow or underflow can be exploited to manipulate token conversions, leading to potential loss of funds.
Foundry
Range Checks: Implement comprehensive range checks to ensure input values are within acceptable bounds before performing conversions.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.