Skip to content
Business logic Audit finding Advanced

The fees are incorrectly updated in the `_deposit` and `_withdraw` functions, which allows the attacker to break the protocol fees

The fee accounting in the Multipool contract's deposit and withdraw functions incorrectly calculates protocol fees by subtracting the position's previously accrued fees, so an attacker can mint tiny amounts to accrue fees beforehand and cause the protocol to lose most of its fees.

duc Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-real-wagmi-judging/issues/52

Found by

duc

Summary

In _deposit and _withdraw function of Multipool contract, fee0 and fee1 are calculated from the accrued fee amounts to update feeGrowth of contract and send the protocol fee to the owner. However, these calculations are incorrect and do not align with the collected fees from the positions. It allows attacker break the protocol fees.

Vulnerability Detail

In function withdraw, all accrued fees from Uniswap V3 pool will be collected (tokensOwed0After and tokensOwed1After). However, fee0 and fee1 are updated by tokensOwed0After - tokensOwed0Before and tokensOwed1After - tokensOwed1Before.

///https://github.com/sherlock-audit/2023-06-real-wagmi/blob/main/concentrator/contracts/Multipool.sol#L509-L536
(, , , uint128 tokensOwed0Before, uint128 tokensOwed1Before) = IUniswapV3Pool(
    position.poolAddress
).positions(position.positionKey);

IUniswapV3Pool(position.poolAddress).mint(
    address(this), //recipient
    position.lowerTick,
    position.upperTick,
    liquidity,
    abi.encode(position.poolFeeAmt)
);

(, , , uint128 tokensOwed0After, uint128 tokensOwed1After) = IUniswapV3Pool(
    position.poolAddress
).positions(position.positionKey);

fee0 += tokensOwed0After - tokensOwed0Before;
fee1 += tokensOwed1After - tokensOwed1Before;

IUniswapV3Pool(position.poolAddress).collect(
    address(this),
    position.lowerTick,
    position.upperTick,
    type(uint128).max,
    type(uint128).max
);

It is incorrect because in cases where tokensOwed0Before > 0 or tokensOwed1Before > 0, fee0 and fee1 end up being smaller than the collected fees. Consequently, when _upFeesGrowth is executed, it will send smaller protocol fees than expected.

Therefore, an attacker can exploit this vulnerability and undermine the protocol fees by minting minuscule amounts to all positions within the Multipool contract. This is possible since anyone can mint for any positions in UniSwapV3 pools (the owner of position is recipient). This causes the fees of each position to be accrued beforehand, resulting in tokensOwed0Before and tokensOwed1Before becoming close to tokensOwed0After and tokensOwed1After. Then the majority of protocol fees will be lost (solvent in the reserve).

Scenario:

  1. Attacker mints minuscule amounts to all positions within the Multipool contract to accrue fees for these positions.
  2. The attacker then calls the earn() function to collect and update fees. In this scenario, tokensOwed0Before and tokensOwed1Before become equal to tokensOwed0After and tokensOwed1After (withdrawing 0 liquidity). As a result, fee0 and fee1 become 0, leading to the loss of these protocol fees.

Similar to _deposit function.

Impact

Protocol will lose its fees.

Code Snippet

https://github.com/sherlock-audit/2023-06-real-wagmi/blob/main/concentrator/contracts/Multipool.sol#L509-L536 https://github.com/sherlock-audit/2023-06-real-wagmi/blob/main/concentrator/contracts/Multipool.sol#L381-L406

Tool used

Manual review

Recommendation

fee0 and fee1 in _deposit and _withdraw function should be equal to the collected fee amounts (tokensOwed0After and tokensOwed1After)

Discussion

huuducsc

Escalate

This issue pertains to the incorrect updating of fees, resulting in a significant loss of the protocol fee. The explanation is clear enough if you have knowledge about UniswapV3. Here are my detailed explanations:

  1. The fee from the UniswapV3 pool is accumulated into the data of a position when that position is updated (by adding liquidity or removing liquidity ...). This allows anyone to mint minuscule amounts for the positions of Multipool to accrue the fee for them into position data.

https://github.com/Uniswap/v3-core/blob/main/contracts/UniswapV3Pool.sol#L442 https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/Position.sol#L79-L85 2. In the function _withdraw of the Multipool contract, tokensOwed0Before and tokensOwed1Before are the current collectible amounts of this position (the current accrued fee). amount0 and amount1 are the amount from burning liquidity.

Therefore, (tokensOwed0After - amount0) - tokensOwed0Before is only equal to the fee from the burn call (which is very small) and doesn't include the accrued fee mentioned above. However, the contract has collected all the collectible amounts of each position, which means the accrued fee has been collected but is not accumulated in fee0 and fee1. ```solidity= (, , , uint128 tokensOwed0Before, uint128 tokensOwed1Before) = IUniswapV3Pool( position.poolAddress ).positions(position.positionKey);

(uint256 amount0, uint256 amount1) = IUniswapV3Pool(position.poolAddress).burn(
  position.lowerTick,
  position.upperTick,
  liquidityToWithdraw
);

withdrawnAmount0 += amount0;
withdrawnAmount1 += amount1;

