In Bribesol _writeVotingCheckpoint isnt called
In the Bribe contract, withdrawing votes does not reduce totalVoting and neither withdraw nor resetVoting writes a voting checkpoint, so the recorded voting totals stay stale. An attacker could rely on these incorrect totals when the contract's voting history is used for decisions.
- Disclosed after under an hour
31397 - [SC - Critical] In Bribesol _writeVotingCheckpoint isnt called ...
Submitted on May 18th 2024 at 01:42:23 UTC by @Praise for Boost | Alchemix
Report ID: #31397
Report type: Smart Contract
Report severity: Critical
Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/Bribe.sol
Impacts:
- necessary updates aren't done
Description
Brief/Intro
in Bribe.sol, _writeVotingCheckpoint() isn't called to update votingCheckpoints and votingNumCheckpoints whenever votes are withdrawn or there's a reset
Vulnerability Details
In Bribe.deposit(), whenever votes are allocated to a given guage totalVoting is updated with the amount and _writeVotingCheckpoint() is called to update votingCheckpoints and votingNumCheckpoints.
function deposit(uint256 amount, uint256 tokenId) external {
require(msg.sender == voter);
totalSupply += amount;
balanceOf[tokenId] += amount;
totalVoting += amount;
_writeCheckpoint(tokenId, balanceOf[tokenId]);
_writeSupplyCheckpoint();
_writeVotingCheckpoint();
emit Deposit(msg.sender, tokenId, amount);
}
Now the issue lies in Bribe.withdraw() & Bribe.resetVoting(), where necessary updates aren't done.
- in
Bribe.withdraw()whenever votes are withdrawn from a given guage, the withdrawn votes aren't deducted fromtotalVotingand_writeVotingCheckpoint()isn't called to updatevotingCheckpointsandvotingNumCheckpoints
function withdraw(uint256 amount, uint256 tokenId) external {
require(msg.sender == voter);
totalSupply -= amount;
balanceOf[tokenId] -= amount;
_writeCheckpoint(tokenId, balanceOf[tokenId]);
_writeSupplyCheckpoint();
emit Withdraw(msg.sender, tokenId, amount);
}
- In
Bribe.resetVoting()whentotalVotingis reset by making it 0,_writeVotingCheckpoint()isn't called to updatevotingCheckpointsandvotingNumCheckpoints
function resetVoting() external {
require(msg.sender == voter);
totalVoting = 0;
}
So whenever Bribe.withdraw() / Bribe.resetVoting() is done, record of balance checkpoints for voting period is not updated.
Impact Details
- After
Bribe.withdraw()is done, withdrawn votes doesn't reflect ontotalVoting. This is wrong - resetting of votes is never updated in
votingCheckpointsandvotingNumCheckpoints
Necessary updates aren't done after such trivial operations.
References
Proof of Concept
function testBribe_sol() public {
uint256 tokenId1 = createVeAlcx(admin, TOKEN_1, MAXTIME, false);
uint256 tokenId2 = createVeAlcx(beef, TOKEN_1, MAXTIME, false);
address bribeAddress = voter.bribes(address(sushiGauge));
// voter deposits votes to bribe.
hevm.startPrank(address(voter));
Bribe(bribeAddress).deposit(1000e18, tokenId1);
uint256 amt1 = Bribe(bribeAddress).totalVoting();
console.log("total Voting after Deposit", amt1);
// voter withdraws votes from bribe BUT it doesn't reflect in totalVoting
Bribe(bribeAddress).withdraw(1000e18, tokenId1);
uint256 amt2 = Bribe(bribeAddress).totalVoting();
console.log("total Voting after withdraw", amt2);
hevm.stopPrank();
///scenario to show totalVoting still exists in `votingCheckpoints` even after resetVoting()
hevm.startPrank(address(voter));
Bribe(bribeAddress).deposit(1000e18, tokenId1);
uint256 amt1a = Bribe(bribeAddress).totalVoting();
console.log("total Voting after Deposit", amt1a);
Bribe(bribeAddress).resetVoting();
(uint256 timeHere, uint256 amt2a) = Bribe(bribeAddress).votingCheckpoints(0);
console.log("total Voting still existing in votingCheckpoints", amt2a);
}
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 withdraw and resetVoting functions were written without mirroring the accounting and checkpoint calls that deposit performs.
Impact
The contract's totalVoting and voting checkpoint history become inaccurate after withdrawals or resets, which could mislead any logic that relies on them.
What the attacker needed
Voter role calls withdraw or resetVoting
How it was found
- The voter deposits votes into the bribe contract for a token.
- The voter withdraws the same votes from the bribe contract.
- The totalVoting value is read and still shows the deposited amount.
- The voter deposits again and then calls resetVoting.
- The voting checkpoint at index zero still contains the old total.
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 contract keeps a running total of votes and a history of that total. When someone takes votes back or resets them, the code forgets to update both the total and the history, so later readers see numbers that no longer match reality.
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.