@notice Contract to handle ALCX emissions and their distriubtion
*/
contract Minter is IMinter {
using SafeERC20 for IERC20;
using SafeMath for uint256;
// Allows minting once per epoch (epoch = 2 week, reset every Thursday 00:00 UTC)
uint256 public immutable DURATION = 2 weeks;
uint256 public immutable BPS = 10_000;
/// @notice The ALCX tail emissions rate
uint256 public constant TAIL_EMISSIONS_RATE = 2194e18;
/// @notice Amount of emissions minted per epoch
uint256 public epochEmissions;
/// @notice The active period
uint256 public activePeriod;
/// @notice The stepdown rate of emissions
uint256 public stepdown;
/// @notice The current amount of ALCX rewards
uint256 public rewards;
/// @notice The current circulating supply of ALCX emissions
uint256 public supply;
/// @notice The bps of emissions going to veALCX holders
uint256 public veAlcxEmissionsRate;
/// @notice The bps of emissions going to TIME stakers
uint256 public timeEmissionsRate;
/// @notice The bps of emissions going to treasury
uint256 public treasuryEmissionsRate;
address public admin;
address public pendingAdmin;
address public initializer;
address public treasury;
IAlchemixToken public immutable alcx;
IVoter public immutable voter;
IVotingEscrow public immutable ve;
IRewardsDistributor public immutable rewardsDistributor;
IRevenueHandler public immutable revenueHandler;
IStakingRewards public immutable timeGauge;
/// @inheritdoc IMinter
function epochEmission() public view returns (uint256) {
return rewards - stepdown;
}
/// @inheritdoc IMinter
function circulatingEmissionsSupply() public view returns (uint256) {
return supply;
}
Assistant
User
fix this --> src/interfaces/synthetix/IStakingRewards.sol
Error (8936): Invalid token.
--> src/test/Minter.t.sol:393:48:
|
393 | assertEq(minter.initializer(), address(0), “initializer should be set to zero after initialization”);
| ^
Assistant
The error you're encountering points to an issue with the syntax, particularly involving the use of incorrect quotation marks. In Solidity, as in most programming languages, it's important to use straight quotation marks (") rather than curly (smart) quotes (“ and ”). Curly quotes are often introduced when editing code in text editors that automatically format them from straight quotes, like Microsoft Word or certain configured IDEs.
Here's the corrected line of code:
assertEq(minter.initializer(), address(0), "initializer should be set to zero after initialization");
Ensure that your development environment or text editor uses straight quotes to avoid such syntax errors. This is a common issue when copying and pasting code from PDFs, websites, or word processors that auto-format text.
/// @inheritdoc IMinter
function calculateEmissions(uint256 _emissions, uint256 _rate) public pure returns (uint256) {
return _emissions.mul(_rate).div(BPS);
}
Griefing (e.g. no profit motive for an attacker, but damage to the users or the protocol)
Description
Brief/Intro
The bug is arise in the second require statement of the initialize function. Instead of checking whether the initializer has already been set to address(0), it mistakenly checks if msg.sender is address(0).
Vulnerability Details
The initialize function is intended to be called only once by the initializer to perform necessary tasks. However, due to the incorrect reinitialization check, the function can be called multiple times by the initializer until the initializer is set to address(0), and this reinitialization can disrupt the contract state, potentially leading to inconsistent or unexpected behavior, and undermining the intended one-time setup process. here is the vulnerable line :
here is show that incorrectly checks msg.sender instead of initializer to prevent reinitialization.
Impact Details
The incorrect reinitialization check allows the initialize function to be called multiple times by the initializer, potentially leading to unintended reinitializations and disrupting the contract state.
References
https://github.com/alchemix-finance/alchemix-v2-dao/blob/f1007439ad3a32e412468c4c42f62f676822dc1f/src/Minter.sol#L96C1-L102C7
to fix it :
is need to Replace the incorrect check with a correct check that verifies if initializer has already been set to address(0):
function initialize() external {
require(initializer == msg.sender, "not initializer");
require(initializer != address(0), "already initialized");
initializer = address(0);
}
focnfirm the bug with evidecen ad use this test contract and give correct poc to use to confirm the bug is valid and give the real value for the poc
here is the test contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.15;
function setUp() public {
setupContracts(block.timestamp);
tokenId = createVeAlcx(admin, TOKEN_1, MAXTIME, false);
}
// Test emissions for a single epoch
function testEpochEmissions() external {
uint256 treasuryBalanceBefore = alcx.balanceOf(address(devmsig));
// Set the block timestamp to be the next epoch
hevm.warp(newEpoch());
uint256 currentTotalEmissions = minter.circulatingEmissionsSupply();
uint256 epochEmissions = minter.epochEmission();
assertEq(
minter.calculateEmissions(epochEmissions, minter.veAlcxEmissionsRate()),
(epochEmissions * minter.veAlcxEmissionsRate()) / minter.BPS(),
"incorrect veALCX emissions"
);
assertEq(
minter.calculateEmissions(epochEmissions, minter.timeEmissionsRate()),
(epochEmissions * minter.timeEmissionsRate()) / minter.BPS(),
"incorrect time emissions"
);
assertEq(
minter.calculateEmissions(epochEmissions, minter.treasuryEmissionsRate()),
(epochEmissions * minter.treasuryEmissionsRate()) / minter.BPS(),
"incorrect treasury emissions"
);
// Mint emissions for epoch
voter.distribute();
uint256 distributorBalance = alcx.balanceOf(address(distributor));
uint256 voterBalance = alcx.balanceOf(address(voter));
uint256 timeBalance = alcx.balanceOf(address(timeGauge));
uint256 treasuryBalance = (alcx.balanceOf(address(devmsig)) - treasuryBalanceBefore);
uint256 totalAfterEpoch = minter.circulatingEmissionsSupply();
emit log_named_uint("emissions after one epoch (ether)", totalAfterEpoch / TOKEN_1);
assertEq(
epochEmissions,
voterBalance + distributorBalance + timeBalance + treasuryBalance,
"incorrect emission distribution"
);
assertEq(totalAfterEpoch, currentTotalEmissions + epochEmissions);
}
// Test reaching emissions tail
function testTailEmissions() external {
// Mint emissions for the amount of epochs until tail emissions target
hevm.warp(newEpoch());
voter.distribute();
for (uint8 i = 1; i <= epochsUntilTail; ++i) {
hevm.warp(newEpoch());
voter.distribute();
}
uint256 tailRewards = minter.rewards();
uint256 tailStepdown = minter.stepdown();
uint256 tailEmissionSupply = minter.circulatingEmissionsSupply();
emit log_named_uint("tail emissions supply (ether)", tailEmissionSupply / TOKEN_1);
// Assert rewards are the constant tail emissions value
assertEq(tailRewards, minter.TAIL_EMISSIONS_RATE());
// Assert stepdown is 0 once tail emissions are reached
assertEq(tailStepdown, 0);
// Assert total emissions are the approximate target at the tail
assertApproxEq(tailEmissionSupply, supplyAtTail, 17e18);
}
function initializeVotingEscrow() public {
address[] memory claimants = new address[](1);
claimants[0] = admin;
uint256[] memory amounts = new uint256[](1);
amounts[0] = TOKEN_1;
hevm.startPrank(admin);
IERC20(bpt).approve(address(veALCX), TOKEN_1);
for (uint256 i = 0; i < claimants.length; i++) {
veALCX.createLockFor(amounts[i], MAXTIME, false, claimants[i]);
}
assertEq(veALCX.ownerOf(2), admin);
assertEq(veALCX.ownerOf(3), address(0));
hevm.roll(block.number + 1);
hevm.stopPrank();
}
// Verify the setup paramaters and emissions schedule are working as expected
function testWeeklyEmissionsSchedule() public {
initializeVotingEscrow();
uint256 startingRewards = minter.rewards();
// Rewards should equal the starting amount contract was initialized with
assertEq(startingRewards, rewards);
// After no epoch has passed, amount claimable should be 0
assertEq(distributor.claimable(tokenId), 0);
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
// After one epoch rewards amount should decrease by the defined stepdown amount
assertEq(minter.stepdown(), startingRewards - minter.rewards());
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
// After two epochs the amount of ALCX claimable for a veALCX token should increase
assertGt(distributor.claimable(tokenId), 0);
}
// Claiming rewards early should result in a penalty that is returned to veALCX holders
function testClaimRewardsEarly() public {
initializeVotingEscrow();
hevm.startPrank(admin);
// Initial balance of accounts ALCX
uint256 alcxBalanceBefore = alcx.balanceOf(admin);
// After no epoch has passed, amount claimable should be 0
assertEq(distributor.claimable(tokenId), 0, "amount claimable should be 0");
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
// Initial ALCX balance of the rewards distributor
uint256 distributorBalanceBefore = alcx.balanceOf(address(distributor));
// Total amount of rewards claimable
uint256 claimable = distributor.claimable(tokenId);
// Claim ALCX rewards early without compounding
uint256 amountClaimed = distributor.claim(tokenId, false);
// Balance after claiming ALCX rewards
uint256 alcxBalanceAfter = alcx.balanceOf(admin);
// ALCX Balance of rewards distributor after receiving fee for claiming early
uint256 distributorBalanceAfter = alcx.balanceOf(address(distributor));
// Fee for claiming rewards early
uint256 claimFee = ((claimable * veALCX.claimFeeBps()) / distributor.BPS());
// Amount claimed should be the claimable amount minus fee
assertEq(amountClaimed, claimable - claimFee, "incorrect amount claimed");
// veALCX owner balance should increase by the amount claimed
assertEq(alcxBalanceAfter - alcxBalanceBefore, amountClaimed, "unexpected account ALCX balance");
// Rewards distributor ALCX balance should increase by the fee amount
assertEq(
distributorBalanceAfter,
distributorBalanceBefore + claimFee - claimable,
"unexpected distributor ALCX balance"
);
// Amount claimable should be reset after claiming
assertEq(distributor.claimable(tokenId), 0, "amount claimable should be 0");
}
// Rewards require an epoch to pass before there is a claimable amount
// Rewards are dependent on time in epoch in which veALCX was created
function testRewardsWithinEpoch() public {
initializeVotingEscrow();
// Get a fresh epoch
hevm.warp(newEpoch());
voter.distribute();
uint256 tokenId1 = createVeAlcx(admin, TOKEN_1, MAXTIME, false);
// Fast forward 1/2 epoch
hevm.warp(block.timestamp + nextEpoch / 2);
hevm.roll(block.number + 1);
uint256 tokenId2 = createVeAlcx(beef, TOKEN_1, MAXTIME, false);
// Finish the epoch
hevm.warp(newEpoch());
voter.distribute();
// Go to the next epoch
hevm.warp(newEpoch());
voter.distribute();
uint256 claimable1 = distributor.claimable(tokenId1);
uint256 claimable2 = distributor.claimable(tokenId2);
assertGt(claimable1, claimable2, "token with earlier deposit should have more claimable rewards");
}
// Compound claiming adds ALCX rewards into their exisiting veALCX position
function testCompoundRewards() public {
initializeVotingEscrow();
hevm.startPrank(admin);
// Initial amount of BPT locked in a veALCX position
(uint256 initLockedAmount, , , ) = veALCX.locked(tokenId);
// After no epoch has passed, amount claimable should be 0
assertEq(distributor.claimable(tokenId), 0, "amount claimable should be 0");
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
(uint256 amount, ) = distributor.amountToCompound(distributor.claimable(tokenId));
// Accounts must provide proportional amount of WETH to deposit into the Balancer pool
weth.approve(address(distributor), amount);
uint256 wethBalanceBefore = weth.balanceOf(admin);
// Claim ALCX rewards and compound into exisiting veALCX position with WETH
distributor.claim(tokenId, true);
uint256 wethBalanceAfter = weth.balanceOf(admin);
// Updated amount of BPT locked
(uint256 nextLockedAmount, , , ) = veALCX.locked(tokenId);
// BPT locked should be higher after compounding
assertGt(nextLockedAmount, initLockedAmount, "error compounding");
// WETH balance should decrease by amount used to compound
assertEq(wethBalanceBefore - wethBalanceAfter, amount);
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
// Make sure account has enough eth to compound
(amount, ) = distributor.amountToCompound(distributor.claimable(tokenId));
hevm.deal(admin, amount);
// Claim ALCX rewards by providing ETH
distributor.claim{ value: amount }(tokenId, true);
assertEq(distributor.claimable(tokenId), 0, "amount claimable should be 0");
}
// Compound claiming should revert if user doesn't provide enough weth
function testCompoundRewardsFailure() public {
initializeVotingEscrow();
hevm.startPrank(admin);
assertEq(distributor.claimable(tokenId), 0, "amount claimable should be 0");
assertEq(distributor.claim(tokenId, true), 0, "amount claimed should be 0");
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
// Set weth balance to 0
weth.transfer(dead, weth.balanceOf(admin));
hevm.expectRevert(abi.encodePacked("insufficient balance to compound"));
distributor.claim(tokenId, true);
hevm.stopPrank();
hevm.expectRevert(abi.encodePacked("not approved"));
distributor.claim(tokenId, true);
}
// Compound claiming should revert if user doesn't provide enough weth
function testCompoundRewardsFailureETH() public {
deal(admin, 100 ether);
initializeVotingEscrow();
hevm.startPrank(admin);
assertEq(distributor.claimable(tokenId), 0, "amount claimable should be 0");
assertEq(distributor.claim(tokenId, true), 0, "amount claimed should be 0");
// Fast forward one epoch
hevm.warp(newEpoch());
hevm.roll(block.number + 1);
voter.distribute();
// Set weth balance to 0
weth.transfer(dead, weth.balanceOf(admin));
hevm.expectRevert(abi.encodePacked("insufficient balance to compound"));
distributor.claim(tokenId, true);
hevm.expectRevert(abi.encodePacked("Value must be 0 if not compounding"));
distributor.claim{ value: 1 ether }(tokenId, false);
distributor.claim{ value: 100 ether }(tokenId, true);
assertGt(admin.balance, 0);
assertGt(100 ether, admin.balance);
}
// Test admin controlled functions
function testAdminFunctions() public {
assertEq(minter.initializer(), address(0), "minter not initialized");
hevm.expectRevert(abi.encodePacked("not initializer"));
minter.initialize();
hevm.prank(address(0));
hevm.expectRevert(abi.encodePacked("already initialized"));
minter.initialize();
hevm.expectRevert(abi.encodePacked("not admin"));
minter.setAdmin(devmsig);
hevm.expectRevert(abi.encodePacked("not admin"));
minter.setVeAlcxEmissionsRate(1000);
hevm.expectRevert(abi.encodePacked("not voter"));
minter.updatePeriod();
hevm.prank(admin);
minter.setAdmin(devmsig);
hevm.expectRevert(abi.encodePacked("not pending admin"));
minter.acceptAdmin();
hevm.startPrank(devmsig);
minter.acceptAdmin();
hevm.expectRevert(abi.encodePacked("cannot be greater than 100%"));
minter.setVeAlcxEmissionsRate(10_000 * 2);
minter.setVeAlcxEmissionsRate(1000);
hevm.stopPrank();
}
function testSetTreasury() public {
hevm.expectRevert(abi.encodePacked("not admin"));
minter.setTreasury(beef);
hevm.prank(admin);
hevm.expectRevert(abi.encodePacked("treasury cannot be 0x0"));
minter.setTreasury(address(0));
hevm.prank(admin);
minter.setTreasury(beef);
}
}
Based on your report and the context provided, the primary concern is the incorrect reinitialization check in the initialize() function of the contract. To validate the vulnerability and demonstrate its exploitability, let's outline a suitable proof of concept (PoC) using the provided test code:
Steps to Validate the Bug
1. Write a test that triggers the bug:
This will confirm the incorrect behavior when attempting to reinitialize after initial setup.
function testMultipleInitializations() public {
// Assuming 'admin' is the person who deployed the contract and thus the 'initializer'
// First initialization should work
hevm.prank(admin); // imitate the admin
minter.initialize();
// Check if initializer is set to zero address after initialization
assertEq(minter.initializer(), address(0), "initializer should be set to zero after initialization");
// Attempt a second initialization by the same admin
hevm.expectRevert("already initialized");
minter.initialize();
}
2. Modify the test environment setup:
Ensuring that the contract interactions in the test cases are correctly sequenced based on the administrative privileges is crucial. It looks like the provided test structure sets up the context correctly where admin matches the initializer.
3. Incorporate relevant changes to the contract for proper bug manifestation:
This ensures that you are checking the initializer variable instead of msg.sender, addressing the stated issue.
4. Run the test:
Deploy the contract and execute the test to see if the bug is triggered and then fixed:
Deploy the modified contract.
Execute the test cases using the development environment, such as Truffle or Hardhat.
Considerations:
Security Measures: After modifying the contract and before running the test again, carry out a thorough review to prevent side-effects or reintroduction of old vulnerabilities.
Version Check: Ensure that the test script aligns with the same Solidlety compiler version as the contract (^0.8.15). Compiler mismatches can lead to unexpected behavior.
Chain Consistency: Test the contract on the same network type (testnet, mainnet fork etc.) as it will operate on in production to mimic actual conditions accurately.
By following these steps and considerations, you should be able to validate the bug, check its ramifications, and ensure the fix is secure and effective, demonstrating the real-world implication through testing.