Skip to content
Audit finding Advanced

PanopticFactory uses spot price when deploying new pools, resulting in liquidity manipulation when minting

The PanopticFactory contract uses the spot price from the Uniswap V3 pool when deploying a new pool, which can be manipulated via flash loans. This could cause the deployer to deposit an unbalanced amount of tokens, potentially leading to a significant loss.

DadeKuma Follow with sammy, Dup1337 and 3 others

Originally disclosed on Code4rena

Submitted by DadeKuma, also found by sammy, Dup1337, Bauchibred, jesjupyter, and Vancelot

When deployNewPool is called it uses the spot price of the pool, which can be manipulated through a flashloan and thus could return a highly inaccurate result.

The price is used when deciding how much liquidity should be minted for each token, so this can result in an unbalanced pool.

In other parts of the code, this is not an issue as there are oracles that prevent price manipulations, but in case there aren’t any checks to avoid so.

Proof of Concept

The spot price is used to calculate the range liquidity for each token:

@>	(uint160 currentSqrtPriceX96, , , , , , ) = v3Pool.slot0();

	// For full range: L = Δx * sqrt(P) = Δy / sqrt(P)
	// We start with fixed token amounts and apply this equation to calculate the liquidity
	// Note that for pools with a tickSpacing that is not a power of 2 or greater than 8 (887272 % ts != 0),
	// a position at the maximum and minimum allowable ticks will be wide, but not necessarily full-range.
	// In this case, the `fullRangeLiquidity` will always be an underestimate in respect to the token amounts required to mint.
	uint128 fullRangeLiquidity;
	unchecked {
	    // Since we know one of the tokens is WETH, we simply add 0.1 ETH + worth in tokens
	    if (token0 == WETH) {
	        fullRangeLiquidity = uint128(
@>	            Math.mulDiv96RoundingUp(FULL_RANGE_LIQUIDITY_AMOUNT_WETH, currentSqrtPriceX96)
	        );
	    } else if (token1 == WETH) {
	        fullRangeLiquidity = uint128(
	            Math.mulDivRoundingUp(
	                FULL_RANGE_LIQUIDITY_AMOUNT_WETH,
	                Constants.FP96,
@>	                currentSqrtPriceX96
	            )
	        );
	    } else {
	        // Find the resulting liquidity for providing 1e6 of both tokens
	        uint128 liquidity0 = uint128(
@>	            Math.mulDiv96RoundingUp(FULL_RANGE_LIQUIDITY_AMOUNT_TOKEN, currentSqrtPriceX96)
	        );
	        uint128 liquidity1 = uint128(
	            Math.mulDivRoundingUp(
	                FULL_RANGE_LIQUIDITY_AMOUNT_TOKEN,
	                Constants.FP96,
@>	                currentSqrtPriceX96
	            )
	        );

	        // Pick the greater of the liquidities - i.e the more "expensive" option
	        // This ensures that the liquidity added is sufficiently large
	        fullRangeLiquidity = liquidity0 > liquidity1 ? liquidity0 : liquidity1;
	    }
	}

https://github.com/code-423n4/2024-04-panoptic/blob/833312ebd600665b577fbd9c03ffa0daf250ed24/contracts/PanopticFactory.sol#L341-L374

But unlike other parts of the code, the PanopticFactory doesn’t have any checks against the price (it doesn’t use any oracles nor the TWAP), so each token liquidity is manipulable through flash loans.

Recommended Mitigation Steps

Consider using the TWAP price instead of the spot price.

Assessed type

Uniswap

dyedm1 (Panoptic) disputed and commented:

True, but I don’t see negative consequences for this? This function is just a way to add some full-range liquidity to the pool so the entire range can be swapped across, and depending on the tokens we can add very small/large amounts of liquidity anyway (mentioned in the readme: Depending on the token, the amount of funds required for the initial factory deployment may be high or unrealistic)

Picodes (judge) commented:

@dyedm1 - assuming the deployer has infinite approvals, can’t we imagine a scenario where, by manipulating the spot pool price, it ends up depositing way too many token1 and getting sandwiched leading to a significant loss?

Said differently, the risk is that currently the deployer has no control over the amount of token1 he will donate and this amount can be manipulated by an attacker.

Picodes (judge) decreased severity to Medium and commented:

This is at most Medium to me considering pool deployers are advanced users and you need to deploy a pool where the manipulation cost is low which should remain exceptional.

dyedm1 (Panoptic) commented:

Yeah I agree this might be less than ideal if you have infinite approvals. Our UI doesn’t do infinite approvals to the factory, but some wallets allow users to edit the approval amount before signing the transaction, so it might be prudent to add slippage checks here (to make the process idiot-proof).


The original report as published on Code4rena, by DadeKuma. 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 contract uses the spot price from the Uniswap V3 pool without any oracle or TWAP check, making it vulnerable to price manipulation.

Impact

An attacker could manipulate the spot price to cause the deployer to deposit an excessive amount of one token, potentially leading to a significant loss, though the judge noted this requires advanced users and low manipulation cost, making it at most Medium severity.

What the attacker needed

Deployer has infinite approvals; low manipulation cost pool; advanced user deploying pool

The fix

The recommendation is to use the TWAP price instead of the spot price, and the developer agreed that adding slippage checks would be prudent.

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.

When a new pool is created, the contract checks the current price of the tokens to decide how much liquidity to add. An attacker can temporarily change that price using a flash loan (a loan that is borrowed and repaid in the same transaction), so the contract might add too much of one token. This could make the deployer lose money.

More by DadeKuma

View all

19 more disclosed reports, best first.

More from Panoptic

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.