Skip to content
Business logic Audit finding Intermediate

The `_estimateWithdrawalLp` function might return a very large value, result in users losing significant incentives or being unable to withdraw from the Dispatcher contract

The `_estimateWithdrawalLp` function calculates LP shares using an average of token ratios, which can return an inflated value when the underlying Uniswap V3 pool reserves fluctuate. This can cause underflow reverts, preventing users from depositing or withdrawing, or lead to excessive LP withdrawals.

crimson-rat-reach Follow with duc and qpzm

Originally disclosed on Sherlock

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

Found by

crimson-rat-reach, duc, qpzm

Summary

The _estimateWithdrawalLp function might return a very large value, result in users losing significant incentives or being unable to withdraw from the Dispatcher contract

Vulnerability Detail

In Dispatcher contract, _estimateWithdrawalLp function returns the value of shares amount based on the average of ratios amount0 / reserve0 and amount1 / reserve1.

function _estimateWithdrawalLp(
    uint256 reserve0,
    uint256 reserve1,
    uint256 _totalSupply,
    uint256 amount0,
    uint256 amount1
) private pure returns (uint256 shareAmount) {
    shareAmount =
        ((amount0 * _totalSupply) / reserve0 + (amount1 * _totalSupply) / reserve1) /
        2;
}

From Dispatcher.withdraw and Dispatcher.deposit function, amount0 and amount1 will be the accumulated fees of users

uint256 lpAmount;
{
    (uint256 fee0, uint256 fee1) = _calcFees(feesGrow, user);
    lpAmount = _estimateWithdrawalLp(reserve0, reserve1, _totalSupply, fee0, fee1);
}
user.shares -= lpAmount;
_withdrawFee(pool, lpAmount, reserve0, reserve1, _totalSupply, deviationBP);

However, it is important to note that the values of reserve0 and reserve1 can fluctuate significantly. This is because the positions of the Multipool in UniSwapV3 pools (concentrated) are unstable on-chain, and they can change substantially as the state of the pools changes. As a result, the _estimateWithdrawalLp function might return a large value even for a small fee amount. This could potentially lead to reverting due to underflow in the deposit function (in cases where lpAmount > user.shares), or it could result in withdrawing a larger amount of Multipool LP than initially expected.

Scenario:

  1. Total supply of Multipool is 1e18, and Alice has 1e16 (1%) LP amounts which deposited into Dispatcher contract.
  2. Alice accrued fees = 200 USDC and 100 USDT
  3. The reserves of Multipool are 100,000 USDC and 100,000 USDT, _estimateWithdrawalLp of Alice fees will be (0.2% + 0.1%) / 2 * totalSupply = 0.15% * totalSupply = 1.5e15 LP amounts
  4. However, in some cases, UniSwapV3 pools may experience fluctuations, reserves of Multipool are 10,000 USDC and 190,000 USDT, _estimateWithdrawalLp of Alice fees will be (2% + 0.052%) / 2 * totalSupply = 1.026% * totalSupply = 1.026e16 LP amounts

This result is greater than LP amounts of Alice (1e16), leading to reverting by underflow in deposit/withdraw function of Dispatcher contract.

Impact

Users may face 2 potential issues when interacting with the Dispatcher contract.

  1. They might be unable to deposit/withdraw
  2. Secondly, users could potentially lose significant incentives when depositing or withdrawing due to unexpected withdrawals of LP amounts for their fees.

Code Snippet

https://github.com/sherlock-audit/2023-06-real-wagmi/blob/main/concentrator/contracts/Dispatcher.sol#L140-L150

Tool used

Manual review

Recommendation

Shouldn't use the average ratio for calculation in_estimateWithdrawalLp function

Discussion

ctf-sec

Think can change the severity to medium

huuducsc

Escalate

