Calling StandardFunding.screeningVote function and ExtraordinaryFunding.voteExtraordinary function when block.number equals respective start block and when block.number is bigger than respective start block can result in different available votes for same voter
Because of if (block.number <= screeningStageEndBlock || block.number > endBlock) revert InvalidVote(), the following StandardFunding.fundingVote function can only execute uint128 newVotingPower = SafeCast.toUint128(_getVotesFunding(msg.sender, votingPower, voter.remainingVotingPower, screeningStageEndBlock)) when block.number is bigger than screeningStageEndBlock.
function fundingVote(
FundingVoteParams[] memory voteParams_
) external override returns (uint256 votesCast_) {
uint24 currentDistributionId = _currentDistributionId;
QuarterlyDistribution storage currentDistribution = _distributions[currentDistributionId];
QuadraticVoter storage voter = _quadraticVoters[currentDistributionId][msg.sender];
uint256 endBlock = currentDistribution.endBlock;
uint256 screeningStageEndBlock = _getScreeningStageEndBlock(endBlock);
// check that the funding stage is active
if (block.number <= screeningStageEndBlock || block.number > endBlock) revert InvalidVote();
uint128 votingPower = voter.votingPower;
// if this is the first time a voter has attempted to vote this period,
// set initial voting power and remaining voting power
if (votingPower == 0) {
// calculate the voting power available to the voting power in this funding stage
uint128 newVotingPower = SafeCast.toUint128(_getVotesFunding(msg.sender, votingPower, voter.remainingVotingPower, screeningStageEndBlock));
voter.votingPower = newVotingPower;
voter.remainingVotingPower = newVotingPower;
}
...
}
When the StandardFunding.fundingVote function calls the following StandardFunding._getVotesFunding function, screeningStageEndBlock would be used as the voteStartBlock_ input for calling the Funding._getVotesAtSnapshotBlocks function below. Because block.number would always be bigger than screeningStageEndBlock, voteStartBlock_ would always be screeningStageEndBlock in the Funding._getVotesAtSnapshotBlocks function. This means that the Funding._getVotesAtSnapshotBlocks function would always return the same voting power for the same voter at any block.number that is bigger than screeningStageEndBlock during the funding phase.
function _getVotesAtSnapshotBlocks(
address account_,
uint256 snapshot_,
uint256 voteStartBlock_
) internal view returns (uint256) {
IVotes token = IVotes(ajnaTokenAddress);
// calculate the number of votes available at the snapshot block
uint256 votes1 = token.getPastVotes(account_, snapshot_);
// enable voting weight to be calculated during the voting period's start block
voteStartBlock_ = voteStartBlock_ != block.number ? voteStartBlock_ : block.number - 1;
// calculate the number of votes available at the stage's start block
uint256 votes2 = token.getPastVotes(account_, voteStartBlock_);
return Maths.min(votes2, votes1);
}
However, because of if (block.number < currentDistribution.startBlock || block.number > _getScreeningStageEndBlock(currentDistribution.endBlock)) revert InvalidVote(), calling the following StandardFunding.screeningVote function would not revert when block.number equals the current distribution period’s start block.
function screeningVote(
ScreeningVoteParams[] memory voteParams_
) external override returns (uint256 votesCast_) {
QuarterlyDistribution memory currentDistribution = _distributions[_currentDistributionId];
// check screening stage is active
if (block.number < currentDistribution.startBlock || block.number > _getScreeningStageEndBlock(currentDistribution.endBlock)) revert InvalidVote();
uint256 numVotesCast = voteParams_.length;
for (uint256 i = 0; i < numVotesCast; ) {
Proposal storage proposal = _standardFundingProposals[voteParams_[i].proposalId];
// check that the proposal is part of the current distribution period
if (proposal.distributionId != currentDistribution.id) revert InvalidVote();
uint256 votes = voteParams_[i].votes;
// cast each successive vote
votesCast_ += votes;
_screeningVote(msg.sender, proposal, votes);
unchecked { ++i; }
}
}
When the StandardFunding.screeningVote function calls the following StandardFunding._screeningVote function, if (screeningVotesCast[distributionId][account_] + votes_ > _getVotesScreening(distributionId, account_)) revert InsufficientVotingPower() is executed in which the StandardFunding._getVotesScreening function below would use the current distribution period’s start block as the voteStartBlock_ input for calling the Funding._getVotesAtSnapshotBlocks function. Since the Funding._getVotesAtSnapshotBlocks function executes voteStartBlock_ = voteStartBlock_ != block.number ? voteStartBlock_ : block.number - 1, voteStartBlock_ would be 1 block prior to the current distribution period’s start block when block.number equals the current distribution period’s start block, and voteStartBlock_ would be the current distribution period’s start block when block.number is bigger than the current distribution period’s start block. However, it is possible that the same voter has different available votes at 1 block prior to the current distribution period’s start block and at the current distribution period’s start block. This is unlike the StandardFunding._getVotesFunding function that would always return the same voting power for the same voter when calling the StandardFunding.fundingVote function during the funding phase. Since calling the StandardFunding._getVotesScreening function when block.number equals the current distribution period’s start block and when block.number is bigger than the current distribution period’s start block during the screening phase can return different available votes for the same voter, this voter would call the StandardFunding.screeningVote function at a chosen block.number that would provide the highest votes.
This should not be allowed because _getVotesScreening(distributionId, account_) needs to return the same number of votes across all blocks during the screening phase to make if (screeningVotesCast[distributionId][account_] + votes_ > _getVotesScreening(distributionId, account_)) revert InsufficientVotingPower() effective in the StandardFunding._screeningVote function. For example, a voter who has no available votes at 1 block prior to the current distribution period’s start block can mint many AJNA tokens at the current distribution period’s start block and call the StandardFunding.screeningVote function at block.number that is bigger than the current distribution period’s start block during the screening phase to use her or his available votes at current distribution period’s start block. For another example, a voter who has available votes at 1 block prior to the current distribution period’s start block can call the StandardFunding.screeningVote function when block.number equals the current distribution period’s start block and then sell all of her or his AJNA tokens at the same block.number. Such voters’ actions are unfair to other voters who maintain the same number of available votes at 1 block prior to the current distribution period’s start block and at the current distribution period’s start block for the screening stage voting.
function _screeningVote(
address account_,
Proposal storage proposal_,
uint256 votes_
) internal {
uint24 distributionId = proposal_.distributionId;
// check that the voter has enough voting power to cast the vote
if (screeningVotesCast[distributionId][account_] + votes_ > _getVotesScreening(distributionId, account_)) revert InsufficientVotingPower();
...
}
function _getVotesScreening(uint24 distributionId_, address account_) internal view returns (uint256 votes_) {
uint256 startBlock = _distributions[distributionId_].startBlock;
// calculate voting weight based on the number of tokens held at the snapshot blocks of the screening stage
votes_ = _getVotesAtSnapshotBlocks(
account_,
startBlock - VOTING_POWER_SNAPSHOT_DELAY,
startBlock
);
}
Please note that calling the following ExtraordinaryFunding.voteExtraordinary function when block.number equals _extraordinaryFundingProposals[proposalId_].startBlock also does not revert, and the ExtraordinaryFunding._getVotesExtraordinary function below also uses _extraordinaryFundingProposals[proposalId_].startBlock as the voteStartBlock_ input for calling the Funding._getVotesAtSnapshotBlocks function. Hence, the same issue also applies to the ExtraordinaryFunding.voteExtraordinary function.
The following steps can occur for the described scenario.
At 1 block prior to the current distribution period’s start block, Alice has no available votes at all.
After noticing the StandardFunding.startNewDistributionPeriod transaction that would end the previous distribution period and starts the current distribution period in the mempool, Alice backruns that transaction by minting a lot of AJNA tokens at the current distribution period’s start block.
When block.number becomes bigger than the current distribution period’s start block during the screening phase, Alice calls the StandardFunding.screeningVote function to successfully use all of her available votes at the current distribution period’s start block for a proposal.
Alice’s actions are unfair to Bob who prepares for the screening stage voting and maintains the same number of available votes at 1 block prior to the current distribution period’s start block and at the current distribution period’s start block.
@MikeHathaway - I think this issue is valid: it is not about different phases but about what happens at voteStartBlock_ == block.number versus voteStartBlock_ == block.number + 1.
Sorry for any confusion. At 1 block prior to the current distribution period's start block, Alice has no available votes at all in the POC section means that Alice has no available votes at that particular block that is 1 block prior to the current distribution period’s start block; yet, she can still have available votes at blocks before that particular block. When Alice has available votes at the block that is 33 blocks prior to the current distribution period’s start block, it is possible that she uses all of her available votes at the current distribution period’s start block for the scenario described in the POC section.
If Alice has no voting power 1 block prior to start, than at the start block she will have no voting power if she doesn’t transfer and delegate tokens to that address. Prior voting power won’t matter, as we take the minimum of the prior snapshot and the current snapshot.
If Alice mints or receives AJNA tokens at the current distribution period’s start block, can she gain available votes at the current distribution period’s start block? If so, she can have voting power at the current distribution period’s start block while has 0 available votes at 1 block prior to the current distribution period’s start block. Please correct me if I am understanding this incorrectly.
That is incorrect, the snapshot system requires that the balance be > 0 for the entire 33 blocks prior to the distribution period’s start. If any block is 0, their power is 0.
This can be verified with a quick unit test:
function testVotingPowerAtDistributionStart() external {
// 14 tokenholders self delegate their tokens to enable voting on the proposals
_selfDelegateVoters(_token, _votersArr);
// roll 32 blocks forward to the block before the distribution period starts
vm.roll(_startBlock + 32);
// _tokenHolder1 transfers their tokens away
address nonVotingAddress = makeAddr("nonVotingAddress");
changePrank(_tokenHolder1);
_token.transfer(nonVotingAddress, 25_000_000 * 1e18);
vm.roll(_startBlock + 33);
// nonVotingAddress returns the funds one block later
changePrank(nonVotingAddress);
_token.transfer(_tokenHolder1, 25_000_000 * 1e18);
// start distribution period
_startDistributionPeriod(_grantFund);
// check voting power of _tokenHolder1 is 0
uint256 votingPower = _getScreeningVotes(_grantFund, _tokenHolder1);
assertEq(votingPower, 0);
votingPower = _getScreeningVotes(_grantFund, nonVotingAddress);
assertEq(votingPower, 0);
}
The thing is that, to vote in the current distribution period, Alice does not have to vote at the current distribution period’s start block and can vote at an eligible block after the current distribution period’s start block as stated in Step 3 of the POC section. When voting at such eligible block, Alice can utilize the available votes that she gained at the start block. To demonstrate this, I’ve made some modifications to your unit test below, which does pass. Please see @audit for the places of modification. For this test, at _startBlock + 33, which is the start block, _tokenHolder1’s voting power is 0; however, at _startBlock + 34 and _startBlock + 35, _tokenHolder1’s voting power becomes 25000000 * 1e18. The inconsistency between the voting power when voting at the start block and when voting at an eligible block after the start block is the main point of this report.
function testVotingPowerAtDistributionStart() external {
// 14 tokenholders self delegate their tokens to enable voting on the proposals
_selfDelegateVoters(_token, _votersArr);
// roll 32 blocks forward to the block before the distribution period starts
vm.roll(_startBlock + 32);
// _tokenHolder1 transfers their tokens away
address nonVotingAddress = makeAddr("nonVotingAddress");
changePrank(_tokenHolder1);
// @audit transfer _token.balanceOf(_tokenHolder1) so _tokenHolder1 does not have any AJNA at _startBlock + 32
_token.transfer(nonVotingAddress, _token.balanceOf(_tokenHolder1));
vm.roll(_startBlock + 33);
// nonVotingAddress returns the funds one block later
changePrank(nonVotingAddress);
_token.transfer(_tokenHolder1, 25_000_000 * 1e18);
// start distribution period
_startDistributionPeriod(_grantFund);
// check voting power of _tokenHolder1 is 0
uint256 votingPower = _getScreeningVotes(_grantFund, _tokenHolder1);
assertEq(votingPower, 0);
votingPower = _getScreeningVotes(_grantFund, nonVotingAddress);
assertEq(votingPower, 0);
// @audit at _startBlock + 34, _tokenHolder1's voting power becomes 25_000_000 * 1e18, which is not 0
vm.roll(_startBlock + 34);
votingPower = _getScreeningVotes(_grantFund, _tokenHolder1);
assertEq(votingPower, 25_000_000 * 1e18);
// @audit at _startBlock + 35, _tokenHolder1's voting power is still 25_000_000 * 1e18, which is not 0
vm.roll(_startBlock + 35);
votingPower = _getScreeningVotes(_grantFund, _tokenHolder1);
assertEq(votingPower, 25_000_000 * 1e18);
}
My mistake, you are correct. This is a real issue. Thank you for the report and the extra assertions!
The original report as published on Code4rena, by rbserver. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
What is known
A medium-severity business logic bug in Ajna Protocol. Code4rena made it public on 29 June 2023. Found in the 2023-05-ajna audit contest (finding M-07), also found by 2 other wardens.
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.