Throughout LiquidityMining.sol the values for currWeek and nextWeek are generated using the lastAccrued timestamp embedded in a local variable time. currWeek is determined by
It can be clearly seen that division before multiplication is implemented to derive these values which is prone to precision loss, for example in conditions where time is divided by WEEK there will be a huge loss of precision in the value as roundings could occur to the value before be multiplied, you can see that dt is later multiplied with liquidity and accrued to timeWeightedWeeklyPositionAmbLiquidity_ which is later used to calculate the rewardForWeek
for each week that the user claims, when the values of (timeWeightedWeeklyPositionAmbLiquidity_[poolIdx][posKey][week] x ambRewardPerWeek_[poolIdx][week]) is divided by a lower timeWeightedWeeklyPositionAmbLiquidity_ the rewards the user gets will be higher.
There are different Instances of this multiplication on the result of a division which can lead to users claiming incorrect amount of rewards.
Wrong values for these variables are generated, which in turn when used to calculate the rewards for a liquidity provider will result in an incorrect amount of rewards given to LPs.
This will cause some LPs to get less amount of rewards, and some get more amount of rewards than supposed to.
Proof of Concept
Whenever currWeek and nextWeek are generated
Division before multiplication is used to calculate its values
In a situation where rounding after division occurs.
Wrong values are generated which are then used to calculate incorrect amounts to be given to LPs when the claim rewards within the protocol.
Tools Used
Manual Review
Recommended Mitigation Steps
The use of multiplication before division is advised as it is less prone to precision loss, Multiply before you divide or use safeMath library.
Lines of code
https://github.com/code-423n4/2023-10-canto/blob/37a1d64cf3a10bf37cbc287a22e8991f04298fa0/canto_ambient/contracts/mixins/LiquidityMining.sol#L51-L52 https://github.com/code-423n4/2023-10-canto/blob/37a1d64cf3a10bf37cbc287a22e8991f04298fa0/canto_ambient/contracts/mixins/LiquidityMining.sol#L96-L97 https://github.com/code-423n4/2023-10-canto/blob/37a1d64cf3a10bf37cbc287a22e8991f04298fa0/canto_ambient/contracts/mixins/LiquidityMining.sol#L208-L209 https://github.com/code-423n4/2023-10-canto/blob/37a1d64cf3a10bf37cbc287a22e8991f04298fa0/canto_ambient/contracts/mixins/LiquidityMining.sol#L238-L239
Vulnerability details
Throughout
LiquidityMining.sol
the values forcurrWeek
andnextWeek
are generated using thelastAccrued
timestamp embedded in a local variabletime
.currWeek
is determined byAnd
nextWeek
is calculated byIt can be clearly seen that
division
beforemultiplication
is implemented to derive these values which is prone to precision loss, for example in conditions wheretime
is divided byWEEK
there will be a huge loss of precision in the value as roundings could occur to the value before be multiplied, you can see thatdt
is later multiplied withliquidity
and accrued totimeWeightedWeeklyPositionAmbLiquidity_
which is later used to calculate therewardForWeek
for each week that the user claims, when the values of
(timeWeightedWeeklyPositionAmbLiquidity_[poolIdx][posKey][week]
xambRewardPerWeek_[poolIdx][week])
is divided by a lowertimeWeightedWeeklyPositionAmbLiquidity_
the rewards the user gets will be higher.There are different Instances of this multiplication on the result of a division which can lead to users claiming incorrect amount of rewards.
timeWeightedWeeklyGlobalConcLiquidity_
timeWeightedWeeklyPositionInRangeConcLiquidity_
timeWeightedWeeklyGlobalAmbLiquidity_
timeWeightedWeeklyPositionAmbLiquidity_
Impact
Wrong values for these variables are generated, which in turn when used to calculate the rewards for a liquidity provider will result in an incorrect amount of rewards given to LPs. This will cause some LPs to get less amount of rewards, and some get more amount of rewards than supposed to.
Proof of Concept
currWeek
andnextWeek
are generatedTools Used
Manual Review
Recommended Mitigation Steps
The use of
multiplication
beforedivision
is advised as it is less prone to precision loss, Multiply before you divide or use safeMath library.Assessed type
Math