Skip to content
Business logic Audit finding Intermediate

Unwhitelisting does not clear _arbitrageProfits, so re-whitelisting may result in an unfair distribution of liquidity rewards

When a pool is unwhitelisted, its accumulated arbitrage profits are not cleared, so if the pool is later re-whitelisted, those stale profits can cause an unfair distribution of liquidity rewards. An attacker could exploit this to receive a larger share of rewards than deserved.

klau5 Follow with falconhoof, Toshii and 8 others

Originally disclosed on Code4rena

Submitted by klau5, also found by falconhoof, Toshii, jasonxiale, 0xCiphky, 0xbepresent (1, 2), jesjupyter, KingNFT, pkqs90, Topmark, 0xAsen, and nonseodion

https://github.com/code-423n4/2024-01-salty/blob/aab6bbc6fe49d4dd37becc7bbd0c847ec4a7c1e6/src/dao/DAO.sol#L157-L164

https://github.com/code-423n4/2024-01-salty/blob/aab6bbc6fe49d4dd37becc7bbd0c847ec4a7c1e6/src/pools/PoolStats.sol#L51-L55

When a pool that has been excluded from the whitelist is added again, it can receive liquidity rewards based on the previous _arbitrageProfits.

Proof of Concept

When unwhitelisting, _arbitrageProfits is not cleared. When added to the whitelist again, rewards can be distributed unfairly due to the remaining _arbitrageProfits.

Let’s assume there are A/WETH, A/WBTC, B/WETH, B/WBTC, WETH/WBTC pools. A swap occurred in each of the A/WETH, B/WETH pools, generating 1 ether of arbitrage profit. If the liquidity rewards are settled at this time, each pool can receive liquidity rewards at a ratio of A/WETH: A/WBTC: B/WETH: B/WBTC: WETH/WBTC = 1: 1: 1: 1: 2. This is because they can take as much as the proportion of profits generated in each pool.

function _calculateArbitrageProfits( bytes32[] memory poolIDs, uint256[] memory _calculatedProfits ) internal view
{
    for( uint256 i = 0; i < poolIDs.length; i++ )
    {
        // references poolID(arbToken2, arbToken3) which defines the arbitage path of WETH->arbToken2->arbToken3->WETH
        bytes32 poolID = poolIDs[i];

        // Split the arbitrage profit between all the pools that contributed to generating the arbitrage for the referenced pool.
@>      uint256 arbitrageProfit = _arbitrageProfits[poolID] / 3;
        if ( arbitrageProfit > 0 )
        {
            ArbitrageIndicies memory indicies = _arbitrageIndicies[poolID];

            if ( indicies.index1 != INVALID_POOL_ID )
                _calculatedProfits[indicies.index1] += arbitrageProfit;

            if ( indicies.index2 != INVALID_POOL_ID )
                _calculatedProfits[indicies.index2] += arbitrageProfit;

            if ( indicies.index3 != INVALID_POOL_ID )
                _calculatedProfits[indicies.index3] += arbitrageProfit;
        }
    }
}

function profitsForWhitelistedPools() external view returns (uint256[] memory _calculatedProfits)
{
@>  bytes32[] memory poolIDs = poolsConfig.whitelistedPools();

    _calculatedProfits = new uint256[](poolIDs.length);
@>  _calculateArbitrageProfits( poolIDs, _calculatedProfits );
}

Suppose you unwhitelist B tokens without settling rewards. The B/WETH and B/WBTC pools will be unwhitelisted. Since they are not included in the whitelist, they cannot be involved in reward settlements, so even if the someone calls performUpkeep , their _arbitrageProfits remain the same.

function clearProfitsForPools() external
{
    require(msg.sender == address(exchangeConfig.upkeep()), "PoolStats.clearProfitsForPools is only callable from the Upkeep contract" );

@>  bytes32[] memory poolIDs = poolsConfig.whitelistedPools();

    for( uint256 i = 0; i < poolIDs.length; i++ )
        _arbitrageProfits[ poolIDs[i] ] = 0;
}

Time has passed and B token is added to the whitelist again, so that the B/WETH, B/WBTC pools are registered on the whitelist. Since the past _arbitrageProfits were not cleared, 1 ether remains in B/WETH pool’s _arbitrageProfits. The rewards for the other pools have already been settled and _arbitrageProfits has been reset.

