Reward token in GaugeFactoryCL can be drained by anyone
The public createGauge function in GaugeFactoryCL lacks access control, allowing anyone to trigger the creation of a new eternal farming incentive that pulls a fixed amount of reward tokens from the factory. If the factory holds reward tokens, an attacker can repeatedly call createGauge to drain them into farms they control.
Submitted by danzero, also found by a39955720, bareli, DarkeEEandMe, Kariukigithinji, mahadev, maxzuvex, wafflewizard, wankleven, and Ziusz
Findings Description and Impact
The GaugeFactoryCL.sol contract, responsible for creating GaugeCL instances for Algebra Concentrated Liquidity pools, has a public createGauge function. Below is the implementation of the function:
function createGauge(address _rewardToken,address _ve,address _pool,address _distribution, address _internal_bribe, address _external_bribe, bool _isPair,
IGaugeManager.FarmingParam memory farmingParam, address _bonusRewardToken) external returns (address) {
createEternalFarming(_pool, farmingParam.algebraEternalFarming, _rewardToken, _bonusRewardToken);
last_gauge = address(new GaugeCL(_rewardToken,_ve,_pool,_distribution,_internal_bribe,_external_bribe,_isPair, farmingParam, _bonusRewardToken, address(this)));
__gauges.push(last_gauge);
return last_gauge;
}
The GaugeFactoryCL.createGauge function lacks access control, allowing any external actor to call it. This function, in turn, calls an internal createEternalFarming function. Below is the implementation of the createEternalFarming function:
function createEternalFarming(address _pool, address _algebraEternalFarming, address _rewardToken, address _bonusRewardToken) internal {
IAlgebraPool algebraPool = IAlgebraPool(_pool);
uint24 tickSpacing = uint24(algebraPool.tickSpacing());
address pluginAddress = algebraPool.plugin();
IncentiveKey memory incentivekey = getIncentiveKey(_rewardToken, _bonusRewardToken, _pool, _algebraEternalFarming);
uint256 remainingTimeInCurrentEpoch = BlackTimeLibrary.epochNext(block.timestamp) - block.timestamp;
uint128 reward = 1e10;
uint128 rewardRate = uint128(reward/remainingTimeInCurrentEpoch);
IERC20(_rewardToken).safeApprove(_algebraEternalFarming, reward);
address customDeployer = IAlgebraPoolAPIStorage(algebraPoolAPIStorage).pairToDeployer(_pool);
IAlgebraEternalFarming.IncentiveParams memory incentiveParams =
IAlgebraEternalFarming.IncentiveParams(reward, 0, rewardRate, 0, tickSpacing);
IAlgebraEternalFarmingCustom(_algebraEternalFarming).createEternalFarming(incentivekey, incentiveParams, pluginAddress, customDeployer);
}
It is designed to seed a new Algebra eternal farming incentive with an initial, hardcoded amount of 1e10 of the _rewardToken. It achieves this by having GaugeFactoryCL approve the algebraEternalFarming contract, which is then expected to pull these tokens from GaugeFactoryCL.
If the GaugeFactoryCL contract is pre-funded with reward tokens to facilitate this initial seeding for legitimate gauges, an attacker can repeatedly call the createGauge function which will trigger the createEternalFarming process, causing the reward token to be transferred from GaugeFactoryCL to a new Algebra farm associated with a pool specified by the attacker ultimately draining the reward token from the GaugeFactoryCL contract.
The attacker can then potentially stake a Liquidity Provider (LP) NFT into this newly created (spam) GaugeCL and its associated Algebra farm, and subsequently claim that reward.
Recommended mitigation steps
Implement robust access control on the GaugeFactoryCL.createGauge() function, restricting its execution to authorized administrators or designated smart contracts, thereby preventing unauthorized calls.
Proof of Concept
- Protocol admin pre-funds
GaugeFactoryCLwith5e10of USDC (50,000). - Attacker calls
GaugeFactoryCL.createGauge():
IGaugeFactoryCL(GFCL_ADDRESS).createGauge(
USDC_ADDRESS, // _rewardToken
VE_ADDRESS,
TARGET_POOL_ADDRESS,
ATTACKER_ADDRESS, // _distribution
ATTACKER_ADDRESS, // _internal_bribe
ATTACKER_ADDRESS, // _external_bribe
true, // _isPair
farmingParams, // including ALGEBRA_ETERNAL_FARMING_ADDRESS
ZERO_ADDRESS // _bonusRewardToken
);
- The public
createGaugefunction is entered which calls its internalcreateEternalFarming(_pool, farmingParam.algebraEternalFarming, USDC_ADDRESS, _bonusRewardToken). - Execution within
GaugeFactoryCL.createEternalFarming():
function createEternalFarming(address _pool, address _algebraEternalFarming, address _rewardToken, address _bonusRewardToken) internal {
// ...
uint128 reward = 1e10; // 10,000 USDC
// ...
// GaugeFactoryCL approves AlgebraEternalFarming to spend its USDC
IERC20(_rewardToken /* USDC_ADDRESS */).safeApprove(_algebraEternalFarming, reward);
// ...
// Call to AlgebraEternalFarming which will pull the approved USDC
IAlgebraEternalFarmingCustom(_algebraEternalFarming).createEternalFarming(incentivekey, incentiveParamsWithReward, pluginAddress, customDeployer);
}
- Execution within
AlgebraEternalFarming.createEternalFarming()here:
/// @inheritdoc IAlgebraEternalFarming
function createEternalFarming(
IncentiveKey memory key,
IncentiveParams memory params,
address plugin
) external override onlyIncentiveMaker returns (address virtualPool) {
address connectedPlugin = key.pool.plugin();
if (connectedPlugin != plugin || connectedPlugin == address(0)) revert pluginNotConnected();
if (IFarmingPlugin(connectedPlugin).incentive() != address(0)) revert anotherFarmingIsActive();
virtualPool = address(new EternalVirtualPool(address(this), connectedPlugin));
IFarmingCenter(farmingCenter).connectVirtualPoolToPlugin(virtualPool, IFarmingPlugin(connectedPlugin));
key.nonce = numOfIncentives++;
incentiveKeys[address(key.pool)] = key;
bytes32 incentiveId = IncentiveId.compute(key);
Incentive storage newIncentive = incentives[incentiveId];
(params.reward, params.bonusReward) = _receiveRewards(key, params.reward, params.bonusReward, newIncentive);
if (params.reward == 0) revert zeroRewardAmount();
unchecked {
if (int256(uint256(params.minimalPositionWidth)) > (int256(TickMath.MAX_TICK) - int256(TickMath.MIN_TICK)))
revert minimalPositionWidthTooWide();
}
newIncentive.virtualPoolAddress = virtualPool;
newIncentive.minimalPositionWidth = params.minimalPositionWidth;
newIncentive.pluginAddress = connectedPlugin;
emit EternalFarmingCreated(
key.rewardToken,
key.bonusRewardToken,
key.pool,
virtualPool,
key.nonce,
params.reward,
params.bonusReward,
params.minimalPositionWidth
);
_addRewards(IAlgebraEternalVirtualPool(virtualPool), params.reward, params.bonusReward, incentiveId);
_setRewardRates(IAlgebraEternalVirtualPool(virtualPool), params.rewardRate, params.bonusRewardRate, incentiveId);
}
- It calls the
_receiveRewardsfunction here:
function _receiveRewards(
IncentiveKey memory key,
uint128 reward,
uint128 bonusReward,
Incentive storage incentive
) internal returns (uint128 receivedReward, uint128 receivedBonusReward) {
if (!unlocked) revert reentrancyLock();
unlocked = false; // reentrancy lock
if (reward > 0) receivedReward = _receiveToken(key.rewardToken, reward);
if (bonusReward > 0) receivedBonusReward = _receiveToken(key.bonusRewardToken, bonusReward);
unlocked = true;
(uint128 _totalRewardBefore, uint128 _bonusRewardBefore) = (incentive.totalReward, incentive.bonusReward);
incentive.totalReward = _totalRewardBefore + receivedReward;
incentive.bonusReward = _bonusRewardBefore + receivedBonusReward;
}
- It calls the
_receiveTokenfunction here:
function _receiveToken(IERC20Minimal token, uint128 amount) private returns (uint128) {
uint256 balanceBefore = _getBalanceOf(token);
TransferHelper.safeTransferFrom(address(token), msg.sender, address(this), amount);
uint256 balanceAfter = _getBalanceOf(token);
require(balanceAfter > balanceBefore);
unchecked {
uint256 received = balanceAfter - balanceBefore;
if (received > type(uint128).max) revert invalidTokenAmount();
return (uint128(received));
}
}
1e10(10,000) USDC has been transferred to the new algebra farm.- Attacker repeats step 2 to 6 for 5 times to fully transfer 50,000 USDC out of the
GaugeCLFactorycontract. - Attacker stakes relevant LP NFT into the spam gauges through the
GaugeCL.depositfunction:
function deposit(uint256 tokenId) external nonReentrant isNotEmergency {
require(msg.sender == nonfungiblePositionManager.ownerOf(tokenId));
nonfungiblePositionManager.approveForFarming(tokenId, true, farmingParam.farmingCenter);
(IERC20Minimal rewardTokenAdd, IERC20Minimal bonusRewardTokenAdd, IAlgebraPool pool, uint256 nonce) =
algebraEternalFarming.incentiveKeys(poolAddress);
IncentiveKey memory incentivekey = IncentiveKey(rewardTokenAdd, bonusRewardTokenAdd, pool, nonce);
farmingCenter.enterFarming(incentivekey, tokenId);
emit Deposit(msg.sender, tokenId);
}
- Attacker directly calls the
AlgebraEternalFarming.claimRewardfunction to claim the rewards here:
/// @inheritdoc IAlgebraEternalFarming
function claimReward(IERC20Minimal rewardToken, address to, uint256 amountRequested) external override returns (uint256 reward) {
return _claimReward(rewardToken, msg.sender, to, amountRequested);
}
function _claimReward(IERC20Minimal rewardToken, address from, address to, uint256 amountRequested) internal returns (uint256 reward) {
if (to == address(0)) revert claimToZeroAddress();
mapping(IERC20Minimal => uint256) storage userRewards = rewards[from];
reward = userRewards[rewardToken];
if (amountRequested == 0 || amountRequested > reward) amountRequested = reward;
if (amountRequested > 0) {
unchecked {
userRewards[rewardToken] = reward - amountRequested;
}
TransferHelper.safeTransfer(address(rewardToken), to, amountRequested);
emit RewardClaimed(to, amountRequested, address(rewardToken), from);
}
}
Blackhole commented:
Blackhole Protocol disputes the classification of this issue as high severity, noting that the protocol is designed to deposit no more than
$0.10worth of BLACK tokens, an amount sufficient to spawn over a million liquidity pools on Blackhole.
Status: Mitigation confirmed. Full details in reports from lonelybones and rayss.
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
Impact
If the factory is pre-funded with reward tokens, an attacker could drain them; the protocol disputes the severity, stating it deposits only about $0.10 worth of BLACK tokens.
What the attacker needed
GaugeFactoryCL holds reward tokens; Attacker can call createGauge
The fix
Access control should be added to the createGauge function so only authorised administrators or designated contracts can call it.
How it was found
- The protocol admin pre-funds GaugeFactoryCL with reward tokens.
- The attacker calls createGauge with a pool and addresses they control.
- The factory approves the AlgebraEternalFarming contract to spend the reward token.
- The AlgebraEternalFarming contract pulls the approved reward tokens from the factory.
- The attacker repeats the call until the factory's reward token balance is drained.
- The attacker stakes an LP NFT into the spam gauge and claims the rewards.
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 that creates farming rewards for liquidity pools had a function that anyone could call. That function automatically sent a small amount of a reward token from the contract to a new farm. If the contract held tokens, an attacker could call the function over and over to send those tokens to farms they set up, then claim the rewards.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.