Incorrect function call in BribeFactoryV3 recoverERC20AndUpdateData
The BribeFactoryV3 contract's recoverERC20AndUpdateData function calls emergencyRecoverERC20 on the Bribe contract instead of the intended recoverERC20AndUpdateData, so the tokenRewardsPerEpoch mapping is not reduced when tokens are recovered. This can lead to over-distribution of rewards, as users may claim based on outdated values and potentially drain the Bribe contract's token balance.
Egbe Follow with NexusAudits
Originally disclosed on Code4rena
Submitted by Egbe, also found by NexusAudits
Finding description
The recoverERC20AndUpdateData function in the BribeFactoryV3 contract incorrectly calls emergencyRecoverERC20 instead of recoverERC20AndUpdateData on the IBribe interface. This misnamed function call results in the failure to update the tokenRewardsPerEpoch mapping in the Bribe contract, which is critical for maintaining accurate reward accounting.
Root Cause
In BribeFactoryV3.sol, the recoverERC20AndUpdateData function is defined as follows:
function recoverERC20AndUpdateData(address[] memory _bribe, address[] memory _tokens, uint[] memory _amounts) external onlyOwner {
uint i = 0;
require(_bribe.length == _tokens.length, 'MISMATCH_LEN');
require(_tokens.length == _amounts.length, 'MISMATCH_LEN');
for(i; i < _bribe.length; i++){
if(_amounts[i] > 0) IBribe(_bribe[i]).emergencyRecoverERC20(_tokens[i], _amounts[i]);
}
}
The function calls IBribe(_bribe[i]).emergencyRecoverERC20 instead of IBribe(_bribe[i]).recoverERC20AndUpdateData. The correct function, recoverERC20AndUpdateData in the Bribe contract, updates the tokenRewardsPerEpoch mapping to reflect the recovered tokens:
function recoverERC20AndUpdateData(address tokenAddress, uint256 tokenAmount) external onlyAllowed {
require(tokenAmount <= IERC20(tokenAddress).balanceOf(address(this)), "TOO_MUCH");
uint256 _startTimestamp = IMinter(minter).active_period() + WEEK;
uint256 _lastReward = tokenRewardsPerEpoch[tokenAddress][_startTimestamp];
tokenRewardsPerEpoch[tokenAddress][_startTimestamp] = _lastReward - tokenAmount;
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}
In contrast, emergencyRecoverERC20 only transfers tokens without updating the mapping:
function emergencyRecoverERC20(address tokenAddress, uint256 tokenAmount) external onlyAllowed {
require(tokenAmount <= IERC20(tokenAddress).balanceOf(address(this)), "TOO_MUCH");
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}
Impact
Incorrect reward accounting - failing to update tokenRewardsPerEpoch can lead to over-distribution of rewards. Users may claim rewards based on outdated values, potentially draining the Bribe contract’s token balance.
Recommended mitigation steps
Update the function Call in BribeFactoryV3:
Modify the recoverERC20AndUpdateData function in BribeFactoryV3.sol to call the correct recoverERC20AndUpdateData function.
Proof of Concept
const { expect } = require("chai");
const { ethers, upgrades } = require("hardhat");
const { ZERO_ADDRESS } = require("@openzeppelin/test-helpers/src/constants.js");
describe("BribeFactoryV3 Recovery Functions", function () {
let bribeFactory;
let mockToken;
let bribe;
let owner;
let voter;
let gaugeManager;
let permissionsRegistry;
let tokenHandler;
let mockVoter;
let mockGaugeManager;
let mockTokenHandler;
let mockVe;
let mockMinter;
beforeEach(async function () {
// Get signers
[owner, voter, gaugeManager, permissionsRegistry, tokenHandler] = await ethers.getSigners();
// Deploy mock token
const MockToken = await ethers.getContractFactory("MockERC20");
mockToken = await MockToken.deploy("Mock Token", "MTK", 18);
await mockToken.deployed();
// Deploy mock Voter
const MockVoter = await ethers.getContractFactory("MockVoter");
mockVoter = await MockVoter.deploy();
await mockVoter.deployed();
// Deploy mock GaugeManager
const MockGaugeManager = await ethers.getContractFactory("MockGaugeManager");
mockGaugeManager = await MockGaugeManager.deploy();
await mockGaugeManager.deployed();
// Deploy mock TokenHandler
const MockTokenHandler = await ethers.getContractFactory("MockTokenHandler");
mockTokenHandler = await MockTokenHandler.deploy();
await mockTokenHandler.deployed();
// Deploy mock VotingEscrow
const MockVotingEscrow = await ethers.getContractFactory("MockVotingEscrow");
mockVe = await MockVotingEscrow.deploy();
await mockVe.deployed();
// Deploy mock Minter
const MockMinter = await ethers.getContractFactory("MockMinter");
mockMinter = await MockMinter.deploy();
await mockMinter.deployed();
// Set up mock Voter to return mock VE
await mockVoter.setVe(mockVe.address);
// Set up mock GaugeManager to return minter
await mockGaugeManager.setMinter(mockMinter.address);
// Set initial active period
await mockMinter.setActivePeriod(Math.floor(Date.now() / 1000));
// Deploy BribeFactoryV3 as upgradeable
const BribeFactoryV3 = await ethers.getContractFactory("BribeFactoryV3");
bribeFactory = await upgrades.deployProxy(BribeFactoryV3, [
mockVoter.address,
mockGaugeManager.address,
permissionsRegistry.address,
mockTokenHandler.address
], { initializer: 'initialize' });
await bribeFactory.deployed();
// Create a bribe contract
const tx = await bribeFactory.createBribe(
owner.address,
mockToken.address,
mockToken.address,
"test"
);
const receipt = await tx.wait();
// Get the bribe address from the factory's last_bribe variable
const bribeAddress = await bribeFactory.last_bribe();
bribe = await ethers.getContractAt("Bribe", bribeAddress);
// Add some tokens to the bribe contract
const amount = ethers.utils.parseEther("1000");
await mockToken.mint(bribeAddress, amount);
});
describe("recoverERC20AndUpdateData", function () {
it("Should demonstrate bug in recoverERC20AndUpdateData", async function () {
// Get initial token rewards per epoch
const epochStart = await bribe.getEpochStart();
const initialRewards = await bribe.tokenRewardsPerEpoch(mockToken.address, epochStart);
// Call recoverERC20AndUpdateData through the factory
const recoveryAmount = ethers.utils.parseEther("100");
await bribeFactory.recoverERC20AndUpdateData(
[bribe.address],
[mockToken.address],
[recoveryAmount]
);
// Get final token rewards per epoch
const finalRewards = await bribe.tokenRewardsPerEpoch(mockToken.address, epochStart);
// This fails because the factory uses emergencyRecoverERC20, which does not update rewards
expect(finalRewards).to.equal(initialRewards.sub(recoveryAmount),
"Token rewards per epoch should be updated but weren't");
});
});
});
Test Result: The test fails with the following error, confirming the bug:
AssertionError: Expected "0" to be equal -100000000000000000000
+ expected - actual
{
- "_hex": "-0x056bc75e2d63100000"
+ "_hex": "0x00"
"_isBigNumber": true
}
Explanation:
The test expects tokenRewardsPerEpoch to decrease by 100 ether after calling recoverERC20AndUpdateData on BribeFactoryV3. However, because emergencyRecoverERC20 is called instead, tokenRewardsPerEpoch remains unchanged (0), causing the test to fail.
This demonstrates that the incorrect function call in BribeFactoryV3 skips the critical update to tokenRewardsPerEpoch, leading to potential reward over-distribution.
Status: Mitigation confirmed. Full details in reports from rayss, lonelybones and maxvzuvex.
The sponsor team requested that the following note be included:
This issue originates from the upstream codebase, inherited from ThenaV2 fork. Given that ThenaV2 has successfully operated at scale for several months without incident, we assess the severity of this issue as low. The implementation has been effectively battle-tested in a production environment, which significantly reduces the practical risk associated with this finding. Reference: https://github.com/ThenafiBNB/THENA-Contracts/blob/main/contracts/factories/BribeFactoryV3.sol#L199
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 a copy-paste or naming mistake: the factory calls emergencyRecoverERC20 instead of recoverERC20AndUpdateData, so the reward accounting mapping is not updated.
Impact
Incorrect reward accounting could lead to over-distribution of rewards, potentially draining the Bribe contract's token balance; the sponsor notes the code is inherited from a battle-tested upstream and assesses severity as low.
What the attacker needed
Owner calls recoverERC20AndUpdateData; bribe contract holds tokens
The fix
The function call in BribeFactoryV3 should be changed to call recoverERC20AndUpdateData on the IBribe interface instead of emergencyRecoverERC20; the issue was mitigated and the mitigation confirmed.
How it was found
- Deploy the BribeFactoryV3 and create a Bribe contract with tokens.
- Record the initial tokenRewardsPerEpoch for the current epoch.
- Call recoverERC20AndUpdateData on the factory with the bribe, token, and amount.
- Observe that tokenRewardsPerEpoch remains unchanged instead of decreasing by the recovered amount.
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.
A smart contract has a function meant to recover tokens and update the record of how many rewards are owed per period. Instead, it calls a different function that only moves the tokens but leaves the reward record unchanged. This means the contract still thinks it owes the old amount, so people could claim more rewards than they should.
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.