Submitted by tapir, also found by hash and SpicyMeatball
When minting or burning a position, an attacker can take advantage of reentrancy to make the pool has more removedLiquidity than it has. This would make the owed premium to be mistakenly very high value. The issue root cause is reentrancy on an uninitialized pool in SFPM. SFPM initialize pool function explicitly makes the locked flag to false which can be leveraged for the attacker to reenter and burn the position before the mint goes through fully. This will lead to an underflow in the removedLiquidity part of the chunk. Thus, the premium owed will be calculated as a very big number since the removed liquidity is used to track the premiums owed.
Its best to explain this issue using an example due to its complexity:
First, the attacker is a contract that has a custom onERC1155Received implemented. In this case, the attacker checks for uninitialized pools in SFPM. Lets say the USDC-ETH 0.05% pool is not yet initialized. The attacker initiates a call to mintTokenizedPosition with a short type. Normally, this action would remove specific liquidity from Uniswapv3 deposited through SFPM. Attempting this without a deposit in that range would normally throw an error in this part of the code. Also, note that although the underlying uniswap pool is not registered, mintTokenizedPosition does not revert since the attacker will initialize the pool in the ERC1155 callback!
The attacker proceeds with the mint function despite expecting it to revert. The tokenIds Uniswap pool part (first 64 bits) remains uninitialized. The attacker takes advantage of this and, once the _mint is called internally in ERC1155, the attacker can execute their custom onERC1155Received in their contract.
As we can see here, before any execution on state and storage the _mint is called so attacker will has its onERC1155Received callback function called by SFPM:
https://github.com/code-423n4/2023-11-panoptic/blob/f75d07c345fd795f907385868c39bafcd6a56624/contracts/SemiFungiblePositionManager.sol#L521
Now, attacker will have the following onERC1155Received function:
The first step here is initializing the pool. This action unlocks the pools reentrancy lock, allowing re-entry into the functions for that pool link.
The second call burns the token. As this was a long position and has the burn flag, it flips the long to short link.
When reaching this part of the code, isLong equals 0, initiating an unchecked scope, resulting in an underflow of removedLiquidity, leading to an immensely large number!
Continuing with the function, these lines execute, minting liquidity using 1 ETH from the attacker, inadvertently increasing the actual liquidity without a prior deposit.
The removedLiquidity and netLiquidity are then written to storage.
Despite the attackers tokenId being burnt and nonexistent due to the onERC1155Received function execution, the process continues. Which means we can now continue with the initial mintTokenizedPosition function
Since the previous call updated removedLiquidity to an enormous number via underflow, and increasing netLiquidity with chunk liquidity, leads to no reversion in these lines.
Afterward, the position is successfully burned from Uniswap. Continuing with the function, at this point, the rightSlot holds the net liquidity from previous storage balance. Hence, we will execute the _collectAndWritePositionData function.
However, we have a problem arising in the _updateStoredPremia function which is called inside the _collectAndWritePositionData
here. The removed liquidity is used to calculate the premium owed. Due to the underflow from previous storage balance in removedLiquidity (leftSlot), the resulting premiumOwed becomes immensely high. This impacts the premium owed to be an immensely big number, returning a false statement. Furthermore, users intending to use the USDC-ETH 0.05% pool may encounter difficulties due to messed-up premiums in one range position, potentially affecting other positions relying on these premiums within the external contract.
Validate the uniswap pool ID before the _mint OR do not explicitly set the locked value to false when initializing the pool
Under/Overflow
dyedm1 (Panoptic) confirmed, but disagreed with severity and commented:
Picodes (judge) decreased severity to Medium and commented:
