If old coefficient is bigger than the new one then the reputer has it's coeff reduced more than it should
The report claims that in the Allora blockchain's reward calculation, when the old listened stake fraction is greater than the new one, a subtraction yields a negative value that is used as a divisor, causing reputer coefficients to be reduced below the minimum allowed fraction. This could break the minStakeFraction invariant and unfairly reduce reputer rewards.
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/92
The protocol has acknowledged this issue.
Found by
0x3b
Summary
If the old coefficient are bigger than the new coefficient, then our reputer's coefficients will be set to smaller value which will be lower than the minimum limit, essentially breaking the minStakeFraction invariant.
Vulnerability
The root cause of this issue is located in GetAllReputersOutput, in the place where if listenedStakeFraction < minStakeFraction we calculate a custom coeff.
if listenedStakeFraction.Lt(minStakeFraction) {
for l := range coefficients {
coeffDiff, err := coefficients[l].Sub(oldCoefficients[l])
if err != nil {
return nil, nil, err
}
listenedDiff, err := minStakeFraction.Sub(listenedStakeFractionOld)
if err != nil {
return nil, nil, err
}
//@audit what if old > new, then this will be negative ?
stakedFracDiff, err := listenedStakeFraction.Sub(listenedStakeFractionOld)
if err != nil {
return nil, nil, err
}
If our listenedStakeFractionOld is bigger than our listenedStakeFraction then stakedFracDiff will be negative. This value is later used to calculate our new coefficients bellow, where we first calculate the diff - coeffDiffTimesListenedDiff, then divide it by our fraction diff (where the actual issue is) and finally add it to oldCoefficients to get the new coefficients.
if stakedFracDiff.IsZero() {
i = gradientDescentMaxIters
} else {
coeffDiffTimesListenedDiff, err := coeffDiff.Mul(listenedDiff)
if err != nil {
return nil, nil, err
}
coefDiffTimesListenedDiffOverStakedFracDiff, err := coeffDiffTimesListenedDiff.Quo(stakedFracDiff)
if err != nil {
return nil, nil, err
}
coefficients[l], err = oldCoefficients[l].Add(coefDiffTimesListenedDiffOverStakedFracDiff)
if err != nil {
return nil, nil, err
}
}
In short, if listenedStakeFraction is bellow the min allowed fraction - minStakeFraction (MinStakeFraction in the params) then we do some custom math to increase a our reputer coefficient. However as stated above that is not the case when our listenedStakeFraction is less than listenedStakeFractionOld.
Path
We got over why it happens, now lets cover the context around it - aka. how it can happen.
GetAllReputersOutput (where the issue is), is called inside GenerateReputerScores in order to calculate our scores and coefficients. Both old and new fractions are calculated with the bellow formulas:
listenedStakeFractionOld = sum(oldCoefficients * stakes) / sum(stakes) code is here
listenedStakeFraction = sum(newCoefficients * stakes) / sum(stakes) code is here
In order to understand these fractions better we need to look into how they are calculated. listenedStakeFractionOld is just the last fraction from the prev. epoch, so I will skip it's math (because it's the same).
Our newCoefficients is calculated from this code snippet, implementing this formula:
min(
max(
coefficient + (learningRate * gradient)
, 0)
, 1)
Where:
coefficientis our old coeff.learningRateis a global fixed parameter controlling the rate of system improvement.gradientis the variable that can most dramatically change ournewCoefficients.
gradient is calculated with the bellow formula:
gradient[i] = (1 - sum(score * stake) / sum(score2 * stake)) / (0.001 || -0.001) code is here
Where our dcoeff, aka the last part - / (0.001 || -0.001) will divide our numerator - (1 - sum(score * stake) / sum(score2 * stake)) by 0.001 or -0.001, based on if our prev. coeff was 1. Division by numbers smaller than 1 is same as multiplication, so this can be interpreted as * (1000 || -1000).
dcoeff := alloraMath.MustNewDecFromString("0.001")
if coefficients[l].Equal(alloraMath.OneDec()) {
dcoeff = alloraMath.MustNewDecFromString("-0.001")
}
With all of that math behind us we can conclude that in order for listenedStakeFraction to be less than listenedStakeFractionOld our gradient needs to change. The bad news is that our gradient changes on every epoch with every score calculation, as if our prev. coeff was 1, our gradient will flip to negative, impacting slightly listenedStakeFraction, potentially making it smaller than listenedStakeFractionOld.
Impact
The effects of all of that are:
- Breaking of invariant like
minStakeFraction, as it should increase the fraction slighly when we are bellowminStakeFraction, but instead it reduces it even more. - Reducing reputers rewards, by reducing their coefficient more than it should. Rewards are calculated based on scores and scores are calculates with these coefficients.
Code Snippet
//@audit what if old > new, then this will be negative ?
stakedFracDiff, err := listenedStakeFraction.Sub(listenedStakeFractionOld)
...
} else {
coeffDiffTimesListenedDiff, err := coeffDiff.Mul(listenedDiff)
if err != nil {
return nil, nil, err
}
coefDiffTimesListenedDiffOverStakedFracDiff, err := coeffDiffTimesListenedDiff.Quo(stakedFracDiff)
if err != nil {
return nil, nil, err
}
coefficients[l], err = oldCoefficients[l].Add(coefDiffTimesListenedDiffOverStakedFracDiff)
if err != nil {
return nil, nil, err
}
}
Tool used
Manual Review
Recommendation
A good solution would be to .abs() the value in order to make sure it is always positive.
Discussion
sherlock-admin3
1 comment(s) were left on this issue during the judging contest.
0xmystery commented:
If old coefficient is larger than the new coefficient then the reputer has its coefficient incorrectly calculated
jmdkastro
This is working as intended and simply follows linear interpolation, where indeed it can be necessary to subtract a small delta from the _old_ coefficient to arrive at the minStakeFraction -> this issue does not require acting on.
jmdkastro
As for the second part (dcoeff flipping sign), when this happens also the difference between score and score2 flips sign, and therefore the sign of the gradient does not change, contrary to what is stated in this issue.
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
Likely the code subtracts the old listened stake fraction from the new one without checking which is larger, so when the old is larger the result is negative and used as a divisor, flipping the intended adjustment.
Impact
The report claims this could break the minStakeFraction invariant and reduce reputer rewards, but the protocol disputed it, stating the behaviour is intended linear interpolation and the sign-flip concern is incorrect.
What the attacker needed
None
The fix
The report suggested taking the absolute value of the difference, but the protocol stated no action is required.
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 is about a blockchain that rewards people for providing data. The code calculates a 'coefficient' that affects rewards. The report says a math mistake can make this coefficient too small when it should be increased, which could unfairly lower rewards.
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.