I believe the severity of this issue is high. There is no reason for downgrading this issue to medium, since it has high impact and high likelihood. The state of the UniswapV3 pool always changes, which causes the reserves of the Multipool contract (Multipool.getReserve()) to fluctuate significantly. The reserves of the Multipool contract are accumulated from all token amounts of each position. (code snippet). It is a fact that if the tick range of a position is short, the token amounts of this position can change significantly by 1 swap, for example, from (1e21, 0) to (0, 1e21)! This is because the amounts of each position depend on the situation of the current sqrtP (square root of the price) of this pool within the tick range of this position. Therefore, using the average calculation for _estimateWithdrawalLp function always puts users at risk of significant loss.

sherlock-admin

Escalate

I believe the severity of this issue is high. There is no reason for downgrading this issue to medium, since it has high impact and high likelihood. The state of the UniswapV3 pool always changes, which causes the reserves of the Multipool contract (Multipool.getReserve()) to fluctuate significantly. The reserves of the Multipool contract are accumulated from all token amounts of each position. (code snippet). It is a fact that if the tick range of a position is short, the token amounts of this position can change significantly by 1 swap, for example, from (1e21, 0) to (0, 1e21)! This is because the amounts of each position depend on the situation of the current sqrtP (square root of the price) of this pool within the tick range of this position. Therefore, using the average calculation for _estimateWithdrawalLp function always puts users at risk of significant loss.

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.

fann95

fixed https://github.com/RealWagmi/concentrator/blob/fbccf1caf28edbb23db8c7e0409d0c40c3a56461/contracts/Dispatcher.sol#L268 and https://github.com/RealWagmi/concentrator/blob/fbccf1caf28edbb23db8c7e0409d0c40c3a56461/contracts/Dispatcher.sol#L409C17-L409C17

JeffCX

Based on the impact

Users may face 2 potential issues when interacting with the Dispatcher contract.

They might be unable to deposit/withdraw Secondly, users could potentially lose significant incentives when depositing or withdrawing due to unexpected withdrawals of LP amounts for their fees.

All impact is medium except when user are not able to withdraw and withdraw revert in underflow,

I think a high severity is ok

hrishibhat

@huuducsc This revert happens only when there is a massive fluctuation in the pool right? These seem to be temporary or extreme cases. I think this should be a medium.

huuducsc

@huuducsc This revert happens only when there is a massive fluctuation in the pool right? These seem to be temporary or extreme cases. I think this should be a medium.

Fluctuations in positions usually occur in an UniswapV3 pool, which utilizes a concentrated price mechanism. For example, if the price of ARB is currently 1.25 USDC, and the Multipool contract holds a position in the ARB-USDC pool with a price range of [1.245, 1.255], then the ratio of tokens (token0/token1) in this position is 1:1. However, if the price of ARB increases to 1.253, the ratio of tokens in this position changes to 2:8.

hrishibhat

Result: High Has duplicates Agree with the above comments and the impact mentioned in its duplicates considering this a valid high as the price change can get user funds to be stuck forever.

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by crimson-rat-reach. 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 use of an average of token ratios to estimate LP shares, which is sensitive to large fluctuations in pool reserves.

Impact

Users may be unable to deposit or withdraw, or could lose significant incentives due to unexpected LP withdrawals.

What the attacker needed

User has deposited LP and accrued fees; significant fluctuation in pool reserves

The fix

The calculation was fixed to not use the average ratio.

How it was found

  1. Alice deposits 1% of Multipool LP into Dispatcher and accrues fees.
  2. The reserves of the Multipool fluctuate significantly due to UniswapV3 pool changes.
  3. The `_estimateWithdrawalLp` function returns a value larger than Alice's shares.
  4. A deposit or withdraw call reverts due to underflow when subtracting shares.

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 estimates how many pool shares a user should get for their fees by averaging two ratios. If the pool's reserves change a lot, this estimate can become too large, causing the contract to fail when subtracting shares, so users cannot withdraw or deposit.

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 crimson-rat-reach

View all

1 more disclosed report, 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.