The identified vulnerability is associated with the _distribute
function in
Distributor.sol
. In scenarios where the total token amount is low or low values are used for percentages, the function may encounter a precision issue.
This arises due to the division of totalAmount by BASIS_POINTS
to calculate the distribution amount for each winner. The precision error can lead to incorrect token distribution, affecting the fairness and accuracy of rewards to winners.
The vulnerability stems from the calculation of amount
within the distribution loop. The formula amount = totalAmount * percentages[i] / BASIS_POINTS
involves a division operation that could result in loss of precision(Rounding to Zero) when dealing with small totalAmount
values or low percentages[i]
. This imprecision can lead to token amounts being rounded down to zero, resulting in unfair or incomplete rewards for winners.
To simulate the vulnerability we need to make changes in the modifier
setUpContestForJasonAndSentJpycv2Token
:
Code:
We change the value transferred to the proxyAddress
to 10
tokens.
Note: We are not using 10 ether
here as ether has 18 decimals which misguides the intended attack.
Now, we create a function called testPrecisionLoss()
wherein we simulate end of a contest and call the deployProxyAndDistribute()
function. This makes use of the modified createData()
function to send in 95
winners, each being rewarded with percentage of 100 BASIS POINTS
, which is equal to (10000 - COMMISSION_FEE)
i.e. 9500 BASIS POINTS
thus satisfying the conditions in the _distribute()
function and allowing distribution of funds.
Code:
The logs prove the existence of precision loss:
As we can see, all of the balance gets transferred to the stadiumAddress
from the contract as the _commissionTransfer()
function declares:
Due to the precision loss(Rounding to Zero), none of the amount gets transferred to any winners and thus, all the "remaining tokens" get sent to the stadiumAddress
, thus leaving our winners - in this case, user1 - rewarded with 0 balance.
The Rounding to Zero vulnerability has the potential to undermine the intended fairness and accuracy of the reward distribution process. In scenarios where the token balance is very small or percentages are low, the distribution algorithm could yield incorrect or negligible rewards to winners. This impacts the trust and credibility of the protocol, potentially leading to user dissatisfaction and decreased participation.
Manual Review
Foundry
VSCode
Consider instituting a predefined minimum threshold
for the percentage
amount used in the token distribution calculation. This approach ensures that the calculated distribution amount maintains a reasonable and equitable value, even when dealing with low percentages.
Additionally, an alternative strategy involves adopting a rounding up equation
that consistently rounds the distribution amount upward
to the nearest integer value.
By incorporating either of these methodologies, the precision vulnerability associated with small values and percentages can be effectively mitigated, resulting in more accurate and reliable token distribution outcomes.
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.