After that, the A/WBTC pool makes new arbitrage profit of 0.1 ether. If the reward is settled in this state, they will receive rewards at a ratio of A/WETH: A/WBTC: B/WETH: B/WBTC: WETH/WBTC = 1: 1: 10: 10: 11.

This is the PoC. You can add it to the DAO.t.sol file and test it.

function testPoCArbitrageProfitsNotCleared() public
{
	vm.prank(DEPLOYER);
	salt.approve(address(liquidityRewardsEmitter), type(uint256).max);

	IERC20 token1 = new TestERC20("TEST", 18);
	IERC20 token2 = new TestERC20("TEST", 18);

	token1.transfer(DEPLOYER, 1000000 ether);
	token2.transfer(DEPLOYER, 1000000 ether);
	token1.transfer(alice, 1000000 ether);
	token2.transfer(alice, 1000000 ether);
	token1.transfer(bob, 1000000 ether);
	token2.transfer(bob, 1000000 ether);

	// --- add whitelist ---

	vm.startPrank(alice);
	staking.stakeSALT( 1000000 ether );

	salt.transfer( address(dao), 2000000 ether );

	uint256 ballotId = proposals.proposeTokenWhitelisting( token1, "", "" );
	_voteForAndFinalizeBallot(ballotId, Vote.YES);

	ballotId = proposals.proposeTokenWhitelisting( token2, "", "" );
	_voteForAndFinalizeBallot(ballotId, Vote.YES);
	vm.stopPrank();

	// --- Add liquidity ---
	vm.startPrank(DEPLOYER);
	token1.approve(address(collateralAndLiquidity), type(uint256).max);
	token2.approve(address(collateralAndLiquidity), type(uint256).max);
	weth.approve(address(collateralAndLiquidity), type(uint256).max);
	wbtc.approve(address(collateralAndLiquidity), type(uint256).max);
	// Adds token1/WETH, token1/WBTC liquidity
	collateralAndLiquidity.depositLiquidityAndIncreaseShare(token1, weth, 1000 ether, 10 ether, 0, block.timestamp, false);
	collateralAndLiquidity.depositLiquidityAndIncreaseShare(token1, wbtc, 1000 ether, 10 * 10**8, 0, block.timestamp, false);
	// Adds token2/WETH, token2/WBTC liquidity
	collateralAndLiquidity.depositLiquidityAndIncreaseShare(token2, weth, 1000 ether, 10 ether, 0, block.timestamp, false);
	collateralAndLiquidity.depositLiquidityAndIncreaseShare(token2, wbtc, 1000 ether, 10 * 10**8, 0, block.timestamp, false);
	// Adds WETH/WBTC liquidity
	collateralAndLiquidity.depositCollateralAndIncreaseShare(1000 * 10**8, 1000 ether, 0, block.timestamp, false);

	vm.stopPrank();

		// --- arbitrage profit ---
	vm.startPrank(bob);
	token1.approve(address(pools), type(uint256).max);
	token2.approve(address(pools), type(uint256).max);
	pools.depositSwapWithdraw(token1, wbtc, 1 ether, 0, block.timestamp);
	pools.depositSwapWithdraw(token2, wbtc, 1 ether, 0, block.timestamp);
	vm.stopPrank();

	uint256 token2_wbtc_index;
	uint256 token2_weth_index;

	bytes32[] memory poolIDs = poolsConfig.whitelistedPools();

	for(uint256 i = 0; i < poolIDs.length; i ++){
		if(poolIDs[i] == PoolUtils._poolID(token2, wbtc)){
			token2_wbtc_index = i;
			continue;
		}
		if(poolIDs[i] == PoolUtils._poolID(token2, weth)){
			token2_weth_index = i;
			continue;
		}
	}
	assertTrue(token2_wbtc_index != 0 && token2_weth_index != 0, "wrong index");

	uint256[] memory old_profits = pools.profitsForWhitelistedPools();

	// --- unwhitelist pool ---

	vm.startPrank(alice);
	ballotId = proposals.proposeTokenUnwhitelisting( token2, "", "" );
	_voteForAndFinalizeBallot(ballotId, Vote.YES);
	vm.stopPrank();

	// Check for the effects of the vote
	assertFalse( poolsConfig.tokenHasBeenWhitelisted(token2, wbtc, weth), "Token should not be whitelisted" );

	// user calls upkeep, the old reward cleared (but token2's reward is not cleared)
	vm.prank(address(upkeep));
	pools.clearProfitsForPools();

	// --- re-whitelist ---

	vm.startPrank(alice);
	ballotId = proposals.proposeTokenWhitelisting( token2, "", "" );
	_voteForAndFinalizeBallot(ballotId, Vote.YES);
	vm.stopPrank();

	poolIDs = poolsConfig.whitelistedPools();

	uint256 new_token2_wbtc_index;
	uint256 new_token2_weth_index;

	for(uint256 i = 0; i < poolIDs.length; i ++){
		if(poolIDs[i] == PoolUtils._poolID(token2, wbtc)){
			new_token2_wbtc_index = i;
			continue;
		}
		if(poolIDs[i] == PoolUtils._poolID(token2, weth)){
			new_token2_weth_index = i;
			continue;
		}
	}
	assertTrue(new_token2_wbtc_index != 0 && new_token2_weth_index != 0, "wrong index");

	// --- old and new reward for token2 pool are same ---
	uint256[] memory new_profits = pools.profitsForWhitelistedPools();

	assertEq(old_profits[token2_wbtc_index], new_profits[new_token2_wbtc_index], "old _arbitrageProfits remain");
	assertEq(old_profits[token2_weth_index], new_profits[new_token2_weth_index], "old _arbitrageProfits remain");

}

