poke() may be dos
The poke() function in a voting contract can be made to revert by ensuring one pool's calculated weight rounds down to zero, preventing the governor from updating voting weights for that NFT. This allows a malicious voter to keep outdated weights, causing some gauges to receive more rewards than they should.
bughuntoor Follow with jennifer37
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-velocimeter-judging/issues/55
Found by
bughuntoor, jennifer37
Summary
Poke() may be dos and this will cause we use the previous voting power to calculate the distribution.
Vulnerability Detail
When we enter next voting epoch, all voting powers will be expected to revote or poke their voting position with updated voting power. Considering that one NFT's voting power will decrease over time, the ve NFT owner does not have the incentive to revote if they don't want to change the voted pool. And the pool reward will be distributed with the previous weights[_pool]. In order to avoid this case, the governor can trigger poke() function to revote for the NFT owner with the same ratio of last epoch's vote.
The vulnerability is that the poke() function can be dos to prevent the revoting.
In poke(), we will calculate each pool's voting weight via _poolWeight = _weights[i] * _weight / _totalVoteWeight and add different voting weight to different pool. poke() does not allow one pool's voting weight is zero. If we can make one pool's weight to 0 in poke(), we can let poke() reverted to avoid the revote in the new epoch.
This is possible. The attack vector is like as below:
- When we first vote, we vote for several pools with different weight, we need to make sure
_poolWeightfor one pool is 1. - When it comes to the next epoch, the governor want to poke this NFT, the contract will calculate the pool's weight via
uint256 _poolWeight = _weights[i] * _weight / _totalVoteWeight;. And the_weightequalsIVotingEscrow(_ve).balanceOfNFT(_tokenId). The_weightin this epoch will be decreased compared with last epoch's voting power. The_poolWeightis probably round down to zero. Then the poke() will be reverted.
function _updateFor(address _gauge) internal {
address _pool = poolForGauge[_gauge];
uint256 _supplied = weights[_pool];
if (_supplied > 0) {
uint _supplyIndex = supplyIndex[_gauge];
uint _index = index; // get global index0 for accumulated distro
supplyIndex[_gauge] = _index; // update _gauge current position to global position
uint _delta = _index - _supplyIndex; // see if there is any difference that need to be accrued
if (_delta > 0) {
uint _share = uint(_supplied) * _delta / 1e18; // add accrued difference for each supplied token
if (isAlive[_gauge]) {
claimable[_gauge] += _share;
}
}
} else {
supplyIndex[_gauge] = index; // new users are set to the default global state
}
}
function poke(uint _tokenId) external onlyNewEpoch(_tokenId) {
require(IVotingEscrow(_ve).isApprovedOrOwner(msg.sender, _tokenId) || msg.sender == governor);
......
_vote(_tokenId, _poolVote, _weights);
}
function _vote(uint _tokenId, address[] memory _poolVote, uint256[] memory _weights) internal {
......
uint256 _weight = IVotingEscrow(_ve).balanceOfNFT(_tokenId);
uint256 _totalVoteWeight = 0;
uint256 _totalWeight = 0;
uint256 _usedWeight = 0;
console.log("Current NFT Balance is :", _weight);
for (uint i = 0; i < _poolCnt; i++) {
_totalVoteWeight += _weights[i];
}
// pool cannot repeated.
for (uint i = 0; i < _poolCnt; i++) {
// One pool, one gauge
address _pool = _poolVote[i];
address _gauge = gauges[_pool];
if (isGauge[_gauge])
{
// Cannot vote for one paused or killed gauge
require(isAlive[_gauge], "gauge already dead");
uint256 _poolWeight = _weights[i] * _weight / _totalVoteWeight;
require(votes[_tokenId][_pool] == 0);
@=> require(_poolWeight != 0, 'Pool weight is zero');
......
Poc
Add this test case into VeloVoting.t.sol, change two pool's weight ratio to make sure one pool's actual voting weight is 1. When we comes to the next epoch, the test case will be reverted.
function testCannotChangeVoteAndPokeAndResetInSameEpoch() public {
address pair = router.pairFor(address(FRAX), address(FLOW), false);
address pair1 = router.pairFor(address(FRAX), address(DAI), true);
// vote
vm.warp(block.timestamp + 1 weeks);
address[] memory pools = new address[](2);
pools[0] = address(pair);
pools[1] = address(pair1);
uint256[] memory weights = new uint256[](2);
weights[0] = 1;
weights[1] = 900000000000000000;
voter.vote(1, pools, weights);
// fwd half epoch
vm.warp(block.timestamp + 1 weeks);
// try voting again and fail
//pools[0] = address(pair2);
//vm.expectRevert(abi.encodePacked("TOKEN_ALREADY_VOTED_THIS_EPOCH"));
//voter.vote(1, pools, weights);
// try poking and fail
//vm.expectRevert(abi.encodePacked("TOKEN_ALREADY_VOTED_THIS_EPOCH"));
console.log("Try to poke");
voter.poke(1);
}
Impact
In normal case, one veNFT's voting power will decrease over time. Hackers can make use of this vulnerability to hold his veNFT's voting power and gain more rewards.
Code Snippet
Tool used
Manual Review
Recommendation
We should make sure poke() can always succeed.
Discussion
nevillehuang
request poc
Need to quantify loss to justify high severity.
Could be invalid,
Sponsor comments, does it affect claimRewards()?
poke is not required for rewards distributors as it is using snapshots and have decay callculated there
sherlock-admin4
PoC requested from @johnson37
Requests remaining: 33
johnson37
@nevillehuang , I think there is not fund loss for the protocol. It's one unfair reward distribution issue.
The key point here is that malicious users can block poke(), and then weights[_pool] cannot be updated timely and correctly. It means that weights[_pool] and totalWeight are wrong. When we try to distribute awards for different gauges, we will calculate the each pool's rewards according to each pool's voting weight. Considering the incorrect weights[_pool], some gauge(pool) may be distributed more rewards and some gauges may be distributed less rewards than expected.
function _updateFor(address _gauge) internal {
address _pool = poolForGauge[_gauge];
uint256 _supplied = weights[_pool];
if (_supplied > 0) {
// index is reward per weight
uint _supplyIndex = supplyIndex[_gauge];
uint _index = index; // get global index0 for accumulated distro
supplyIndex[_gauge] = _index; // update _gauge current position to global position
uint _delta = _index - _supplyIndex; // see if there is any difference that need to be accrued
if (_delta > 0) {
uint _share = uint(_supplied) * _delta / 1e18; // add accrued difference for each supplied token
if (isAlive[_gauge]) {
claimable[_gauge] += _share;
}
...
}
Now let me answer the sponsor's question: In one gauge, all depositors will share this gauge's whole rewards. It's correct that the rewards will be distributed according to different depositor's checkpoint. And poke() dos does not have one impact on this. However, just like what I describe as above, poke() dos will impact the weights[_pool]'s update. This will have one bad impact when all rewards in vote are distributed to different gauges.
Here is one example:
- There are two active gauges, gaugeA(poolA) and gaugeB(poolB)
- Both Alice and Bob own one veNFT token and assume these two veNFT token has the same voting power.
- Alice vote all voting power for gaugeA.
- Bob vote most of his voting power for gaugeB and vote one wei voting power for gauge A. This will prevent this veNFT poke(). This has been proved on the above poc.
- When we distribute the first time, the rewards distributed to each gauge are almost the same, because each pool's weight ratio reach nearly 50%.
- When we come to the next epoch, Alice's veNFT can be poked. Then the
weights[poolA]will decrease because veNFT's voting power decrease. However, theweights[poolB]will keep the same as the last epoch considering that poke() will be reverted. Soweights[poolB]'s weight ratio will be larger than 50%, the gaugeB will be distributed more rewards than expected.
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Velocimeter/v4-contracts/pull/21
spacegliderrrr
Fix looks good. Function now does not revert on 0 vote, but instead continues with the loop.
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 poke() function requires each pool's recalculated weight to be non-zero, but the weight can round down to zero when the NFT's voting power decreases, causing a revert.
Impact
A malicious voter can block poke() for their NFT, keeping outdated weights and causing some gauges to receive more rewards and others less than expected; the report notes there is no direct fund loss to the protocol.
What the attacker needed
A veNFT with voting power; ability to vote for multiple pools; one pool's weight rounds to zero
The fix
The fix was to change poke() so it does not revert when a pool's weight is zero, but instead continues with the loop.
How it was found
- Vote for several pools with weights such that one pool's calculated weight is exactly 1.
- Advance to the next voting epoch, where the NFT's voting power has decreased.
- Call poke() for that NFT; the pool's weight rounds down to zero and the call reverts.
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.
In this voting system, people lock tokens to get voting power that decreases over time. The contract has a function called poke() that lets someone (like the governor) refresh a vote using the current, lower voting power. The bug is that if a voter splits their vote so that one pool gets a tiny amount, the recalculation can round that amount to zero, which makes poke() fail. As a result, the old, higher voting weight stays in place and rewards are shared incorrectly.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.