`getCommunityVotingPower` doesn't calculate voting Power correctly due to precision loss
The getCommunityVotingPower function in Staking.sol performs division by PERCENT after each multiplication, causing precision loss that accumulates across three terms. This leads to incorrect community voting power calculations, which could affect governance decisions.
Source: https://github.com/sherlock-audit/2022-11-frankendao-judging/issues/48
Found by
ElKu
Summary
In Staking.sol, the getCommunityVotingPower function, doesn't calculate the votes correctly due to precision loss.
Vulnerability Detail
In getCommunityVotingPower function, the return statement is where the mistake lies in:
return
(votes * cpMultipliers.votes / PERCENT) +
(proposalsCreated * cpMultipliers.proposalsCreated / PERCENT) +
(proposalsPassed * cpMultipliers.proposalsPassed / PERCENT);
Here, after each multiplication by the Multipliers, we immediately divide it by PERCENT. Every time we do a division, there is a certain amount of precision loss. And when its done thrice, the loss just accumulates. So instead, the division by PERCENT should be done after all 3 terms are added together.
Note that this loss is not there, if the Multipliers are a multiple of PERCENT. But these values can be changed through governance later. So its better to be careful assuming that they may not always be a multiple of PERCENT.
Impact
The community voting power of the user is calculated wrongly.
Code Snippet
The getCommunityVotingPower function:
function getCommunityVotingPower(address _voter) public override view returns (uint) {
uint64 votes;
uint64 proposalsCreated;
uint64 proposalsPassed;
// We allow this function to be called with the max uint value to get the total community voting power
if (_voter == address(type(uint160).max)) {
(votes, proposalsCreated, proposalsPassed) = governance.totalCommunityScoreData();
} else {
// This is only the case if they are delegated or unstaked, both of which should zero out the result
if (tokenVotingPower[_voter] == 0) return 0;
(votes, proposalsCreated, proposalsPassed) = governance.userCommunityScoreData(_voter);
}
CommunityPowerMultipliers memory cpMultipliers = communityPowerMultipliers;
return
(votes * cpMultipliers.votes / PERCENT) +
(proposalsCreated * cpMultipliers.proposalsCreated / PERCENT) +
(proposalsPassed * cpMultipliers.proposalsPassed / PERCENT);
}
Tool used
VSCode, Manual Analysis
Recommendation
Do the division once after all terms are added together:
return
( (votes * cpMultipliers.votes) +
(proposalsCreated * cpMultipliers.proposalsCreated) +
(proposalsPassed * cpMultipliers.proposalsPassed) ) / PERCENT;
}
Discussion
zobront
This is a good suggestion but don't think it warrants a Medium. All these values are intended to be > 100. In the event that votes is lowered more, it will always be the case that proposalsCreated & proposalsPassed will be greater than 100, so combining them doesn't improve the accuracy.
El-Ku
Escalate for 5 USDC
If the multipliers are not a multiple of 100, say for example 125, 150 and 175 or something like that, then the precision will be still lost. But as I myself mentioned in the report, if all the multipliers are a multiplier of 100, then these equations dont cause an issue.
Otherwise we should do all the additions before doing the division. Even though the loss is still there it can be minimized by this step.
I think it deserves a medium as this calculation controls many Governance functions.
sherlock-admin
Escalate for 5 USDC
If the multipliers are not a multiple of 100, say for example 125, 150 and 175 or something like that, then the precision will be still lost. But as I myself mentioned in the report, if all the multipliers are a multiplier of 100, then these equations dont cause an issue.
Otherwise we should do all the additions before doing the division. Even though the loss is still there it can be minimized by this step.
I think it deserves a medium as this calculation controls many Governance functions.
You've created a valid escalation for 5 USDC!
To remove the escalation from consideration: Delete your comment. To change the amount you've staked on this escalation: Edit your comment (do not create a new comment).
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
zobront
I think this makes sense and I was wrong to dispute it. It's a change we would like to make and agree there's the potential for some values to calculated incorrectly.
Evert0x
Escalation accepted
sherlock-admin
Escalation accepted
This issue's escalations have been accepted!
Contestants' payouts and scores will be updated according to the changes made on this issue.
zobront
Fixed: https://github.com/Solidity-Guild/FrankenDAO/pull/24
jack-the-pug
Fix confirmed
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 division by PERCENT is performed separately for each term instead of once after summing all terms, causing cumulative precision loss.
Impact
The community voting power of users is calculated incorrectly, potentially affecting governance outcomes, though the impact is limited when multipliers are multiples of PERCENT.
What the attacker needed
Multipliers not multiples of PERCENT
The fix
The issue was fixed by performing the division once after adding all terms together.
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.
When a smart contract calculates voting power, it multiplies numbers and then divides by 100 to get a percentage. If you divide after each multiplication instead of after adding all the results, small rounding errors add up. This can make a user's voting power slightly wrong, which might matter in close votes.