Recommended Mitigation Steps

Before finalizing the unwhitelist ballot, user should call performUpkeep first to force the rewards to be settled. Check if _arbitrageProfits is cleared.

function _executeApproval( Ballot memory ballot ) internal
{
    if ( ballot.ballotType == BallotType.UNWHITELIST_TOKEN )
    {
+       require(pools._arbitrageProfits(poolId_wbtc) == 0, "not cleared yet");
+       require(pools._arbitrageProfits(poolId_weth) == 0, "not cleared yet");
        // All tokens are paired with both WBTC and WETH so unwhitelist those pools
        poolsConfig.unwhitelistPool( pools, IERC20(ballot.address1), exchangeConfig.wbtc() );
        poolsConfig.unwhitelistPool( pools, IERC20(ballot.address1), exchangeConfig.weth() );

        emit UnwhitelistToken(IERC20(ballot.address1));
    }
    ...
}

othernet-global (Salty.IO) disputed and commented:

It is acceptable that an unwhitelisted pool will retain some rewards after being whitelisted again - as the period in which they were unwhitelisted they did not receive rewards that were owed to them. As the frequency of performUpkeep is sufficiently high, this behavior is acceptable.


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

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 unwhitelisting process does not clear the `_arbitrageProfits` for the removed pools, so stale profit data persists and is used again if the pool is re-whitelisted.

Impact

Liquidity rewards may be distributed unfairly, giving more rewards to re-whitelisted pools at the expense of others; the project disputed this, stating that the unwhitelisted pool did not receive rewards owed to it during that period and that frequent upkeep makes the behaviour acceptable.

What the attacker needed

Ability to create and whitelist tokens; ability to unwhitelist a token; ability to trigger upkeep

The fix

Before finalizing an unwhitelist ballot, require that the pool's `_arbitrageProfits` are cleared, for example by calling `performUpkeep` first.

How it was found

  1. Create pools and generate arbitrage profits in some of them.
  2. Unwhitelist a token without settling rewards, leaving its pools' `_arbitrageProfits` unchanged.
  3. Call upkeep to clear profits for whitelisted pools, but the unwhitelisted pools' profits remain.
  4. Re-whitelist the token, making its pools eligible for rewards again.
  5. Generate new arbitrage profit in another pool and settle rewards; the re-whitelisted pools receive rewards based on the old profits.

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, liquidity providers earn rewards based on arbitrage profits generated in their pools. When a pool is removed from the approved list (unwhitelisted), its recorded profits are not reset. If the pool is later added back, those old profits are still counted, which can give that pool more rewards than it should get.

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 klau5

View all

21 more disclosed reports, best first.

More from Salty.IO

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.