math miscalculation artificially deflates scores
A math error in the consensus score calculation causes scores to be artificially deflated when a consensus loss is zero. An attacker could exploit this to unfairly lower scores in rare cases.
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/104
Found by
0x3b
Summary
When GetConsensusScore calculates a score, it has a chance to artificially deflate the value, due to a a check the changes consensusLosses from 0 to epsilon.
Vulnerability Detail
How scores are calculated
GetConsensusScore calculates scores using the bellow formula:
$$ \frac{1}{\left(\frac{\sqrt{\sum (rLoss - \text{consensusLosses})^2}}{\sqrt{\sum \text{consensusLosses}^2}}\right) + \text{tolerance}} $$
Also represented as:
1 / (Sqrt(sum((rLoss - consensusLosses)^2)) / Sqrt(sum(consensusLosses^2)) + tolerance)
Breaking it down, we can find that Sqrt(sum(consensusLosses^2)) is just the square root of all consensusLosses * consensusLosses
for _, cLoss := range consensusLosses {
cLossSquared, err := cLoss.Mul(cLoss)
if err != nil {
return alloraMath.ZeroDec(), err
}
sumConsensusSquared, err = sumConsensusSquared.Add(cLossSquared)
if err != nil {
return alloraMath.ZeroDec(), err
}
}
// Sqrt(sum(consensusLosses^2))
consensusNorm, err := sumConsensusSquared.Sqrt()
And (Sqrt(sum((rLoss - consensusLosses)^2)) is the same thing, but with 1 caveat, it's not consensusLosses, but rLoss - consensusLosses.
for i, rLoss := range reputerLosses {
...
rLossLessConsensusLoss, err := rLoss.Sub(consensusLosses[i])
if err != nil {
return alloraMath.ZeroDec(), err
}
rLossLessCLossSquared, err := rLossLessConsensusLoss.Mul(rLossLessConsensusLoss) // == Pow(x,2)
if err != nil {
return alloraMath.ZeroDec(), err
}
distanceSquared, err = distanceSquared.Add(rLossLessCLossSquared)
if err != nil {
return alloraMath.ZeroDec(), err
}
}
distance, err := distanceSquared.Sqrt()
if err != nil {
return alloraMath.ZeroDec(), err
}
We can see the final part where our distance (Sqrt(sum((rLoss - consensusLosses)^2))) is divided by consensusNorm Sqrt(sum(consensusLosses^2)), where the issue takes place.
distanceOverConsensusNorm, err := distance.Quo(consensusNorm)
if err != nil {
return alloraMath.ZeroDec(), err
}
Where the vulnerabilities takes place
Before this division, during the for loop for calculatingdistance we have an interesting if, which changes the value of consensusLosses to epsilon.
if consensusLosses[i].IsZero() {
consensusLosses[i] = epsilon
}
However that if impacts our formula only on the left side, as consensusNorm is already calculated as Sqrt(sum(consensusLosses^2)), where one of these (or more) consensusLosses is 0. After this if our right side stays the same but our left side changes from Sqrt(sum((rLoss - 0)^2)) to Sqrt(sum((rLoss - epsilon)^2))
Sqrt(sum((rLoss - consensusLosses)^2)) / Sqrt(sum(consensusLosses^2))
This will cause our result from the above equation to be bigger, because we didn't include if consensusLosses == 0 then consensusLosses = epsilon in our divisor (Sqrt(sum(consensusLosses^2))). Getting this part bigger will cause our score to be smaller, since the final score is the 1 / the above formula + tolerance
1 / (Sqrt(sum((rLoss - consensusLosses)^2)) / Sqrt(sum(consensusLosses^2)) + tolerance)
Impact
In rare cases when consensusLosses == 0 scores are deflated, more than they should be.
Code Snippet
if consensusLosses[i].IsZero() {
consensusLosses[i] = epsilon
}
Tool used
Manual Review
Recommendation
In the cases where consensusLosses == 0, make sure after setting it to epsilon you also include it in the consensusNorm.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/507
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
The code replaces zero consensus losses with epsilon only when computing the distance, but not when computing the consensus norm, leading to an inconsistent formula.
What the attacker needed
ConsensusLosses equals zero
The fix
The protocol team fixed this issue in a pull request.
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.
This report is about a bug in a smart contract that calculates scores for participants. When one of the inputs is zero, the code replaces it with a tiny number in one part of the calculation but not in another, making the final score smaller than it should be. This could unfairly lower someone's score.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.