(, , , uint128 tokensOwed0After, uint128 tokensOwed1After) = IUniswapV3Pool(
    position.poolAddress
).positions(position.positionKey);

fee0 += (tokensOwed0After - amount0) - tokensOwed0Before;
fee1 += (tokensOwed1After - amount1) - tokensOwed1Before;
```
  1. The _withdraw function will trigger _upFeesGrowth function with incorrect fee0 and fee1. They don't include the accrued fee of this position before. This will result in the protocol losing a significant protocol fee.

sherlock-admin

Escalate

This issue pertains to the incorrect updating of fees, resulting in a significant loss of the protocol fee. The explanation is clear enough if you have knowledge about UniswapV3. Here are my detailed explanations:

  1. The fee from the UniswapV3 pool is accumulated into the data of a position when that position is updated (by adding liquidity or removing liquidity ...). This allows anyone to mint minuscule amounts for the positions of Multipool to accrue the fee for them into position data. https://github.com/Uniswap/v3-core/blob/main/contracts/UniswapV3Pool.sol#L442 https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/Position.sol#L79-L85
  2. In the function _withdraw of the Multipool contract, tokensOwed0Before and tokensOwed1Before are the current collectible amounts of this position (the current accrued fee). amount0 and amount1 are the amount from burning liquidity. Therefore, (tokensOwed0After - amount0) - tokensOwed0Before is only equal to the fee from the burn call (which is very small) and doesn't include the accrued fee mentioned above. However, the contract has collected all the collectible amounts of each position, which means the accrued fee has been collected but is not accumulated in fee0 and fee1.
    (, , , uint128 tokensOwed0Before, uint128 tokensOwed1Before) = IUniswapV3Pool(
      position.poolAddress
    ).positions(position.positionKey);
    
    (uint256 amount0, uint256 amount1) = IUniswapV3Pool(position.poolAddress).burn(
      position.lowerTick,
      position.upperTick,
      liquidityToWithdraw
    );
    
    withdrawnAmount0 += amount0;
    withdrawnAmount1 += amount1;
    
    (, , , uint128 tokensOwed0After, uint128 tokensOwed1After) = IUniswapV3Pool(
        position.poolAddress
    ).positions(position.positionKey);
    
    fee0 += (tokensOwed0After - amount0) - tokensOwed0Before;
    fee1 += (tokensOwed1After - amount1) - tokensOwed1Before;
    
  3. The _withdraw function will trigger _upFeesGrowth function with incorrect fee0 and fee1. They don't include the accrued fee of this position before. This will result in the protocol losing a significant protocol fee.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

ctf-sec

This seems like a medium, recommend bring sponsor for review

hrishibhat

@huuducsc Sponsor comment:

It doesn't make sense and does not matter what small position the user will mint. The Uniswap positions are common for all users. The Uniswap positions are owned by the multipool and fees will be collected on the entire liquidity of the multitool for each withdrawal or deposit.

huuducsc

@huuducsc Sponsor comment:

It doesn't make sense and does not matter what small position the user will mint. The Uniswap positions are common for all users. The Uniswap positions are owned by the multipool and fees will be collected on the entire liquidity of the multitool for each withdrawal or deposit.

I agree that fees will be collected on the entire liquidity of the multitool for each withdrawal or deposit, but it is not related to this issue. This issue is about the protocol fee, which is derived from the total fees collected from Multipool's positions, and it may be compromised. The protocol fee is only charged in the _deposit and _withdraw functions. If the fees from Multipool's positions were to be collected without using _deposit and _withdraw (for example, by adding a small amount of liquidity directly), the protocol fee would not increase during the next _deposit or _withdraw action. As a result, an attacker would be able to manipulate the protocol fee by front-running these functions.

fann95

FIXED: https://github.com/RealWagmi/concentrator/commit/9036fe43b0de2694a0c5e18cfe821d40ce17a9b8

hrishibhat

Result: Medium Unique Considering this issue a valid medium

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

0xffff11

Fixed by deleting the subtraction of tokensOwed0Before and accounting for the collected fee amounts

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

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, the fee calculation subtracts the position's previously accrued fees (tokensOwedBefore) from the total collected, so any fees accrued before the deposit or withdraw are not counted as protocol fees.

Impact

The protocol loses a significant portion of its fees, which remain in the reserve.

What the attacker needed

Attacker can mint to any Uniswap V3 position

The fix

The fee calculation should use the collected fee amounts directly, without subtracting the previously accrued fees.

How it was found

  1. Attacker mints minuscule amounts to all positions within the Multipool contract to accrue fees for these positions.
  2. The attacker then calls the earn() function to collect and update fees.
  3. In this scenario, tokensOwed0Before and tokensOwed1Before become equal to tokensOwed0After and tokensOwed1After (withdrawing 0 liquidity).
  4. As a result, fee0 and fee1 become 0, leading to the loss of these protocol fees.

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 contract collects trading fees from Uniswap and is supposed to take a cut for the protocol. But it calculates the cut incorrectly, ignoring fees that had already built up. An attacker can make those fees build up in advance, so when the contract later collects them, it thinks there are no new fees and takes nothing.

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 duc

View all

5 more disclosed reports, best first.

More from Real Wagmi #2

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.