QuantAMM

QuantAMM
49,600 OP
View results
Submission Details
Severity: low
Invalid

Missing Zero Check for updateInterval Can Lead to _calculateMultiplerAndSetWeights failure due to division by zero

updateInterval parameter serves two main purposes:

Vulnerability Details

In UpdateWeightRunner.sol There's no zero check for _poolSettings.updateInterval value when setting it in setRuleForPool.

poolRuleSettings[msg.sender] = PoolRuleSettings({
lambda: _poolSettings.lambda,
epsilonMax: _poolSettings.epsilonMax,
absoluteWeightGuardRail: _poolSettings.absoluteWeightGuardRail,
ruleParameters: _poolSettings.ruleParameters,
timingSettings: PoolTimingSettings({ updateInterval: _poolSettings.updateInterval, lastPoolUpdateRun: 0 }),
poolManager: _poolSettings.poolManager
});

It's later used in calculating the blockMultiplier #472 in _calculateMultiplerAndSetWeights function.

int256 blockMultiplier = (local.updatedWeights[i] - local.currentWeights[i]) / local.updateInterval;

Division by zero will result in reverting the transaction.

Impact

_calculateMultiplerAndSetWeights is used in:

Division by zero makes it impossible to calculate the weights and update them.

POC

function testUpdatesNotSuccessfullyAfterUpdateInterval() public {
int256[] memory initialWeights = new int256[]();
initialWeights[0] = 0.0000000005e18;
initialWeights[1] = 0.0000000005e18;
initialWeights[2] = 0;
initialWeights[3] = 0;
// Set initial weights
mockPool.setInitialWeights(initialWeights);
mockPool.setPoolRegistry(9);
vm.startPrank(owner);
updateWeightRunner.setApprovedActionsForPool(address(mockPool), 9);
vm.stopPrank();
int216 fixedValue = 1000;
chainlinkOracle = deployOracle(fixedValue, 3601);
vm.startPrank(owner);
updateWeightRunner.addOracle(OracleWrapper(chainlinkOracle));
vm.stopPrank();
vm.startPrank(address(mockPool));
address[][] memory oracles = new address[][]();
oracles[0] = new address[]();
oracles[0][0] = address(chainlinkOracle);
uint64[] memory lambda = new uint64[]();
lambda[0] = 0.0000000005e18;
updateWeightRunner.setRuleForPool(
IQuantAMMWeightedPool.PoolSettings({
assets: new IERC20[](0),
rule: IUpdateRule(mockRule),
oracles: oracles,
updateInterval: 0, // Assigning updateInterval by zero
lambda: lambda,
epsilonMax: 0.2e18,
absoluteWeightGuardRail: 0.2e18,
maxTradeSizeRatio: 0.2e18,
ruleParameters: new int256[][](),
poolManager: addr2
})
);
vm.stopPrank();
vm.startPrank(addr2);
updateWeightRunner.InitialisePoolLastRunTime(address(mockPool), 1);
vm.stopPrank();
vm.warp(block.timestamp + 10000000);
mockRule.CalculateNewWeights(
initialWeights,
new int256[](0),
address(mockPool),
new int256[][](),
new uint64[](0),
0.2e18,
0.2e18
);
vm.expectRevert(stdError.divisionError);
updateWeightRunner.performUpdate(address(mockPool));
}
function testCalculateMultiplierAndSetWeightsFromRuleFailureDueToDivisionByZero() public {
int256[] memory weights = new int256[]();
weights[0] = 0.5e18;
weights[1] = 0.5e18;
weights[2] = 0;
weights[3] = 0;
mockPool.setPoolRegistry(8);
vm.startPrank(owner);
updateWeightRunner.setApprovedActionsForPool(address(mockPool), 8);
vm.stopPrank();
uint40 blockTime = uint40(block.timestamp);
int216 fixedValue = 1000;
uint delay = 3600;
chainlinkOracle = deployOracle(fixedValue, delay);
vm.startPrank(owner);
updateWeightRunner.addOracle(OracleWrapper(chainlinkOracle));
vm.stopPrank();
vm.startPrank(address(mockPool));
address[][] memory oracles = new address[][]();
oracles[0] = new address[]();
oracles[0][0] = address(chainlinkOracle);
uint64[] memory lambda = new uint64[]();
lambda[0] = 0.0000000005e18;
updateWeightRunner.setRuleForPool(
IQuantAMMWeightedPool.PoolSettings({
assets: new IERC20[](0),
rule: mockRule,
oracles: oracles,
updateInterval: 100,
lambda: lambda,
epsilonMax: 0.2e18,
absoluteWeightGuardRail: 0.2e18,
maxTradeSizeRatio: 0.2e18,
ruleParameters: new int256[][](),
poolManager: addr2
})
);
vm.stopPrank();
vm.startPrank(addr2);
updateWeightRunner.InitialisePoolLastRunTime(address(mockPool), blockTime);
vm.stopPrank();
vm.startPrank(addr2);
updateWeightRunner.setWeightsManually(weights, address(mockPool), 6, 2);
vm.stopPrank();
mockPool.setPoolRegistry(32);
int256[] memory poolWeights = new int256[]();
poolWeights[0] = 0.6e18;
poolWeights[1] = 0.4e18;
uint256[] memory currentWeightsUnsigned = IWeightedPool(address(mockPool)).getNormalizedWeights();
int256[] memory currentWeights = new int256[]();
for (uint i; i < currentWeights.length; ) {
currentWeights[i] = int256(currentWeightsUnsigned[i]);
unchecked {
i++;
}
}
vm.startPrank(address(mockRule));
vm.expectRevert(stdError.divisionError);
updateWeightRunner.calculateMultiplierAndSetWeightsFromRule(UpdateWeightRunner.CalculateMuliplierAndSetWeightsLocal({
currentWeights: currentWeights,
updatedWeights: poolWeights,
updateInterval: 0, // Assigning zero to updateInterval in the local rule
absoluteWeightGuardRail18: 0.2e18,
poolAddress: address(mockPool)
})
);
vm.stopPrank();
}
forge test --match-test testUpdatesNotSuccessfullyAfterUpdateInterval -vv --root pkg/pool-quantamm
Ran 1 test for test/foundry/UpdateWeightRunner.t.sol:UpdateWeightRunnerTest
[PASS] testUpdatesNotSuccessfullyAfterUpdateInterval() (gas: 678300)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 2.76ms (821.13µs CPU time)
forge test --match-test testCalculateMultiplierAndSetWeightsFromRuleFailureDueToDivisionByZero -vv --root pkg/pool-quantamm
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 2.88ms (180.38µs CPU time)

Tools Used

Manual Review

Recommendations

Add a requirement that it's bigger than zero.

require(_poolSettings.updateInterval > 0, "Update interval must be positive");

And make a condition that if it's zero the blockMultiplier will be its default (0)

int256 blockMultiplier = 0;
if(local.updateInterval > 0){
// this would be the simple scenario if we did not have to worry about guard rails
blockMultiplier = (local.updatedWeights[i] - local.currentWeights[i]) / local.updateInterval;
}
Updates

Lead Judging Commences

n0kto Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational or Gas / Admin is trusted / Pool creation is trusted / User mistake / Suppositions

Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelyhood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!